Skip to content

Commit

Permalink
PBR Renderer: enable toggling individual shader features
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Oct 7, 2023
1 parent 53c53ef commit b807328
Show file tree
Hide file tree
Showing 11 changed files with 846 additions and 429 deletions.
14 changes: 5 additions & 9 deletions Hydrogent/src/HnRendererImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ HnRendererImpl::HnRendererImpl(IReferenceCounters* pRefCounters,
USDRendererCI.RTVFmt = CI.RTVFormat;
USDRendererCI.DSVFmt = CI.DSVFormat;

USDRendererCI.FrontCCW = CI.FrontCCW;
USDRendererCI.AllowDebugView = true;
USDRendererCI.UseIBL = true;
USDRendererCI.UseAO = true;
USDRendererCI.UseEmissive = true;
USDRendererCI.EnableMeshIdRendering = true;

// Use samplers from texture views
Expand All @@ -94,8 +89,6 @@ HnRendererImpl::HnRendererImpl(IReferenceCounters* pRefCounters,
// Use separate textures for metallic and roughness
USDRendererCI.UseSeparateMetallicRoughnessTextures = true;

USDRendererCI.ConvertOutputToSRGB = CI.ConvertOutputToSRGB;

static constexpr LayoutElement Inputs[] =
{
{0, 0, 3, VT_FLOAT32}, //float3 Pos : ATTRIB0;
Expand Down Expand Up @@ -128,7 +121,7 @@ HnRendererImpl::HnRendererImpl(IReferenceCounters* pRefCounters,
PBR_Renderer::PSO_FLAG_USE_VERTEX_NORMALS |
PBR_Renderer::PSO_FLAG_USE_TEXCOORD0 |
PBR_Renderer::PSO_FLAG_USE_TEXCOORD1 |
PBR_Renderer::PSO_FLAG_USE_DIFFUSE_MAP |
PBR_Renderer::PSO_FLAG_USE_COLOR_MAP |
PBR_Renderer::PSO_FLAG_USE_NORMAL_MAP |
PBR_Renderer::PSO_FLAG_USE_METALLIC_MAP |
PBR_Renderer::PSO_FLAG_USE_ROUGHNESS_MAP;
Expand All @@ -139,8 +132,11 @@ HnRendererImpl::HnRendererImpl(IReferenceCounters* pRefCounters,
PBR_Renderer::PSO_FLAG_USE_IBL |
PBR_Renderer::PSO_FLAG_ALLOW_DEBUG_VIEW;

if (CI.FrontCCW)
m_PSOFlags |= PBR_Renderer::PSO_FLAG_FRONT_CCW;

if (CI.ConvertOutputToSRGB)
m_PSOFlags |= PBR_Renderer::PSO_FLAG_CONVERT_TO_SRGB;
m_PSOFlags |= PBR_Renderer::PSO_FLAG_CONVERT_OUTPUT_TO_SRGB;
}

HnRendererImpl::~HnRendererImpl()
Expand Down
4 changes: 3 additions & 1 deletion PBR/interface/GLTF_PBR_Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ class GLTF_PBR_Renderer : public PBR_Renderer

/// Highlight color that is applied to the mesh after tone mapping
float4 HighlightColor = float4{0, 0, 0, 0};

PSO_FLAGS Flags = PSO_FLAG_DEFAULT;
};

/// GLTF Model shader resource binding information
Expand Down Expand Up @@ -221,7 +223,7 @@ class GLTF_PBR_Renderer : public PBR_Renderer

RenderInfo m_RenderParams;

PSO_FLAGS m_PSOFlags = PSO_FLAG_NONE;
PSO_FLAGS m_SupportedPSOFlags = PSO_FLAG_NONE;
};

DEFINE_FLAG_ENUM_OPERATORS(GLTF_PBR_Renderer::RenderInfo::ALPHA_MODE_FLAGS)
Expand Down
60 changes: 35 additions & 25 deletions PBR/interface/PBR_Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,36 +58,26 @@ class PBR_Renderer
/// render callback function.
TEXTURE_FORMAT DSVFmt = TEX_FORMAT_UNKNOWN;

/// Indicates if front face is CCW.
bool FrontCCW = false;
/// Indicates whether to enable IBL.
/// A pipeline state can use IBL only if this flag is set to true.
bool EnableIBL = true;

/// Indicates if the renderer should allow debug views.
/// Rendering with debug views disabled is more efficient.
bool AllowDebugView = false;
/// Whether to use enable ambient occlusion.
/// A pipeline state can use AO only if this flag is set to true.
bool EnableAO = true;

/// Indicates whether to use IBL.
bool UseIBL = false;

/// Whether to use ambient occlusion texture.
bool UseAO = true;

/// Whether to use emissive texture.
bool UseEmissive = true;
/// Whether to enable emissive texture.
/// A pipeline state can use emissive texture only if this flag is set to true.
bool EnableEmissive = true;

/// When set to true, pipeline state will be compiled with immutable samplers.
/// When set to false, samplers from the texture views will be used.
bool UseImmutableSamplers = true;

/// Whether to use texture atlas (e.g. apply UV transforms when sampling textures).
bool UseTextureAtlas = false;

/// Whether to use separate textures for metallic and roughness
/// instead of a combined physical description texture.
bool UseSeparateMetallicRoughnessTextures = false;

/// Manually convert shader output to sRGB color space.
bool ConvertOutputToSRGB = false;

/// Whether to enable rendering mesh Ids to a separate render target.
bool EnableMeshIdRendering = false;

Expand Down Expand Up @@ -216,7 +206,7 @@ class PBR_Renderer
PSO_FLAG_USE_TEXCOORD1 = 1u << 3u,
PSO_FLAG_USE_JOINTS = 1u << 4u,

PSO_FLAG_USE_DIFFUSE_MAP = 1u << 5u,
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,
Expand All @@ -225,10 +215,28 @@ class PBR_Renderer
PSO_FLAG_USE_EMISSIVE_MAP = 1u << 11u,
PSO_FLAG_USE_IBL = 1u << 12u,

PSO_FLAG_FRONT_CCW = 1u << 13u,
PSO_FLAG_ALLOW_DEBUG_VIEW = 1u << 14u,
PSO_FLAG_USE_TEXTURE_ATLAS = 1u << 15u,
PSO_FLAG_CONVERT_TO_SRGB = 1u << 16u,
PSO_FLAG_FRONT_CCW = 1u << 13u,
PSO_FLAG_ALLOW_DEBUG_VIEW = 1u << 14u,
PSO_FLAG_USE_TEXTURE_ATLAS = 1u << 15u,
PSO_FLAG_CONVERT_OUTPUT_TO_SRGB = 1u << 16u,

PSO_FLAG_LAST = PSO_FLAG_CONVERT_OUTPUT_TO_SRGB,

PSO_FLAG_DEFAULT =
PSO_FLAG_USE_VERTEX_COLORS |
PSO_FLAG_USE_VERTEX_NORMALS |
PSO_FLAG_USE_TEXCOORD0 |
PSO_FLAG_USE_TEXCOORD1 |
PSO_FLAG_USE_JOINTS |
PSO_FLAG_USE_COLOR_MAP |
PSO_FLAG_USE_NORMAL_MAP |
PSO_FLAG_USE_PHYS_DESC_MAP |
PSO_FLAG_USE_AO_MAP |
PSO_FLAG_USE_EMISSIVE_MAP |
PSO_FLAG_USE_IBL |
PSO_FLAG_FRONT_CCW,

PSO_FLAG_ALL = PSO_FLAG_LAST * 2u - 1u,
};

struct PSOKey
Expand Down Expand Up @@ -276,7 +284,9 @@ class PBR_Renderer

using PSOCacheType = std::unordered_map<PSOKey, RefCntAutoPtr<IPipelineState>, PSOKey::Hasher>;

static IPipelineState* GetPSO(PSOCacheType& PSOCache, const PSOKey& Key, std::function<void()> CreatePSO);
IPipelineState* GetPSO(PSOCacheType& PSOCache, PSOKey Key, std::function<void()> CreatePSO);

static std::string GetVSOutputStruct(PSO_FLAGS PSOFlags);

private:
void PrecomputeBRDF(IDeviceContext* pCtx,
Expand Down
47 changes: 24 additions & 23 deletions PBR/src/GLTF_PBR_Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,27 @@ GLTF_PBR_Renderer::GLTF_PBR_Renderer(IRenderDevice* pDevice,
const CreateInfo& CI) :
PBR_Renderer{pDevice, pStateCache, pCtx, CI}
{
m_PSOFlags |=
m_SupportedPSOFlags |=
PSO_FLAG_USE_VERTEX_NORMALS |
PSO_FLAG_USE_TEXCOORD0 |
PSO_FLAG_USE_TEXCOORD1 |
PSO_FLAG_USE_JOINTS |
PSO_FLAG_USE_DIFFUSE_MAP |
PSO_FLAG_USE_COLOR_MAP |
PSO_FLAG_USE_NORMAL_MAP |
PSO_FLAG_USE_PHYS_DESC_MAP;
if (CI.UseAO)
m_PSOFlags |= PSO_FLAG_USE_AO_MAP;
if (CI.UseEmissive)
m_PSOFlags |= PSO_FLAG_USE_EMISSIVE_MAP;
if (CI.UseIBL)
m_PSOFlags |= PSO_FLAG_USE_IBL;
if (CI.AllowDebugView)
m_PSOFlags |= PSO_FLAG_ALLOW_DEBUG_VIEW;
if (CI.UseTextureAtlas)
m_PSOFlags |= PSO_FLAG_USE_TEXTURE_ATLAS;
if (CI.ConvertOutputToSRGB)
m_PSOFlags |= PSO_FLAG_CONVERT_TO_SRGB;

if (CI.EnableAO)
m_SupportedPSOFlags |= PSO_FLAG_USE_AO_MAP;
if (CI.EnableEmissive)
m_SupportedPSOFlags |= PSO_FLAG_USE_EMISSIVE_MAP;
if (CI.EnableIBL)
m_SupportedPSOFlags |= PSO_FLAG_USE_IBL;

m_SupportedPSOFlags |=
PSO_FLAG_FRONT_CCW |
PSO_FLAG_ALLOW_DEBUG_VIEW |
PSO_FLAG_USE_TEXTURE_ATLAS |
PSO_FLAG_CONVERT_OUTPUT_TO_SRGB;
}

void GLTF_PBR_Renderer::InitMaterialSRB(GLTF::Model& Model,
Expand Down Expand Up @@ -138,11 +139,11 @@ void GLTF_PBR_Renderer::InitMaterialSRB(GLTF::Model& Model,
SetTexture(GLTF::DefaultBaseColorTextureAttribId, m_pWhiteTexSRV, "g_ColorMap");
SetTexture(GLTF::DefaultMetallicRoughnessTextureAttribId, m_pDefaultPhysDescSRV, "g_PhysicalDescriptorMap");
SetTexture(GLTF::DefaultNormalTextureAttribId, m_pDefaultNormalMapSRV, "g_NormalMap");
if (m_Settings.UseAO)
if (m_Settings.EnableAO)
{
SetTexture(GLTF::DefaultOcclusionTextureAttribId, m_pWhiteTexSRV, "g_AOMap");
}
if (m_Settings.UseEmissive)
if (m_Settings.EnableEmissive)
{
SetTexture(GLTF::DefaultEmissiveTextureAttribId, m_pBlackTexSRV, "g_EmissiveMap");
}
Expand Down Expand Up @@ -181,11 +182,11 @@ void GLTF_PBR_Renderer::CreateResourceCacheSRB(IRenderDevice* pDevice,
SetTexture(CacheUseInfo.BaseColorFormat, "g_ColorMap");
SetTexture(CacheUseInfo.PhysicalDescFormat, "g_PhysicalDescriptorMap");
SetTexture(CacheUseInfo.NormalFormat, "g_NormalMap");
if (m_Settings.UseAO)
if (m_Settings.EnableAO)
{
SetTexture(CacheUseInfo.OcclusionFormat, "g_AOMap");
}
if (m_Settings.UseEmissive)
if (m_Settings.EnableEmissive)
{
SetTexture(CacheUseInfo.EmissiveFormat, "g_EmissiveMap");
}
Expand Down Expand Up @@ -241,8 +242,7 @@ void GLTF_PBR_Renderer::Begin(IRenderDevice* pDevice,
Bindings.Version = TextureVersion;
}

if (auto pPSO = GetPSO(PSOKey{m_PSOFlags, PBR_Renderer::ALPHA_MODE_OPAQUE, false}, true))
pCtx->TransitionShaderResources(pPSO, Bindings.pSRB);
pCtx->TransitionShaderResources(Bindings.pSRB);

if (auto* pVertexPool = CacheUseInfo.pResourceMgr->GetVertexPool(CacheUseInfo.VtxLayoutKey))
{
Expand Down Expand Up @@ -317,6 +317,7 @@ void GLTF_PBR_Renderer::Render(IDeviceContext* pCtx,
IShaderResourceBinding* pCurrSRB = nullptr;
PSOKey CurrPSOKey;

auto PSOFlags = RenderParams.Flags & m_SupportedPSOFlags;
for (auto AlphaMode : AlphaModes)
{
for (const auto* pNode : Scene.LinearNodes)
Expand All @@ -339,7 +340,7 @@ void GLTF_PBR_Renderer::Render(IDeviceContext* pCtx,
if (material.Attribs.AlphaMode != AlphaMode)
continue;

const PSOKey Key{m_PSOFlags, GltfAlphaModeToAlphaMode(AlphaMode), material.DoubleSided};
const PSOKey Key{PSOFlags, GltfAlphaModeToAlphaMode(AlphaMode), material.DoubleSided};

if (Key != CurrPSOKey)
{
Expand All @@ -355,7 +356,7 @@ void GLTF_PBR_Renderer::Render(IDeviceContext* pCtx,
}
else
{
VERIFY_EXPR(pCurrPSO == GetPSO(PSOKey{m_PSOFlags, GltfAlphaModeToAlphaMode(AlphaMode), material.DoubleSided}, false));
VERIFY_EXPR(pCurrPSO == GetPSO(PSOKey{PSOFlags, GltfAlphaModeToAlphaMode(AlphaMode), material.DoubleSided}, false));
}

if (pModelBindings != nullptr)
Expand Down Expand Up @@ -424,7 +425,7 @@ void GLTF_PBR_Renderer::Render(IDeviceContext* pCtx,
RendererParams.WhitePoint = m_RenderParams.WhitePoint;
RendererParams.IBLScale = m_RenderParams.IBLScale;
RendererParams.HighlightColor = m_RenderParams.HighlightColor;
RendererParams.PrefilteredCubeMipLevels = m_Settings.UseIBL ? static_cast<float>(m_pPrefilteredEnvMapSRV->GetTexture()->GetDesc().MipLevels) : 0.f;
RendererParams.PrefilteredCubeMipLevels = m_Settings.EnableIBL ? static_cast<float>(m_pPrefilteredEnvMapSRV->GetTexture()->GetDesc().MipLevels) : 0.f;
}

if (primitive.HasIndices())
Expand Down
Loading

0 comments on commit b807328

Please sign in to comment.