Skip to content

Commit 8869c95

Browse files
committed
Add texture support to NPREffect
1 parent 942c6b9 commit 8869c95

5 files changed

Lines changed: 368 additions & 32 deletions

File tree

‎Inc/Effects.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ namespace DirectX
225225
// Vertex color setting.
226226
DIRECTX_TOOLKIT_API void __cdecl SetVertexColorEnabled(bool value);
227227

228-
// Texture setting.
228+
// Texture settings.
229229
DIRECTX_TOOLKIT_API void __cdecl SetTextureEnabled(bool value);
230230
DIRECTX_TOOLKIT_API void __cdecl SetTexture(_In_opt_ ID3D11ShaderResourceView* value);
231231

@@ -927,7 +927,8 @@ namespace DirectX
927927
static constexpr int MaxDirectionalLights = 1;
928928

929929
// Texture settings.
930-
// TODO: Implement texture settings.
930+
DIRECTX_TOOLKIT_API void __cdecl SetTextureEnabled(bool value);
931+
DIRECTX_TOOLKIT_API void __cdecl SetTexture(_In_opt_ ID3D11ShaderResourceView* value);
931932

932933
// Shader mode setting.
933934
DIRECTX_TOOLKIT_API void __cdecl SetMode(Mode mode);

‎Src/NPREffect.cpp‎

Lines changed: 127 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ namespace
3636
{
3737
using ConstantBufferType = NPREffectConstants;
3838

39-
static constexpr int VertexShaderCount = 8;
40-
static constexpr int PixelShaderCount = 2;
41-
static constexpr int ShaderPermutationCount = 16;
39+
static constexpr int VertexShaderCount = 16;
40+
static constexpr int PixelShaderCount = 4;
41+
static constexpr int ShaderPermutationCount = 32;
4242

4343
static constexpr int ModeCount = 2;
4444
};
@@ -67,7 +67,9 @@ class NPREffect::Impl : public EffectBase<NPREffectTraits>
6767
bool vertexColorEnabled;
6868
bool biasedVertexNormals;
6969
bool instancing;
70+
bool textureEnabled;
7071
NPREffect::Mode nprMode;
72+
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> texture;
7173

7274
int GetCurrentShaderPermutation() const noexcept;
7375

@@ -92,8 +94,22 @@ namespace
9294
#include "XboxOneNPREffect_VSNPREffectVcBn.inc"
9395
#include "XboxOneNPREffect_VSNPREffectVcBnInst.inc"
9496

97+
#include "XboxOneNPREffect_VSNPREffectTx.inc"
98+
#include "XboxOneNPREffect_VSNPREffectInstTx.inc"
99+
100+
#include "XboxOneNPREffect_VSNPREffectVcTx.inc"
101+
#include "XboxOneNPREffect_VSNPREffectVcInstTx.inc"
102+
103+
#include "XboxOneNPREffect_VSNPREffectBnTx.inc"
104+
#include "XboxOneNPREffect_VSNPREffectBnInstTx.inc"
105+
106+
#include "XboxOneNPREffect_VSNPREffectVcBnTx.inc"
107+
#include "XboxOneNPREffect_VSNPREffectVcBnInstTx.inc"
108+
95109
#include "XboxOneNPREffect_PSCelShading.inc"
110+
#include "XboxOneNPREffect_PSCelShadingTx.inc"
96111
#include "XboxOneNPREffect_PSGoochShading.inc"
112+
#include "XboxOneNPREffect_PSGoochShadingTx.inc"
97113
#else
98114
#include "NPREffect_VSNPREffect.inc"
99115
#include "NPREffect_VSNPREffectInst.inc"
@@ -107,23 +123,46 @@ namespace
107123
#include "NPREffect_VSNPREffectVcBn.inc"
108124
#include "NPREffect_VSNPREffectVcBnInst.inc"
109125

126+
#include "NPREffect_VSNPREffectTx.inc"
127+
#include "NPREffect_VSNPREffectInstTx.inc"
128+
129+
#include "NPREffect_VSNPREffectVcTx.inc"
130+
#include "NPREffect_VSNPREffectVcInstTx.inc"
131+
132+
#include "NPREffect_VSNPREffectBnTx.inc"
133+
#include "NPREffect_VSNPREffectBnInstTx.inc"
134+
135+
#include "NPREffect_VSNPREffectVcBnTx.inc"
136+
#include "NPREffect_VSNPREffectVcBnInstTx.inc"
137+
110138
#include "NPREffect_PSCelShading.inc"
139+
#include "NPREffect_PSCelShadingTx.inc"
140+
111141
#include "NPREffect_PSGoochShading.inc"
142+
#include "NPREffect_PSGoochShadingTx.inc"
112143
#endif
113144
}
114145

115146

