Skip to content

Commit

Permalink
Prevent uncaught validation error on water example
Browse files Browse the repository at this point in the history
  • Loading branch information
cwfitzgerald committed Feb 25, 2022
1 parent 96bdf32 commit 799f030
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
22 changes: 19 additions & 3 deletions wgpu/examples/water/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ impl Example {
| wgpu::TextureUsages::RENDER_ATTACHMENT,
});

let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("Texture Sampler"),
let color_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("Color Sampler"),
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
address_mode_w: wgpu::AddressMode::ClampToEdge,
Expand All @@ -226,6 +226,11 @@ impl Example {
..Default::default()
});

let depth_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("Depth Sampler"),
..Default::default()
});

let depth_view = draw_depth_buffer.create_view(&wgpu::TextureViewDescriptor::default());

let water_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
Expand All @@ -247,7 +252,11 @@ impl Example {
},
wgpu::BindGroupEntry {
binding: 3,
resource: wgpu::BindingResource::Sampler(&sampler),
resource: wgpu::BindingResource::Sampler(&color_sampler),
},
wgpu::BindGroupEntry {
binding: 4,
resource: wgpu::BindingResource::Sampler(&depth_sampler),
},
],
label: Some("Water Bind Group"),
Expand Down Expand Up @@ -394,6 +403,13 @@ impl framework::Example for Example {
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None,
},
// Sampler to be able to sample the textures.
wgpu::BindGroupLayoutEntry {
binding: 4,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::NonFiltering),
count: None,
},
],
});

Expand Down
3 changes: 2 additions & 1 deletion wgpu/examples/water/water.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ let zFar = 400.0;
@group(0) @binding(1) var reflection: texture_2d<f32>;
@group(0) @binding(2) var terrain_depth_tex: texture_2d<f32>;
@group(0) @binding(3) var colour_sampler: sampler;
@group(0) @binding(4) var depth_sampler: sampler;

fn to_linear_depth(depth: f32) -> f32 {
let z_n = 2.0 * depth - 1.0;
Expand All @@ -238,7 +239,7 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {

let pixel_depth = to_linear_depth(in.position.z);
let normalized_coords = in.position.xy / vec2<f32>(uniforms.time_size_width.w, uniforms.viewport_height);
let terrain_depth = to_linear_depth(textureSample(terrain_depth_tex, colour_sampler, normalized_coords).r);
let terrain_depth = to_linear_depth(textureSample(terrain_depth_tex, depth_sampler, normalized_coords).r);

let dist = terrain_depth - pixel_depth;
let clamped = pow(smoothStep(0.0, 1.5, dist), 4.8);
Expand Down

0 comments on commit 799f030

Please sign in to comment.