diff --git a/crates/bevy_pbr/src/render/mesh_view_types.wgsl b/crates/bevy_pbr/src/render/mesh_view_types.wgsl index c732cabc9453c..57ac0cfa27d92 100644 --- a/crates/bevy_pbr/src/render/mesh_view_types.wgsl +++ b/crates/bevy_pbr/src/render/mesh_view_types.wgsl @@ -1,16 +1,7 @@ #define_import_path bevy_pbr::mesh_view_types -struct View { - view_proj: mat4x4, - inverse_view_proj: mat4x4, - view: mat4x4, - inverse_view: mat4x4, - projection: mat4x4, - inverse_projection: mat4x4, - world_position: vec3, - // viewport(x_origin, y_origin, width, height) - viewport: vec4, -}; +#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] @@ -119,18 +110,3 @@ struct ClusterOffsetsAndCounts { data: array, 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 -} diff --git a/crates/bevy_render/src/globals.rs b/crates/bevy_render/src/globals.rs index f0a4989467f66..474defa802e1b 100644 --- a/crates/bevy_render/src/globals.rs +++ b/crates/bevy_render/src/globals.rs @@ -1,19 +1,25 @@ use crate::{ extract_resource::ExtractResource, + prelude::Shader, render_resource::{ShaderType, UniformBuffer}, renderer::{RenderDevice, RenderQueue}, Extract, ExtractSchedule, RenderApp, RenderSet, }; 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_time::Time; +pub const GLOBALS_TYPE_HANDLE: HandleUntyped = + HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 17924628719070609599); + 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::(); if let Ok(render_app) = app.get_sub_app_mut(RenderApp) { diff --git a/crates/bevy_render/src/globals.wgsl b/crates/bevy_render/src/globals.wgsl new file mode 100644 index 0000000000000..6e0f271982b20 --- /dev/null +++ b/crates/bevy_render/src/globals.wgsl @@ -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 +}; diff --git a/crates/bevy_render/src/view/mod.rs b/crates/bevy_render/src/view/mod.rs index 5d278223c9e0e..b70db4693324c 100644 --- a/crates/bevy_render/src/view/mod.rs +++ b/crates/bevy_render/src/view/mod.rs @@ -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}, @@ -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}; @@ -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::() .register_type::() .register_type::() diff --git a/crates/bevy_render/src/view/view.wgsl b/crates/bevy_render/src/view/view.wgsl new file mode 100644 index 0000000000000..00a2fcbd5f931 --- /dev/null +++ b/crates/bevy_render/src/view/view.wgsl @@ -0,0 +1,13 @@ +#define_import_path bevy_render::view + +struct View { + view_proj: mat4x4, + inverse_view_proj: mat4x4, + view: mat4x4, + inverse_view: mat4x4, + projection: mat4x4, + inverse_projection: mat4x4, + world_position: vec3, + // viewport(x_origin, y_origin, width, height) + viewport: vec4, +}; diff --git a/crates/bevy_sprite/src/mesh2d/mesh2d_view_types.wgsl b/crates/bevy_sprite/src/mesh2d/mesh2d_view_types.wgsl index 78c3459235a16..7868a05d40ff8 100644 --- a/crates/bevy_sprite/src/mesh2d/mesh2d_view_types.wgsl +++ b/crates/bevy_sprite/src/mesh2d/mesh2d_view_types.wgsl @@ -1,28 +1,4 @@ #define_import_path bevy_sprite::mesh2d_view_types -struct View { - view_proj: mat4x4, - inverse_view_proj: mat4x4, - view: mat4x4, - inverse_view: mat4x4, - projection: mat4x4, - inverse_projection: mat4x4, - world_position: vec3, - // viewport(x_origin, y_origin, width, height) - viewport: vec4, -}; - -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 diff --git a/crates/bevy_sprite/src/render/sprite.wgsl b/crates/bevy_sprite/src/render/sprite.wgsl index d1097e61af6fc..0bb90e61507e7 100644 --- a/crates/bevy_sprite/src/render/sprite.wgsl +++ b/crates/bevy_sprite/src/render/sprite.wgsl @@ -2,17 +2,8 @@ #import bevy_core_pipeline::tonemapping #endif -struct View { - view_proj: mat4x4, - inverse_view_proj: mat4x4, - view: mat4x4, - inverse_view: mat4x4, - projection: mat4x4, - inverse_projection: mat4x4, - world_position: vec3, - // viewport(x_origin, y_origin, width, height) - viewport: vec4, -}; +#import bevy_render::view + @group(0) @binding(0) var view: View; diff --git a/crates/bevy_ui/src/render/ui.wgsl b/crates/bevy_ui/src/render/ui.wgsl index 0cf8ed8aa784c..11ce13aa5468d 100644 --- a/crates/bevy_ui/src/render/ui.wgsl +++ b/crates/bevy_ui/src/render/ui.wgsl @@ -1,14 +1,5 @@ -struct View { - view_proj: mat4x4, - inverse_view_proj: mat4x4, - view: mat4x4, - inverse_view: mat4x4, - projection: mat4x4, - inverse_projection: mat4x4, - world_position: vec3, - // viewport(x_origin, y_origin, width, height) - viewport: vec4, -}; +#import bevy_render::view + @group(0) @binding(0) var view: View;