From 8f2b5f29b6a4347e832cbae562cec2b7d0c076d7 Mon Sep 17 00:00:00 2001 From: assiduous Date: Wed, 13 Dec 2023 21:03:18 -0800 Subject: [PATCH] PBR Renderer: made texture PSO flags consistent with texture attrib indices --- PBR/interface/PBR_Renderer.hpp | 34 +++++++++++++++++++--------------- PBR/src/PBR_Renderer.cpp | 32 +++++++++++++++++--------------- 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/PBR/interface/PBR_Renderer.hpp b/PBR/interface/PBR_Renderer.hpp index 74d5006f..c5f05298 100644 --- a/PBR/interface/PBR_Renderer.hpp +++ b/PBR/interface/PBR_Renderer.hpp @@ -54,7 +54,7 @@ class PBR_Renderer public: enum PSO_FLAGS : Uint32; - enum TEXTURE_ATTRIB_ID + enum TEXTURE_ATTRIB_ID : Uint32 { TEXTURE_ATTRIB_ID_BASE_COLOR = 0, TEXTURE_ATTRIB_ID_NORMAL, @@ -245,20 +245,24 @@ class PBR_Renderer { PSO_FLAG_NONE = 0u, - PSO_FLAG_USE_VERTEX_COLORS = 1u << 0u, - PSO_FLAG_USE_VERTEX_NORMALS = 1u << 1u, - PSO_FLAG_USE_TEXCOORD0 = 1u << 2u, - PSO_FLAG_USE_TEXCOORD1 = 1u << 3u, - PSO_FLAG_USE_JOINTS = 1u << 4u, - - PSO_FLAG_USE_COLOR_MAP = 1u << 5u, - PSO_FLAG_USE_NORMAL_MAP = 1u << 6u, - PSO_FLAG_USE_METALLIC_MAP = 1u << 7u, - PSO_FLAG_USE_ROUGHNESS_MAP = 1u << 8u, - PSO_FLAG_USE_PHYS_DESC_MAP = 1u << 9u, - PSO_FLAG_USE_AO_MAP = 1u << 10u, - PSO_FLAG_USE_EMISSIVE_MAP = 1u << 11u, - PSO_FLAG_USE_IBL = 1u << 12u, + PSO_FLAG_USE_COLOR_MAP = 1u << 0u, + PSO_FLAG_USE_NORMAL_MAP = 1u << 1u, + PSO_FLAG_USE_PHYS_DESC_MAP = 1u << 2u, + PSO_FLAG_USE_METALLIC_MAP = 1u << 3u, + PSO_FLAG_USE_ROUGHNESS_MAP = 1u << 4u, + PSO_FLAG_USE_AO_MAP = 1u << 5u, + PSO_FLAG_USE_EMISSIVE_MAP = 1u << 6u, + + PSO_FLAG_LAST_TEXTURE = PSO_FLAG_USE_EMISSIVE_MAP, + PSO_FLAG_ALL_TEXTURES = PSO_FLAG_LAST_TEXTURE * 2u - 1u, + + PSO_FLAG_USE_VERTEX_COLORS = 1u << 7u, + PSO_FLAG_USE_VERTEX_NORMALS = 1u << 8u, + PSO_FLAG_USE_TEXCOORD0 = 1u << 9u, + PSO_FLAG_USE_TEXCOORD1 = 1u << 10u, + PSO_FLAG_USE_JOINTS = 1u << 11u, + + PSO_FLAG_USE_IBL = 1u << 12u, PSO_FLAG_USE_TEXTURE_ATLAS = 1u << 13u, PSO_FLAG_ENABLE_TEXCOORD_TRANSFORM = 1u << 14u, diff --git a/PBR/src/PBR_Renderer.cpp b/PBR/src/PBR_Renderer.cpp index c7b530e2..a1f3e86c 100644 --- a/PBR/src/PBR_Renderer.cpp +++ b/PBR/src/PBR_Renderer.cpp @@ -82,20 +82,6 @@ static Uint32 ComputeMaxShaderTextureAttribs(const PBR_Renderer::CreateInfo& CI) return MaxIndex >= 0 ? static_cast(MaxIndex + 1) : 0; } -static const std::array kTextureAttribIndexFlags = []() { - std::array TextureAttribIndexFlags{}; - TextureAttribIndexFlags[PBR_Renderer::TEXTURE_ATTRIB_ID_BASE_COLOR] = PBR_Renderer::PSO_FLAG_USE_COLOR_MAP; - TextureAttribIndexFlags[PBR_Renderer::TEXTURE_ATTRIB_ID_NORMAL] = PBR_Renderer::PSO_FLAG_USE_NORMAL_MAP; - TextureAttribIndexFlags[PBR_Renderer::TEXTURE_ATTRIB_ID_PHYS_DESC] = PBR_Renderer::PSO_FLAG_USE_PHYS_DESC_MAP; - TextureAttribIndexFlags[PBR_Renderer::TEXTURE_ATTRIB_ID_METALLIC] = PBR_Renderer::PSO_FLAG_USE_METALLIC_MAP; - TextureAttribIndexFlags[PBR_Renderer::TEXTURE_ATTRIB_ID_ROUGHNESS] = PBR_Renderer::PSO_FLAG_USE_ROUGHNESS_MAP; - TextureAttribIndexFlags[PBR_Renderer::TEXTURE_ATTRIB_ID_OCCLUSION] = PBR_Renderer::PSO_FLAG_USE_AO_MAP; - TextureAttribIndexFlags[PBR_Renderer::TEXTURE_ATTRIB_ID_EMISSIVE] = PBR_Renderer::PSO_FLAG_USE_EMISSIVE_MAP; - static_assert(PBR_Renderer::TEXTURE_ATTRIB_ID_COUNT == 7, "Not all texture attribs are initialized"); - - return TextureAttribIndexFlags; -}(); - PBR_Renderer::PBR_Renderer(IRenderDevice* pDevice, IRenderStateCache* pStateCache, IDeviceContext* pCtx, @@ -625,6 +611,22 @@ void PBR_Renderer::CreateSignature() } } +static constexpr PBR_Renderer::PSO_FLAGS GetTextureAttribPSOFlag(PBR_Renderer::TEXTURE_ATTRIB_ID AttribId) +{ + // clang-format off + static_assert(PBR_Renderer::PSO_FLAG_USE_COLOR_MAP == 1u << PBR_Renderer::TEXTURE_ATTRIB_ID_BASE_COLOR, "PSO_FLAG_USE_COLOR_MAP must be 1 << TEXTURE_ATTRIB_ID_BASE_COLOR"); + static_assert(PBR_Renderer::PSO_FLAG_USE_NORMAL_MAP == 1u << PBR_Renderer::TEXTURE_ATTRIB_ID_NORMAL, "PSO_FLAG_USE_NORMAL_MAP must be 1 << TEXTURE_ATTRIB_ID_NORMAL"); + static_assert(PBR_Renderer::PSO_FLAG_USE_PHYS_DESC_MAP == 1u << PBR_Renderer::TEXTURE_ATTRIB_ID_PHYS_DESC, "PSO_FLAG_USE_PHYS_DESC_MAP must be 1 << TEXTURE_ATTRIB_ID_PHYS_DESC"); + static_assert(PBR_Renderer::PSO_FLAG_USE_METALLIC_MAP == 1u << PBR_Renderer::TEXTURE_ATTRIB_ID_METALLIC, "PSO_FLAG_USE_METALLIC_MAP must be 1 << TEXTURE_ATTRIB_ID_METALLIC"); + static_assert(PBR_Renderer::PSO_FLAG_USE_ROUGHNESS_MAP == 1u << PBR_Renderer::TEXTURE_ATTRIB_ID_ROUGHNESS, "PSO_FLAG_USE_ROUGHNESS_MAP must be 1 << TEXTURE_ATTRIB_ID_ROUGHNESS"); + static_assert(PBR_Renderer::PSO_FLAG_USE_AO_MAP == 1u << PBR_Renderer::TEXTURE_ATTRIB_ID_OCCLUSION, "PSO_FLAG_USE_AO_MAP must be 1 << TEXTURE_ATTRIB_ID_OCCLUSION"); + static_assert(PBR_Renderer::PSO_FLAG_USE_EMISSIVE_MAP == 1u << PBR_Renderer::TEXTURE_ATTRIB_ID_EMISSIVE, "PSO_FLAG_USE_EMISSIVE_MAP must be 1 << TEXTURE_ATTRIB_ID_EMISSIVE"); + // clang-format on + static_assert(PBR_Renderer::PSO_FLAG_LAST_TEXTURE == 1u << 6u, "Did you add new texture flag? You may need to handle it here."); + + return static_cast(1u << AttribId); +} + ShaderMacroHelper PBR_Renderer::DefineMacros(PSO_FLAGS PSOFlags, DebugViewType DebugView) const { @@ -708,7 +710,7 @@ ShaderMacroHelper PBR_Renderer::DefineMacros(PSO_FLAGS PSOFlags, for (size_t i = 0; i < TEXTURE_ATTRIB_ID_COUNT; ++i) { const auto& AttribName = TextureAttribNames[i]; - const auto Flag = kTextureAttribIndexFlags[i]; + const auto Flag = GetTextureAttribPSOFlag(static_cast(i)); if (m_Settings.TextureAttribIndices[i] >= 0) { Macros.Add(AttribName, m_Settings.TextureAttribIndices[i]);