Skip to content

Commit

Permalink
Refactor Globals and View structs into separate shaders (bevyengine#7512
Browse files Browse the repository at this point in the history
)

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 <52322338+torsteingrindvik@users.noreply.github.com>
  • Loading branch information
2 people authored and myreprise1 committed Feb 15, 2023
1 parent c99e540 commit 6acf2c2
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 77 deletions.
1 change: 1 addition & 0 deletions crates/bevy_globals/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ keywords = ["bevy"]
[dependencies]
# # bevy
bevy_app = { path = "../bevy_app", version = "0.9.0" }
bevy_asset = { path = "../bevy_asset", version = "0.9.0" }
bevy_core = { path = "../bevy_core", version = "0.9.0" }
bevy_ecs = { path = "../bevy_ecs", version = "0.9.0" }
bevy_reflect = { path = "../bevy_reflect", version = "0.9.0", features = ["bevy"] }
Expand Down
16 changes: 16 additions & 0 deletions crates/bevy_globals/src/globals.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#define_import_path bevy_render::globals

struct Globals {
// The time since startup in seconds
// Wraps to 0 after 1 hour.
time: f32,
// The delta time since the previous frame in seconds
delta_time: f32,
// Frame count since the start of the app.
// It wraps to zero when it reaches the maximum value of a u32.
frame_count: u32,
#ifdef SIXTEEN_BYTE_ALIGNMENT
// WebGL2 structs must be 16 byte aligned.
_webgl2_padding: f32
#endif
};
8 changes: 7 additions & 1 deletion crates/bevy_globals/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
use bevy_app::{App, Plugin};
use bevy_asset::{load_internal_asset, HandleUntyped};
use bevy_core::FrameCount;
use bevy_ecs::prelude::*;
use bevy_reflect::Reflect;
use bevy_reflect::{Reflect, TypeUuid};
use bevy_render::{
extract_resource::ExtractResource,
prelude::Shader,
render_resource::{ShaderType, UniformBuffer},
renderer::{RenderDevice, RenderQueue},
Extract, ExtractSchedule, RenderApp, RenderSet,
};
use bevy_time::Time;

pub const GLOBALS_TYPE_HANDLE: HandleUntyped =
HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 17924628719070609599);

#[derive(Default)]
pub struct GlobalsPlugin;

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

if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
Expand Down
28 changes: 2 additions & 26 deletions crates/bevy_pbr/src/render/mesh_view_types.wgsl
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
#define_import_path bevy_pbr::mesh_view_types

struct View {
view_proj: mat4x4<f32>,
inverse_view_proj: mat4x4<f32>,
view: mat4x4<f32>,
inverse_view: mat4x4<f32>,
projection: mat4x4<f32>,
inverse_projection: mat4x4<f32>,
world_position: vec3<f32>,
// viewport(x_origin, y_origin, width, height)
viewport: vec4<f32>,
};
#import bevy_render::view
#import bevy_render::globals

