Skip to content

Commit

Permalink
PBR Renderer: reworked how texture attribute indices are defined
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Dec 14, 2023
1 parent ced980b commit d173171
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 126 deletions.
2 changes: 1 addition & 1 deletion Hydrogent/src/HnMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ void HnMaterial::InitTextureAttribs(HnTextureRegistry& TexRegistry, const USD_Re
}
};

const auto& TexAttribIndices = UsdRenderer.GetShaderTextureAttributeIndices();
const auto& TexAttribIndices = UsdRenderer.GetSettings().TextureAttribIndinces;
// clang-format off
SetTextureParams(HnTokens->diffuseColor, TexAttribIndices.BaseColor);
SetTextureParams(HnTokens->normal, TexAttribIndices.Normal);
Expand Down
28 changes: 12 additions & 16 deletions PBR/interface/PBR_Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,17 @@ class PBR_Renderer
IBuffer* pPrimitiveAttribsCB = nullptr;

/// Texture attribute index info.
struct ShaderTextureAttribIndex
struct ShaderTextureAttribIndinces
{
/// Texture attribute index name (e.g. "BaseColorTextureAttribId").
const char* Name = nullptr;

/// Texture attribute index value.
Uint32 Index = 0;
int BaseColor = -1;
int Normal = -1;
int PhysDesc = -1;
int Metallic = -1;
int Roughness = -1;
int Occlusion = -1;
int Emissive = -1;
};
/// An array of NumShaderTextureAttribIndices texture attribute index info.
const ShaderTextureAttribIndex* pShaderTextureAttribIndices = nullptr;

/// The number of texture attributes in pShaderTextureAttribIndices array.
Uint32 NumShaderTextureAttribIndices = 0;
ShaderTextureAttribIndinces TextureAttribIndinces;
};

enum ALPHA_MODE
Expand Down Expand Up @@ -410,6 +408,8 @@ class PBR_Renderer

Uint32 GetPBRPrimitiveAttribsSize() const;

const CreateInfo& GetSettings() const { return m_Settings; }

protected:
ShaderMacroHelper DefineMacros(PSO_FLAGS PSOFlags, DebugViewType DebugView) const;

Expand All @@ -432,11 +432,7 @@ class PBR_Renderer

protected:
const InputLayoutDescX m_InputLayout;

const std::vector<std::string> m_ShaderTextureAttribIndexNames;
const std::vector<CreateInfo::ShaderTextureAttribIndex> m_ShaderTextureAttribIndices;

const CreateInfo m_Settings;
const CreateInfo m_Settings;

// The number of texture attributes in PBRMaterialShaderInfo.Textures array
// (aka PBR_NUM_TEXTURE_ATTRIBUTES).
Expand Down
16 changes: 0 additions & 16 deletions PBR/interface/USD_Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,10 @@ namespace Diligent
class USD_Renderer : public PBR_Renderer
{
public:
struct ShaderTextureAttributeIndices
{
Uint32 BaseColor = 0;
Uint32 Normal = 1;
Uint32 Metallic = 2;
Uint32 Roughness = 3;
Uint32 Occlusion = 4;
Uint32 Emissive = 5;
};

struct CreateInfo : PBR_Renderer::CreateInfo
{
Uint32 ColorTargetIndex = 0;
Uint32 MeshIdTargetIndex = 1;

ShaderTextureAttributeIndices TextureAttribIndices;
};
/// Initializes the renderer
USD_Renderer(IRenderDevice* pDevice,
Expand All @@ -66,17 +54,13 @@ class USD_Renderer : public PBR_Renderer
USD_PSO_FLAG_ENABLE_ALL_OUTPUTS = USD_PSO_FLAG_ENABLE_COLOR_AND_MESH_ID_OUTPUTS
};

const ShaderTextureAttributeIndices& GetShaderTextureAttributeIndices() const { return m_ShaderTextureAttribIndices; }

private:
std::string GetUsdPbrPSMainSource(USD_Renderer::PSO_FLAGS PSOFlags) const;
struct USDRendererCreateInfoWrapper;

private:
const Uint32 m_ColorTargetIndex;
const Uint32 m_MeshIdTargetIndex;

const ShaderTextureAttributeIndices m_ShaderTextureAttribIndices;
};
DEFINE_FLAG_ENUM_OPERATORS(USD_Renderer::USD_PSO_FLAGS)

