This repository has been archived by the owner on Mar 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSponzaRenderer.cpp
400 lines (328 loc) · 15.5 KB
/
SponzaRenderer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
// Developed by Minigraph
//
// Author(s): Alex Nankervis
// James Stanard
//
// From Core
#include "GraphicsCore.h"
#include "BufferManager.h"
#include "Camera.h"
#include "CommandContext.h"
#include "TemporalEffects.h"
#include "SSAO.h"
#include "SystemTime.h"
#include "ShadowCamera.h"
#include "ParticleEffects.h"
#include "SponzaRenderer.h"
#include "Renderer.h"
// From Model
#include "ModelH3D.h"
// From ModelViewer
#include "LightManager.h"
#include "CompiledShaders/DepthViewerVS.h"
#include "CompiledShaders/DepthViewerPS.h"
#include "CompiledShaders/ModelViewerVS.h"
#include "CompiledShaders/ModelViewerPS.h"
using namespace Math;
using namespace Graphics;
namespace Sponza
{
void RenderLightShadows(GraphicsContext& gfxContext, const Camera& camera);
enum eObjectFilter { kOpaque = 0x1, kCutout = 0x2, kTransparent = 0x4, kAll = 0xF, kNone = 0x0 };
void RenderObjects( GraphicsContext& Context, const Matrix4& ViewProjMat, const Vector3& viewerPos, eObjectFilter Filter = kAll );
GraphicsPSO m_DepthPSO = { (L"Sponza: Depth PSO") };
GraphicsPSO m_CutoutDepthPSO = { (L"Sponza: Cutout Depth PSO") };
GraphicsPSO m_ModelPSO = { (L"Sponza: Color PSO") };
GraphicsPSO m_CutoutModelPSO = { (L"Sponza: Cutout Color PSO") };
GraphicsPSO m_ShadowPSO(L"Sponza: Shadow PSO");
GraphicsPSO m_CutoutShadowPSO(L"Sponza: Cutout Shadow PSO");
ModelH3D m_Model;
std::vector<bool> m_pMaterialIsCutout;
Vector3 m_SunDirection;
ShadowCamera m_SunShadow;
ExpVar m_AmbientIntensity("Sponza/Lighting/Ambient Intensity", 0.1f, -16.0f, 16.0f, 0.1f);
ExpVar m_SunLightIntensity("Sponza/Lighting/Sun Light Intensity", 4.0f, 0.0f, 16.0f, 0.1f);
NumVar m_SunOrientation("Sponza/Lighting/Sun Orientation", -0.5f, -100.0f, 100.0f, 0.1f );
NumVar m_SunInclination("Sponza/Lighting/Sun Inclination", 0.75f, 0.0f, 1.0f, 0.01f );
NumVar ShadowDimX("Sponza/Lighting/Shadow Dim X", 5000, 1000, 10000, 100 );
NumVar ShadowDimY("Sponza/Lighting/Shadow Dim Y", 3000, 1000, 10000, 100 );
NumVar ShadowDimZ("Sponza/Lighting/Shadow Dim Z", 3000, 1000, 10000, 100 );
}
void Sponza::Startup( Camera& Camera )
{
DXGI_FORMAT ColorFormat = g_SceneColorBuffer.GetFormat();
DXGI_FORMAT NormalFormat = g_SceneNormalBuffer.GetFormat();
DXGI_FORMAT DepthFormat = g_SceneDepthBuffer.GetFormat();
//DXGI_FORMAT ShadowFormat = g_ShadowBuffer.GetFormat();
D3D12_INPUT_ELEMENT_DESC vertElem[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D12_APPEND_ALIGNED_ELEMENT, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D12_APPEND_ALIGNED_ELEMENT, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D12_APPEND_ALIGNED_ELEMENT, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
{ "TANGENT", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D12_APPEND_ALIGNED_ELEMENT, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
{ "BITANGENT", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D12_APPEND_ALIGNED_ELEMENT, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }
};
// Depth-only (2x rate)
m_DepthPSO.SetRootSignature(Renderer::m_RootSig);
m_DepthPSO.SetRasterizerState(RasterizerDefault);
m_DepthPSO.SetBlendState(BlendNoColorWrite);
m_DepthPSO.SetDepthStencilState(DepthStateReadWrite);
m_DepthPSO.SetInputLayout(_countof(vertElem), vertElem);
m_DepthPSO.SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE);
m_DepthPSO.SetRenderTargetFormats(0, nullptr, DepthFormat);
m_DepthPSO.SetVertexShader(g_pDepthViewerVS, sizeof(g_pDepthViewerVS));
m_DepthPSO.Finalize();
// Depth-only shading but with alpha testing
m_CutoutDepthPSO = m_DepthPSO;
m_CutoutDepthPSO.SetPixelShader(g_pDepthViewerPS, sizeof(g_pDepthViewerPS));
m_CutoutDepthPSO.SetRasterizerState(RasterizerTwoSided);
m_CutoutDepthPSO.Finalize();
// Depth-only but with a depth bias and/or render only backfaces
m_ShadowPSO = m_DepthPSO;
m_ShadowPSO.SetRasterizerState(RasterizerShadow);
m_ShadowPSO.SetRenderTargetFormats(0, nullptr, g_ShadowBuffer.GetFormat());
m_ShadowPSO.Finalize();
// Shadows with alpha testing
m_CutoutShadowPSO = m_ShadowPSO;
m_CutoutShadowPSO.SetPixelShader(g_pDepthViewerPS, sizeof(g_pDepthViewerPS));
m_CutoutShadowPSO.SetRasterizerState(RasterizerShadowTwoSided);
m_CutoutShadowPSO.Finalize();
DXGI_FORMAT formats[2] = { ColorFormat, NormalFormat };
// Full color pass
m_ModelPSO = m_DepthPSO;
m_ModelPSO.SetBlendState(BlendDisable);
m_ModelPSO.SetDepthStencilState(DepthStateTestEqual);
m_ModelPSO.SetRenderTargetFormats(2, formats, DepthFormat);
m_ModelPSO.SetVertexShader( g_pModelViewerVS, sizeof(g_pModelViewerVS) );
m_ModelPSO.SetPixelShader( g_pModelViewerPS, sizeof(g_pModelViewerPS) );
m_ModelPSO.Finalize();
m_CutoutModelPSO = m_ModelPSO;
m_CutoutModelPSO.SetRasterizerState(RasterizerTwoSided);
m_CutoutModelPSO.Finalize();
ASSERT(m_Model.Load(L"Sponza/sponza.h3d"), "Failed to load model");
ASSERT(m_Model.GetMeshCount() > 0, "Model contains no meshes");
// The caller of this function can override which materials are considered cutouts
m_pMaterialIsCutout.resize(m_Model.GetMaterialCount());
for (uint32_t i = 0; i < m_Model.GetMaterialCount(); ++i)
{
const ModelH3D::Material& mat = m_Model.GetMaterial(i);
if (std::string(mat.texDiffusePath).find("thorn") != std::string::npos ||
std::string(mat.texDiffusePath).find("plant") != std::string::npos ||
std::string(mat.texDiffusePath).find("chain") != std::string::npos)
{
m_pMaterialIsCutout[i] = true;
}
else
{
m_pMaterialIsCutout[i] = false;
}
}
ParticleEffects::InitFromJSON(L"Sponza/particles.json");
float modelRadius = Length(m_Model.GetBoundingBox().GetDimensions()) * 0.5f;
const Vector3 eye = m_Model.GetBoundingBox().GetCenter() + Vector3(modelRadius * 0.5f, 0.0f, 0.0f);
Camera.SetEyeAtUp( eye, Vector3(kZero), Vector3(kYUnitVector) );
Lighting::CreateRandomLights(m_Model.GetBoundingBox().GetMin(), m_Model.GetBoundingBox().GetMax());
}
const ModelH3D& Sponza::GetModel()
{
return Sponza::m_Model;
}
void Sponza::Cleanup( void )
{
m_Model.Clear();
Lighting::Shutdown();
TextureManager::Shutdown();
}
void Sponza::RenderObjects( GraphicsContext& gfxContext, const Matrix4& ViewProjMat, const Vector3& viewerPos, eObjectFilter Filter )
{
struct VSConstants
{
Matrix4 modelToProjection;
Matrix4 modelToShadow;
XMFLOAT3 viewerPos;
} vsConstants;
vsConstants.modelToProjection = ViewProjMat;
vsConstants.modelToShadow = m_SunShadow.GetShadowMatrix();
XMStoreFloat3(&vsConstants.viewerPos, viewerPos);
gfxContext.SetDynamicConstantBufferView(Renderer::kMeshConstants, sizeof(vsConstants), &vsConstants);
__declspec(align(16)) uint32_t materialIdx = 0xFFFFFFFFul;
uint32_t VertexStride = m_Model.GetVertexStride();
for (uint32_t meshIndex = 0; meshIndex < m_Model.GetMeshCount(); meshIndex++)
{
const ModelH3D::Mesh& mesh = m_Model.GetMesh(meshIndex);
uint32_t indexCount = mesh.indexCount;
uint32_t startIndex = mesh.indexDataByteOffset / sizeof(uint16_t);
uint32_t baseVertex = mesh.vertexDataByteOffset / VertexStride;
if (mesh.materialIndex != materialIdx)
{
if ( m_pMaterialIsCutout[mesh.materialIndex] && !(Filter & kCutout) ||
!m_pMaterialIsCutout[mesh.materialIndex] && !(Filter & kOpaque) )
continue;
materialIdx = mesh.materialIndex;
gfxContext.SetDescriptorTable(Renderer::kMaterialSRVs, m_Model.GetSRVs(materialIdx));
gfxContext.SetDynamicConstantBufferView(Renderer::kCommonCBV, sizeof(uint32_t), &materialIdx);
}
gfxContext.DrawIndexed(indexCount, startIndex, baseVertex);
}
}
void Sponza::RenderLightShadows(GraphicsContext& gfxContext, const Camera& camera)
{
using namespace Lighting;
ScopedTimer _prof(L"RenderLightShadows", gfxContext);
static uint32_t LightIndex = 0;
if (LightIndex >= MaxLights)
return;
m_LightShadowTempBuffer.BeginRendering(gfxContext);
{
gfxContext.SetPipelineState(m_ShadowPSO);
RenderObjects(gfxContext, m_LightShadowMatrix[LightIndex], camera.GetPosition(), kOpaque);
gfxContext.SetPipelineState(m_CutoutShadowPSO);
RenderObjects(gfxContext, m_LightShadowMatrix[LightIndex], camera.GetPosition(), kCutout);
}
//m_LightShadowTempBuffer.EndRendering(gfxContext);
gfxContext.TransitionResource(m_LightShadowTempBuffer, D3D12_RESOURCE_STATE_COPY_SOURCE);
gfxContext.TransitionResource(m_LightShadowArray, D3D12_RESOURCE_STATE_COPY_DEST);
gfxContext.CopySubresource(m_LightShadowArray, LightIndex, m_LightShadowTempBuffer, 0);
gfxContext.TransitionResource(m_LightShadowArray, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
++LightIndex;
}
void Sponza::RenderScene(
GraphicsContext& gfxContext,
const Camera& camera,
const D3D12_VIEWPORT& viewport,
const D3D12_RECT& scissor,
bool skipDiffusePass,
bool skipShadowMap)
{
Renderer::UpdateGlobalDescriptors();
uint32_t FrameIndex = TemporalEffects::GetFrameIndexMod2();
float costheta = cosf(m_SunOrientation);
float sintheta = sinf(m_SunOrientation);
float cosphi = cosf(m_SunInclination * 3.14159f * 0.5f);
float sinphi = sinf(m_SunInclination * 3.14159f * 0.5f);
m_SunDirection = Normalize(Vector3( costheta * cosphi, sinphi, sintheta * cosphi ));
__declspec(align(16)) struct
{
Vector3 sunDirection;
Vector3 sunLight;
Vector3 ambientLight;
float ShadowTexelSize[4];
float InvTileDim[4];
uint32_t TileCount[4];
uint32_t FirstLightIndex[4];
uint32_t FrameIndexMod2;
} psConstants;
psConstants.sunDirection = m_SunDirection;
psConstants.sunLight = Vector3(1.0f, 1.0f, 1.0f) * m_SunLightIntensity;
psConstants.ambientLight = Vector3(1.0f, 1.0f, 1.0f) * m_AmbientIntensity;
psConstants.ShadowTexelSize[0] = 1.0f / g_ShadowBuffer.GetWidth();
psConstants.InvTileDim[0] = 1.0f / Lighting::LightGridDim;
psConstants.InvTileDim[1] = 1.0f / Lighting::LightGridDim;
psConstants.TileCount[0] = Math::DivideByMultiple(g_SceneColorBuffer.GetWidth(), Lighting::LightGridDim);
psConstants.TileCount[1] = Math::DivideByMultiple(g_SceneColorBuffer.GetHeight(), Lighting::LightGridDim);
psConstants.FirstLightIndex[0] = Lighting::m_FirstConeLight;
psConstants.FirstLightIndex[1] = Lighting::m_FirstConeShadowedLight;
psConstants.FrameIndexMod2 = FrameIndex;
// Set the default state for command lists
auto& pfnSetupGraphicsState = [&](void)
{
gfxContext.SetRootSignature(Renderer::m_RootSig);
gfxContext.SetDescriptorHeap(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, Renderer::s_TextureHeap.GetHeapPointer());
gfxContext.SetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
gfxContext.SetIndexBuffer(m_Model.GetIndexBuffer());
gfxContext.SetVertexBuffer(0, m_Model.GetVertexBuffer());
};
pfnSetupGraphicsState();
RenderLightShadows(gfxContext, camera);
{
ScopedTimer _prof(L"Z PrePass", gfxContext);
gfxContext.SetDynamicConstantBufferView(Renderer::kMaterialConstants, sizeof(psConstants), &psConstants);
{
ScopedTimer _prof2(L"Opaque", gfxContext);
{
gfxContext.TransitionResource(g_SceneDepthBuffer, D3D12_RESOURCE_STATE_DEPTH_WRITE, true);
gfxContext.ClearDepth(g_SceneDepthBuffer);
gfxContext.SetPipelineState(m_DepthPSO);
gfxContext.SetDepthStencilTarget(g_SceneDepthBuffer.GetDSV());
gfxContext.SetViewportAndScissor(viewport, scissor);
}
RenderObjects(gfxContext, camera.GetViewProjMatrix(), camera.GetPosition(), kOpaque );
}
{
ScopedTimer _prof2(L"Cutout", gfxContext);
{
gfxContext.SetPipelineState(m_CutoutDepthPSO);
}
RenderObjects(gfxContext, camera.GetViewProjMatrix(), camera.GetPosition(), kCutout );
}
}
SSAO::Render(gfxContext, camera);
if (!skipDiffusePass)
{
Lighting::FillLightGrid(gfxContext, camera);
if (!SSAO::DebugDraw)
{
ScopedTimer _prof(L"Main Render", gfxContext);
{
gfxContext.TransitionResource(g_SceneColorBuffer, D3D12_RESOURCE_STATE_RENDER_TARGET, true);
gfxContext.TransitionResource(g_SceneNormalBuffer, D3D12_RESOURCE_STATE_RENDER_TARGET, true);
gfxContext.ClearColor(g_SceneColorBuffer);
}
}
}
if (!skipShadowMap)
{
if (!SSAO::DebugDraw)
{
pfnSetupGraphicsState();
{
ScopedTimer _prof2(L"Render Shadow Map", gfxContext);
m_SunShadow.UpdateMatrix(-m_SunDirection, Vector3(0, -500.0f, 0), Vector3(ShadowDimX, ShadowDimY, ShadowDimZ),
(uint32_t)g_ShadowBuffer.GetWidth(), (uint32_t)g_ShadowBuffer.GetHeight(), 16);
g_ShadowBuffer.BeginRendering(gfxContext);
gfxContext.SetPipelineState(m_ShadowPSO);
RenderObjects(gfxContext, m_SunShadow.GetViewProjMatrix(), camera.GetPosition(), kOpaque);
gfxContext.SetPipelineState(m_CutoutShadowPSO);
RenderObjects(gfxContext, m_SunShadow.GetViewProjMatrix(), camera.GetPosition(), kCutout);
g_ShadowBuffer.EndRendering(gfxContext);
}
}
}
if (!skipDiffusePass)
{
if (!SSAO::DebugDraw)
{
if (SSAO::AsyncCompute)
{
gfxContext.Flush();
pfnSetupGraphicsState();
// Make the 3D queue wait for the Compute queue to finish SSAO
g_CommandManager.GetGraphicsQueue().StallForProducer(g_CommandManager.GetComputeQueue());
}
{
ScopedTimer _prof2(L"Render Color", gfxContext);
gfxContext.TransitionResource(g_SSAOFullScreen, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
gfxContext.SetDescriptorTable(Renderer::kCommonSRVs, Renderer::m_CommonTextures);
gfxContext.SetDynamicConstantBufferView(Renderer::kMaterialConstants, sizeof(psConstants), &psConstants);
{
gfxContext.SetPipelineState(m_ModelPSO);
gfxContext.TransitionResource(g_SceneDepthBuffer, D3D12_RESOURCE_STATE_DEPTH_READ);
D3D12_CPU_DESCRIPTOR_HANDLE rtvs[]{ g_SceneColorBuffer.GetRTV(), g_SceneNormalBuffer.GetRTV() };
gfxContext.SetRenderTargets(ARRAYSIZE(rtvs), rtvs, g_SceneDepthBuffer.GetDSV_DepthReadOnly());
gfxContext.SetViewportAndScissor(viewport, scissor);
}
RenderObjects( gfxContext, camera.GetViewProjMatrix(), camera.GetPosition(), Sponza::kOpaque );
gfxContext.SetPipelineState(m_CutoutModelPSO);
RenderObjects( gfxContext, camera.GetViewProjMatrix(), camera.GetPosition(), Sponza::kCutout );
}
}
}
}