Skip to content

Commit

Permalink
Merge #64
Browse files Browse the repository at this point in the history
64: Don't create SRVs of the height for mipmapping r=kvark a=kvark

This is a follow-up to #62 that makes it friendly to Macs and older GLs.

Co-authored-by: Dzmitry Malyshau <kvarkus@gmail.com>
  • Loading branch information
bors[bot] and kvark committed Jan 6, 2019
2 parents 8dd7a5d + 4d840a5 commit 1458ace
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 56 deletions.
4 changes: 2 additions & 2 deletions data/shader/color.inc.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ float evaluate_palette(uint type, float value, float ycoord) {

vec4 evaluate_color(uint type, vec3 tex_coord, float height_normalized, float lit_factor) {
float diff =
textureOffset(t_Height, tex_coord, ivec2(1, 0)).x -
textureOffset(t_Height, tex_coord, ivec2(-1, 0)).x;
textureLodOffset(t_Height, tex_coord, 0.0, ivec2(1, 0)).x -
textureLodOffset(t_Height, tex_coord, 0.0, ivec2(-1, 0)).x;
vec3 mat = type == 0U ? vec3(5.0, 1.25, 0.5) : vec3(1.0);
float light_clr = evaluate_light(mat, diff);
float tmp = light_clr - c_HorFactor * (1.0 - height_normalized);
Expand Down
6 changes: 3 additions & 3 deletions data/shader/surface.inc.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ Surface get_surface(vec2 pos) {
}

suf.low_alt =
textureOffset(t_Height, suf.tex_coord, ivec2(-1, 0)).x
textureLodOffset(t_Height, suf.tex_coord, 0.0, ivec2(-1, 0)).x
* u_TextureScale.z;
suf.high_alt = texture(t_Height, suf.tex_coord).x * u_TextureScale.z;
suf.high_alt = textureLod(t_Height, suf.tex_coord, 0.0).x * u_TextureScale.z;
suf.delta = float(delta) * c_DeltaScale * u_TextureScale.z;
} else {
suf.high_type = suf.low_type;

suf.low_alt = texture(t_Height, tc).x * u_TextureScale.z;
suf.low_alt = textureLod(t_Height, tc, 0.0).x * u_TextureScale.z;
suf.high_alt = suf.low_alt;
suf.delta = 0.0;
}
Expand Down
26 changes: 11 additions & 15 deletions data/shader/terrain_mip.glsl
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#if GL_ARB_texture_gather
#extension GL_ARB_texture_gather : enable
#endif

varying vec3 v_TexCoord;

#ifdef SHADER_VS
Expand All @@ -19,22 +15,22 @@ void main() {

uniform sampler2DArray t_Height;

uniform c_Surface {
vec4 u_TextureScale; // XY = source size, Z = source mipmap level, W = 1
};


out float Target0;

void main() {
#if GL_ARB_texture_gather
vec4 heights = textureGather(t_Height, v_TexCoord);
#else
// we are at a pixel center, so a slight offset guarantees
// a sample from a particular neighbor in 2x2 grid around the center
float delta = 0.00001;
ivec3 tc = ivec3(u_TextureScale.xyw * v_TexCoord);
int lod = int(u_TextureScale.z);
vec4 heights = vec4(
texture(t_Height, v_TexCoord + vec3(-delta, -delta, 0.0)).r,
texture(t_Height, v_TexCoord + vec3(delta, -delta, 0.0)).r,
texture(t_Height, v_TexCoord + vec3(delta, delta, 0.0)).r,
texture(t_Height, v_TexCoord + vec3(-delta, delta, 0.0)).r
texelFetch(t_Height, tc - ivec3(0, 0, 0), lod).x,
texelFetch(t_Height, tc - ivec3(0, 1, 0), lod).x,
texelFetch(t_Height, tc - ivec3(1, 0, 0), lod).x,
texelFetch(t_Height, tc - ivec3(1, 1, 0), lod).x
);
#endif
Target0 = max(max(heights.x, heights.y), max(heights.z, heights.w));
}
#endif //FS
4 changes: 2 additions & 2 deletions data/shader/terrain_ray.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ vec3 cast_ray(vec3 point, vec3 dir) {
float affinity;
vec2 proximity = mod(cell_id, 2.0) - 0.5;
if (units.x <= units.y) {
ipos.x = cell_tl.x + (dir.x < 0.0 ? -1 : 1 << lod);
ipos.x = dir.x < 0.0 ? cell_tl.x - 1 : cell_tl.x + (1 << lod);
affinity = dir.x * proximity.x;
}
if (units.y <= units.x) {
ipos.y = cell_tl.y + (dir.y < 0.0 ? -1 : 1 << lod);
ipos.y = dir.y < 0.0 ? cell_tl.y - 1 : cell_tl.y + (1 << lod);
affinity = dir.y * proximity.y;
}
if (lod < u_Params.x && affinity > 0.0) {
Expand Down
77 changes: 43 additions & 34 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ gfx_defines!{

pipeline terrain_mip {
vbuf: gfx::VertexBuffer<TerrainVertex> = (),
suf_constants: gfx::ConstantBuffer<SurfaceConstants> = "c_Surface",
height: gfx::TextureSampler<f32> = "t_Height",
out_color: gfx::RenderTarget<HeightFormat> = "Target0",
}
Expand All @@ -150,14 +151,13 @@ enum Terrain<R: gfx::Resources> {

struct TerrainMip<R: gfx::Resources> {
target: gfx::handle::RenderTargetView<R, HeightFormat>,
resource: (gfx::handle::ShaderResourceView<R, f32>, gfx::handle::Sampler<R>),
}

struct MaxMipper<R: gfx::Resources> {
slice_size: (gfx::texture::Size, gfx::texture::Size),
pso: gfx::PipelineState<R, terrain_mip::Meta>,
data: terrain_mip::Data<R>,
mips: Vec<Vec<TerrainMip<R>>>,
vertex_buf: gfx::handle::Buffer<R, TerrainVertex>,
vertex_capacity: usize,
}

Expand Down Expand Up @@ -191,46 +191,46 @@ impl<R: gfx::Resources> MaxMipper<R> {
for slice in 0 .. num_slices {
let mut slice_mips = Vec::with_capacity(info.levels as usize);
for level in 0 .. info.levels {
//Note: gfx pre-ll doesn't actually respect the SRV level ranges...
// so we pair it with a sampler
let srv = factory
.view_texture_as_shader_resource::<HeightFormat>(
texture, (level, level), gfx::format::Swizzle::new()
)
.unwrap();
let lod = tex::Lod::from(level as f32);
let sampler = factory.create_sampler(tex::SamplerInfo {
lod_range: (lod, lod),
.. tex::SamplerInfo::new(
tex::FilterMethod::Scale,
tex::WrapMode::Tile,
)
});
slice_mips.push(TerrainMip {
target: factory
.view_texture_as_render_target(texture, level, Some(slice))
.unwrap(),
resource: (srv, sampler.clone()),
});
}
mips.push(slice_mips);
}

let srv = factory
.view_texture_as_shader_resource::<HeightFormat>(
texture, (0, info.levels - 1), gfx::format::Swizzle::new()
)
.unwrap();
let sampler = factory
.create_sampler(tex::SamplerInfo::new(
tex::FilterMethod::Mipmap,
tex::WrapMode::Tile,
));

let vertex_capacity = 256;
let (wid, het, _, _) = info.kind.get_dimensions();

MaxMipper {
slice_size: (wid, het),
pso: Self::create_pso(factory),
data: terrain_mip::Data {
vbuf: factory
.create_buffer(
vertex_capacity,
gfx::buffer::Role::Vertex,
mem::Usage::Dynamic,
mem::Bind::TRANSFER_DST,
)
.unwrap(),
suf_constants: factory.create_constant_buffer(1),
height: (srv, sampler),
out_color: mips[0][0].target.clone(),
},
mips,
vertex_buf: factory
.create_buffer(
vertex_capacity,
gfx::buffer::Role::Vertex,
mem::Usage::Dynamic,
mem::Bind::TRANSFER_DST,
)
.unwrap(),
vertex_capacity,
}
}
Expand Down Expand Up @@ -261,8 +261,6 @@ impl<R: gfx::Resources> MaxMipper<R> {
continue
}

vertices.clear();

for r in rects {
let v_abs = [
(r.x, r.y),
Expand All @@ -285,17 +283,28 @@ impl<R: gfx::Resources> MaxMipper<R> {
}

assert!(vertices.len() <= self.vertex_capacity);
encoder.update_buffer(&self.vertex_buf, &vertices, 0).unwrap();
encoder.update_buffer(&self.data.vbuf, &vertices, 0).unwrap();
let slice = gfx::Slice {
end: vertices.len() as gfx::VertexCount,
.. gfx::Slice::new_match_vertex_buffer(&self.vertex_buf)
.. gfx::Slice::new_match_vertex_buffer(&self.data.vbuf)
};

for mip in 1 .. slice_mips.len() {
for mip in 0 .. slice_mips.len() - 1 {
vertices.clear();

let suf_constants = SurfaceConstants {
tex_scale: [
(self.slice_size.0 >> mip) as f32,
(self.slice_size.1 >> mip) as f32,
mip as f32,
1.0,
],
};
encoder.update_constant_buffer(&self.data.suf_constants, &suf_constants);

encoder.draw(&slice, &self.pso, &terrain_mip::Data {
vbuf: self.vertex_buf.clone(),
height: slice_mips[mip - 1].resource.clone(),
out_color: slice_mips[mip].target.clone(),
out_color: slice_mips[mip + 1].target.clone(),
.. self.data.clone()
});
}
}
Expand Down

0 comments on commit 1458ace

Please sign in to comment.