From 181ddf8266ea667f8ae796beecdc054314a7e6ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 15 Mar 2023 09:56:32 +0100 Subject: [PATCH] Change the API of the old VulkanPushBuffer to match VulkanPushPool. --- Common/GPU/Vulkan/VulkanMemory.h | 68 ++++++-------------------------- GPU/Common/ReplacedTexture.cpp | 2 +- GPU/Vulkan/DrawEngineVulkan.cpp | 4 +- 3 files changed, 16 insertions(+), 58 deletions(-) diff --git a/Common/GPU/Vulkan/VulkanMemory.h b/Common/GPU/Vulkan/VulkanMemory.h index cafc8206b7..5d4e4b8659 100644 --- a/Common/GPU/Vulkan/VulkanMemory.h +++ b/Common/GPU/Vulkan/VulkanMemory.h @@ -30,11 +30,9 @@ public: // VulkanPushBuffer // Simple incrementing allocator. -// Use these to push vertex, index and uniform data. Generally you'll have two of these +// Use these to push vertex, index and uniform data. Generally you'll have two or three of these // and alternate on each frame. Make sure not to reset until the fence from the last time you used it // has completed. -// -// TODO: Make it possible to suballocate pushbuffers from a large DeviceMemory block. class VulkanPushBuffer : public VulkanMemoryManager { struct BufInfo { VkBuffer buffer; @@ -78,69 +76,29 @@ public: } void Map(); - void Unmap(); // When using the returned memory, make sure to bind the returned vkbuf. - // This will later allow for handling overflow correctly. - size_t Allocate(size_t numBytes, VkBuffer *vkbuf) { - size_t out = offset_; - offset_ += (numBytes + 3) & ~3; // Round up to 4 bytes. - - if (offset_ >= size_) { + uint8_t *Allocate(VkDeviceSize numBytes, VkDeviceSize alignment, VkBuffer *vkbuf, uint32_t *bindOffset) { + size_t offset = (offset_ + alignment - 1) & ~(alignment - 1); + if (offset + numBytes > size_) { NextBuffer(numBytes); - out = offset_; - offset_ += (numBytes + 3) & ~3; + offset = offset_; } + offset_ = offset + numBytes; + *bindOffset = (uint32_t)offset; *vkbuf = buffers_[buf_].buffer; - return out; + return writePtr_ + offset; } - // Returns the offset that should be used when binding this buffer to get this data. - size_t Push(const void *data, size_t size, VkBuffer *vkbuf) { - _dbg_assert_(writePtr_); - size_t off = Allocate(size, vkbuf); - memcpy(writePtr_ + off, data, size); - return off; - } - - uint32_t PushAligned(const void *data, size_t size, int align, VkBuffer *vkbuf) { - _dbg_assert_(writePtr_); - offset_ = (offset_ + align - 1) & ~(align - 1); - size_t off = Allocate(size, vkbuf); - memcpy(writePtr_ + off, data, size); - return (uint32_t)off; - } - - size_t GetOffset() const { - return offset_; - } - - // "Zero-copy" variant - you can write the data directly as you compute it. - // Recommended. - void *Push(size_t size, uint32_t *bindOffset, VkBuffer *vkbuf) { - _dbg_assert_(writePtr_); - size_t off = Allocate(size, vkbuf); - *bindOffset = (uint32_t)off; - return writePtr_ + off; - } - void *PushAligned(size_t size, uint32_t *bindOffset, VkBuffer *vkbuf, int align) { - _dbg_assert_(writePtr_); - offset_ = (offset_ + align - 1) & ~(align - 1); - size_t off = Allocate(size, vkbuf); - *bindOffset = (uint32_t)off; - return writePtr_ + off; - } - - template - void PushUBOData(const T &data, VkDescriptorBufferInfo *info) { + VkDeviceSize Push(const void *data, VkDeviceSize numBytes, int alignment, VkBuffer *vkbuf) { uint32_t bindOffset; - void *ptr = PushAligned(sizeof(T), &bindOffset, &info->buffer, vulkan_->GetPhysicalDeviceProperties().properties.limits.minUniformBufferOffsetAlignment); - memcpy(ptr, &data, sizeof(T)); - info->offset = bindOffset; - info->range = sizeof(T); + uint8_t *ptr = Allocate(numBytes, alignment, vkbuf, &bindOffset); + memcpy(ptr, data, numBytes); + return bindOffset; } + size_t GetOffset() const { return offset_; } size_t GetTotalSize() const; private: diff --git a/GPU/Common/ReplacedTexture.cpp b/GPU/Common/ReplacedTexture.cpp index 4246919faf..3a9d47e3a4 100644 --- a/GPU/Common/ReplacedTexture.cpp +++ b/GPU/Common/ReplacedTexture.cpp @@ -509,7 +509,7 @@ ReplacedTexture::LoadLevelResult ReplacedTexture::LoadLevelData(VFSFileReference } levelData_->data[i].resize(dataSizeBytes); - transcoder.transcode_image_level(i, 0, 0, &out[0], outputSize, transcoderFormat, 0, outputPitch, level.h, -1, -1, &transcodeState); + transcoder.transcode_image_level(i, 0, 0, &out[0], (uint32_t)outputSize, transcoderFormat, 0, (uint32_t)outputPitch, level.h, -1, -1, &transcodeState); level.w = levelInfo.m_orig_width; level.h = levelInfo.m_orig_height; if (i != 0) diff --git a/GPU/Vulkan/DrawEngineVulkan.cpp b/GPU/Vulkan/DrawEngineVulkan.cpp index fbf8f7ac79..68302f8855 100644 --- a/GPU/Vulkan/DrawEngineVulkan.cpp +++ b/GPU/Vulkan/DrawEngineVulkan.cpp @@ -364,7 +364,7 @@ void DrawEngineVulkan::DecodeVertsToPushBuffer(VulkanPushBuffer *push, uint32_t // Figure out how much pushbuffer space we need to allocate. if (push) { int vertsToDecode = ComputeNumVertsToDecode(); - dest = (u8 *)push->Push(vertsToDecode * dec_->GetDecVtxFmt().stride, bindOffset, vkbuf); + dest = (u8 *)push->Allocate(vertsToDecode * dec_->GetDecVtxFmt().stride, 4, vkbuf, bindOffset); } DecodeVerts(dest); } @@ -691,7 +691,7 @@ void DrawEngineVulkan::DoFlush() { if (useElements) { u32 size = sizeof(uint16_t) * indexGen.VertexCount(); - void *dest = vertexCache_->Push(size, &vai->ibOffset, &vai->ib); + void *dest = vertexCache_->Allocate(size, 4, &vai->ib, &vai->ibOffset); memcpy(dest, decIndex, size); } else { vai->ib = VK_NULL_HANDLE;