116147
template<>
117148
const ShaderBytecode EffectBase<NPREffectTraits>::VertexShaderBytecode[] =
118149
{
119-
{ NPREffect_VSNPREffect, sizeof(NPREffect_VSNPREffect) },
120-
{ NPREffect_VSNPREffectVc, sizeof(NPREffect_VSNPREffectVc) },
121-
{ NPREffect_VSNPREffectBn, sizeof(NPREffect_VSNPREffectBn) },
122-
{ NPREffect_VSNPREffectVcBn, sizeof(NPREffect_VSNPREffectVcBn) },
123-
{ NPREffect_VSNPREffectInst, sizeof(NPREffect_VSNPREffectInst) },
124-
{ NPREffect_VSNPREffectVcInst, sizeof(NPREffect_VSNPREffectVcInst) },
125-
{ NPREffect_VSNPREffectBnInst, sizeof(NPREffect_VSNPREffectBnInst) },
126-
{ NPREffect_VSNPREffectVcBnInst, sizeof(NPREffect_VSNPREffectVcBnInst) },
150+
{ NPREffect_VSNPREffect, sizeof(NPREffect_VSNPREffect) },
151+
{ NPREffect_VSNPREffectVc, sizeof(NPREffect_VSNPREffectVc) },
152+
{ NPREffect_VSNPREffectBn, sizeof(NPREffect_VSNPREffectBn) },
153+
{ NPREffect_VSNPREffectVcBn, sizeof(NPREffect_VSNPREffectVcBn) },
154+
{ NPREffect_VSNPREffectInst, sizeof(NPREffect_VSNPREffectInst) },
155+
{ NPREffect_VSNPREffectVcInst, sizeof(NPREffect_VSNPREffectVcInst) },
156+
{ NPREffect_VSNPREffectBnInst, sizeof(NPREffect_VSNPREffectBnInst) },
157+
{ NPREffect_VSNPREffectVcBnInst, sizeof(NPREffect_VSNPREffectVcBnInst) },
158+
{ NPREffect_VSNPREffectTx, sizeof(NPREffect_VSNPREffectTx) },
159+
{ NPREffect_VSNPREffectVcTx, sizeof(NPREffect_VSNPREffectVcTx) },
160+
{ NPREffect_VSNPREffectBnTx, sizeof(NPREffect_VSNPREffectBnTx) },
161+
{ NPREffect_VSNPREffectVcBnTx, sizeof(NPREffect_VSNPREffectVcBnTx) },
162+
{ NPREffect_VSNPREffectInstTx, sizeof(NPREffect_VSNPREffectInstTx) },
163+
{ NPREffect_VSNPREffectVcInstTx, sizeof(NPREffect_VSNPREffectVcInstTx) },
164+
{ NPREffect_VSNPREffectBnInstTx, sizeof(NPREffect_VSNPREffectBnInstTx) },
165+
{ NPREffect_VSNPREffectVcBnInstTx, sizeof(NPREffect_VSNPREffectVcBnInstTx) },
127166
};
128167

129168

@@ -153,14 +192,40 @@ const int EffectBase<NPREffectTraits>::VertexShaderIndices[] =
153192

154193
7, // instancing + vertex color (biased vertex normal) + cel shading
155194
7, // instancing + vertex color (biased vertex normal) + gooch shading
195+
196+
8, // cel shading + texture
197+
8, // gooch shading + texture
198+
199+
9, // vertex color + cel shading + texture
200+
9, // vertex color + gooch shading + texture
201+
202+
10, // cel shading (biased vertex normal) + texture
203+
10, // gooch shading (biased vertex normal) + texture
204+
205+
11, // vertex color (biased vertex normal) + cel shading + texture
206+
11, // vertex color (biased vertex normal) + gooch shading + texture
207+
208+
12, // instancing + cel shading + texture
209+
12, // instancing + gooch shading + texture
210+
211+
13, // instancing + vertex color + cel shading + texture
212+
13, // instancing + vertex color + gooch shading + texture
213+
214+
14, // instancing (biased vertex normal) + cel shading + texture
215+
14, // instancing (biased vertex normal) + gooch shading + texture
216+
217+
15, // instancing + vertex color (biased vertex normal) + cel shading + texture
218+
15, // instancing + vertex color (biased vertex normal) + gooch shading + texture
156219
};
157220

158221

159222
template<>
160223
const ShaderBytecode EffectBase<NPREffectTraits>::PixelShaderBytecode[] =
161224
{
162-
{ NPREffect_PSCelShading, sizeof(NPREffect_PSCelShading) },
163-
{ NPREffect_PSGoochShading, sizeof(NPREffect_PSGoochShading) },
225+
{ NPREffect_PSCelShading, sizeof(NPREffect_PSCelShading) },
226+
{ NPREffect_PSGoochShading, sizeof(NPREffect_PSGoochShading) },
227+
{ NPREffect_PSCelShadingTx, sizeof(NPREffect_PSCelShadingTx) },
228+
{ NPREffect_PSGoochShadingTx, sizeof(NPREffect_PSGoochShadingTx) },
164229
};
165230

166231

@@ -190,6 +255,30 @@ const int EffectBase<NPREffectTraits>::PixelShaderIndices[] =
190255

