From 5db2933fd59a802dbd6554d71cd111d10a9988fe Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Tue, 31 May 2022 23:23:25 +0000 Subject: [PATCH] Split mesh shader files (#4867) # Objective - Split PBR and 2D mesh shaders into types and bindings to prepare the shaders to be more reusable. - See #3969 for details. I'm doing this in multiple steps to make review easier. --- ## Changelog - Changed: 2D and PBR mesh shaders are now split into types and bindings, the following shader imports are available: `bevy_pbr::mesh_view_types`, `bevy_pbr::mesh_view_bindings`, `bevy_pbr::mesh_types`, `bevy_pbr::mesh_bindings`, `bevy_sprite::mesh2d_view_types`, `bevy_sprite::mesh2d_view_bindings`, `bevy_sprite::mesh2d_types`, `bevy_sprite::mesh2d_bindings` ## Migration Guide - In shaders for 3D meshes: - `#import bevy_pbr::mesh_view_bind_group` -> `#import bevy_pbr::mesh_view_bindings` - `#import bevy_pbr::mesh_struct` -> `#import bevy_pbr::mesh_types` - NOTE: If you are using the mesh bind group at bind group index 2, you can remove those binding statements in your shader and just use `#import bevy_pbr::mesh_bindings` which itself imports the mesh types needed for the bindings. - In shaders for 2D meshes: - `#import bevy_sprite::mesh2d_view_bind_group` -> `#import bevy_sprite::mesh2d_view_bindings` - `#import bevy_sprite::mesh2d_struct` -> `#import bevy_sprite::mesh2d_types` - NOTE: If you are using the mesh2d bind group at bind group index 2, you can remove those binding statements in your shader and just use `#import bevy_sprite::mesh2d_bindings` which itself imports the mesh2d types needed for the bindings. --- assets/shaders/animate_shader.wgsl | 4 +- .../custom_material_screenspace_texture.wgsl | 2 +- assets/shaders/custom_vertex_attribute.wgsl | 7 +-- assets/shaders/instancing.wgsl | 4 +- assets/shaders/shader_defs.wgsl | 6 +- crates/bevy_pbr/src/lib.rs | 16 ++++++ crates/bevy_pbr/src/pbr_material.rs | 2 +- crates/bevy_pbr/src/render/depth.wgsl | 9 +-- crates/bevy_pbr/src/render/mesh.rs | 25 ++++++--- crates/bevy_pbr/src/render/mesh.wgsl | 14 +---- crates/bevy_pbr/src/render/mesh_bindings.wgsl | 11 ++++ .../{mesh_struct.wgsl => mesh_types.wgsl} | 2 +- .../src/render/mesh_view_bindings.wgsl | 42 ++++++++++++++ ...w_bind_group.wgsl => mesh_view_types.wgsl} | 41 +------------- crates/bevy_pbr/src/render/pbr.wgsl | 56 ++----------------- crates/bevy_pbr/src/render/pbr_bindings.wgsl | 26 +++++++++ crates/bevy_pbr/src/render/pbr_types.wgsl | 24 ++++++++ crates/bevy_pbr/src/render/wireframe.wgsl | 4 +- .../src/mesh2d/color_material.wgsl | 9 +-- crates/bevy_sprite/src/mesh2d/mesh.rs | 30 +++++++--- crates/bevy_sprite/src/mesh2d/mesh2d.wgsl | 12 +--- .../src/mesh2d/mesh2d_bindings.wgsl | 6 ++ .../{mesh2d_struct.wgsl => mesh2d_types.wgsl} | 2 +- .../src/mesh2d/mesh2d_view_bindings.wgsl | 6 ++ ...bind_group.wgsl => mesh2d_view_types.wgsl} | 2 +- examples/2d/mesh2d_manual.rs | 7 +-- 26 files changed, 207 insertions(+), 162 deletions(-) create mode 100644 crates/bevy_pbr/src/render/mesh_bindings.wgsl rename crates/bevy_pbr/src/render/{mesh_struct.wgsl => mesh_types.wgsl} (88%) create mode 100644 crates/bevy_pbr/src/render/mesh_view_bindings.wgsl rename crates/bevy_pbr/src/render/{mesh_view_bind_group.wgsl => mesh_view_types.wgsl} (65%) create mode 100644 crates/bevy_pbr/src/render/pbr_bindings.wgsl create mode 100644 crates/bevy_pbr/src/render/pbr_types.wgsl create mode 100644 crates/bevy_sprite/src/mesh2d/mesh2d_bindings.wgsl rename crates/bevy_sprite/src/mesh2d/{mesh2d_struct.wgsl => mesh2d_types.wgsl} (81%) create mode 100644 crates/bevy_sprite/src/mesh2d/mesh2d_view_bindings.wgsl rename crates/bevy_sprite/src/mesh2d/{mesh2d_view_bind_group.wgsl => mesh2d_view_types.wgsl} (77%) diff --git a/assets/shaders/animate_shader.wgsl b/assets/shaders/animate_shader.wgsl index fdc60da00b539..6dbe165db24f7 100644 --- a/assets/shaders/animate_shader.wgsl +++ b/assets/shaders/animate_shader.wgsl @@ -1,5 +1,5 @@ -#import bevy_pbr::mesh_view_bind_group -#import bevy_pbr::mesh_struct +#import bevy_pbr::mesh_types +#import bevy_pbr::mesh_view_bindings [[group(1), binding(0)]] var mesh: Mesh; diff --git a/assets/shaders/custom_material_screenspace_texture.wgsl b/assets/shaders/custom_material_screenspace_texture.wgsl index a1fa6998f20d2..2228b79083af9 100644 --- a/assets/shaders/custom_material_screenspace_texture.wgsl +++ b/assets/shaders/custom_material_screenspace_texture.wgsl @@ -1,4 +1,4 @@ -#import bevy_pbr::mesh_view_bind_group +#import bevy_pbr::mesh_view_bindings [[group(1), binding(0)]] var texture: texture_2d; diff --git a/assets/shaders/custom_vertex_attribute.wgsl b/assets/shaders/custom_vertex_attribute.wgsl index 3e42b03d710ae..52129f4acd39a 100644 --- a/assets/shaders/custom_vertex_attribute.wgsl +++ b/assets/shaders/custom_vertex_attribute.wgsl @@ -1,5 +1,5 @@ -#import bevy_pbr::mesh_view_bind_group -#import bevy_pbr::mesh_struct +#import bevy_pbr::mesh_view_bindings +#import bevy_pbr::mesh_bindings struct Vertex { [[location(0)]] position: vec3; @@ -12,9 +12,6 @@ struct CustomMaterial { [[group(1), binding(0)]] var material: CustomMaterial; -[[group(2), binding(0)]] -var mesh: Mesh; - struct VertexOutput { [[builtin(position)]] clip_position: vec4; [[location(0)]] blend_color: vec4; diff --git a/assets/shaders/instancing.wgsl b/assets/shaders/instancing.wgsl index 262df7224b887..a6d0f7c2e05ba 100644 --- a/assets/shaders/instancing.wgsl +++ b/assets/shaders/instancing.wgsl @@ -1,5 +1,5 @@ -#import bevy_pbr::mesh_view_bind_group -#import bevy_pbr::mesh_struct +#import bevy_pbr::mesh_types +#import bevy_pbr::mesh_view_bindings [[group(1), binding(0)]] var mesh: Mesh; diff --git a/assets/shaders/shader_defs.wgsl b/assets/shaders/shader_defs.wgsl index 0d1c93d37e5ea..ca63026b50958 100644 --- a/assets/shaders/shader_defs.wgsl +++ b/assets/shaders/shader_defs.wgsl @@ -1,5 +1,5 @@ -#import bevy_pbr::mesh_view_bind_group -#import bevy_pbr::mesh_struct +#import bevy_pbr::mesh_types +#import bevy_pbr::mesh_view_bindings [[group(1), binding(0)]] var mesh: Mesh; @@ -26,7 +26,7 @@ fn vertex(vertex: Vertex) -> VertexOutput { [[stage(fragment)]] fn fragment() -> [[location(0)]] vec4 { var color = vec4(0.0, 0.0, 1.0, 1.0); -# ifdef IS_RED +# ifdef IS_RED color = vec4(1.0, 0.0, 0.0, 1.0); # endif return color; diff --git a/crates/bevy_pbr/src/lib.rs b/crates/bevy_pbr/src/lib.rs index 83585694ba659..b677d27fadf90 100644 --- a/crates/bevy_pbr/src/lib.rs +++ b/crates/bevy_pbr/src/lib.rs @@ -49,6 +49,10 @@ use bevy_render::{ }; use bevy_transform::TransformSystem; +pub const PBR_TYPES_SHADER_HANDLE: HandleUntyped = + HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 1708015359337029744); +pub const PBR_BINDINGS_SHADER_HANDLE: HandleUntyped = + HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 5635987986427308186); pub const PBR_SHADER_HANDLE: HandleUntyped = HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 4805239651767701046); pub const SHADOW_SHADER_HANDLE: HandleUntyped = @@ -60,6 +64,18 @@ pub struct PbrPlugin; impl Plugin for PbrPlugin { fn build(&self, app: &mut App) { + load_internal_asset!( + app, + PBR_TYPES_SHADER_HANDLE, + "render/pbr_types.wgsl", + Shader::from_wgsl + ); + load_internal_asset!( + app, + PBR_BINDINGS_SHADER_HANDLE, + "render/pbr_bindings.wgsl", + Shader::from_wgsl + ); load_internal_asset!(app, PBR_SHADER_HANDLE, "render/pbr.wgsl", Shader::from_wgsl); load_internal_asset!( app, diff --git a/crates/bevy_pbr/src/pbr_material.rs b/crates/bevy_pbr/src/pbr_material.rs index 40f4024331889..3c91e7f9ada55 100644 --- a/crates/bevy_pbr/src/pbr_material.rs +++ b/crates/bevy_pbr/src/pbr_material.rs @@ -118,7 +118,7 @@ impl From> for StandardMaterial { } } -// NOTE: These must match the bit flags in bevy_pbr/src/render/pbr.wgsl! +// NOTE: These must match the bit flags in bevy_pbr/src/render/pbr_types.wgsl! bitflags::bitflags! { #[repr(transparent)] pub struct StandardMaterialFlags: u32 { diff --git a/crates/bevy_pbr/src/render/depth.wgsl b/crates/bevy_pbr/src/render/depth.wgsl index 9f5e17fca3433..f15c48c37eaff 100644 --- a/crates/bevy_pbr/src/render/depth.wgsl +++ b/crates/bevy_pbr/src/render/depth.wgsl @@ -1,11 +1,6 @@ -#import bevy_pbr::mesh_struct +#import bevy_pbr::mesh_view_types +#import bevy_pbr::mesh_types -// NOTE: Keep in sync with pbr.wgsl -struct View { - view_proj: mat4x4; - projection: mat4x4; - world_position: vec3; -}; [[group(0), binding(0)]] var view: View; diff --git a/crates/bevy_pbr/src/render/mesh.rs b/crates/bevy_pbr/src/render/mesh.rs index c946720b6fa83..204c44c3bd45a 100644 --- a/crates/bevy_pbr/src/render/mesh.rs +++ b/crates/bevy_pbr/src/render/mesh.rs @@ -35,10 +35,14 @@ const MAX_JOINTS: usize = 256; const JOINT_SIZE: usize = std::mem::size_of::(); pub(crate) const JOINT_BUFFER_SIZE: usize = MAX_JOINTS * JOINT_SIZE; -pub const MESH_VIEW_BIND_GROUP_HANDLE: HandleUntyped = +pub const MESH_VIEW_TYPES_HANDLE: HandleUntyped = + HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 8140454348013264787); +pub const MESH_VIEW_BINDINGS_HANDLE: HandleUntyped = HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 9076678235888822571); -pub const MESH_STRUCT_HANDLE: HandleUntyped = +pub const MESH_TYPES_HANDLE: HandleUntyped = HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 2506024101911992377); +pub const MESH_BINDINGS_HANDLE: HandleUntyped = + HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 16831548636314682308); pub const MESH_SHADER_HANDLE: HandleUntyped = HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 3252377289100772450); pub const SKINNING_HANDLE: HandleUntyped = @@ -46,19 +50,26 @@ pub const SKINNING_HANDLE: HandleUntyped = impl Plugin for MeshRenderPlugin { fn build(&self, app: &mut bevy_app::App) { - load_internal_asset!(app, MESH_SHADER_HANDLE, "mesh.wgsl", Shader::from_wgsl); load_internal_asset!( app, - MESH_STRUCT_HANDLE, - "mesh_struct.wgsl", + MESH_VIEW_TYPES_HANDLE, + "mesh_view_types.wgsl", + Shader::from_wgsl + ); + load_internal_asset!( + app, + MESH_VIEW_BINDINGS_HANDLE, + "mesh_view_bindings.wgsl", Shader::from_wgsl ); + load_internal_asset!(app, MESH_TYPES_HANDLE, "mesh_types.wgsl", Shader::from_wgsl); load_internal_asset!( app, - MESH_VIEW_BIND_GROUP_HANDLE, - "mesh_view_bind_group.wgsl", + MESH_BINDINGS_HANDLE, + "mesh_bindings.wgsl", Shader::from_wgsl ); + load_internal_asset!(app, MESH_SHADER_HANDLE, "mesh.wgsl", Shader::from_wgsl); load_internal_asset!(app, SKINNING_HANDLE, "skinning.wgsl", Shader::from_wgsl); app.add_plugin(UniformComponentPlugin::::default()); diff --git a/crates/bevy_pbr/src/render/mesh.wgsl b/crates/bevy_pbr/src/render/mesh.wgsl index 2adaab7c086a7..f35ed2b92175b 100644 --- a/crates/bevy_pbr/src/render/mesh.wgsl +++ b/crates/bevy_pbr/src/render/mesh.wgsl @@ -1,5 +1,5 @@ -#import bevy_pbr::mesh_view_bind_group -#import bevy_pbr::mesh_struct +#import bevy_pbr::mesh_view_bindings +#import bevy_pbr::mesh_bindings struct Vertex { [[location(0)]] position: vec3; @@ -30,14 +30,6 @@ struct VertexOutput { #endif }; -[[group(2), binding(0)]] -var mesh: Mesh; -#ifdef SKINNED -[[group(2), binding(1)]] -var joint_matrices: SkinnedMesh; -#import bevy_pbr::skinning -#endif - [[stage(vertex)]] fn vertex(vertex: Vertex) -> VertexOutput { var out: VertexOutput; @@ -68,7 +60,7 @@ fn vertex(vertex: Vertex) -> VertexOutput { #endif #ifdef VERTEX_COLORS out.color = vertex.color; -#endif +#endif out.uv = vertex.uv; out.clip_position = view.view_proj * out.world_position; diff --git a/crates/bevy_pbr/src/render/mesh_bindings.wgsl b/crates/bevy_pbr/src/render/mesh_bindings.wgsl new file mode 100644 index 0000000000000..8f0c69a781832 --- /dev/null +++ b/crates/bevy_pbr/src/render/mesh_bindings.wgsl @@ -0,0 +1,11 @@ +#define_import_path bevy_pbr::mesh_bindings + +#import bevy_pbr::mesh_types + +[[group(2), binding(0)]] +var mesh: Mesh; +#ifdef SKINNED +[[group(2), binding(1)]] +var joint_matrices: SkinnedMesh; +#import bevy_pbr::skinning +#endif diff --git a/crates/bevy_pbr/src/render/mesh_struct.wgsl b/crates/bevy_pbr/src/render/mesh_types.wgsl similarity index 88% rename from crates/bevy_pbr/src/render/mesh_struct.wgsl rename to crates/bevy_pbr/src/render/mesh_types.wgsl index de29921f46f7b..c04720d551e42 100644 --- a/crates/bevy_pbr/src/render/mesh_struct.wgsl +++ b/crates/bevy_pbr/src/render/mesh_types.wgsl @@ -1,4 +1,4 @@ -#define_import_path bevy_pbr::mesh_struct +#define_import_path bevy_pbr::mesh_types struct Mesh { model: mat4x4; diff --git a/crates/bevy_pbr/src/render/mesh_view_bindings.wgsl b/crates/bevy_pbr/src/render/mesh_view_bindings.wgsl new file mode 100644 index 0000000000000..ec6f5dbb4398c --- /dev/null +++ b/crates/bevy_pbr/src/render/mesh_view_bindings.wgsl @@ -0,0 +1,42 @@ +#define_import_path bevy_pbr::mesh_view_bindings + +#import bevy_pbr::mesh_view_types + +[[group(0), binding(0)]] +var view: View; +[[group(0), binding(1)]] +var lights: Lights; +#ifdef NO_ARRAY_TEXTURES_SUPPORT +[[group(0), binding(2)]] +var point_shadow_textures: texture_depth_cube; +#else +[[group(0), binding(2)]] +var point_shadow_textures: texture_depth_cube_array; +#endif +[[group(0), binding(3)]] +var point_shadow_textures_sampler: sampler_comparison; +#ifdef NO_ARRAY_TEXTURES_SUPPORT +[[group(0), binding(4)]] +var directional_shadow_textures: texture_depth_2d; +#else +[[group(0), binding(4)]] +var directional_shadow_textures: texture_depth_2d_array; +#endif +[[group(0), binding(5)]] +var directional_shadow_textures_sampler: sampler_comparison; + +#ifdef NO_STORAGE_BUFFERS_SUPPORT +[[group(0), binding(6)]] +var point_lights: PointLights; +[[group(0), binding(7)]] +var cluster_light_index_lists: ClusterLightIndexLists; +[[group(0), binding(8)]] +var cluster_offsets_and_counts: ClusterOffsetsAndCounts; +#else +[[group(0), binding(6)]] +var point_lights: PointLights; +[[group(0), binding(7)]] +var cluster_light_index_lists: ClusterLightIndexLists; +[[group(0), binding(8)]] +var cluster_offsets_and_counts: ClusterOffsetsAndCounts; +#endif diff --git a/crates/bevy_pbr/src/render/mesh_view_bind_group.wgsl b/crates/bevy_pbr/src/render/mesh_view_types.wgsl similarity index 65% rename from crates/bevy_pbr/src/render/mesh_view_bind_group.wgsl rename to crates/bevy_pbr/src/render/mesh_view_types.wgsl index 54dd56c967a16..3892cdefb46dc 100644 --- a/crates/bevy_pbr/src/render/mesh_view_bind_group.wgsl +++ b/crates/bevy_pbr/src/render/mesh_view_types.wgsl @@ -1,4 +1,4 @@ -#define_import_path bevy_pbr::mesh_view_bind_group +#define_import_path bevy_pbr::mesh_view_types struct View { view_proj: mat4x4; @@ -79,42 +79,3 @@ struct ClusterOffsetsAndCounts { data: array>; }; #endif - -[[group(0), binding(0)]] -var view: View; -[[group(0), binding(1)]] -var lights: Lights; -#ifdef NO_ARRAY_TEXTURES_SUPPORT -[[group(0), binding(2)]] -var point_shadow_textures: texture_depth_cube; -#else -[[group(0), binding(2)]] -var point_shadow_textures: texture_depth_cube_array; -#endif -[[group(0), binding(3)]] -var point_shadow_textures_sampler: sampler_comparison; -#ifdef NO_ARRAY_TEXTURES_SUPPORT -[[group(0), binding(4)]] -var directional_shadow_textures: texture_depth_2d; -#else -[[group(0), binding(4)]] -var directional_shadow_textures: texture_depth_2d_array; -#endif -[[group(0), binding(5)]] -var directional_shadow_textures_sampler: sampler_comparison; - -#ifdef NO_STORAGE_BUFFERS_SUPPORT -[[group(0), binding(6)]] -var point_lights: PointLights; -[[group(0), binding(7)]] -var cluster_light_index_lists: ClusterLightIndexLists; -[[group(0), binding(8)]] -var cluster_offsets_and_counts: ClusterOffsetsAndCounts; -#else -[[group(0), binding(6)]] -var point_lights: PointLights; -[[group(0), binding(7)]] -var cluster_light_index_lists: ClusterLightIndexLists; -[[group(0), binding(8)]] -var cluster_offsets_and_counts: ClusterOffsetsAndCounts; -#endif diff --git a/crates/bevy_pbr/src/render/pbr.wgsl b/crates/bevy_pbr/src/render/pbr.wgsl index c697df4a28713..403f91abe8db8 100644 --- a/crates/bevy_pbr/src/render/pbr.wgsl +++ b/crates/bevy_pbr/src/render/pbr.wgsl @@ -32,57 +32,9 @@ // // The above integration needs to be approximated. -#import bevy_pbr::mesh_view_bind_group -#import bevy_pbr::mesh_struct - -[[group(2), binding(0)]] -var mesh: Mesh; - -struct StandardMaterial { - base_color: vec4; - emissive: vec4; - perceptual_roughness: f32; - metallic: f32; - reflectance: f32; - // 'flags' is a bit field indicating various options. u32 is 32 bits so we have up to 32 options. - flags: u32; - alpha_cutoff: f32; -}; - -let STANDARD_MATERIAL_FLAGS_BASE_COLOR_TEXTURE_BIT: u32 = 1u; -let STANDARD_MATERIAL_FLAGS_EMISSIVE_TEXTURE_BIT: u32 = 2u; -let STANDARD_MATERIAL_FLAGS_METALLIC_ROUGHNESS_TEXTURE_BIT: u32 = 4u; -let STANDARD_MATERIAL_FLAGS_OCCLUSION_TEXTURE_BIT: u32 = 8u; -let STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT: u32 = 16u; -let STANDARD_MATERIAL_FLAGS_UNLIT_BIT: u32 = 32u; -let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_OPAQUE: u32 = 64u; -let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK: u32 = 128u; -let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_BLEND: u32 = 256u; -let STANDARD_MATERIAL_FLAGS_TWO_COMPONENT_NORMAL_MAP: u32 = 512u; -let STANDARD_MATERIAL_FLAGS_FLIP_NORMAL_MAP_Y: u32 = 1024u; - -[[group(1), binding(0)]] -var material: StandardMaterial; -[[group(1), binding(1)]] -var base_color_texture: texture_2d; -[[group(1), binding(2)]] -var base_color_sampler: sampler; -[[group(1), binding(3)]] -var emissive_texture: texture_2d; -[[group(1), binding(4)]] -var emissive_sampler: sampler; -[[group(1), binding(5)]] -var metallic_roughness_texture: texture_2d; -[[group(1), binding(6)]] -var metallic_roughness_sampler: sampler; -[[group(1), binding(7)]] -var occlusion_texture: texture_2d; -[[group(1), binding(8)]] -var occlusion_sampler: sampler; -[[group(1), binding(9)]] -var normal_map_texture: texture_2d; -[[group(1), binding(10)]] -var normal_map_sampler: sampler; +#import bevy_pbr::mesh_view_bindings +#import bevy_pbr::pbr_bindings +#import bevy_pbr::mesh_bindings let PI: f32 = 3.141592653589793; @@ -467,7 +419,7 @@ struct FragmentInput { #endif #ifdef VERTEX_COLORS [[location(4)]] color: vec4; -#endif +#endif }; [[stage(fragment)]] diff --git a/crates/bevy_pbr/src/render/pbr_bindings.wgsl b/crates/bevy_pbr/src/render/pbr_bindings.wgsl new file mode 100644 index 0000000000000..4c9205461ce11 --- /dev/null +++ b/crates/bevy_pbr/src/render/pbr_bindings.wgsl @@ -0,0 +1,26 @@ +#define_import_path bevy_pbr::pbr_bindings + +#import bevy_pbr::pbr_types + +[[group(1), binding(0)]] +var material: StandardMaterial; +[[group(1), binding(1)]] +var base_color_texture: texture_2d; +[[group(1), binding(2)]] +var base_color_sampler: sampler; +[[group(1), binding(3)]] +var emissive_texture: texture_2d; +[[group(1), binding(4)]] +var emissive_sampler: sampler; +[[group(1), binding(5)]] +var metallic_roughness_texture: texture_2d; +[[group(1), binding(6)]] +var metallic_roughness_sampler: sampler; +[[group(1), binding(7)]] +var occlusion_texture: texture_2d; +[[group(1), binding(8)]] +var occlusion_sampler: sampler; +[[group(1), binding(9)]] +var normal_map_texture: texture_2d; +[[group(1), binding(10)]] +var normal_map_sampler: sampler; diff --git a/crates/bevy_pbr/src/render/pbr_types.wgsl b/crates/bevy_pbr/src/render/pbr_types.wgsl new file mode 100644 index 0000000000000..6927424fb4b67 --- /dev/null +++ b/crates/bevy_pbr/src/render/pbr_types.wgsl @@ -0,0 +1,24 @@ +#define_import_path bevy_pbr::pbr_types + +struct StandardMaterial { + base_color: vec4; + emissive: vec4; + perceptual_roughness: f32; + metallic: f32; + reflectance: f32; + // 'flags' is a bit field indicating various options. u32 is 32 bits so we have up to 32 options. + flags: u32; + alpha_cutoff: f32; +}; + +let STANDARD_MATERIAL_FLAGS_BASE_COLOR_TEXTURE_BIT: u32 = 1u; +let STANDARD_MATERIAL_FLAGS_EMISSIVE_TEXTURE_BIT: u32 = 2u; +let STANDARD_MATERIAL_FLAGS_METALLIC_ROUGHNESS_TEXTURE_BIT: u32 = 4u; +let STANDARD_MATERIAL_FLAGS_OCCLUSION_TEXTURE_BIT: u32 = 8u; +let STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT: u32 = 16u; +let STANDARD_MATERIAL_FLAGS_UNLIT_BIT: u32 = 32u; +let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_OPAQUE: u32 = 64u; +let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK: u32 = 128u; +let STANDARD_MATERIAL_FLAGS_ALPHA_MODE_BLEND: u32 = 256u; +let STANDARD_MATERIAL_FLAGS_TWO_COMPONENT_NORMAL_MAP: u32 = 512u; +let STANDARD_MATERIAL_FLAGS_FLIP_NORMAL_MAP_Y: u32 = 1024u; diff --git a/crates/bevy_pbr/src/render/wireframe.wgsl b/crates/bevy_pbr/src/render/wireframe.wgsl index 39c5f443854a6..f17123e5825b8 100644 --- a/crates/bevy_pbr/src/render/wireframe.wgsl +++ b/crates/bevy_pbr/src/render/wireframe.wgsl @@ -1,5 +1,5 @@ -#import bevy_pbr::mesh_view_bind_group -#import bevy_pbr::mesh_struct +#import bevy_pbr::mesh_types +#import bevy_pbr::mesh_view_bindings struct Vertex { [[location(0)]] position: vec3; diff --git a/crates/bevy_sprite/src/mesh2d/color_material.wgsl b/crates/bevy_sprite/src/mesh2d/color_material.wgsl index dac272b09686e..24b55e448177d 100644 --- a/crates/bevy_sprite/src/mesh2d/color_material.wgsl +++ b/crates/bevy_sprite/src/mesh2d/color_material.wgsl @@ -1,5 +1,5 @@ -#import bevy_sprite::mesh2d_view_bind_group -#import bevy_sprite::mesh2d_struct +#import bevy_sprite::mesh2d_types +#import bevy_sprite::mesh2d_view_bindings struct ColorMaterial { color: vec4; @@ -8,9 +8,6 @@ struct ColorMaterial { }; let COLOR_MATERIAL_FLAGS_TEXTURE_BIT: u32 = 1u; -[[group(0), binding(0)]] -var view: View; - [[group(1), binding(0)]] var material: ColorMaterial; [[group(1), binding(1)]] @@ -45,4 +42,4 @@ fn fragment(in: FragmentInput) -> [[location(0)]] vec4 { #endif } return output_color; -} \ No newline at end of file +} diff --git a/crates/bevy_sprite/src/mesh2d/mesh.rs b/crates/bevy_sprite/src/mesh2d/mesh.rs index de9376ea9cbc8..c0c3e3010c302 100644 --- a/crates/bevy_sprite/src/mesh2d/mesh.rs +++ b/crates/bevy_sprite/src/mesh2d/mesh.rs @@ -35,28 +35,44 @@ impl From> for Mesh2dHandle { #[derive(Default)] pub struct Mesh2dRenderPlugin; -pub const MESH2D_VIEW_BIND_GROUP_HANDLE: HandleUntyped = +pub const MESH2D_VIEW_TYPES_HANDLE: HandleUntyped = + HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 12677582416765805110); +pub const MESH2D_VIEW_BINDINGS_HANDLE: HandleUntyped = HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 6901431444735842434); -pub const MESH2D_STRUCT_HANDLE: HandleUntyped = +pub const MESH2D_TYPES_HANDLE: HandleUntyped = HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 8994673400261890424); +pub const MESH2D_BINDINGS_HANDLE: HandleUntyped = + HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 8983617858458862856); pub const MESH2D_SHADER_HANDLE: HandleUntyped = HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 2971387252468633715); impl Plugin for Mesh2dRenderPlugin { fn build(&self, app: &mut bevy_app::App) { - load_internal_asset!(app, MESH2D_SHADER_HANDLE, "mesh2d.wgsl", Shader::from_wgsl); load_internal_asset!( app, - MESH2D_STRUCT_HANDLE, - "mesh2d_struct.wgsl", + MESH2D_VIEW_TYPES_HANDLE, + "mesh2d_view_types.wgsl", + Shader::from_wgsl + ); + load_internal_asset!( + app, + MESH2D_VIEW_BINDINGS_HANDLE, + "mesh2d_view_bindings.wgsl", Shader::from_wgsl ); load_internal_asset!( app, - MESH2D_VIEW_BIND_GROUP_HANDLE, - "mesh2d_view_bind_group.wgsl", + MESH2D_TYPES_HANDLE, + "mesh2d_types.wgsl", Shader::from_wgsl ); + load_internal_asset!( + app, + MESH2D_BINDINGS_HANDLE, + "mesh2d_bindings.wgsl", + Shader::from_wgsl + ); + load_internal_asset!(app, MESH2D_SHADER_HANDLE, "mesh2d.wgsl", Shader::from_wgsl); app.add_plugin(UniformComponentPlugin::::default()); diff --git a/crates/bevy_sprite/src/mesh2d/mesh2d.wgsl b/crates/bevy_sprite/src/mesh2d/mesh2d.wgsl index 4766c5cf7fb26..2466a196e5a0f 100644 --- a/crates/bevy_sprite/src/mesh2d/mesh2d.wgsl +++ b/crates/bevy_sprite/src/mesh2d/mesh2d.wgsl @@ -1,5 +1,5 @@ -#import bevy_sprite::mesh2d_view_bind_group -#import bevy_sprite::mesh2d_struct +#import bevy_sprite::mesh2d_view_bindings +#import bevy_sprite::mesh2d_bindings struct Vertex { [[location(0)]] position: vec3; @@ -26,12 +26,6 @@ struct VertexOutput { #endif }; -[[group(0), binding(0)]] -var view: View; - -[[group(2), binding(0)]] -var mesh: Mesh2d; - [[stage(vertex)]] fn vertex(vertex: Vertex) -> VertexOutput { let world_position = mesh.model * vec4(vertex.position, 1.0); @@ -74,4 +68,4 @@ struct FragmentInput { [[stage(fragment)]] fn fragment(in: FragmentInput) -> [[location(0)]] vec4 { return vec4(1.0, 0.0, 1.0, 1.0); -} \ No newline at end of file +} diff --git a/crates/bevy_sprite/src/mesh2d/mesh2d_bindings.wgsl b/crates/bevy_sprite/src/mesh2d/mesh2d_bindings.wgsl new file mode 100644 index 0000000000000..1bb2ec5959b8e --- /dev/null +++ b/crates/bevy_sprite/src/mesh2d/mesh2d_bindings.wgsl @@ -0,0 +1,6 @@ +#define_import_path bevy_sprite::mesh2d_bindings + +#import bevy_sprite::mesh2d_types + +[[group(2), binding(0)]] +var mesh: Mesh2d; diff --git a/crates/bevy_sprite/src/mesh2d/mesh2d_struct.wgsl b/crates/bevy_sprite/src/mesh2d/mesh2d_types.wgsl similarity index 81% rename from crates/bevy_sprite/src/mesh2d/mesh2d_struct.wgsl rename to crates/bevy_sprite/src/mesh2d/mesh2d_types.wgsl index e60d07c33bbd2..83f83366eb25c 100644 --- a/crates/bevy_sprite/src/mesh2d/mesh2d_struct.wgsl +++ b/crates/bevy_sprite/src/mesh2d/mesh2d_types.wgsl @@ -1,4 +1,4 @@ -#define_import_path bevy_sprite::mesh2d_struct +#define_import_path bevy_sprite::mesh2d_types struct Mesh2d { model: mat4x4; diff --git a/crates/bevy_sprite/src/mesh2d/mesh2d_view_bindings.wgsl b/crates/bevy_sprite/src/mesh2d/mesh2d_view_bindings.wgsl new file mode 100644 index 0000000000000..fedf12bff0072 --- /dev/null +++ b/crates/bevy_sprite/src/mesh2d/mesh2d_view_bindings.wgsl @@ -0,0 +1,6 @@ +#define_import_path bevy_sprite::mesh2d_view_bindings + +#import bevy_sprite::mesh2d_view_types + +[[group(0), binding(0)]] +var view: View; diff --git a/crates/bevy_sprite/src/mesh2d/mesh2d_view_bind_group.wgsl b/crates/bevy_sprite/src/mesh2d/mesh2d_view_types.wgsl similarity index 77% rename from crates/bevy_sprite/src/mesh2d/mesh2d_view_bind_group.wgsl rename to crates/bevy_sprite/src/mesh2d/mesh2d_view_types.wgsl index b85d542fc0e0f..2e54a898e5621 100644 --- a/crates/bevy_sprite/src/mesh2d/mesh2d_view_bind_group.wgsl +++ b/crates/bevy_sprite/src/mesh2d/mesh2d_view_types.wgsl @@ -1,4 +1,4 @@ -#define_import_path bevy_sprite::mesh2d_view_bind_group +#define_import_path bevy_sprite::mesh2d_view_types struct View { view_proj: mat4x4; diff --git a/examples/2d/mesh2d_manual.rs b/examples/2d/mesh2d_manual.rs index 0fea936b4f44f..72a6cbfd4604b 100644 --- a/examples/2d/mesh2d_manual.rs +++ b/examples/2d/mesh2d_manual.rs @@ -209,10 +209,9 @@ type DrawColoredMesh2d = ( // using `include_str!()`, or loaded like any other asset with `asset_server.load()`. const COLORED_MESH2D_SHADER: &str = r" // Import the standard 2d mesh uniforms and set their bind groups -#import bevy_sprite::mesh2d_view_bind_group -[[group(0), binding(0)]] -var view: View; -#import bevy_sprite::mesh2d_struct +#import bevy_sprite::mesh2d_types +#import bevy_sprite::mesh2d_view_bindings + [[group(1), binding(0)]] var mesh: Mesh2d;