From b97c9d5800cc37caa2450b042625870e56b92c60 Mon Sep 17 00:00:00 2001 From: Dmitry Stepanov Date: Thu, 10 Oct 2024 23:56:01 +0300 Subject: [PATCH] pass sceneDepth texture to shaders explicitly - removed built-in uniforms entirely --- fyrox-graphics/src/gl/program.rs | 23 ++----------------- fyrox-graphics/src/gpu_program.rs | 8 ------- .../standard/standard_particle_system.shader | 6 +++-- fyrox-impl/src/renderer/bundle.rs | 19 +++++++-------- fyrox-impl/src/scene/mesh/mod.rs | 1 + 5 files changed, 15 insertions(+), 42 deletions(-) diff --git a/fyrox-graphics/src/gl/program.rs b/fyrox-graphics/src/gl/program.rs index c3b68e331..e2c801bdd 100644 --- a/fyrox-graphics/src/gl/program.rs +++ b/fyrox-graphics/src/gl/program.rs @@ -26,8 +26,8 @@ use crate::{ error::FrameworkError, gl::server::{GlGraphicsServer, GlKind}, gpu_program::{ - BuiltInUniform, BuiltInUniformBlock, GpuProgram, PropertyDefinition, PropertyKind, - SamplerKind, UniformLocation, + BuiltInUniformBlock, GpuProgram, PropertyDefinition, PropertyKind, SamplerKind, + UniformLocation, }, }; use fxhash::FxHashMap; @@ -131,7 +131,6 @@ pub struct GlProgram { // Force compiler to not implement Send and Sync, because OpenGL is not thread-safe. thread_mark: PhantomData<*const u8>, uniform_locations: RefCell>>, - pub built_in_uniform_locations: [Option; BuiltInUniform::Count as usize], pub built_in_uniform_blocks: [Option; BuiltInUniformBlock::Count as usize], } @@ -187,19 +186,6 @@ fn fetch_built_in_uniform_blocks( locations } -fn fetch_built_in_uniform_locations( - server: &GlGraphicsServer, - program: glow::Program, -) -> [Option; BuiltInUniform::Count as usize] { - const INIT: Option = None; - let mut locations = [INIT; BuiltInUniform::Count as usize]; - - locations[BuiltInUniform::SceneDepth as usize] = - fetch_uniform_location(server, program, "fyrox_sceneDepth"); - - locations -} - impl GlProgram { pub fn from_source_and_properties( server: &GlGraphicsServer, @@ -355,7 +341,6 @@ impl GlProgram { id: program, thread_mark: PhantomData, uniform_locations: Default::default(), - built_in_uniform_locations: fetch_built_in_uniform_locations(server, program), built_in_uniform_blocks: fetch_built_in_uniform_blocks(server, program), }) } @@ -396,10 +381,6 @@ impl GpuProgram for GlProgram { self } - fn built_in_uniform_locations(&self) -> &[Option] { - &self.built_in_uniform_locations - } - fn built_in_uniform_blocks(&self) -> &[Option] { &self.built_in_uniform_blocks } diff --git a/fyrox-graphics/src/gpu_program.rs b/fyrox-graphics/src/gpu_program.rs index 9fc8392a0..321c6a238 100644 --- a/fyrox-graphics/src/gpu_program.rs +++ b/fyrox-graphics/src/gpu_program.rs @@ -33,7 +33,6 @@ use std::{any::Any, marker::PhantomData, path::PathBuf}; pub trait GpuProgram: Any { fn as_any(&self) -> &dyn Any; fn as_any_mut(&mut self) -> &mut dyn Any; - fn built_in_uniform_locations(&self) -> &[Option]; fn built_in_uniform_blocks(&self) -> &[Option]; fn uniform_location(&self, name: &ImmutableString) -> Result; fn uniform_block_index(&self, name: &ImmutableString) -> Result; @@ -51,13 +50,6 @@ pub enum BuiltInUniformBlock { Count, } -#[repr(usize)] -pub enum BuiltInUniform { - SceneDepth, - // Must be last. - Count, -} - #[derive(Clone, Debug)] pub struct UniformLocation { pub id: glow::UniformLocation, diff --git a/fyrox-impl/src/material/shader/standard/standard_particle_system.shader b/fyrox-impl/src/material/shader/standard/standard_particle_system.shader index 6cb56d4e8..32dbc5c0a 100644 --- a/fyrox-impl/src/material/shader/standard/standard_particle_system.shader +++ b/fyrox-impl/src/material/shader/standard/standard_particle_system.shader @@ -6,6 +6,10 @@ name: "diffuseTexture", kind: Sampler(default: None, kind: Sampler2D, fallback: White), ), + ( + name: "fyrox_sceneDepth", + kind: Sampler(default: None, kind: Sampler2D, fallback: White), + ), ( name: "softBoundarySharpnessFactor", kind: Float(100.0), @@ -78,8 +82,6 @@ fragment_shader: r#" - uniform sampler2D fyrox_sceneDepth; - layout(std140) uniform FyroxCameraData { TCameraData cameraData; }; diff --git a/fyrox-impl/src/renderer/bundle.rs b/fyrox-impl/src/renderer/bundle.rs index dc1ce4843..3b935c111 100644 --- a/fyrox-impl/src/renderer/bundle.rs +++ b/fyrox-impl/src/renderer/bundle.rs @@ -45,7 +45,7 @@ use crate::{ buffer::Buffer, error::FrameworkError, framebuffer::{FrameBuffer, ResourceBindGroup, ResourceBinding}, - gpu_program::{BuiltInUniform, BuiltInUniformBlock, GpuProgram}, + gpu_program::{BuiltInUniformBlock, GpuProgram}, gpu_texture::GpuTexture, server::GraphicsServer, uniform::StaticUniformBuffer, @@ -173,18 +173,15 @@ impl<'a> BundleRenderContext<'a> { program: &dyn GpuProgram, material_bindings: &mut ArrayVec, ) -> Result<(), FrameworkError> { - let built_in_uniforms = program.built_in_uniform_locations(); - - // Collect texture bindings. - if let Some(location) = &built_in_uniforms[BuiltInUniform::SceneDepth as usize] { - if let Some(scene_depth) = self.scene_depth.as_ref() { - material_bindings.push(ResourceBinding::texture(scene_depth, location)); - } - } - let shader = material.shader().data_ref(); for property in shader.definition.properties.iter() { - if let Some(PropertyValue::Sampler { value, fallback }) = + if property.name.as_str() == "fyrox_sceneDepth" { + if let Some(scene_depth) = self.scene_depth.as_ref() { + if let Ok(location) = program.uniform_location(&property.name) { + material_bindings.push(ResourceBinding::texture(scene_depth, &location)); + } + } + } else if let Some(PropertyValue::Sampler { value, fallback }) = material.properties().get(&property.name) { let texture = value diff --git a/fyrox-impl/src/scene/mesh/mod.rs b/fyrox-impl/src/scene/mesh/mod.rs index 3b0263bf6..0d30c81fa 100644 --- a/fyrox-impl/src/scene/mesh/mod.rs +++ b/fyrox-impl/src/scene/mesh/mod.rs @@ -400,6 +400,7 @@ impl TypeUuidProvider for Mesh { } impl Mesh { + /// Default name of the blend shapes storage property in a shader. pub const DEFAULT_BLEND_SHAPES_PROPERTY_NAME: &'static str = "blendShapesStorage"; /// Sets surfaces for the mesh.