Expand Down
21 changes: 5 additions & 16 deletions PBR/src/GLTF_PBR_Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,6 @@ namespace HLSL
namespace
{

// clang-format off
static constexpr std::array<PBR_Renderer::CreateInfo::ShaderTextureAttribIndex, 5> DefaultShaderTextureAttribIndices =
{
PBR_Renderer::CreateInfo::ShaderTextureAttribIndex{"BaseColorTextureAttribId", GLTF::DefaultBaseColorTextureAttribId},
PBR_Renderer::CreateInfo::ShaderTextureAttribIndex{"PhysicalDescriptorTextureAttribId", GLTF::DefaultMetallicRoughnessTextureAttribId},
PBR_Renderer::CreateInfo::ShaderTextureAttribIndex{"NormalTextureAttribId", GLTF::DefaultNormalTextureAttribId},
PBR_Renderer::CreateInfo::ShaderTextureAttribIndex{"OcclusionTextureAttribId", GLTF::DefaultOcclusionTextureAttribId},
PBR_Renderer::CreateInfo::ShaderTextureAttribIndex{"EmissiveTextureAttribId", GLTF::DefaultEmissiveTextureAttribId}
};
// clang-format on

struct PBRRendererCreateInfoWrapper
{
PBRRendererCreateInfoWrapper(const PBR_Renderer::CreateInfo& _CI) :
Expand All @@ -79,11 +68,11 @@ struct PBRRendererCreateInfoWrapper
CI.InputLayout = InputLayout;
}

if (CI.NumShaderTextureAttribIndices == 0)
{
CI.pShaderTextureAttribIndices = DefaultShaderTextureAttribIndices.data();
CI.NumShaderTextureAttribIndices = static_cast<Uint32>(DefaultShaderTextureAttribIndices.size());
}
CI.TextureAttribIndinces.BaseColor = GLTF::DefaultBaseColorTextureAttribId;
CI.TextureAttribIndinces.PhysDesc = GLTF::DefaultMetallicRoughnessTextureAttribId;
CI.TextureAttribIndinces.Normal = GLTF::DefaultNormalTextureAttribId;
CI.TextureAttribIndinces.Occlusion = GLTF::DefaultOcclusionTextureAttribId;
CI.TextureAttribIndinces.Emissive = GLTF::DefaultEmissiveTextureAttribId;
}

operator const PBR_Renderer::CreateInfo &() const
Expand Down
89 changes: 31 additions & 58 deletions PBR/src/PBR_Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,76 +72,36 @@ PBR_Renderer::PSOKey::PSOKey(PSO_FLAGS _Flags,
Hash = ComputeHash(Flags, AlphaMode, DoubleSided, static_cast<Uint32>(DebugView));
}

static std::vector<std::string> CopyShaderTextureAttribIndexNames(const PBR_Renderer::CreateInfo& CI)
static Uint32 ComputeNumShaderTextureAttribs(const PBR_Renderer::CreateInfo::ShaderTextureAttribIndinces& TextureAttribIndinces)
{
std::vector<std::string> Names(CI.NumShaderTextureAttribIndices);
if (CI.pShaderTextureAttribIndices != nullptr)
{
for (Uint32 i = 0; i < CI.NumShaderTextureAttribIndices; ++i)
{
const char* SrcName = CI.pShaderTextureAttribIndices[i].Name;
if (SrcName == nullptr || *SrcName == '\0')
{
DEV_ERROR("Shader texture attribute name must not be null or empty");
continue;
}
Names[i] = SrcName;
}
}
return Names;
}

static std::vector<PBR_Renderer::CreateInfo::ShaderTextureAttribIndex> CopyShaderTextureAttribIndices(const PBR_Renderer::CreateInfo& CI,
const std::vector<std::string>& Names)
{
std::vector<PBR_Renderer::CreateInfo::ShaderTextureAttribIndex> Indices;
if (CI.pShaderTextureAttribIndices != nullptr)
{
Indices = {CI.pShaderTextureAttribIndices, CI.pShaderTextureAttribIndices + CI.NumShaderTextureAttribIndices};
VERIFY_EXPR(Indices.size() == Names.size());
for (size_t i = 0; i < Indices.size(); ++i)
{
Indices[i].Name = Names[i].c_str();
}
}
return Indices;
}

