Skip to content

Commit

Permalink
pass sceneDepth texture to shaders explicitly
Browse files Browse the repository at this point in the history
- removed built-in uniforms entirely
  • Loading branch information
mrDIMAS committed Oct 10, 2024
1 parent 02d89d7 commit b97c9d5
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 42 deletions.
23 changes: 2 additions & 21 deletions fyrox-graphics/src/gl/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<FxHashMap<ImmutableString, Option<UniformLocation>>>,
pub built_in_uniform_locations: [Option<UniformLocation>; BuiltInUniform::Count as usize],
pub built_in_uniform_blocks: [Option<usize>; BuiltInUniformBlock::Count as usize],
}

Expand Down Expand Up @@ -187,19 +186,6 @@ fn fetch_built_in_uniform_blocks(
locations
}

fn fetch_built_in_uniform_locations(
server: &GlGraphicsServer,
program: glow::Program,
) -> [Option<UniformLocation>; BuiltInUniform::Count as usize] {
const INIT: Option<UniformLocation> = 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,
Expand Down Expand Up @@ -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),
})
}
Expand Down Expand Up @@ -396,10 +381,6 @@ impl GpuProgram for GlProgram {
self
}

fn built_in_uniform_locations(&self) -> &[Option<UniformLocation>] {
&self.built_in_uniform_locations
}

fn built_in_uniform_blocks(&self) -> &[Option<usize>] {
&self.built_in_uniform_blocks
}
Expand Down
8 changes: 0 additions & 8 deletions fyrox-graphics/src/gpu_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UniformLocation>];
fn built_in_uniform_blocks(&self) -> &[Option<usize>];
fn uniform_location(&self, name: &ImmutableString) -> Result<UniformLocation, FrameworkError>;
fn uniform_block_index(&self, name: &ImmutableString) -> Result<usize, FrameworkError>;
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -78,8 +82,6 @@

fragment_shader:
r#"
uniform sampler2D fyrox_sceneDepth;

layout(std140) uniform FyroxCameraData {
TCameraData cameraData;
};
Expand Down
19 changes: 8 additions & 11 deletions fyrox-impl/src/renderer/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -173,18 +173,15 @@ impl<'a> BundleRenderContext<'a> {
program: &dyn GpuProgram,
material_bindings: &mut ArrayVec<ResourceBinding, 32>,
) -> 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
Expand Down
1 change: 1 addition & 0 deletions fyrox-impl/src/scene/mesh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit b97c9d5

Please sign in to comment.