diff --git a/src/dxvk/dxvk_buffer.cpp b/src/dxvk/dxvk_buffer.cpp index 9b144290..0eead859 100644 --- a/src/dxvk/dxvk_buffer.cpp +++ b/src/dxvk/dxvk_buffer.cpp @@ -36,48 +36,6 @@ namespace dxvk { } - DxvkBufferSliceHandle DxvkBuffer::allocSlice() { - std::unique_lock freeLock(m_freeMutex); - - // If no slices are available, swap the two free lists. - if (m_freeSlices.size() == 0) { - std::unique_lock swapLock(m_swapMutex); - std::swap(m_freeSlices, m_nextSlices); - } - - // If there are still no slices available, create a new - // backing buffer and add all slices to the free list. - if (m_freeSlices.size() == 0) { - std::unique_lock swapLock(m_swapMutex); - DxvkBufferHandle handle = allocBuffer(m_physSliceCount); - - for (uint32_t i = 0; i < m_physSliceCount; i++) { - DxvkBufferSliceHandle slice; - slice.handle = handle.buffer; - slice.offset = m_physSliceStride * i; - slice.length = m_physSliceLength; - slice.mapPtr = handle.memory.mapPtr(slice.offset); - m_freeSlices.push_back(slice); - } - - m_buffers.push_back(std::move(handle)); - m_physSliceCount *= 2; - } - - // Take the first slice from the queue - DxvkBufferSliceHandle result = std::move(m_freeSlices.back()); - m_freeSlices.pop_back(); - return result; - } - - - void DxvkBuffer::freeSlice(const DxvkBufferSliceHandle& slice) { - // Add slice to a separate free list to reduce lock contention. - std::unique_lock swapLock(m_swapMutex); - m_nextSlices.push_back(slice); - } - - DxvkBufferHandle DxvkBuffer::allocBuffer(VkDeviceSize sliceCount) const { auto vkd = m_device->vkd(); diff --git a/src/dxvk/dxvk_buffer.h b/src/dxvk/dxvk_buffer.h index 3a641515..7adde694 100644 --- a/src/dxvk/dxvk_buffer.h +++ b/src/dxvk/dxvk_buffer.h @@ -232,7 +232,39 @@ namespace dxvk { * \brief Allocates new buffer slice * \returns The new buffer slice */ - DxvkBufferSliceHandle allocSlice(); + DxvkBufferSliceHandle allocSlice() { + std::unique_lock freeLock(m_freeMutex); + + // If no slices are available, swap the two free lists. + if (unlikely(m_freeSlices.size() == 0)) { + std::unique_lock swapLock(m_swapMutex); + std::swap(m_freeSlices, m_nextSlices); + } + + // If there are still no slices available, create a new + // backing buffer and add all slices to the free list. + if (unlikely(m_freeSlices.size() == 0)) { + std::unique_lock swapLock(m_swapMutex); + DxvkBufferHandle handle = allocBuffer(m_physSliceCount); + + for (uint32_t i = 0; i < m_physSliceCount; i++) { + DxvkBufferSliceHandle slice; + slice.handle = handle.buffer; + slice.offset = m_physSliceStride * i; + slice.length = m_physSliceLength; + slice.mapPtr = handle.memory.mapPtr(slice.offset); + m_freeSlices.push_back(slice); + } + + m_buffers.push_back(std::move(handle)); + m_physSliceCount *= 2; + } + + // Take the first slice from the queue + DxvkBufferSliceHandle result = m_freeSlices.back(); + m_freeSlices.pop_back(); + return result; + } /** * \brief Frees a buffer slice @@ -242,8 +274,11 @@ namespace dxvk { * the slice is no longer needed by the GPU. * \param [in] slice The buffer slice to free */ - void freeSlice( - const DxvkBufferSliceHandle& slice); + void freeSlice(const DxvkBufferSliceHandle& slice) { + // Add slice to a separate free list to reduce lock contention. + std::unique_lock swapLock(m_swapMutex); + m_nextSlices.push_back(slice); + } private: