Skip to content

Commit

Permalink
Refine work-graphs interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
StarsX committed Dec 21, 2024
1 parent 2d8ec0f commit 94d4505
Show file tree
Hide file tree
Showing 14 changed files with 376 additions and 184 deletions.
8 changes: 4 additions & 4 deletions XUSG-EZ/XUSG-EZ.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ namespace XUSG
uint32_t numResources, const ResourceView* pResourceViews, uint32_t space = 0) = 0;
virtual void SetGraphicsDescriptorTable(Shader::Stage stage, DescriptorType descriptorType, const DescriptorTable& descriptorTable, uint32_t space) = 0;
virtual void SetComputeDescriptorTable(DescriptorType descriptorType, const DescriptorTable& descriptorTable, uint32_t space) = 0;
virtual void SetGraphics32BitConstant(Shader::Stage stage, uint32_t srcData, uint32_t destOffsetIn32BitValues = 0) const = 0;
virtual void SetCompute32BitConstant(uint32_t srcData, uint32_t destOffsetIn32BitValues = 0) const = 0;
virtual void SetGraphics32BitConstants(Shader::Stage stage, uint32_t num32BitValuesToSet, const void* pSrcData, uint32_t destOffsetIn32BitValues = 0) const = 0;
virtual void SetCompute32BitConstants(uint32_t num32BitValuesToSet, const void* pSrcData, uint32_t destOffsetIn32BitValues = 0) const = 0;
virtual void SetGraphics32BitConstant(Shader::Stage stage, uint32_t srcData, uint32_t destOffsetIn32BitValues = 0) = 0;
virtual void SetCompute32BitConstant(uint32_t srcData, uint32_t destOffsetIn32BitValues = 0) = 0;
virtual void SetGraphics32BitConstants(Shader::Stage stage, uint32_t num32BitValuesToSet, const void* pSrcData, uint32_t destOffsetIn32BitValues = 0) = 0;
virtual void SetCompute32BitConstants(uint32_t num32BitValuesToSet, const void* pSrcData, uint32_t destOffsetIn32BitValues = 0) = 0;

virtual void IASetPrimitiveTopology(PrimitiveTopology primitiveTopology) = 0;
virtual void IASetIndexBuffer(const IndexBufferView& view) = 0;
Expand Down
106 changes: 65 additions & 41 deletions XUSG-EZ/XUSG-EZ_DX12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ EZ::CommandList_DX12::CommandList_DX12() :
m_clearRTVs(0),
m_graphicsSpaceToParamIndexMap(),
m_computeSpaceToParamIndexMap(),
m_graphicsSetDescriptorTables(),
m_graphicsConstantParamIndices(),
m_computeConstantParamIndex(0),
m_shaders()
Expand Down Expand Up @@ -483,34 +484,36 @@ void EZ::CommandList_DX12::SetResources(Shader::Stage stage, DescriptorType desc
void EZ::CommandList_DX12::SetGraphicsDescriptorTable(Shader::Stage stage, DescriptorType descriptorType,
const DescriptorTable& descriptorTable, uint32_t space)
{
XUSG::CommandList_DX12::SetGraphicsDescriptorTable(m_graphicsSpaceToParamIndexMap[stage][static_cast<uint32_t>(descriptorType)][space], descriptorTable);
// Defer setting the descriptor table after pipeline layout set
m_graphicsSetDescriptorTables.emplace_back(SetDescriptorTable{ stage, descriptorType, descriptorTable, space });
}

void EZ::CommandList_DX12::SetComputeDescriptorTable(DescriptorType descriptorType, const DescriptorTable& descriptorTable, uint32_t space)
{
XUSG::CommandList_DX12::SetComputeDescriptorTable(m_computeSpaceToParamIndexMap[static_cast<uint32_t>(descriptorType)][space], descriptorTable);
}

void EZ::CommandList_DX12::SetGraphics32BitConstant(Shader::Stage stage, uint32_t srcData, uint32_t destOffsetIn32BitValues) const
void EZ::CommandList_DX12::SetGraphics32BitConstant(Shader::Stage stage, uint32_t srcData, uint32_t destOffsetIn32BitValues)
{
// Defer setting the constant after pipeline layout set
assert(stage < Shader::Stage::NUM_GRAPHICS);
XUSG::CommandList_DX12::SetGraphics32BitConstant(m_graphicsConstantParamIndices[stage], srcData, destOffsetIn32BitValues);
m_graphicsSetSingle32BitConstants.emplace_back(Set32BitConstant{ stage, srcData, destOffsetIn32BitValues });
}

void EZ::CommandList_DX12::SetCompute32BitConstant(uint32_t srcData, uint32_t destOffsetIn32BitValues) const
void EZ::CommandList_DX12::SetCompute32BitConstant(uint32_t srcData, uint32_t destOffsetIn32BitValues)
{
XUSG::CommandList_DX12::SetCompute32BitConstant(m_computeConstantParamIndex, srcData, destOffsetIn32BitValues);
}

void EZ::CommandList_DX12::SetGraphics32BitConstants(Shader::Stage stage, uint32_t num32BitValuesToSet,
const void* pSrcData, uint32_t destOffsetIn32BitValues) const
const void* pSrcData, uint32_t destOffsetIn32BitValues)
{
// Defer setting the constants after pipeline layout set
assert(stage < Shader::Stage::NUM_GRAPHICS);
XUSG::CommandList_DX12::SetGraphics32BitConstants(m_graphicsConstantParamIndices[stage],
num32BitValuesToSet, pSrcData, destOffsetIn32BitValues);
m_graphicsSet32BitConstants.emplace_back(Set32BitConstants{ stage, num32BitValuesToSet, pSrcData, destOffsetIn32BitValues });
}

