Skip to content

Commit 171c18b

Browse files
committed
Get Vertex position from Z-Buffer
1 parent 66a1e4d commit 171c18b

File tree

4 files changed

+106
-29
lines changed

4 files changed

+106
-29
lines changed

src/Shader/LightPass.hlsl

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#define PI 3.14159265359
22
cbuffer ScreenQuadBuffer : register(b0)
33
{
4+
matrix InverseView;
5+
matrix InverseProjection;
46
float3 cameraPos;
7+
float2 screenSize;
58
}
69

710
struct VertexOutput
@@ -18,7 +21,7 @@ VertexOutput VertexMain(float4 position : POSITION0, float2 uv : TEXCOORD0)
1821

1922
Texture2DMS<float4> albedo : register(t0);
2023
Texture2DMS<float4> normal : register(t1);
21-
Texture2DMS<float4> position : register(t2);
24+
Texture2DMS<float4> depthTex : register(t2);
2225
Texture2DMS<float4> orm : register(t3);
2326

2427
struct PixelOutput
@@ -66,14 +69,41 @@ float GeometrySmith(float3 N, float3 V, float3 L, float roughness)
6669
return ggx1 * ggx2;
6770
}
6871

72+
float LinearizeDepth(float depth)
73+
{
74+
float z = depth * 2.0 - 1.0;
75+
float near = 0.1f;
76+
float far = 100.0f;
77+
return (2.0 * near * far) / (far + near - z * (far - near));
78+
}
79+
80+
float3 ReconstructPosition(float2 pixelCoord, uint index)
81+
{
82+
float depth = LinearizeDepth(depthTex.Load(pixelCoord, index).r);
83+
84+
float2 ndc = (pixelCoord / screenSize) * 2.0f - 1.0f;
85+
ndc.y *= -1.0f;
86+
87+
float4 clipPos = float4(ndc.xy, depth, 1.0f);
88+
89+
float4 viewPos = mul(InverseProjection, clipPos);
90+
viewPos /= viewPos.w;
91+
92+
float4 worldPos = mul(InverseView, viewPos);
93+
94+
return worldPos.xyz;
95+
}
96+
97+
6998
PixelOutput PixelMain(VertexOutput input, uint index : SV_SampleIndex)
7099
{
71100
float3 lightPos = float3(0.f, 5.f, 5.f);
72101
float3 lightColor = float3(100.f, 100.f, 100.f);
73102

74103
float4 albedoColor = albedo.Load(input.position.xy, index);
75104
float4 nml = normalize(normal.Load(input.position.xy, index) * 2 - 1);
76-
float4 vertexPos = position.Load(input.position.xy, index);
105+
float3 pos = ReconstructPosition(input.position.xy, index);
106+
float4 vertexPos = float4(pos.x, pos.y, pos.z, 1.f);
77107

78108
/* ORM G-Buffer */
79109
float3 ormData = orm.Load(input.position.xy, index).rgb;
@@ -115,10 +145,10 @@ PixelOutput PixelMain(VertexOutput input, uint index : SV_SampleIndex)
115145

116146
Lo += (kD * albedoColor.xyz / PI + specular) * radiance * NdotL;
117147

118-
float3 ambient = float3(0.3f, 0.3f, 0.3f) * albedoColor.xyz * ao;
119-
ambient = lerp(ambient, albedoColor.xyz * ao * 0.2, metallic); // Los metales reflejan más el ambiente
148+
float3 ambient = 0.1f * albedoColor.xyz * ao;
120149

121150
float3 color = ambient + Lo;
151+
//float3 color = vertexPos.xyz;
122152

123153
PixelOutput output;
124154

src/private/Core/Renderer/D3D12.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,20 +187,20 @@ void D3D12::InitDepth() {
187187
Descriptor dsv = this->m_dsvHeap->GetDescriptor(0);
188188

189189
D3D12_RESOURCE_DESC depthBuffDesc = { };
190-
depthBuffDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
190+
depthBuffDesc.Format = DXGI_FORMAT_R24G8_TYPELESS;
191191
depthBuffDesc.SampleDesc.Count = 8;
192192
depthBuffDesc.Width = this->m_nWidth;
193193
depthBuffDesc.Height = this->m_nHeight;
194194
depthBuffDesc.MipLevels = 1;
195195
depthBuffDesc.DepthOrArraySize = 1;
196-
depthBuffDesc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL | D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE;
196+
depthBuffDesc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
197197
depthBuffDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
198198

199199
D3D12_HEAP_PROPERTIES heapProps = { };
200200
heapProps.Type = D3D12_HEAP_TYPE_DEFAULT;
201201

202202
D3D12_CLEAR_VALUE dsvClear = { };
203-
dsvClear.Format = depthBuffDesc.Format;
203+
dsvClear.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
204204
dsvClear.DepthStencil.Depth = 1.f;
205205
dsvClear.DepthStencil.Stencil = 0.f;
206206

@@ -214,7 +214,7 @@ void D3D12::InitDepth() {
214214
this->m_depthBuffer->SetName(L"Depth buffer");
215215

216216
D3D12_DEPTH_STENCIL_VIEW_DESC dsvDesc = { };
217-
dsvDesc.Format = depthBuffDesc.Format;
217+
dsvDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
218218
dsvDesc.Flags = D3D12_DSV_FLAG_NONE;
219219
dsvDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2DMS;
220220

src/private/Core/Renderer/ScreenQuad.cpp

Lines changed: 65 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void ScreenQuad::D3D12Init(D3D12* renderer) {
6767

6868
Descriptor albedoDesc = renderer->m_cbvSrvHeap->GetDescriptor(0);
6969
Descriptor normalDesc = renderer->m_cbvSrvHeap->GetDescriptor(1);
70-
Descriptor positionDesc = renderer->m_cbvSrvHeap->GetDescriptor(2);
70+
Descriptor depthDesc = renderer->m_cbvSrvHeap->GetDescriptor(2);
7171
Descriptor ORMDesc = renderer->m_cbvSrvHeap->GetDescriptor(3);
7272

7373
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = { };
@@ -76,38 +76,44 @@ void ScreenQuad::D3D12Init(D3D12* renderer) {
7676
srvDesc.Texture2D.MipLevels = 1;
7777
srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
7878

79+
D3D12_SHADER_RESOURCE_VIEW_DESC dsvSrvDesc = { };
80+
dsvSrvDesc.Format = DXGI_FORMAT_R24_UNORM_X8_TYPELESS;
81+
dsvSrvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2DMS;
82+
dsvSrvDesc.Texture2D.MipLevels = 1;
83+
dsvSrvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
84+
7985
this->m_dev->CreateShaderResourceView(renderer->m_albedoBuff.Get(), &srvDesc, albedoDesc.cpuHandle);
8086
this->m_dev->CreateShaderResourceView(renderer->m_uvBuff.Get(), &srvDesc, normalDesc.cpuHandle);
81-
this->m_dev->CreateShaderResourceView(renderer->m_positionBuff.Get(), &srvDesc, positionDesc.cpuHandle);
87+
this->m_dev->CreateShaderResourceView(renderer->m_depthBuffer.Get(), &dsvSrvDesc, depthDesc.cpuHandle);
8288
this->m_dev->CreateShaderResourceView(renderer->m_ORMBuff.Get(), &srvDesc, ORMDesc.cpuHandle);
8389

8490
CD3DX12_DESCRIPTOR_RANGE albedoRange;
8591
CD3DX12_DESCRIPTOR_RANGE normalRange;
86-
CD3DX12_DESCRIPTOR_RANGE positionRange;
92+
CD3DX12_DESCRIPTOR_RANGE depthRange;
8793
CD3DX12_DESCRIPTOR_RANGE ORMRange;
8894
CD3DX12_DESCRIPTOR_RANGE cbuffRange;
8995

9096
albedoRange.Init(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, 0);
9197
normalRange.Init(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, 1);
92-
positionRange.Init(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, 2);
98+
depthRange.Init(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, 2);
9399
ORMRange.Init(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, 3);
94100
cbuffRange.Init(D3D12_DESCRIPTOR_RANGE_TYPE_CBV, 1, 0);
95101

96102
CD3DX12_ROOT_PARAMETER albedoParam;
97103
CD3DX12_ROOT_PARAMETER normalParam;
98-
CD3DX12_ROOT_PARAMETER positionParam;
104+
CD3DX12_ROOT_PARAMETER depthParam;
99105
CD3DX12_ROOT_PARAMETER ORMParam;
100106
CD3DX12_ROOT_PARAMETER cbuffParam;
101107
albedoParam.InitAsDescriptorTable(1, &albedoRange, D3D12_SHADER_VISIBILITY_PIXEL);
102108
normalParam.InitAsDescriptorTable(1, &normalRange, D3D12_SHADER_VISIBILITY_PIXEL);
103-
positionParam.InitAsDescriptorTable(1, &positionRange, D3D12_SHADER_VISIBILITY_PIXEL);
109+
depthParam.InitAsDescriptorTable(1, &depthRange, D3D12_SHADER_VISIBILITY_PIXEL);
104110
ORMParam.InitAsDescriptorTable(1, &ORMRange, D3D12_SHADER_VISIBILITY_PIXEL);
105111
cbuffParam.InitAsDescriptorTable(1, &cbuffRange, D3D12_SHADER_VISIBILITY_PIXEL);
106112

107113
D3D12_ROOT_PARAMETER rootParams[] = {
108114
albedoParam,
109115
normalParam,
110-
positionParam,
116+
depthParam,
111117
ORMParam,
112118
cbuffParam
113119
};
@@ -177,6 +183,23 @@ void ScreenQuad::InitConstantBuffer() {
177183
if (D3D12* renderer = dynamic_cast<D3D12*>(this->m_renderer)) {
178184
Camera* currentCamera = this->m_sceneMgr->GetCurrentScene()->GetCurrentCamera();
179185

186+
Transform cameraTransform = this->m_sceneMgr->GetCurrentScene()->GetCurrentCamera()->transform;
187+
this->m_sqCBuffData.InverseView = XMMatrixTranspose(XMMatrixIdentity());
188+
this->m_sqCBuffData.InverseView *= XMMatrixTranspose(XMMatrixRotationX(XMConvertToRadians(currentCamera->transform.rotation.x)));
189+
this->m_sqCBuffData.InverseView *= XMMatrixTranspose(XMMatrixRotationY(XMConvertToRadians(currentCamera->transform.rotation.y)));
190+
this->m_sqCBuffData.InverseView *= XMMatrixTranspose(XMMatrixRotationZ(XMConvertToRadians(currentCamera->transform.rotation.z)));
191+
this->m_sqCBuffData.InverseView *= XMMatrixTranspose(XMMatrixTranslation(
192+
currentCamera->transform.location.x,
193+
currentCamera->transform.location.y,
194+
currentCamera->transform.location.z));
195+
196+
this->m_sqCBuffData.InverseProjection = XMMatrixTranspose(XMMatrixPerspectiveFovRH(
197+
XMConvertToRadians(70.f),
198+
static_cast<float>(renderer->m_nWidth) / static_cast<float>(renderer->m_nHeight),
199+
0.01f,
200+
3000.f));
201+
202+
180203
this->m_sqCBuffData.cameraPosition = XMFLOAT3(
181204
currentCamera->transform.location.x,
182205
currentCamera->transform.location.y,
@@ -198,27 +221,49 @@ void ScreenQuad::InitConstantBuffer() {
198221
}
199222

200223
void ScreenQuad::UpdateConstantBuffer() {
201-
Camera* currentCamera = this->m_sceneMgr->GetCurrentScene()->GetCurrentCamera();
224+
if (D3D12* renderer = dynamic_cast<D3D12*>(this->m_renderer)) {
225+
Camera* currentCamera = this->m_sceneMgr->GetCurrentScene()->GetCurrentCamera();
226+
227+
Transform cameraTransform = this->m_sceneMgr->GetCurrentScene()->GetCurrentCamera()->transform;
228+
this->m_sqCBuffData.InverseView = XMMatrixTranspose(XMMatrixIdentity());
229+
this->m_sqCBuffData.InverseView *= XMMatrixTranspose(XMMatrixRotationX(XMConvertToRadians(currentCamera->transform.rotation.x)));
230+
this->m_sqCBuffData.InverseView *= XMMatrixTranspose(XMMatrixRotationY(XMConvertToRadians(currentCamera->transform.rotation.y)));
231+
this->m_sqCBuffData.InverseView *= XMMatrixTranspose(XMMatrixRotationZ(XMConvertToRadians(currentCamera->transform.rotation.z)));
232+
this->m_sqCBuffData.InverseView *= XMMatrixTranspose(XMMatrixTranslation(
233+
currentCamera->transform.location.x,
234+
currentCamera->transform.location.y,
235+
currentCamera->transform.location.z));
236+
this->m_sqCBuffData.InverseView = (XMMatrixInverse(nullptr, this->m_sqCBuffData.InverseView));
202237

203-
this->m_sqCBuffData.cameraPosition = XMFLOAT3(
204-
currentCamera->transform.location.x,
205-
currentCamera->transform.location.y,
206-
currentCamera->transform.location.z
207-
);
238+
this->m_sqCBuffData.InverseProjection = XMMatrixTranspose(XMMatrixPerspectiveFovRH(
239+
XMConvertToRadians(70.f),
240+
static_cast<float>(renderer->m_nWidth) / static_cast<float>(renderer->m_nHeight),
241+
0.01f,
242+
3000.f));
243+
this->m_sqCBuffData.InverseProjection = XMMatrixInverse(nullptr, this->m_sqCBuffData.InverseProjection);
244+
this->m_sqCBuffData.screenSize = XMFLOAT2(renderer->m_nWidth, renderer->m_nHeight);
208245

209-
UINT nConstantBufferSize = (sizeof(this->m_sqCBuffData) + 255) & ~255;
210246

211-
PVOID pData;
212-
ThrowIfFailed(this->m_sqCBuffer->Map(0, nullptr, &pData));
213-
memcpy(pData, &this->m_sqCBuffData, nConstantBufferSize);
214-
this->m_sqCBuffer->Unmap(0, nullptr);
247+
this->m_sqCBuffData.cameraPosition = XMFLOAT3(
248+
currentCamera->transform.location.x,
249+
currentCamera->transform.location.y,
250+
currentCamera->transform.location.z
251+
);
252+
253+
UINT nConstantBufferSize = (sizeof(this->m_sqCBuffData) + 255) & ~255;
254+
255+
PVOID pData;
256+
ThrowIfFailed(this->m_sqCBuffer->Map(0, nullptr, &pData));
257+
memcpy(pData, &this->m_sqCBuffData, nConstantBufferSize);
258+
this->m_sqCBuffer->Unmap(0, nullptr);
259+
}
215260
}
216261

217262
void ScreenQuad::D3D12Render(D3D12* renderer) {
218263
this->UpdateConstantBuffer();
219264
Descriptor albedoDesc = renderer->m_cbvSrvHeap->GetDescriptor(0);
220265
Descriptor normalDesc = renderer->m_cbvSrvHeap->GetDescriptor(1);
221-
Descriptor positionDesc = renderer->m_cbvSrvHeap->GetDescriptor(2);
266+
Descriptor depthParam = renderer->m_cbvSrvHeap->GetDescriptor(2);
222267
Descriptor materialDesc = renderer->m_cbvSrvHeap->GetDescriptor(3);
223268
Descriptor sqBuffDesc = renderer->m_cbvSrvHeap->GetDescriptor(this->m_nSqCBuffIndex);
224269
this->m_list->OMSetRenderTargets(1, &this->m_rtvDescriptor.cpuHandle, FALSE, nullptr);
@@ -232,8 +277,7 @@ void ScreenQuad::D3D12Render(D3D12* renderer) {
232277

233278
this->m_list->SetGraphicsRootDescriptorTable(0, albedoDesc.gpuHandle);
234279
this->m_list->SetGraphicsRootDescriptorTable(1, normalDesc.gpuHandle);
235-
this->m_list->SetGraphicsRootDescriptorTable(2, positionDesc.gpuHandle);
236-
this->m_list->SetGraphicsRootDescriptorTable(3, materialDesc.gpuHandle);
280+
this->m_list->SetGraphicsRootDescriptorTable(2, depthParam.gpuHandle);
237281
this->m_list->SetGraphicsRootDescriptorTable(3, materialDesc.gpuHandle);
238282
this->m_list->SetGraphicsRootDescriptorTable(4, sqBuffDesc.gpuHandle);
239283

src/public/Core/Renderer/ScreenQuad.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ class SceneManager;
1919

2020

2121
struct ScreenQuadBuffer {
22+
XMMATRIX InverseView;
23+
XMMATRIX InverseProjection;
2224
XMFLOAT3 cameraPosition;
25+
XMFLOAT2 screenSize;
2326
};
2427

2528
class ScreenQuad {

0 commit comments

Comments
 (0)