From 9773ae21eb11b7ab008072c011c04a824b78ab23 Mon Sep 17 00:00:00 2001 From: assiduous Date: Sat, 21 Oct 2023 21:39:21 -0700 Subject: [PATCH] Hydrogent: determine alpha mode from the material tag in the render pass --- Hydrogent/include/HnRenderPass.hpp | 1 + Hydrogent/include/HnTypeConversions.hpp | 3 +++ Hydrogent/src/HnMaterial.cpp | 9 ++------- Hydrogent/src/HnRenderPass.cpp | 19 ++++++++++++------- Hydrogent/src/HnTypeConversions.cpp | 11 +++++++++++ 5 files changed, 29 insertions(+), 14 deletions(-) diff --git a/Hydrogent/include/HnRenderPass.hpp b/Hydrogent/include/HnRenderPass.hpp index 895805e8..460434db 100644 --- a/Hydrogent/include/HnRenderPass.hpp +++ b/Hydrogent/include/HnRenderPass.hpp @@ -72,6 +72,7 @@ class HnRenderPass final : public pxr::HdRenderPass unsigned int m_TaskRenderTagsVersion = ~0u; pxr::TfTokenVector m_RenderTags; + pxr::TfToken m_MaterialTag; }; } // namespace USD diff --git a/Hydrogent/include/HnTypeConversions.hpp b/Hydrogent/include/HnTypeConversions.hpp index f58cf93a..252129a6 100644 --- a/Hydrogent/include/HnTypeConversions.hpp +++ b/Hydrogent/include/HnTypeConversions.hpp @@ -31,6 +31,7 @@ #include "RasterizerState.h" #include "DepthStencilState.h" #include "BlendState.h" +#include "PBR_Renderer.hpp" #include "pxr/pxr.h" #include "pxr/imaging/hd/types.h" @@ -53,6 +54,8 @@ STENCIL_OP HdStencilOpToStencilOp(pxr::HdStencilOp hdStencilOp); BLEND_OPERATION HdBlendOpToBlendOperation(pxr::HdBlendOp hdBlendOp); BLEND_FACTOR HdBlendFactorToBlendFactor(pxr::HdBlendFactor hdBlendFactor); +PBR_Renderer::ALPHA_MODE MaterialTagToPbrAlphaMode(const pxr::TfToken& MaterialTag); + } // namespace USD } // namespace Diligent diff --git a/Hydrogent/src/HnMaterial.cpp b/Hydrogent/src/HnMaterial.cpp index 251b2382..816f8d08 100644 --- a/Hydrogent/src/HnMaterial.cpp +++ b/Hydrogent/src/HnMaterial.cpp @@ -27,6 +27,7 @@ #include "HnMaterial.hpp" #include "HnRenderDelegate.hpp" #include "HnTokens.hpp" +#include "HnTypeConversions.hpp" #include "pxr/imaging/hd/sceneDelegate.h" @@ -137,13 +138,7 @@ void HnMaterial::Sync(pxr::HdSceneDelegate* SceneDelegate, m_ShaderAttribs.TextureSlice3 = 0; m_ShaderAttribs.TextureSlice4 = 0; - const auto& Tag = m_Network.GetTag(); - if (Tag == HnMaterialTagTokens->translucent) - m_ShaderAttribs.AlphaMode = PBR_Renderer::ALPHA_MODE_BLEND; - else if (Tag == HnMaterialTagTokens->masked) - PBR_Renderer::ALPHA_MODE_MASK; - else - PBR_Renderer::ALPHA_MODE_OPAQUE; + m_ShaderAttribs.AlphaMode = MaterialTagToPbrAlphaMode(m_Network.GetTag()); m_ShaderAttribs.AlphaMaskCutoff = m_Network.GetOpacityThreshold(); m_ShaderAttribs.BaseColorFactor.a = m_Network.GetOpacity(); diff --git a/Hydrogent/src/HnRenderPass.cpp b/Hydrogent/src/HnRenderPass.cpp index f1baf7c7..8ea5d785 100644 --- a/Hydrogent/src/HnRenderPass.cpp +++ b/Hydrogent/src/HnRenderPass.cpp @@ -29,6 +29,7 @@ #include "HnRenderDelegate.hpp" #include "HnMesh.hpp" #include "HnMaterial.hpp" +#include "HnTypeConversions.hpp" #include "pxr/imaging/hd/renderIndex.h" @@ -96,7 +97,7 @@ void HnRenderPass::_Execute(const pxr::HdRenderPassStateSharedPtr& RPState, pRenderDelegate->GetDeviceContext(), USDRenderer->GetPBRAttribsCB(), static_cast(*RPState), - PBR_Renderer::ALPHA_MODE_OPAQUE, + MaterialTagToPbrAlphaMode(m_MaterialTag), }; GraphicsPipelineDesc GraphicsDesc = State.RPState.GetGraphicsPipelineDesc(); @@ -148,18 +149,18 @@ void HnRenderPass::UpdateDrawItems(const pxr::TfTokenVector& RenderTags) pxr::HdRenderIndex* pRenderIndex = GetRenderIndex(); //HnRenderDelegate* pRenderDelegate = static_cast(pRenderIndex->GetRenderDelegate()); - const pxr::HdRprimCollection& Collection = GetRprimCollection(); - const pxr::HdChangeTracker& Tracker = pRenderIndex->GetChangeTracker(); + const pxr::HdRprimCollection& Collection = GetRprimCollection(); + const pxr::HdChangeTracker& Tracker = pRenderIndex->GetChangeTracker(); + const pxr::TfToken& MaterialTag = Collection.GetMaterialTag(); const unsigned int CollectionVersion = Tracker.GetCollectionVersion(Collection.GetName()); const unsigned int RprimRenderTagVersion = Tracker.GetRenderTagVersion(); const unsigned int TaskRenderTagsVersion = Tracker.GetTaskRenderTagsVersion(); - //const unsigned int MaterialTagsVersion = GetMaterialTagsVersion(pRenderIndex); //const unsigned int GeomSubsetDrawItemsVersion = GetGeomSubsetDrawItemsVersion(pRenderIndex); const bool CollectionChanged = (m_CollectionVersion != CollectionVersion); const bool RprimRenderTagChanged = (m_RprimRenderTagVersion != RprimRenderTagVersion); - //const bool MaterialTagsChanged = (m_MaterialTagsVersion != MaterialTagsVersion); + const bool MaterialTagChanged = (m_MaterialTag != MaterialTag); //const bool GeomSubsetDrawItemsChanged = (m_GeomSubsetDrawItemsVersion != GeomSubsetDrawItemsVersion); bool TaskRenderTagsChanged = false; @@ -175,7 +176,7 @@ void HnRenderPass::UpdateDrawItems(const pxr::TfTokenVector& RenderTags) if (CollectionChanged || RprimRenderTagChanged || - //MaterialTagsChanged || + MaterialTagChanged || //GeomSubsetDrawItemsChanged || TaskRenderTagsChanged) { @@ -193,6 +194,7 @@ void HnRenderPass::UpdateDrawItems(const pxr::TfTokenVector& RenderTags) m_CollectionVersion = CollectionVersion; m_RprimRenderTagVersion = RprimRenderTagVersion; + m_MaterialTag = MaterialTag; } @@ -253,7 +255,10 @@ void HnRenderPass::RenderMesh(RenderState& State, PBR_Renderer::PSO_FLAG_USE_IBL | PBR_Renderer::PSO_FLAG_ENABLE_DEBUG_VIEW; - pPSO = State.PbrPSOCache.Get({PSOFlags, static_cast(ShaderAttribs.AlphaMode), /*DoubleSided = */ false}, true); + VERIFY(ShaderAttribs.AlphaMode == State.AlphaMode, + "Alpha mode derived from the material tag is not consistent with the alpha mode in the shader attributes. " + "This may indicate an issue in how alpha mode is determined in the material, or (less likely) an issue in Rprim sorting by Hydra."); + pPSO = State.PbrPSOCache.Get({PSOFlags, static_cast(State.AlphaMode), /*DoubleSided = */ false}, true); } else if (RenderMode == HN_RENDER_MODE_MESH_EDGES) { diff --git a/Hydrogent/src/HnTypeConversions.cpp b/Hydrogent/src/HnTypeConversions.cpp index 2a4a8d04..9b6fb09d 100644 --- a/Hydrogent/src/HnTypeConversions.cpp +++ b/Hydrogent/src/HnTypeConversions.cpp @@ -25,6 +25,7 @@ */ #include "HnTypeConversions.hpp" +#include "HnTokens.hpp" #include "DebugUtilities.hpp" @@ -221,6 +222,16 @@ BLEND_FACTOR HdBlendFactorToBlendFactor(pxr::HdBlendFactor hdBlendFactor) } } +PBR_Renderer::ALPHA_MODE MaterialTagToPbrAlphaMode(const pxr::TfToken& MaterialTag) +{ + if (MaterialTag == HnMaterialTagTokens->translucent) + return PBR_Renderer::ALPHA_MODE_BLEND; + else if (MaterialTag == HnMaterialTagTokens->masked) + return PBR_Renderer::ALPHA_MODE_MASK; + else + return PBR_Renderer::ALPHA_MODE_OPAQUE; +} + } // namespace USD } // namespace Diligent