void EZ::CommandList_DX12::SetCompute32BitConstants(uint32_t num32BitValuesToSet, const void* pSrcData, uint32_t destOffsetIn32BitValues) const
void EZ::CommandList_DX12::SetCompute32BitConstants(uint32_t num32BitValuesToSet, const void* pSrcData, uint32_t destOffsetIn32BitValues)
{
XUSG::CommandList_DX12::SetCompute32BitConstants(m_computeConstantParamIndex, num32BitValuesToSet, pSrcData, destOffsetIn32BitValues);
}
Expand Down Expand Up @@ -1066,8 +1069,25 @@ void EZ::CommandList_DX12::predraw()
clearUAVsUint();
clearUAVsFloat();

// Set pipeline layout
XUSG::CommandList_DX12::SetGraphicsPipelineLayout(m_pipelineLayouts[GRAPHICS]);
// Create pipeline for dynamic states
assert(m_graphicsState);
if (m_isGraphicsDirty)
{
// Set pipeline layout
XUSG::CommandList_DX12::SetGraphicsPipelineLayout(m_pipelineLayouts[GRAPHICS]);

m_graphicsState->SetPipelineLayout(m_pipelineLayouts[GRAPHICS]);
const auto pipeline = m_graphicsState->GetPipeline(m_graphicsPipelineLib.get());
if (pipeline)
{
if (m_pipeline != pipeline)
{
XUSG::CommandList_DX12::SetPipelineState(pipeline);
m_pipeline = pipeline;
}
m_isGraphicsDirty = false;
}
}

// Set descriptor tables
for (uint8_t i = 0; i < Shader::Stage::NUM_GRAPHICS; ++i)
Expand Down Expand Up @@ -1100,22 +1120,26 @@ void EZ::CommandList_DX12::predraw()
}
}

// Create pipeline for dynamic states
assert(m_graphicsState);
if (m_isGraphicsDirty)
for (const auto& set : m_graphicsSetDescriptorTables)
{
m_graphicsState->SetPipelineLayout(m_pipelineLayouts[GRAPHICS]);
const auto pipeline = m_graphicsState->GetPipeline(m_graphicsPipelineLib.get());
if (pipeline)
{
if (m_pipeline != pipeline)
{
XUSG::CommandList_DX12::SetPipelineState(pipeline);
m_pipeline = pipeline;
}
m_isGraphicsDirty = false;
}
const auto& paramIdx = m_graphicsSpaceToParamIndexMap[set.Stage][static_cast<uint32_t>(set.Type)][set.Space];
XUSG::CommandList_DX12::SetGraphicsDescriptorTable(paramIdx, set.Table);
}
m_graphicsSetDescriptorTables.clear();

for (const auto& set : m_graphicsSetSingle32BitConstants)
{
const auto& paramIdx = m_graphicsConstantParamIndices[set.Stage];
XUSG::CommandList_DX12::SetGraphics32BitConstant(paramIdx, set.SrcData, set.Offset);
}
m_graphicsSetSingle32BitConstants.clear();

for (const auto& set : m_graphicsSet32BitConstants)
{
const auto& paramIdx = m_graphicsConstantParamIndices[set.Stage];
XUSG::CommandList_DX12::SetGraphics32BitConstants(paramIdx, set.Num32BitValues, set.pSrcData, set.Offset);
}
m_graphicsSet32BitConstants.clear();
}

