Skip to content

Commit 22b3852

Browse files
committed
pbr.wgsl: Move discard after textureSample
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.
1 parent 931dac9 commit 22b3852

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

pipelined/bevy_pbr2/src/render/pbr.wgsl

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -535,20 +535,6 @@ fn fragment(in: FragmentInput) -> [[location(0)]] vec4<f32> {
535535
occlusion = textureSample(occlusion_texture, occlusion_sampler, in.uv).r;
536536
}
537537

538-
if ((material.flags & STANDARD_MATERIAL_FLAGS_ALPHA_MODE_OPAQUE) != 0u) {
539-
// NOTE: If rendering as opaque, alpha should be ignored so set to 1.0
540-
output_color.a = 1.0;
541-
} elseif ((material.flags & STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK) != 0u) {
542-
if (output_color.a >= material.alpha_cutoff) {
543-
// NOTE: If rendering as masked alpha and >= the cutoff, render as fully opaque
544-
output_color.a = 1.0;
545-
} else {
546-
// NOTE: output_color.a < material.alpha_cutoff should not is not rendered
547-
// NOTE: This and any other discards mean that early-z testing cannot be done!
548-
discard;
549-
}
550-
}
551-
552538
var N: vec3<f32> = normalize(in.world_normal);
553539

554540
#ifdef VERTEX_TANGENTS
@@ -575,7 +561,20 @@ fn fragment(in: FragmentInput) -> [[location(0)]] vec4<f32> {
575561
let TBN = mat3x3<f32>(T, B, N);
576562
N = TBN * normalize(textureSample(normal_map_texture, normal_map_sampler, in.uv).rgb * 2.0 - 1.0);
577563
#endif
578-
#endif
564+
565+
if ((material.flags & STANDARD_MATERIAL_FLAGS_ALPHA_MODE_OPAQUE) != 0u) {
566+
// NOTE: If rendering as opaque, alpha should be ignored so set to 1.0
567+
output_color.a = 1.0;
568+
} elseif ((material.flags & STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK) != 0u) {
569+
if (output_color.a >= material.alpha_cutoff) {
570+
// NOTE: If rendering as masked alpha and >= the cutoff, render as fully opaque
571+
output_color.a = 1.0;
572+
} else {
573+
// NOTE: output_color.a < material.alpha_cutoff should not is not rendered
574+
// NOTE: This and any other discards mean that early-z testing cannot be done!
575+
discard;
576+
}
577+
}
579578

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

0 commit comments

Comments
 (0)