File size: 10,719 Bytes
0c51b93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

#include "NvCoDx12RenderTarget.h"
#include "appD3D12Ctx.h"

#include <external/D3D12/include/d3dx12.h>

namespace nvidia {
namespace Common {

Dx12RenderTarget::Dx12RenderTarget()
{
	memset(m_srvIndices, -1, sizeof(m_srvIndices));
}

int Dx12RenderTarget::init(AppGraphCtxD3D12* renderContext, const Desc& desc)
{
	ID3D12Device* device = renderContext->m_device;

	m_desc = desc;

	// set viewport
	{
		m_viewport.Width = float(desc.m_width);
		m_viewport.Height = float(desc.m_height);
		m_viewport.MinDepth = 0;
		m_viewport.MaxDepth = 1;
		m_viewport.TopLeftX = 0;
		m_viewport.TopLeftY = 0;
	}

	{
		m_scissorRect.left = 0;
		m_scissorRect.top = 0;
		m_scissorRect.right = desc.m_width;
		m_scissorRect.bottom = desc.m_height;
	}

	if (desc.m_targetFormat != DXGI_FORMAT_UNKNOWN)
	{
		D3D12_DESCRIPTOR_HEAP_DESC heapDesc = {};
		heapDesc.NumDescriptors = 1;
		heapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
		heapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
		NV_RETURN_ON_FAIL(device->CreateDescriptorHeap(&heapDesc, IID_PPV_ARGS(&m_descriptorHeapRtv)));

		DXGI_FORMAT resourceFormat = DxFormatUtil::calcResourceFormat(DxFormatUtil::USAGE_TARGET, m_desc.m_usageFlags, desc.m_targetFormat);
		DXGI_FORMAT targetFormat = DxFormatUtil::calcFormat(DxFormatUtil::USAGE_TARGET, resourceFormat);

		D3D12_RESOURCE_DESC resourceDesc = CD3DX12_RESOURCE_DESC::Tex2D(resourceFormat, UINT(desc.m_width), UINT(desc.m_height), 1, 1, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET, D3D12_TEXTURE_LAYOUT_UNKNOWN, 0);
		D3D12_HEAP_PROPERTIES heapProps = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);

		// Depending on usage this format may not be right...
		D3D12_CLEAR_VALUE clearValue;
		clearValue.Format = targetFormat;
		clearValue.Color[0] = desc.m_targetClearColor[0];
		clearValue.Color[1] = desc.m_targetClearColor[1];
		clearValue.Color[2] = desc.m_targetClearColor[2];
		clearValue.Color[3] = desc.m_targetClearColor[3];

		NV_RETURN_ON_FAIL(m_renderTarget.initCommitted(device, heapProps, D3D12_HEAP_FLAG_NONE, resourceDesc, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, &clearValue));

		D3D12_RENDER_TARGET_VIEW_DESC rtvDesc = {};
		rtvDesc.Format = targetFormat;
		rtvDesc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D;
		rtvDesc.Texture2D.MipSlice = 0;
		rtvDesc.Texture2D.PlaneSlice = 0;
		device->CreateRenderTargetView(m_renderTarget, &rtvDesc, m_descriptorHeapRtv->GetCPUDescriptorHandleForHeapStart());
	}

	// Create the depth stencil descriptor heap
	if (desc.m_depthStencilFormat != DXGI_FORMAT_UNKNOWN)
	{
		D3D12_DESCRIPTOR_HEAP_DESC heapDesc = {};
		heapDesc.NumDescriptors = 1;
		heapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV;
		heapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
		NV_RETURN_ON_FAIL(device->CreateDescriptorHeap(&heapDesc, IID_PPV_ARGS(&m_descriptorHeapDsv)));
	
		DXGI_FORMAT resourceFormat = DxFormatUtil::calcResourceFormat(DxFormatUtil::USAGE_DEPTH_STENCIL, m_desc.m_usageFlags, desc.m_depthStencilFormat);
		DXGI_FORMAT depthStencilFormat = DxFormatUtil::calcFormat(DxFormatUtil::USAGE_DEPTH_STENCIL, resourceFormat);

		D3D12_RESOURCE_DESC resourceDesc = CD3DX12_RESOURCE_DESC::Tex2D(resourceFormat, UINT(desc.m_width), UINT(desc.m_height), 1, 1, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL, D3D12_TEXTURE_LAYOUT_UNKNOWN, 0);
		D3D12_HEAP_PROPERTIES heapProps = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);

		D3D12_CLEAR_VALUE clearValue;
		clearValue.Format = depthStencilFormat; 
		clearValue.DepthStencil.Depth = 1.0f;
		clearValue.DepthStencil.Stencil = 0;
		NV_RETURN_ON_FAIL(m_depthStencilBuffer.initCommitted(device, heapProps, D3D12_HEAP_FLAG_NONE, resourceDesc, D3D12_RESOURCE_STATE_DEPTH_WRITE, &clearValue));
		