void EZ::CommandList_DX12::predispatch()
Expand All @@ -1128,6 +1152,23 @@ void EZ::CommandList_DX12::predispatch()
clearUAVsUint();
clearUAVsFloat();

// Create pipeline for dynamic states
assert(m_computeState);
if (m_isComputeDirty)
{
m_computeState->SetPipelineLayout(m_pipelineLayouts[COMPUTE]);
const auto pipeline = m_computeState->GetPipeline(m_computePipelineLib.get());
if (pipeline)
{
if (m_pipeline != pipeline)
{
XUSG::CommandList_DX12::SetPipelineState(pipeline);
m_pipeline = pipeline;
}
m_isComputeDirty = false;
}
}

// Create and set sampler table
auto& samplerTable = m_samplerTables[Shader::Stage::CS];
if (samplerTable)
Expand All @@ -1154,23 +1195,6 @@ void EZ::CommandList_DX12::predispatch()
}
}
}

// Create pipeline for dynamic states
assert(m_computeState);
if (m_isComputeDirty)
{
m_computeState->SetPipelineLayout(m_pipelineLayouts[COMPUTE]);
const auto pipeline = m_computeState->GetPipeline(m_computePipelineLib.get());
if (pipeline)
{
if (m_pipeline != pipeline)
{
XUSG::CommandList_DX12::SetPipelineState(pipeline);
m_pipeline = pipeline;
}
m_isComputeDirty = false;
}
}
}

void EZ::CommandList_DX12::preexecuteIndirect(Resource* pArgumentBuffer, Resource* pCountBuffer)
Expand Down
35 changes: 31 additions & 4 deletions XUSG-EZ/XUSG-EZ_DX12.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ namespace XUSG
uint32_t numResources, const ResourceView* pResourceViews, uint32_t space = 0);
void SetGraphicsDescriptorTable(Shader::Stage stage, DescriptorType descriptorType, const DescriptorTable& descriptorTable, uint32_t space);
void SetComputeDescriptorTable(DescriptorType descriptorType, const DescriptorTable& descriptorTable, uint32_t space);
void SetGraphics32BitConstant(Shader::Stage stage, uint32_t srcData, uint32_t destOffsetIn32BitValues = 0) const;
void SetCompute32BitConstant(uint32_t srcData, uint32_t destOffsetIn32BitValues = 0) const;
void SetGraphics32BitConstants(Shader::Stage stage, uint32_t num32BitValuesToSet, const void* pSrcData, uint32_t destOffsetIn32BitValues = 0) const;
void SetCompute32BitConstants(uint32_t num32BitValuesToSet, const void* pSrcData, uint32_t destOffsetIn32BitValues = 0) const;
void SetGraphics32BitConstant(Shader::Stage stage, uint32_t srcData, uint32_t destOffsetIn32BitValues = 0);
void SetCompute32BitConstant(uint32_t srcData, uint32_t destOffsetIn32BitValues = 0);
void SetGraphics32BitConstants(Shader::Stage stage, uint32_t num32BitValuesToSet, const void* pSrcData, uint32_t destOffsetIn32BitValues = 0);
void SetCompute32BitConstants(uint32_t num32BitValuesToSet, const void* pSrcData, uint32_t destOffsetIn32BitValues = 0);
void IASetPrimitiveTopology(PrimitiveTopology primitiveTopology);
void IASetIndexBuffer(const IndexBufferView& view);
void IASetVertexBuffers(uint32_t startSlot, uint32_t numViews, const VertexBufferView* pViews);
Expand Down Expand Up @@ -279,6 +279,29 @@ namespace XUSG
std::vector<RectRange> Rects;
};

struct SetDescriptorTable
{
Shader::Stage Stage;
DescriptorType Type;
DescriptorTable Table;
uint32_t Space;
};

struct Set32BitConstant
{
Shader::Stage Stage;
uint32_t SrcData;
uint32_t Offset;
};

struct Set32BitConstants
{
Shader::Stage Stage;
uint32_t Num32BitValues;
const void* pSrcData;
uint32_t Offset;
};

static const uint8_t CbvSrvUavTypes = 3;

bool init(XUSG::CommandList* pCommandList, uint32_t samplerHeapSize, uint32_t cbvSrvUavHeapSize);
Expand Down Expand Up @@ -342,6 +365,10 @@ namespace XUSG
std::vector<uint32_t> m_graphicsSpaceToParamIndexMap[Shader::Stage::NUM_GRAPHICS][CbvSrvUavTypes];
std::vector<uint32_t> m_computeSpaceToParamIndexMap[CbvSrvUavTypes];

