From 516ed3e9a0c2ac3e19611cad99709f12a7346d19 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Fri, 28 Jun 2024 21:21:17 +0200 Subject: [PATCH] Cleanup some rendering code --- Source/Engine/Foliage/Foliage.cpp | 1 - Source/Engine/Graphics/Models/Mesh.cpp | 12 +++--------- Source/Engine/Graphics/Models/SkinnedMesh.cpp | 8 ++------ Source/Engine/Graphics/RenderTools.h | 5 +++++ Source/Engine/Level/Actors/Skybox.cpp | 5 +++-- Source/Engine/Renderer/Editor/QuadOverdrawPass.cpp | 3 ++- Source/Engine/Renderer/RenderList.cpp | 2 +- Source/Engine/Terrain/TerrainChunk.cpp | 5 +++-- Source/Engine/UI/TextRender.cpp | 7 ++----- Source/Shaders/MaterialCommon.hlsl | 9 +-------- 10 files changed, 22 insertions(+), 35 deletions(-) diff --git a/Source/Engine/Foliage/Foliage.cpp b/Source/Engine/Foliage/Foliage.cpp index 39451207d..562a8b706 100644 --- a/Source/Engine/Foliage/Foliage.cpp +++ b/Source/Engine/Foliage/Foliage.cpp @@ -490,7 +490,6 @@ void Foliage::DrawType(RenderContext& renderContext, const FoliageType& type, Dr batch.DrawCall.World.SetRow4(Float4(firstInstance.InstanceOrigin, 1.0f)); batch.DrawCall.Surface.PrevWorld = batch.DrawCall.World; batch.DrawCall.Surface.GeometrySize = mesh.GetBox().GetSize(); - batch.DrawCall.Surface.Skinning = nullptr; batch.DrawCall.WorldDeterminantSign = 1; if (EnumHasAnyFlags(drawModes, DrawPass::Forward)) diff --git a/Source/Engine/Graphics/Models/Mesh.cpp b/Source/Engine/Graphics/Models/Mesh.cpp index 39439af5c..fda0550ba 100644 --- a/Source/Engine/Graphics/Models/Mesh.cpp +++ b/Source/Engine/Graphics/Models/Mesh.cpp @@ -407,11 +407,7 @@ void Mesh::Draw(const RenderContext& renderContext, MaterialBase* material, cons drawCall.ObjectRadius = (float)_sphere.Radius * drawCall.World.GetScaleVector().GetAbsolute().MaxValue(); drawCall.Surface.GeometrySize = _box.GetSize(); drawCall.Surface.PrevWorld = world; - drawCall.Surface.Lightmap = nullptr; - drawCall.Surface.LightmapUVsArea = Rectangle::Empty; - drawCall.Surface.Skinning = nullptr; - drawCall.Surface.LODDitherFactor = 0.0f; - drawCall.WorldDeterminantSign = Math::FloatSelect(world.RotDeterminant(), 1, -1); + drawCall.WorldDeterminantSign = RenderTools::GetWorldDeterminantSign(drawCall.World); drawCall.PerInstanceRandom = perInstanceRandom; #if USE_EDITOR const ViewMode viewMode = renderContext.View.Mode; @@ -477,9 +473,8 @@ void Mesh::Draw(const RenderContext& renderContext, const DrawInfo& info, float drawCall.Surface.PrevWorld = info.DrawState->PrevWorld; drawCall.Surface.Lightmap = (info.Flags & StaticFlags::Lightmap) != StaticFlags::None ? info.Lightmap : nullptr; drawCall.Surface.LightmapUVsArea = info.LightmapUVs ? *info.LightmapUVs : Rectangle::Empty; - drawCall.Surface.Skinning = nullptr; drawCall.Surface.LODDitherFactor = lodDitherFactor; - drawCall.WorldDeterminantSign = Math::FloatSelect(drawCall.World.RotDeterminant(), 1, -1); + drawCall.WorldDeterminantSign = RenderTools::GetWorldDeterminantSign(drawCall.World); drawCall.PerInstanceRandom = info.PerInstanceRandom; #if USE_EDITOR const ViewMode viewMode = renderContext.View.Mode; @@ -539,9 +534,8 @@ void Mesh::Draw(const RenderContextBatch& renderContextBatch, const DrawInfo& in drawCall.Surface.PrevWorld = info.DrawState->PrevWorld; drawCall.Surface.Lightmap = (info.Flags & StaticFlags::Lightmap) != StaticFlags::None ? info.Lightmap : nullptr; drawCall.Surface.LightmapUVsArea = info.LightmapUVs ? *info.LightmapUVs : Rectangle::Empty; - drawCall.Surface.Skinning = nullptr; drawCall.Surface.LODDitherFactor = lodDitherFactor; - drawCall.WorldDeterminantSign = Math::FloatSelect(drawCall.World.RotDeterminant(), 1, -1); + drawCall.WorldDeterminantSign = RenderTools::GetWorldDeterminantSign(drawCall.World); drawCall.PerInstanceRandom = info.PerInstanceRandom; #if USE_EDITOR const ViewMode viewMode = renderContextBatch.GetMainContext().View.Mode; diff --git a/Source/Engine/Graphics/Models/SkinnedMesh.cpp b/Source/Engine/Graphics/Models/SkinnedMesh.cpp index 06c1201ef..207ff8991 100644 --- a/Source/Engine/Graphics/Models/SkinnedMesh.cpp +++ b/Source/Engine/Graphics/Models/SkinnedMesh.cpp @@ -249,11 +249,9 @@ void SkinnedMesh::Draw(const RenderContext& renderContext, const DrawInfo& info, drawCall.ObjectRadius = (float)info.Bounds.Radius; // TODO: should it be kept in sync with ObjectPosition? drawCall.Surface.GeometrySize = _box.GetSize(); drawCall.Surface.PrevWorld = info.DrawState->PrevWorld; - drawCall.Surface.Lightmap = nullptr; - drawCall.Surface.LightmapUVsArea = Rectangle::Empty; drawCall.Surface.Skinning = info.Skinning; drawCall.Surface.LODDitherFactor = lodDitherFactor; - drawCall.WorldDeterminantSign = Math::FloatSelect(drawCall.World.RotDeterminant(), 1, -1); + drawCall.WorldDeterminantSign = RenderTools::GetWorldDeterminantSign(drawCall.World); drawCall.PerInstanceRandom = info.PerInstanceRandom; // Push draw call to the render list @@ -292,11 +290,9 @@ void SkinnedMesh::Draw(const RenderContextBatch& renderContextBatch, const DrawI drawCall.ObjectRadius = (float)info.Bounds.Radius; // TODO: should it be kept in sync with ObjectPosition? drawCall.Surface.GeometrySize = _box.GetSize(); drawCall.Surface.PrevWorld = info.DrawState->PrevWorld; - drawCall.Surface.Lightmap = nullptr; - drawCall.Surface.LightmapUVsArea = Rectangle::Empty; drawCall.Surface.Skinning = info.Skinning; drawCall.Surface.LODDitherFactor = lodDitherFactor; - drawCall.WorldDeterminantSign = Math::FloatSelect(drawCall.World.RotDeterminant(), 1, -1); + drawCall.WorldDeterminantSign = RenderTools::GetWorldDeterminantSign(drawCall.World); drawCall.PerInstanceRandom = info.PerInstanceRandom; // Push draw call to the render lists diff --git a/Source/Engine/Graphics/RenderTools.h b/Source/Engine/Graphics/RenderTools.h index 8d2873c29..3f3cab65f 100644 --- a/Source/Engine/Graphics/RenderTools.h +++ b/Source/Engine/Graphics/RenderTools.h @@ -29,6 +29,11 @@ public: return mipSlice + arraySlice * mipLevels; } + FORCE_INLINE static float GetWorldDeterminantSign(const Matrix& worldMatrix) + { + return Math::FloatSelect(worldMatrix.RotDeterminant(), 1, -1); + } + /// /// Computes the feature level for the given shader profile. /// diff --git a/Source/Engine/Level/Actors/Skybox.cpp b/Source/Engine/Level/Actors/Skybox.cpp index e61800d0a..e59e3a3e4 100644 --- a/Source/Engine/Level/Actors/Skybox.cpp +++ b/Source/Engine/Level/Actors/Skybox.cpp @@ -3,10 +3,11 @@ #include "Skybox.h" #include "Engine/Core/Math/Color.h" #include "Engine/Core/Types/Variant.h" -#include "Engine/Graphics/RenderView.h" #include "Engine/Renderer/RenderList.h" #include "Engine/Serialization/Serialization.h" +#include "Engine/Graphics/RenderView.h" #include "Engine/Graphics/RenderTask.h" +#include "Engine/Graphics/RenderTools.h" #include "Engine/Level/Scene/SceneRendering.h" #include "Engine/Content/Assets/Material.h" #include "Engine/Content/Content.h" @@ -101,7 +102,7 @@ void Skybox::ApplySky(GPUContext* context, RenderContext& renderContext, const M drawCall.ObjectPosition = drawCall.World.GetTranslation(); drawCall.ObjectRadius = (float)_sphere.Radius; drawCall.Surface.GeometrySize = _box.GetSize(); - drawCall.WorldDeterminantSign = Math::FloatSelect(world.RotDeterminant(), 1, -1); + drawCall.WorldDeterminantSign = RenderTools::GetWorldDeterminantSign(drawCall.World); drawCall.PerInstanceRandom = GetPerInstanceRandom(); MaterialBase::BindParameters bindParams(context, renderContext, drawCall); bindParams.BindViewData(); diff --git a/Source/Engine/Renderer/Editor/QuadOverdrawPass.cpp b/Source/Engine/Renderer/Editor/QuadOverdrawPass.cpp index 2674c66bd..fda1bcc4c 100644 --- a/Source/Engine/Renderer/Editor/QuadOverdrawPass.cpp +++ b/Source/Engine/Renderer/Editor/QuadOverdrawPass.cpp @@ -14,6 +14,7 @@ #include "Engine/Graphics/RenderBuffers.h" #include "Engine/Graphics/RenderTargetPool.h" #include "Engine/Renderer/RenderList.h" +#include "Engine/Graphics/RenderTools.h" void QuadOverdrawPass::Render(RenderContext& renderContext, GPUContext* context, GPUTextureView* lightBuffer) { @@ -82,7 +83,7 @@ void QuadOverdrawPass::Render(RenderContext& renderContext, GPUContext* context, m1 *= m2; drawCall.World = m1; drawCall.ObjectPosition = drawCall.World.GetTranslation(); - drawCall.WorldDeterminantSign = Math::FloatSelect(drawCall.World.RotDeterminant(), 1, -1); + drawCall.WorldDeterminantSign = RenderTools::GetWorldDeterminantSign(drawCall.World); skyMaterial->Bind(bindParams); skyModel->Render(context); } diff --git a/Source/Engine/Renderer/RenderList.cpp b/Source/Engine/Renderer/RenderList.cpp index bf38675c7..154fed7c4 100644 --- a/Source/Engine/Renderer/RenderList.cpp +++ b/Source/Engine/Renderer/RenderList.cpp @@ -691,7 +691,6 @@ void RenderList::ExecuteDrawCalls(const RenderContext& renderContext, DrawCallsL // Prepare instance buffer if (useInstancing) { - PROFILE_CPU_NAMED("Build Instancing"); int32 instancedBatchesCount = 0; for (int32 i = 0; i < list.Batches.Count(); i++) { @@ -707,6 +706,7 @@ void RenderList::ExecuteDrawCalls(const RenderContext& renderContext, DrawCallsL } if (instancedBatchesCount != 0) { + PROFILE_CPU_NAMED("Build Instancing"); _instanceBuffer.Clear(); _instanceBuffer.Data.Resize(instancedBatchesCount * sizeof(InstanceData)); auto instanceData = (InstanceData*)_instanceBuffer.Data.Get(); diff --git a/Source/Engine/Terrain/TerrainChunk.cpp b/Source/Engine/Terrain/TerrainChunk.cpp index 6902b5d8d..fa483eb39 100644 --- a/Source/Engine/Terrain/TerrainChunk.cpp +++ b/Source/Engine/Terrain/TerrainChunk.cpp @@ -9,6 +9,7 @@ #include "Engine/Graphics/RenderTask.h" #include "Engine/Graphics/Textures/GPUTexture.h" #include "Engine/Renderer/RenderList.h" +#include "Engine/Graphics/RenderTools.h" #include "Engine/Core/Math/OrientedBoundingBox.h" #include "Engine/Level/Scene/Scene.h" #if USE_EDITOR @@ -121,7 +122,7 @@ void TerrainChunk::Draw(const RenderContext& renderContext) const drawCall.Terrain.Lightmap = nullptr; drawCall.Terrain.LightmapUVsArea = Rectangle::Empty; } - drawCall.WorldDeterminantSign = Math::FloatSelect(drawCall.World.RotDeterminant(), 1, -1); + drawCall.WorldDeterminantSign = RenderTools::GetWorldDeterminantSign(drawCall.World); drawCall.PerInstanceRandom = _perInstanceRandom; // Add half-texel offset for heightmap sampling in vertex shader @@ -178,7 +179,7 @@ void TerrainChunk::Draw(const RenderContext& renderContext, MaterialBase* materi drawCall.Terrain.Lightmap = nullptr; drawCall.Terrain.LightmapUVsArea = Rectangle::Empty; } - drawCall.WorldDeterminantSign = Math::FloatSelect(drawCall.World.RotDeterminant(), 1, -1); + drawCall.WorldDeterminantSign = RenderTools::GetWorldDeterminantSign(drawCall.World); drawCall.PerInstanceRandom = _perInstanceRandom; // Add half-texel offset for heightmap sampling in vertex shader diff --git a/Source/Engine/UI/TextRender.cpp b/Source/Engine/UI/TextRender.cpp index 8640791f5..5a18d915c 100644 --- a/Source/Engine/UI/TextRender.cpp +++ b/Source/Engine/UI/TextRender.cpp @@ -17,6 +17,7 @@ #include "Engine/Content/Assets/MaterialInstance.h" #include "Engine/Content/Content.h" #include "Engine/Core/Types/Variant.h" +#include "Engine/Graphics/RenderTools.h" #include "Engine/Localization/Localization.h" #if USE_EDITOR #include "Editor/Editor.h" @@ -369,11 +370,7 @@ void TextRender::Draw(RenderContext& renderContext) drawCall.ObjectRadius = (float)_sphere.Radius; drawCall.Surface.GeometrySize = _localBox.GetSize(); drawCall.Surface.PrevWorld = _drawState.PrevWorld; - drawCall.Surface.Lightmap = nullptr; - drawCall.Surface.LightmapUVsArea = Rectangle::Empty; - drawCall.Surface.Skinning = nullptr; - drawCall.Surface.LODDitherFactor = 0.0f; - drawCall.WorldDeterminantSign = Math::FloatSelect(world.RotDeterminant(), 1, -1); + drawCall.WorldDeterminantSign = RenderTools::GetWorldDeterminantSign(drawCall.World); drawCall.PerInstanceRandom = GetPerInstanceRandom(); drawCall.Geometry.IndexBuffer = _ib.GetBuffer(); drawCall.Geometry.VertexBuffers[0] = _vb0.GetBuffer(); diff --git a/Source/Shaders/MaterialCommon.hlsl b/Source/Shaders/MaterialCommon.hlsl index 7fcfae018..0660e5cf9 100644 --- a/Source/Shaders/MaterialCommon.hlsl +++ b/Source/Shaders/MaterialCommon.hlsl @@ -118,7 +118,7 @@ struct ModelInput float4 Tangent : TANGENT; float2 LightmapUV : TEXCOORD1; #if USE_VERTEX_COLOR - half4 Color : COLOR; + half4 Color : COLOR; #endif #if USE_INSTANCING float4 InstanceOrigin : ATTRIBUTE0; // .w contains PerInstanceRandom @@ -149,13 +149,6 @@ struct ModelInput_Skinned float4 Tangent : TANGENT; uint4 BlendIndices : BLENDINDICES; float4 BlendWeights : BLENDWEIGHT; -#if USE_INSTANCING - float4 InstanceOrigin : ATTRIBUTE0; // .w contains PerInstanceRandom - float4 InstanceTransform1 : ATTRIBUTE1; // .w contains LODDitherFactor - float3 InstanceTransform2 : ATTRIBUTE2; - float3 InstanceTransform3 : ATTRIBUTE3; - half4 InstanceLightmapArea : ATTRIBUTE4; -#endif }; struct Model_VS2PS