		// Create the depth stencil view
		D3D12_DEPTH_STENCIL_VIEW_DESC dsvDesc = {};
		dsvDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
		dsvDesc.Format = depthStencilFormat;
		dsvDesc.Texture2D.MipSlice = 0;
		dsvDesc.Flags = D3D12_DSV_FLAG_NONE;
		device->CreateDepthStencilView(m_depthStencilBuffer, &dsvDesc, m_descriptorHeapDsv->GetCPUDescriptorHandleForHeapStart());
	}

	return NV_OK;
}

/* static */DxFormatUtil::UsageType Dx12RenderTarget::getUsageType(BufferType type)
{
	switch (type)
	{
		case BUFFER_DEPTH_STENCIL:	return DxFormatUtil::USAGE_DEPTH_STENCIL;
		case BUFFER_TARGET:			return DxFormatUtil::USAGE_TARGET;
		default:					return DxFormatUtil::USAGE_UNKNOWN;
	}
}

void Dx12RenderTarget::bind(AppGraphCtxD3D12* renderContext)
{
	ID3D12GraphicsCommandList* commandList = renderContext->m_commandList;

	// Work out what is bound
	int numTargets = 0;
	D3D12_CPU_DESCRIPTOR_HANDLE dsvHandle = {};
	D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = {};

	if (m_renderTarget)
	{
		rtvHandle = m_descriptorHeapRtv->GetCPUDescriptorHandleForHeapStart();
		numTargets = 1;
	}
	if (m_depthStencilBuffer)
	{
		dsvHandle = m_descriptorHeapDsv->GetCPUDescriptorHandleForHeapStart();
		commandList->OMSetRenderTargets(numTargets, &rtvHandle, FALSE, &dsvHandle);
	}
	else
	{
		commandList->OMSetRenderTargets(numTargets, &rtvHandle, FALSE, nullptr);
	}

	commandList->RSSetViewports(1, &m_viewport);
	commandList->RSSetScissorRects(1, &m_scissorRect);
}


void Dx12RenderTarget::clear(AppGraphCtxD3D12* renderContext)
{
	ID3D12GraphicsCommandList* commandList = renderContext->m_commandList;
	D3D12_RECT rect = { 0, 0, m_desc.m_width, m_desc.m_height };

	// Clear
	if (m_renderTarget)
	{
		D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = m_descriptorHeapRtv->GetCPUDescriptorHandleForHeapStart();
		commandList->ClearRenderTargetView(rtvHandle, m_desc.m_targetClearColor, 1, &rect);
	}
	if (m_depthStencilBuffer)
	{
		D3D12_CPU_DESCRIPTOR_HANDLE dsvHandle = m_descriptorHeapDsv->GetCPUDescriptorHandleForHeapStart();
		commandList->ClearDepthStencilView(dsvHandle, D3D12_CLEAR_FLAG_DEPTH, m_desc.m_depthStencilClearDepth, 0, 1, &rect);
	}
}

void Dx12RenderTarget::bindAndClear(AppGraphCtxD3D12* renderContext)
{
	ID3D12GraphicsCommandList* commandList = renderContext->m_commandList;

	// Work out what is bound
	int numTargets = 0;
	D3D12_CPU_DESCRIPTOR_HANDLE dsvHandle = {}; 
	D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = {}; 

	if (m_renderTarget)
	{
		rtvHandle = m_descriptorHeapRtv->GetCPUDescriptorHandleForHeapStart();
		numTargets = 1;
	}
	if (m_depthStencilBuffer)
	{
		dsvHandle = m_descriptorHeapDsv->GetCPUDescriptorHandleForHeapStart();
		commandList->OMSetRenderTargets(numTargets, &rtvHandle, FALSE, &dsvHandle);
	}
	else
	{
		commandList->OMSetRenderTargets(numTargets, &rtvHandle, FALSE, nullptr);
	}

	commandList->RSSetViewports(1, &m_viewport);
	commandList->RSSetScissorRects(1, &m_scissorRect);

	D3D12_RECT rect = { 0, 0, m_desc.m_width, m_desc.m_height };

	// Clear
	if (m_renderTarget)
	{
		commandList->ClearRenderTargetView(rtvHandle, m_desc.m_targetClearColor, 1, &rect);
	}
	if (m_depthStencilBuffer)
	{
		commandList->ClearDepthStencilView(dsvHandle, D3D12_CLEAR_FLAG_DEPTH, m_desc.m_depthStencilClearDepth, 0, 1, &rect);
	}
}

