Skip to content

Commit

Permalink
[d3d11] Use resource cookies for draw buffer tracking
Browse files Browse the repository at this point in the history
Avoids keeping draw buffers alive when the app stops using indirect
draws. Unlikely to have caused issues in practice, but draw buffers
are not part of the API state to begin with.
  • Loading branch information
doitsujin committed Feb 16, 2025
1 parent 3754e64 commit 5a5541f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
20 changes: 12 additions & 8 deletions src/d3d11/d3d11_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4644,10 +4644,6 @@ namespace dxvk {
ApplyRasterizerSampleCount();
ApplyViewportState();

BindDrawBuffers(
m_state.id.argBuffer.ptr(),
m_state.id.cntBuffer.ptr());

BindIndexBuffer(
m_state.ia.indexBuffer.buffer.ptr(),
m_state.ia.indexBuffer.offset,
Expand Down Expand Up @@ -4686,6 +4682,11 @@ namespace dxvk {
RestoreSamplers<DxbcProgramType::GeometryShader>();
RestoreSamplers<DxbcProgramType::PixelShader>();
RestoreSamplers<DxbcProgramType::ComputeShader>();

// Draw buffer bindings aren't persistent at the API level, and
// we can't meaningfully track them. Just reset this state here
// and reapply on the next indirect draw.
SetDrawBuffers(nullptr, nullptr);
}


Expand Down Expand Up @@ -5012,10 +5013,13 @@ namespace dxvk {
auto argBuffer = static_cast<D3D11Buffer*>(pBufferForArgs);
auto cntBuffer = static_cast<D3D11Buffer*>(pBufferForCount);

if (m_state.id.argBuffer != argBuffer
|| m_state.id.cntBuffer != cntBuffer) {
m_state.id.argBuffer = argBuffer;
m_state.id.cntBuffer = cntBuffer;
auto argBufferCookie = argBuffer ? argBuffer->GetCookie() : 0u;
auto cntBufferCookie = cntBuffer ? cntBuffer->GetCookie() : 0u;

if (m_state.id.argBufferCookie != argBufferCookie
|| m_state.id.cntBufferCookie != cntBufferCookie) {
m_state.id.argBufferCookie = argBufferCookie;
m_state.id.cntBufferCookie = cntBufferCookie;

BindDrawBuffers(argBuffer, cntBuffer);
}
Expand Down
5 changes: 5 additions & 0 deletions src/d3d11/d3d11_context_imm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,11 @@ namespace dxvk {
Rc<DxvkLatencyTracker> LatencyTracker) {
D3D10DeviceLock lock = LockContext();

// Don't keep draw buffers alive indefinitely. This cannot be
// done in ExecuteFlush because command recording itself might
// flush, so no state changes are allowed to happen there.
SetDrawBuffers(nullptr, nullptr);

EmitCs<false>([
cTracker = std::move(LatencyTracker)
] (DxvkContext* ctx) {
Expand Down
10 changes: 5 additions & 5 deletions src/d3d11/d3d11_context_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,12 @@ namespace dxvk {
* argument and draw count buffer.
*/
struct D3D11ContextStateID {
Com<D3D11Buffer, false> argBuffer = nullptr;
Com<D3D11Buffer, false> cntBuffer = nullptr;
uint64_t argBufferCookie = 0u;
uint64_t cntBufferCookie = 0u;

void reset() {
argBuffer = nullptr;
cntBuffer = nullptr;
argBufferCookie = 0u;
cntBufferCookie = 0u;
}
};

Expand Down Expand Up @@ -347,4 +347,4 @@ namespace dxvk {
uint32_t soCount;
};

}
}

0 comments on commit 5a5541f

Please sign in to comment.