Skip to content

Commit

Permalink
[d3d11] Cache raw mapped pointer rather than allocation object
Browse files Browse the repository at this point in the history
Reduces ref counting overhead again and matches previous behaviour.

We should probably do something about the possible case of deferred context
execution with MAP_WRITE_DISCARD followed by MAP_WRITE_NO_OVERWRITE on the
immediate context, but we haven't seen a game rely on this yet.
  • Loading branch information
doitsujin committed Sep 26, 2024
1 parent 0134a4e commit 6ec91e8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/d3d11/d3d11_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace dxvk {
info.usage |= VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;

m_buffer = m_parent->GetDXVKDevice()->importBuffer(info, importInfo, GetMemoryFlags());
m_allocation = m_buffer->getAllocation();
m_mapPtr = m_buffer->mapPtr(0);

m_mapMode = DetermineMapMode(m_buffer->memFlags());
} else if (!(pDesc->MiscFlags & D3D11_RESOURCE_MISC_TILE_POOL)) {
Expand All @@ -114,12 +114,12 @@ namespace dxvk {
// Create the buffer and set the entire buffer slice as mapped,
// so that we only have to update it when invalidating the buffer
m_buffer = m_parent->GetDXVKDevice()->createBuffer(info, memoryFlags);
m_allocation = m_buffer->getAllocation();
m_mapPtr = m_buffer->mapPtr(0);
} else {
m_sparseAllocator = m_parent->GetDXVKDevice()->createSparsePageAllocator();
m_sparseAllocator->setCapacity(info.size / SparseMemoryPageSize);

m_allocation = nullptr;
m_mapPtr = nullptr;
m_mapMode = D3D11_COMMON_BUFFER_MAP_MODE_NONE;
}

Expand Down
12 changes: 7 additions & 5 deletions src/d3d11/d3d11_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ namespace dxvk {
}

Rc<DxvkResourceAllocation> DiscardSlice(DxvkLocalAllocationCache* cache) {
m_allocation = m_buffer->allocateSlice(cache);
return m_allocation;
auto allocation = m_buffer->allocateSlice(cache);
m_mapPtr = allocation->mapPtr();
return allocation;
}

Rc<DxvkResourceAllocation> GetMappedSlice() const {
return m_allocation;
void* GetMapPtr() const {
return m_mapPtr;
}

D3D10Buffer* GetD3D10Iface() {
Expand Down Expand Up @@ -184,9 +185,10 @@ namespace dxvk {
Rc<DxvkBuffer> m_buffer;
Rc<DxvkBuffer> m_soCounter;
Rc<DxvkSparsePageAllocator> m_sparseAllocator;
Rc<DxvkResourceAllocation> m_allocation;
uint64_t m_seq = 0ull;

void* m_mapPtr = nullptr;

D3D11DXGIResource m_resource;
D3D10Buffer m_d3d10;

Expand Down
11 changes: 5 additions & 6 deletions src/d3d11/d3d11_context_imm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ namespace dxvk {
} else if (likely(MapType == D3D11_MAP_WRITE_NO_OVERWRITE)) {
// Put this on a fast path without any extra checks since it's
// a somewhat desired method to partially update large buffers
pMappedResource->pData = pResource->GetMappedSlice()->mapPtr();
pMappedResource->pData = pResource->GetMapPtr();
pMappedResource->RowPitch = bufferSize;
pMappedResource->DepthPitch = bufferSize;
return S_OK;
Expand Down Expand Up @@ -376,10 +376,9 @@ namespace dxvk {
}

if (doInvalidatePreserve) {
auto srcSlice = pResource->GetMappedSlice();
auto dstSlice = pResource->DiscardSlice(nullptr);
auto srcPtr = pResource->GetMapPtr();

auto srcPtr = srcSlice->mapPtr();
auto dstSlice = pResource->DiscardSlice(nullptr);
auto dstPtr = dstSlice->mapPtr();

EmitCs([
Expand All @@ -398,7 +397,7 @@ namespace dxvk {
if (!WaitForResource(buffer, sequenceNumber, MapType, MapFlags))
return DXGI_ERROR_WAS_STILL_DRAWING;

pMappedResource->pData = pResource->GetMappedSlice()->mapPtr();
pMappedResource->pData = pResource->GetMapPtr();
pMappedResource->RowPitch = bufferSize;
pMappedResource->DepthPitch = bufferSize;
return S_OK;
Expand Down Expand Up @@ -713,7 +712,7 @@ namespace dxvk {
ctx->invalidateBuffer(cBuffer, std::move(cBufferSlice));
});
} else {
mapPtr = pDstBuffer->GetMappedSlice()->mapPtr();
mapPtr = pDstBuffer->GetMapPtr();
}

std::memcpy(reinterpret_cast<char*>(mapPtr) + Offset, pSrcData, Length);
Expand Down
2 changes: 1 addition & 1 deletion src/d3d11/d3d11_initializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,4 @@ namespace dxvk {
}
}

}
}

0 comments on commit 6ec91e8

Please sign in to comment.