void Dx12RenderTarget::setShadowDefaultLight(FXMVECTOR eye, FXMVECTOR at, FXMVECTOR up)
{
	float sizeX = 50.0f;
	float sizeY = 50.0f;
	float znear = -200.0f;
	float zfar = 200.0f;

	setShadowLightMatrices(eye, at, up, sizeX, sizeY, znear, zfar);
}

void Dx12RenderTarget::setShadowLightMatrices(FXMVECTOR eye, FXMVECTOR lookAt, FXMVECTOR up, float sizeX, float sizeY, float zNear, float zFar)
{
	m_shadowLightView = XMMatrixLookAtLH(eye, lookAt, up);
	m_shadowLightProjection = XMMatrixOrthographicLH(sizeX, sizeY, zNear, zFar);
	DirectX::XMMATRIX clip2Tex(0.5, 0, 0, 0,
		0, -0.5, 0, 0,
		0, 0, 1, 0,
		0.5, 0.5, 0, 1);
	DirectX::XMMATRIX viewProjection = m_shadowLightView * m_shadowLightProjection;
	m_shadowLightWorldToTex = viewProjection * clip2Tex;
}

void Dx12RenderTarget::toWritable(AppGraphCtxD3D12* renderContext)
{
	Dx12BarrierSubmitter submitter(renderContext->m_commandList);

	m_renderTarget.transition(D3D12_RESOURCE_STATE_RENDER_TARGET, submitter);
	m_depthStencilBuffer.transition(D3D12_RESOURCE_STATE_DEPTH_WRITE, submitter);
}

void Dx12RenderTarget::toReadable(AppGraphCtxD3D12* renderContext)
{
	Dx12BarrierSubmitter submitter(renderContext->m_commandList);

	m_renderTarget.transition(D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, submitter);
	m_depthStencilBuffer.transition(D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, submitter);
}

DXGI_FORMAT Dx12RenderTarget::getSrvFormat(BufferType type) const
{
	return Dx12Resource::calcFormat(DxFormatUtil::USAGE_SRV, getResource(type));
}

DXGI_FORMAT Dx12RenderTarget::getTargetFormat(BufferType type) const
{
	return Dx12Resource::calcFormat(getUsageType(type), getResource(type));
}

D3D12_SHADER_RESOURCE_VIEW_DESC Dx12RenderTarget::calcDefaultSrvDesc(BufferType type) const
{
	ID3D12Resource* resource = getResource(type);

	D3D12_RESOURCE_DESC resourceDesc = resource->GetDesc();

	D3D12_SHADER_RESOURCE_VIEW_DESC desc;

	desc.Format = getSrvFormat(type);
	desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;

	desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;

	desc.Texture2D.MipLevels = resourceDesc.MipLevels;
	desc.Texture2D.MostDetailedMip = 0;
	desc.Texture2D.PlaneSlice = 0;
	desc.Texture2D.ResourceMinLODClamp = 0.0f;
	return desc;
}

int Dx12RenderTarget::allocateSrvView(BufferType type, ID3D12Device* device, Dx12DescriptorHeap& heap, const D3D12_SHADER_RESOURCE_VIEW_DESC* desc)
{
	if (m_srvIndices[type] >= 0)
	{
		printf("A srv index has already been associated!");
		return m_srvIndices[type];
	}

	if (!desc)
	{
		// If not set construct a default desc
		D3D12_SHADER_RESOURCE_VIEW_DESC defaultDesc = calcDefaultSrvDesc(type);
		return allocateSrvView(type, device, heap, &defaultDesc);
	}

	// Create the srv for the shadow buffer
	ID3D12Resource* resource = getResource(type);
	int srvIndex = heap.allocate();

	if (srvIndex < 0)
	{
		return srvIndex;
	}

	// Create on the the heap
	device->CreateShaderResourceView(resource, desc, heap.getCpuHandle(srvIndex));

	m_srvIndices[type] = srvIndex;
	// Return the allocated index
	return srvIndex;
}

void Dx12RenderTarget::setDebugName(BufferType type, const std::wstring& name)
{
	getResource(type).setDebugName(name);
}

void Dx12RenderTarget::setDebugName(const std::wstring& name)
{
	std::wstring buf(name);
	buf += L" [Target]";
	setDebugName(BUFFER_TARGET, buf);
	buf = name;
	buf += L" [DepthStencil]";
	setDebugName(BUFFER_DEPTH_STENCIL, buf);
}


void Dx12RenderTarget::createSrv(ID3D12Device* device, Dx12DescriptorHeap& heap, BufferType type, int descriptorIndex)
{
	D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = calcDefaultSrvDesc(type);
	ID3D12Resource* resource = getResource(type);
	device->CreateShaderResourceView(resource, &srvDesc, heap.getCpuHandle(descriptorIndex));
}

} // namespace Common 
} // namespace nvidia