Skip to content

Commit

Permalink
DebugEffect, NormalMapEffect, PBREffect updated with instancing suppo…
Browse files Browse the repository at this point in the history
…rt (#99)
  • Loading branch information
walbourn authored Aug 2, 2021
1 parent 8a62fe1 commit 411a4d6
Show file tree
Hide file tree
Showing 19 changed files with 640 additions and 102 deletions.
5 changes: 5 additions & 0 deletions Inc/Effects.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ namespace DirectX
constexpr uint32_t PerPixelLighting = 0x04 | Lighting; // per pixel lighting implies lighting enabled
constexpr uint32_t VertexColor = 0x08;
constexpr uint32_t Texture = 0x10;
constexpr uint32_t Instancing = 0x20;

constexpr uint32_t Specular = 0x100; // enable optional specular/specularMap feature
constexpr uint32_t Emissive = 0x200; // enable optional emissive/emissiveMap feature
Expand Down Expand Up @@ -799,6 +800,8 @@ namespace DirectX

void __cdecl EnableFogging(bool enabled) noexcept;

void __cdecl EnableInstancing(bool enabled) noexcept;

private:
// Private implementation.
class Impl;
Expand Down Expand Up @@ -838,6 +841,8 @@ namespace DirectX

void __cdecl SetSharing(bool enabled) noexcept;

void __cdecl EnableInstancing(bool enabled) noexcept;

private:
// Private implementation.
class Impl;
Expand Down
2 changes: 2 additions & 0 deletions Inc/GeometricPrimitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ namespace DirectX
// Draw the primitive.
void __cdecl Draw(_In_ ID3D12GraphicsCommandList* commandList) const;

void __cdecl DrawInstanced(_In_ ID3D12GraphicsCommandList* commandList, uint32_t instanceCount, uint32_t startInstanceLocation = 0) const;

private:
GeometricPrimitive() noexcept(false);

Expand Down
16 changes: 10 additions & 6 deletions Src/AlphaTestEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class AlphaTestEffect::Impl : public EffectBase<AlphaTestEffectTraits>
D3D12_GPU_DESCRIPTOR_HANDLE texture;
D3D12_GPU_DESCRIPTOR_HANDLE textureSampler;

int GetPipelineStatePermutation(bool vertexColorEnabled) const noexcept;
int GetPipelineStatePermutation(uint32_t effectFlags) const noexcept;

void Apply(_In_ ID3D12GraphicsCommandList* commandList);
};
Expand Down Expand Up @@ -219,13 +219,17 @@ AlphaTestEffect::Impl::Impl(
}
else if (effectFlags & EffectFlags::Lighting)
{
DebugTrace("ERROR: DualTextureEffect does not implement EffectFlags::Lighting\n");
DebugTrace("ERROR: AlphaTestEffect does not implement EffectFlags::Lighting\n");
throw std::invalid_argument("Lighting effect flag is invalid");
}
else if (effectFlags & EffectFlags::Instancing)
{
DebugTrace("ERROR: AlphaTestEffect does not implement EffectFlags::Instancing\n");
throw std::invalid_argument("Instancing effect flag is invalid");
}

// Create pipeline state.
int sp = GetPipelineStatePermutation(
(effectFlags & EffectFlags::VertexColor) != 0);
int sp = GetPipelineStatePermutation(effectFlags);
assert(sp >= 0 && sp < AlphaTestEffectTraits::ShaderPermutationCount);
_Analysis_assume_(sp >= 0 && sp < AlphaTestEffectTraits::ShaderPermutationCount);

Expand All @@ -247,7 +251,7 @@ AlphaTestEffect::Impl::Impl(
}


int AlphaTestEffect::Impl::GetPipelineStatePermutation(bool vertexColorEnabled) const noexcept
int AlphaTestEffect::Impl::GetPipelineStatePermutation(uint32_t effectFlags) const noexcept
{
int permutation = 0;

Expand All @@ -258,7 +262,7 @@ int AlphaTestEffect::Impl::GetPipelineStatePermutation(bool vertexColorEnabled)
}

// Support vertex coloring?
if (vertexColorEnabled)
if (effectFlags & EffectFlags::VertexColor)
{
permutation += 2;
}
Expand Down
21 changes: 12 additions & 9 deletions Src/BasicEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class BasicEffect::Impl : public EffectBase<BasicEffectTraits>

EffectLights lights;

