Refactor Globals and View structs into separate shaders (#7512)
fixes #6799 # Objective 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. ## Solution Refactor both `Globals` and `View` into separate importable shaders. Use the imports throughout. Co-authored-by: Torstein Grindvik <52322338+torsteingrindvik@users.noreply.github.com>
This commit is contained in:
		
							parent
							
								
									de98850a3e
								
							
						
					
					
						commit
						38766faccb
					
				@ -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]
 | 
			
		||||
@ -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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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::<GlobalsUniform>();
 | 
			
		||||
 | 
			
		||||
        if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										16
									
								
								crates/bevy_render/src/globals.wgsl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								crates/bevy_render/src/globals.wgsl
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
			
		||||
};
 | 
			
		||||
@ -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::<ComputedVisibility>()
 | 
			
		||||
            .register_type::<ComputedVisibilityFlags>()
 | 
			
		||||
            .register_type::<Msaa>()
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										13
									
								
								crates/bevy_render/src/view/view.wgsl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								crates/bevy_render/src/view/view.wgsl
									
									
									
									
									
										Normal file
									
								
							@ -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>,
 | 
			
		||||
};
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user