File: d3d12_helpers.h

package info (click to toggle)
renderdoc 1.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 79,584 kB
  • sloc: cpp: 491,671; ansic: 285,823; python: 12,617; java: 11,345; cs: 7,181; makefile: 6,703; yacc: 5,682; ruby: 4,648; perl: 3,461; php: 2,119; sh: 2,068; lisp: 1,835; tcl: 1,068; ml: 747; xml: 137
file content (263 lines) | stat: -rw-r--r-- 8,205 bytes parent folder | download
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
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2018 Baldur Karlsson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/

#pragma once

#include <comdef.h>
#include "dx/d3d_helpers.h"
#include "dx/official/d3d12.h"
#include "dx/official/dxgi1_4.h"

#define COM_SMARTPTR(classname) _COM_SMARTPTR_TYPEDEF(classname, __uuidof(classname))

COM_SMARTPTR(ID3DBlob);
COM_SMARTPTR(IDXGISwapChain);
COM_SMARTPTR(IDXGISwapChain1);
COM_SMARTPTR(IDXGIFactory4);

COM_SMARTPTR(ID3D12Debug);
COM_SMARTPTR(ID3D12Debug1);

COM_SMARTPTR(ID3D12Device);
COM_SMARTPTR(ID3D12Device1);
COM_SMARTPTR(ID3D12Device2);
COM_SMARTPTR(ID3D12Device3);

COM_SMARTPTR(ID3D12Fence);

COM_SMARTPTR(ID3D12CommandQueue);
COM_SMARTPTR(ID3D12CommandAllocator);
COM_SMARTPTR(ID3D12CommandList);
COM_SMARTPTR(ID3D12GraphicsCommandList);

COM_SMARTPTR(ID3D12RootSignature);
COM_SMARTPTR(ID3D12PipelineState);

COM_SMARTPTR(ID3D12Resource);

COM_SMARTPTR(ID3D12DescriptorHeap);

COM_SMARTPTR(ID3D12InfoQueue);

struct D3D12GraphicsTest;

class D3D12PSOCreator
{
public:
  D3D12PSOCreator(D3D12GraphicsTest *test);

  D3D12PSOCreator &VS(ID3DBlobPtr blob);
  D3D12PSOCreator &HS(ID3DBlobPtr blob);
  D3D12PSOCreator &DS(ID3DBlobPtr blob);
  D3D12PSOCreator &GS(ID3DBlobPtr blob);
  D3D12PSOCreator &PS(ID3DBlobPtr blob);
  D3D12PSOCreator &CS(ID3DBlobPtr blob);

  D3D12PSOCreator &InputLayout(const std::vector<D3D12_INPUT_ELEMENT_DESC> &elements);
  D3D12PSOCreator &InputLayout();

  D3D12PSOCreator &RootSig(ID3D12RootSignaturePtr rootSig);

  D3D12PSOCreator &RTVs(const std::vector<DXGI_FORMAT> &fmts);
  D3D12PSOCreator &DSV(DXGI_FORMAT fmt);

  operator ID3D12PipelineState *() const;
  operator ID3D12PipelineStatePtr() const
  {
    return ID3D12PipelineStatePtr((ID3D12PipelineState *)*this);
  }

  D3D12_GRAPHICS_PIPELINE_STATE_DESC GraphicsDesc = {};
  D3D12_COMPUTE_PIPELINE_STATE_DESC ComputeDesc = {};

private:
  D3D12GraphicsTest *m_Test;
};

class D3D12BufferCreator
{
public:
  D3D12BufferCreator(D3D12GraphicsTest *test);

  D3D12BufferCreator &UAV();

  D3D12BufferCreator &Upload();
  D3D12BufferCreator &Readback();

  D3D12BufferCreator &Data(const void *data);
  D3D12BufferCreator &Size(UINT size);

  template <typename T, size_t N>
  D3D12BufferCreator &Data(const T (&data)[N])
  {
    return Data(&data[0]).Size(UINT(N * sizeof(T)));
  }

  template <typename T>
  D3D12BufferCreator &Data(const std::vector<T> &data)
  {
    return Data(data.data()).Size(UINT(data.size() * sizeof(T)));
  }

  operator ID3D12Resource *() const;
  operator ID3D12ResourcePtr() const { return ID3D12ResourcePtr((ID3D12Resource *)*this); }
private:
  D3D12GraphicsTest *m_Test;

  D3D12_RESOURCE_DESC m_BufDesc;
  D3D12_HEAP_PROPERTIES m_HeapDesc;
  const void *m_Initdata = NULL;
};

class D3D12TextureCreator
{
public:
  D3D12TextureCreator(D3D12GraphicsTest *test, DXGI_FORMAT format, UINT width, UINT height,
                      UINT depth);

