Skip to content

Commit

Permalink
VectorFieldRenderer: enabled async shader compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Aug 12, 2024
1 parent 02fda23 commit ee4c1f7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
6 changes: 5 additions & 1 deletion Components/interface/VectorFieldRenderer.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Diligent Graphics LLC
* Copyright 2023-2024 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.
Expand Down Expand Up @@ -62,6 +62,9 @@ class VectorFieldRenderer
/// with the SHADER_COMPILE_FLAG_PACK_MATRIX_ROW_MAJOR flag and
/// use the row-major layout.
bool PackMatrixRowMajor = false;

/// Whether to compile shaders asynchronously.
bool AsyncShaders = false;
};
VectorFieldRenderer(const CreateInfo& CI);

Expand Down Expand Up @@ -127,6 +130,7 @@ class VectorFieldRenderer
const TEXTURE_FORMAT m_DSVFormat;
const std::string m_PSMainSource;
const bool m_PackMatrixRowMajor;
const bool m_AsyncShaders;

std::unordered_map<PSOKey, RefCntAutoPtr<IPipelineState>, PSOKey::Hasher> m_PSOs;

Expand Down
38 changes: 22 additions & 16 deletions Components/src/VectorFieldRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ VectorFieldRenderer::VectorFieldRenderer(const CreateInfo& CI) :
m_RTVFormats{CI.RTVFormats, CI.RTVFormats + CI.NumRenderTargets},
m_DSVFormat{CI.DSVFormat},
m_PSMainSource{CI.PSMainSource != nullptr ? CI.PSMainSource : ""},
m_PackMatrixRowMajor{CI.PackMatrixRowMajor}
m_PackMatrixRowMajor{CI.PackMatrixRowMajor},
m_AsyncShaders{CI.AsyncShaders}
{
DEV_CHECK_ERR(m_pDevice != nullptr, "Device must not be null");

Expand Down Expand Up @@ -92,19 +93,18 @@ IPipelineState* VectorFieldRenderer::GetPSO(const PSOKey& Key)

RenderDeviceWithCache_N Device{m_pDevice, m_pStateCache};

std::string PSMainSource = m_PSMainSource;
if (PSMainSource.empty())
PSMainSource = DefaultPSMain;

RefCntAutoPtr<IShaderSourceInputStreamFactory> pMemorySourceFactory =
CreateMemoryShaderSourceFactory({MemoryShaderSourceFileInfo{"PSMainGenerated.generated", PSMainSource}});
CreateMemoryShaderSourceFactory({MemoryShaderSourceFileInfo{"PSMainGenerated.generated", !m_PSMainSource.empty() ? m_PSMainSource.c_str() : DefaultPSMain}});
RefCntAutoPtr<IShaderSourceInputStreamFactory> pShaderSourceFactory =
CreateCompoundShaderSourceFactory({&DiligentFXShaderSourceStreamFactory::GetInstance(), pMemorySourceFactory});

ShaderCreateInfo ShaderCI;
ShaderCI.SourceLanguage = SHADER_SOURCE_LANGUAGE_HLSL;
ShaderCI.pShaderSourceStreamFactory = pShaderSourceFactory;
ShaderCI.CompileFlags = m_PackMatrixRowMajor ? SHADER_COMPILE_FLAG_PACK_MATRIX_ROW_MAJOR : SHADER_COMPILE_FLAG_NONE;
if (m_PackMatrixRowMajor)
ShaderCI.CompileFlags |= SHADER_COMPILE_FLAG_PACK_MATRIX_ROW_MAJOR;
if (m_AsyncShaders)
ShaderCI.CompileFlags |= SHADER_COMPILE_FLAG_ASYNCHRONOUS;

ShaderMacroHelper Macros;
Macros.Add("CONVERT_OUTPUT_TO_SRGB", Key.ConvertOutputToSRGB);
Expand Down Expand Up @@ -141,7 +141,7 @@ IPipelineState* VectorFieldRenderer::GetPSO(const PSOKey& Key)

PipelineResourceLayoutDescX ResourceLauout;
ResourceLauout
.SetDefaultVariableType(SHADER_RESOURCE_VARIABLE_TYPE_STATIC)
.SetDefaultVariableType(SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE)
.AddVariable(SHADER_TYPE_VERTEX, "g_tex2DVectorField", SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC)
.AddImmutableSampler(SHADER_TYPE_VERTEX, "g_tex2DVectorField", Sam_LinearClamp);

Expand All @@ -156,20 +156,15 @@ IPipelineState* VectorFieldRenderer::GetPSO(const PSOKey& Key)
for (auto RTVFormat : m_RTVFormats)
PsoCI.AddRenderTarget(RTVFormat);

if (m_AsyncShaders)
PsoCI.Flags |= PSO_CREATE_FLAG_ASYNCHRONOUS;

auto PSO = Device.CreateGraphicsPipelineState(PsoCI);
if (!PSO)
{
UNEXPECTED("Failed to create vector field PSO");
return nullptr;
}
PSO->GetStaticVariableByName(SHADER_TYPE_VERTEX, "cbAttribs")->Set(m_RenderAttribsCB);

if (!m_SRB)
{
PSO->CreateShaderResourceBinding(&m_SRB, true);
m_pVectorFieldVar = m_SRB->GetVariableByName(SHADER_TYPE_VERTEX, "g_tex2DVectorField");
VERIFY_EXPR(m_pVectorFieldVar != nullptr);
}

m_PSOs.emplace(Key, PSO);
return PSO;
Expand Down Expand Up @@ -199,6 +194,17 @@ void VectorFieldRenderer::Render(const RenderAttribs& Attribs)
return;
}

if (pPSO->GetStatus() != PIPELINE_STATE_STATUS_READY)
return;

if (!m_SRB)
{
pPSO->CreateShaderResourceBinding(&m_SRB, true);
m_SRB->GetVariableByName(SHADER_TYPE_VERTEX, "cbAttribs")->Set(m_RenderAttribsCB);
m_pVectorFieldVar = m_SRB->GetVariableByName(SHADER_TYPE_VERTEX, "g_tex2DVectorField");
VERIFY_EXPR(m_pVectorFieldVar != nullptr);
}

m_pVectorFieldVar->Set(Attribs.pVectorField);

{
Expand Down
4 changes: 3 additions & 1 deletion Hydrogent/src/Tasks/HnPostProcessTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,14 +470,16 @@ void HnPostProcessTask::CopyFrameTechnique::PrepareSRB(Uint32 FrameIdx)

void HnPostProcessTask::CreateVectorFieldRenderer(TEXTURE_FORMAT RTVFormat)
{
HnRenderDelegate* RenderDelegate = static_cast<HnRenderDelegate*>(m_RenderIndex->GetRenderDelegate());
HnRenderDelegate* RenderDelegate = static_cast<HnRenderDelegate*>(m_RenderIndex->GetRenderDelegate());
const HnRenderParam* pRenderParam = static_cast<const HnRenderParam*>(RenderDelegate->GetRenderParam());

VectorFieldRenderer::CreateInfo CI;
CI.pDevice = RenderDelegate->GetDevice();
CI.pStateCache = RenderDelegate->GetRenderStateCache();
CI.NumRenderTargets = 1;
CI.RTVFormats[0] = RTVFormat;
CI.PackMatrixRowMajor = true;
CI.AsyncShaders = pRenderParam->GetAsyncShaderCompilation();

m_VectorFieldRenderer = std::make_unique<VectorFieldRenderer>(CI);
}
Expand Down

0 comments on commit ee4c1f7

Please sign in to comment.