Skip to content

Commit

Permalink
Hydrogent: determine alpha mode from the material tag in the render pass
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Oct 22, 2023
1 parent c80067d commit 9773ae2
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
1 change: 1 addition & 0 deletions Hydrogent/include/HnRenderPass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Hydrogent/include/HnTypeConversions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
9 changes: 2 additions & 7 deletions Hydrogent/src/HnMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "HnMaterial.hpp"
#include "HnRenderDelegate.hpp"
#include "HnTokens.hpp"
#include "HnTypeConversions.hpp"

#include "pxr/imaging/hd/sceneDelegate.h"

Expand Down Expand Up @@ -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();
Expand Down
19 changes: 12 additions & 7 deletions Hydrogent/src/HnRenderPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "HnRenderDelegate.hpp"
#include "HnMesh.hpp"
#include "HnMaterial.hpp"
#include "HnTypeConversions.hpp"

#include "pxr/imaging/hd/renderIndex.h"

Expand Down Expand Up @@ -96,7 +97,7 @@ void HnRenderPass::_Execute(const pxr::HdRenderPassStateSharedPtr& RPState,
pRenderDelegate->GetDeviceContext(),
USDRenderer->GetPBRAttribsCB(),
static_cast<const HnRenderPassState&>(*RPState),
PBR_Renderer::ALPHA_MODE_OPAQUE,
MaterialTagToPbrAlphaMode(m_MaterialTag),
};

GraphicsPipelineDesc GraphicsDesc = State.RPState.GetGraphicsPipelineDesc();
Expand Down Expand Up @@ -148,18 +149,18 @@ void HnRenderPass::UpdateDrawItems(const pxr::TfTokenVector& RenderTags)
pxr::HdRenderIndex* pRenderIndex = GetRenderIndex();
//HnRenderDelegate* pRenderDelegate = static_cast<HnRenderDelegate*>(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;
Expand All @@ -175,7 +176,7 @@ void HnRenderPass::UpdateDrawItems(const pxr::TfTokenVector& RenderTags)

if (CollectionChanged ||
RprimRenderTagChanged ||
//MaterialTagsChanged ||
MaterialTagChanged ||
//GeomSubsetDrawItemsChanged ||
TaskRenderTagsChanged)
{
Expand All @@ -193,6 +194,7 @@ void HnRenderPass::UpdateDrawItems(const pxr::TfTokenVector& RenderTags)

m_CollectionVersion = CollectionVersion;
m_RprimRenderTagVersion = RprimRenderTagVersion;
m_MaterialTag = MaterialTag;
}


Expand Down Expand Up @@ -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<PBR_Renderer::ALPHA_MODE>(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<PBR_Renderer::ALPHA_MODE>(State.AlphaMode), /*DoubleSided = */ false}, true);
}
else if (RenderMode == HN_RENDER_MODE_MESH_EDGES)
{
Expand Down
11 changes: 11 additions & 0 deletions Hydrogent/src/HnTypeConversions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/

#include "HnTypeConversions.hpp"
#include "HnTokens.hpp"

#include "DebugUtilities.hpp"

Expand Down Expand Up @@ -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

0 comments on commit 9773ae2

Please sign in to comment.