Skip to content

Commit

Permalink
Hydrogent: enabled mesh edges rendering mode
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Sep 20, 2023
1 parent 03e5699 commit e31577b
Show file tree
Hide file tree
Showing 21 changed files with 347 additions and 120 deletions.
6 changes: 3 additions & 3 deletions Hydrogent/include/HnMesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ class HnMesh final : public pxr::HdMesh

struct IndexData
{
pxr::VtVec3iArray TrianglesFaceIndices;
pxr::VtIntArray PrimitiveParam;
pxr::VtIntArray TrianglesEdgeIndices;
pxr::VtVec3iArray TrianglesFaceIndices;
pxr::VtIntArray PrimitiveParam;
std::vector<pxr::GfVec2i> MeshEdgeIndices;
};
std::unique_ptr<IndexData> m_IndexData;

Expand Down
4 changes: 2 additions & 2 deletions Hydrogent/include/HnRendererImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
namespace Diligent
{

class PBR_Renderer;
class USD_Renderer;
class EnvMapRenderer;

namespace USD
Expand Down Expand Up @@ -82,7 +82,7 @@ class HnRendererImpl final : public ObjectBase<IHnRenderer>
RefCntAutoPtr<IBuffer> m_CameraAttribsCB;
RefCntAutoPtr<IBuffer> m_LightAttribsCB;

std::shared_ptr<PBR_Renderer> m_PBRRenderer;
std::shared_ptr<USD_Renderer> m_USDRenderer;
std::unique_ptr<EnvMapRenderer> m_EnvMapRenderer;

std::unique_ptr<HnRenderDelegate> m_RenderDelegate;
Expand Down
11 changes: 11 additions & 0 deletions Hydrogent/interface/HnRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ struct HnRendererCreateInfo
IBuffer* pLightAttribsCB = nullptr;
};

enum HN_RENDER_MODE
{
HN_RENDER_MODE_SOLID,
HN_RENDER_MODE_MESH_EDGES,
HN_RENDER_MODE_COUNT
};

struct HnDrawAttribs
{
float4x4 Transform = float4x4::Identity();
Expand All @@ -71,6 +78,10 @@ struct HnDrawAttribs
float MiddleGray = 0.18f;
float WhitePoint = 3.0f;
float IBLScale = 1;

float4 WireframeColor = float4{1, 1, 1, 1};

HN_RENDER_MODE RenderMode = HN_RENDER_MODE_SOLID;
};

class IHnRenderer : public IObject
Expand Down
7 changes: 4 additions & 3 deletions Hydrogent/src/HnMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ void HnMesh::UpdateTopology(pxr::HdSceneDelegate& SceneDelegate,
MeshUtil.ComputeTriangleIndices(
&m_IndexData->TrianglesFaceIndices,
&m_IndexData->PrimitiveParam,
&m_IndexData->TrianglesEdgeIndices);
nullptr);
MeshUtil.EnumerateEdges(&m_IndexData->MeshEdgeIndices);

DirtyBits &= ~pxr::HdChangeTracker::DirtyTopology;
}
Expand Down Expand Up @@ -315,7 +316,7 @@ void HnMesh::UpdateIndexBuffer(const RenderDeviceX_N& Device)
{
const auto Name = GetId().GetString() + " - Edge Index Buffer";

m_NumEdges = static_cast<size_t>(m_IndexData->TrianglesEdgeIndices.size()) / 2;
m_NumEdges = static_cast<size_t>(m_IndexData->MeshEdgeIndices.size());

BufferDesc Desc{
Name.c_str(),
Expand All @@ -324,7 +325,7 @@ void HnMesh::UpdateIndexBuffer(const RenderDeviceX_N& Device)
USAGE_IMMUTABLE,
};

BufferData InitData{m_IndexData->TrianglesEdgeIndices.data(), Desc.Size};
BufferData InitData{m_IndexData->MeshEdgeIndices.data(), Desc.Size};
m_pEdgeIndexBuffer = Device.CreateBuffer(Desc, &InitData);
}

