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
|
/*
* Copyright (c) 2008-2018, NVIDIA CORPORATION. All rights reserved.
*
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto. Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
*/
#pragma once
#if SUPPORT_D3D12
#include <Windows.h>
#include <tchar.h>
#include <wrl/client.h>
#include <stdexcept>
#include <dxgi1_3.h>
#include <d3d12.h>
#include <d3dcompiler.h>
#include "d3dx12.h"
typedef D3D12_SHADER_BYTECODE GFSDK_D3D12_VertexShader;
typedef D3D12_SHADER_BYTECODE GFSDK_D3D12_GeometryShader;
typedef D3D12_SHADER_BYTECODE GFSDK_D3D12_PixelShader;
//--------------------------------------------------------------------------------
struct ShaderResourceView
{
ShaderResourceView()
{
memset(this, 0, sizeof(*this));
}
ShaderResourceView(GFSDK_SSAO_ShaderResourceView_D3D12 A)
: pResource(A.pResource)
, GpuHandle({ A.GpuHandle })
{
}
ID3D12Resource* pResource;
D3D12_GPU_DESCRIPTOR_HANDLE GpuHandle;
};
struct RenderTargetView
{
RenderTargetView()
{
memset(this, 0, sizeof(*this));
}
RenderTargetView(GFSDK_SSAO_RenderTargetView_D3D12 A)
: pResource(A.pResource)
, CpuHandle({ A.CpuHandle })
{
}
ID3D12Resource* pResource;
D3D12_CPU_DESCRIPTOR_HANDLE CpuHandle;
};
struct GFSDK_D3D12_DescriptorHeap
{
D3D12_DESCRIPTOR_HEAP_DESC Desc;
ID3D12DescriptorHeap* pDescHeap;
D3D12_CPU_DESCRIPTOR_HANDLE hCPUHeap;
D3D12_GPU_DESCRIPTOR_HANDLE hGPUHeap;
UINT HandleIncrementSize;
UINT NumDescriptors;
UINT BaseIndex;
void InitDescriptorHeap(ID3D12Device* pDevice, D3D12_DESCRIPTOR_HEAP_TYPE HeapType, const GFSDK_SSAO_DescriptorHeapRange_D3D12* pHeapInfo, bool bShaderVisible)
{
NumDescriptors = pHeapInfo->pDescHeap->GetDesc().NumDescriptors;
pDescHeap = pHeapInfo->pDescHeap;
BaseIndex = pHeapInfo->BaseIndex;
if (NumDescriptors > 0)
{
pDescHeap->AddRef();
hCPUHeap = pDescHeap->GetCPUDescriptorHandleForHeapStart();
if (bShaderVisible)
{
hGPUHeap = pDescHeap->GetGPUDescriptorHandleForHeapStart();
}
HandleIncrementSize = pDevice->GetDescriptorHandleIncrementSize(HeapType);
}
else
{
pDescHeap = nullptr;
HandleIncrementSize = 0;
}
}
void Release()
{
SAFE_RELEASE(pDescHeap);
}
D3D12_CPU_DESCRIPTOR_HANDLE GetCPUHandle(UINT index)
{
ASSERT(index < NumDescriptors);
return{ hCPUHeap.ptr + (BaseIndex + index) * HandleIncrementSize };
}
D3D12_GPU_DESCRIPTOR_HANDLE GetGPUHandle(UINT index)
{
ASSERT(index < NumDescriptors);
return{ hGPUHeap.ptr + (BaseIndex + index) * HandleIncrementSize };
}
};
enum CBVSRVUAVLayoutBase
{
eGlobalCB = 0,
ePerPassCB,
ePerPassCBEnd = ePerPassCB + 16 - 1,
eFullResViewDepthTexture,
eFullResViewDepthTexture2,
eFullResNormalTexture,
eQuarterResViewDepthTextureArray,
eQuarterResViewDepthTextureArrayEnd = eQuarterResViewDepthTextureArray + 16,
eQuarterResAOTextureArray,
eQuarterResAOTextureArrayEnd = eQuarterResAOTextureArray + 16,
eFullResAOZTexture,
eFullResAOZTexture2,
eCBVSRVUAVLayoutBaseMax
};
enum RTVLayoutBase
{
eFullResViewDepthTextureRTV,
eFullResViewDepthTexture2RTV,
eFullResNormalTextureRTV,
eQuarterResViewDepthTextureArrayRTV,
eQuarterResAOTextureArrayRTV = eQuarterResViewDepthTextureArrayRTV + 1 + 16,
eFullResAOZTextureRTV = eQuarterResAOTextureArrayRTV + 1 + 16,
eFullResAOZTexture2RTV,
eRTVLayoutBaseMax
};
struct GFSDK_D3D12_DescriptorHeaps
{
GFSDK_D3D12_DescriptorHeap CBVSRVUAV;
GFSDK_D3D12_DescriptorHeap RTV;
void InitDescriptorHeaps(ID3D12Device* pDevice, const GFSDK_SSAO_DescriptorHeaps_D3D12& DescriptorHeaps)
{
CBVSRVUAV.InitDescriptorHeap(pDevice, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, &DescriptorHeaps.CBV_SRV_UAV, true);
RTV.InitDescriptorHeap(pDevice, D3D12_DESCRIPTOR_HEAP_TYPE_RTV, &DescriptorHeaps.RTV, false);
}
D3D12_GPU_DESCRIPTOR_HANDLE GetGpuHandle(CBVSRVUAVLayoutBase BaseOffset, UINT Index)
{
return CBVSRVUAV.GetGPUHandle(BaseOffset + Index);
}
void Release()
{
RTV.Release();
CBVSRVUAV.Release();
}
};
inline void GFSDK_D3D12_SetResourceBarrier(ID3D12GraphicsCommandList* commandList,
ID3D12Resource* res,
D3D12_RESOURCE_STATES before,
D3D12_RESOURCE_STATES after)
{
D3D12_RESOURCE_BARRIER desc = {};
desc.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
desc.Transition.pResource = res;
desc.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
desc.Transition.StateBefore = before;
desc.Transition.StateAfter = after;
desc.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
commandList->ResourceBarrier(1, &desc);
}
struct GFSDK_D3D12_GraphicsContext
{
ID3D12Device* pDevice;
GFSDK_D3D12_DescriptorHeaps DescHeaps;
ID3D12GraphicsCommandList* pCmdList;
ID3D12CommandQueue* pCmdQueue;
ID3D12Fence* pFence;
HANDLE hFenceEvent = 0;
UINT64 FenceValue;
UINT NodeMask;
void Init(ID3D12Device* _pDevice, const GFSDK_SSAO_DescriptorHeaps_D3D12& DescriptorHeaps, UINT _NodeMask)
{
ASSERT(pDevice == NULL);
ASSERT(pCmdList == NULL);
ASSERT(pCmdQueue == NULL);
pDevice = _pDevice;
pCmdList = NULL;
pCmdQueue = NULL;
NodeMask = _NodeMask;
DescHeaps.InitDescriptorHeaps(pDevice, DescriptorHeaps);
THROW_IF_FAILED(pDevice->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&pFence)));
#if _DEBUG
pFence->SetName(L"SSAOFence");
#endif
hFenceEvent = CreateEventEx(nullptr, FALSE, FALSE, EVENT_ALL_ACCESS);
FenceValue = 0;
}
// This should be called when the library ends
void Release()
{
CloseHandle(hFenceEvent);
SAFE_RELEASE(pFence);
DescHeaps.Release();
}
void IncrFenceValue()
{
FenceValue++;
}
void WaitGPUIdle()
{
// Schedule a Signal command in the queue.
THROW_IF_FAILED(pCmdQueue->Signal(pFence, FenceValue));
THROW_IF_FAILED(pFence->SetEventOnCompletion(FenceValue, hFenceEvent));
WaitForSingleObjectEx(hFenceEvent, INFINITE, FALSE);
}
void AddResourceBarrier(ID3D12Resource* pResource, D3D12_RESOURCE_STATES OldState, D3D12_RESOURCE_STATES NewState)
{
if (OldState = NewState)
{
return;
}
D3D12_RESOURCE_BARRIER BarrierDesc = {};
BarrierDesc.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
BarrierDesc.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
BarrierDesc.Transition.pResource = pResource;
BarrierDesc.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
BarrierDesc.Transition.StateBefore = OldState;
BarrierDesc.Transition.StateAfter = NewState;
pCmdList->ResourceBarrier(1, &BarrierDesc);
}
};
struct GFSDK_D3D12_RootParameter
{
D3D12_ROOT_PARAMETER RootParam;
};
#endif // SUPPORT_D3D12
|