Skip to content

Commit

Permalink
uniform buffer for decals
Browse files Browse the repository at this point in the history
  • Loading branch information
mrDIMAS committed Oct 2, 2024
1 parent d880b96 commit 41295cf
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 38 deletions.
18 changes: 3 additions & 15 deletions fyrox-impl/src/renderer/gbuffer/decal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,12 @@ use crate::renderer::framework::{
};

pub struct DecalShader {
pub world_view_projection: UniformLocation,
pub scene_depth: UniformLocation,
pub diffuse_texture: UniformLocation,
pub normal_texture: UniformLocation,
pub inv_view_proj: UniformLocation,
pub inv_world_decal: UniformLocation,
pub resolution: UniformLocation,
pub color: UniformLocation,
pub layer_index: UniformLocation,
pub decal_mask: UniformLocation,
pub program: GpuProgram,
pub uniform_buffer_binding: usize,
}

impl DecalShader {
Expand All @@ -47,20 +42,13 @@ impl DecalShader {
let program =
GpuProgram::from_source(server, "DecalShader", vertex_source, fragment_source)?;
Ok(Self {
world_view_projection: program
.uniform_location(server, &ImmutableString::new("worldViewProjection"))?,
uniform_buffer_binding: program
.uniform_block_index(server, &ImmutableString::new("Uniforms"))?,
scene_depth: program.uniform_location(server, &ImmutableString::new("sceneDepth"))?,
diffuse_texture: program
.uniform_location(server, &ImmutableString::new("diffuseTexture"))?,
normal_texture: program
.uniform_location(server, &ImmutableString::new("normalTexture"))?,
inv_view_proj: program
.uniform_location(server, &ImmutableString::new("invViewProj"))?,
inv_world_decal: program
.uniform_location(server, &ImmutableString::new("invWorldDecal"))?,
resolution: program.uniform_location(server, &ImmutableString::new("resolution"))?,
color: program.uniform_location(server, &ImmutableString::new("color"))?,
layer_index: program.uniform_location(server, &ImmutableString::new("layerIndex"))?,
decal_mask: program.uniform_location(server, &ImmutableString::new("decalMask"))?,
program,
})
Expand Down
29 changes: 17 additions & 12 deletions fyrox-impl/src/renderer/gbuffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ use fxhash::FxHashSet;
use fyrox_graphics::buffer::BufferUsage;
use fyrox_graphics::framebuffer::{ResourceBindGroup, ResourceBinding};
use fyrox_graphics::state::GraphicsServer;
use fyrox_graphics::uniform::StaticUniformBuffer;
use std::{cell::RefCell, rc::Rc};

mod decal;
Expand Down Expand Up @@ -445,21 +446,25 @@ impl GBuffer {
ResourceBinding::texture(&diffuse_texture, &shader.diffuse_texture),
ResourceBinding::texture(&normal_texture, &shader.normal_texture),
ResourceBinding::texture(&decal_mask, &shader.decal_mask),
ResourceBinding::Buffer {
buffer: uniform_buffer_cache.write(
server,
StaticUniformBuffer::<256>::new()
.with(&world_view_proj)
.with(&inv_view_proj)
.with(
&decal.global_transform().try_inverse().unwrap_or_default(),
)
.with(&resolution)
.with(&decal.color().srgb_to_linear_f32())
.with(&(decal.layer() as u32)),
)?,
shader_location: shader.uniform_buffer_binding,
},
],
}],
ElementRange::Full,
&mut |mut program_binding| {
program_binding
.set_matrix4(&shader.world_view_projection, &world_view_proj)
.set_matrix4(&shader.inv_view_proj, &inv_view_proj)
.set_matrix4(
&shader.inv_world_decal,
&decal.global_transform().try_inverse().unwrap_or_default(),
)
.set_vector2(&shader.resolution, &resolution)
.set_u32(&shader.layer_index, decal.layer() as u32)
.set_linear_color(&shader.color, &decal.color());
},
&mut |_| {},
)?;
}

Expand Down
22 changes: 13 additions & 9 deletions fyrox-impl/src/renderer/shaders/decal_fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ uniform sampler2D sceneDepth;
uniform sampler2D diffuseTexture;
uniform sampler2D normalTexture;
uniform usampler2D decalMask;
uniform mat4 invViewProj;
uniform mat4 invWorldDecal;
uniform vec2 resolution;
uniform vec4 color;
uniform uint layerIndex;

layout(location = 0) out vec4 outDiffuseMap;
layout(location = 1) out vec4 outNormalMap;
layout (std140) uniform Uniforms {
mat4 worldViewProjection;
mat4 invViewProj;
mat4 invWorldDecal;
vec2 resolution;
vec4 color;
uint layerIndex;
};

layout (location = 0) out vec4 outDiffuseMap;
layout (location = 1) out vec4 outNormalMap;

in vec4 clipSpacePosition;

Expand All @@ -18,8 +22,8 @@ void main()
vec2 screenPos = clipSpacePosition.xy / clipSpacePosition.w;

vec2 texCoord = vec2(
(1.0 + screenPos.x) / 2.0 + (0.5 / resolution.x),
(1.0 + screenPos.y) / 2.0 + (0.5 / resolution.y)
(1.0 + screenPos.x) / 2.0 + (0.5 / resolution.x),
(1.0 + screenPos.y) / 2.0 + (0.5 / resolution.y)
);

uvec4 maskIndex = texture(decalMask, texCoord);
Expand Down
11 changes: 9 additions & 2 deletions fyrox-impl/src/renderer/shaders/decal_vs.glsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
layout(location = 0) in vec3 vertexPosition;
layout (location = 0) in vec3 vertexPosition;

uniform mat4 worldViewProjection;
layout (std140) uniform Uniforms {
mat4 worldViewProjection;
mat4 invViewProj;
mat4 invWorldDecal;
vec2 resolution;
vec4 color;
uint layerIndex;
};

out vec4 clipSpacePosition;

Expand Down

0 comments on commit 41295cf

Please sign in to comment.