Skip to content

Commit

Permalink
Hydrogent: implemented PBR
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Sep 15, 2023
1 parent 970060d commit 225e7d5
Show file tree
Hide file tree
Showing 20 changed files with 488 additions and 316 deletions.
1 change: 1 addition & 0 deletions Hydrogent/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ PRIVATE
Diligent-GraphicsTools
Diligent-TextureLoader
Diligent-Common
DiligentFX
)

if(PLATFORM_WIN32 OR PLATFORM_UNIVERSAL_WINDOWS)
Expand Down
33 changes: 30 additions & 3 deletions Hydrogent/include/HnMaterial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,26 @@

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

#include "RenderDevice.h"
#include "ShaderResourceBinding.h"
#include "RefCntAutoPtr.hpp"
#include "BasicMath.hpp"

namespace Diligent
{

class PBR_Renderer;

namespace USD
{

namespace HLSL
{

#include "Shaders/PBR/public/PBR_Structures.fxh"

}

/// Hydra material implementation in Hydrogent.
class HnMaterial final : public pxr::HdMaterial
{
Expand All @@ -49,16 +63,25 @@ class HnMaterial final : public pxr::HdMaterial
~HnMaterial();

// Synchronizes state from the delegate to this object.
virtual void Sync(pxr::HdSceneDelegate* sceneDelegate,
pxr::HdRenderParam* renderParam,
pxr::HdDirtyBits* dirtyBits) override final;
virtual void Sync(pxr::HdSceneDelegate* SceneDelegate,
pxr::HdRenderParam* RenderParam,
pxr::HdDirtyBits* DirtyBits) override final;

// Returns the minimal set of dirty bits to place in the
// change tracker for use in the first sync of this prim.
virtual pxr::HdDirtyBits GetInitialDirtyBitsMask() const override final;

const HnTextureRegistry::TextureHandle* GetTexture(const pxr::TfToken& Name) const;

void UpdateSRB(IRenderDevice* pDevice,
PBR_Renderer& PbrRenderer,
IBuffer* pCameraAttribs,
IBuffer* pLightAttribs);

IShaderResourceBinding* GetSRB() const { return m_SRB; }

const HLSL::PBRMaterialShaderInfo& GetShaderAttribs() const { return m_ShaderAttribs; }

private:
HnMaterial(pxr::SdfPath const& id);

Expand All @@ -68,6 +91,10 @@ class HnMaterial final : public pxr::HdMaterial
HnMaterialNetwork m_Network;

std::unordered_map<pxr::TfToken, HnTextureRegistry::TextureHandleSharedPtr, pxr::TfToken::HashFunctor> m_Textures;

RefCntAutoPtr<IShaderResourceBinding> m_SRB;

HLSL::PBRMaterialShaderInfo m_ShaderAttribs{};
};

} // namespace USD
Expand Down
18 changes: 16 additions & 2 deletions Hydrogent/include/HnRenderDelegate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
namespace Diligent
{

class PBR_Renderer;

namespace USD
{

Expand All @@ -49,9 +51,18 @@ class HnMesh;
class HnRenderDelegate final : public pxr::HdRenderDelegate
{
public:
static std::unique_ptr<HnRenderDelegate> Create(IRenderDevice* pDevice, IDeviceContext* pContext);
struct CreateInfo
{
IRenderDevice* pDevice = nullptr;
IDeviceContext* pContext = nullptr;
IBuffer* pCameraAttribs = nullptr;
IBuffer* pLightAttribs = nullptr;

std::shared_ptr<PBR_Renderer> PBRRenderer;
};
static std::unique_ptr<HnRenderDelegate> Create(const CreateInfo& CI);

HnRenderDelegate(IRenderDevice* pDevice, IDeviceContext* pContext);
HnRenderDelegate(const CreateInfo& CI);

virtual ~HnRenderDelegate() override final;

Expand Down Expand Up @@ -181,6 +192,9 @@ class HnRenderDelegate final : public pxr::HdRenderDelegate

RefCntAutoPtr<IRenderDevice> m_pDevice;
RefCntAutoPtr<IDeviceContext> m_pContext;
RefCntAutoPtr<IBuffer> m_CameraAttribsCB;
RefCntAutoPtr<IBuffer> m_LightAttribsCB;
std::shared_ptr<PBR_Renderer> m_PBRRenderer;

HnTextureRegistry m_TextureRegistry;

Expand Down
28 changes: 15 additions & 13 deletions Hydrogent/include/HnRendererImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
namespace Diligent
{

class PBR_Renderer;

namespace USD
{

Expand All @@ -60,22 +62,27 @@ class HnRendererImpl final : public ObjectBase<IHnRenderer>
static constexpr INTERFACE_ID IID_Impl =
{0xb8e2e916, 0xb4e6, 0x4c1e, {0xa2, 0xdd, 0x78, 0xfc, 0xd7, 0x63, 0xf4, 0x3e}};

HnRendererImpl(IReferenceCounters* pRefCounters,
IRenderDevice* pDevice,
IDeviceContext* pContext,
TEXTURE_FORMAT RTVFormat,
TEXTURE_FORMAT DSVFormat);
HnRendererImpl(IReferenceCounters* pRefCounters,
IRenderDevice* pDevice,
IDeviceContext* pContext,
const HnRendererCreateInfo& CI);
~HnRendererImpl();

IMPLEMENT_QUERY_INTERFACE2_IN_PLACE(IID_HnRenderer, IID_Impl, TBase)

void LoadUSDStage(const char* FileName) override final;
void Update() override final;
void Draw(IDeviceContext* pCtx, const float4x4& CameraViewProj) override final;
virtual void LoadUSDStage(const char* FileName) override final;
virtual void Update() override final;
virtual void Draw(IDeviceContext* pCtx, const float4x4& CameraViewProj) override final;
virtual void SetEnvironmentMap(IDeviceContext* pCtx, ITextureView* pEnvironmentMapSRV) override final;

private:
RenderDeviceWithCache_N m_Device;

RefCntAutoPtr<IBuffer> m_CameraAttribsCB;
RefCntAutoPtr<IBuffer> m_LightAttribsCB;

std::shared_ptr<PBR_Renderer> m_PBRRenderer;

std::unique_ptr<HnRenderDelegate> m_RenderDelegate;

pxr::UsdStageRefPtr m_Stage;
Expand All @@ -84,11 +91,6 @@ class HnRendererImpl final : public ObjectBase<IHnRenderer>
pxr::UsdImagingDelegate* m_ImagingDelegate = nullptr;
pxr::TfTokenVector m_RenderTags;
pxr::HdRenderPassSharedPtr m_GeometryPass;

RefCntAutoPtr<IPipelineState> m_pPSO;
RefCntAutoPtr<IShaderResourceBinding> m_pSRB;
RefCntAutoPtr<IBuffer> m_pVSConstants;
RefCntAutoPtr<ITextureView> m_pWhiteTexSRV;
};

} // namespace USD
Expand Down
1 change: 1 addition & 0 deletions Hydrogent/include/HnTextureRegistry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class HnTextureRegistry final
struct TextureHandle
{
RefCntAutoPtr<ITexture> pTexture;
RefCntAutoPtr<ISampler> pSampler;
};

using TextureHandleSharedPtr = std::shared_ptr<TextureHandle>;
Expand Down
4 changes: 4 additions & 0 deletions Hydrogent/include/HnTokens.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ namespace USD
(minScreenSpaceWidths) \
(shadowCompareTextures) \
(diffuseColor) \
(metallic) \
(roughness) \
(normal) \
(occlusion) \
(st0)

#define HN_MATERIAL_TAG_TOKENS \
Expand Down
19 changes: 18 additions & 1 deletion Hydrogent/interface/HnRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ namespace USD
static const INTERFACE_ID IID_HnRenderer =
{0xea95099b, 0xe894, 0x47a6, {0xaf, 0x33, 0xb2, 0x0, 0x96, 0xc4, 0xcf, 0x44}};

struct HnRendererCreateInfo
{
/// Render target format.
TEXTURE_FORMAT RTVFormat = TEX_FORMAT_UNKNOWN;

/// Depth-buffer format.
TEXTURE_FORMAT DSVFormat = TEX_FORMAT_UNKNOWN;

/// Camera attributes constant buffer.
IBuffer* pCameraAttribsCB = nullptr;

/// Light attributes constant buffer.
IBuffer* pLightAttribsCB = nullptr;
};

class IHnRenderer : public IObject
{
public:
Expand All @@ -49,9 +64,11 @@ class IHnRenderer : public IObject
virtual void Update() = 0;

virtual void Draw(IDeviceContext* pCtx, const float4x4& CameraViewProj) = 0;

virtual void SetEnvironmentMap(IDeviceContext* pCtx, ITextureView* pEnvironmentMapSRV) = 0;
};

void CreateHnRenderer(IRenderDevice* pDevice, IDeviceContext* pContext, TEXTURE_FORMAT RTVFormat, TEXTURE_FORMAT DSVFormat, IHnRenderer** ppRenderer);
void CreateHnRenderer(IRenderDevice* pDevice, IDeviceContext* pContext, const HnRendererCreateInfo& CI, IHnRenderer** ppRenderer);

} // namespace USD

Expand Down
105 changes: 105 additions & 0 deletions Hydrogent/src/HnMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@

#include "HnMaterial.hpp"
#include "HnRenderDelegate.hpp"
#include "HnTokens.hpp"

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

#include "PBR_Renderer.hpp"
#include "DebugUtilities.hpp"

namespace Diligent
Expand Down Expand Up @@ -82,6 +84,57 @@ void HnMaterial::Sync(pxr::HdSceneDelegate* SceneDelegate,

HnTextureRegistry& TexRegistry = static_cast<HnRenderDelegate*>(SceneDelegate->GetRenderIndex().GetRenderDelegate())->GetTextureRegistry();
AllocateTextures(TexRegistry);

m_ShaderAttribs.BaseColorFactor = float4{1, 1, 1, 1};
m_ShaderAttribs.EmissiveFactor = float4{1, 1, 1, 1};
m_ShaderAttribs.SpecularFactor = float4{1, 1, 1, 1};
m_ShaderAttribs.MetallicFactor = 1;
m_ShaderAttribs.RoughnessFactor = 1;
m_ShaderAttribs.OcclusionFactor = 1;

const auto& MaterialParams = m_Network.GetParameters();

auto SetFallbackValue = [&](const pxr::TfToken& Name, auto SetValue) {
if (m_Textures.find(Name) != m_Textures.end())
return;

for (const auto Param : MaterialParams)
{
if (Param.Type == HnMaterialParameter::ParamType::Fallback && Param.Name == Name)
{
SetValue(Param.FallbackValue);
break;
}
}
};

SetFallbackValue(HnTokens->diffuseColor, [this](const pxr::VtValue& Val) {
m_ShaderAttribs.BaseColorFactor.MakeVector(Val.Get<pxr::GfVec4f>().data());
});
SetFallbackValue(HnTokens->metallic, [this](const pxr::VtValue& Val) {
m_ShaderAttribs.MetallicFactor = Val.Get<float>();
});
SetFallbackValue(HnTokens->roughness, [this](const pxr::VtValue& Val) {
m_ShaderAttribs.RoughnessFactor = Val.Get<float>();
});
SetFallbackValue(HnTokens->occlusion, [this](const pxr::VtValue& Val) {
m_ShaderAttribs.OcclusionFactor = Val.Get<float>();
});

m_ShaderAttribs.Workflow = PBR_Renderer::PBR_WORKFLOW_METALL_ROUGH;
m_ShaderAttribs.UVSelector0 = 0;
m_ShaderAttribs.UVSelector1 = 0;
m_ShaderAttribs.UVSelector2 = 0;
m_ShaderAttribs.UVSelector3 = 0;
m_ShaderAttribs.UVSelector4 = 0;
m_ShaderAttribs.TextureSlice0 = 0;
m_ShaderAttribs.TextureSlice1 = 0;
m_ShaderAttribs.TextureSlice2 = 0;
m_ShaderAttribs.TextureSlice3 = 0;
m_ShaderAttribs.TextureSlice4 = 0;

m_ShaderAttribs.AlphaMode = PBR_Renderer::ALPHA_MODE_OPAQUE;
m_ShaderAttribs.AlphaMaskCutoff = 0.5f;
}

*DirtyBits = HdMaterial::Clean;
Expand Down Expand Up @@ -109,6 +162,58 @@ pxr::HdDirtyBits HnMaterial::GetInitialDirtyBitsMask() const
return pxr::HdMaterial::AllDirty;
}

void HnMaterial::UpdateSRB(IRenderDevice* pDevice,
PBR_Renderer& PbrRenderer,
IBuffer* pCameraAttribs,
IBuffer* pLightAttribs)
{
if (m_SRB)
return;

PbrRenderer.CreateResourceBinding(&m_SRB);

PbrRenderer.InitCommonSRBVars(m_SRB, pCameraAttribs, pLightAttribs);

auto SetTexture = [&](const pxr::TfToken& Name, ITextureView* pDefaultTexSRV, const char* VarName) //
{
RefCntAutoPtr<ITextureView> pTexSRV;

if (auto* pTexHandle = GetTexture(Name))
{
if (pTexHandle->pTexture)
{
const auto& TexDesc = pTexHandle->pTexture->GetDesc();
if (TexDesc.Type == RESOURCE_DIM_TEX_2D_ARRAY)
pTexSRV = pTexHandle->pTexture->GetDefaultView(TEXTURE_VIEW_SHADER_RESOURCE);
else
{
const auto Name = std::string{"Tex2DArray view of texture '"} + TexDesc.Name + "'";

TextureViewDesc SRVDesc;
SRVDesc.Name = Name.c_str();
SRVDesc.ViewType = TEXTURE_VIEW_SHADER_RESOURCE;
SRVDesc.TextureDim = RESOURCE_DIM_TEX_2D_ARRAY;
pTexHandle->pTexture->CreateView(SRVDesc, &pTexSRV);

pTexSRV->SetSampler(pTexHandle->pSampler);
}
}
}

if (pTexSRV == nullptr)
pTexSRV = pDefaultTexSRV;

if (auto* pVar = m_SRB->GetVariableByName(SHADER_TYPE_PIXEL, VarName))
pVar->Set(pTexSRV);
};

SetTexture(HnTokens->diffuseColor, PbrRenderer.GetWhiteTexSRV(), "g_ColorMap");
SetTexture(HnTokens->metallic, PbrRenderer.GetWhiteTexSRV(), "g_MetallicMap");
SetTexture(HnTokens->roughness, PbrRenderer.GetWhiteTexSRV(), "g_RoughnessMap");
SetTexture(HnTokens->normal, PbrRenderer.GetDefaultNormalMapSRV(), "g_NormalMap");
SetTexture(HnTokens->occlusion, PbrRenderer.GetWhiteTexSRV(), "g_AOMap");
}

} // namespace USD

} // namespace Diligent
21 changes: 14 additions & 7 deletions Hydrogent/src/HnRenderDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ namespace Diligent
namespace USD
{

std::unique_ptr<HnRenderDelegate> HnRenderDelegate::Create(IRenderDevice* pDevice, IDeviceContext* pContext)
std::unique_ptr<HnRenderDelegate> HnRenderDelegate::Create(const CreateInfo& CI)
{
return std::make_unique<HnRenderDelegate>(pDevice, pContext);
return std::make_unique<HnRenderDelegate>(CI);
}

// clang-format off
Expand All @@ -59,10 +59,13 @@ const pxr::TfTokenVector HnRenderDelegate::SupportedBPrimTypes =
};
// clang-format on

