Skip to content

Commit

Permalink
reduced code bloat by saving observer info in render bundle
Browse files Browse the repository at this point in the history
- duplication makes no sense anyway, because shader constants must be the same with which a render bundle was prepared
  • Loading branch information
mrDIMAS committed Oct 25, 2024
1 parent f5535b8 commit 69d0d5e
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 107 deletions.
13 changes: 0 additions & 13 deletions editor/src/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use crate::{
core::{
algebra::{Matrix4, Vector3},
color::Color,
math::Matrix4Ext,
pool::Handle,
sstorage::ImmutableString,
},
Expand Down Expand Up @@ -230,12 +229,6 @@ impl SceneRenderPass for HighlightRenderPass {
self.framebuffer
.clear(ctx.viewport, Some(Color::TRANSPARENT), Some(1.0), None);

let view_projection = ctx.camera.view_projection_matrix();
let inv_view = ctx.camera.inv_view_matrix().unwrap();

let camera_up = inv_view.up();
let camera_side = inv_view.side();

stats += render_bundle_storage.render_to_frame_buffer(
ctx.server,
ctx.geometry_cache,
Expand All @@ -246,12 +239,6 @@ impl SceneRenderPass for HighlightRenderPass {
texture_cache: ctx.texture_cache,
render_pass_name: &render_pass_name,
frame_buffer: &mut *self.framebuffer,
view_projection_matrix: &view_projection,
camera_position: &ctx.camera.global_position(),
camera_up_vector: &camera_up,
camera_side_vector: &camera_side,
z_near: ctx.camera.projection().z_near(),
z_far: ctx.camera.projection().z_far(),
use_pom: false,
light_position: &Default::default(),
fallback_resources: ctx.fallback_resources,
Expand Down
40 changes: 23 additions & 17 deletions fyrox-impl/src/renderer/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ use crate::{
},
};
use fxhash::{FxBuildHasher, FxHashMap, FxHasher};
use fyrox_core::math::Matrix4Ext;
use fyrox_graph::{SceneGraph, SceneGraphNode};
use std::{
cell::RefCell,
Expand Down Expand Up @@ -154,20 +155,12 @@ pub struct BundleRenderContext<'a> {
pub uniform_memory_allocator: &'a mut UniformMemoryAllocator,

// Built-in uniforms.
pub view_projection_matrix: &'a Matrix4<f32>,
pub use_pom: bool,
pub light_position: &'a Vector3<f32>,
pub ambient_light: Color,
// TODO: Add depth pre-pass to remove Option here. Current architecture allows only forward
// renderer to have access to depth buffer that is available from G-Buffer.
pub scene_depth: Option<&'a Rc<RefCell<dyn GpuTexture>>>,

pub camera_position: &'a Vector3<f32>,
pub camera_up_vector: &'a Vector3<f32>,
pub camera_side_vector: &'a Vector3<f32>,
pub z_near: f32,
pub z_far: f32,

pub fallback_resources: &'a FallbackResources,
}

Expand Down Expand Up @@ -354,6 +347,7 @@ impl RenderDataBundle {
/// Writes all the required uniform data of the bundle to uniform memory allocator.
pub fn write_uniforms(
&self,
view_projection_matrix: &Matrix4<f32>,
render_context: &mut BundleRenderContext,
) -> Option<BundleUniformData> {
let mut material_state = self.material.state();
Expand Down Expand Up @@ -404,7 +398,7 @@ impl RenderDataBundle {
for instance in self.instances.iter() {
let instance_buffer = StaticUniformBuffer::<1024>::new()
.with(&instance.world_transform)
.with(&(render_context.view_projection_matrix * instance.world_transform))
.with(&(view_projection_matrix * instance.world_transform))
.with(&(instance.blend_shapes_weights.len() as i32))
.with(&(!instance.bone_matrices.is_empty()))
.with_slice_with_max_size(
Expand Down Expand Up @@ -733,6 +727,7 @@ pub struct LightSource {
#[derive(Default)]
pub struct RenderDataBundleStorage {
bundle_map: FxHashMap<u64, usize>,
pub observer_info: ObserverInfo,
/// A sorted list of bundles.
pub bundles: Vec<RenderDataBundle>,
pub light_sources: Vec<LightSource>,
Expand Down Expand Up @@ -764,6 +759,7 @@ impl RenderDataBundleStorage {
let capacity = graph.node_count() as usize;
let mut storage = Self {
bundle_map: FxHashMap::with_capacity_and_hasher(capacity, FxBuildHasher::default()),
observer_info: observer_info.clone(),
bundles: Vec::with_capacity(capacity),
light_sources: Default::default(),
};
Expand Down Expand Up @@ -938,14 +934,22 @@ impl RenderDataBundleStorage {
.allocate(lights_data);

// Upload camera uniforms.
let inv_view = self
.observer_info
.view_matrix
.try_inverse()
.unwrap_or_default();
let view_projection = self.observer_info.projection_matrix * self.observer_info.view_matrix;
let camera_up = inv_view.up();
let camera_side = inv_view.side();
let camera_uniforms = StaticUniformBuffer::<512>::new()
.with(render_context.view_projection_matrix)
.with(render_context.camera_position)
.with(render_context.camera_up_vector)
.with(render_context.camera_side_vector)
.with(&render_context.z_near)
.with(&render_context.z_far)
.with(&(render_context.z_far - render_context.z_near));
.with(&view_projection)
.with(&self.observer_info.observer_position)
.with(&camera_up)
.with(&camera_side)
.with(&self.observer_info.z_near)
.with(&self.observer_info.z_far)
.with(&(self.observer_info.z_far - self.observer_info.z_near));
let camera_block = render_context
.uniform_memory_allocator
.allocate(camera_uniforms);
Expand Down Expand Up @@ -978,12 +982,14 @@ impl RenderDataBundleStorage {
{
let global_uniforms = self.write_global_uniform_blocks(&mut render_context);

let view_projection = self.observer_info.projection_matrix * self.observer_info.view_matrix;
let mut bundle_uniform_data_set = Vec::with_capacity(self.bundles.len());
for bundle in self.bundles.iter() {
if !bundle_filter(bundle) {
continue;
}
bundle_uniform_data_set.push(bundle.write_uniforms(&mut render_context));
bundle_uniform_data_set
.push(bundle.write_uniforms(&view_projection, &mut render_context));
}
render_context.uniform_memory_allocator.upload(server)?;

Expand Down
25 changes: 3 additions & 22 deletions fyrox-impl/src/renderer/forward_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@
//! Forward renderer is used to render transparent meshes and meshes with custom blending options.

use crate::{
core::{
color::Color,
math::{Matrix4Ext, Rect},
sstorage::ImmutableString,
},
core::{color::Color, math::Rect, sstorage::ImmutableString},
renderer::{
bundle::{BundleRenderContext, RenderDataBundleStorage},
cache::{
Expand All @@ -39,17 +35,16 @@ use crate::{
},
FallbackResources, GeometryCache, QualitySettings, RenderPassStatistics,
},
scene::{camera::Camera, mesh::RenderPath},
scene::mesh::RenderPath,
};
use std::{cell::RefCell, rc::Rc};

pub(crate) struct ForwardRenderer {
render_pass_name: ImmutableString,
}

pub(crate) struct ForwardRenderContext<'a, 'b> {
pub(crate) struct ForwardRenderContext<'a> {
pub state: &'a dyn GraphicsServer,
pub camera: &'b Camera,
pub geom_cache: &'a mut GeometryCache,
pub texture_cache: &'a mut TextureCache,
pub shader_cache: &'a mut ShaderCache,
Expand Down Expand Up @@ -79,7 +74,6 @@ impl ForwardRenderer {

let ForwardRenderContext {
state,
camera,
geom_cache,
texture_cache,
shader_cache,
Expand All @@ -94,13 +88,6 @@ impl ForwardRenderer {
uniform_memory_allocator,
} = args;

let view_projection = camera.view_projection_matrix();

let inv_view = camera.inv_view_matrix().unwrap();

let camera_up = inv_view.up();
let camera_side = inv_view.side();

statistics += bundle_storage.render_to_frame_buffer(
state,
geom_cache,
Expand All @@ -114,12 +101,6 @@ impl ForwardRenderer {
viewport,
uniform_buffer_cache,
uniform_memory_allocator,
view_projection_matrix: &view_projection,
camera_position: &camera.global_position(),
camera_up_vector: &camera_up,
camera_side_vector: &camera_side,
z_near: camera.projection().z_near(),
z_far: camera.projection().z_far(),
use_pom: quality_settings.use_parallax_mapping,
light_position: &Default::default(),
fallback_resources,
Expand Down
13 changes: 1 addition & 12 deletions fyrox-impl/src/renderer/gbuffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::{
core::{
algebra::{Matrix4, Vector2},
color::Color,
math::{Matrix4Ext, Rect},
math::Rect,
sstorage::ImmutableString,
},
renderer::{
Expand Down Expand Up @@ -232,11 +232,6 @@ impl GBuffer {
Some(0),
);

let inv_view = camera.inv_view_matrix().unwrap();

let camera_up = inv_view.up();
let camera_side = inv_view.side();

let grid_cell = self
.occlusion_tester
.grid_cache
Expand All @@ -260,17 +255,11 @@ impl GBuffer {
viewport,
uniform_buffer_cache,
uniform_memory_allocator,
view_projection_matrix: &view_projection,
camera_position: &camera.global_position(),
camera_up_vector: &camera_up,
camera_side_vector: &camera_side,
z_near: camera.projection().z_near(),
use_pom: quality_settings.use_parallax_mapping,
light_position: &Default::default(),
fallback_resources,
ambient_light: Color::WHITE, // TODO
scene_depth: None, // TODO. Add z-pre-pass.
z_far: camera.projection().z_far(),
},
)?;

Expand Down
1 change: 0 additions & 1 deletion fyrox-impl/src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1525,7 +1525,6 @@ impl Renderer {
scene_associated_data.statistics +=
self.forward_renderer.render(ForwardRenderContext {
state: server,
camera,
geom_cache: &mut self.geometry_cache,
texture_cache: &mut self.texture_cache,
shader_cache: &mut self.shader_cache,
Expand Down
12 changes: 1 addition & 11 deletions fyrox-impl/src/renderer/shadow/csm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
core::{
algebra::{Matrix4, Point3, Vector2, Vector3},
color::Color,
math::{aabb::AxisAlignedBoundingBox, frustum::Frustum, Matrix4Ext, Rect},
math::{aabb::AxisAlignedBoundingBox, frustum::Frustum, Rect},
},
renderer::{
bundle::{
Expand Down Expand Up @@ -244,10 +244,6 @@ impl CsmRenderer {
aabb.min.x, aabb.max.x, aabb.min.y, aabb.max.y, aabb.min.z, aabb.max.z,
);

let inv_view = light_view_matrix.try_inverse().unwrap();
let camera_up = inv_view.up();
let camera_side = inv_view.side();

let light_view_projection = cascade_projection_matrix * light_view_matrix;
self.cascades[i].view_proj_matrix = light_view_projection;
self.cascades[i].z_far = z_far;
Expand Down Expand Up @@ -284,17 +280,11 @@ impl CsmRenderer {
viewport,
uniform_buffer_cache,
uniform_memory_allocator,
view_projection_matrix: &light_view_projection,
camera_position: &camera.global_position(),
camera_up_vector: &camera_up,
camera_side_vector: &camera_side,
z_near,
use_pom: false,
light_position: &Default::default(),
fallback_resources,
ambient_light: Color::WHITE, // TODO
scene_depth: None,
z_far,
},
)?;
}
Expand Down
28 changes: 9 additions & 19 deletions fyrox-impl/src/renderer/shadow/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::renderer::bundle::RenderDataBundleStorageOptions;
use crate::renderer::FallbackResources;
use crate::{
core::{
algebra::{Matrix4, Point3, Vector3},
color::Color,
math::{Matrix4Ext, Rect},
math::Rect,
},
renderer::{
bundle::{BundleRenderContext, ObserverInfo, RenderDataBundleStorage},
bundle::{
BundleRenderContext, ObserverInfo, RenderDataBundleStorage,
RenderDataBundleStorageOptions,
},
cache::{
shader::ShaderCache,
texture::TextureCache,
Expand All @@ -37,17 +38,17 @@ use crate::{
error::FrameworkError,
framebuffer::{Attachment, AttachmentKind, FrameBuffer},
gpu_texture::{
CubeMapFace, GpuTexture, GpuTextureKind, MagnificationFilter, MinificationFilter,
PixelKind, WrapMode,
CubeMapFace, GpuTexture, GpuTextureDescriptor, GpuTextureKind, MagnificationFilter,
MinificationFilter, PixelKind, WrapMode,
},
server::GraphicsServer,
},
shadow::cascade_size,
GeometryCache, RenderPassStatistics, ShadowMapPrecision, POINT_SHADOW_PASS_NAME,
FallbackResources, GeometryCache, RenderPassStatistics, ShadowMapPrecision,
POINT_SHADOW_PASS_NAME,
},
scene::graph::Graph,
};
use fyrox_graphics::gpu_texture::GpuTextureDescriptor;
use std::{cell::RefCell, rc::Rc};

pub struct PointShadowMapRenderer {
Expand Down Expand Up @@ -222,11 +223,6 @@ impl PointShadowMapRenderer {
&Point3::from(light_look_at),
&face.up,
);
let light_view_projection_matrix = light_projection_matrix * light_view_matrix;

let inv_view = light_view_matrix.try_inverse().unwrap();
let camera_up = inv_view.up();
let camera_side = inv_view.side();

let bundle_storage = RenderDataBundleStorage::from_graph(
graph,
Expand Down Expand Up @@ -256,17 +252,11 @@ impl PointShadowMapRenderer {
viewport,
uniform_buffer_cache,
uniform_memory_allocator,
view_projection_matrix: &light_view_projection_matrix,
camera_position: &Default::default(),
camera_up_vector: &camera_up,
camera_side_vector: &camera_side,
z_near,
use_pom: false,
light_position: &light_pos,
fallback_resources,
ambient_light: Color::WHITE, // TODO
scene_depth: None,
z_far,
},
)?;
}
Expand Down
Loading

0 comments on commit 69d0d5e

Please sign in to comment.