forked from microsoft/DirectXTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlphaTestEffect.cpp
442 lines (326 loc) · 13.3 KB
/
AlphaTestEffect.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//--------------------------------------------------------------------------------------
// File: AlphaTestEffect.cpp
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// http://go.microsoft.com/fwlink/?LinkId=248929
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "EffectCommon.h"
using namespace DirectX;
// Constant buffer layout. Must match the shader!
struct AlphaTestEffectConstants
{
XMVECTOR diffuseColor;
XMVECTOR alphaTest;
XMVECTOR fogColor;
XMVECTOR fogVector;
XMMATRIX worldViewProj;
};
static_assert( ( sizeof(AlphaTestEffectConstants) % 16 ) == 0, "CB size not padded correctly" );
// Traits type describes our characteristics to the EffectBase template.
struct AlphaTestEffectTraits
{
typedef AlphaTestEffectConstants ConstantBufferType;
static const int VertexShaderCount = 4;
static const int PixelShaderCount = 4;
static const int ShaderPermutationCount = 8;
};
// Internal AlphaTestEffect implementation class.
class AlphaTestEffect::Impl : public EffectBase<AlphaTestEffectTraits>
{
public:
Impl(_In_ ID3D11Device* device);
D3D11_COMPARISON_FUNC alphaFunction;
int referenceAlpha;
bool vertexColorEnabled;
EffectColor color;
int GetCurrentShaderPermutation() const;
void Apply(_In_ ID3D11DeviceContext* deviceContext);
};
// Include the precompiled shader code.
namespace
{
#if defined(_XBOX_ONE) && defined(_TITLE)
#include "Shaders/Compiled/XboxOneAlphaTestEffect_VSAlphaTest.inc"
#include "Shaders/Compiled/XboxOneAlphaTestEffect_VSAlphaTestNoFog.inc"
#include "Shaders/Compiled/XboxOneAlphaTestEffect_VSAlphaTestVc.inc"
#include "Shaders/Compiled/XboxOneAlphaTestEffect_VSAlphaTestVcNoFog.inc"
#include "Shaders/Compiled/XboxOneAlphaTestEffect_PSAlphaTestLtGt.inc"
#include "Shaders/Compiled/XboxOneAlphaTestEffect_PSAlphaTestLtGtNoFog.inc"
#include "Shaders/Compiled/XboxOneAlphaTestEffect_PSAlphaTestEqNe.inc"
#include "Shaders/Compiled/XboxOneAlphaTestEffect_PSAlphaTestEqNeNoFog.inc"
#else
#include "Shaders/Compiled/AlphaTestEffect_VSAlphaTest.inc"
#include "Shaders/Compiled/AlphaTestEffect_VSAlphaTestNoFog.inc"
#include "Shaders/Compiled/AlphaTestEffect_VSAlphaTestVc.inc"
#include "Shaders/Compiled/AlphaTestEffect_VSAlphaTestVcNoFog.inc"
#include "Shaders/Compiled/AlphaTestEffect_PSAlphaTestLtGt.inc"
#include "Shaders/Compiled/AlphaTestEffect_PSAlphaTestLtGtNoFog.inc"
#include "Shaders/Compiled/AlphaTestEffect_PSAlphaTestEqNe.inc"
#include "Shaders/Compiled/AlphaTestEffect_PSAlphaTestEqNeNoFog.inc"
#endif
}
const ShaderBytecode EffectBase<AlphaTestEffectTraits>::VertexShaderBytecode[] =
{
{ AlphaTestEffect_VSAlphaTest, sizeof(AlphaTestEffect_VSAlphaTest) },
{ AlphaTestEffect_VSAlphaTestNoFog, sizeof(AlphaTestEffect_VSAlphaTestNoFog) },
{ AlphaTestEffect_VSAlphaTestVc, sizeof(AlphaTestEffect_VSAlphaTestVc) },
{ AlphaTestEffect_VSAlphaTestVcNoFog, sizeof(AlphaTestEffect_VSAlphaTestVcNoFog) },
};
const int EffectBase<AlphaTestEffectTraits>::VertexShaderIndices[] =
{
0, // lt/gt
1, // lt/gt, no fog
2, // lt/gt, vertex color
3, // lt/gt, vertex color, no fog
0, // eq/ne
1, // eq/ne, no fog
2, // eq/ne, vertex color
3, // eq/ne, vertex color, no fog
};
const ShaderBytecode EffectBase<AlphaTestEffectTraits>::PixelShaderBytecode[] =
{
{ AlphaTestEffect_PSAlphaTestLtGt, sizeof(AlphaTestEffect_PSAlphaTestLtGt) },
{ AlphaTestEffect_PSAlphaTestLtGtNoFog, sizeof(AlphaTestEffect_PSAlphaTestLtGtNoFog) },
{ AlphaTestEffect_PSAlphaTestEqNe, sizeof(AlphaTestEffect_PSAlphaTestEqNe) },
{ AlphaTestEffect_PSAlphaTestEqNeNoFog, sizeof(AlphaTestEffect_PSAlphaTestEqNeNoFog) },
};
const int EffectBase<AlphaTestEffectTraits>::PixelShaderIndices[] =
{
0, // lt/gt
1, // lt/gt, no fog
0, // lt/gt, vertex color
1, // lt/gt, vertex color, no fog
2, // eq/ne
3, // eq/ne, no fog
2, // eq/ne, vertex color
3, // eq/ne, vertex color, no fog
};
// Global pool of per-device AlphaTestEffect resources.
SharedResourcePool<ID3D11Device*, EffectBase<AlphaTestEffectTraits>::DeviceResources> EffectBase<AlphaTestEffectTraits>::deviceResourcesPool;
// Constructor.
AlphaTestEffect::Impl::Impl(_In_ ID3D11Device* device)
: EffectBase(device),
alphaFunction(D3D11_COMPARISON_GREATER),
referenceAlpha(0),
vertexColorEnabled(false)
{
static_assert( _countof(EffectBase<AlphaTestEffectTraits>::VertexShaderIndices) == AlphaTestEffectTraits::ShaderPermutationCount, "array/max mismatch" );
static_assert( _countof(EffectBase<AlphaTestEffectTraits>::VertexShaderBytecode) == AlphaTestEffectTraits::VertexShaderCount, "array/max mismatch" );
static_assert( _countof(EffectBase<AlphaTestEffectTraits>::PixelShaderBytecode) == AlphaTestEffectTraits::PixelShaderCount, "array/max mismatch" );
static_assert( _countof(EffectBase<AlphaTestEffectTraits>::PixelShaderIndices) == AlphaTestEffectTraits::ShaderPermutationCount, "array/max mismatch" );
}
int AlphaTestEffect::Impl::GetCurrentShaderPermutation() const
{
int permutation = 0;
// Use optimized shaders if fog is disabled.
if (!fog.enabled)
{
permutation += 1;
}
// Support vertex coloring?
if (vertexColorEnabled)
{
permutation += 2;
}
// Which alpha compare mode?
if (alphaFunction == D3D11_COMPARISON_EQUAL ||
alphaFunction == D3D11_COMPARISON_NOT_EQUAL)
{
permutation += 4;
}
return permutation;
}
// Sets our state onto the D3D device.
void AlphaTestEffect::Impl::Apply(_In_ ID3D11DeviceContext* deviceContext)
{
// Compute derived parameter values.
matrices.SetConstants(dirtyFlags, constants.worldViewProj);
fog.SetConstants(dirtyFlags, matrices.worldView, constants.fogVector);
color.SetConstants(dirtyFlags, constants.diffuseColor);
// Recompute the alpha test settings?
if (dirtyFlags & EffectDirtyFlags::AlphaTest)
{
// Convert reference alpha from 8 bit integer to 0-1 float format.
float reference = (float)referenceAlpha / 255.0f;
// Comparison tolerance of half the 8 bit integer precision.
const float threshold = 0.5f / 255.0f;
// What to do if the alpha comparison passes or fails. Positive accepts the pixel, negative clips it.
static const XMVECTORF32 selectIfTrue = { 1, -1 };
static const XMVECTORF32 selectIfFalse = { -1, 1 };
static const XMVECTORF32 selectNever = { -1, -1 };
static const XMVECTORF32 selectAlways = { 1, 1 };
float compareTo;
XMVECTOR resultSelector;
switch (alphaFunction)
{
case D3D11_COMPARISON_LESS:
// Shader will evaluate: clip((a < x) ? z : w)
compareTo = reference - threshold;
resultSelector = selectIfTrue;
break;
case D3D11_COMPARISON_LESS_EQUAL:
// Shader will evaluate: clip((a < x) ? z : w)
compareTo = reference + threshold;
resultSelector = selectIfTrue;
break;
case D3D11_COMPARISON_GREATER_EQUAL:
// Shader will evaluate: clip((a < x) ? z : w)
compareTo = reference - threshold;
resultSelector = selectIfFalse;
break;
case D3D11_COMPARISON_GREATER:
// Shader will evaluate: clip((a < x) ? z : w)
compareTo = reference + threshold;
resultSelector = selectIfFalse;
break;
case D3D11_COMPARISON_EQUAL:
// Shader will evaluate: clip((abs(a - x) < y) ? z : w)
compareTo = reference;
resultSelector = selectIfTrue;
break;
case D3D11_COMPARISON_NOT_EQUAL:
// Shader will evaluate: clip((abs(a - x) < y) ? z : w)
compareTo = reference;
resultSelector = selectIfFalse;
break;
case D3D11_COMPARISON_NEVER:
// Shader will evaluate: clip((a < x) ? z : w)
compareTo = 0;
resultSelector = selectNever;
break;
case D3D11_COMPARISON_ALWAYS:
// Shader will evaluate: clip((a < x) ? z : w)
compareTo = 0;
resultSelector = selectAlways;
break;
default:
throw std::exception("Unknown alpha test function");
}
// x = compareTo, y = threshold, zw = resultSelector.
constants.alphaTest = XMVectorPermute<0, 1, 4, 5>(XMVectorSet(compareTo, threshold, 0, 0), resultSelector);
dirtyFlags &= ~EffectDirtyFlags::AlphaTest;
dirtyFlags |= EffectDirtyFlags::ConstantBuffer;
}
// Set the texture.
ID3D11ShaderResourceView* textures[1] = { texture.Get() };
deviceContext->PSSetShaderResources(0, 1, textures);
// Set shaders and constant buffers.
ApplyShaders(deviceContext, GetCurrentShaderPermutation());
}
// Public constructor.
AlphaTestEffect::AlphaTestEffect(_In_ ID3D11Device* device)
: pImpl(new Impl(device))
{
}
// Move constructor.
AlphaTestEffect::AlphaTestEffect(AlphaTestEffect&& moveFrom)
: pImpl(std::move(moveFrom.pImpl))
{
}
// Move assignment.
AlphaTestEffect& AlphaTestEffect::operator= (AlphaTestEffect&& moveFrom)
{
pImpl = std::move(moveFrom.pImpl);
return *this;
}
// Public destructor.
AlphaTestEffect::~AlphaTestEffect()
{
}
// IEffect methods.
void AlphaTestEffect::Apply(_In_ ID3D11DeviceContext* deviceContext)
{
pImpl->Apply(deviceContext);
}
void AlphaTestEffect::GetVertexShaderBytecode(_Out_ void const** pShaderByteCode, _Out_ size_t* pByteCodeLength)
{
pImpl->GetVertexShaderBytecode(pImpl->GetCurrentShaderPermutation(), pShaderByteCode, pByteCodeLength);
}
// Camera settings.
void XM_CALLCONV AlphaTestEffect::SetWorld(FXMMATRIX value)
{
pImpl->matrices.world = value;
pImpl->dirtyFlags |= EffectDirtyFlags::WorldViewProj | EffectDirtyFlags::WorldInverseTranspose | EffectDirtyFlags::FogVector;
}
void XM_CALLCONV AlphaTestEffect::SetView(FXMMATRIX value)
{
pImpl->matrices.view = value;
pImpl->dirtyFlags |= EffectDirtyFlags::WorldViewProj | EffectDirtyFlags::EyePosition | EffectDirtyFlags::FogVector;
}
void XM_CALLCONV AlphaTestEffect::SetProjection(FXMMATRIX value)
{
pImpl->matrices.projection = value;
pImpl->dirtyFlags |= EffectDirtyFlags::WorldViewProj;
}
void XM_CALLCONV AlphaTestEffect::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 | EffectDirtyFlags::EyePosition | EffectDirtyFlags::FogVector;
}
// Material settings
void XM_CALLCONV AlphaTestEffect::SetDiffuseColor(FXMVECTOR value)
{
pImpl->color.diffuseColor = value;
pImpl->dirtyFlags |= EffectDirtyFlags::MaterialColor;
}
void AlphaTestEffect::SetAlpha(float value)
{
pImpl->color.alpha = value;
pImpl->dirtyFlags |= EffectDirtyFlags::MaterialColor;
}
void XM_CALLCONV AlphaTestEffect::SetColorAndAlpha(FXMVECTOR value)
{
pImpl->color.diffuseColor = value;
pImpl->color.alpha = XMVectorGetW(value);
pImpl->dirtyFlags |= EffectDirtyFlags::MaterialColor;
}
// Fog settings.
void AlphaTestEffect::SetFogEnabled(bool value)
{
pImpl->fog.enabled = value;
pImpl->dirtyFlags |= EffectDirtyFlags::FogEnable;
}
void AlphaTestEffect::SetFogStart(float value)
{
pImpl->fog.start = value;
pImpl->dirtyFlags |= EffectDirtyFlags::FogVector;
}
void AlphaTestEffect::SetFogEnd(float value)
{
pImpl->fog.end = value;
pImpl->dirtyFlags |= EffectDirtyFlags::FogVector;
}
void XM_CALLCONV AlphaTestEffect::SetFogColor(FXMVECTOR value)
{
pImpl->constants.fogColor = value;
pImpl->dirtyFlags |= EffectDirtyFlags::ConstantBuffer;
}
// Vertex color setting.
void AlphaTestEffect::SetVertexColorEnabled(bool value)
{
pImpl->vertexColorEnabled = value;
}
// Texture settings.
void AlphaTestEffect::SetTexture(_In_opt_ ID3D11ShaderResourceView* value)
{
pImpl->texture = value;
}
void AlphaTestEffect::SetAlphaFunction(D3D11_COMPARISON_FUNC value)
{
pImpl->alphaFunction = value;
pImpl->dirtyFlags |= EffectDirtyFlags::AlphaTest;
}
void AlphaTestEffect::SetReferenceAlpha(int value)
{
pImpl->referenceAlpha = value;
pImpl->dirtyFlags |= EffectDirtyFlags::AlphaTest;
}