Refactor material shaders to use separate constant buffer (slot 1) for shared per-view constants
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
/// <summary>
|
||||
/// Current materials shader version.
|
||||
/// </summary>
|
||||
#define MATERIAL_GRAPH_VERSION 158
|
||||
#define MATERIAL_GRAPH_VERSION 159
|
||||
|
||||
class Material;
|
||||
class GPUShader;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user