Blender V5.3
vk_state_manager.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "gpu_state_private.hh"
12
13#include "BLI_array.hh"
14
16
17namespace blender::gpu {
18class VKTexture;
19class VKUniformBuffer;
20class VKVertexBuffer;
21class VKStorageBuffer;
22class VKIndexBuffer;
23class VKContext;
25class VKTopLevelAS;
26
34static constexpr int BIND_SPACE_IMAGE_OFFSET = 512;
35
38 public:
40
41 void bind(VKUniformBuffer *resource, int binding)
42 {
43 if (bound_resources.size() <= binding) {
44 bound_resources.resize(binding + 1);
45 }
46 bound_resources[binding] = resource;
47 }
48
49 VKUniformBuffer *get(int binding) const
50 {
51 return bound_resources[binding];
52 }
53
54 void unbind(void *resource)
55 {
56 for (int index : IndexRange(bound_resources.size())) {
57 if (bound_resources[index] == resource) {
58 bound_resources[index] = nullptr;
59 }
60 }
61 }
62
64 {
65 bound_resources.clear();
66 }
67};
68
72template<int Offset> class BindSpaceImages {
73 public:
75
76 void bind(VKTexture *resource,
77 int binding,
78 TextureWriteFormat format,
79 StateManager *state_manager)
80 {
81 if (binding >= Offset) {
82 binding -= Offset;
83 }
84 if (bound_resources.size() <= binding) {
85 bound_resources.resize(binding + 1);
86 }
87 bound_resources[binding] = resource;
88 state_manager->image_formats[binding] = format;
89 }
90
91 VKTexture *get(int binding) const
92 {
93 if (binding >= Offset) {
94 binding -= Offset;
95 }
96 return bound_resources[binding];
97 }
98
99 void unbind(void *resource, StateManager *state_manager)
100 {
101 for (int index : IndexRange(bound_resources.size())) {
102 if (bound_resources[index] == resource) {
103 bound_resources[index] = nullptr;
105 }
106 }
107 }
108
110 {
111 bound_resources.clear();
112 }
113};
114
117 public:
118 enum class Type {
119 Unused,
121 VertexBuffer,
122 IndexBuffer,
124 Buffer,
125 };
126 struct Elem {
128 void *resource;
129 VkDeviceSize offset;
130 };
132
133 void bind(Type resource_type, void *resource, int binding, VkDeviceSize offset)
134 {
135 if (bound_resources.size() <= binding) {
136 bound_resources.resize(binding + 1);
137 }
138 bound_resources[binding].resource_type = resource_type;
139 bound_resources[binding].resource = resource;
140 bound_resources[binding].offset = offset;
141 }
142
143 const Elem &get(int binding) const
144 {
145 return bound_resources[binding];
146 }
147
148 void unbind(void *resource)
149 {
150 for (int index : IndexRange(bound_resources.size())) {
151 if (bound_resources[index].resource == resource) {
152 bound_resources[index].resource = nullptr;
153 bound_resources[index].resource_type = Type::Unused;
154 bound_resources[index].offset = 0u;
155 }
156 }
157 }
158
160 {
161 bound_resources.clear();
162 }
163};
164
167 public:
168 enum class Type {
169 Unused,
171 VertexBuffer,
172 };
179
180 void bind(Type resource_type, void *resource, GPUSamplerState sampler, int binding)
181 {
182 if (bound_resources.size() <= binding) {
183 bound_resources.resize(binding + 1, {});
184 }
185 bound_resources[binding].resource_type = resource_type;
186 bound_resources[binding].resource = resource;
187 bound_resources[binding].sampler = sampler;
188 }
189
190 const Elem *get(int binding) const
191 {
192 if (binding >= bound_resources.size()) {
193 /* TODO: Check with @Jeroen-Bakker.
194 * Could we ensure state_manager adds default initialized bindings for each ShaderInterface
195 * resource? (See #142097). */
196 return nullptr;
197 }
198 return &bound_resources[binding];
199 }
200
201 void unbind(void *resource)
202 {
203 for (int index : IndexRange(bound_resources.size())) {
204 if (bound_resources[index].resource == resource) {
205 bound_resources[index].resource = nullptr;
206 bound_resources[index].resource_type = Type::Unused;
208 }
209 }
210 }
211
213 {
214 bound_resources.clear();
215 }
216};
217
219 public:
221
222 void bind(VKTopLevelAS *resource, int binding)
223 {
224 if (bound_resources.size() <= binding) {
225 bound_resources.resize(binding + 1);
226 }
227 bound_resources[binding] = resource;
228 }
229
230 VKTopLevelAS *get(int binding) const
231 {
232 return bound_resources[binding];
233 }
234
235 void unbind(void *resource)
236 {
237 for (int index : IndexRange(bound_resources.size())) {
238 if (bound_resources[index] == resource) {
239 bound_resources[index] = nullptr;
240 }
241 }
242 }
243
245 {
246 bound_resources.clear();
247 }
248};
249
253
254 BindSpaceTextures textures_;
256 BindSpaceUniformBuffers uniform_buffers_;
257 BindSpaceStorageBuffers storage_buffers_;
258 BindSpaceTopLevelAS acceleration_structures_;
259
260 public:
261 bool is_dirty = false;
262
263 void apply_state() override;
264 void force_state() override;
265
266 void issue_barrier(GPUBarrier barrier_bits) override;
267
268 void texture_bind(Texture *texture, GPUSamplerState sampler, int binding) override;
269 void texture_unbind(Texture *texture) override;
270 void texture_unbind_all() override;
271
272 void image_bind(Texture *texture, int binding) override;
273 void image_unbind(Texture *texture) override;
274 void image_unbind_all() override;
275
276 void uniform_buffer_bind(VKUniformBuffer *uniform_buffer, int binding);
277 void uniform_buffer_unbind(VKUniformBuffer *uniform_buffer);
279
280 void texel_buffer_bind(VKVertexBuffer &vertex_buffer, int binding);
281 void texel_buffer_unbind(VKVertexBuffer &vertex_buffer);
282
284 void *resource,
285 int binding)
286 {
287 storage_buffer_bind(resource_type, resource, binding, 0);
288 }
290 void *resource,
291 int binding,
292 VkDeviceSize offset);
293 void storage_buffer_unbind(void *resource);
295
296 void toplevelas_bind(VKTopLevelAS &tlas, int slot);
297 void toplevelas_unbind(void *resource);
298};
299} // namespace blender::gpu
constexpr IndexRange()=default
void bind(VKTexture *resource, int binding, TextureWriteFormat format, StateManager *state_manager)
void unbind(void *resource, StateManager *state_manager)
VKTexture * get(int binding) const
Vector< VKTexture * > bound_resources
void bind(Type resource_type, void *resource, int binding, VkDeviceSize offset)
const Elem & get(int binding) const
const Elem * get(int binding) const
void bind(Type resource_type, void *resource, GPUSamplerState sampler, int binding)
VKTopLevelAS * get(int binding) const
Vector< VKTopLevelAS * > bound_resources
void bind(VKTopLevelAS *resource, int binding)
Vector< VKUniformBuffer * > bound_resources
void bind(VKUniformBuffer *resource, int binding)
VKUniformBuffer * get(int binding) const
std::array< TextureWriteFormat, GPU_MAX_IMAGE > image_formats
void toplevelas_bind(VKTopLevelAS &tlas, int slot)
void texel_buffer_bind(VKVertexBuffer &vertex_buffer, int binding)
void image_bind(Texture *texture, int binding) override
void toplevelas_unbind(void *resource)
void uniform_buffer_unbind(VKUniformBuffer *uniform_buffer)
void texture_unbind(Texture *texture) override
void storage_buffer_unbind(void *resource)
void texel_buffer_unbind(VKVertexBuffer &vertex_buffer)
void uniform_buffer_bind(VKUniformBuffer *uniform_buffer, int binding)
void image_unbind(Texture *texture) override
void texture_bind(Texture *texture, GPUSamplerState sampler, int binding) override
void storage_buffer_bind(BindSpaceStorageBuffers::Type resource_type, void *resource, int binding)
void issue_barrier(GPUBarrier barrier_bits) override
#define sampler(slot)
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
static constexpr int BIND_SPACE_IMAGE_OFFSET
static constexpr GPUSamplerState default_sampler()
int size() const
Definition vector.hh:130
void resize(int new_size)
Definition vector.hh:94