std::vector<SetDescriptorTable> m_graphicsSetDescriptorTables;
std::vector<Set32BitConstant> m_graphicsSetSingle32BitConstants;
std::vector<Set32BitConstants> m_graphicsSet32BitConstants;

uint32_t m_graphicsConstantParamIndices[Shader::Stage::NUM_GRAPHICS];
uint32_t m_computeConstantParamIndex;

Expand Down
4 changes: 1 addition & 3 deletions XUSG-EZ/XUSGUltimate-EZ.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ namespace XUSG
virtual void DSSetState(const Graphics::DepthStencil* pDepthStencil) = 0;
virtual void DSSetState(Graphics::DepthStencilPreset preset) = 0;
virtual void SetGraphicsShader(Shader::Stage stage, const Blob& shader) = 0;
virtual void SetMeshGraphicsShader(Shader::Stage stage, const Blob& shader) = 0;
virtual void SetMeshGraphics32BitConstant(Shader::Stage stage, uint32_t srcData, uint32_t destOffsetIn32BitValues = 0) const = 0;
virtual void SetMeshGraphics32BitConstants(Shader::Stage stage, uint32_t num32BitValuesToSet, const void* pSrcData, uint32_t destOffsetIn32BitValues = 0) const = 0;
virtual void MSSetGraphicsShader(Shader::Stage stage, const Blob& shader) = 0;
virtual void SetGraphicsNodeMask(uint32_t nodeMask) = 0;
virtual void DispatchMesh(uint32_t ThreadGroupCountX, uint32_t ThreadGroupCountY, uint32_t ThreadGroupCountZ) = 0;
virtual void DispatchMeshIndirect(const CommandLayout* pCommandlayout,
Expand Down
80 changes: 45 additions & 35 deletions XUSG-EZ/XUSGUltimate-EZ_DX12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,28 +242,14 @@ void EZ::CommandList_DX12::SetGraphicsShader(Shader::Stage stage, const Blob& sh
else XUSG::EZ::CommandList_DX12::SetGraphicsShader(stage, shader);
}

void EZ::CommandList_DX12::SetMeshGraphicsShader(Shader::Stage stage, const Blob& shader)
void EZ::CommandList_DX12::MSSetGraphicsShader(Shader::Stage stage, const Blob& shader)
{
assert(m_meshShaderState);
assert(stage == Shader::Stage::PS || stage == Shader::Stage::MS || stage == Shader::Stage::AS);
m_meshShaderState->SetShader(stage, shader);
m_isGraphicsDirty = true;
}

void EZ::CommandList_DX12::SetMeshGraphics32BitConstant(Shader::Stage stage, uint32_t srcData, uint32_t destOffsetIn32BitValues) const
{
const auto stageIdx = getShaderStageIndex(stage);
XUSG::CommandList_DX12::SetGraphics32BitConstant(m_meshShaderConstantParamIndices[stageIdx], srcData, destOffsetIn32BitValues);
}

void EZ::CommandList_DX12::SetMeshGraphics32BitConstants(Shader::Stage stage, uint32_t num32BitValuesToSet,
const void* pSrcData, uint32_t destOffsetIn32BitValues) const
{
const auto stageIdx = getShaderStageIndex(stage);
XUSG::CommandList_DX12::SetGraphics32BitConstants(m_meshShaderConstantParamIndices[stageIdx],
num32BitValuesToSet, pSrcData, destOffsetIn32BitValues);
}

void EZ::CommandList_DX12::SetGraphicsNodeMask(uint32_t nodeMask)
{
assert(m_meshShaderState);
Expand Down Expand Up @@ -581,8 +567,25 @@ void EZ::CommandList_DX12::predispatchMesh()
clearUAVsUint();
clearUAVsFloat();

// Set pipeline layout
XUSG::CommandList_DX12::SetGraphicsPipelineLayout(m_pipelineLayout);
// Create pipeline for dynamic states
assert(m_meshShaderState);
if (m_isGraphicsDirty)
{
// Set pipeline layout
XUSG::CommandList_DX12::SetGraphicsPipelineLayout(m_pipelineLayout);

m_meshShaderState->SetPipelineLayout(m_pipelineLayout);
const auto pipeline = m_meshShaderState->GetPipeline(m_meshShaderPipelineLib.get());
if (pipeline)
{
if (m_pipeline != pipeline)
{
XUSG::CommandList_DX12::SetPipelineState(pipeline);
m_pipeline = pipeline;
}
m_isGraphicsDirty = false;
}
}

// Set descriptor tables
for (uint8_t i = 0; i < NUM_STAGE; ++i)
Expand Down Expand Up @@ -617,22 +620,29 @@ void EZ::CommandList_DX12::predispatchMesh()
}
}

