Skip to content

Commit

Permalink
fix staging buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
star-e committed Dec 5, 2024
1 parent dd096b0 commit 152ec27
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
1 change: 1 addition & 0 deletions native/cocos/renderer/gfx-metal/MTLDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ of this software and associated engine source code (the "Software"), a limited,
CCMTLGPUStagingBufferPool *bufferPool = _gpuStagingBufferPools[index];
if (bufferPool) {
bufferPool->reset();
bufferPool->shrinkSize();
CCMTLGPUGarbageCollectionPool::getInstance()->clear(index);
static_cast<CCMTLCommandBuffer*>(_cmdBuff)->signalFence();
}
Expand Down
14 changes: 11 additions & 3 deletions native/cocos/renderer/gfx-metal/MTLGPUObjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ struct CCMTLGPUDescriptorSet {

class CCMTLGPUStagingBufferPool final {
public:
static constexpr uint32_t CHUNK_SIZE = 16 * 1024 * 1024; // 16M per block by default

CCMTLGPUStagingBufferPool(id<MTLDevice> device)
: _device(device) {}

Expand Down Expand Up @@ -225,8 +227,14 @@ class CCMTLGPUStagingBufferPool final {
}
}

void shrinkSize() {
void shrinkSize(size_t minimalSize = CHUNK_SIZE) {
size_t reservedSize = 0;
for (auto iter = _pool.begin(); iter != _pool.end() && _pool.size() > 1;) {
if (reservedSize < minimalSize) {
reservedSize += [iter->mtlBuffer length];
++iter;
continue;
}
if (iter->curOffset == 0) {
[iter->mtlBuffer release];
iter = _pool.erase(iter);
Expand Down Expand Up @@ -256,7 +264,7 @@ struct CCMTLGPUBufferImageCopy {
MTLOrigin destinationOrigin = {0, 0, 0};
};

//destroy GPU resource only, delete the owner object mannually.
// destroy GPU resource only, delete the owner object mannually.
class CCMTLGPUGarbageCollectionPool final {
using GCFunc = std::function<void(void)>;

Expand Down Expand Up @@ -298,7 +306,7 @@ class CCMTLGPUGarbageCollectionPool final {
}

protected:
//avoid cross-reference with CCMTLDevice
// avoid cross-reference with CCMTLDevice
std::function<uint8_t(void)> _getFrameIndex;
ccstd::queue<GCFunc> _releaseQueue[MAX_FRAMES_IN_FLIGHT];
};
Expand Down
20 changes: 13 additions & 7 deletions native/cocos/renderer/gfx-vulkan/VKDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ CC_DISABLE_WARNINGS()
#include "vk_mem_alloc.h"
#define THSVS_ERROR_CHECK_MIXED_IMAGE_LAYOUT
// remote potential hazard because of programmable blend
//#define THSVS_ERROR_CHECK_POTENTIAL_HAZARD
// #define THSVS_ERROR_CHECK_POTENTIAL_HAZARD
#define THSVS_SIMPLER_VULKAN_SYNCHRONIZATION_IMPLEMENTATION
#include "thsvs_simpler_vulkan_synchronization.h"
CC_ENABLE_WARNINGS()
Expand Down Expand Up @@ -642,6 +642,10 @@ void CCVKDevice::acquire(Swapchain *const *swapchains, uint32_t count) {
_gpuDescriptorSetHub->flush();
_gpuSemaphorePool->reset();

auto *bufferPool = gpuStagingBufferPool();
bufferPool->reset();
bufferPool->shrinkSize();

for (uint32_t i = 0; i < vkSwapchains.size(); ++i) {
VkSemaphore acquireSemaphore = _gpuSemaphorePool->alloc();
VkResult res = vkAcquireNextImageKHR(_gpuDevice->vkDevice, vkSwapchains[i], ~0ULL,
Expand Down Expand Up @@ -773,12 +777,14 @@ void CCVKDevice::initDeviceFeature() {
_features[toNumber(Feature::RASTERIZATION_ORDER_NOCOHERENT)] = true;
_features[toNumber(Feature::MULTI_SAMPLE_RESOLVE_DEPTH_STENCIL)] = checkExtension("VK_KHR_depth_stencil_resolve");

_gpuContext->debugReport = _gpuContext->checkExtension(VK_EXT_DEBUG_REPORT_EXTENSION_NAME) &&
_gpuContext->debugReport =
_gpuContext->checkExtension(VK_EXT_DEBUG_REPORT_EXTENSION_NAME) &&
checkExtension(VK_EXT_DEBUG_MARKER_EXTENSION_NAME) &&
(vkCmdDebugMarkerBeginEXT != nullptr) &&
(vkCmdDebugMarkerInsertEXT != nullptr) &&
(vkCmdDebugMarkerEndEXT != nullptr);
_gpuContext->debugUtils = _gpuContext->checkExtension(VK_EXT_DEBUG_UTILS_EXTENSION_NAME) &&
_gpuContext->debugUtils =
_gpuContext->checkExtension(VK_EXT_DEBUG_UTILS_EXTENSION_NAME) &&
(vkCmdBeginDebugUtilsLabelEXT != nullptr) &&
(vkCmdInsertDebugUtilsLabelEXT != nullptr) &&
(vkCmdEndDebugUtilsLabelEXT != nullptr);
Expand Down Expand Up @@ -1104,14 +1110,14 @@ SampleCount CCVKDevice::getMaxSampleCount(Format format, TextureUsage usage, Tex

VkImageFormatProperties imageFormatProperties = {};
vkGetPhysicalDeviceImageFormatProperties(_gpuContext->physicalDevice, vkFormat, VK_IMAGE_TYPE_2D,
VK_IMAGE_TILING_OPTIMAL, usages, 0, &imageFormatProperties);
VK_IMAGE_TILING_OPTIMAL, usages, 0, &imageFormatProperties);

if (imageFormatProperties.sampleCounts & VK_SAMPLE_COUNT_64_BIT) return SampleCount::X64;
if (imageFormatProperties.sampleCounts & VK_SAMPLE_COUNT_32_BIT) return SampleCount::X32;
if (imageFormatProperties.sampleCounts & VK_SAMPLE_COUNT_16_BIT) return SampleCount::X16;
if (imageFormatProperties.sampleCounts & VK_SAMPLE_COUNT_8_BIT) return SampleCount::X8;
if (imageFormatProperties.sampleCounts & VK_SAMPLE_COUNT_4_BIT) return SampleCount::X4;
if (imageFormatProperties.sampleCounts & VK_SAMPLE_COUNT_2_BIT) return SampleCount::X2;
if (imageFormatProperties.sampleCounts & VK_SAMPLE_COUNT_8_BIT) return SampleCount::X8;
if (imageFormatProperties.sampleCounts & VK_SAMPLE_COUNT_4_BIT) return SampleCount::X4;
if (imageFormatProperties.sampleCounts & VK_SAMPLE_COUNT_2_BIT) return SampleCount::X2;

return SampleCount::X1;
}
Expand Down
24 changes: 20 additions & 4 deletions native/cocos/renderer/gfx-vulkan/VKGPUObjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class CCVKGPUDeviceObject : public GFXDeviceObject<CCVKDeviceObjectDeleter> {
CCVKGPUDeviceObject() = default;
~CCVKGPUDeviceObject() = default;

virtual void shutdown(){};
virtual void shutdown() {};
};

template <typename T>
Expand All @@ -128,7 +128,7 @@ class CCVKGPURenderPass final : public CCVKGPUDeviceObject {
// helper storage
ccstd::vector<VkClearValue> clearValues;
ccstd::vector<VkSampleCountFlagBits> sampleCounts; // per subpass
ccstd::vector<bool> hasSelfDependency; // per subpass
ccstd::vector<bool> hasSelfDependency; // per subpass

const CCVKGPUGeneralBarrier *getBarrier(size_t index, CCVKGPUDevice *gpuDevice) const;
bool hasShadingAttachment(uint32_t subPassId) const;
Expand Down Expand Up @@ -697,8 +697,8 @@ class CCVKGPUCommandBufferPool final {
vkDestroyCommandPool(_device->vkDevice, pool.vkCommandPool, nullptr);
pool.vkCommandPool = VK_NULL_HANDLE;
}
for (auto &item: pool.usedCommandBuffers)item.clear();
for (auto &item: pool.commandBuffers)item.clear();
for (auto &item : pool.usedCommandBuffers) item.clear();
for (auto &item : pool.commandBuffers) item.clear();
}
_pools.clear();
}
Expand Down Expand Up @@ -839,6 +839,22 @@ class CCVKGPUStagingBufferPool final {
}
}

void shrinkSize(size_t minimalSize = CHUNK_SIZE) {
size_t reservedSize = 0;
for (auto iter = _pool.begin(); iter != _pool.end() && _pool.size() > 1;) {
if (reservedSize < minimalSize) {
reservedSize += iter->gpuBuffer->size;
++iter;
continue;
}
if (iter->curOffset == 0) {
iter = _pool.erase(iter);
} else {
++iter;
}
}
}

private:
struct Buffer {
IntrusivePtr<CCVKGPUBuffer> gpuBuffer;
Expand Down

0 comments on commit 152ec27

Please sign in to comment.