Expand Down
61 changes: 35 additions & 26 deletions Hydrogent/src/HnRendererImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include "HnTokens.hpp"

#include "EngineMemory.h"
#include "PBR_Renderer.hpp"
#include "USD_Renderer.hpp"
#include "EnvMapRenderer.hpp"
#include "MapHelper.hpp"

Expand Down Expand Up @@ -69,30 +69,30 @@ HnRendererImpl::HnRendererImpl(IReferenceCounters* pRefCounters,
m_Device{pDevice},
m_CameraAttribsCB{CI.pCameraAttribsCB},
m_LightAttribsCB{CI.pLightAttribsCB},
m_PBRRenderer{
std::make_shared<PBR_Renderer>(
m_USDRenderer{
std::make_shared<USD_Renderer>(
pDevice,
nullptr,
pContext,
[](const HnRendererCreateInfo& CI) {
PBR_Renderer::CreateInfo PBRRendererCI;
PBRRendererCI.RTVFmt = CI.RTVFormat;
PBRRendererCI.DSVFmt = CI.DSVFormat;
USD_Renderer::CreateInfo USDRendererCI;
USDRendererCI.RTVFmt = CI.RTVFormat;
USDRendererCI.DSVFmt = CI.DSVFormat;

PBRRendererCI.FrontCCW = CI.FrontCCW;
PBRRendererCI.AllowDebugView = true;
PBRRendererCI.UseIBL = true;
PBRRendererCI.UseAO = true;
PBRRendererCI.UseEmissive = false;
USDRendererCI.FrontCCW = CI.FrontCCW;
USDRendererCI.AllowDebugView = true;
USDRendererCI.UseIBL = true;
USDRendererCI.UseAO = true;
USDRendererCI.UseEmissive = false;

// Use samplers from texture views
PBRRendererCI.UseImmutableSamplers = false;
USDRendererCI.UseImmutableSamplers = false;
// Disable animation
PBRRendererCI.MaxJointCount = 0;
USDRendererCI.MaxJointCount = 0;
// Use separate textures for metallic and roughness
PBRRendererCI.UseSeparateMetallicRoughnessTextures = true;
USDRendererCI.UseSeparateMetallicRoughnessTextures = true;

PBRRendererCI.ConvertOutputToSRGB = CI.ConvertOutputToSRGB;
USDRendererCI.ConvertOutputToSRGB = CI.ConvertOutputToSRGB;

static constexpr LayoutElement Inputs[] =
{
Expand All @@ -102,10 +102,10 @@ HnRendererImpl::HnRendererImpl(IReferenceCounters* pRefCounters,
{3, 3, 2, VT_FLOAT32}, //float2 UV1 : ATTRIB3;
};

PBRRendererCI.InputLayout.LayoutElements = Inputs;
PBRRendererCI.InputLayout.NumElements = _countof(Inputs);
USDRendererCI.InputLayout.LayoutElements = Inputs;
USDRendererCI.InputLayout.NumElements = _countof(Inputs);

return PBRRendererCI;
return USDRendererCI;
}(CI)),
},
m_EnvMapRenderer{
Expand All @@ -120,7 +120,7 @@ HnRendererImpl::HnRendererImpl(IReferenceCounters* pRefCounters,

return EnvMapRndrCI;
}(CI, pDevice))},
m_RenderDelegate{HnRenderDelegate::Create({pDevice, pContext, m_CameraAttribsCB, m_LightAttribsCB, m_PBRRenderer})}
m_RenderDelegate{HnRenderDelegate::Create({pDevice, pContext, m_CameraAttribsCB, m_LightAttribsCB, m_USDRenderer})}
{
}

Expand Down Expand Up @@ -200,7 +200,7 @@ void HnRendererImpl::Draw(IDeviceContext* pCtx, const HnDrawAttribs& Attribs)
if (Meshes.empty())
return;

if (auto* pEnvMapSRV = m_PBRRenderer->GetPrefilteredEnvMapSRV())
if (auto* pEnvMapSRV = m_USDRenderer->GetPrefilteredEnvMapSRV())
{
Diligent::HLSL::ToneMappingAttribs TMAttribs;
TMAttribs.iToneMappingMode = TONE_MAPPING_MODE_UNCHARTED2;
Expand All @@ -219,9 +219,12 @@ void HnRendererImpl::Draw(IDeviceContext* pCtx, const HnDrawAttribs& Attribs)
m_EnvMapRenderer->Render(EnvMapAttribs, TMAttribs);
}

for (auto AlphaMode : {PBR_Renderer::ALPHA_MODE_OPAQUE, PBR_Renderer::ALPHA_MODE_MASK, PBR_Renderer::ALPHA_MODE_BLEND})
for (auto AlphaMode : {USD_Renderer::ALPHA_MODE_OPAQUE, USD_Renderer::ALPHA_MODE_MASK, USD_Renderer::ALPHA_MODE_BLEND})
{
pCtx->SetPipelineState(m_PBRRenderer->GetPSO({AlphaMode, /*DoubleSided = */ false}));
if (Attribs.RenderMode == HN_RENDER_MODE_MESH_EDGES)
pCtx->SetPipelineState(m_USDRenderer->GetMeshEdgesPSO());
else
pCtx->SetPipelineState(m_USDRenderer->GetPSO({AlphaMode, /*DoubleSided = */ false}));

for (auto mesh_it : Meshes)
{
Expand All @@ -243,7 +246,9 @@ void HnRendererImpl::Draw(IDeviceContext* pCtx, const HnDrawAttribs& Attribs)
auto* pVB0 = Mesh.GetVertexBuffer(HnMesh::VERTEX_BUFFER_ID_POSITION);
auto* pVB1 = Mesh.GetVertexBuffer(HnMesh::VERTEX_BUFFER_ID_NORMAL);
auto* pVB2 = Mesh.GetVertexBuffer(HnMesh::VERTEX_BUFFER_ID_TEXCOORD);
auto* pIB = Mesh.GetTriangleIndexBuffer();
auto* pIB = (Attribs.RenderMode == HN_RENDER_MODE_MESH_EDGES) ?
Mesh.GetEdgeIndexBuffer() :
Mesh.GetTriangleIndexBuffer();

if (pVB0 == nullptr || pVB1 == nullptr || pVB2 == nullptr || pIB == nullptr || pSRB == nullptr)
continue;
Expand All @@ -254,7 +259,7 @@ void HnRendererImpl::Draw(IDeviceContext* pCtx, const HnDrawAttribs& Attribs)
pCtx->SetIndexBuffer(pIB, 0, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);

{
MapHelper<HLSL::PBRShaderAttribs> pDstShaderAttribs{pCtx, m_PBRRenderer->GetPBRAttribsCB(), MAP_WRITE, MAP_FLAG_DISCARD};
MapHelper<HLSL::PBRShaderAttribs> pDstShaderAttribs{pCtx, m_USDRenderer->GetPBRAttribsCB(), MAP_WRITE, MAP_FLAG_DISCARD};

pDstShaderAttribs->Transforms.NodeMatrix = Mesh.GetTransform() * Attribs.Transform;
pDstShaderAttribs->Transforms.JointCount = 0;
Expand All @@ -272,18 +277,22 @@ void HnRendererImpl::Draw(IDeviceContext* pCtx, const HnDrawAttribs& Attribs)
RendererParams.WhitePoint = Attribs.WhitePoint;
RendererParams.IBLScale = Attribs.IBLScale;
RendererParams.PrefilteredCubeMipLevels = 5; //m_Settings.UseIBL ? static_cast<float>(m_pPrefilteredEnvMapSRV->GetTexture()->GetDesc().MipLevels) : 0.f;
RendererParams.WireframeColor = Attribs.WireframeColor;
}

pCtx->CommitShaderResources(pSRB, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
DrawIndexedAttribs DrawAttrs{Mesh.GetNumTriangles() * 3, VT_UINT32, DRAW_FLAG_VERIFY_ALL};
DrawIndexedAttribs DrawAttrs = (Attribs.RenderMode == HN_RENDER_MODE_MESH_EDGES) ?
DrawIndexedAttribs{Mesh.GetNumEdges() * 2, VT_UINT32, DRAW_FLAG_VERIFY_ALL} :
DrawIndexedAttribs{Mesh.GetNumTriangles() * 3, VT_UINT32, DRAW_FLAG_VERIFY_ALL};

pCtx->DrawIndexed(DrawAttrs);
}
}
}

void HnRendererImpl::SetEnvironmentMap(IDeviceContext* pCtx, ITextureView* pEnvironmentMapSRV)
{
m_PBRRenderer->PrecomputeCubemaps(m_Device, m_Device, pCtx, pEnvironmentMapSRV);
m_USDRenderer->PrecomputeCubemaps(m_Device, m_Device, pCtx, pEnvironmentMapSRV);
}

} // namespace USD
Expand Down
2 changes: 2 additions & 0 deletions PBR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ cmake_minimum_required (VERSION 3.6)
set(SOURCE
"${CMAKE_CURRENT_SOURCE_DIR}/src/PBR_Renderer.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/GLTF_PBR_Renderer.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/USD_Renderer.cpp"
)

set(INCLUDE
"${CMAKE_CURRENT_SOURCE_DIR}/interface/PBR_Renderer.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/interface/GLTF_PBR_Renderer.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/interface/USD_Renderer.hpp"
)

target_sources(DiligentFX PRIVATE ${SOURCE} ${INCLUDE})
Expand Down
5 changes: 5 additions & 0 deletions PBR/interface/PBR_Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
#include "../../../DiligentCore/Platforms/Basic/interface/DebugUtilities.hpp"
#include "../../../DiligentCore/Graphics/GraphicsEngine/interface/DeviceContext.h"
#include "../../../DiligentCore/Graphics/GraphicsEngine/interface/RenderDevice.h"
#include "../../../DiligentCore/Graphics/GraphicsEngine/interface/GraphicsTypesX.hpp"
#include "../../../DiligentCore/Graphics/GraphicsTools/interface/RenderStateCache.h"
#include "../../../DiligentCore/Graphics/GraphicsTools/interface/ShaderMacroHelper.hpp"
#include "../../../DiligentCore/Common/interface/RefCntAutoPtr.hpp"

namespace Diligent
Expand Down Expand Up @@ -235,6 +237,9 @@ class PBR_Renderer
IBuffer* pLightAttribs);

protected:
ShaderMacroHelper DefineMacros() const;
InputLayoutDescX GetInputLayout() const;

static size_t GetPSOIdx(const PSOKey& Key)
{
size_t PSOIdx;
Expand Down
53 changes: 53 additions & 0 deletions PBR/interface/USD_Renderer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2023 Diligent Graphics LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* In no event and under no legal theory, whether in tort (including negligence),
* contract, or otherwise, unless required by applicable law (such as deliberate
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
* liable for any damages, including any direct, indirect, special, incidental,
* or consequential damages of any character arising as a result of this License or
* out of the use or inability to use the software (including but not limited to damages
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
* all other commercial damages or losses), even if such Contributor has been advised
* of the possibility of such damages.
*/

#pragma once

#include "PBR_Renderer.hpp"

namespace Diligent
{

/// Implementation of a GLTF PBR renderer
class USD_Renderer : public PBR_Renderer
{
public:
/// Initializes the renderer
USD_Renderer(IRenderDevice* pDevice,
IRenderStateCache* pStateCache,
IDeviceContext* pCtx,
const CreateInfo& CI);

IPipelineState* GetMeshEdgesPSO() const { return m_MeshEdgesPSO; }

private:
void CreatMeshEdgesPSO(IRenderDevice* pDevice, IRenderStateCache* pStateCache);

private:
RefCntAutoPtr<IPipelineState> m_MeshEdgesPSO;
};

} // namespace Diligent
Loading

0 comments on commit e31577b

Please sign in to comment.