static Uint32 GetMaxShaderTextureAttribIndex(const PBR_Renderer::CreateInfo& CI)
{
Uint32 MaxIndex = 0;
if (CI.pShaderTextureAttribIndices != nullptr)
{
for (Uint32 i = 0; i < CI.NumShaderTextureAttribIndices; ++i)
{
MaxIndex = std::max(MaxIndex, CI.pShaderTextureAttribIndices[i].Index);
}
}
return MaxIndex;
int MaxIndex = -1;
MaxIndex = std::max(MaxIndex, TextureAttribIndinces.BaseColor);
MaxIndex = std::max(MaxIndex, TextureAttribIndinces.Normal);
MaxIndex = std::max(MaxIndex, TextureAttribIndinces.PhysDesc);
MaxIndex = std::max(MaxIndex, TextureAttribIndinces.Metallic);
MaxIndex = std::max(MaxIndex, TextureAttribIndinces.Roughness);
MaxIndex = std::max(MaxIndex, TextureAttribIndinces.Occlusion);
MaxIndex = std::max(MaxIndex, TextureAttribIndinces.Emissive);
static_assert(sizeof(TextureAttribIndinces) == sizeof(Uint32) * 7, "Did you add new index to TextureAttribIndinces? Please handle it here.");

return MaxIndex >= 0 ? static_cast<Uint32>(MaxIndex + 1) : 0;
}

