mirror of
https://github.com/doitsujin/dxvk.git
synced 2025-03-14 22:29:15 +01:00
[d3d11] Optimize index buffer binding with offset
Do not rebind the buffer if only the offset changes. Instead, adjust StartIndexLocation in indexed draw calls. For indirect draws, this will be disabled on the fly. This may save a whole bunch of work in the backend, and reduces the number of commands being sent to the CS thread in the first place, which is why this optimization is not being done in the backend itself but rather on the client API side.
This commit is contained in:
parent
f869881f55
commit
b8bc36559d
@ -1459,6 +1459,7 @@ namespace dxvk {
|
|||||||
UINT StartIndexLocation,
|
UINT StartIndexLocation,
|
||||||
INT BaseVertexLocation) {
|
INT BaseVertexLocation) {
|
||||||
D3D10DeviceLock lock = LockContext();
|
D3D10DeviceLock lock = LockContext();
|
||||||
|
StartIndexLocation += m_state.ia.indexBuffer.firstIndex;
|
||||||
|
|
||||||
EmitCs([=] (DxvkContext* ctx) {
|
EmitCs([=] (DxvkContext* ctx) {
|
||||||
ctx->drawIndexed(
|
ctx->drawIndexed(
|
||||||
@ -1493,6 +1494,7 @@ namespace dxvk {
|
|||||||
INT BaseVertexLocation,
|
INT BaseVertexLocation,
|
||||||
UINT StartInstanceLocation) {
|
UINT StartInstanceLocation) {
|
||||||
D3D10DeviceLock lock = LockContext();
|
D3D10DeviceLock lock = LockContext();
|
||||||
|
StartIndexLocation += m_state.ia.indexBuffer.firstIndex;
|
||||||
|
|
||||||
EmitCs([=] (DxvkContext* ctx) {
|
EmitCs([=] (DxvkContext* ctx) {
|
||||||
ctx->drawIndexed(
|
ctx->drawIndexed(
|
||||||
@ -1667,6 +1669,12 @@ namespace dxvk {
|
|||||||
if (needsUpdate)
|
if (needsUpdate)
|
||||||
m_state.ia.indexBuffer.buffer = newBuffer;
|
m_state.ia.indexBuffer.buffer = newBuffer;
|
||||||
|
|
||||||
|
if (likely(m_state.ia.indexBuffer.optimized)) {
|
||||||
|
uint32_t shift = Format == DXGI_FORMAT_R16_UINT ? 1 : 2;
|
||||||
|
m_state.ia.indexBuffer.firstIndex = Offset >> shift;
|
||||||
|
Offset = 0;
|
||||||
|
}
|
||||||
|
|
||||||
needsUpdate |= m_state.ia.indexBuffer.offset != Offset
|
needsUpdate |= m_state.ia.indexBuffer.offset != Offset
|
||||||
|| m_state.ia.indexBuffer.format != Format;
|
|| m_state.ia.indexBuffer.format != Format;
|
||||||
|
|
||||||
@ -3377,7 +3385,13 @@ namespace dxvk {
|
|||||||
|
|
||||||
void D3D11DeviceContext::BindFramebuffer() {
|
void D3D11DeviceContext::BindFramebuffer() {
|
||||||
DxvkRenderTargets attachments;
|
DxvkRenderTargets attachments;
|
||||||
|
|
||||||
|
// Re-enable index buffer optimization here. Some games will
|
||||||
|
// use indirect draws for actual scene rendering, but then
|
||||||
|
// use direct draws for things like the user interface.
|
||||||
|
if (!m_state.ia.indexBuffer.optimized)
|
||||||
|
SetIndexBufferOptimized(true);
|
||||||
|
|
||||||
// D3D11 doesn't have the concept of a framebuffer object,
|
// D3D11 doesn't have the concept of a framebuffer object,
|
||||||
// so we'll just create a new one every time the render
|
// so we'll just create a new one every time the render
|
||||||
// target bindings are updated. Set up the attachments.
|
// target bindings are updated. Set up the attachments.
|
||||||
@ -3572,12 +3586,39 @@ namespace dxvk {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void D3D11DeviceContext::SetIndexBufferOptimized(
|
||||||
|
BOOL Enable) {
|
||||||
|
uint32_t shift = m_state.ia.indexBuffer.format == DXGI_FORMAT_R16_UINT ? 1 : 2;
|
||||||
|
|
||||||
|
if (Enable) {
|
||||||
|
m_state.ia.indexBuffer.firstIndex = m_state.ia.indexBuffer.offset >> shift;
|
||||||
|
m_state.ia.indexBuffer.offset = 0;
|
||||||
|
} else {
|
||||||
|
m_state.ia.indexBuffer.offset += m_state.ia.indexBuffer.firstIndex << shift;
|
||||||
|
m_state.ia.indexBuffer.firstIndex = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_state.ia.indexBuffer.optimized = Enable;
|
||||||
|
|
||||||
|
BindIndexBuffer(
|
||||||
|
m_state.ia.indexBuffer.buffer.ptr(),
|
||||||
|
m_state.ia.indexBuffer.offset,
|
||||||
|
m_state.ia.indexBuffer.format);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void D3D11DeviceContext::SetDrawBuffers(
|
void D3D11DeviceContext::SetDrawBuffers(
|
||||||
ID3D11Buffer* pBufferForArgs,
|
ID3D11Buffer* pBufferForArgs,
|
||||||
ID3D11Buffer* pBufferForCount) {
|
ID3D11Buffer* pBufferForCount) {
|
||||||
auto argBuffer = static_cast<D3D11Buffer*>(pBufferForArgs);
|
auto argBuffer = static_cast<D3D11Buffer*>(pBufferForArgs);
|
||||||
auto cntBuffer = static_cast<D3D11Buffer*>(pBufferForCount);
|
auto cntBuffer = static_cast<D3D11Buffer*>(pBufferForCount);
|
||||||
|
|
||||||
|
// Bind index buffer with the actual offset since otherwise the
|
||||||
|
// first index members of the indirect draw structures would be
|
||||||
|
// incorrect.
|
||||||
|
if (unlikely(m_state.ia.indexBuffer.optimized))
|
||||||
|
SetIndexBufferOptimized(false);
|
||||||
|
|
||||||
if (m_state.id.argBuffer != argBuffer
|
if (m_state.id.argBuffer != argBuffer
|
||||||
|| m_state.id.cntBuffer != cntBuffer) {
|
|| m_state.id.cntBuffer != cntBuffer) {
|
||||||
m_state.id.argBuffer = argBuffer;
|
m_state.id.argBuffer = argBuffer;
|
||||||
|
@ -786,6 +786,9 @@ namespace dxvk {
|
|||||||
ID3D11Resource* pResource,
|
ID3D11Resource* pResource,
|
||||||
UINT Subresource);
|
UINT Subresource);
|
||||||
|
|
||||||
|
void SetIndexBufferOptimized(
|
||||||
|
BOOL Enable);
|
||||||
|
|
||||||
void SetDrawBuffers(
|
void SetDrawBuffers(
|
||||||
ID3D11Buffer* pBufferForArgs,
|
ID3D11Buffer* pBufferForArgs,
|
||||||
ID3D11Buffer* pBufferForCount);
|
ID3D11Buffer* pBufferForCount);
|
||||||
|
@ -103,6 +103,8 @@ namespace dxvk {
|
|||||||
Com<D3D11Buffer> buffer = nullptr;
|
Com<D3D11Buffer> buffer = nullptr;
|
||||||
UINT offset = 0;
|
UINT offset = 0;
|
||||||
DXGI_FORMAT format = DXGI_FORMAT_UNKNOWN;
|
DXGI_FORMAT format = DXGI_FORMAT_UNKNOWN;
|
||||||
|
UINT firstIndex = 0;
|
||||||
|
BOOL optimized = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user