diff --git a/Common/GPU/Vulkan/VulkanContext.h b/Common/GPU/Vulkan/VulkanContext.h index 21dbaad08e..49e6eb96c3 100644 --- a/Common/GPU/Vulkan/VulkanContext.h +++ b/Common/GPU/Vulkan/VulkanContext.h @@ -330,6 +330,7 @@ public: } int GetInflightFrames() const { + // out of MAX_INFLIGHT_FRAMES. return inflightFrames_; } // Don't call while a frame is in progress. diff --git a/Common/GPU/Vulkan/VulkanMemory.cpp b/Common/GPU/Vulkan/VulkanMemory.cpp index 9a25e5cbb0..f679ff63be 100644 --- a/Common/GPU/Vulkan/VulkanMemory.cpp +++ b/Common/GPU/Vulkan/VulkanMemory.cpp @@ -25,17 +25,31 @@ #include "Common/Log.h" #include "Common/TimeUtil.h" +#include "Common/Math/math_util.h" #include "Common/GPU/Vulkan/VulkanMemory.h" +#include "Common/Data/Text/Parsers.h" using namespace PPSSPP_VK; +// Always keep around push buffers at least this long (seconds). +static const double PUSH_GARBAGE_COLLECTION_DELAY = 10.0; + // Global push buffer tracker for vulkan memory profiling. // Don't want to manually dig up all the active push buffers. static std::mutex g_pushBufferListMutex; -static std::set g_pushBuffers; +static std::set g_pushBuffers; -VulkanPushBuffer::VulkanPushBuffer(VulkanContext *vulkan, const char *name, size_t size, VkBufferUsageFlags usage, PushBufferType type) - : vulkan_(vulkan), name_(name), size_(size), usage_(usage), type_(type) { +std::vector GetActiveVulkanMemoryManagers() { + std::vector buffers; + std::lock_guard guard(g_pushBufferListMutex); + for (auto iter : g_pushBuffers) { + buffers.push_back(iter); + } + return buffers; +} + +VulkanPushBuffer::VulkanPushBuffer(VulkanContext *vulkan, const char *name, size_t size, VkBufferUsageFlags usage) + : vulkan_(vulkan), name_(name), size_(size), usage_(usage) { { std::lock_guard guard(g_pushBufferListMutex); g_pushBuffers.insert(this); @@ -55,15 +69,6 @@ VulkanPushBuffer::~VulkanPushBuffer() { _assert_(buffers_.empty()); } -std::vector VulkanPushBuffer::GetAllActive() { - std::vector buffers; - std::lock_guard guard(g_pushBufferListMutex); - for (auto iter : g_pushBuffers) { - buffers.push_back(iter); - } - return buffers; -} - bool VulkanPushBuffer::AddBuffer() { BufInfo info; VkDevice device = vulkan_->GetDevice(); @@ -77,7 +82,7 @@ bool VulkanPushBuffer::AddBuffer() { b.pQueueFamilyIndices = nullptr; VmaAllocationCreateInfo allocCreateInfo{}; - allocCreateInfo.usage = type_ == PushBufferType::CPU_TO_GPU ? VMA_MEMORY_USAGE_CPU_TO_GPU : VMA_MEMORY_USAGE_GPU_ONLY; + allocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_TO_GPU; VmaAllocationInfo allocInfo{}; VkResult res = vmaCreateBuffer(vulkan_->Allocator(), &b, &allocCreateInfo, &info.buffer, &info.allocation, &allocInfo); @@ -103,8 +108,7 @@ void VulkanPushBuffer::Destroy(VulkanContext *vulkan) { void VulkanPushBuffer::NextBuffer(size_t minSize) { // First, unmap the current memory. - if (type_ == PushBufferType::CPU_TO_GPU) - Unmap(); + Unmap(); buf_++; if (buf_ >= buffers_.size() || minSize > size_) { @@ -123,8 +127,7 @@ void VulkanPushBuffer::NextBuffer(size_t minSize) { // Now, move to the next buffer and map it. offset_ = 0; - if (type_ == PushBufferType::CPU_TO_GPU) - Map(); + Map(); } void VulkanPushBuffer::Defragment(VulkanContext *vulkan) { @@ -149,8 +152,13 @@ size_t VulkanPushBuffer::GetTotalSize() const { return sum; } -size_t VulkanPushBuffer::GetTotalCapacity() const { - return size_ * buffers_.size(); +void VulkanPushBuffer::GetDebugString(char *buffer, size_t bufSize) const { + size_t sum = 0; + if (buffers_.size() > 1) + sum += size_ * (buffers_.size() - 1); + sum += offset_; + size_t capacity = size_ * buffers_.size(); + snprintf(buffer, bufSize, "Push %s: %s/%s", name_, NiceSizeFormat(capacity).c_str(), NiceSizeFormat(sum).c_str()); } void VulkanPushBuffer::Map() { @@ -264,3 +272,148 @@ VkResult VulkanDescSetPool::Recreate(bool grow) { } return result; } + +VulkanPushPool::VulkanPushPool(VulkanContext *vulkan, const char *name, size_t originalBlockSize, VkBufferUsageFlags usage) + : vulkan_(vulkan), name_(name), originalBlockSize_(originalBlockSize), usage_(usage) { + { + std::lock_guard guard(g_pushBufferListMutex); + g_pushBuffers.insert(this); + } + + for (int i = 0; i < VulkanContext::MAX_INFLIGHT_FRAMES; i++) { + blocks_.push_back(CreateBlock(originalBlockSize)); + blocks_.back().original = true; + blocks_.back().frameIndex = i; + } +} + +VulkanPushPool::~VulkanPushPool() { + { + std::lock_guard guard(g_pushBufferListMutex); + g_pushBuffers.erase(this); + } + + _dbg_assert_(blocks_.empty()); +} + +void VulkanPushPool::Destroy() { + for (auto &block : blocks_) { + block.Destroy(vulkan_); + } + blocks_.clear(); +} + +VulkanPushPool::Block VulkanPushPool::CreateBlock(size_t size) { + Block block{}; + block.size = size; + block.frameIndex = -1; + + VkBufferCreateInfo b{ VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO }; + b.size = size; + b.usage = usage_; + b.sharingMode = VK_SHARING_MODE_EXCLUSIVE; + VmaAllocationCreateInfo allocCreateInfo{}; + allocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_TO_GPU; + VmaAllocationInfo allocInfo{}; + + VkResult result = vmaCreateBuffer(vulkan_->Allocator(), &b, &allocCreateInfo, &block.buffer, &block.allocation, &allocInfo); + _assert_(result == VK_SUCCESS); + + result = vmaMapMemory(vulkan_->Allocator(), block.allocation, (void **)(&block.writePtr)); + _assert_(result == VK_SUCCESS); + + return block; +} + +VulkanPushPool::Block::~Block() {} + +void VulkanPushPool::Block::Destroy(VulkanContext *vulkan) { + vmaUnmapMemory(vulkan->Allocator(), allocation); + vulkan->Delete().QueueDeleteBufferAllocation(buffer, allocation); +} + +void VulkanPushPool::BeginFrame() { + double now = time_now_d(); + curBlockIndex_ = -1; + for (auto &block : blocks_) { + if (block.frameIndex == vulkan_->GetCurFrame()) { + if (curBlockIndex_ == -1) { + // Pick a block associated with the current frame to start at. + // We always start with one block per frame index. + curBlockIndex_ = block.frameIndex; + block.lastUsed = now; + } + block.used = 0; + if (!block.original) { + // Return block to the common pool + block.frameIndex = -1; + } + } + } + + // Do a single pass of bubblesort to move the bigger buffers earlier in the sequence. + // Over multiple frames this will quickly converge to the right order. + for (size_t i = 3; i < blocks_.size() - 1; i++) { + if (blocks_[i].frameIndex == -1 && blocks_[i + 1].frameIndex == -1 && blocks_[i].size < blocks_[i + 1].size) { + std::swap(blocks_[i], blocks_[i + 1]); + } + } + + // If we have lots of little buffers and the last one hasn't been used in a while, drop it. + // Still, let's keep around a few big ones (6 - 3). + if (blocks_.size() > 6 && blocks_.back().lastUsed < now - PUSH_GARBAGE_COLLECTION_DELAY) { + double start = time_now_d(); + size_t size = blocks_.back().size; + blocks_.back().Destroy(vulkan_); + blocks_.pop_back(); + DEBUG_LOG(G3D, "%s: Garbage collected block of size %s in %0.2f ms", name_, NiceSizeFormat(size).c_str(), time_now_d() - start); + } +} + +void VulkanPushPool::NextBlock(VkDeviceSize allocationSize) { + int curFrameIndex = vulkan_->GetCurFrame(); + curBlockIndex_++; + while (curBlockIndex_ < blocks_.size()) { + Block &block = blocks_[curBlockIndex_]; + // Grab the first matching block, or unused block (frameIndex == -1). + if ((block.frameIndex == curFrameIndex || block.frameIndex == -1) && block.size >= allocationSize) { + _assert_(block.used == 0); + block.used = allocationSize; + block.lastUsed = time_now_d(); + block.frameIndex = curFrameIndex; + return; + } + curBlockIndex_++; + } + + double start = time_now_d(); + VkDeviceSize newBlockSize = std::max(originalBlockSize_ * 2, (VkDeviceSize)RoundUpToPowerOf2((uint32_t)allocationSize)); + // We're still here and ran off the end of blocks. Create a new one. + blocks_.push_back(CreateBlock(newBlockSize)); + blocks_.back().frameIndex = curFrameIndex; + blocks_.back().used = allocationSize; + blocks_.back().lastUsed = time_now_d(); + // curBlockIndex_ is already set correctly here. + DEBUG_LOG(G3D, "%s: Created new block of size %s in %0.2f ms", name_, NiceSizeFormat(newBlockSize).c_str(), 1000.0 * (time_now_d() - start)); +} + +size_t VulkanPushPool::GetUsedThisFrame() const { + size_t used = 0; + for (auto &block : blocks_) { + if (block.frameIndex == vulkan_->GetCurFrame()) { + used += block.used; + } + } + return used; +} + +void VulkanPushPool::GetDebugString(char *buffer, size_t bufSize) const { + size_t used = 0; + size_t capacity = 0; + for (auto &block : blocks_) { + used += block.used; + capacity += block.size; + } + + snprintf(buffer, bufSize, "Pool %s: %s / %s (%d extra blocks)", name_, NiceSizeFormat(used).c_str(), NiceSizeFormat(capacity).c_str(), (int)blocks_.size() - 3); +} diff --git a/Common/GPU/Vulkan/VulkanMemory.h b/Common/GPU/Vulkan/VulkanMemory.h index 863e4f965e..f4d502e138 100644 --- a/Common/GPU/Vulkan/VulkanMemory.h +++ b/Common/GPU/Vulkan/VulkanMemory.h @@ -14,19 +14,22 @@ VK_DEFINE_HANDLE(VmaAllocation); // // Vulkan memory management utils. -enum class PushBufferType { - CPU_TO_GPU, - GPU_ONLY, +// Just an abstract thing to get debug information. +class VulkanMemoryManager { +public: + virtual ~VulkanMemoryManager() {} + + virtual void GetDebugString(char *buffer, size_t bufSize) const = 0; + virtual const char *Name() const = 0; // for sorting }; // 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 { +// NOTE: This has now been replaced with VulkanPushPool for all uses except the vertex cache. +class VulkanPushBuffer : public VulkanMemoryManager { struct BufInfo { VkBuffer buffer; VmaAllocation allocation; @@ -36,105 +39,55 @@ public: // NOTE: If you create a push buffer with PushBufferType::GPU_ONLY, // then you can't use any of the push functions as pointers will not be reachable from the CPU. // You must in this case use Allocate() only, and pass the returned offset and the VkBuffer to Vulkan APIs. - VulkanPushBuffer(VulkanContext *vulkan, const char *name, size_t size, VkBufferUsageFlags usage, PushBufferType type); + VulkanPushBuffer(VulkanContext *vulkan, const char *name, size_t size, VkBufferUsageFlags usage); ~VulkanPushBuffer(); void Destroy(VulkanContext *vulkan); void Reset() { offset_ = 0; } + void GetDebugString(char *buffer, size_t bufSize) const override; + const char *Name() const override { + return name_; + } + // Needs context in case of defragment. void Begin(VulkanContext *vulkan) { buf_ = 0; offset_ = 0; // Note: we must defrag because some buffers may be smaller than size_. Defragment(vulkan); - if (type_ == PushBufferType::CPU_TO_GPU) - Map(); + Map(); } - void BeginNoReset() { - if (type_ == PushBufferType::CPU_TO_GPU) - Map(); - } - - void End() { - if (type_ == PushBufferType::CPU_TO_GPU) - Unmap(); - } + void BeginNoReset() { Map(); } + void End() { Unmap(); } 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_; - } - - const char *Name() const { - return name_; - } - - // "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 GetTotalSize() const; // Used size - size_t GetTotalCapacity() const; - - static std::vector GetAllActive(); + size_t GetOffset() const { return offset_; } + size_t GetTotalSize() const; private: bool AddBuffer(); @@ -142,7 +95,6 @@ private: void Defragment(VulkanContext *vulkan); VulkanContext *vulkan_; - PushBufferType type_; std::vector buffers_; size_t buf_ = 0; @@ -153,6 +105,81 @@ private: const char *name_; }; +// Simple memory pushbuffer pool that can share blocks between the "frames", to reduce the impact of push memory spikes - +// a later frame can gobble up redundant buffers from an earlier frame even if they don't share frame index. +class VulkanPushPool : public VulkanMemoryManager { +public: + VulkanPushPool(VulkanContext *vulkan, const char *name, size_t originalBlockSize, VkBufferUsageFlags usage); + ~VulkanPushPool(); + + void Destroy(); + void BeginFrame(); + + const char *Name() const override { + return name_; + } + void GetDebugString(char *buffer, size_t bufSize) const override; + + // When using the returned memory, make sure to bind the returned vkbuf. + uint8_t *Allocate(VkDeviceSize numBytes, VkDeviceSize alignment, VkBuffer *vkbuf, uint32_t *bindOffset) { + _dbg_assert_(curBlockIndex_ >= 0); + + Block &block = blocks_[curBlockIndex_]; + + VkDeviceSize offset = (block.used + (alignment - 1)) & ~(alignment - 1); + if (offset + numBytes <= block.size) { + block.used = offset + numBytes; + *vkbuf = block.buffer; + *bindOffset = (uint32_t)offset; + return block.writePtr + offset; + } + + NextBlock(numBytes); + + *vkbuf = blocks_[curBlockIndex_].buffer; + *bindOffset = 0; // Newly allocated buffer will start at 0. + return blocks_[curBlockIndex_].writePtr; + } + + VkDeviceSize Push(const void *data, VkDeviceSize numBytes, int alignment, VkBuffer *vkbuf) { + uint32_t bindOffset; + uint8_t *ptr = Allocate(numBytes, alignment, vkbuf, &bindOffset); + memcpy(ptr, data, numBytes); + return bindOffset; + } + + size_t GetUsedThisFrame() const; + +private: + void NextBlock(VkDeviceSize allocationSize); + + struct Block { + ~Block(); + VkBuffer buffer; + VmaAllocation allocation; + + VkDeviceSize size; + VkDeviceSize used; + + int frameIndex; + bool original; // these blocks aren't garbage collected. + double lastUsed; + + uint8_t *writePtr; + + void Destroy(VulkanContext *vulkan); + }; + + Block CreateBlock(size_t sz); + + VulkanContext *vulkan_; + VkDeviceSize originalBlockSize_; + std::vector blocks_; + VkBufferUsageFlags usage_; + int curBlockIndex_ = -1; + const char *name_; +}; + // Only appropriate for use in a per-frame pool. class VulkanDescSetPool { public: @@ -182,3 +209,6 @@ private: uint32_t usage_ = 0; bool grow_; }; + +std::vector GetActiveVulkanMemoryManagers(); + diff --git a/Common/GPU/Vulkan/thin3d_vulkan.cpp b/Common/GPU/Vulkan/thin3d_vulkan.cpp index 008fcd0159..fe1929cd52 100644 --- a/Common/GPU/Vulkan/thin3d_vulkan.cpp +++ b/Common/GPU/Vulkan/thin3d_vulkan.cpp @@ -283,8 +283,8 @@ public: } // Returns the binding offset, and the VkBuffer to bind. - size_t PushUBO(VulkanPushBuffer *buf, VulkanContext *vulkan, VkBuffer *vkbuf) { - return buf->PushAligned(ubo_, uboSize_, vulkan->GetPhysicalDeviceProperties().properties.limits.minUniformBufferOffsetAlignment, vkbuf); + size_t PushUBO(VulkanPushPool *buf, VulkanContext *vulkan, VkBuffer *vkbuf) { + return buf->Push(ubo_, uboSize_, vulkan->GetPhysicalDeviceProperties().properties.limits.minUniformBufferOffsetAlignment, vkbuf); } int GetUBOSize() const { @@ -334,9 +334,9 @@ struct DescriptorSetKey { class VKTexture : public Texture { public: - VKTexture(VulkanContext *vulkan, VkCommandBuffer cmd, VulkanPushBuffer *pushBuffer, const TextureDesc &desc) + VKTexture(VulkanContext *vulkan, VkCommandBuffer cmd, VulkanPushPool *pushBuffer, const TextureDesc &desc) : vulkan_(vulkan), mipLevels_(desc.mipLevels), format_(desc.format) {} - bool Create(VkCommandBuffer cmd, VulkanPushBuffer *pushBuffer, const TextureDesc &desc); + bool Create(VkCommandBuffer cmd, VulkanPushPool *pushBuffer, const TextureDesc &desc); ~VKTexture() { Destroy(); @@ -536,12 +536,12 @@ private: VkImageView boundImageView_[MAX_BOUND_TEXTURES]{}; TextureBindFlags boundTextureFlags_[MAX_BOUND_TEXTURES]; + VulkanPushPool *push_ = nullptr; + struct FrameData { FrameData() : descriptorPool("VKContext", false) { descriptorPool.Setup([this] { descSets_.clear(); }); } - - VulkanPushBuffer *pushBuffer = nullptr; // Per-frame descriptor set cache. As it's per frame and reset every frame, we don't need to // worry about invalidating descriptors pointing to deleted textures. // However! ARM is not a fan of doing it this way. @@ -551,8 +551,6 @@ private: FrameData frame_[VulkanContext::MAX_INFLIGHT_FRAMES]; - VulkanPushBuffer *push_ = nullptr; - DeviceCaps caps_{}; uint8_t stencilRef_ = 0; @@ -675,7 +673,7 @@ VulkanTexture *VKContext::GetNullTexture() { VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT); uint32_t bindOffset; VkBuffer bindBuf; - uint32_t *data = (uint32_t *)push_->Push(w * h * 4, &bindOffset, &bindBuf); + uint32_t *data = (uint32_t *)push_->Allocate(w * h * 4, 4, &bindBuf, &bindOffset); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { // data[y*w + x] = ((x ^ y) & 1) ? 0xFF808080 : 0xFF000000; // gray/black checkerboard @@ -739,7 +737,7 @@ enum class TextureState { PENDING_DESTRUCTION, }; -bool VKTexture::Create(VkCommandBuffer cmd, VulkanPushBuffer *push, const TextureDesc &desc) { +bool VKTexture::Create(VkCommandBuffer cmd, VulkanPushPool *push, const TextureDesc &desc) { // Zero-sized textures not allowed. _assert_(desc.width * desc.height * desc.depth > 0); // remember to set depth to 1! if (desc.width * desc.height * desc.depth <= 0) { @@ -779,12 +777,12 @@ bool VKTexture::Create(VkCommandBuffer cmd, VulkanPushBuffer *push, const Textur VkBuffer buf; size_t size = w * h * d * bytesPerPixel; if (desc.initDataCallback) { - uint8_t *dest = (uint8_t *)push->PushAligned(size, &offset, &buf, 16); + uint8_t *dest = (uint8_t *)push->Allocate(size, 16, &buf, &offset); if (!desc.initDataCallback(dest, desc.initData[i], w, h, d, w * bytesPerPixel, h * w * bytesPerPixel)) { memcpy(dest, desc.initData[i], size); } } else { - offset = push->PushAligned((const void *)desc.initData[i], size, 16, &buf); + offset = push->Push((const void *)desc.initData[i], size, 16, &buf); } TextureCopyBatch batch; vkTex_->CopyBufferToMipLevel(cmd, &batch, i, w, h, 0, buf, offset, w); @@ -983,9 +981,10 @@ VKContext::VKContext(VulkanContext *vulkan) // 200 textures per frame was not enough for the UI. dp.maxSets = 4096; + VkBufferUsageFlags usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT; + push_ = new VulkanPushPool(vulkan_, "pushBuffer", 4 * 1024 * 1024, usage); + for (int i = 0; i < VulkanContext::MAX_INFLIGHT_FRAMES; i++) { - VkBufferUsageFlags usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT; - frame_[i].pushBuffer = new VulkanPushBuffer(vulkan_, "pushBuffer", 1024 * 1024, usage, PushBufferType::CPU_TO_GPU); frame_[i].descriptorPool.Create(vulkan_, dp, dpTypes); } @@ -1037,9 +1036,9 @@ VKContext::~VKContext() { // This also destroys all descriptor sets. for (int i = 0; i < VulkanContext::MAX_INFLIGHT_FRAMES; i++) { frame_[i].descriptorPool.Destroy(); - frame_[i].pushBuffer->Destroy(vulkan_); - delete frame_[i].pushBuffer; } + push_->Destroy(); + delete push_; vulkan_->Delete().QueueDeleteDescriptorSetLayout(descriptorSetLayout_); vulkan_->Delete().QueueDeletePipelineLayout(pipelineLayout_); vulkan_->Delete().QueueDeletePipelineCache(pipelineCache_); @@ -1050,23 +1049,15 @@ void VKContext::BeginFrame() { renderManager_.BeginFrame(debugFlags_ & DebugFlags::PROFILE_TIMESTAMPS, debugFlags_ & DebugFlags::PROFILE_SCOPES); FrameData &frame = frame_[vulkan_->GetCurFrame()]; - push_ = frame.pushBuffer; - // OK, we now know that nothing is reading from this frame's data pushbuffer, - push_->Reset(); - push_->Begin(vulkan_); + push_->BeginFrame(); frame.descriptorPool.Reset(); } void VKContext::EndFrame() { - // Stop collecting data in the frame's data pushbuffer. - push_->End(); - renderManager_.Finish(); - push_ = nullptr; - // Unbind stuff, to avoid accidentally relying on it across frames (and provide some protection against forgotten unbinds of deleted things). Invalidate(InvalidationFlags::CACHED_RENDER_STATE); } @@ -1452,7 +1443,7 @@ void VKContext::Draw(int vertexCount, int offset) { VkBuffer vulkanVbuf; VkBuffer vulkanUBObuf; uint32_t ubo_offset = (uint32_t)curPipeline_->PushUBO(push_, vulkan_, &vulkanUBObuf); - size_t vbBindOffset = push_->Push(vbuf->GetData(), vbuf->GetSize(), &vulkanVbuf); + size_t vbBindOffset = push_->Push(vbuf->GetData(), vbuf->GetSize(), 4, &vulkanVbuf); VkDescriptorSet descSet = GetOrCreateDescriptorSet(vulkanUBObuf); if (descSet == VK_NULL_HANDLE) { @@ -1471,8 +1462,8 @@ void VKContext::DrawIndexed(int vertexCount, int offset) { VkBuffer vulkanVbuf, vulkanIbuf, vulkanUBObuf; uint32_t ubo_offset = (uint32_t)curPipeline_->PushUBO(push_, vulkan_, &vulkanUBObuf); - size_t vbBindOffset = push_->Push(vbuf->GetData(), vbuf->GetSize(), &vulkanVbuf); - size_t ibBindOffset = push_->Push(ibuf->GetData(), ibuf->GetSize(), &vulkanIbuf); + size_t vbBindOffset = push_->Push(vbuf->GetData(), vbuf->GetSize(), 4, &vulkanVbuf); + size_t ibBindOffset = push_->Push(ibuf->GetData(), ibuf->GetSize(), 4, &vulkanIbuf); VkDescriptorSet descSet = GetOrCreateDescriptorSet(vulkanUBObuf); if (descSet == VK_NULL_HANDLE) { @@ -1487,7 +1478,7 @@ void VKContext::DrawIndexed(int vertexCount, int offset) { void VKContext::DrawUP(const void *vdata, int vertexCount) { VkBuffer vulkanVbuf, vulkanUBObuf; - size_t vbBindOffset = push_->Push(vdata, vertexCount * curPipeline_->stride[0], &vulkanVbuf); + size_t vbBindOffset = push_->Push(vdata, vertexCount * curPipeline_->stride[0], 4, &vulkanVbuf); uint32_t ubo_offset = (uint32_t)curPipeline_->PushUBO(push_, vulkan_, &vulkanUBObuf); VkDescriptorSet descSet = GetOrCreateDescriptorSet(vulkanUBObuf); @@ -1779,7 +1770,8 @@ uint64_t VKContext::GetNativeObject(NativeObject obj, void *srcObject) { return (uint64_t)curFramebuffer_->GetFB()->GetRTView(); case NativeObject::THIN3D_PIPELINE_LAYOUT: return (uint64_t)pipelineLayout_; - + case NativeObject::PUSH_POOL: + return (uint64_t)push_; default: Crash(); return 0; diff --git a/Common/GPU/thin3d.h b/Common/GPU/thin3d.h index 3013470e95..ae3db6af00 100644 --- a/Common/GPU/thin3d.h +++ b/Common/GPU/thin3d.h @@ -252,6 +252,7 @@ enum class NativeObject { NULL_IMAGEVIEW, NULL_IMAGEVIEW_ARRAY, THIN3D_PIPELINE_LAYOUT, + PUSH_POOL, }; enum FBChannel { 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/DebugVisVulkan.cpp b/GPU/Vulkan/DebugVisVulkan.cpp index ff03218efd..1c1cf1ff7a 100644 --- a/GPU/Vulkan/DebugVisVulkan.cpp +++ b/GPU/Vulkan/DebugVisVulkan.cpp @@ -39,7 +39,7 @@ #undef DrawText -bool comparePushBufferNames(const VulkanPushBuffer *a, const VulkanPushBuffer *b) { +bool comparePushBufferNames(const VulkanMemoryManager *a, const VulkanMemoryManager *b) { return strcmp(a->Name(), b->Name()) < 0; } @@ -75,13 +75,13 @@ void DrawAllocatorVis(UIContext *ui, GPUInterface *gpu) { str << "Push buffers:" << std::endl; // Now list the various push buffers. - auto pushBuffers = VulkanPushBuffer::GetAllActive(); - std::sort(pushBuffers.begin(), pushBuffers.end(), comparePushBufferNames); + auto managers = GetActiveVulkanMemoryManagers(); + std::sort(managers.begin(), managers.end(), comparePushBufferNames); - for (auto push : pushBuffers) { - str << " " << push->Name() << " " - << NiceSizeFormat(push->GetTotalCapacity()) << ", used: " - << NiceSizeFormat(push->GetTotalSize()) << std::endl; + char buffer[512]; + for (auto manager : managers) { + manager->GetDebugString(buffer, sizeof(buffer)); + str << " " << buffer << std::endl; } const int padding = 10 + System_GetPropertyFloat(SYSPROP_DISPLAY_SAFE_INSET_LEFT); diff --git a/GPU/Vulkan/DrawEngineVulkan.cpp b/GPU/Vulkan/DrawEngineVulkan.cpp index 11907c13b7..39c72b27a7 100644 --- a/GPU/Vulkan/DrawEngineVulkan.cpp +++ b/GPU/Vulkan/DrawEngineVulkan.cpp @@ -169,13 +169,14 @@ void DrawEngineVulkan::InitDeviceObjects() { for (int i = 0; i < VulkanContext::MAX_INFLIGHT_FRAMES; i++) { frame_[i].descPool.Create(vulkan, dp, dpTypes); - // Note that pushUBO is also used for tessellation data (search for SetPushBuffer), and to upload + // Note that pushUBO_ is also used for tessellation data (search for SetPushBuffer), and to upload // the null texture. This should be cleaned up... - frame_[i].pushUBO = new VulkanPushBuffer(vulkan, "pushUBO", 8 * 1024 * 1024, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT, PushBufferType::CPU_TO_GPU); - frame_[i].pushVertex = new VulkanPushBuffer(vulkan, "pushVertex", 2 * 1024 * 1024, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, PushBufferType::CPU_TO_GPU); - frame_[i].pushIndex = new VulkanPushBuffer(vulkan, "pushIndex", 1 * 1024 * 1024, VK_BUFFER_USAGE_INDEX_BUFFER_BIT, PushBufferType::CPU_TO_GPU); } + pushUBO_ = (VulkanPushPool *)draw_->GetNativeObject(Draw::NativeObject::PUSH_POOL); + pushVertex_ = new VulkanPushPool(vulkan, "pushVertex", 4 * 1024 * 1024, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); + pushIndex_ = new VulkanPushPool(vulkan, "pushIndex", 1 * 512 * 1024, VK_BUFFER_USAGE_INDEX_BUFFER_BIT); + VkPipelineLayoutCreateInfo pl{ VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO }; pl.pPushConstantRanges = nullptr; pl.pushConstantRangeCount = 0; @@ -204,7 +205,7 @@ void DrawEngineVulkan::InitDeviceObjects() { res = vkCreateSampler(device, &samp, nullptr, &nullSampler_); _dbg_assert_(VK_SUCCESS == res); - vertexCache_ = new VulkanPushBuffer(vulkan, "pushVertexCache", VERTEX_CACHE_SIZE, VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, PushBufferType::CPU_TO_GPU); + vertexCache_ = new VulkanPushBuffer(vulkan, "pushVertexCache", VERTEX_CACHE_SIZE, VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); tessDataTransferVulkan = new TessellationDataTransferVulkan(vulkan); tessDataTransfer = tessDataTransferVulkan; @@ -221,22 +222,6 @@ DrawEngineVulkan::~DrawEngineVulkan() { void DrawEngineVulkan::FrameData::Destroy(VulkanContext *vulkan) { descPool.Destroy(); - - if (pushUBO) { - pushUBO->Destroy(vulkan); - delete pushUBO; - pushUBO = nullptr; - } - if (pushVertex) { - pushVertex->Destroy(vulkan); - delete pushVertex; - pushVertex = nullptr; - } - if (pushIndex) { - pushIndex->Destroy(vulkan); - delete pushIndex; - pushIndex = nullptr; - } } void DrawEngineVulkan::DestroyDeviceObjects() { @@ -256,6 +241,20 @@ void DrawEngineVulkan::DestroyDeviceObjects() { for (int i = 0; i < VulkanContext::MAX_INFLIGHT_FRAMES; i++) { frame_[i].Destroy(vulkan); } + + pushUBO_ = nullptr; + + if (pushVertex_) { + pushVertex_->Destroy(); + delete pushVertex_; + pushVertex_ = nullptr; + } + if (pushIndex_) { + pushIndex_->Destroy(); + delete pushIndex_; + pushIndex_ = nullptr; + } + if (samplerSecondaryNearest_ != VK_NULL_HANDLE) vulkan->Delete().QueueDeleteSampler(samplerSecondaryNearest_); if (samplerSecondaryLinear_ != VK_NULL_HANDLE) @@ -295,31 +294,27 @@ void DrawEngineVulkan::BeginFrame() { lastPipeline_ = nullptr; + // pushUBO is the thin3d push pool, don't need to BeginFrame again. + pushVertex_->BeginFrame(); + pushIndex_->BeginFrame(); + + tessDataTransferVulkan->SetPushPool(pushUBO_); + + DirtyAllUBOs(); + FrameData *frame = &GetCurFrame(); // First reset all buffers, then begin. This is so that Reset can free memory and Begin can allocate it, // if growing the buffer is needed. Doing it this way will reduce fragmentation if more than one buffer // needs to grow in the same frame. The state where many buffers are reset can also be used to // defragment memory. - frame->pushUBO->Reset(); - frame->pushVertex->Reset(); - frame->pushIndex->Reset(); - VulkanContext *vulkan = (VulkanContext *)draw_->GetNativeObject(Draw::NativeObject::CONTEXT); - frame->pushUBO->Begin(vulkan); - frame->pushVertex->Begin(vulkan); - frame->pushIndex->Begin(vulkan); - - tessDataTransferVulkan->SetPushBuffer(frame->pushUBO); - - DirtyAllUBOs(); - // Wipe the vertex cache if it's grown too large. if (vertexCache_->GetTotalSize() > VERTEX_CACHE_SIZE) { vertexCache_->Destroy(vulkan); delete vertexCache_; // orphans the buffers, they'll get deleted once no longer used by an in-flight frame. - vertexCache_ = new VulkanPushBuffer(vulkan, "vertexCacheR", VERTEX_CACHE_SIZE, VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, PushBufferType::CPU_TO_GPU); + vertexCache_ = new VulkanPushBuffer(vulkan, "vertexCacheR", VERTEX_CACHE_SIZE, VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); vai_.Iterate([&](uint32_t hash, VertexArrayInfoVulkan *vai) { delete vai; }); @@ -359,12 +354,8 @@ void DrawEngineVulkan::BeginFrame() { void DrawEngineVulkan::EndFrame() { FrameData *frame = &GetCurFrame(); - stats_.pushUBOSpaceUsed = (int)frame->pushUBO->GetOffset(); - stats_.pushVertexSpaceUsed = (int)frame->pushVertex->GetOffset(); - stats_.pushIndexSpaceUsed = (int)frame->pushIndex->GetOffset(); - frame->pushUBO->End(); - frame->pushVertex->End(); - frame->pushIndex->End(); + stats_.pushVertexSpaceUsed = (int)pushVertex_->GetUsedThisFrame(); + stats_.pushIndexSpaceUsed = (int)pushIndex_->GetUsedThisFrame(); vertexCache_->End(); } @@ -374,7 +365,18 @@ 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); +} + +void DrawEngineVulkan::DecodeVertsToPushPool(VulkanPushPool *push, uint32_t *bindOffset, VkBuffer *vkbuf) { + u8 *dest = decoded; + + // Figure out how much pushbuffer space we need to allocate. + if (push) { + int vertsToDecode = ComputeNumVertsToDecode(); + dest = push->Allocate(vertsToDecode * dec_->GetDecVtxFmt().stride, 4, vkbuf, bindOffset); } DecodeVerts(dest); } @@ -620,7 +622,7 @@ void DrawEngineVulkan::DoFlush() { vai->minihash = ComputeMiniHash(); vai->status = VertexArrayInfoVulkan::VAI_HASHING; vai->drawsUntilNextFullHash = 0; - DecodeVertsToPushBuffer(frameData.pushVertex, &vbOffset, &vbuf); // writes to indexGen + DecodeVertsToPushPool(pushVertex_, &vbOffset, &vbuf); // writes to indexGen vai->numVerts = indexGen.VertexCount(); vai->prim = indexGen.Prim(); vai->maxIndex = indexGen.MaxIndex(); @@ -646,7 +648,7 @@ void DrawEngineVulkan::DoFlush() { } if (newMiniHash != vai->minihash || newHash != vai->hash) { MarkUnreliable(vai); - DecodeVertsToPushBuffer(frameData.pushVertex, &vbOffset, &vbuf); + DecodeVertsToPushPool(pushVertex_, &vbOffset, &vbuf); goto rotateVBO; } if (vai->numVerts > 64) { @@ -665,7 +667,7 @@ void DrawEngineVulkan::DoFlush() { u32 newMiniHash = ComputeMiniHash(); if (newMiniHash != vai->minihash) { MarkUnreliable(vai); - DecodeVertsToPushBuffer(frameData.pushVertex, &vbOffset, &vbuf); + DecodeVertsToPushPool(pushVertex_, &vbOffset, &vbuf); goto rotateVBO; } } @@ -690,7 +692,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; @@ -737,7 +739,7 @@ void DrawEngineVulkan::DoFlush() { if (vai->lastFrame != gpuStats.numFlips) { vai->numFrames++; } - DecodeVertsToPushBuffer(frameData.pushVertex, &vbOffset, &vbuf); + DecodeVertsToPushPool(pushVertex_, &vbOffset, &vbuf); goto rotateVBO; } default: @@ -747,11 +749,11 @@ void DrawEngineVulkan::DoFlush() { if (decOptions_.applySkinInDecode && (lastVType_ & GE_VTYPE_WEIGHT_MASK)) { // If software skinning, we've already predecoded into "decoded". So push that content. VkDeviceSize size = decodedVerts_ * dec_->GetDecVtxFmt().stride; - u8 *dest = (u8 *)frameData.pushVertex->Push(size, &vbOffset, &vbuf); + u8 *dest = (u8 *)pushVertex_->Allocate(size, 4, &vbuf, &vbOffset); memcpy(dest, decoded, size); } else { // Decode directly into the pushbuffer - DecodeVertsToPushBuffer(frameData.pushVertex, &vbOffset, &vbuf); + DecodeVertsToPushPool(pushVertex_, &vbOffset, &vbuf); } rotateVBO: @@ -834,7 +836,7 @@ void DrawEngineVulkan::DoFlush() { }; if (useElements) { if (!ibuf) { - ibOffset = (uint32_t)frameData.pushIndex->Push(decIndex, sizeof(uint16_t) * indexGen.VertexCount(), &ibuf); + ibOffset = (uint32_t)pushIndex_->Push(decIndex, sizeof(uint16_t) * indexGen.VertexCount(), 4, &ibuf); } renderManager->DrawIndexed(ds, ARRAY_SIZE(dynamicUBOOffsets), dynamicUBOOffsets, vbuf, vbOffset, ibuf, ibOffset, vertexCount, 1, VK_INDEX_TYPE_UINT16); } else { @@ -972,12 +974,12 @@ void DrawEngineVulkan::DoFlush() { if (result.drawIndexed) { VkBuffer vbuf, ibuf; - vbOffset = (uint32_t)frameData.pushVertex->Push(result.drawBuffer, maxIndex * sizeof(TransformedVertex), &vbuf); - ibOffset = (uint32_t)frameData.pushIndex->Push(inds, sizeof(short) * result.drawNumTrans, &ibuf); + vbOffset = (uint32_t)pushVertex_->Push(result.drawBuffer, maxIndex * sizeof(TransformedVertex), 4, &vbuf); + ibOffset = (uint32_t)pushIndex_->Push(inds, sizeof(short) * result.drawNumTrans, 4, &ibuf); renderManager->DrawIndexed(ds, ARRAY_SIZE(dynamicUBOOffsets), dynamicUBOOffsets, vbuf, vbOffset, ibuf, ibOffset, result.drawNumTrans, 1, VK_INDEX_TYPE_UINT16); } else { VkBuffer vbuf; - vbOffset = (uint32_t)frameData.pushVertex->Push(result.drawBuffer, result.drawNumTrans * sizeof(TransformedVertex), &vbuf); + vbOffset = (uint32_t)pushVertex_->Push(result.drawBuffer, result.drawNumTrans * sizeof(TransformedVertex), 4, &vbuf); renderManager->Draw(ds, ARRAY_SIZE(dynamicUBOOffsets), dynamicUBOOffsets, vbuf, vbOffset, result.drawNumTrans); } } else if (result.action == SW_CLEAR) { @@ -1032,15 +1034,15 @@ void DrawEngineVulkan::DoFlush() { void DrawEngineVulkan::UpdateUBOs(FrameData *frame) { if ((dirtyUniforms_ & DIRTY_BASE_UNIFORMS) || baseBuf == VK_NULL_HANDLE) { - baseUBOOffset = shaderManager_->PushBaseBuffer(frame->pushUBO, &baseBuf); + baseUBOOffset = shaderManager_->PushBaseBuffer(pushUBO_, &baseBuf); dirtyUniforms_ &= ~DIRTY_BASE_UNIFORMS; } if ((dirtyUniforms_ & DIRTY_LIGHT_UNIFORMS) || lightBuf == VK_NULL_HANDLE) { - lightUBOOffset = shaderManager_->PushLightBuffer(frame->pushUBO, &lightBuf); + lightUBOOffset = shaderManager_->PushLightBuffer(pushUBO_, &lightBuf); dirtyUniforms_ &= ~DIRTY_LIGHT_UNIFORMS; } if ((dirtyUniforms_ & DIRTY_BONE_UNIFORMS) || boneBuf == VK_NULL_HANDLE) { - boneUBOOffset = shaderManager_->PushBoneBuffer(frame->pushUBO, &boneBuf); + boneUBOOffset = shaderManager_->PushBoneBuffer(pushUBO_, &boneBuf); dirtyUniforms_ &= ~DIRTY_BONE_UNIFORMS; } } @@ -1062,7 +1064,7 @@ void TessellationDataTransferVulkan::SendDataToShader(const SimpleVertex *const int size = size_u * size_v; int ssboAlignment = vulkan_->GetPhysicalDeviceProperties().properties.limits.minStorageBufferOffsetAlignment; - uint8_t *data = (uint8_t *)push_->PushAligned(size * sizeof(TessData), (uint32_t *)&bufInfo_[0].offset, &bufInfo_[0].buffer, ssboAlignment); + uint8_t *data = (uint8_t *)push_->Allocate(size * sizeof(TessData), ssboAlignment, &bufInfo_[0].buffer, (uint32_t *)&bufInfo_[0].offset); bufInfo_[0].range = size * sizeof(TessData); float *pos = (float *)(data); @@ -1075,12 +1077,12 @@ void TessellationDataTransferVulkan::SendDataToShader(const SimpleVertex *const using Spline::Weight; // Weights U - data = (uint8_t *)push_->PushAligned(weights.size_u * sizeof(Weight), (uint32_t *)&bufInfo_[1].offset, &bufInfo_[1].buffer, ssboAlignment); + data = (uint8_t *)push_->Allocate(weights.size_u * sizeof(Weight), ssboAlignment, &bufInfo_[1].buffer, (uint32_t *)&bufInfo_[1].offset); memcpy(data, weights.u, weights.size_u * sizeof(Weight)); bufInfo_[1].range = weights.size_u * sizeof(Weight); // Weights V - data = (uint8_t *)push_->PushAligned(weights.size_v * sizeof(Weight), (uint32_t *)&bufInfo_[2].offset, &bufInfo_[2].buffer, ssboAlignment); + data = (uint8_t *)push_->Allocate(weights.size_v * sizeof(Weight), ssboAlignment, &bufInfo_[2].buffer, (uint32_t *)&bufInfo_[2].offset); memcpy(data, weights.v, weights.size_v * sizeof(Weight)); bufInfo_[2].range = weights.size_v * sizeof(Weight); } diff --git a/GPU/Vulkan/DrawEngineVulkan.h b/GPU/Vulkan/DrawEngineVulkan.h index b0e99a7c24..c84ae19602 100644 --- a/GPU/Vulkan/DrawEngineVulkan.h +++ b/GPU/Vulkan/DrawEngineVulkan.h @@ -55,10 +55,10 @@ class FramebufferManagerVulkan; class VulkanContext; class VulkanPushBuffer; +class VulkanPushPool; struct VulkanPipeline; struct DrawEngineVulkanStats { - int pushUBOSpaceUsed; int pushVertexSpaceUsed; int pushIndexSpaceUsed; }; @@ -112,13 +112,13 @@ class TessellationDataTransferVulkan : public TessellationDataTransfer { public: TessellationDataTransferVulkan(VulkanContext *vulkan) : vulkan_(vulkan) {} - void SetPushBuffer(VulkanPushBuffer *push) { push_ = push; } + void SetPushPool(VulkanPushPool *push) { push_ = push; } // Send spline/bezier's control points and weights to vertex shader through structured shader buffer. void SendDataToShader(const SimpleVertex *const *points, int size_u, int size_v, u32 vertType, const Spline::Weight2D &weights) override; const VkDescriptorBufferInfo *GetBufferInfo() { return bufInfo_; } private: VulkanContext *vulkan_; - VulkanPushBuffer *push_; // Updated each frame. + VulkanPushPool *push_; // Updated each frame. VkDescriptorBufferInfo bufInfo_[3]{}; }; @@ -192,8 +192,8 @@ public: lastPipeline_ = nullptr; } - VulkanPushBuffer *GetPushBufferForTextureData() { - return GetCurFrame().pushUBO; + VulkanPushPool *GetPushBufferForTextureData() { + return pushUBO_; } const DrawEngineVulkanStats &GetStats() const { @@ -218,6 +218,7 @@ private: void DestroyDeviceObjects(); + void DecodeVertsToPushPool(VulkanPushPool *push, uint32_t *bindOffset, VkBuffer *vkbuf); void DecodeVertsToPushBuffer(VulkanPushBuffer *push, uint32_t *bindOffset, VkBuffer *vkbuf); void DoFlush(); @@ -268,10 +269,6 @@ private: VulkanDescSetPool descPool; - VulkanPushBuffer *pushUBO = nullptr; - VulkanPushBuffer *pushVertex = nullptr; - VulkanPushBuffer *pushIndex = nullptr; - // We do rolling allocation and reset instead of caching across frames. That we might do later. DenseHashMap descSets; @@ -281,6 +278,12 @@ private: GEPrimitiveType lastPrim_ = GE_PRIM_INVALID; FrameData frame_[VulkanContext::MAX_INFLIGHT_FRAMES]; + // This one's not accurately named, it's used for all kinds of stuff that's not vertices or indices. + VulkanPushPool *pushUBO_ = nullptr; + + VulkanPushPool *pushVertex_ = nullptr; + VulkanPushPool *pushIndex_ = nullptr; + // Other ShaderManagerVulkan *shaderManager_ = nullptr; PipelineManagerVulkan *pipelineManager_ = nullptr; diff --git a/GPU/Vulkan/GPU_Vulkan.cpp b/GPU/Vulkan/GPU_Vulkan.cpp index 0ee619f4db..b8cd310244 100644 --- a/GPU/Vulkan/GPU_Vulkan.cpp +++ b/GPU/Vulkan/GPU_Vulkan.cpp @@ -455,12 +455,11 @@ void GPU_Vulkan::GetStats(char *buffer, size_t bufsize) { textureCacheVulkan_->GetStats(texStats, sizeof(texStats)); snprintf(buffer, bufsize, "Vertex, Fragment, Pipelines loaded: %i, %i, %i\n" - "Pushbuffer space used: UBO %d, Vtx %d, Idx %d\n" + "Pushbuffer space used: Vtx %d, Idx %d\n" "%s\n", shaderManagerVulkan_->GetNumVertexShaders(), shaderManagerVulkan_->GetNumFragmentShaders(), pipelineManager_->GetNumPipelines(), - drawStats.pushUBOSpaceUsed, drawStats.pushVertexSpaceUsed, drawStats.pushIndexSpaceUsed, texStats diff --git a/GPU/Vulkan/ShaderManagerVulkan.h b/GPU/Vulkan/ShaderManagerVulkan.h index 38b4b2e1e1..13013fa498 100644 --- a/GPU/Vulkan/ShaderManagerVulkan.h +++ b/GPU/Vulkan/ShaderManagerVulkan.h @@ -34,7 +34,7 @@ class VulkanContext; class DrawEngineVulkan; -class VulkanPushBuffer; +class VulkanPushPool; class VulkanFragmentShader { public: @@ -145,15 +145,15 @@ public: bool IsLightDirty() { return true; } bool IsBoneDirty() { return true; } - uint32_t PushBaseBuffer(VulkanPushBuffer *dest, VkBuffer *buf) { - return dest->PushAligned(&ub_base, sizeof(ub_base), uboAlignment_, buf); + uint32_t PushBaseBuffer(VulkanPushPool *dest, VkBuffer *buf) { + return dest->Push(&ub_base, sizeof(ub_base), uboAlignment_, buf); } - uint32_t PushLightBuffer(VulkanPushBuffer *dest, VkBuffer *buf) { - return dest->PushAligned(&ub_lights, sizeof(ub_lights), uboAlignment_, buf); + uint32_t PushLightBuffer(VulkanPushPool *dest, VkBuffer *buf) { + return dest->Push(&ub_lights, sizeof(ub_lights), uboAlignment_, buf); } // TODO: Only push half the bone buffer if we only have four bones. - uint32_t PushBoneBuffer(VulkanPushBuffer *dest, VkBuffer *buf) { - return dest->PushAligned(&ub_bones, sizeof(ub_bones), uboAlignment_, buf); + uint32_t PushBoneBuffer(VulkanPushPool *dest, VkBuffer *buf) { + return dest->Push(&ub_bones, sizeof(ub_bones), uboAlignment_, buf); } bool LoadCacheFlags(FILE *f, DrawEngineVulkan *drawEngine); diff --git a/GPU/Vulkan/TextureCacheVulkan.cpp b/GPU/Vulkan/TextureCacheVulkan.cpp index 7f60c27d2f..a08f231cd5 100644 --- a/GPU/Vulkan/TextureCacheVulkan.cpp +++ b/GPU/Vulkan/TextureCacheVulkan.cpp @@ -550,7 +550,7 @@ void TextureCacheVulkan::BuildTexture(TexCacheEntry *const entry) { levels = plan.levelsToLoad; } - VulkanPushBuffer *pushBuffer = drawEngine_->GetPushBufferForTextureData(); + VulkanPushPool *pushBuffer = drawEngine_->GetPushBufferForTextureData(); // Batch the copies. TextureCopyBatch copyBatch; @@ -583,11 +583,11 @@ void TextureCacheVulkan::BuildTexture(TexCacheEntry *const entry) { saveData.resize(sz); data = &saveData[0]; } else { - data = pushBuffer->PushAligned(sz, &bufferOffset, &texBuf, pushAlignment); + data = pushBuffer->Allocate(sz, pushAlignment, &texBuf, &bufferOffset); } LoadVulkanTextureLevel(*entry, (uint8_t *)data, lstride, srcLevel, lfactor, actualFmt); if (plan.saveTexture) - bufferOffset = pushBuffer->PushAligned(&saveData[0], sz, pushAlignment, &texBuf); + bufferOffset = pushBuffer->Push(&saveData[0], sz, pushAlignment, &texBuf); }; bool dataScaled = true; @@ -599,7 +599,7 @@ void TextureCacheVulkan::BuildTexture(TexCacheEntry *const entry) { rowLength = (mipWidth + 3) & ~3; } // Directly load the replaced image. - data = pushBuffer->PushAligned(uploadSize, &bufferOffset, &texBuf, pushAlignment); + data = pushBuffer->Allocate(uploadSize, pushAlignment, &texBuf, &bufferOffset); double replaceStart = time_now_d(); if (!plan.replaced->CopyLevelTo(plan.baseLevelSrc + i, data, byteStride)) { // If plan.replaceValid, this shouldn't fail. WARN_LOG(G3D, "Failed to copy replaced texture level");