Files
test2/source/blender/gpu/vulkan/vk_resource_pool.hh
Jeroen Bakker ef3ceb3629 Vulkan: Resource Pools
This PR implements #126353; In short: keep discard list as part of swap chain images. This allows
better determination when resources are actually not in use anymore.

## Resource pool

Resource pools keep track of the resources for a swap chain image.

In Blender this is a bit more complicated due to the way GPUContext work. A single thread can have
multiple contexts. Some of them have a swap chain (GHOST Window) other don't (draw manager). The
resource pool should be shared between the contexts running on the same thread.

When opening multiple windows there are also multiple swap chains to consider.

### Discard pile

Resource handles that are deleted and stored in the discard pile. When we are sure that these
resources are not used on the GPU anymore these are destroyed.

### Reusable resources

There are other resources as well like:
- Descriptor sets
- Descriptor pools

## Open issues

There are some limitations that require future PRs to fix including:
- Background rendering
- Handling multiple windows
- Improve CPU/GPU synchronization
- Reuse staging buffers

Pull Request: https://projects.blender.org/blender/blender/pulls/126353
2024-08-19 15:37:48 +02:00

67 lines
2.0 KiB
C++

/* SPDX-FileCopyrightText: 2024 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup gpu
*/
#pragma once
#include "vk_common.hh"
#include "vk_descriptor_pools.hh"
namespace blender::gpu {
/**
* Pool of resources that are discarded, but can still be in used and cannot be destroyed.
*
* When GPU resources are deleted (`GPU_*_delete`) the GPU handles are kept inside a discard pool.
* When we are sure that the resource isn't used on the GPU anymore we can safely destroy it.
*
* When preparing the next frame, the previous frame can still be rendered. Resources that needs to
* be destroyed can only be when the previous frame has been completed and being displayed on the
* screen.
*/
class VKDiscardPool {
private:
Vector<std::pair<VkImage, VmaAllocation>> images_;
Vector<std::pair<VkBuffer, VmaAllocation>> buffers_;
Vector<VkImageView> image_views_;
Vector<VkShaderModule> shader_modules_;
Vector<VkPipelineLayout> pipeline_layouts_;
std::mutex mutex_;
public:
void deinit(VKDevice &device);
void discard_image(VkImage vk_image, VmaAllocation vma_allocation);
void discard_image_view(VkImageView vk_image_view);
void discard_buffer(VkBuffer vk_buffer, VmaAllocation vma_allocation);
void discard_shader_module(VkShaderModule vk_shader_module);
void discard_pipeline_layout(VkPipelineLayout vk_pipeline_layout);
/**
* Move discarded resources from src_pool into this.
*
* GPU resources that are discarded from the dependency graph are stored in the device orphaned
* data. When a swap chain context list is made active the orphaned data can be merged into a
* swap chain discard pool.
*/
void move_data(VKDiscardPool &src_pool);
void destroy_discarded_resources(VKDevice &device);
};
class VKResourcePool {
public:
VKDescriptorPools descriptor_pools;
VKDescriptorSetTracker descriptor_set;
VKDiscardPool discard_pool;
void init(VKDevice &device);
void deinit(VKDevice &device);
void reset();
};
} // namespace blender::gpu