HnRenderDelegate::HnRenderDelegate(IRenderDevice* pDevice, IDeviceContext* pContext) :
m_pDevice{pDevice},
m_pContext{pContext},
m_TextureRegistry{pDevice}
HnRenderDelegate::HnRenderDelegate(const CreateInfo& CI) :
m_pDevice{CI.pDevice},
m_pContext{CI.pContext},
m_CameraAttribsCB{CI.pCameraAttribs},
m_LightAttribsCB{CI.pLightAttribs},
m_PBRRenderer{CI.PBRRenderer},
m_TextureRegistry{CI.pDevice}
{
}

Expand Down Expand Up @@ -170,7 +173,11 @@ void HnRenderDelegate::DestroyBprim(pxr::HdBprim* bprim)
void HnRenderDelegate::CommitResources(pxr::HdChangeTracker* tracker)
{
m_TextureRegistry.Commit(m_pContext);
for (auto& mesh_it : m_Meshes)
for (auto mat_it : m_Materials)
{
mat_it.second->UpdateSRB(m_pDevice, *m_PBRRenderer, m_CameraAttribsCB, m_LightAttribsCB);
}
for (auto mesh_it : m_Meshes)
{
mesh_it.second->CommitGPUResources(m_pDevice);
}
Expand Down
Loading

0 comments on commit 225e7d5

Please sign in to comment.