From 2486e6f1e1bf6cf3026e2d040d8460d28ccd635f Mon Sep 17 00:00:00 2001 From: IceSentry Date: Mon, 6 Feb 2023 23:59:04 -0500 Subject: [PATCH 1/9] allow prepass in webgl --- Cargo.toml | 4 +- assets/shaders/show_prepass.wgsl | 33 ++++++-- crates/bevy_pbr/src/prepass/mod.rs | 84 +++++++++++++++++-- .../bevy_pbr/src/prepass/prepass_utils.wgsl | 8 +- crates/bevy_pbr/src/render/mesh.rs | 75 ++++------------- examples/shader/shader_prepass.rs | 67 +++++++++++---- 6 files changed, 176 insertions(+), 95 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 023a4189688f8..ab2ddea013f3d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1293,9 +1293,9 @@ path = "examples/shader/shader_prepass.rs" [package.metadata.example.shader_prepass] name = "Material Prepass" -description = "A shader that uses the depth texture generated in a prepass" +description = "A shader that uses the various textures generated by the prepass" category = "Shaders" -wasm = false +wasm = true [[example]] diff --git a/assets/shaders/show_prepass.wgsl b/assets/shaders/show_prepass.wgsl index 87c48993aa203..837b3c04add11 100644 --- a/assets/shaders/show_prepass.wgsl +++ b/assets/shaders/show_prepass.wgsl @@ -2,25 +2,44 @@ #import bevy_pbr::mesh_view_bindings #import bevy_pbr::prepass_utils +struct ShowPrepassSettings { + show_depth: f32, + show_normals: f32, + is_webgl: f32, + padding__: f32, +} @group(1) @binding(0) -var show_depth: f32; +var settings: ShowPrepassSettings; @group(1) @binding(1) -var show_normal: f32; +var show_prepass_sampler: sampler; @fragment fn fragment( @builtin(position) frag_coord: vec4, + #ifndef WEBGL @builtin(sample_index) sample_index: u32, + #endif// WEBGL #import bevy_pbr::mesh_vertex_output ) -> @location(0) vec4 { - if show_depth == 1.0 { + if settings.show_depth == 1.0 { + #ifdef WEBGL + // prepass_depth() uses textureLoad which doesn't work in WebGL for depth textures. + // Instead we need to use a sampler + let dims = textureDimensions(depth_prepass_texture); + let uv = frag_coord.xy / vec2(dims); + let depth = textureSample(depth_prepass_texture, show_prepass_sampler, uv); + #else let depth = prepass_depth(frag_coord, sample_index); + #endif // WEBGL return vec4(depth, depth, depth, 1.0); - } else if show_normal == 1.0 { + } else if settings.show_normals == 1.0 { + #ifdef WEBGL + let normal = prepass_normal(frag_coord, 0u); + #else // WEBGL let normal = prepass_normal(frag_coord, sample_index); + #endif // WEBGL return vec4(normal, 1.0); - } else { - // transparent - return vec4(0.0); } + + return vec4(0.0); } diff --git a/crates/bevy_pbr/src/prepass/mod.rs b/crates/bevy_pbr/src/prepass/mod.rs index f4f75f7a04b99..9f7019eaec067 100644 --- a/crates/bevy_pbr/src/prepass/mod.rs +++ b/crates/bevy_pbr/src/prepass/mod.rs @@ -26,16 +26,17 @@ use bevy_render::{ }, render_resource::{ BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout, BindGroupLayoutDescriptor, - BindGroupLayoutEntry, BindingType, BlendState, BufferBindingType, ColorTargetState, - ColorWrites, CompareFunction, DepthBiasState, DepthStencilState, Extent3d, FragmentState, - FrontFace, MultisampleState, PipelineCache, PolygonMode, PrimitiveState, - RenderPipelineDescriptor, Shader, ShaderDefVal, ShaderRef, ShaderStages, ShaderType, - SpecializedMeshPipeline, SpecializedMeshPipelineError, SpecializedMeshPipelines, - StencilFaceState, StencilState, TextureDescriptor, TextureDimension, TextureFormat, - TextureUsages, VertexState, + BindGroupLayoutEntry, BindingResource, BindingType, BlendState, BufferBindingType, + ColorTargetState, ColorWrites, CompareFunction, DepthBiasState, DepthStencilState, + Extent3d, FragmentState, FrontFace, MultisampleState, PipelineCache, PolygonMode, + PrimitiveState, RenderPipelineDescriptor, Shader, ShaderDefVal, ShaderRef, ShaderStages, + ShaderType, SpecializedMeshPipeline, SpecializedMeshPipelineError, + SpecializedMeshPipelines, StencilFaceState, StencilState, TextureDescriptor, + TextureDimension, TextureFormat, TextureSampleType, TextureUsages, TextureViewDimension, + VertexState, }, renderer::RenderDevice, - texture::TextureCache, + texture::{FallbackImagesDepth, FallbackImagesMsaa, TextureCache}, view::{ExtractedView, Msaa, ViewUniform, ViewUniformOffset, ViewUniforms, VisibleEntities}, Extract, ExtractSchedule, RenderApp, RenderSet, }; @@ -333,6 +334,73 @@ where } } +pub fn get_bind_group_layout_entries( + bindings: [u32; 2], + multisampled: bool, +) -> [BindGroupLayoutEntry; 2] { + [ + // Depth texture + BindGroupLayoutEntry { + binding: bindings[0], + visibility: ShaderStages::FRAGMENT, + ty: BindingType::Texture { + multisampled, + sample_type: TextureSampleType::Depth, + view_dimension: TextureViewDimension::D2, + }, + count: None, + }, + // Normal texture + BindGroupLayoutEntry { + binding: bindings[1], + visibility: ShaderStages::FRAGMENT, + ty: BindingType::Texture { + multisampled, + sample_type: TextureSampleType::Float { filterable: true }, + view_dimension: TextureViewDimension::D2, + }, + count: None, + }, + ] +} + +pub fn get_bindings<'a>( + prepass_textures: Option<&'a ViewPrepassTextures>, + fallback_images: &'a mut FallbackImagesMsaa, + fallback_depths: &'a mut FallbackImagesDepth, + msaa: &'a Msaa, + bindings: [u32; 2], +) -> [BindGroupEntry<'a>; 2] { + let depth_view = match prepass_textures.and_then(|x| x.depth.as_ref()) { + Some(texture) => &texture.default_view, + None => { + &fallback_depths + .image_for_samplecount(msaa.samples()) + .texture_view + } + }; + + let normal_view = match prepass_textures.and_then(|x| x.normal.as_ref()) { + Some(texture) => &texture.default_view, + None => { + &fallback_images + .image_for_samplecount(msaa.samples()) + .texture_view + } + }; + + [ + BindGroupEntry { + binding: bindings[0], + resource: BindingResource::TextureView(depth_view), + }, + BindGroupEntry { + binding: bindings[1], + resource: BindingResource::TextureView(normal_view), + }, + ] +} + // Extract the render phases for the prepass pub fn extract_camera_prepass_phase( mut commands: Commands, diff --git a/crates/bevy_pbr/src/prepass/prepass_utils.wgsl b/crates/bevy_pbr/src/prepass/prepass_utils.wgsl index 1fd1365a650a4..99c0cf4e89b2b 100644 --- a/crates/bevy_pbr/src/prepass/prepass_utils.wgsl +++ b/crates/bevy_pbr/src/prepass/prepass_utils.wgsl @@ -5,8 +5,8 @@ fn prepass_normal(frag_coord: vec4, sample_index: u32) -> vec3 { #ifdef MULTISAMPLED let normal_sample = textureLoad(normal_prepass_texture, vec2(frag_coord.xy), i32(sample_index)); #else - let normal_sample = textureLoad(normal_prepass_texture, vec2(frag_coord.xy), 0); -#endif + let normal_sample = textureLoad(normal_prepass_texture, vec2(frag_coord.xy), 0i); +#endif //MULTISAMPLED return normal_sample.xyz * 2.0 - vec3(1.0); } #endif // NORMAL_PREPASS @@ -16,8 +16,8 @@ fn prepass_depth(frag_coord: vec4, sample_index: u32) -> f32 { #ifdef MULTISAMPLED let depth_sample = textureLoad(depth_prepass_texture, vec2(frag_coord.xy), i32(sample_index)); #else - let depth_sample = textureLoad(depth_prepass_texture, vec2(frag_coord.xy), 0); -#endif + let depth_sample = textureLoad(depth_prepass_texture, vec2(frag_coord.xy), 0i); +#endif // MULTISAMPLED return depth_sample; } #endif // DEPTH_PREPASS diff --git a/crates/bevy_pbr/src/render/mesh.rs b/crates/bevy_pbr/src/render/mesh.rs index 0b4f5463eb758..bd94b12b9af1a 100644 --- a/crates/bevy_pbr/src/render/mesh.rs +++ b/crates/bevy_pbr/src/render/mesh.rs @@ -1,9 +1,8 @@ use crate::{ - environment_map, queue_shadow_view_bind_group, EnvironmentMapLight, FogMeta, GlobalLightMeta, - GpuFog, GpuLights, GpuPointLights, LightMeta, NotShadowCaster, NotShadowReceiver, - ShadowPipeline, ViewClusterBindings, ViewFogUniformOffset, ViewLightsUniformOffset, - ViewShadowBindings, CLUSTERED_FORWARD_STORAGE_BUFFER_COUNT, MAX_CASCADES_PER_LIGHT, - MAX_DIRECTIONAL_LIGHTS, + FogMeta, GlobalLightMeta, GpuFog, GpuLights, GpuPointLights, LightMeta, NotShadowCaster, + NotShadowReceiver, ShadowPipeline, ViewClusterBindings, ViewFogUniformOffset, + ViewLightsUniformOffset, ViewShadowBindings, CLUSTERED_FORWARD_STORAGE_BUFFER_COUNT, + MAX_CASCADES_PER_LIGHT, MAX_DIRECTIONAL_LIGHTS, }; use bevy_app::Plugin; use bevy_asset::{load_internal_asset, Assets, Handle, HandleUntyped}; @@ -432,29 +431,11 @@ impl FromWorld for MeshPipeline { let tonemapping_lut_entries = get_lut_bind_group_layout_entries([14, 15]); entries.extend_from_slice(&tonemapping_lut_entries); - if cfg!(not(feature = "webgl")) { - // Depth texture - entries.push(BindGroupLayoutEntry { - binding: 16, - visibility: ShaderStages::FRAGMENT, - ty: BindingType::Texture { - multisampled, - sample_type: TextureSampleType::Depth, - view_dimension: TextureViewDimension::D2, - }, - count: None, - }); - // Normal texture - entries.push(BindGroupLayoutEntry { - binding: 17, - visibility: ShaderStages::FRAGMENT, - ty: BindingType::Texture { - multisampled, - sample_type: TextureSampleType::Float { filterable: true }, - view_dimension: TextureViewDimension::D2, - }, - count: None, - }); + if cfg!(not(feature = "webgl")) || (cfg!(feature = "webgl") && !multisampled) { + entries.extend_from_slice(&prepass::get_bind_group_layout_entries( + [11, 12], + multisampled, + )); } entries @@ -1066,35 +1047,15 @@ pub fn queue_mesh_view_bind_groups( let tonemapping_luts = get_lut_bindings(&images, &tonemapping_luts, tonemapping, [14, 15]); entries.extend_from_slice(&tonemapping_luts); - - // When using WebGL with MSAA, we can't create the fallback textures required by the prepass - // When using WebGL, and MSAA is disabled, we can't bind the textures either - if cfg!(not(feature = "webgl")) { - let depth_view = match prepass_textures.and_then(|x| x.depth.as_ref()) { - Some(texture) => &texture.default_view, - None => { - &fallback_depths - .image_for_samplecount(msaa.samples()) - .texture_view - } - }; - entries.push(BindGroupEntry { - binding: 16, - resource: BindingResource::TextureView(depth_view), - }); - - let normal_view = match prepass_textures.and_then(|x| x.normal.as_ref()) { - Some(texture) => &texture.default_view, - None => { - &fallback_images - .image_for_samplecount(msaa.samples()) - .texture_view - } - }; - entries.push(BindGroupEntry { - binding: 17, - resource: BindingResource::TextureView(normal_view), - }); + // When using WebGL, we can't have a depth texture with multisampling + if cfg!(not(feature = "webgl")) || (cfg!(feature = "webgl") && msaa.samples() == 1) { + entries.extend_from_slice(&prepass::get_bindings( + prepass_textures, + &mut fallback_images, + &mut fallback_depths, + &msaa, + [11, 12], + )); } let view_bind_group = render_device.create_bind_group(&BindGroupDescriptor { diff --git a/examples/shader/shader_prepass.rs b/examples/shader/shader_prepass.rs index 0f9697bcadf6b..f807865ea90a4 100644 --- a/examples/shader/shader_prepass.rs +++ b/examples/shader/shader_prepass.rs @@ -10,11 +10,13 @@ use bevy::{ pbr::{NotShadowCaster, PbrPlugin}, prelude::*, reflect::TypeUuid, - render::render_resource::{AsBindGroup, ShaderRef}, + render::render_resource::{AsBindGroup, ShaderRef, ShaderType}, }; fn main() { App::new() + // When using the prepass with WebGL, you can't use Msaa + .insert_resource(Msaa::Off) .add_plugins(DefaultPlugins.set(PbrPlugin { // The prepass is enabled by default on the StandardMaterial, // but you can disable it if you need to. @@ -30,7 +32,7 @@ fn main() { }) .add_startup_system(setup) .add_system(rotate) - .add_system(update) + .add_system(toggle_prepass_view) .run(); } @@ -69,8 +71,9 @@ fn setup( MaterialMeshBundle { mesh: meshes.add(shape::Quad::new(Vec2::new(20.0, 20.0)).into()), material: depth_materials.add(PrepassOutputMaterial { - show_depth: 0.0, - show_normal: 0.0, + settings: ShowPrepassSettings::default(), + // We don't actually need a texture here, we just want bevy to give us a sampler + sampler: None, }), transform: Transform::from_xyz(-0.75, 1.25, 3.0) .looking_at(Vec3::new(2.0, -2.5, -5.0), Vec3::Y), @@ -193,14 +196,23 @@ fn rotate(mut q: Query<&mut Transform, With>, time: Res