From 835a230323c99115b78a772dbac7a4bef91d7267 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sun, 27 Nov 2022 12:06:31 +0100 Subject: [PATCH] Refactor material shaders to use separate constant buffer (slot 1) for shared per-view constants --- Content/Editor/MaterialTemplates/Decal.shader | 9 +--- .../MaterialTemplates/Deformable.shader | 10 +--- .../Editor/MaterialTemplates/Particle.shader | 9 +--- .../Editor/MaterialTemplates/Surface.shader | 13 +---- .../Editor/MaterialTemplates/Terrain.shader | 9 +--- .../MaterialTemplates/VolumeParticle.shader | 9 +--- Source/Engine/Content/Assets/Material.cpp | 2 +- .../Materials/DecalMaterialShader.cpp | 16 ------- .../Materials/DeferredMaterialShader.cpp | 25 +--------- .../Materials/DeformableMaterialShader.cpp | 18 ------- .../Materials/ForwardMaterialShader.cpp | 25 ---------- Source/Engine/Graphics/Materials/IMaterial.h | 7 +++ .../Graphics/Materials/MaterialShader.cpp | 47 +++++++++++++++++++ .../Graphics/Materials/MaterialShader.h | 2 +- .../Materials/ParticleMaterialShader.cpp | 16 ------- .../Materials/TerrainMaterialShader.cpp | 16 ------- .../VolumeParticleMaterialShader.cpp | 16 ------- .../DirectX/DX11/GPUContextDX11.cpp | 8 ---- Source/Engine/Level/Actors/Skybox.cpp | 1 + .../Renderer/Editor/MaterialComplexity.cpp | 1 + .../Renderer/Editor/QuadOverdrawPass.cpp | 1 + Source/Engine/Renderer/GBufferPass.cpp | 1 + Source/Engine/Renderer/RenderList.cpp | 1 + Source/Engine/Renderer/Renderer.cpp | 1 + Source/Engine/Renderer/VolumetricFogPass.cpp | 1 + Source/Shaders/MaterialCommon.hlsl | 22 +++++++++ 26 files changed, 92 insertions(+), 194 deletions(-) diff --git a/Content/Editor/MaterialTemplates/Decal.shader b/Content/Editor/MaterialTemplates/Decal.shader index a304b8c6e..58ef9492c 100644 --- a/Content/Editor/MaterialTemplates/Decal.shader +++ b/Content/Editor/MaterialTemplates/Decal.shader @@ -2,6 +2,7 @@ // Version: @0 #define MATERIAL 1 +#define USE_PER_VIEW_CONSTANTS 1 @3 #include "./Flax/Common.hlsl" @@ -10,17 +11,9 @@ @7 // Primary constant buffer (with additional material parameters) META_CB_BEGIN(0, Data) -float4x4 ViewProjectionMatrix; float4x4 WorldMatrix; -float4x4 ViewMatrix; float4x4 InvWorld; float4x4 SVPositionToWorld; -float3 ViewPos; -float ViewFar; -float3 ViewDir; -float TimeParam; -float4 ViewInfo; -float4 ScreenSize; @1META_CB_END // Use depth buffer for per-pixel decal layering diff --git a/Content/Editor/MaterialTemplates/Deformable.shader b/Content/Editor/MaterialTemplates/Deformable.shader index ace304e68..5be86e33b 100644 --- a/Content/Editor/MaterialTemplates/Deformable.shader +++ b/Content/Editor/MaterialTemplates/Deformable.shader @@ -2,6 +2,7 @@ // Version: @0 #define MATERIAL 1 +#define USE_PER_VIEW_CONSTANTS 1 @3 #include "./Flax/Common.hlsl" #include "./Flax/MaterialCommon.hlsl" @@ -9,23 +10,14 @@ @7 // Primary constant buffer (with additional material parameters) META_CB_BEGIN(0, Data) -float4x4 ViewProjectionMatrix; float4x4 WorldMatrix; float4x4 LocalMatrix; -float4x4 ViewMatrix; -float3 ViewPos; -float ViewFar; -float3 ViewDir; -float TimeParam; -float4 ViewInfo; -float4 ScreenSize; float3 Dummy0; float WorldDeterminantSign; float MeshMinZ; float Segment; float ChunksPerSegment; float PerInstanceRandom; -float4 TemporalAAJitter; float3 GeometrySize; float MeshMaxZ; @1META_CB_END diff --git a/Content/Editor/MaterialTemplates/Particle.shader b/Content/Editor/MaterialTemplates/Particle.shader index ec73cd072..18a270cc0 100644 --- a/Content/Editor/MaterialTemplates/Particle.shader +++ b/Content/Editor/MaterialTemplates/Particle.shader @@ -2,6 +2,7 @@ // Version: @0 #define MATERIAL 1 +#define USE_PER_VIEW_CONSTANTS 1 @3 // Ribbons don't use sorted indices so overlap the segment distances buffer on the slot #define HAS_SORTED_INDICES (!defined(_VS_Ribbon)) @@ -19,15 +20,7 @@ struct SpriteInput // Primary constant buffer (with additional material parameters) META_CB_BEGIN(0, Data) -float4x4 ViewProjectionMatrix; float4x4 WorldMatrix; -float4x4 ViewMatrix; -float3 ViewPos; -float ViewFar; -float3 ViewDir; -float TimeParam; -float4 ViewInfo; -float4 ScreenSize; uint SortedIndicesOffset; float PerInstanceRandom; int ParticleStride; diff --git a/Content/Editor/MaterialTemplates/Surface.shader b/Content/Editor/MaterialTemplates/Surface.shader index 150da0441..0e7c948a1 100644 --- a/Content/Editor/MaterialTemplates/Surface.shader +++ b/Content/Editor/MaterialTemplates/Surface.shader @@ -2,6 +2,7 @@ // Version: @0 #define MATERIAL 1 +#define USE_PER_VIEW_CONSTANTS 1 @3 #include "./Flax/Common.hlsl" #include "./Flax/MaterialCommon.hlsl" @@ -9,23 +10,11 @@ @7 // Primary constant buffer (with additional material parameters) META_CB_BEGIN(0, Data) -float4x4 ViewProjectionMatrix; float4x4 WorldMatrix; -float4x4 ViewMatrix; -float4x4 PrevViewProjectionMatrix; float4x4 PrevWorldMatrix; -float4x4 MainViewProjectionMatrix; -float4 MainScreenSize; -float3 ViewPos; -float ViewFar; -float3 ViewDir; -float TimeParam; -float4 ViewInfo; -float4 ScreenSize; float2 Dummy0; float LODDitherFactor; float PerInstanceRandom; -float4 TemporalAAJitter; float3 GeometrySize; float WorldDeterminantSign; @1META_CB_END diff --git a/Content/Editor/MaterialTemplates/Terrain.shader b/Content/Editor/MaterialTemplates/Terrain.shader index 1326bd0eb..b60ca2a3c 100644 --- a/Content/Editor/MaterialTemplates/Terrain.shader +++ b/Content/Editor/MaterialTemplates/Terrain.shader @@ -2,6 +2,7 @@ // Version: @0 #define MATERIAL 1 +#define USE_PER_VIEW_CONSTANTS 1 @3 // Enables/disables smooth terrain chunks LOD transitions (with morphing higher LOD near edges to the lower LOD in the neighbour) #define USE_SMOOTH_LOD_TRANSITION 1 @@ -16,15 +17,7 @@ @7 // Primary constant buffer (with additional material parameters) META_CB_BEGIN(0, Data) -float4x4 ViewProjectionMatrix; float4x4 WorldMatrix; -float4x4 ViewMatrix; -float3 ViewPos; -float ViewFar; -float3 ViewDir; -float TimeParam; -float4 ViewInfo; -float4 ScreenSize; float3 WorldInvScale; float WorldDeterminantSign; float PerInstanceRandom; diff --git a/Content/Editor/MaterialTemplates/VolumeParticle.shader b/Content/Editor/MaterialTemplates/VolumeParticle.shader index c0fc57e54..c21e7c3bb 100644 --- a/Content/Editor/MaterialTemplates/VolumeParticle.shader +++ b/Content/Editor/MaterialTemplates/VolumeParticle.shader @@ -2,6 +2,7 @@ // Version: @0 #define MATERIAL 1 +#define USE_PER_VIEW_CONSTANTS 1 @3 #include "./Flax/Common.hlsl" @@ -11,17 +12,9 @@ // Primary constant buffer (with additional material parameters) META_CB_BEGIN(0, Data) -float4x4 ViewProjectionMatrix; float4x4 InverseViewProjectionMatrix; -float4x4 ViewMatrix; float4x4 WorldMatrix; float4x4 WorldMatrixInverseTransposed; -float3 ViewPos; -float ViewFar; -float3 ViewDir; -float TimeParam; -float4 ViewInfo; -float4 ScreenSize; float3 GridSize; float PerInstanceRandom; float Dummy0; diff --git a/Source/Engine/Content/Assets/Material.cpp b/Source/Engine/Content/Assets/Material.cpp index 7acc6dec7..f6de35df7 100644 --- a/Source/Engine/Content/Assets/Material.cpp +++ b/Source/Engine/Content/Assets/Material.cpp @@ -482,7 +482,7 @@ void Material::InitCompilationOptions(ShaderCompilationOptions& options) options.Macros.Add({ "MATERIAL_TESSELLATION", "MATERIAL_TESSELLATION_PHONG" }); break; } - options.Macros.Add({ "MAX_TESSELLATION_FACTOR", Numbers[info.MaxTessellationFactor] }); + options.Macros.Add({ "MAX_TESSELLATION_FACTOR", Numbers[Math::Min(info.MaxTessellationFactor, ARRAY_COUNT(Numbers) - 1)] }); } // Helper macros (used by the parser) diff --git a/Source/Engine/Graphics/Materials/DecalMaterialShader.cpp b/Source/Engine/Graphics/Materials/DecalMaterialShader.cpp index 6ca243fc1..54ca0e137 100644 --- a/Source/Engine/Graphics/Materials/DecalMaterialShader.cpp +++ b/Source/Engine/Graphics/Materials/DecalMaterialShader.cpp @@ -12,17 +12,9 @@ #include "Engine/Renderer/DrawCall.h" PACK_STRUCT(struct DecalMaterialShaderData { - Matrix ViewProjectionMatrix; Matrix WorldMatrix; - Matrix ViewMatrix; Matrix InvWorld; Matrix SVPositionToWorld; - Float3 ViewPos; - float ViewFar; - Float3 ViewDir; - float TimeParam; - Float4 ViewInfo; - Float4 ScreenSize; }); DrawPass DecalMaterialShader::GetDrawModes() const @@ -58,15 +50,7 @@ void DecalMaterialShader::Bind(BindParameters& params) // Setup material constants { - Matrix::Transpose(view.Frustum.GetMatrix(), materialData->ViewProjectionMatrix); Matrix::Transpose(drawCall.World, materialData->WorldMatrix); - Matrix::Transpose(view.View, materialData->ViewMatrix); - materialData->ViewPos = view.Position; - materialData->ViewFar = view.Far; - materialData->ViewDir = view.Direction; - materialData->TimeParam = params.TimeParam; - materialData->ViewInfo = view.ViewInfo; - materialData->ScreenSize = view.ScreenSize; // Matrix for transformation from world space to decal object space Matrix invWorld; diff --git a/Source/Engine/Graphics/Materials/DeferredMaterialShader.cpp b/Source/Engine/Graphics/Materials/DeferredMaterialShader.cpp index 9f03f81ca..4879c7896 100644 --- a/Source/Engine/Graphics/Materials/DeferredMaterialShader.cpp +++ b/Source/Engine/Graphics/Materials/DeferredMaterialShader.cpp @@ -17,23 +17,11 @@ #include "Engine/Graphics/RenderTask.h" PACK_STRUCT(struct DeferredMaterialShaderData { - Matrix ViewProjectionMatrix; Matrix WorldMatrix; - Matrix ViewMatrix; - Matrix PrevViewProjectionMatrix; Matrix PrevWorldMatrix; - Matrix MainViewProjectionMatrix; - Float4 MainScreenSize; - Float3 ViewPos; - float ViewFar; - Float3 ViewDir; - float TimeParam; - Float4 ViewInfo; - Float4 ScreenSize; Float2 Dummy0; float LODDitherFactor; float PerInstanceRandom; - Float4 TemporalAAJitter; Float3 GeometrySize; float WorldDeterminantSign; }); @@ -56,6 +44,7 @@ bool DeferredMaterialShader::CanUseInstancing(InstancingHandler& handler) const void DeferredMaterialShader::Bind(BindParameters& params) { + //PROFILE_CPU(); // Prepare auto context = params.GPUContext; auto& view = params.RenderContext.View; @@ -81,23 +70,11 @@ void DeferredMaterialShader::Bind(BindParameters& params) // Setup material constants { - Matrix::Transpose(view.Frustum.GetMatrix(), materialData->ViewProjectionMatrix); Matrix::Transpose(drawCall.World, materialData->WorldMatrix); - Matrix::Transpose(view.View, materialData->ViewMatrix); Matrix::Transpose(drawCall.Surface.PrevWorld, materialData->PrevWorldMatrix); - Matrix::Transpose(view.PrevViewProjection, materialData->PrevViewProjectionMatrix); - Matrix::Transpose(view.MainViewProjection, materialData->MainViewProjectionMatrix); - materialData->MainScreenSize = view.MainScreenSize; - materialData->ViewPos = view.Position; - materialData->ViewFar = view.Far; - materialData->ViewDir = view.Direction; - materialData->TimeParam = params.TimeParam; - materialData->ViewInfo = view.ViewInfo; - materialData->ScreenSize = view.ScreenSize; materialData->WorldDeterminantSign = drawCall.WorldDeterminantSign; materialData->LODDitherFactor = drawCall.Surface.LODDitherFactor; materialData->PerInstanceRandom = drawCall.PerInstanceRandom; - materialData->TemporalAAJitter = view.TemporalAAJitter; materialData->GeometrySize = drawCall.Surface.GeometrySize; } diff --git a/Source/Engine/Graphics/Materials/DeformableMaterialShader.cpp b/Source/Engine/Graphics/Materials/DeformableMaterialShader.cpp index ec3b800fe..02ec26d94 100644 --- a/Source/Engine/Graphics/Materials/DeformableMaterialShader.cpp +++ b/Source/Engine/Graphics/Materials/DeformableMaterialShader.cpp @@ -15,23 +15,14 @@ #include "Engine/Graphics/RenderTask.h" PACK_STRUCT(struct DeformableMaterialShaderData { - Matrix ViewProjectionMatrix; Matrix WorldMatrix; Matrix LocalMatrix; - Matrix ViewMatrix; - Float3 ViewPos; - float ViewFar; - Float3 ViewDir; - float TimeParam; - Float4 ViewInfo; - Float4 ScreenSize; Float3 Dummy0; float WorldDeterminantSign; float MeshMinZ; float Segment; float ChunksPerSegment; float PerInstanceRandom; - Float4 TemporalAAJitter; Float3 GeometrySize; float MeshMaxZ; }); @@ -69,23 +60,14 @@ void DeformableMaterialShader::Bind(BindParameters& params) // Setup material constants { - Matrix::Transpose(view.Frustum.GetMatrix(), materialData->ViewProjectionMatrix); Matrix::Transpose(drawCall.World, materialData->WorldMatrix); Matrix::Transpose(drawCall.Deformable.LocalMatrix, materialData->LocalMatrix); - Matrix::Transpose(view.View, materialData->ViewMatrix); - materialData->ViewPos = view.Position; - materialData->ViewFar = view.Far; - materialData->ViewDir = view.Direction; - materialData->TimeParam = params.TimeParam; - materialData->ViewInfo = view.ViewInfo; - materialData->ScreenSize = view.ScreenSize; materialData->WorldDeterminantSign = drawCall.WorldDeterminantSign; materialData->Segment = drawCall.Deformable.Segment; materialData->ChunksPerSegment = drawCall.Deformable.ChunksPerSegment; materialData->MeshMinZ = drawCall.Deformable.MeshMinZ; materialData->MeshMaxZ = drawCall.Deformable.MeshMaxZ; materialData->PerInstanceRandom = drawCall.PerInstanceRandom; - materialData->TemporalAAJitter = view.TemporalAAJitter; materialData->GeometrySize = drawCall.Deformable.GeometrySize; } diff --git a/Source/Engine/Graphics/Materials/ForwardMaterialShader.cpp b/Source/Engine/Graphics/Materials/ForwardMaterialShader.cpp index 9897063d1..9c658f9c9 100644 --- a/Source/Engine/Graphics/Materials/ForwardMaterialShader.cpp +++ b/Source/Engine/Graphics/Materials/ForwardMaterialShader.cpp @@ -16,26 +16,12 @@ #include "Engine/Renderer/Lightmaps.h" #endif -#define MAX_LOCAL_LIGHTS 4 - PACK_STRUCT(struct ForwardMaterialShaderData { - Matrix ViewProjectionMatrix; Matrix WorldMatrix; - Matrix ViewMatrix; - Matrix PrevViewProjectionMatrix; Matrix PrevWorldMatrix; - Matrix MainViewProjectionMatrix; - Float4 MainScreenSize; - Float3 ViewPos; - float ViewFar; - Float3 ViewDir; - float TimeParam; - Float4 ViewInfo; - Float4 ScreenSize; Float2 Dummy0; float LODDitherFactor; float PerInstanceRandom; - Float4 TemporalAAJitter; Float3 GeometrySize; float WorldDeterminantSign; }); @@ -89,19 +75,8 @@ void ForwardMaterialShader::Bind(BindParameters& params) // Setup material constants { - Matrix::Transpose(view.Frustum.GetMatrix(), materialData->ViewProjectionMatrix); Matrix::Transpose(drawCall.World, materialData->WorldMatrix); - Matrix::Transpose(view.View, materialData->ViewMatrix); Matrix::Transpose(drawCall.Surface.PrevWorld, materialData->PrevWorldMatrix); - Matrix::Transpose(view.PrevViewProjection, materialData->PrevViewProjectionMatrix); - Matrix::Transpose(view.MainViewProjection, materialData->MainViewProjectionMatrix); - materialData->MainScreenSize = view.MainScreenSize; - materialData->ViewPos = view.Position; - materialData->ViewFar = view.Far; - materialData->ViewDir = view.Direction; - materialData->TimeParam = params.TimeParam; - materialData->ViewInfo = view.ViewInfo; - materialData->ScreenSize = view.ScreenSize; materialData->WorldDeterminantSign = drawCall.WorldDeterminantSign; materialData->LODDitherFactor = drawCall.Surface.LODDitherFactor; materialData->PerInstanceRandom = drawCall.PerInstanceRandom; diff --git a/Source/Engine/Graphics/Materials/IMaterial.h b/Source/Engine/Graphics/Materials/IMaterial.h index 9c6eed0cf..68e83547e 100644 --- a/Source/Engine/Graphics/Materials/IMaterial.h +++ b/Source/Engine/Graphics/Materials/IMaterial.h @@ -8,6 +8,7 @@ struct MaterialParamsLink; class GPUShader; class GPUContext; class GPUTextureView; +class GPUConstantBuffer; class RenderBuffers; class SceneRenderTask; struct RenderView; @@ -157,6 +158,12 @@ public: BindParameters(::GPUContext* context, const ::RenderContext& renderContext); BindParameters(::GPUContext* context, const ::RenderContext& renderContext, const DrawCall& drawCall); BindParameters(::GPUContext* context, const ::RenderContext& renderContext, const DrawCall* firstDrawCall, int32 drawCallsCount); + + // Per-view shared constant buffer (see ViewData in MaterialCommon.hlsl). + static GPUConstantBuffer* PerViewConstants; + + // Binds the shared per-view constant buffer at slot 1 (see ViewData in MaterialCommon.hlsl) + void BindViewData(); }; /// diff --git a/Source/Engine/Graphics/Materials/MaterialShader.cpp b/Source/Engine/Graphics/Materials/MaterialShader.cpp index a81293430..ac9453e64 100644 --- a/Source/Engine/Graphics/Materials/MaterialShader.cpp +++ b/Source/Engine/Graphics/Materials/MaterialShader.cpp @@ -4,6 +4,7 @@ #include "Engine/Core/Log.h" #include "Engine/Serialization/MemoryReadStream.h" #include "Engine/Renderer/RenderList.h" +#include "Engine/Graphics/RenderTask.h" #include "Engine/Graphics/GPUDevice.h" #include "Engine/Graphics/Shaders/GPUConstantBuffer.h" #include "Engine/Graphics/Shaders/GPUShader.h" @@ -18,6 +19,21 @@ #include "DeformableMaterialShader.h" #include "VolumeParticleMaterialShader.h" +PACK_STRUCT(struct MaterialShaderDataPerView { + Matrix ViewMatrix; + Matrix ViewProjectionMatrix; + Matrix PrevViewProjectionMatrix; + Matrix MainViewProjectionMatrix; + Float4 MainScreenSize; + Float3 ViewPos; + float ViewFar; + Float3 ViewDir; + float TimeParam; + Float4 ViewInfo; + Float4 ScreenSize; + Float4 TemporalAAJitter; + }); + IMaterial::BindParameters::BindParameters(::GPUContext* context, const ::RenderContext& renderContext) : GPUContext(context) , RenderContext(renderContext) @@ -45,6 +61,37 @@ IMaterial::BindParameters::BindParameters(::GPUContext* context, const ::RenderC { } +GPUConstantBuffer* IMaterial::BindParameters::PerViewConstants = nullptr; + +void IMaterial::BindParameters::BindViewData() +{ + // Lazy-init + if (!PerViewConstants) + { + PerViewConstants = GPUDevice::Instance->CreateConstantBuffer(sizeof(MaterialShaderDataPerView), TEXT("PerViewConstants")); + } + + // Setup data + MaterialShaderDataPerView cb; + int aa1 = sizeof(MaterialShaderDataPerView); + Matrix::Transpose(RenderContext.View.Frustum.GetMatrix(), cb.ViewProjectionMatrix); + Matrix::Transpose(RenderContext.View.View, cb.ViewMatrix); + Matrix::Transpose(RenderContext.View.PrevViewProjection, cb.PrevViewProjectionMatrix); + Matrix::Transpose(RenderContext.View.MainViewProjection, cb.MainViewProjectionMatrix); + cb.MainScreenSize = RenderContext.View.MainScreenSize; + cb.ViewPos = RenderContext.View.Position; + cb.ViewFar = RenderContext.View.Far; + cb.ViewDir = RenderContext.View.Direction; + cb.TimeParam = TimeParam; + cb.ViewInfo = RenderContext.View.ViewInfo; + cb.ScreenSize = RenderContext.View.ScreenSize; + cb.TemporalAAJitter = RenderContext.View.TemporalAAJitter; + + // Update constants + GPUContext->UpdateCB(PerViewConstants, &cb); + GPUContext->BindCB(1, PerViewConstants); +} + GPUPipelineState* MaterialShader::PipelineStateCache::InitPS(CullMode mode, bool wireframe) { Desc.CullMode = mode; diff --git a/Source/Engine/Graphics/Materials/MaterialShader.h b/Source/Engine/Graphics/Materials/MaterialShader.h index d48e5d16f..1faa78eee 100644 --- a/Source/Engine/Graphics/Materials/MaterialShader.h +++ b/Source/Engine/Graphics/Materials/MaterialShader.h @@ -10,7 +10,7 @@ /// /// Current materials shader version. /// -#define MATERIAL_GRAPH_VERSION 158 +#define MATERIAL_GRAPH_VERSION 159 class Material; class GPUShader; diff --git a/Source/Engine/Graphics/Materials/ParticleMaterialShader.cpp b/Source/Engine/Graphics/Materials/ParticleMaterialShader.cpp index 196108608..a994d0e3f 100644 --- a/Source/Engine/Graphics/Materials/ParticleMaterialShader.cpp +++ b/Source/Engine/Graphics/Materials/ParticleMaterialShader.cpp @@ -15,15 +15,7 @@ #include "Engine/Particles/Graph/CPU/ParticleEmitterGraph.CPU.h" PACK_STRUCT(struct ParticleMaterialShaderData { - Matrix ViewProjectionMatrix; Matrix WorldMatrix; - Matrix ViewMatrix; - Float3 ViewPos; - float ViewFar; - Float3 ViewDir; - float TimeParam; - Float4 ViewInfo; - Float4 ScreenSize; uint32 SortedIndicesOffset; float PerInstanceRandom; int32 ParticleStride; @@ -109,15 +101,7 @@ void ParticleMaterialShader::Bind(BindParameters& params) static StringView ParticleScaleOffset(TEXT("Scale")); static StringView ParticleModelFacingModeOffset(TEXT("ModelFacingMode")); - Matrix::Transpose(view.Frustum.GetMatrix(), materialData->ViewProjectionMatrix); Matrix::Transpose(drawCall.World, materialData->WorldMatrix); - Matrix::Transpose(view.View, materialData->ViewMatrix); - materialData->ViewPos = view.Position; - materialData->ViewFar = view.Far; - materialData->ViewDir = view.Direction; - materialData->TimeParam = params.TimeParam; - materialData->ViewInfo = view.ViewInfo; - materialData->ScreenSize = view.ScreenSize; materialData->SortedIndicesOffset = drawCall.Particle.Particles->GPU.SortedIndices && params.RenderContext.View.Pass != DrawPass::Depth ? sortedIndicesOffset : 0xFFFFFFFF; materialData->PerInstanceRandom = drawCall.PerInstanceRandom; materialData->ParticleStride = drawCall.Particle.Particles->Stride; diff --git a/Source/Engine/Graphics/Materials/TerrainMaterialShader.cpp b/Source/Engine/Graphics/Materials/TerrainMaterialShader.cpp index bcdb4ee83..7b3d17658 100644 --- a/Source/Engine/Graphics/Materials/TerrainMaterialShader.cpp +++ b/Source/Engine/Graphics/Materials/TerrainMaterialShader.cpp @@ -16,15 +16,7 @@ #include "Engine/Terrain/TerrainPatch.h" PACK_STRUCT(struct TerrainMaterialShaderData { - Matrix ViewProjectionMatrix; Matrix WorldMatrix; - Matrix ViewMatrix; - Float3 ViewPos; - float ViewFar; - Float3 ViewDir; - float TimeParam; - Float4 ViewInfo; - Float4 ScreenSize; Float3 WorldInvScale; float WorldDeterminantSign; float PerInstanceRandom; @@ -74,15 +66,7 @@ void TerrainMaterialShader::Bind(BindParameters& params) // Setup material constants { - Matrix::Transpose(view.Frustum.GetMatrix(), materialData->ViewProjectionMatrix); Matrix::Transpose(drawCall.World, materialData->WorldMatrix); - Matrix::Transpose(view.View, materialData->ViewMatrix); - materialData->ViewPos = view.Position; - materialData->ViewFar = view.Far; - materialData->ViewDir = view.Direction; - materialData->TimeParam = params.TimeParam; - materialData->ViewInfo = view.ViewInfo; - materialData->ScreenSize = view.ScreenSize; const float scaleX = Float3(drawCall.World.M11, drawCall.World.M12, drawCall.World.M13).Length(); const float scaleY = Float3(drawCall.World.M21, drawCall.World.M22, drawCall.World.M23).Length(); const float scaleZ = Float3(drawCall.World.M31, drawCall.World.M32, drawCall.World.M33).Length(); diff --git a/Source/Engine/Graphics/Materials/VolumeParticleMaterialShader.cpp b/Source/Engine/Graphics/Materials/VolumeParticleMaterialShader.cpp index 42d8ec87e..06b5a66d2 100644 --- a/Source/Engine/Graphics/Materials/VolumeParticleMaterialShader.cpp +++ b/Source/Engine/Graphics/Materials/VolumeParticleMaterialShader.cpp @@ -15,17 +15,9 @@ #include "Engine/Particles/Graph/CPU/ParticleEmitterGraph.CPU.h" PACK_STRUCT(struct VolumeParticleMaterialShaderData { - Matrix ViewProjectionMatrix; Matrix InverseViewProjectionMatrix; - Matrix ViewMatrix; Matrix WorldMatrix; Matrix WorldMatrixInverseTransposed; - Float3 ViewPos; - float ViewFar; - Float3 ViewDir; - float TimeParam; - Float4 ViewInfo; - Float4 ScreenSize; Float3 GridSize; float PerInstanceRandom; float Dummy0; @@ -83,17 +75,9 @@ void VolumeParticleMaterialShader::Bind(BindParameters& params) // Setup material constants { - Matrix::Transpose(view.Frustum.GetMatrix(), materialData->ViewProjectionMatrix); Matrix::Transpose(view.IVP, materialData->InverseViewProjectionMatrix); - Matrix::Transpose(view.View, materialData->ViewMatrix); Matrix::Transpose(drawCall.World, materialData->WorldMatrix); Matrix::Invert(drawCall.World, materialData->WorldMatrixInverseTransposed); - materialData->ViewPos = view.Position; - materialData->ViewFar = view.Far; - materialData->ViewDir = view.Direction; - materialData->TimeParam = params.TimeParam; - materialData->ViewInfo = view.ViewInfo; - materialData->ScreenSize = view.ScreenSize; materialData->GridSize = customData->GridSize; materialData->PerInstanceRandom = drawCall.PerInstanceRandom; materialData->VolumetricFogMaxDistance = customData->VolumetricFogMaxDistance; diff --git a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.cpp b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.cpp index 300b1baeb..74ebdde32 100644 --- a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.cpp +++ b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.cpp @@ -1,6 +1,5 @@ // Copyright (c) 2012-2022 Wojciech Figat. All rights reserved. -#include "Engine/Profiler/ProfilerCPU.h" #if GRAPHICS_API_DIRECTX11 #include "GPUContextDX11.h" @@ -349,7 +348,6 @@ void GPUContextDX11::BindUA(int32 slot, GPUResourceView* view) void GPUContextDX11::BindVB(const Span& vertexBuffers, const uint32* vertexBuffersOffsets) { - PROFILE_CPU(); ASSERT(vertexBuffers.Length() >= 0 && vertexBuffers.Length() <= GPU_MAX_VB_BINDED); bool vbEdited = false; @@ -374,7 +372,6 @@ void GPUContextDX11::BindVB(const Span& vertexBuffers, const uint32* void GPUContextDX11::BindIB(GPUBuffer* indexBuffer) { - PROFILE_CPU(); const auto ibDX11 = static_cast(indexBuffer); if (ibDX11 != _ibHandle) { @@ -455,7 +452,6 @@ void GPUContextDX11::ResolveMultisample(GPUTexture* sourceMultisampleTexture, GP void GPUContextDX11::DrawInstanced(uint32 verticesCount, uint32 instanceCount, int32 startInstance, int32 startVertex) { - PROFILE_CPU(); onDrawCall(); if (instanceCount > 1) _context->DrawInstanced(verticesCount, instanceCount, startVertex, startInstance); @@ -466,7 +462,6 @@ void GPUContextDX11::DrawInstanced(uint32 verticesCount, uint32 instanceCount, i void GPUContextDX11::DrawIndexedInstanced(uint32 indicesCount, uint32 instanceCount, int32 startInstance, int32 startVertex, int32 startIndex) { - PROFILE_CPU(); onDrawCall(); if (instanceCount > 1) _context->DrawIndexedInstanced(indicesCount, instanceCount, startIndex, startVertex, startInstance); @@ -477,7 +472,6 @@ void GPUContextDX11::DrawIndexedInstanced(uint32 indicesCount, uint32 instanceCo void GPUContextDX11::DrawInstancedIndirect(GPUBuffer* bufferForArgs, uint32 offsetForArgs) { - PROFILE_CPU(); ASSERT(bufferForArgs && bufferForArgs->GetFlags() & GPUBufferFlags::Argument); const auto bufferForArgsDX11 = static_cast(bufferForArgs); @@ -489,7 +483,6 @@ void GPUContextDX11::DrawInstancedIndirect(GPUBuffer* bufferForArgs, uint32 offs void GPUContextDX11::DrawIndexedInstancedIndirect(GPUBuffer* bufferForArgs, uint32 offsetForArgs) { - PROFILE_CPU(); ASSERT(bufferForArgs && bufferForArgs->GetFlags() & GPUBufferFlags::Argument); const auto bufferForArgsDX11 = static_cast(bufferForArgs); @@ -880,7 +873,6 @@ void GPUContextDX11::flushOM() void GPUContextDX11::onDrawCall() { - PROFILE_CPU(); flushCBs(); flushSRVs(); flushUAVs(); diff --git a/Source/Engine/Level/Actors/Skybox.cpp b/Source/Engine/Level/Actors/Skybox.cpp index 162d515cc..784cdaee8 100644 --- a/Source/Engine/Level/Actors/Skybox.cpp +++ b/Source/Engine/Level/Actors/Skybox.cpp @@ -104,6 +104,7 @@ void Skybox::ApplySky(GPUContext* context, RenderContext& renderContext, const M drawCall.WorldDeterminantSign = Math::FloatSelect(world.RotDeterminant(), 1, -1); drawCall.PerInstanceRandom = GetPerInstanceRandom(); MaterialBase::BindParameters bindParams(context, renderContext, drawCall); + bindParams.BindViewData(); // Check if use custom material if (CustomMaterial) diff --git a/Source/Engine/Renderer/Editor/MaterialComplexity.cpp b/Source/Engine/Renderer/Editor/MaterialComplexity.cpp index b20e1e976..569050f5e 100644 --- a/Source/Engine/Renderer/Editor/MaterialComplexity.cpp +++ b/Source/Engine/Renderer/Editor/MaterialComplexity.cpp @@ -126,6 +126,7 @@ void MaterialComplexityMaterialShader::Draw(RenderContext& renderContext, GPUCon PROFILE_GPU_CPU_NAMED("Decals"); DrawCall drawCall; MaterialBase::BindParameters bindParams(context, renderContext, drawCall); + bindParams.BindViewData(); drawCall.WorldDeterminantSign = 1.0f; context->SetRenderTarget(lightBuffer); for (int32 i = 0; i < decals.Count(); i++) diff --git a/Source/Engine/Renderer/Editor/QuadOverdrawPass.cpp b/Source/Engine/Renderer/Editor/QuadOverdrawPass.cpp index e0ed4ae31..cd1918988 100644 --- a/Source/Engine/Renderer/Editor/QuadOverdrawPass.cpp +++ b/Source/Engine/Renderer/Editor/QuadOverdrawPass.cpp @@ -48,6 +48,7 @@ void QuadOverdrawPass::Render(RenderContext& renderContext, GPUContext* context, Platform::MemoryClear(&drawCall, sizeof(DrawCall)); drawCall.PerInstanceRandom = 1.0f; MaterialBase::BindParameters bindParams(context, renderContext, drawCall); + bindParams.BindViewData(); renderContext.View.Pass = DrawPass::QuadOverdraw; context->SetRenderTarget(*renderContext.Buffers->DepthBuffer, (GPUTextureView*)nullptr); renderContext.List->ExecuteDrawCalls(renderContext, DrawCallsListType::GBuffer); diff --git a/Source/Engine/Renderer/GBufferPass.cpp b/Source/Engine/Renderer/GBufferPass.cpp index 1ee90a7cd..87ce7a432 100644 --- a/Source/Engine/Renderer/GBufferPass.cpp +++ b/Source/Engine/Renderer/GBufferPass.cpp @@ -433,6 +433,7 @@ void GBufferPass::DrawDecals(RenderContext& renderContext, GPUTextureView* light // Prepare DrawCall drawCall; MaterialBase::BindParameters bindParams(context, renderContext, drawCall); + bindParams.BindViewData(); drawCall.Material = nullptr; drawCall.WorldDeterminantSign = 1.0f; diff --git a/Source/Engine/Renderer/RenderList.cpp b/Source/Engine/Renderer/RenderList.cpp index 7a75ac147..95abc11be 100644 --- a/Source/Engine/Renderer/RenderList.cpp +++ b/Source/Engine/Renderer/RenderList.cpp @@ -695,6 +695,7 @@ DRAW: // Execute draw calls MaterialBase::BindParameters bindParams(context, renderContext); bindParams.Input = input; + bindParams.BindViewData(); if (useInstancing) { int32 instanceBufferOffset = 0; diff --git a/Source/Engine/Renderer/Renderer.cpp b/Source/Engine/Renderer/Renderer.cpp index 710312ab6..10fde8f51 100644 --- a/Source/Engine/Renderer/Renderer.cpp +++ b/Source/Engine/Renderer/Renderer.cpp @@ -119,6 +119,7 @@ void RendererService::Dispose() { PassList[i]->Dispose(); } + SAFE_DELETE_GPU_RESOURCE(IMaterial::BindParameters::PerViewConstants); } void RenderAntiAliasingPass(RenderContext& renderContext, GPUTexture* input, GPUTextureView* output) diff --git a/Source/Engine/Renderer/VolumetricFogPass.cpp b/Source/Engine/Renderer/VolumetricFogPass.cpp index 3185fad6f..6fb9c3ce3 100644 --- a/Source/Engine/Renderer/VolumetricFogPass.cpp +++ b/Source/Engine/Renderer/VolumetricFogPass.cpp @@ -536,6 +536,7 @@ void VolumetricFogPass::Render(RenderContext& renderContext) customData.GridSize = cache.GridSize; customData.VolumetricFogMaxDistance = cache.Data.VolumetricFogMaxDistance; bindParams.CustomData = &customData; + bindParams.BindViewData(); for (auto& drawCall : renderContext.List->VolumetricFogParticles) { diff --git a/Source/Shaders/MaterialCommon.hlsl b/Source/Shaders/MaterialCommon.hlsl index 60ba7e298..86f720cd6 100644 --- a/Source/Shaders/MaterialCommon.hlsl +++ b/Source/Shaders/MaterialCommon.hlsl @@ -55,6 +55,9 @@ #ifndef USE_DITHERED_LOD_TRANSITION #define USE_DITHERED_LOD_TRANSITION 0 #endif +#ifndef USE_PER_VIEW_CONSTANTS +#define USE_PER_VIEW_CONSTANTS 0 +#endif #ifndef MATERIAL_TESSELLATION #define MATERIAL_TESSELLATION MATERIAL_TESSELLATION_NONE #endif @@ -88,6 +91,25 @@ struct Material #endif }; +// Secondary constant buffer (with per-view constants at slot 1) +#if USE_PER_VIEW_CONSTANTS +cbuffer ViewData : register(b1) +{ + float4x4 ViewMatrix; + float4x4 ViewProjectionMatrix; + float4x4 PrevViewProjectionMatrix; + float4x4 MainViewProjectionMatrix; + float4 MainScreenSize; + float3 ViewPos; + float ViewFar; + float3 ViewDir; + float TimeParam; + float4 ViewInfo; + float4 ScreenSize; + float4 TemporalAAJitter; +}; +#endif + struct ModelInput { float3 Position : POSITION;