// Create pipeline for dynamic states
assert(m_meshShaderState);
if (m_isGraphicsDirty)
for (const auto& set : m_graphicsSetDescriptorTables)
{
m_meshShaderState->SetPipelineLayout(m_pipelineLayout);
const auto pipeline = m_meshShaderState->GetPipeline(m_meshShaderPipelineLib.get());
if (pipeline)
{
if (m_pipeline != pipeline)
{
XUSG::CommandList_DX12::SetPipelineState(pipeline);
m_pipeline = pipeline;
}
m_isGraphicsDirty = false;
}
const auto stageIdx = getShaderStageIndex(set.Stage);
const auto& paramIdx = m_meshShaderSpaceToParamIndexMap[stageIdx][static_cast<uint32_t>(set.Type)][set.Space];
XUSG::CommandList_DX12::SetGraphicsDescriptorTable(paramIdx, set.Table);
}
m_graphicsSetDescriptorTables.clear();

for (const auto& set : m_graphicsSetSingle32BitConstants)
{
const auto stageIdx = getShaderStageIndex(set.Stage);
const auto& paramIdx = m_meshShaderConstantParamIndices[stageIdx];
XUSG::CommandList_DX12::SetGraphics32BitConstant(paramIdx, set.SrcData, set.Offset);
}
m_graphicsSetSingle32BitConstants.clear();

for (const auto& set : m_graphicsSet32BitConstants)
{
const auto stageIdx = getShaderStageIndex(set.Stage);
const auto& paramIdx = m_meshShaderConstantParamIndices[stageIdx];
XUSG::CommandList_DX12::SetGraphics32BitConstants(paramIdx, set.Num32BitValues, set.pSrcData, set.Offset);
}
m_graphicsSet32BitConstants.clear();
}

void EZ::CommandList_DX12::predispatchGraph()
Expand All @@ -645,6 +655,9 @@ void EZ::CommandList_DX12::predispatchGraph()
clearUAVsUint();
clearUAVsFloat();

// Create pipeline for dynamic states
getWorkGraphPipeline();

// Create and set sampler table
auto& samplerTable = m_samplerTables[Shader::Stage::CS];
if (samplerTable)
Expand All @@ -671,9 +684,6 @@ void EZ::CommandList_DX12::predispatchGraph()
}
}
}

// Create pipeline for dynamic states
getWorkGraphPipeline();
}

void EZ::CommandList_DX12::getWorkGraphPipeline()
Expand Down Expand Up @@ -707,7 +717,7 @@ void EZ::CommandList_DX12::getWorkGraphPipeline()
assert(workGraph.NodeIndices.empty());

workGraph.Index = workGraphIndex;
workGraph.Identifier = GetDX12ProgramIdentifier(pipeline, workGraphName);
workGraph.Identifier = m_workGraphState->GetProgramIdentifier(workGraphName);

const auto numEntryPoints = m_workGraphState->GetNumEntrypoints(workGraph.Index);
workGraph.EntrypointIDs.resize(numEntryPoints);
Expand Down
4 changes: 1 addition & 3 deletions XUSG-EZ/XUSGUltimate-EZ_DX12.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ namespace XUSG
void DSSetState(const Graphics::DepthStencil* pDepthStencil);
void DSSetState(Graphics::DepthStencilPreset preset);
void SetGraphicsShader(Shader::Stage stage, const Blob& shader);
void SetMeshGraphicsShader(Shader::Stage stage, const Blob& shader);
void SetMeshGraphics32BitConstant(Shader::Stage stage, uint32_t srcData, uint32_t destOffsetIn32BitValues = 0) const;
void SetMeshGraphics32BitConstants(Shader::Stage stage, uint32_t num32BitValuesToSet, const void* pSrcData, uint32_t destOffsetIn32BitValues = 0) const;
void MSSetGraphicsShader(Shader::Stage stage, const Blob& shader);
void SetGraphicsNodeMask(uint32_t nodeMask);
void DispatchMesh(uint32_t ThreadGroupCountX, uint32_t ThreadGroupCountY, uint32_t ThreadGroupCountZ);
void DispatchMeshIndirect(const CommandLayout* pCommandlayout,
Expand Down
Loading

0 comments on commit 94d4505

Please sign in to comment.