Skip to content

Commit 623a1d8

Browse files
committed
Light: Shadow pre-pass done.
1 parent 982fda6 commit 623a1d8

File tree

13 files changed

+194
-46
lines changed

13 files changed

+194
-46
lines changed

src/Shader/ShadowPass.hlsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ VertexOutput VertexMain(float4 position : POSITION)
1919
return output;
2020
}
2121

22-
void PixelMain(VertexOutput input)
22+
float4 PixelMain(VertexOutput input) : SV_Target
2323
{
24-
return;
24+
return float4(0, 0, 0, 0);
2525
}

src/private/Core/GameObject/Component/Mesh.cpp

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,31 @@ void Mesh::Update() {
190190

191191
}
192192

193+
void Mesh::ShadowPass(WVP wvp) {
194+
this->UpdateConstantBuffer();
195+
196+
wvp.World = this->m_wvp.World;
197+
198+
UINT nWVPSize = (sizeof(wvp) + 255) & ~255;
199+
200+
PVOID pData;
201+
ThrowIfFailed(this->m_wvpRes->Map(0, nullptr, &pData));
202+
memcpy(pData, &wvp, nWVPSize);
203+
this->m_wvpRes->Unmap(0, nullptr);
204+
205+
Descriptor wvpDesc = this->m_cbv_srvHeap->GetDescriptor(this->m_nWvpIndex);
206+
this->m_list->SetGraphicsRootDescriptorTable(0, wvpDesc.gpuHandle);
207+
int i = 0;
208+
for (D3D12_VERTEX_BUFFER_VIEW vbv : this->m_VBVs) {
209+
D3D12_INDEX_BUFFER_VIEW ibv = this->m_IBVs[i];
210+
this->m_list->IASetVertexBuffers(0, 1, &vbv);
211+
this->m_list->IASetIndexBuffer(&ibv);
212+
213+
this->m_list->DrawIndexedInstanced(this->m_indices[i].size(), 1, 0, 0, 0);
214+
i++;
215+
}
216+
}
217+
193218
void Mesh::Render() {
194219
this->UpdateConstantBuffer();
195220
this->m_list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
@@ -449,16 +474,16 @@ void Mesh::LoadModel(std::string filename) {
449474
if (texture != nullptr) {
450475
std::string name = std::string(texPath.C_Str());
451476
taskPending++;
452-
threads.emplace_back([=, &taskPending, this]() {
453-
if (!texture) {
454-
taskPending--;
455-
return;
456-
}
457-
ComPtr<ID3D12Resource> resource;
458-
resMgr->LoadTexture((BYTE*)texture->pcData, texture->mWidth, name, resource);
459-
resource->SetName(L"Mesh Base color");
460-
this->m_textures[i] = resource;
477+
if (!texture) {
461478
taskPending--;
479+
return;
480+
}
481+
ComPtr<ID3D12Resource> resource;
482+
resMgr->LoadTexture((BYTE*)texture->pcData, texture->mWidth, name, resource);
483+
resource->SetName(L"Mesh Base color");
484+
this->m_textures[i] = resource;
485+
taskPending--;
486+
threads.emplace_back([=, &taskPending, this]() {
462487
});
463488
}
464489
}
@@ -478,6 +503,18 @@ void Mesh::LoadModel(std::string filename) {
478503
this->m_ORMTextures[i] = resource;
479504
taskPending--;
480505
}
506+
} else {
507+
float metalness = 0.f;
508+
float roughness = 0.f;
509+
float ao = 1.f;
510+
material->Get(AI_MATKEY_METALLIC_FACTOR, metalness);
511+
material->Get(AI_MATKEY_ROUGHNESS_FACTOR, roughness);
512+
513+
ComPtr<ID3D12Resource> resource;
514+
// resMgr->LoadTextureFile("T_Black.png", resource);
515+
resMgr->CreateTextureVec4(ao, roughness, metalness, 1.f, resource);
516+
resource->SetName(L"Mesh Metallic Roughness");
517+
this->m_ORMTextures[i] = resource;
481518
}
482519
}
483520

src/private/Core/GameObject/GameObject.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ Component* GameObject::GetComponent(std::string name) {
2525
return nullptr;
2626
}
2727

28+
void GameObject::ShadowPass(WVP wvp) {
29+
for (Component* component : this->m_components) {
30+
if (Mesh* mesh = dynamic_cast<Mesh*>(component)) {
31+
mesh->ShadowPass(wvp);
32+
}
33+
}
34+
}
35+
2836
void GameObject::Render() {
2937
for (Component* component : this->m_components) {
3038
if (Mesh* mesh = dynamic_cast<Mesh*>(component)) {

src/private/Core/GameObject/Light/Light.cpp

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ void Light::Init() {
1515
GameObject::Init();
1616

1717
if (D3D12* renderer = reinterpret_cast<D3D12*>(this->m_renderer)) {
18+
renderer->GetDevice(this->m_dev);
19+
renderer->GetCommandList(this->m_list);
20+
1821
this->InitConstantBuffers(renderer);
1922

2023
this->m_shader = new Shader("ShadowPass.hlsl", "VertexMain", "PixelMain");
@@ -39,7 +42,7 @@ void Light::Init() {
3942
dsvClear.DepthStencil.Depth = 1.f;
4043
dsvClear.DepthStencil.Stencil = 0.f;
4144

42-
ThrowIfFailed(renderer->m_dev->CreateCommittedResource(
45+
ThrowIfFailed(this->m_dev->CreateCommittedResource(
4346
&heapProps,
4447
D3D12_HEAP_FLAG_NONE,
4548
&depthBuffDesc,
@@ -57,7 +60,9 @@ void Light::Init() {
5760

5861
Descriptor descriptor = renderer->m_dsvHeap->GetDescriptor(this->m_nDepthIndex);
5962

60-
renderer->m_dev->CreateDepthStencilView(this->m_depth.Get(), &dsvDesc, descriptor.cpuHandle);
63+
this->m_dev->CreateDepthStencilView(this->m_depth.Get(), &dsvDesc, descriptor.cpuHandle);
64+
65+
this->InitPipeline(renderer);
6166
}
6267

6368
}
@@ -67,8 +72,62 @@ void Light::InitPipeline(D3D12* renderer) {
6772
wvpRange.Init(D3D12_DESCRIPTOR_RANGE_TYPE_CBV, 1, 0);
6873

6974
CD3DX12_ROOT_PARAMETER wvpParam;
70-
wvpParam.InitAsConstantBufferView(0, D3D12_SHADER_VISIBILITY_VERTEX);
75+
wvpParam.InitAsDescriptorTable(1, &wvpRange, D3D12_SHADER_VISIBILITY_VERTEX);
76+
77+
D3D12_ROOT_PARAMETER rootParams[] = {
78+
wvpParam
79+
};
7180

81+
D3D12_ROOT_SIGNATURE_DESC rootDesc = { };
82+
rootDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;
83+
rootDesc.NumParameters = _countof(rootParams);
84+
rootDesc.pParameters = rootParams;
85+
rootDesc.NumStaticSamplers = 0;
86+
rootDesc.pStaticSamplers = nullptr;
87+
88+
ComPtr<ID3DBlob> rootBlob, errBlob;
89+
ThrowIfFailed(D3D12SerializeRootSignature(&rootDesc, D3D_ROOT_SIGNATURE_VERSION_1_0, rootBlob.GetAddressOf(), errBlob.GetAddressOf()));
90+
91+
if (errBlob) {
92+
spdlog::error("Light: Root signature error {0}", errBlob->GetBufferPointer());
93+
return;
94+
}
95+
96+
ThrowIfFailed(this->m_dev->CreateRootSignature(0, rootBlob->GetBufferPointer(), rootBlob->GetBufferSize(), IID_PPV_ARGS(this->m_rootSig.GetAddressOf())));
97+
98+
D3D12_INPUT_ELEMENT_DESC elements[] = {
99+
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, NULL }
100+
};
101+
102+
LPVOID lpVertex = nullptr;
103+
UINT nVertexSize = this->m_shader->GetBuffer(SHADER_BUFFER::VERTEX, lpVertex);
104+
105+
D3D12_INPUT_LAYOUT_DESC layout = { };
106+
layout.pInputElementDescs = elements;
107+
layout.NumElements = _countof(elements);
108+
109+
D3D12_GRAPHICS_PIPELINE_STATE_DESC plDesc = { };
110+
plDesc.Flags = D3D12_PIPELINE_STATE_FLAG_NONE;
111+
plDesc.DepthStencilState = CD3DX12_DEPTH_STENCIL_DESC(D3D12_DEFAULT);
112+
plDesc.DepthStencilState.DepthEnable = TRUE;
113+
plDesc.DepthStencilState.StencilEnable = FALSE;
114+
plDesc.InputLayout = layout;
115+
plDesc.VS.pShaderBytecode = lpVertex;
116+
plDesc.VS.BytecodeLength = nVertexSize;
117+
// plDesc.PS.pShaderBytecode = lpPixel;
118+
// plDesc.PS.BytecodeLength = nPixelSize;
119+
plDesc.pRootSignature = this->m_rootSig.Get();
120+
plDesc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
121+
plDesc.DSVFormat = DXGI_FORMAT_D24_UNORM_S8_UINT;
122+
plDesc.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
123+
plDesc.RasterizerState.CullMode = D3D12_CULL_MODE_BACK;
124+
plDesc.RasterizerState.FrontCounterClockwise = FALSE;
125+
plDesc.NumRenderTargets = 0;
126+
plDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
127+
plDesc.SampleDesc.Count = 8;
128+
plDesc.SampleMask = UINT32_MAX;
129+
130+
ThrowIfFailed(this->m_dev->CreateGraphicsPipelineState(&plDesc, IID_PPV_ARGS(this->m_plState.GetAddressOf())));
72131
}
73132

74133
void Light::InitConstantBuffers(D3D12* renderer) {
@@ -104,9 +163,18 @@ void Light::InitConstantBuffers(D3D12* renderer) {
104163

105164
void Light::Update() {
106165
GameObject::Update();
107-
this->InitConstantBuffers(dynamic_cast<D3D12*>(this->m_renderer));
108166
}
109167

110168
void Light::Render() {
169+
this->InitConstantBuffers(dynamic_cast<D3D12*>(this->m_renderer));
170+
171+
if (D3D12* renderer = dynamic_cast<D3D12*>(this->m_renderer)) {
172+
Descriptor dsvDesc = renderer->m_dsvHeap->GetDescriptor(this->m_nDepthIndex);
173+
this->m_list->ClearDepthStencilView(dsvDesc.cpuHandle, D3D12_CLEAR_FLAG_DEPTH, 1.f, 0.f, 0, nullptr);
174+
this->m_list->OMSetRenderTargets(0, nullptr, FALSE, &dsvDesc.cpuHandle);
175+
this->m_list->SetPipelineState(this->m_plState.Get());
176+
this->m_list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
177+
this->m_list->SetGraphicsRootSignature(this->m_rootSig.Get());
178+
}
111179

112180
}

src/private/Core/Renderer/D3D12.cpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ D3D12::D3D12() : Renderer::Renderer() {
1010

1111
this->m_nAlbedoIndex = 0;
1212
this->m_nUVIndex = 0;
13-
this->m_nPositionIndex = 0;
13+
this->m_nEmissiveIndex = 0;
1414
this->sceneMgr = SceneManager::GetInstance();
1515
this->m_samplerHeap = nullptr;
1616
this->m_editor = Editor::GetInstance();
@@ -96,18 +96,18 @@ void D3D12::Init(HWND hwnd) {
9696

9797
this->CreateTexture(this->m_nWidth, this->m_nHeight, 8, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET, this->m_albedoBuff);
9898
this->CreateTexture(this->m_nWidth, this->m_nHeight, 8, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET, this->m_uvBuff);
99-
this->CreateTexture(this->m_nWidth, this->m_nHeight, 8, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET, this->m_positionBuff);
99+
this->CreateTexture(this->m_nWidth, this->m_nHeight, 8, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET, this->m_emissiveBuff);
100100
this->CreateTexture(this->m_nWidth, this->m_nHeight, 8, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET, this->m_ORMBuff);
101101

102102
this->m_albedoBuff->SetName(L"Albedo");
103103
this->m_uvBuff->SetName(L"Normal");
104-
this->m_positionBuff->SetName(L"Position");
104+
this->m_emissiveBuff->SetName(L"Emissive");
105105
this->m_ORMBuff->SetName(L"ORM G-Buffer");
106106

107107
this->m_nAlbedoIndex = this->m_rtvHeap->GetDescriptorCount();
108108
this->m_nUVIndex = this->m_nAlbedoIndex + 1;
109-
this->m_nPositionIndex = this->m_nUVIndex + 1;
110-
this->m_nORMIndex = this->m_nPositionIndex + 1;
109+
this->m_nEmissiveIndex = this->m_nUVIndex + 1;
110+
this->m_nORMIndex = this->m_nEmissiveIndex + 1;
111111

112112
D3D12_RENDER_TARGET_VIEW_DESC GBufferDesc = { };
113113
GBufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
@@ -119,23 +119,23 @@ void D3D12::Init(HWND hwnd) {
119119

120120
Descriptor albedoDesc = this->m_rtvHeap->GetDescriptor(this->m_nAlbedoIndex);
121121
Descriptor UVDesc = this->m_rtvHeap->GetDescriptor(this->m_nUVIndex);
122-
Descriptor positionDesc = this->m_rtvHeap->GetDescriptor(this->m_nPositionIndex);
122+
Descriptor positionDesc = this->m_rtvHeap->GetDescriptor(this->m_nEmissiveIndex);
123123
Descriptor ORMDesc = this->m_rtvHeap->GetDescriptor(this->m_nORMIndex);
124124

125125
this->m_dev->CreateRenderTargetView(this->m_albedoBuff.Get(), &GBufferDesc, albedoDesc.cpuHandle);
126126
this->m_dev->CreateRenderTargetView(this->m_uvBuff.Get(), &GBufferDesc, UVDesc.cpuHandle);
127-
this->m_dev->CreateRenderTargetView(this->m_positionBuff.Get(), &GBufferDesc, positionDesc.cpuHandle);
127+
this->m_dev->CreateRenderTargetView(this->m_emissiveBuff.Get(), &GBufferDesc, positionDesc.cpuHandle);
128128
this->m_dev->CreateRenderTargetView(this->m_ORMBuff.Get(), &GBufferDesc, ORMDesc.cpuHandle);
129129

130130
this->ResourceBarrier(this->m_albedoBuff.Get(), D3D12_RESOURCE_STATE_GENERIC_READ, D3D12_RESOURCE_STATE_RENDER_TARGET);
131131
this->ResourceBarrier(this->m_uvBuff.Get(), D3D12_RESOURCE_STATE_GENERIC_READ, D3D12_RESOURCE_STATE_RENDER_TARGET);
132-
this->ResourceBarrier(this->m_positionBuff.Get(), D3D12_RESOURCE_STATE_GENERIC_READ, D3D12_RESOURCE_STATE_RENDER_TARGET);
132+
this->ResourceBarrier(this->m_emissiveBuff.Get(), D3D12_RESOURCE_STATE_GENERIC_READ, D3D12_RESOURCE_STATE_RENDER_TARGET);
133133
this->ResourceBarrier(this->m_ORMBuff.Get(), D3D12_RESOURCE_STATE_GENERIC_READ, D3D12_RESOURCE_STATE_RENDER_TARGET);
134134

135135
/* Track states */
136136
m_resourceStates[this->m_albedoBuff.Get()] = D3D12_RESOURCE_STATE_RENDER_TARGET;
137137
m_resourceStates[this->m_uvBuff.Get()] = D3D12_RESOURCE_STATE_RENDER_TARGET;
138-
m_resourceStates[this->m_positionBuff.Get()] = D3D12_RESOURCE_STATE_RENDER_TARGET;
138+
m_resourceStates[this->m_emissiveBuff.Get()] = D3D12_RESOURCE_STATE_RENDER_TARGET;
139139
m_resourceStates[this->m_ORMBuff.Get()] = D3D12_RESOURCE_STATE_RENDER_TARGET;
140140

141141
// We'll allocate the initial value for our ScreenQuad.
@@ -233,18 +233,29 @@ void D3D12::Update() {
233233
ComPtr<ID3D12Resource> actualBuffer = this->m_backBuffers[this->m_nActualBackBuffer];
234234
this->ResourceBarrier(actualBuffer, D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET);
235235

236+
ID3D12DescriptorHeap* descriptorHeaps[] = {
237+
this->m_cbvSrvHeap->m_heap.Get(),
238+
this->m_samplerHeap->m_heap.Get()
239+
};
240+
this->m_list->SetDescriptorHeaps(_countof(descriptorHeaps), descriptorHeaps);
241+
242+
this->m_list->RSSetViewports(1, &this->m_viewport);
243+
this->m_list->RSSetScissorRects(1, &this->m_scissor);
244+
245+
this->sceneMgr->ShadowPass();
246+
236247
if (this->m_resourceStates[this->m_albedoBuff.Get()] != D3D12_RESOURCE_STATE_RENDER_TARGET) {
237248
this->ResourceBarrier(this->m_albedoBuff, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET);
238249
this->ResourceBarrier(this->m_uvBuff, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET);
239-
this->ResourceBarrier(this->m_positionBuff, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET);
250+
this->ResourceBarrier(this->m_emissiveBuff, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET);
240251
this->ResourceBarrier(this->m_ORMBuff, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET);
241252
}
242253

243254

244255
Descriptor albedoDesc = this->m_rtvHeap->GetDescriptor(this->m_nAlbedoIndex);
245256
Descriptor UVDesc = this->m_rtvHeap->GetDescriptor(this->m_nUVIndex);
246257
Descriptor ORMDesc = this->m_rtvHeap->GetDescriptor(this->m_nORMIndex);
247-
Descriptor positionDesc = this->m_rtvHeap->GetDescriptor(this->m_nPositionIndex);
258+
Descriptor positionDesc = this->m_rtvHeap->GetDescriptor(this->m_nEmissiveIndex);
248259
Descriptor dsvDesc = this->m_dsvHeap->GetDescriptor(0);
249260
this->m_list->ClearRenderTargetView(albedoDesc.cpuHandle, RGBA{ 0.f, 0.f, 0.f, 1.f }, 0, nullptr);
250261
this->m_list->ClearRenderTargetView(UVDesc.cpuHandle, RGBA{ 0.f, 0.f, 0.f, 1.f }, 0, nullptr);
@@ -263,17 +274,12 @@ void D3D12::Update() {
263274
};
264275
this->m_list->OMSetRenderTargets(_countof(gbuffers), gbuffers, FALSE, &dsvDesc.cpuHandle);
265276

266-
ID3D12DescriptorHeap* descriptorHeaps[] = {
267-
this->m_cbvSrvHeap->m_heap.Get(),
268-
this->m_samplerHeap->m_heap.Get()
269-
};
270-
this->m_list->SetDescriptorHeaps(_countof(descriptorHeaps), descriptorHeaps);
271277

272278
this->sceneMgr->Render();
273279

274280
this->ResourceBarrier(this->m_albedoBuff, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
275281
this->ResourceBarrier(this->m_uvBuff, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
276-
this->ResourceBarrier(this->m_positionBuff, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
282+
this->ResourceBarrier(this->m_emissiveBuff, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
277283
this->ResourceBarrier(this->m_ORMBuff, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
278284

279285
Descriptor rtv = this->m_rtvHeap->GetDescriptor(this->m_nActualBackBuffer);
@@ -319,8 +325,6 @@ void D3D12::Update() {
319325

320326
ImGuizmo::SetRect(0.0f, 0.0f, static_cast<float>(this->m_nWidth), static_cast<float>(this->m_nHeight));
321327

322-
323-
324328
this->m_editor->Update();
325329

326330
ImGui::Render();

0 commit comments

Comments
 (0)