Skip to content

Commit 6acf2c2

Browse files
torsteingrindvikmyreprise1
authored andcommitted
Refactor Globals and View structs into separate shaders (bevyengine#7512)
fixes bevyengine#6799 We should be able to reuse the `Globals` or `View` shader struct definitions from anywhere (including third party plugins) without needing to worry about defining unrelated shader defs. Also we'd like to refactor these structs to not be repeatedly defined. Refactor both `Globals` and `View` into separate importable shaders. Use the imports throughout. Co-Authored-By: Torstein Grindvik <[email protected]>
1 parent c99e540 commit 6acf2c2

File tree

10 files changed

+69
-77
lines changed

10 files changed

+69
-77
lines changed

crates/bevy_globals/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ keywords = ["bevy"]
1111
[dependencies]
1212
# # bevy
1313
bevy_app = { path = "../bevy_app", version = "0.9.0" }
14+
bevy_asset = { path = "../bevy_asset", version = "0.9.0" }
1415
bevy_core = { path = "../bevy_core", version = "0.9.0" }
1516
bevy_ecs = { path = "../bevy_ecs", version = "0.9.0" }
1617
bevy_reflect = { path = "../bevy_reflect", version = "0.9.0", features = ["bevy"] }

crates/bevy_globals/src/globals.wgsl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#define_import_path bevy_render::globals
2+
3+
struct Globals {
4+
// The time since startup in seconds
5+
// Wraps to 0 after 1 hour.
6+
time: f32,
7+
// The delta time since the previous frame in seconds
8+
delta_time: f32,
9+
// Frame count since the start of the app.
10+
// It wraps to zero when it reaches the maximum value of a u32.
11+
frame_count: u32,
12+
#ifdef SIXTEEN_BYTE_ALIGNMENT
13+
// WebGL2 structs must be 16 byte aligned.
14+
_webgl2_padding: f32
15+
#endif
16+
};

crates/bevy_globals/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
use bevy_app::{App, Plugin};
2+
use bevy_asset::{load_internal_asset, HandleUntyped};
23
use bevy_core::FrameCount;
34
use bevy_ecs::prelude::*;
4-
use bevy_reflect::Reflect;
5+
use bevy_reflect::{Reflect, TypeUuid};
56
use bevy_render::{
67
extract_resource::ExtractResource,
8+
prelude::Shader,
79
render_resource::{ShaderType, UniformBuffer},
810
renderer::{RenderDevice, RenderQueue},
911
Extract, ExtractSchedule, RenderApp, RenderSet,
1012
};
1113
use bevy_time::Time;
1214

15+
pub const GLOBALS_TYPE_HANDLE: HandleUntyped =
16+
HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 17924628719070609599);
17+
1318
#[derive(Default)]
1419
pub struct GlobalsPlugin;
1520

