Add capabilities-based depth texture ifdef guards

This commit is contained in:
Marco Buono 2023-06-19 18:47:39 -03:00
parent 6573485dcd
commit fa57d38062
5 changed files with 27 additions and 15 deletions

View file

@ -24,7 +24,11 @@ fn fragment(
let sample_index = 0u;
#endif
if settings.show_depth == 1u {
#ifdef PREPASS_DEPTH_SUPPORTED
let depth = prepass_depth(frag_coord, sample_index);
#else
let depth = 0.0;
#endif
return vec4(depth, depth, depth, 1.0);
} else if settings.show_normals == 1u {
let normal = prepass_normal(frag_coord, sample_index);

View file

@ -1,15 +1,25 @@
#define_import_path bevy_pbr::prepass_utils
#ifdef PREPASS_TEXTURES
#ifndef DEPTH_PREPASS
fn prepass_depth(frag_coord: vec4<f32>, sample_index: u32) -> f32 {
#ifdef DEPTH_TEXTURE_LOAD_SUPPORTED
#ifdef MULTISAMPLED
let depth_sample = textureLoad(depth_prepass_texture, vec2<i32>(frag_coord.xy), i32(sample_index));
#else
let depth_sample = textureLoad(depth_prepass_texture, vec2<i32>(frag_coord.xy), 0);
#endif
return depth_sample;
#ifdef DEPTH_TEXTURE_MULTISAMPLED_SUPPORTED
#define PREPASS_DEPTH_SUPPORTED
fn prepass_depth(frag_coord: vec4<f32>, sample_index: u32) -> f32 {
return textureLoad(depth_prepass_texture, vec2<i32>(frag_coord.xy), i32(sample_index));
}
#endif // DEPTH_TEXTURE_MULTISAMPLED_SUPPORTED
#else // MULTISAMPLED
#define PREPASS_DEPTH_SUPPORTED
fn prepass_depth(frag_coord: vec4<f32>, sample_index: u32) -> f32 {
return textureLoad(depth_prepass_texture, vec2<i32>(frag_coord.xy), 0);
}
#endif // MULTISAMPLED
#endif // DEPTH_TEXTURE_LOAD_SUPPORTED
#endif // DEPTH_PREPASS
#ifndef NORMAL_PREPASS
@ -33,4 +43,3 @@ fn prepass_motion_vector(frag_coord: vec4<f32>, sample_index: u32) -> vec2<f32>
return motion_vector_sample.rg;
}
#endif // MOTION_VECTOR_PREPASS
#endif // PREPASS_TEXTURES

View file

@ -742,10 +742,9 @@ impl SpecializedMeshPipeline for MeshPipeline {
vertex_attributes.push(Mesh::ATTRIBUTE_COLOR.at_shader_location(4));
}
if cfg!(any(not(feature = "webgl"), not(target_arch = "wasm32")))
|| (cfg!(all(feature = "webgl", target_arch = "wasm32")) && key.msaa_samples() == 1)
{
shader_defs.push("PREPASS_TEXTURES".into());
if cfg!(any(not(feature = "webgl"), not(target_arch = "wasm32"))) {
shader_defs.push("DEPTH_TEXTURE_LOAD_SUPPORTED".into());
shader_defs.push("DEPTH_TEXTURE_MULTISAMPLED_SUPPORTED".into());
}
let mut bind_group_layout = match key.msaa_samples() {

View file

@ -58,10 +58,11 @@ var dt_lut_texture: texture_3d<f32>;
@group(0) @binding(15)
var dt_lut_sampler: sampler;
#ifdef PREPASS_TEXTURES
#ifdef MULTISAMPLED
#ifdef DEPTH_TEXTURE_MULTISAMPLED_SUPPORTED
@group(0) @binding(16)
var depth_prepass_texture: texture_depth_multisampled_2d;
#endif // DEPTH_TEXTURE_MULTISAMPLED_SUPPORTED
@group(0) @binding(17)
var normal_prepass_texture: texture_multisampled_2d<f32>;
@group(0) @binding(18)
@ -74,7 +75,6 @@ var normal_prepass_texture: texture_2d<f32>;
@group(0) @binding(18)
var motion_vector_prepass_texture: texture_2d<f32>;
#endif // MULTISAMPLED
#endif // PREPASS_TEXTURES
@group(0) @binding(19)
var view_transmission_texture: texture_2d<f32>;

View file

@ -402,7 +402,7 @@ fn fetch_transmissive_background(offset_position: vec2<f32>, frag_coord: vec3<f3
// Calucalte final offset position, with blur and spiral offset
let modified_offset_position = offset_position + rotated_spiral_offset * blur_intensity * (1.0 - f32(pixel_mesh) * 0.1);
#ifdef PREPASS_TEXTURES
#ifdef PREPASS_DEPTH_SUPPORTED
// Use depth prepass data to reject values that are in front of the current fragment
if (prepass_depth(vec4<f32>(modified_offset_position * view.viewport.zw, 0.0, 0.0), 0u) > frag_coord.z) {
continue;