int GetPipelineStatePermutation(bool preferPerPixelLighting, bool vertexColorEnabled, bool biasedVertexNormals) const noexcept;
int GetPipelineStatePermutation(uint32_t effectFlags) const noexcept;

void Apply(_In_ ID3D12GraphicsCommandList* commandList);
};
Expand Down Expand Up @@ -423,6 +423,12 @@ BasicEffect::Impl::Impl(
static_assert(static_cast<int>(std::size(EffectBase<BasicEffectTraits>::PixelShaderBytecode)) == BasicEffectTraits::PixelShaderCount, "array/max mismatch");
static_assert(static_cast<int>(std::size(EffectBase<BasicEffectTraits>::PixelShaderIndices)) == BasicEffectTraits::ShaderPermutationCount, "array/max mismatch");

if (effectFlags & EffectFlags::Instancing)
{
DebugTrace("ERROR: BasicEffect does not implement EffectFlags::Instancing\n");
throw std::invalid_argument("Instancing effect flag is invalid");
}

lights.InitializeConstants(constants.specularColorAndPower, constants.lightDirection, constants.lightDiffuseColor, constants.lightSpecularColor);

fog.enabled = (effectFlags & EffectFlags::Fog) != 0;
Expand Down Expand Up @@ -470,10 +476,7 @@ BasicEffect::Impl::Impl(
assert(mRootSignature != nullptr);

// Create pipeline state.
int sp = GetPipelineStatePermutation(
(effectFlags & EffectFlags::PerPixelLightingBit) != 0,
(effectFlags & EffectFlags::VertexColor) != 0,
(effectFlags & EffectFlags::BiasedVertexNormals) != 0);
int sp = GetPipelineStatePermutation(effectFlags);
assert(sp >= 0 && sp < BasicEffectTraits::ShaderPermutationCount);
_Analysis_assume_(sp >= 0 && sp < BasicEffectTraits::ShaderPermutationCount);

Expand All @@ -495,7 +498,7 @@ BasicEffect::Impl::Impl(
}


int BasicEffect::Impl::GetPipelineStatePermutation(bool preferPerPixelLighting, bool vertexColorEnabled, bool biasedVertexNormals) const noexcept
int BasicEffect::Impl::GetPipelineStatePermutation(uint32_t effectFlags) const noexcept
{
int permutation = 0;

Expand All @@ -506,7 +509,7 @@ int BasicEffect::Impl::GetPipelineStatePermutation(bool preferPerPixelLighting,
}

// Support vertex coloring?
if (vertexColorEnabled)
if (effectFlags & EffectFlags::VertexColor)
{
permutation += 2;
}
Expand All @@ -519,7 +522,7 @@ int BasicEffect::Impl::GetPipelineStatePermutation(bool preferPerPixelLighting,

if (lightingEnabled)
{
if (preferPerPixelLighting)
if (effectFlags & EffectFlags::PerPixelLightingBit)
{
// Do lighting in the pixel shader.
permutation += 16;
Expand All @@ -529,7 +532,7 @@ int BasicEffect::Impl::GetPipelineStatePermutation(bool preferPerPixelLighting,
permutation += 8;
}

if (biasedVertexNormals)
if (effectFlags & EffectFlags::BiasedVertexNormals)
{
// Compressed normals need to be scaled and biased in the vertex shader.
permutation += 16;
Expand Down
111 changes: 90 additions & 21 deletions Src/DebugEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ namespace
{
using ConstantBufferType = DebugEffectConstants;

static constexpr int VertexShaderCount = 4;
static constexpr int VertexShaderCount = 8;
static constexpr int PixelShaderCount = 4;
static constexpr int ShaderPermutationCount = 16;
static constexpr int ShaderPermutationCount = 32;
static constexpr int RootSignatureCount = 1;
};
}
Expand All @@ -53,7 +53,7 @@ class DebugEffect::Impl : public EffectBase<DebugEffectTraits>
RootParameterCount
};

int GetPipelineStatePermutation(bool vertexColorEnabled, DebugEffect::Mode debugMode, bool biasedVertexNormals) const noexcept;
int GetPipelineStatePermutation(DebugEffect::Mode debugMode, uint32_t effectFlags) const noexcept;

void Apply(_In_ ID3D12GraphicsCommandList* commandList);
};
Expand All @@ -64,43 +64,67 @@ namespace
{
#ifdef _GAMING_XBOX_SCARLETT
#include "Shaders/Compiled/XboxGamingScarlettDebugEffect_VSDebug.inc"
#include "Shaders/Compiled/XboxGamingScarlettDebugEffect_VSDebugInst.inc"

#include "Shaders/Compiled/XboxGamingScarlettDebugEffect_VSDebugVc.inc"
#include "Shaders/Compiled/XboxGamingScarlettDebugEffect_VSDebugVcInst.inc"

#include "Shaders/Compiled/XboxGamingScarlettDebugEffect_VSDebugBn.inc"
#include "Shaders/Compiled/XboxGamingScarlettDebugEffect_VSDebugBnInst.inc"

#include "Shaders/Compiled/XboxGamingScarlettDebugEffect_VSDebugVcBn.inc"
#include "Shaders/Compiled/XboxGamingScarlettDebugEffect_VSDebugVcBnInst.inc"

#include "Shaders/Compiled/XboxGamingScarlettDebugEffect_PSHemiAmbient.inc"
#include "Shaders/Compiled/XboxGamingScarlettDebugEffect_PSRGBNormals.inc"
#include "Shaders/Compiled/XboxGamingScarlettDebugEffect_PSRGBTangents.inc"
#include "Shaders/Compiled/XboxGamingScarlettDebugEffect_PSRGBBiTangents.inc"
#elif defined(_GAMING_XBOX)
#include "Shaders/Compiled/XboxGamingXboxOneDebugEffect_VSDebug.inc"
#include "Shaders/Compiled/XboxGamingXboxOneDebugEffect_VSDebugInst.inc"

#include "Shaders/Compiled/XboxGamingXboxOneDebugEffect_VSDebugVc.inc"
#include "Shaders/Compiled/XboxGamingXboxOneDebugEffect_VSDebugVcInst.inc"

#include "Shaders/Compiled/XboxGamingXboxOneDebugEffect_VSDebugBn.inc"
#include "Shaders/Compiled/XboxGamingXboxOneDebugEffect_VSDebugBnInst.inc"

#include "Shaders/Compiled/XboxGamingXboxOneDebugEffect_VSDebugVcBn.inc"
#include "Shaders/Compiled/XboxGamingXboxOneDebugEffect_VSDebugVcBnInst.inc"

#include "Shaders/Compiled/XboxGamingXboxOneDebugEffect_PSHemiAmbient.inc"
#include "Shaders/Compiled/XboxGamingXboxOneDebugEffect_PSRGBNormals.inc"
#include "Shaders/Compiled/XboxGamingXboxOneDebugEffect_PSRGBTangents.inc"
#include "Shaders/Compiled/XboxGamingXboxOneDebugEffect_PSRGBBiTangents.inc"
#elif defined(_XBOX_ONE) && defined(_TITLE)
#include "Shaders/Compiled/XboxOneDebugEffect_VSDebug.inc"
#include "Shaders/Compiled/XboxOneDebugEffect_VSDebugInst.inc"

#include "Shaders/Compiled/XboxOneDebugEffect_VSDebugVc.inc"
#include "Shaders/Compiled/XboxOneDebugEffect_VSDebugVcInst.inc"

#include "Shaders/Compiled/XboxOneDebugEffect_VSDebugBn.inc"
#include "Shaders/Compiled/XboxOneDebugEffect_VSDebugBnInst.inc"

#include "Shaders/Compiled/XboxOneDebugEffect_VSDebugVcBn.inc"
#include "Shaders/Compiled/XboxOneDebugEffect_VSDebugVcBnInst.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_VSDebugInst.inc"

#include "Shaders/Compiled/DebugEffect_VSDebugVc.inc"
#include "Shaders/Compiled/DebugEffect_VSDebugVcInst.inc"

#include "Shaders/Compiled/DebugEffect_VSDebugBn.inc"
#include "Shaders/Compiled/DebugEffect_VSDebugBnInst.inc"

#include "Shaders/Compiled/DebugEffect_VSDebugVcBn.inc"
#include "Shaders/Compiled/DebugEffect_VSDebugVcBnInst.inc"

#include "Shaders/Compiled/DebugEffect_PSHemiAmbient.inc"
#include "Shaders/Compiled/DebugEffect_PSRGBNormals.inc"
Expand All @@ -113,17 +137,20 @@ namespace
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) },
{ DebugEffect_VSDebug, sizeof(DebugEffect_VSDebug) },
{ DebugEffect_VSDebugVc, sizeof(DebugEffect_VSDebugVc) },
{ DebugEffect_VSDebugBn, sizeof(DebugEffect_VSDebugBn) },
{ DebugEffect_VSDebugVcBn, sizeof(DebugEffect_VSDebugVcBn) },
{ DebugEffect_VSDebugInst, sizeof(DebugEffect_VSDebugInst) },
{ DebugEffect_VSDebugVcInst, sizeof(DebugEffect_VSDebugVcInst) },
{ DebugEffect_VSDebugBnInst, sizeof(DebugEffect_VSDebugBnInst) },
{ DebugEffect_VSDebugVcBnInst, sizeof(DebugEffect_VSDebugVcBnInst) },
};


template<>
const int EffectBase<DebugEffectTraits>::VertexShaderIndices[] =
{
{
0, // default
0, // normals
0, // tangents
Expand All @@ -143,22 +170,41 @@ const int EffectBase<DebugEffectTraits>::VertexShaderIndices[] =
3, // vertex color (biased vertex normal) + normals
3, // vertex color (biased vertex normal) + tangents
3, // vertex color (biased vertex normal) + bitangents
};

4, // instancing
4, // instancing + normals
4, // instancing + tangents
4, // instancing + bitangents

5, // instancing + vertex color + default
5, // instancing + vertex color + normals
5, // instancing + vertex color + tangents
5, // instancing + vertex color + bitangents

6, // instancing (biased vertex normal)
6, // instancing + normals (biased vertex normal)
6, // instancing + tangents (biased vertex normal)
6, // instancing + bitangents (biased vertex normal)

7, // instancing + vertex color (biased vertex normal)
7, // instancing + vertex color (biased vertex normal) + normals
7, // instancing + vertex color (biased vertex normal) + tangents
7, // instancing + 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_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
Expand All @@ -178,6 +224,26 @@ const int EffectBase<DebugEffectTraits>::PixelShaderIndices[] =
1, // vertex color (biased vertex normal) + normals
2, // vertex color (biased vertex normal) + tangents
3, // vertex color (biased vertex normal) + bitangents

0, // instancing
1, // instancing + normals
2, // instancing + tangents
3, // instancing + bitangents

0, // instancing + vertex color + default
1, // instancing + vertex color + normals
2, // instancing + vertex color + tangents
3, // instancing + vertex color + bitangents

0, // instancing (biased vertex normal)
1, // instancing + normals (biased vertex normal)
2, // instancing + tangents (biased vertex normal)
3, // instancing + bitangents (biased vertex normal)

0, // instancing + vertex color (biased vertex normal)
1, // instancing + vertex color (biased vertex normal) + normals
2, // instancing + vertex color (biased vertex normal) + tangents
3, // instancing + vertex color (biased vertex normal) + bitangents
};


Expand Down Expand Up @@ -227,10 +293,7 @@ DebugEffect::Impl::Impl(
assert(mRootSignature != nullptr);

// Create pipeline state.
int sp = GetPipelineStatePermutation(
(effectFlags & EffectFlags::VertexColor) != 0,
debugMode,
(effectFlags & EffectFlags::BiasedVertexNormals) != 0);
int sp = GetPipelineStatePermutation(debugMode, effectFlags);
assert(sp >= 0 && sp < DebugEffectTraits::ShaderPermutationCount);
_Analysis_assume_(sp >= 0 && sp < DebugEffectTraits::ShaderPermutationCount);

Expand All @@ -252,22 +315,28 @@ DebugEffect::Impl::Impl(
}


int DebugEffect::Impl::GetPipelineStatePermutation(bool vertexColorEnabled, DebugEffect::Mode debugMode, bool biasedVertexNormals) const noexcept
int DebugEffect::Impl::GetPipelineStatePermutation(DebugEffect::Mode debugMode, uint32_t effectFlags) const noexcept
{
int permutation = static_cast<int>(debugMode);

// Support vertex coloring?
if (vertexColorEnabled)
if (effectFlags & EffectFlags::VertexColor)
{
permutation += 4;
}

if (biasedVertexNormals)
if (effectFlags & EffectFlags::BiasedVertexNormals)
{
// Compressed normals need to be scaled and biased in the vertex shader.
permutation += 8;
}

if (effectFlags & EffectFlags::Instancing)
{
// Vertex shader needs to use vertex matrix transform.
permutation += 16;
}

return permutation;
}

Expand Down
Loading

0 comments on commit 411a4d6

Please sign in to comment.