1621
impl Plugin for GlobalsPlugin {
1722
fn build(&self, app: &mut App) {
23+
load_internal_asset!(app, GLOBALS_TYPE_HANDLE, "globals.wgsl", Shader::from_wgsl);
1824
app.register_type::<GlobalsUniform>();
1925

2026
if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {

crates/bevy_pbr/src/render/mesh_view_types.wgsl

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
#define_import_path bevy_pbr::mesh_view_types
22

3-
struct View {
4-
view_proj: mat4x4<f32>,
5-
inverse_view_proj: mat4x4<f32>,
6-
view: mat4x4<f32>,
7-
inverse_view: mat4x4<f32>,
8-
projection: mat4x4<f32>,
9-
inverse_projection: mat4x4<f32>,
10-
world_position: vec3<f32>,
11-
// viewport(x_origin, y_origin, width, height)
12-
viewport: vec4<f32>,
13-
};
3+
#import bevy_render::view
4+
#import bevy_render::globals
145

156
struct PointLight {
167
// For point lights: the lower-right 2x2 values of the projection matrix [2][2] [2][3] [3][2] [3][3]
@@ -119,18 +110,3 @@ struct ClusterOffsetsAndCounts {
119110
data: array<vec4<u32>, 1024u>,
120111
};
121112
#endif
122-
123-
struct Globals {
124-
// The time since startup in seconds
125-
// Wraps to 0 after 1 hour.
126-
time: f32,
127-
// The delta time since the previous frame in seconds
128-
delta_time: f32,
129-
// Frame count since the start of the app.
130-
// It wraps to zero when it reaches the maximum value of a u32.
131-
frame_count: u32,
132-
#ifdef SIXTEEN_BYTE_ALIGNMENT
133-
// WebGL2 structs must be 16 byte aligned.
134-
_wasm_padding: f32
135-
#endif
136-
}

crates/bevy_render/src/globals.wgsl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#define_import_path bevy_render::globals
2+
3+
struct Globals {
4+
// The time since startup in seconds
5+
// Wraps to 0 after 1 hour.
6+
time: f32,
7+
// The delta time since the previous frame in seconds
8+
delta_time: f32,
9+
// Frame count since the start of the app.
10+
// It wraps to zero when it reaches the maximum value of a u32.
11+
frame_count: u32,
12+
#ifdef SIXTEEN_BYTE_ALIGNMENT
13+
// WebGL2 structs must be 16 byte aligned.
14+
_webgl2_padding: f32
15+
#endif
16+
};

crates/bevy_render/src/view/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
pub mod visibility;
22
pub mod window;
33

4+
use bevy_asset::{load_internal_asset, HandleUntyped};
45
pub use visibility::*;
56
pub use window::*;
67

78
use crate::{
89
camera::ExtractedCamera,
910
extract_resource::{ExtractResource, ExtractResourcePlugin},
10-
prelude::Image,
11+
prelude::{Image, Shader},
1112
render_asset::RenderAssets,
1213
render_phase::ViewRangefinder3d,
1314
render_resource::{DynamicUniformBuffer, ShaderType, Texture, TextureView},
@@ -18,7 +19,7 @@ use crate::{
1819
use bevy_app::{App, Plugin};
1920
use bevy_ecs::prelude::*;
2021
use bevy_math::{Mat4, UVec4, Vec3, Vec4};
21-
use bevy_reflect::Reflect;
22+
use bevy_reflect::{Reflect, TypeUuid};
2223
use bevy_transform::components::GlobalTransform;
2324
use bevy_utils::HashMap;
2425
use std::sync::atomic::{AtomicUsize, Ordering};
@@ -27,10 +28,15 @@ use wgpu::{
2728
TextureFormat, TextureUsages,
2829
};
2930

31+
pub const VIEW_TYPE_HANDLE: HandleUntyped =
32+
HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 15421373904451797197);
33+
3034
pub struct ViewPlugin;
3135

3236
impl Plugin for ViewPlugin {
3337
fn build(&self, app: &mut App) {
38+
load_internal_asset!(app, VIEW_TYPE_HANDLE, "view.wgsl", Shader::from_wgsl);
39+
3440
app.register_type::<ComputedVisibility>()
3541
.register_type::<ComputedVisibilityFlags>()
3642
.register_type::<Msaa>()

crates/bevy_render/src/view/view.wgsl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#define_import_path bevy_render::view
2+
3+
struct View {
4+
view_proj: mat4x4<f32>,
5+
inverse_view_proj: mat4x4<f32>,
6+
view: mat4x4<f32>,
7+
inverse_view: mat4x4<f32>,
8+
projection: mat4x4<f32>,
9+
inverse_projection: mat4x4<f32>,
10+
world_position: vec3<f32>,
11+
// viewport(x_origin, y_origin, width, height)
12+
viewport: vec4<f32>,
13+
};
Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,4 @@
11
#define_import_path bevy_sprite::mesh2d_view_types
22

3-
struct View {
4-
view_proj: mat4x4<f32>,
5-
inverse_view_proj: mat4x4<f32>,
6-
view: mat4x4<f32>,
7-
inverse_view: mat4x4<f32>,
8-
projection: mat4x4<f32>,
9-
inverse_projection: mat4x4<f32>,
10-
world_position: vec3<f32>,
11-
// viewport(x_origin, y_origin, width, height)
12-
viewport: vec4<f32>,
13-
};
14-
15-
struct Globals {
16-
// The time since startup in seconds
17-
// Wraps to 0 after 1 hour.
18-
time: f32,
19-
// The delta time since the previous frame in seconds
20-
delta_time: f32,
21-
// Frame count since the start of the app.
22-
// It wraps to zero when it reaches the maximum value of a u32.
23-
frame_count: u32,
24-
#ifdef SIXTEEN_BYTE_ALIGNMENT
25-
// WebGL2 structs must be 16 byte aligned.
26-
_wasm_padding: f32
27-
#endif
28-
}
3+
#import bevy_render::view
4+
#import bevy_render::globals

crates/bevy_sprite/src/render/sprite.wgsl

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,8 @@
22
#import bevy_core_pipeline::tonemapping
33
#endif
44

5-
struct View {
6-
view_proj: mat4x4<f32>,
7-
inverse_view_proj: mat4x4<f32>,
8-
view: mat4x4<f32>,
9-
inverse_view: mat4x4<f32>,
10-
projection: mat4x4<f32>,
11-
inverse_projection: mat4x4<f32>,
12-
world_position: vec3<f32>,
13-
// viewport(x_origin, y_origin, width, height)
14-
viewport: vec4<f32>,
15-
};
5+
#import bevy_render::view
6+
167
@group(0) @binding(0)
178
var<uniform> view: View;
189

crates/bevy_ui/src/render/ui.wgsl

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1-
struct View {
2-
view_proj: mat4x4<f32>,
3-
inverse_view_proj: mat4x4<f32>,
4-
view: mat4x4<f32>,
5-
inverse_view: mat4x4<f32>,
6-
projection: mat4x4<f32>,
7-
inverse_projection: mat4x4<f32>,
8-
world_position: vec3<f32>,
9-
// viewport(x_origin, y_origin, width, height)
10-
viewport: vec4<f32>,
11-
};
1+
#import bevy_render::view
2+
123
@group(0) @binding(0)
134
var<uniform> view: View;
145

0 commit comments

Comments
 (0)