PBR_Renderer::PBR_Renderer(IRenderDevice* pDevice,
IRenderStateCache* pStateCache,
IDeviceContext* pCtx,
const CreateInfo& CI) :
m_InputLayout{CI.InputLayout},
m_ShaderTextureAttribIndexNames{CopyShaderTextureAttribIndexNames(CI)},
m_ShaderTextureAttribIndices{CopyShaderTextureAttribIndices(CI, m_ShaderTextureAttribIndexNames)},
m_Settings{
[this](CreateInfo CI) {
CI.InputLayout = m_InputLayout;
if (CI.pShaderTextureAttribIndices != nullptr)
{
CI.pShaderTextureAttribIndices = m_ShaderTextureAttribIndices.data();
}
return CI;
}(CI)},
m_NumShaderTextureAttribs{GetMaxShaderTextureAttribIndex(CI) + 1},
m_NumShaderTextureAttribs{ComputeNumShaderTextureAttribs(CI.TextureAttribIndinces)},
m_Device{pDevice, pStateCache},
m_PBRPrimitiveAttribsCB{CI.pPrimitiveAttribsCB}
{
DEV_CHECK_ERR(m_Settings.InputLayout.NumElements != 0, "Input layout must not be empty");
DEV_CHECK_ERR(m_Settings.NumShaderTextureAttribIndices > 0, "The number of shader texture attribute indices must be greater than 0");

if (m_Settings.EnableIBL)
{
Expand Down Expand Up @@ -726,11 +686,24 @@ ShaderMacroHelper PBR_Renderer::DefineMacros(PSO_FLAGS PSOFlags,
Macros.Add("TEX_COLOR_CONVERSION_MODE", m_Settings.TexColorConversionMode);

Macros.Add("PBR_NUM_TEXTURE_ATTRIBUTES", static_cast<int>(m_NumShaderTextureAttribs));
for (const auto& AttribIdx : m_ShaderTextureAttribIndices)
{
if (*AttribIdx.Name != '\0')
Macros.Add(AttribIdx.Name, static_cast<int>(AttribIdx.Index));
}

auto DefineTextureAttribIndex = [&Macros, PSOFlags](const char* Name, int Index, PSO_FLAGS Flag) {
if (Index >= 0)
{
Macros.Add(Name, Index);
}
else
{
DEV_CHECK_ERR((PSOFlags & Flag) == 0, "Shader expects ", Name, ", but it is not provided.");
}
};
DefineTextureAttribIndex("BaseColorTextureAttribId", m_Settings.TextureAttribIndinces.BaseColor, PSO_FLAG_USE_COLOR_MAP);
DefineTextureAttribIndex("NormalTextureAttribId", m_Settings.TextureAttribIndinces.Normal, PSO_FLAG_USE_NORMAL_MAP);
DefineTextureAttribIndex("PhysicalDescriptorTextureAttribId", m_Settings.TextureAttribIndinces.PhysDesc, PSO_FLAG_USE_PHYS_DESC_MAP);
DefineTextureAttribIndex("MetallicTextureAttribId", m_Settings.TextureAttribIndinces.Metallic, PSO_FLAG_USE_METALLIC_MAP);
DefineTextureAttribIndex("RoughnessTextureAttribId", m_Settings.TextureAttribIndinces.Roughness, PSO_FLAG_USE_ROUGHNESS_MAP);
DefineTextureAttribIndex("OcclusionTextureAttribId", m_Settings.TextureAttribIndinces.Occlusion, PSO_FLAG_USE_AO_MAP);
DefineTextureAttribIndex("EmissiveTextureAttribId", m_Settings.TextureAttribIndinces.Emissive, PSO_FLAG_USE_EMISSIVE_MAP);

return Macros;
}
Expand Down
28 changes: 9 additions & 19 deletions PBR/src/USD_Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,19 @@ void main(in VSOutput VSOut,
struct USD_Renderer::USDRendererCreateInfoWrapper
{
USDRendererCreateInfoWrapper(const USD_Renderer::CreateInfo& _CI, const USD_Renderer& Renderer) :
CI{_CI},
ShaderTextureAttribIndices{
PBR_Renderer::CreateInfo::ShaderTextureAttribIndex{"BaseColorTextureAttribId", CI.TextureAttribIndices.BaseColor},
PBR_Renderer::CreateInfo::ShaderTextureAttribIndex{"NormalTextureAttribId", CI.TextureAttribIndices.Normal},
PBR_Renderer::CreateInfo::ShaderTextureAttribIndex{"MetallicTextureAttribId", CI.TextureAttribIndices.Metallic},
PBR_Renderer::CreateInfo::ShaderTextureAttribIndex{"RoughnessTextureAttribId", CI.TextureAttribIndices.Roughness},
PBR_Renderer::CreateInfo::ShaderTextureAttribIndex{"OcclusionTextureAttribId", CI.TextureAttribIndices.Occlusion},
PBR_Renderer::CreateInfo::ShaderTextureAttribIndex{"EmissiveTextureAttribId", CI.TextureAttribIndices.Emissive},
}
CI{_CI}
{
CI.TextureAttribIndinces.BaseColor = 0;
CI.TextureAttribIndinces.Normal = 1;
CI.TextureAttribIndinces.Metallic = 2;
CI.TextureAttribIndinces.Roughness = 3;
CI.TextureAttribIndinces.Occlusion = 4;
CI.TextureAttribIndinces.Emissive = 5;

if (CI.GetPSMainSource == nullptr)
{
CI.GetPSMainSource = std::bind(&USD_Renderer::GetUsdPbrPSMainSource, &Renderer, std::placeholders::_1);
}

if (CI.pShaderTextureAttribIndices == nullptr)
{
CI.pShaderTextureAttribIndices = ShaderTextureAttribIndices.data();
CI.NumShaderTextureAttribIndices = static_cast<Uint32>(ShaderTextureAttribIndices.size());
}
}

operator const PBR_Renderer::CreateInfo &() const
Expand All @@ -121,8 +114,6 @@ struct USD_Renderer::USDRendererCreateInfoWrapper
}

USD_Renderer::CreateInfo CI;

std::array<USD_Renderer::CreateInfo::ShaderTextureAttribIndex, 6> ShaderTextureAttribIndices;
};

USD_Renderer::USD_Renderer(IRenderDevice* pDevice,
Expand All @@ -136,8 +127,7 @@ USD_Renderer::USD_Renderer(IRenderDevice* pDevice,
USDRendererCreateInfoWrapper{CI, *this},
},
m_ColorTargetIndex{CI.ColorTargetIndex},
m_MeshIdTargetIndex{CI.MeshIdTargetIndex},
m_ShaderTextureAttribIndices{CI.TextureAttribIndices}
m_MeshIdTargetIndex{CI.MeshIdTargetIndex}
{
}

Expand Down

0 comments on commit d173171

Please sign in to comment.