forked from microsoft/DirectXTK12
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebugEffect.cpp
368 lines (274 loc) · 11.9 KB
/
DebugEffect.cpp
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//--------------------------------------------------------------------------------------
// File: DebugEffect.cpp
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
// http://go.microsoft.com/fwlink/?LinkID=615561
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "EffectCommon.h"
using namespace DirectX;
// Constant buffer layout. Must match the shader!
struct DebugEffectConstants
{
XMVECTOR ambientDownAndAlpha;
XMVECTOR ambientRange;
XMMATRIX world;
XMVECTOR worldInverseTranspose[3];
XMMATRIX worldViewProj;
};
static_assert((sizeof(DebugEffectConstants) % 16) == 0, "CB size not padded correctly");
// Traits type describes our characteristics to the EffectBase template.
struct DebugEffectTraits
{
using ConstantBufferType = DebugEffectConstants;
static const int VertexShaderCount = 4;
static const int PixelShaderCount = 4;
static const int ShaderPermutationCount = 16;
static const int RootSignatureCount = 1;
};
// Internal DebugEffect implementation class.
class DebugEffect::Impl : public EffectBase<DebugEffectTraits>
{
public:
Impl(_In_ ID3D12Device* device, int effectFlags, const EffectPipelineStateDescription& pipelineDescription, DebugEffect::Mode debugMode);
enum RootParameterIndex
{
ConstantBuffer,
RootParameterCount
};
int GetPipelineStatePermutation(bool vertexColorEnabled, DebugEffect::Mode debugMode, bool biasedVertexNormals) const;
void Apply(_In_ ID3D12GraphicsCommandList* commandList);
};
// Include the precompiled shader code.
namespace
{
#if defined(_XBOX_ONE) && defined(_TITLE)
#include "Shaders/Compiled/XboxOneDebugEffect_VSDebug.inc"
#include "Shaders/Compiled/XboxOneDebugEffect_VSDebugVc.inc"
#include "Shaders/Compiled/XboxOneDebugEffect_VSDebugBn.inc"
#include "Shaders/Compiled/XboxOneDebugEffect_VSDebugVcBn.inc"
#include "Shaders/Compiled/XboxOneDebugEffect_PSHemiAmbient.inc"
#include "Shaders/Compiled/XboxOneDebugEffect_PSRGBNormals.inc"
#include "Shaders/Compiled/XboxOneDebugEffect_PSRGBTangents.inc"
#include "Shaders/Compiled/XboxOneDebugEffect_PSRGBBiTangents.inc"
#else
#include "Shaders/Compiled/DebugEffect_VSDebug.inc"
#include "Shaders/Compiled/DebugEffect_VSDebugVc.inc"
#include "Shaders/Compiled/DebugEffect_VSDebugBn.inc"
#include "Shaders/Compiled/DebugEffect_VSDebugVcBn.inc"
#include "Shaders/Compiled/DebugEffect_PSHemiAmbient.inc"
#include "Shaders/Compiled/DebugEffect_PSRGBNormals.inc"
#include "Shaders/Compiled/DebugEffect_PSRGBTangents.inc"
#include "Shaders/Compiled/DebugEffect_PSRGBBiTangents.inc"
#endif
}
template<>
const D3D12_SHADER_BYTECODE EffectBase<DebugEffectTraits>::VertexShaderBytecode[] =
{
{ DebugEffect_VSDebug, sizeof(DebugEffect_VSDebug) },
{ DebugEffect_VSDebugVc, sizeof(DebugEffect_VSDebugVc) },
{ DebugEffect_VSDebugBn, sizeof(DebugEffect_VSDebugBn) },
{ DebugEffect_VSDebugVcBn, sizeof(DebugEffect_VSDebugVcBn) },
};
template<>
const int EffectBase<DebugEffectTraits>::VertexShaderIndices[] =
{
0, // default
0, // normals
0, // tangents
0, // bitangents
1, // vertex color + default
1, // vertex color + normals
1, // vertex color + tangents
1, // vertex color + bitangents
2, // default (biased vertex normal)
2, // normals (biased vertex normal)
2, // tangents (biased vertex normal)
2, // bitangents (biased vertex normal)
3, // vertex color (biased vertex normal)
3, // vertex color (biased vertex normal) + normals
3, // vertex color (biased vertex normal) + tangents
3, // vertex color (biased vertex normal) + bitangents
};
template<>
const D3D12_SHADER_BYTECODE EffectBase<DebugEffectTraits>::PixelShaderBytecode[] =
{
{ DebugEffect_PSHemiAmbient, sizeof(DebugEffect_PSHemiAmbient) },
{ DebugEffect_PSRGBNormals, sizeof(DebugEffect_PSRGBNormals) },
{ DebugEffect_PSRGBTangents, sizeof(DebugEffect_PSRGBTangents) },
{ DebugEffect_PSRGBBiTangents, sizeof(DebugEffect_PSRGBBiTangents) },
};
template<>
const int EffectBase<DebugEffectTraits>::PixelShaderIndices[] =
{
0, // default
1, // normals
2, // tangents
3, // bitangents
0, // vertex color + default
1, // vertex color + normals
2, // vertex color + tangents
3, // vertex color + bitangents
0, // default (biased vertex normal)
1, // normals (biased vertex normal)
2, // tangents (biased vertex normal)
3, // bitangents (biased vertex normal)
0, // vertex color (biased vertex normal)
1, // vertex color (biased vertex normal) + normals
2, // vertex color (biased vertex normal) + tangents
3, // vertex color (biased vertex normal) + bitangents
};
// Global pool of per-deviceDebugEffect resources.
template<>
SharedResourcePool<ID3D12Device*, EffectBase<DebugEffectTraits>::DeviceResources> EffectBase<DebugEffectTraits>::deviceResourcesPool = {};
// Constructor.
DebugEffect::Impl::Impl(_In_ ID3D12Device* device, int effectFlags, const EffectPipelineStateDescription& pipelineDescription, DebugEffect::Mode debugMode)
: EffectBase(device)
{
static_assert(_countof(EffectBase<DebugEffectTraits>::VertexShaderIndices) == DebugEffectTraits::ShaderPermutationCount, "array/max mismatch");
static_assert(_countof(EffectBase<DebugEffectTraits>::VertexShaderBytecode) == DebugEffectTraits::VertexShaderCount, "array/max mismatch");
static_assert(_countof(EffectBase<DebugEffectTraits>::PixelShaderBytecode) == DebugEffectTraits::PixelShaderCount, "array/max mismatch");
static_assert(_countof(EffectBase<DebugEffectTraits>::PixelShaderIndices) == DebugEffectTraits::ShaderPermutationCount, "array/max mismatch");
static const XMVECTORF32 s_lower = { { { 0.f, 0.f, 0.f, 1.f } } };
constants.ambientDownAndAlpha = s_lower;
constants.ambientRange = g_XMOne;
// Create root signature.
{
D3D12_ROOT_SIGNATURE_FLAGS rootSignatureFlags =
D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT |
D3D12_ROOT_SIGNATURE_FLAG_DENY_DOMAIN_SHADER_ROOT_ACCESS |
D3D12_ROOT_SIGNATURE_FLAG_DENY_GEOMETRY_SHADER_ROOT_ACCESS |
D3D12_ROOT_SIGNATURE_FLAG_DENY_HULL_SHADER_ROOT_ACCESS;
// Create root parameters and initialize first (constants)
CD3DX12_ROOT_PARAMETER rootParameters[RootParameterIndex::RootParameterCount] = {};
rootParameters[RootParameterIndex::ConstantBuffer].InitAsConstantBufferView(0, 0, D3D12_SHADER_VISIBILITY_ALL);
// Root parameter descriptor - conditionally initialized
CD3DX12_ROOT_SIGNATURE_DESC rsigDesc = {};
rsigDesc.Init(1, rootParameters, 0, nullptr, rootSignatureFlags);
mRootSignature = GetRootSignature(0, rsigDesc);
}
assert(mRootSignature != nullptr);
// Create pipeline state.
int sp = GetPipelineStatePermutation(
(effectFlags & EffectFlags::VertexColor) != 0,
debugMode,
(effectFlags & EffectFlags::BiasedVertexNormals) != 0);
assert(sp >= 0 && sp < DebugEffectTraits::ShaderPermutationCount);
_Analysis_assume_(sp >= 0 && sp < DebugEffectTraits::ShaderPermutationCount);
int vi = EffectBase<DebugEffectTraits>::VertexShaderIndices[sp];
assert(vi >= 0 && vi < DebugEffectTraits::VertexShaderCount);
_Analysis_assume_(vi >= 0 && vi < DebugEffectTraits::VertexShaderCount);
int pi = EffectBase<DebugEffectTraits>::PixelShaderIndices[sp];
assert(pi >= 0 && pi < DebugEffectTraits::PixelShaderCount);
_Analysis_assume_(pi >= 0 && pi < DebugEffectTraits::PixelShaderCount);
pipelineDescription.CreatePipelineState(
device,
mRootSignature,
EffectBase<DebugEffectTraits>::VertexShaderBytecode[vi],
EffectBase<DebugEffectTraits>::PixelShaderBytecode[pi],
mPipelineState.GetAddressOf());
SetDebugObjectName(mPipelineState.Get(), L"DebugEffect");
}
int DebugEffect::Impl::GetPipelineStatePermutation(bool vertexColorEnabled, DebugEffect::Mode debugMode, bool biasedVertexNormals) const
{
int permutation = static_cast<int>(debugMode);
// Support vertex coloring?
if (vertexColorEnabled)
{
permutation += 4;
}
if (biasedVertexNormals)
{
// Compressed normals need to be scaled and biased in the vertex shader.
permutation += 8;
}
return permutation;
}
// Sets our state onto the D3D device.
void DebugEffect::Impl::Apply(_In_ ID3D12GraphicsCommandList* commandList)
{
// Compute derived parameter values.
matrices.SetConstants(dirtyFlags, constants.worldViewProj);
// World inverse transpose matrix.
if (dirtyFlags & EffectDirtyFlags::WorldInverseTranspose)
{
constants.world = XMMatrixTranspose(matrices.world);
XMMATRIX worldInverse = XMMatrixInverse(nullptr, matrices.world);
constants.worldInverseTranspose[0] = worldInverse.r[0];
constants.worldInverseTranspose[1] = worldInverse.r[1];
constants.worldInverseTranspose[2] = worldInverse.r[2];
dirtyFlags &= ~EffectDirtyFlags::WorldInverseTranspose;
dirtyFlags |= EffectDirtyFlags::ConstantBuffer;
}
UpdateConstants();
// Set the root signature
commandList->SetGraphicsRootSignature(mRootSignature);
// Set constants
commandList->SetGraphicsRootConstantBufferView(RootParameterIndex::ConstantBuffer, GetConstantBufferGpuAddress());
// Set the pipeline state
commandList->SetPipelineState(EffectBase::mPipelineState.Get());
}
// Public constructor.
DebugEffect::DebugEffect(_In_ ID3D12Device* device, int effectFlags, const EffectPipelineStateDescription& pipelineDescription, Mode debugMode)
: pImpl(std::make_unique<Impl>(device, effectFlags, pipelineDescription, debugMode))
{
}
// Move constructor.
DebugEffect::DebugEffect(DebugEffect&& moveFrom) noexcept
: pImpl(std::move(moveFrom.pImpl))
{
}
// Move assignment.
DebugEffect& DebugEffect::operator= (DebugEffect&& moveFrom) noexcept
{
pImpl = std::move(moveFrom.pImpl);
return *this;
}
// Public destructor.
DebugEffect::~DebugEffect()
{
}
// IEffect methods.
void DebugEffect::Apply(_In_ ID3D12GraphicsCommandList* commandList)
{
pImpl->Apply(commandList);
}
// Camera settings.
void XM_CALLCONV DebugEffect::SetWorld(FXMMATRIX value)
{
pImpl->matrices.world = value;
pImpl->dirtyFlags |= EffectDirtyFlags::WorldViewProj | EffectDirtyFlags::WorldInverseTranspose;
}
void XM_CALLCONV DebugEffect::SetView(FXMMATRIX value)
{
pImpl->matrices.view = value;
pImpl->dirtyFlags |= EffectDirtyFlags::WorldViewProj;
}
void XM_CALLCONV DebugEffect::SetProjection(FXMMATRIX value)
{
pImpl->matrices.projection = value;
pImpl->dirtyFlags |= EffectDirtyFlags::WorldViewProj;
}
void XM_CALLCONV DebugEffect::SetMatrices(FXMMATRIX world, CXMMATRIX view, CXMMATRIX projection)
{
pImpl->matrices.world = world;
pImpl->matrices.view = view;
pImpl->matrices.projection = projection;
pImpl->dirtyFlags |= EffectDirtyFlags::WorldViewProj | EffectDirtyFlags::WorldInverseTranspose;
}
// Material settings.
void XM_CALLCONV DebugEffect::SetHemisphericalAmbientColor(FXMVECTOR upper, FXMVECTOR lower)
{
// Set xyz to new value, but preserve existing w (alpha).
pImpl->constants.ambientDownAndAlpha = XMVectorSelect(pImpl->constants.ambientDownAndAlpha, lower, g_XMSelect1110);
pImpl->constants.ambientRange = XMVectorSubtract(upper, lower);
pImpl->dirtyFlags |= EffectDirtyFlags::ConstantBuffer;
}
void DebugEffect::SetAlpha(float value)
{
// Set w to new value, but preserve existing xyz (ambient down).
pImpl->constants.ambientDownAndAlpha = XMVectorSetW(pImpl->constants.ambientDownAndAlpha, value);
pImpl->dirtyFlags |= EffectDirtyFlags::ConstantBuffer;
}