Skip to content

Commit 3c63c0d

Browse files
committed
Move prepass functions to prepass_utils (#7354)
# Objective - The functions added to utils.wgsl by the prepass assume that mesh_view_bindings are present, which isn't always the case - Fixes #7353 ## Solution - Move these functions to their own `prepass_utils.wgsl` file Co-authored-by: IceSentry <IceSentry@users.noreply.github.com>
1 parent cf612c8 commit 3c63c0d

File tree

5 files changed

+35
-24
lines changed

5 files changed

+35
-24
lines changed

assets/shaders/show_prepass.wgsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#import bevy_pbr::mesh_types
22
#import bevy_pbr::mesh_view_bindings
3-
#import bevy_pbr::utils
3+
#import bevy_pbr::prepass_utils
44

55
@group(1) @binding(0)
66
var<uniform> show_depth: f32;

crates/bevy_core_pipeline/src/prepass/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//! it will always create a depth buffer that will be used by the main pass.
1717
//!
1818
//! When using the default mesh view bindings you should be able to use `prepass_depth()`
19-
//! and `prepass_normal()` to load the related textures. These functions are defined in `bevy_pbr::utils`.
19+
//! and `prepass_normal()` to load the related textures. These functions are defined in `bevy_pbr::prepass_utils`.
2020
//! See the `shader_prepass` example that shows how to use it.
2121
//!
2222
//! The prepass runs for each `Material`. You can control if the prepass should run per-material by setting the `prepass_enabled`

crates/bevy_pbr/src/prepass/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ pub const PREPASS_SHADER_HANDLE: HandleUntyped =
5757
pub const PREPASS_BINDINGS_SHADER_HANDLE: HandleUntyped =
5858
HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 5533152893177403494);
5959

60+
pub const PREPASS_UTILS_SHADER_HANDLE: HandleUntyped =
61+
HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 4603948296044544);
62+
6063
pub struct PrepassPlugin<M: Material>(PhantomData<M>);
6164

6265
impl<M: Material> Default for PrepassPlugin<M> {
@@ -84,6 +87,13 @@ where
8487
Shader::from_wgsl
8588
);
8689

90+
load_internal_asset!(
91+
app,
92+
PREPASS_UTILS_SHADER_HANDLE,
93+
"prepass_utils.wgsl",
94+
Shader::from_wgsl
95+
);
96+
8797
let Ok(render_app) = app.get_sub_app_mut(RenderApp) else {
8898
return;
8999
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#define_import_path bevy_pbr::prepass_utils
2+
3+
#ifndef NORMAL_PREPASS
4+
fn prepass_normal(frag_coord: vec4<f32>, sample_index: u32) -> vec3<f32> {
5+
#ifdef MULTISAMPLED
6+
let normal_sample = textureLoad(normal_prepass_texture, vec2<i32>(frag_coord.xy), i32(sample_index));
7+
#else
8+
let normal_sample = textureLoad(normal_prepass_texture, vec2<i32>(frag_coord.xy), 0);
9+
#endif
10+
return normal_sample.xyz * 2.0 - vec3(1.0);
11+
}
12+
#endif // NORMAL_PREPASS
13+
14+
#ifndef DEPTH_PREPASS
15+
fn prepass_depth(frag_coord: vec4<f32>, sample_index: u32) -> f32 {
16+
#ifdef MULTISAMPLED
17+
let depth_sample = textureLoad(depth_prepass_texture, vec2<i32>(frag_coord.xy), i32(sample_index));
18+
#else
19+
let depth_sample = textureLoad(depth_prepass_texture, vec2<i32>(frag_coord.xy), 0);
20+
#endif
21+
return depth_sample;
22+
}
23+
#endif // DEPTH_PREPASS

crates/bevy_pbr/src/render/utils.wgsl

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,3 @@ fn random1D(s: f32) -> f32 {
2525
fn coords_to_viewport_uv(position: vec2<f32>, viewport: vec4<f32>) -> vec2<f32> {
2626
return (position - viewport.xy) / viewport.zw;
2727
}
28-
29-
#ifndef NORMAL_PREPASS
30-
fn prepass_normal(frag_coord: vec4<f32>, sample_index: u32) -> vec3<f32> {
31-
#ifdef MULTISAMPLED
32-
let normal_sample = textureLoad(normal_prepass_texture, vec2<i32>(frag_coord.xy), i32(sample_index));
33-
#else
34-
let normal_sample = textureLoad(normal_prepass_texture, vec2<i32>(frag_coord.xy), 0);
35-
#endif
36-
return normal_sample.xyz * 2.0 - vec3(1.0);
37-
}
38-
#endif // NORMAL_PREPASS
39-
40-
#ifndef DEPTH_PREPASS
41-
fn prepass_depth(frag_coord: vec4<f32>, sample_index: u32) -> f32 {
42-
#ifdef MULTISAMPLED
43-
let depth_sample = textureLoad(depth_prepass_texture, vec2<i32>(frag_coord.xy), i32(sample_index));
44-
#else
45-
let depth_sample = textureLoad(depth_prepass_texture, vec2<i32>(frag_coord.xy), 0);
46-
#endif
47-
return depth_sample;
48-
}
49-
#endif // DEPTH_PREPASS

0 commit comments

Comments
 (0)