From 6f6e75b4b8811a7a4e973e422941721246f088e8 Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Sun, 22 Sep 2024 12:23:09 +0200 Subject: [PATCH] [d3d9] Use DxvkBufferAllocation where appropriate --- src/d3d9/d3d9_common_buffer.cpp | 2 +- src/d3d9/d3d9_common_buffer.h | 16 ++++++---------- src/d3d9/d3d9_constant_buffer.cpp | 30 +++++++++++++++--------------- src/d3d9/d3d9_constant_buffer.h | 4 ++-- src/d3d9/d3d9_device.cpp | 30 +++++++++++++++--------------- 5 files changed, 39 insertions(+), 43 deletions(-) diff --git a/src/d3d9/d3d9_common_buffer.cpp b/src/d3d9/d3d9_common_buffer.cpp index 7b5f60d5..a12d7b86 100644 --- a/src/d3d9/d3d9_common_buffer.cpp +++ b/src/d3d9/d3d9_common_buffer.cpp @@ -14,7 +14,7 @@ namespace dxvk { if (m_mapMode == D3D9_COMMON_BUFFER_MAP_MODE_BUFFER) m_stagingBuffer = CreateStagingBuffer(); - m_sliceHandle = GetMapBuffer()->getSliceHandle(); + m_allocation = GetMapBuffer()->getAllocation(); if (m_desc.Pool != D3DPOOL_DEFAULT) m_dirtyRange = D3D9Range(0, m_desc.Size); diff --git a/src/d3d9/d3d9_common_buffer.h b/src/d3d9/d3d9_common_buffer.h index 6c648f96..c0ffc324 100644 --- a/src/d3d9/d3d9_common_buffer.h +++ b/src/d3d9/d3d9_common_buffer.h @@ -133,17 +133,13 @@ namespace dxvk { return DxvkBufferSlice(); } - inline DxvkBufferSliceHandle AllocMapSlice() { - return GetMapBuffer()->allocSlice(); + inline DxvkBufferAllocation DiscardMapSlice() { + m_allocation = GetMapBuffer()->allocateSlice(); + return m_allocation; } - inline DxvkBufferSliceHandle DiscardMapSlice() { - m_sliceHandle = GetMapBuffer()->allocSlice(); - return m_sliceHandle; - } - - inline DxvkBufferSliceHandle GetMappedSlice() const { - return m_sliceHandle; + inline DxvkBufferAllocation GetMappedSlice() const { + return m_allocation; } inline DWORD GetMapFlags() const { return m_mapFlags; } @@ -240,7 +236,7 @@ namespace dxvk { Rc m_buffer; Rc m_stagingBuffer; - DxvkBufferSliceHandle m_sliceHandle; + DxvkBufferAllocation m_allocation; D3D9Range m_dirtyRange; diff --git a/src/d3d9/d3d9_constant_buffer.cpp b/src/d3d9/d3d9_constant_buffer.cpp index 5f6bdf0f..aaedbbc3 100644 --- a/src/d3d9/d3d9_constant_buffer.cpp +++ b/src/d3d9/d3d9_constant_buffer.cpp @@ -42,22 +42,20 @@ namespace dxvk { void* D3D9ConstantBuffer::Alloc(VkDeviceSize size) { - if (unlikely(m_buffer == nullptr)) { - this->createBuffer(); - m_slice = m_buffer->getSliceHandle(); - } + if (unlikely(m_buffer == nullptr)) + m_slice = this->createBuffer(); size = align(size, m_align); if (unlikely(m_offset + size > m_size)) { - m_slice = m_buffer->allocSlice(); + m_slice = m_buffer->allocateSlice(); m_offset = 0; m_device->EmitCs([ cBuffer = m_buffer, cSlice = m_slice - ] (DxvkContext* ctx) { - ctx->invalidateBuffer(cBuffer, cSlice); + ] (DxvkContext* ctx) mutable { + ctx->invalidateBuffer(cBuffer, std::move(cSlice)); }); } @@ -70,7 +68,7 @@ namespace dxvk { ctx->bindUniformBufferRange(cStages, cBinding, cOffset, cLength); }); - void* mapPtr = reinterpret_cast(m_slice.mapPtr) + m_offset; + void* mapPtr = reinterpret_cast(m_slice.mapPtr()) + m_offset; m_offset += size; return mapPtr; } @@ -78,22 +76,22 @@ namespace dxvk { void* D3D9ConstantBuffer::AllocSlice() { if (unlikely(m_buffer == nullptr)) - this->createBuffer(); - - m_slice = m_buffer->allocSlice(); + m_slice = this->createBuffer(); + else + m_slice = m_buffer->allocateSlice(); m_device->EmitCs([ cBuffer = m_buffer, cSlice = m_slice - ] (DxvkContext* ctx) { - ctx->invalidateBuffer(cBuffer, cSlice); + ] (DxvkContext* ctx) mutable { + ctx->invalidateBuffer(cBuffer, std::move(cSlice)); }); - return m_slice.mapPtr; + return m_slice.mapPtr(); } - void D3D9ConstantBuffer::createBuffer() { + DxvkBufferAllocation D3D9ConstantBuffer::createBuffer() { auto options = m_device->GetOptions(); // Buffer usage and access flags don't make much of a difference @@ -125,6 +123,8 @@ namespace dxvk { ] (DxvkContext* ctx) mutable { ctx->bindUniformBuffer(cStages, cBinding, std::move(cSlice)); }); + + return m_buffer->getAllocation(); } diff --git a/src/d3d9/d3d9_constant_buffer.h b/src/d3d9/d3d9_constant_buffer.h index 469e7a01..9cc2836f 100644 --- a/src/d3d9/d3d9_constant_buffer.h +++ b/src/d3d9/d3d9_constant_buffer.h @@ -74,9 +74,9 @@ namespace dxvk { VkDeviceSize m_offset = 0ull; Rc m_buffer = nullptr; - DxvkBufferSliceHandle m_slice = { }; + DxvkBufferAllocation m_slice = { }; - void createBuffer(); + DxvkBufferAllocation createBuffer(); VkDeviceSize getAlignment(const Rc& device) const; diff --git a/src/d3d9/d3d9_device.cpp b/src/d3d9/d3d9_device.cpp index d25cb388..fcd1af47 100644 --- a/src/d3d9/d3d9_device.cpp +++ b/src/d3d9/d3d9_device.cpp @@ -4386,16 +4386,16 @@ namespace dxvk { VkDeviceSize alignedSize = align(size, CACHE_LINE_SIZE); if (unlikely(m_upBufferOffset + alignedSize > UPBufferSize)) { - auto sliceHandle = m_upBuffer->allocSlice(); + auto slice = m_upBuffer->allocateSlice(); m_upBufferOffset = 0; - m_upBufferMapPtr = sliceHandle.mapPtr; + m_upBufferMapPtr = slice.mapPtr(); EmitCs([ cBuffer = m_upBuffer, - cSlice = sliceHandle - ] (DxvkContext* ctx) { - ctx->invalidateBuffer(cBuffer, cSlice); + cSlice = std::move(slice) + ] (DxvkContext* ctx) mutable { + ctx->invalidateBuffer(cBuffer, std::move(cSlice)); }); } @@ -5128,19 +5128,20 @@ namespace dxvk { Rc mappingBuffer = pResource->GetBuffer(); - DxvkBufferSliceHandle physSlice; + uint8_t* data = nullptr; if ((Flags & D3DLOCK_DISCARD) && (directMapping || needsReadback)) { // Allocate a new backing slice for the buffer and set // it as the 'new' mapped slice. This assumes that the // only way to invalidate a buffer is by mapping it. - physSlice = pResource->DiscardMapSlice(); + auto bufferSlice = pResource->DiscardMapSlice(); + data = reinterpret_cast(bufferSlice.mapPtr()); EmitCs([ cBuffer = std::move(mappingBuffer), - cBufferSlice = physSlice - ] (DxvkContext* ctx) { - ctx->invalidateBuffer(cBuffer, cBufferSlice); + cBufferSlice = std::move(bufferSlice) + ] (DxvkContext* ctx) mutable { + ctx->invalidateBuffer(cBuffer, std::move(cBufferSlice)); }); pResource->SetNeedsReadback(false); @@ -5149,7 +5150,7 @@ namespace dxvk { // Use map pointer from previous map operation. This // way we don't have to synchronize with the CS thread // if the map mode is D3DLOCK_NOOVERWRITE. - physSlice = pResource->GetMappedSlice(); + data = reinterpret_cast(pResource->GetMappedSlice().mapPtr()); const bool needsReadback = pResource->NeedsReadback(); const bool readOnly = Flags & D3DLOCK_READONLY; @@ -5166,7 +5167,6 @@ namespace dxvk { } } - uint8_t* data = reinterpret_cast(physSlice.mapPtr); // The offset/size is not clamped to or affected by the desc size. data += OffsetToLock; @@ -5197,7 +5197,7 @@ namespace dxvk { D3D9Range& range = pResource->DirtyRange(); D3D9BufferSlice slice = AllocStagingBuffer(range.max - range.min); - void* srcData = reinterpret_cast(srcSlice.mapPtr) + range.min; + void* srcData = reinterpret_cast(srcSlice.mapPtr()) + range.min; memcpy(slice.mapPtr, srcData, range.max - range.min); EmitCs([ @@ -5379,7 +5379,7 @@ namespace dxvk { if (likely(copy.copyBufferLength != 0)) { const auto* vbo = GetCommonBuffer(m_state.vertexBuffers[i].vertexBuffer); uint8_t* data = reinterpret_cast(upSlice.mapPtr) + copy.dstOffset; - const uint8_t* src = reinterpret_cast(vbo->GetMappedSlice().mapPtr) + copy.srcOffset; + const uint8_t* src = reinterpret_cast(vbo->GetMappedSlice().mapPtr()) + copy.srcOffset; if (likely(copy.copyElementStride == copy.copyElementSize)) { std::memcpy(data, src, copy.copyBufferLength); @@ -5428,7 +5428,7 @@ namespace dxvk { VkIndexType indexType = DecodeIndexType(ibo->Desc()->Format); uint32_t offset = indexStride * FirstIndex; uint8_t* data = reinterpret_cast(upSlice.mapPtr) + iboUPBufferOffset; - uint8_t* src = reinterpret_cast(ibo->GetMappedSlice().mapPtr) + offset; + uint8_t* src = reinterpret_cast(ibo->GetMappedSlice().mapPtr()) + offset; std::memcpy(data, src, iboUPBufferSize); auto iboSlice = upSlice.slice.subSlice(iboUPBufferOffset, iboUPBufferSize);