struct PointLight {
// For point lights: the lower-right 2x2 values of the projection matrix [2][2] [2][3] [3][2] [3][3]
Expand Down Expand Up @@ -119,18 +110,3 @@ struct ClusterOffsetsAndCounts {
data: array<vec4<u32>, 1024u>,
};
#endif

struct Globals {
// The time since startup in seconds
// Wraps to 0 after 1 hour.
time: f32,
// The delta time since the previous frame in seconds
delta_time: f32,
// Frame count since the start of the app.
// It wraps to zero when it reaches the maximum value of a u32.
frame_count: u32,
#ifdef SIXTEEN_BYTE_ALIGNMENT
// WebGL2 structs must be 16 byte aligned.
_wasm_padding: f32
#endif
}
16 changes: 16 additions & 0 deletions crates/bevy_render/src/globals.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#define_import_path bevy_render::globals

struct Globals {
// The time since startup in seconds
// Wraps to 0 after 1 hour.
time: f32,
// The delta time since the previous frame in seconds
delta_time: f32,
// Frame count since the start of the app.
// It wraps to zero when it reaches the maximum value of a u32.
frame_count: u32,
#ifdef SIXTEEN_BYTE_ALIGNMENT
// WebGL2 structs must be 16 byte aligned.
_webgl2_padding: f32
#endif
};
10 changes: 8 additions & 2 deletions crates/bevy_render/src/view/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
pub mod visibility;
pub mod window;

use bevy_asset::{load_internal_asset, HandleUntyped};
pub use visibility::*;
pub use window::*;

use crate::{
camera::ExtractedCamera,
extract_resource::{ExtractResource, ExtractResourcePlugin},
prelude::Image,
prelude::{Image, Shader},
render_asset::RenderAssets,
render_phase::ViewRangefinder3d,
render_resource::{DynamicUniformBuffer, ShaderType, Texture, TextureView},
Expand All @@ -18,7 +19,7 @@ use crate::{
use bevy_app::{App, Plugin};
use bevy_ecs::prelude::*;
use bevy_math::{Mat4, UVec4, Vec3, Vec4};
use bevy_reflect::Reflect;
use bevy_reflect::{Reflect, TypeUuid};
use bevy_transform::components::GlobalTransform;
use bevy_utils::HashMap;
use std::sync::atomic::{AtomicUsize, Ordering};
Expand All @@ -27,10 +28,15 @@ use wgpu::{
TextureFormat, TextureUsages,
};

pub const VIEW_TYPE_HANDLE: HandleUntyped =
HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 15421373904451797197);

pub struct ViewPlugin;

impl Plugin for ViewPlugin {
fn build(&self, app: &mut App) {
load_internal_asset!(app, VIEW_TYPE_HANDLE, "view.wgsl", Shader::from_wgsl);

app.register_type::<ComputedVisibility>()
.register_type::<ComputedVisibilityFlags>()
.register_type::<Msaa>()
Expand Down
13 changes: 13 additions & 0 deletions crates/bevy_render/src/view/view.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#define_import_path bevy_render::view

struct View {
view_proj: mat4x4<f32>,
inverse_view_proj: mat4x4<f32>,
view: mat4x4<f32>,
inverse_view: mat4x4<f32>,
projection: mat4x4<f32>,
inverse_projection: mat4x4<f32>,
world_position: vec3<f32>,
// viewport(x_origin, y_origin, width, height)
viewport: vec4<f32>,
};
28 changes: 2 additions & 26 deletions crates/bevy_sprite/src/mesh2d/mesh2d_view_types.wgsl
Original file line number Diff line number Diff line change
@@ -1,28 +1,4 @@
#define_import_path bevy_sprite::mesh2d_view_types

struct View {
view_proj: mat4x4<f32>,
inverse_view_proj: mat4x4<f32>,
view: mat4x4<f32>,
inverse_view: mat4x4<f32>,
projection: mat4x4<f32>,
inverse_projection: mat4x4<f32>,
world_position: vec3<f32>,
// viewport(x_origin, y_origin, width, height)
viewport: vec4<f32>,
};

struct Globals {
// The time since startup in seconds
// Wraps to 0 after 1 hour.
time: f32,
// The delta time since the previous frame in seconds
delta_time: f32,
// Frame count since the start of the app.
// It wraps to zero when it reaches the maximum value of a u32.
frame_count: u32,
#ifdef SIXTEEN_BYTE_ALIGNMENT
// WebGL2 structs must be 16 byte aligned.
_wasm_padding: f32
#endif
}
#import bevy_render::view
#import bevy_render::globals
13 changes: 2 additions & 11 deletions crates/bevy_sprite/src/render/sprite.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,8 @@
#import bevy_core_pipeline::tonemapping
#endif

struct View {
view_proj: mat4x4<f32>,
inverse_view_proj: mat4x4<f32>,
view: mat4x4<f32>,
inverse_view: mat4x4<f32>,
projection: mat4x4<f32>,
inverse_projection: mat4x4<f32>,
world_position: vec3<f32>,
// viewport(x_origin, y_origin, width, height)
viewport: vec4<f32>,
};
#import bevy_render::view

@group(0) @binding(0)
var<uniform> view: View;

Expand Down
13 changes: 2 additions & 11 deletions crates/bevy_ui/src/render/ui.wgsl
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
struct View {
view_proj: mat4x4<f32>,
inverse_view_proj: mat4x4<f32>,
view: mat4x4<f32>,
inverse_view: mat4x4<f32>,
projection: mat4x4<f32>,
inverse_projection: mat4x4<f32>,
world_position: vec3<f32>,
// viewport(x_origin, y_origin, width, height)
viewport: vec4<f32>,
};
#import bevy_render::view

@group(0) @binding(0)
var<uniform> view: View;

Expand Down

0 comments on commit 6acf2c2

Please sign in to comment.