  D3D12TextureCreator &Mips(UINT mips);
  D3D12TextureCreator &Array(UINT size);
  D3D12TextureCreator &Multisampled(UINT count, UINT quality = 0);

  D3D12TextureCreator &UAV();
  D3D12TextureCreator &RTV();
  D3D12TextureCreator &DSV();

  D3D12TextureCreator &Upload();
  D3D12TextureCreator &Readback();

  D3D12TextureCreator &InitialState(D3D12_RESOURCE_STATES state);

  operator ID3D12Resource *() const;
  operator ID3D12ResourcePtr() const { return ID3D12ResourcePtr((ID3D12Resource *)*this); }
protected:
  D3D12GraphicsTest *m_Test;

  D3D12_RESOURCE_STATES m_InitialState;
  D3D12_RESOURCE_DESC m_TexDesc;
  D3D12_HEAP_PROPERTIES m_HeapDesc;
};

enum class ResourceType
{
  Buffer,
  Texture1D,
  Texture1DArray,
  Texture2D,
  Texture2DArray,
  Texture2DMS,
  Texture2DMSArray,
  Texture3D,
};

enum class ViewType
{
  SRV,
  RTV,
  DSV,
  UAV,
};

class D3D12ViewCreator
{
public:
  D3D12ViewCreator(D3D12GraphicsTest *test, ID3D12DescriptorHeap *heap, ViewType viewType,
                   ID3D12Resource *res);

  // common params
  D3D12ViewCreator &Format(DXGI_FORMAT format);

  // buffer params
  D3D12ViewCreator &FirstElement(UINT el);
  D3D12ViewCreator &NumElements(UINT num);
  D3D12ViewCreator &StructureStride(UINT stride);
  D3D12ViewCreator &ByteAddressed();

  // TODO counter buffer offset

  // texture params
  D3D12ViewCreator &FirstMip(UINT mip);
  D3D12ViewCreator &NumMips(UINT num);
  D3D12ViewCreator &FirstSlice(UINT mip);
  D3D12ViewCreator &NumSlices(UINT num);

  // TODO plane slice, swizzle, ResourceMinLODClamp

  // depth stencil only
  D3D12ViewCreator &ReadOnlyDepth();
  D3D12ViewCreator &ReadOnlyStencil();

  D3D12_CPU_DESCRIPTOR_HANDLE Create(ID3D12DescriptorHeap *heap, uint32_t descriptor);
  D3D12_CPU_DESCRIPTOR_HANDLE Create(uint32_t descriptor) { return Create(m_Heap, descriptor); }
private:
  void SetupDescriptors(ViewType viewType, ResourceType resType);

  D3D12GraphicsTest *m_Test;
  ID3D12Resource *m_Res;
  ID3D12DescriptorHeap *m_Heap;
  ViewType m_Type;

  // instead of a huge mess trying to auto populate the actual descriptors from saved values, as
  // they aren't very nicely compatible (e.g. RTVs have mipslice selection on 3D textures, SRVs
  // don't, SRVs support 1D texturse but DSVs don't, UAVs don't support MSAA textures, etc etc).
  // Instead we just set save pointers that might be NULL in the constructor based on view and
  // resource type
  union
  {
    D3D12_SHADER_RESOURCE_VIEW_DESC srv;
    D3D12_RENDER_TARGET_VIEW_DESC rtv;
    D3D12_DEPTH_STENCIL_VIEW_DESC dsv;
    D3D12_UNORDERED_ACCESS_VIEW_DESC uav;
  } desc;

  UINT64 *firstElement = NULL;
  UINT *numElements = NULL;
  UINT *firstMip = NULL, *numMips = NULL;
  UINT *firstSlice = NULL, *numSlices = NULL;
};

D3D12_ROOT_PARAMETER1 cbvParam(D3D12_SHADER_VISIBILITY vis, UINT space, UINT reg);

D3D12_ROOT_PARAMETER1 constParam(D3D12_SHADER_VISIBILITY vis, UINT space, UINT reg, UINT num);

D3D12_ROOT_PARAMETER1 tableParam(D3D12_SHADER_VISIBILITY vis, D3D12_DESCRIPTOR_RANGE_TYPE type,
                                 UINT space, UINT basereg, UINT numreg);

#define GET_REFCOUNT(val, obj) \
  do                           \
  {                            \
    obj->AddRef();             \
    val = obj->Release();      \
  } while(0)

#define CHECK_HR(expr)                                                                    \
  {                                                                                       \
    HRESULT hr = (expr);                                                                  \
    if(FAILED(hr))                                                                        \
    {                                                                                     \
      TEST_ERROR("Failed HRESULT at %s:%d (%x): %s", __FILE__, (int)__LINE__, hr, #expr); \
      DEBUG_BREAK();                                                                      \
      exit(1);                                                                            \
    }                                                                                     \
  }