Skip to content

Commit

Permalink
pbr.wgsl: Move discard after textureSample
Browse files Browse the repository at this point in the history
In order for texture sampling to be able to do mipmapping, it must be
done uniformly across a pixel quad. As such, the discard must come after
all textureSample calls. Note that one can use textureSampleLevel to
remove the requirement for uniformity, but then one has to supply a
specific mip level.
  • Loading branch information
superdump committed Nov 13, 2021
1 parent 931dac9 commit 22b3852
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions pipelined/bevy_pbr2/src/render/pbr.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -535,20 +535,6 @@ fn fragment(in: FragmentInput) -> [[location(0)]] vec4<f32> {
occlusion = textureSample(occlusion_texture, occlusion_sampler, in.uv).r;
}

if ((material.flags & STANDARD_MATERIAL_FLAGS_ALPHA_MODE_OPAQUE) != 0u) {
// NOTE: If rendering as opaque, alpha should be ignored so set to 1.0
output_color.a = 1.0;
} elseif ((material.flags & STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK) != 0u) {
if (output_color.a >= material.alpha_cutoff) {
// NOTE: If rendering as masked alpha and >= the cutoff, render as fully opaque
output_color.a = 1.0;
} else {
// NOTE: output_color.a < material.alpha_cutoff should not is not rendered
// NOTE: This and any other discards mean that early-z testing cannot be done!
discard;
}
}

var N: vec3<f32> = normalize(in.world_normal);

#ifdef VERTEX_TANGENTS
Expand All @@ -575,7 +561,20 @@ fn fragment(in: FragmentInput) -> [[location(0)]] vec4<f32> {
let TBN = mat3x3<f32>(T, B, N);
N = TBN * normalize(textureSample(normal_map_texture, normal_map_sampler, in.uv).rgb * 2.0 - 1.0);
#endif
#endif

if ((material.flags & STANDARD_MATERIAL_FLAGS_ALPHA_MODE_OPAQUE) != 0u) {
// NOTE: If rendering as opaque, alpha should be ignored so set to 1.0
output_color.a = 1.0;
} elseif ((material.flags & STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK) != 0u) {
if (output_color.a >= material.alpha_cutoff) {
// NOTE: If rendering as masked alpha and >= the cutoff, render as fully opaque
output_color.a = 1.0;
} else {
// NOTE: output_color.a < material.alpha_cutoff should not is not rendered
// NOTE: This and any other discards mean that early-z testing cannot be done!
discard;
}
}

var V: vec3<f32>;
if (view.projection.w.w != 1.0) { // If the projection is not orthographic
Expand Down

0 comments on commit 22b3852

Please sign in to comment.