From a9e8f7db2c8311e09d54231baad480a8e6073d18 Mon Sep 17 00:00:00 2001 From: Dmitry Stepanov Date: Sat, 26 Oct 2024 13:21:03 +0300 Subject: [PATCH] removed duplicated code --- editor/src/highlight.rs | 25 ++++--------------------- fyrox-impl/src/renderer/fxaa.rs | 23 ++++------------------- fyrox-impl/src/renderer/hdr/mod.rs | 9 ++------- fyrox-impl/src/renderer/light/mod.rs | 14 ++------------ fyrox-impl/src/renderer/light_volume.rs | 15 ++------------- fyrox-impl/src/renderer/mod.rs | 16 +++------------- fyrox-impl/src/renderer/ssao/mod.rs | 14 ++------------ 7 files changed, 19 insertions(+), 97 deletions(-) diff --git a/editor/src/highlight.rs b/editor/src/highlight.rs index 186935043..97e774973 100644 --- a/editor/src/highlight.rs +++ b/editor/src/highlight.rs @@ -20,16 +20,11 @@ use crate::{ fyrox::{ - core::{ - algebra::{Matrix4, Vector3}, - color::Color, - pool::Handle, - sstorage::ImmutableString, - }, + core::{color::Color, pool::Handle, sstorage::ImmutableString}, fxhash::FxHashSet, graph::{BaseSceneGraph, SceneGraph}, renderer::{ - bundle::{BundleRenderContext, RenderContext, RenderDataBundleStorage}, + bundle::{BundleRenderContext, ObserverInfo, RenderContext, RenderDataBundleStorage}, framework::{ buffer::BufferUsage, error::FrameworkError, @@ -45,13 +40,12 @@ use crate::{ BlendFactor, BlendFunc, BlendParameters, CompareFunc, DrawParameters, ElementRange, GeometryBufferExt, }, - RenderPassStatistics, SceneRenderPass, SceneRenderPassContext, + make_viewport_matrix, RenderPassStatistics, SceneRenderPass, SceneRenderPassContext, }, scene::{mesh::surface::SurfaceData, node::Node, Scene}, }, Editor, }; -use fyrox::renderer::bundle::ObserverInfo; use std::{any::TypeId, cell::RefCell, rc::Rc}; struct EdgeDetectShader { @@ -255,18 +249,7 @@ impl SceneRenderPass for HighlightRenderPass { // Render full screen quad with edge detect shader to draw outline of selected objects. { - let frame_matrix = Matrix4::new_orthographic( - 0.0, - ctx.viewport.w() as f32, - ctx.viewport.h() as f32, - 0.0, - -1.0, - 1.0, - ) * Matrix4::new_nonuniform_scaling(&Vector3::new( - ctx.viewport.w() as f32, - ctx.viewport.h() as f32, - 0.0, - )); + let frame_matrix = make_viewport_matrix(ctx.viewport); let shader = &self.edge_detect_shader; let frame_texture = self.framebuffer.color_attachments()[0].texture.clone(); ctx.framebuffer.draw( diff --git a/fyrox-impl/src/renderer/fxaa.rs b/fyrox-impl/src/renderer/fxaa.rs index 2ba6ab579..a59faa925 100644 --- a/fyrox-impl/src/renderer/fxaa.rs +++ b/fyrox-impl/src/renderer/fxaa.rs @@ -19,17 +19,14 @@ // SOFTWARE. use crate::{ - core::{ - algebra::{Matrix4, Vector2, Vector3}, - math::Rect, - sstorage::ImmutableString, - }, + core::{algebra::Vector2, math::Rect, sstorage::ImmutableString}, + renderer::make_viewport_matrix, renderer::{ cache::uniform::UniformBufferCache, framework::{ buffer::BufferUsage, error::FrameworkError, - framebuffer::{FrameBuffer, ResourceBindGroup, ResourceBinding}, + framebuffer::{BufferLocation, FrameBuffer, ResourceBindGroup, ResourceBinding}, geometry_buffer::GeometryBuffer, gpu_program::{GpuProgram, UniformLocation}, gpu_texture::GpuTexture, @@ -41,7 +38,6 @@ use crate::{ }, scene::mesh::surface::SurfaceData, }; -use fyrox_graphics::framebuffer::BufferLocation; use std::{cell::RefCell, rc::Rc}; struct FxaaShader { @@ -91,18 +87,7 @@ impl FxaaRenderer { ) -> Result { let mut statistics = RenderPassStatistics::default(); - let frame_matrix = Matrix4::new_orthographic( - 0.0, - viewport.w() as f32, - viewport.h() as f32, - 0.0, - -1.0, - 1.0, - ) * Matrix4::new_nonuniform_scaling(&Vector3::new( - viewport.w() as f32, - viewport.h() as f32, - 0.0, - )); + let frame_matrix = make_viewport_matrix(viewport); statistics += frame_buffer.draw( &*self.quad, diff --git a/fyrox-impl/src/renderer/hdr/mod.rs b/fyrox-impl/src/renderer/hdr/mod.rs index 88b0b290a..81bbc4060 100644 --- a/fyrox-impl/src/renderer/hdr/mod.rs +++ b/fyrox-impl/src/renderer/hdr/mod.rs @@ -20,7 +20,7 @@ use crate::{ core::{ - algebra::{Matrix4, Vector2, Vector3}, + algebra::{Matrix4, Vector2}, color::Color, math::Rect, transmute_slice, value_as_u8_slice, @@ -95,12 +95,7 @@ impl LumBuffer { } fn matrix(&self) -> Matrix4 { - Matrix4::new_orthographic(0.0, self.size as f32, self.size as f32, 0.0, -1.0, 1.0) - * Matrix4::new_nonuniform_scaling(&Vector3::new( - self.size as f32, - self.size as f32, - 0.0, - )) + make_viewport_matrix(Rect::new(0, 0, self.size as i32, self.size as i32)) } fn texture(&self) -> Rc> { diff --git a/fyrox-impl/src/renderer/light/mod.rs b/fyrox-impl/src/renderer/light/mod.rs index a9d33ccf4..959fa0ab5 100644 --- a/fyrox-impl/src/renderer/light/mod.rs +++ b/fyrox-impl/src/renderer/light/mod.rs @@ -18,6 +18,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. +use crate::renderer::make_viewport_matrix; use crate::{ core::{ algebra::{Matrix4, Point3, UnitQuaternion, Vector2, Vector3}, @@ -301,18 +302,7 @@ impl DeferredLightRenderer { let frustum = Frustum::from_view_projection_matrix(camera.view_projection_matrix()) .unwrap_or_default(); - let frame_matrix = Matrix4::new_orthographic( - 0.0, - viewport.w() as f32, - viewport.h() as f32, - 0.0, - -1.0, - 1.0, - ) * Matrix4::new_nonuniform_scaling(&Vector3::new( - viewport.w() as f32, - viewport.h() as f32, - 0.0, - )); + let frame_matrix = make_viewport_matrix(viewport); let projection_matrix = camera.projection_matrix(); let view_projection = camera.view_projection_matrix(); diff --git a/fyrox-impl/src/renderer/light_volume.rs b/fyrox-impl/src/renderer/light_volume.rs index 0aee73c31..b885613c0 100644 --- a/fyrox-impl/src/renderer/light_volume.rs +++ b/fyrox-impl/src/renderer/light_volume.rs @@ -19,6 +19,7 @@ // SOFTWARE. use crate::renderer::bundle::{LightSource, LightSourceKind}; +use crate::renderer::make_viewport_matrix; use crate::{ core::{ algebra::{Isometry3, Matrix4, Point3, Translation, Vector3}, @@ -136,19 +137,7 @@ impl LightVolumeRenderer { ) -> Result { let mut stats = RenderPassStatistics::default(); - let frame_matrix = Matrix4::new_orthographic( - 0.0, - viewport.w() as f32, - viewport.h() as f32, - 0.0, - -1.0, - 1.0, - ) * Matrix4::new_nonuniform_scaling(&Vector3::new( - viewport.w() as f32, - viewport.h() as f32, - 0.0, - )); - + let frame_matrix = make_viewport_matrix(viewport); let position = view.transform_point(&Point3::from(light.position)).coords; match light.kind { diff --git a/fyrox-impl/src/renderer/mod.rs b/fyrox-impl/src/renderer/mod.rs index c60c9ed46..86e2ad7a2 100644 --- a/fyrox-impl/src/renderer/mod.rs +++ b/fyrox-impl/src/renderer/mod.rs @@ -617,7 +617,8 @@ impl AssociatedSceneData { } } -pub(crate) fn make_viewport_matrix(viewport: Rect) -> Matrix4 { +/// Creates a view-projection matrix that projects unit quad a screen with the specified viewport. +pub fn make_viewport_matrix(viewport: Rect) -> Matrix4 { Matrix4::new_orthographic( 0.0, viewport.w() as f32, @@ -865,18 +866,7 @@ fn blit_pixels( viewport: Rect, quad: &dyn GeometryBuffer, ) -> Result { - let matrix = Matrix4::new_orthographic( - 0.0, - viewport.w() as f32, - viewport.h() as f32, - 0.0, - -1.0, - 1.0, - ) * Matrix4::new_nonuniform_scaling(&Vector3::new( - viewport.w() as f32, - viewport.h() as f32, - 0.0, - )); + let matrix = make_viewport_matrix(viewport); let uniform_buffer = uniform_buffer_cache.write(StaticUniformBuffer::<256>::new().with(&matrix))?; framebuffer.draw( diff --git a/fyrox-impl/src/renderer/ssao/mod.rs b/fyrox-impl/src/renderer/ssao/mod.rs index 8f5db1c9c..f8e71f035 100644 --- a/fyrox-impl/src/renderer/ssao/mod.rs +++ b/fyrox-impl/src/renderer/ssao/mod.rs @@ -18,6 +18,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. +use crate::renderer::make_viewport_matrix; use crate::{ core::{ algebra::{Matrix3, Matrix4, Vector2, Vector3}, @@ -198,18 +199,7 @@ impl ScreenSpaceAmbientOcclusionRenderer { let viewport = Rect::new(0, 0, self.width, self.height); - let frame_matrix = Matrix4::new_orthographic( - 0.0, - viewport.w() as f32, - viewport.h() as f32, - 0.0, - -1.0, - 1.0, - ) * Matrix4::new_nonuniform_scaling(&Vector3::new( - viewport.w() as f32, - viewport.h() as f32, - 0.0, - )); + let frame_matrix = make_viewport_matrix(viewport); self.framebuffer.clear( viewport,