Blender V5.3
vk_texture_pool.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
12
13#include "vk_texture.hh"
14
15#include <list>
16
17namespace blender::gpu {
18
19/* Hashable struct describing a segment of allocated memory.. */
21 VkDeviceSize offset;
22 VkDeviceSize size;
23
24 uint64_t hash() const;
25 bool operator==(const VKMemorySegment &) const = default;
26};
27
28/* Hashable struct containing key information to identify or create a VkImage handle. */
30 VkImageCreateInfo create_info;
31 VmaAllocation allocation;
33
34 uint64_t hash() const;
35 bool operator==(const VKImageInfo &) const;
36};
37
38/* Map to VkImage handles bound to segments of specific allocations. */
40 /* Unused VkImages are eventually passed to discard pool, unless they are reused. */
41 static constexpr int max_unused_cycles_ = 8;
42
43 struct VKImageHandle {
45 /* Views of the image, kept between textures using it. They are destroyed with the image. */
46 Vector<VKImageView> image_views;
47 int unused_cycles_count = 0;
48 };
49
51
52 public:
53 /* Given key information identifying a particular VkImage, get or create it. */
55
56 /* Move the image views of a cached image out of the cache, for reuse by a texture. */
58
59 /* Move the image views of a released texture back into the cache. */
60 void store_image_views(const VKImageInfo &info, Vector<VKImageView> image_views);
61
62 /* Remove unused images and send them to the discard pool.
63 * If `force_free` is true, removes all images in the cache. */
64 void reset(bool force_reset = false);
65
66 /* Reset VKImageHandle internal counter. */
67 void reset_unused_cycles_count(const VKImageInfo &info);
68
69 /* Discard cached images bound to an allocation. */
70 void discard_all_of(VmaAllocation allocation);
71
72 uint64_t size() const
73 {
74 return cache_.size();
75 }
76};
77
79 /* Performed allocation size, current is 64mb. */
80 static constexpr VkDeviceSize allocation_size = 1 << 26;
81
82 /* Defer deallocation enough cycles to avoid interleaved calls to different viewport render
83 * functions (selection / display) causing constant allocation / deallocation (See #113024). */
84 static constexpr int max_unused_cycles_ = 8;
85
86 /* Struct to manage a VmaAllocation. The allocation is segmented
87 * for binding to multiple images at a time, and allows aliasing. */
88 struct AllocationHandle {
89 VmaAllocation allocation = VK_NULL_HANDLE;
90 VmaAllocationInfo allocation_info = {};
91
92 /* Counter to track the number of unused cycles before deallocation in `pool_`. */
93 int unused_cycles_count = 0;
94
95 /* Linked list of unused segments of the allocation. */
96 std::list<VKMemorySegment> segments;
97
98 /* Allocate/deallocate the handle internals. */
99 void alloc(VkMemoryRequirements memory_requirements);
100 void free();
101
102 /* Extract a segment of the allocation for binding, if compatible. */
103 std::optional<VKMemorySegment> acquire(VkMemoryRequirements memory_requirements);
104
105 /* Return a segment to the allocation for reuse. */
106 void release(VKMemorySegment segment);
107
108 /* Check if no part of the allocation is acquired. */
109 bool is_unused() const
110 {
111 return !segments.empty() && segments.front().size == allocation_info.size;
112 }
113
114 /* We use the pointer as hash/comparator, as a VmaAllocation is unique.
115 * This means we can find the handle without knowing other internals. */
116 uint64_t hash() const
117 {
118 return get_default_hash(allocation);
119 }
120
121 bool operator==(const AllocationHandle &o) const
122 {
123 return allocation == o.allocation;
124 }
125 };
126
127 /* Struct to manage an acquired texture. The texture has a backing image in
128 * the VKimageCache, which is bound to a segment of an allocation. */
129 struct TextureHandle {
130 VKTexture *texture = nullptr;
131 VKImageInfo image_info;
132
133 /* Counter to track texture acquire/retain mismatches in `acquire_`. */
134 int users_count = 1;
135
136 /* We use the pointer as hash/comparator, as a TextureHandle cannot be acquired twice.
137 * This means we can find the handle without knowing other internals */
138 uint64_t hash() const
139 {
141 }
142
143 bool operator==(const TextureHandle &o) const
144 {
145 return texture == o.texture;
146 }
147 };
148
149 /* Cache of VkImage handles to avoid repeated memory binding. */
150 std::optional<VKImageCache> image_cache_;
151 /* Allocated memory on which images are bound. */
152 Set<AllocationHandle> allocations_;
153 /* Texture handles currently in use. */
154 Set<TextureHandle> acquired_;
155
156 /* Debug storage to log memory usage. Log is only output
157 * if values have changed since the last `::reset()`. */
158 struct LogUsageData {
159 int64_t allocation_count = 0;
160 int64_t image_cache_size = 0;
161 VkDeviceSize acquired_segment_size = 0;
162 VkDeviceSize acquired_segment_size_max = 0;
163
164 bool operator==(const LogUsageData &o) const
165 {
166 return allocation_count == o.allocation_count && image_cache_size == o.image_cache_size &&
167 acquired_segment_size == o.acquired_segment_size &&
168 acquired_segment_size_max == o.acquired_segment_size_max;
169 }
170 };
171 LogUsageData previous_usage_data_ = {};
172 LogUsageData current_usage_data_ = {};
173
174 /* Output usage data to debug log. Called on `--debug-gpu` */
175 void log_usage_data();
176
177 protected:
179 int mip_len,
180 GPUTextureType type,
181 TextureFormat format,
183 const char *name = nullptr) override;
184
185 public:
188
189 void release_texture(Texture *texture) override;
190
191 void reset(bool force_free = false) override;
192
193 void offset_users_count(Texture *texture, int offset) override;
194};
195
196} // namespace blender::gpu
void reset_unused_cycles_count(const VKImageInfo &info)
VKResourceWithHandle< VkImage > get_or_create(const VKImageInfo &info)
Vector< VKImageView > take_image_views(const VKImageInfo &info)
void store_image_views(const VKImageInfo &info, Vector< VKImageView > image_views)
void reset(bool force_reset=false)
void discard_all_of(VmaAllocation allocation)
Texture * acquire_texture_impl(int3 extent, int mip_len, GPUTextureType type, TextureFormat format, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, const char *name=nullptr) override
void release_texture(Texture *texture) override
void reset(bool force_free=false) override
void offset_users_count(Texture *texture, int offset) override
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
void free(Subdiv *subdiv)
Definition subdiv.cc:192
bool operator==(const GPUState &a, const GPUState &b)
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
VecBase< int32_t, 3 > int3
@ GPU_TEXTURE_USAGE_GENERAL
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
#define hash
Definition noise_c.cc:156
VkImageCreateInfo create_info
bool operator==(const VKImageInfo &) const
bool operator==(const VKMemorySegment &) const =default