191256
0, // instancing + vertex color (biased vertex normal) + cel shading
192257
1, // instancing + vertex color (biased vertex normal) + gooch shading
258+
259+
2, // cel shading + texture
260+
3, // gooch shading + texture
261+
262+
2, // vertex color + cel shading + texture
263+
3, // vertex color + gooch shading + texture
264+
265+
2, // cel shading (biased vertex normal) + texture
266+
3, // gooch shading (biased vertex normal) + texture
267+
268+
2, // vertex color (biased vertex normal) + cel shading + texture
269+
3, // vertex color (biased vertex normal) + gooch shading + texture
270+
271+
2, // instancing + cel shading + texture
272+
3, // instancing + gooch shading + texture
273+
274+
2, // instancing + vertex color + cel shading + texture
275+
3, // instancing + vertex color + gooch shading + texture
276+
277+
2, // instancing (biased vertex normal) + cel shading + texture
278+
3, // instancing (biased vertex normal) + gooch shading + texture
279+
280+
2, // instancing + vertex color (biased vertex normal) + cel shading + texture
281+
3, // instancing + vertex color (biased vertex normal) + gooch shading + texture
193282
};
194283
#pragma endregion
195284

@@ -204,6 +293,7 @@ NPREffect::Impl::Impl(_In_ ID3D11Device* device)
204293
vertexColorEnabled(false),
205294
biasedVertexNormals(false),
206295
instancing(false),
296+
textureEnabled(false),
207297
nprMode(NPREffect::Mode_Cel)
208298
{
209299
static_assert(static_cast<int>(std::size(EffectBase<NPREffectTraits>::VertexShaderIndices)) == NPREffectTraits::ShaderPermutationCount, "array/max mismatch");
@@ -242,6 +332,12 @@ int NPREffect::Impl::GetCurrentShaderPermutation() const noexcept
242332
permutation += 8;
243333
}
244334

335+
// Use shaders without texture coordinates?
336+
if (textureEnabled)
337+
{
338+
permutation += 16;
339+
}
340+
245341
return permutation;
246342
}
247343

@@ -281,6 +377,13 @@ void NPREffect::Impl::Apply(_In_ ID3D11DeviceContext* deviceContext)
281377

282378
// Set shaders and constant buffers.
283379
ApplyShaders(deviceContext, GetCurrentShaderPermutation());
380+
381+
// Set texture.
382+
if (textureEnabled)
383+
{
384+
ID3D11ShaderResourceView* srv = texture.Get();
385+
deviceContext->PSSetShaderResources(0, 1, &srv);
386+
}
284387
}
285388

286389

@@ -425,6 +528,7 @@ void NPREffect::SetSpecularColor(FXMVECTOR value)
425528

426529
void NPREffect::SetSpecularPower(float value)
427530
{
531+
// TODO: specular power needs to be specular threshold!
428532
// Set w of specularColorAndSpecularPower.
429533
pImpl->constants.specularColorAndSpecularPower = XMVectorSetW(pImpl->constants.specularColorAndSpecularPower, value);
430534

@@ -461,7 +565,16 @@ void NPREffect::SetColorAndAlpha(FXMVECTOR value)
461565

462566

463567
// Texture settings.
464-
// TODO: Implement texture settings.
568+
void NPREffect::SetTextureEnabled(bool value)
569+
{
570+
pImpl->textureEnabled = value;
571+
}
572+
573+
574+
void NPREffect::SetTexture(_In_opt_ ID3D11ShaderResourceView* value)
575+
{
576+
pImpl->texture = value;
577+
}
465578

466579

467580
// Shader mode setting.

‎Src/Shaders/CompileShaders.cmd‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,21 @@ call :CompileShaderSM4%1 NPREffect vs VSNPREffectBnInst
236236
call :CompileShaderSM4%1 NPREffect vs VSNPREffectVcInst
237237
call :CompileShaderSM4%1 NPREffect vs VSNPREffectVcBnInst
238238

239+
call :CompileShaderSM4%1 NPREffect vs VSNPREffectTx
240+
call :CompileShaderSM4%1 NPREffect vs VSNPREffectBnTx
241+
call :CompileShaderSM4%1 NPREffect vs VSNPREffectVcTx
242+
call :CompileShaderSM4%1 NPREffect vs VSNPREffectVcBnTx
243+
244+
call :CompileShaderSM4%1 NPREffect vs VSNPREffectInstTx
245+
call :CompileShaderSM4%1 NPREffect vs VSNPREffectBnInstTx
246+
call :CompileShaderSM4%1 NPREffect vs VSNPREffectVcInstTx
247+
call :CompileShaderSM4%1 NPREffect vs VSNPREffectVcBnInstTx
248+
239249
call :CompileShaderSM4%1 NPREffect ps PSCelShading
250+
call :CompileShaderSM4%1 NPREffect ps PSCelShadingTx
251+
240252
call :CompileShaderSM4%1 NPREffect ps PSGoochShading
253+
call :CompileShaderSM4%1 NPREffect ps PSGoochShadingTx
241254

242255
call :CompileShader%1 SpriteEffect vs SpriteVertexShader
243256
call :CompileShader%1 SpriteEffect ps SpritePixelShader

0 commit comments

Comments
 (0)