Skip to content

Commit

Permalink
[D3D12] Fix crash during descriptor cache flush for PSO without descr…
Browse files Browse the repository at this point in the history
…iptor heap.

D3D12CommandContext::PrepareStagingDescriptorHeaps() must be called for each PSO binding, not just for those with resource bindings.
Otherwise, the layout information is not updated and the next call to any of the Flush*StagingDescriptorTables() functions can crash the D3D runtime.
  • Loading branch information
LukasBanana committed Jun 29, 2024
1 parent e4eb957 commit ff24a98
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
7 changes: 6 additions & 1 deletion sources/Renderer/Direct3D12/Command/D3D12CommandBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,14 +703,19 @@ void D3D12CommandBuffer::SetPipelineState(PipelineState& pipelineState)
/* Keep reference to pipeline layout */
boundPipelineLayout_ = pipelineStateD3D.GetPipelineLayout();

/* Prepare staging descriptor heaps for bound pipeline layout */
if (boundPipelineLayout_ != nullptr)
{
/* Prepare staging descriptor heaps for bound pipeline layout */
commandContext_.PrepareStagingDescriptorHeaps(
boundPipelineLayout_->GetDescriptorHeapSetLayout(),
boundPipelineLayout_->GetRootParameterIndices()
);
}
else
{
/* Reset staging descriptor layout to avoid undefined behavior in next Flush*StagingDescriptorTables() call */
commandContext_.PrepareStagingDescriptorHeaps({}, {});
}
}

void D3D12CommandBuffer::SetBlendFactor(const float color[4])
Expand Down
30 changes: 18 additions & 12 deletions sources/Renderer/Direct3D12/Command/D3D12CommandContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,19 +368,25 @@ void D3D12CommandContext::PrepareStagingDescriptorHeaps(
stagingDescriptorSetLayout_ = layout;
stagingDescriptorIndices_ = indices;

/* Bind shader-visible descriptor heaps */
ID3D12DescriptorHeap* const stagingDescriptorHeaps[2] =
if (stagingDescriptorSetLayout_.numHeapResourceViews > 0 ||
stagingDescriptorSetLayout_.numHeapSamplers > 0 ||
stagingDescriptorSetLayout_.numResourceViews > 0 ||
stagingDescriptorSetLayout_.numSamplers > 0)
{
stagingDescriptorPools_[currentAllocatorIndex_][0].GetDescriptorHeap(),
stagingDescriptorPools_[currentAllocatorIndex_][1].GetDescriptorHeap()
};
SetDescriptorHeaps(2, stagingDescriptorHeaps);

/* Reset descriptor cache for dynamic descriptors */
descriptorCaches_[currentAllocatorIndex_].Reset(
stagingDescriptorSetLayout_.numResourceViews,
stagingDescriptorSetLayout_.numSamplers
);
/* Bind shader-visible descriptor heaps */
ID3D12DescriptorHeap* const stagingDescriptorHeaps[2] =
{
stagingDescriptorPools_[currentAllocatorIndex_][0].GetDescriptorHeap(),
stagingDescriptorPools_[currentAllocatorIndex_][1].GetDescriptorHeap()
};
SetDescriptorHeaps(2, stagingDescriptorHeaps);

/* Reset descriptor cache for dynamic descriptors */
descriptorCaches_[currentAllocatorIndex_].Reset(
stagingDescriptorSetLayout_.numResourceViews,
stagingDescriptorSetLayout_.numSamplers
);
}
}

void D3D12CommandContext::SetGraphicsConstant(UINT parameterIndex, D3D12Constant value, UINT offset)
Expand Down

0 comments on commit ff24a98

Please sign in to comment.