Blender V4.5
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;
25
33static constexpr int BIND_SPACE_IMAGE_OFFSET = 512;
34
37 public:
39
40 void bind(VKUniformBuffer *resource, int binding)
41 {
42 if (bound_resources.size() <= binding) {
43 bound_resources.resize(binding + 1);
44 }
45 bound_resources[binding] = resource;
46 }
47
48 VKUniformBuffer *get(int binding) const
49 {
50 return bound_resources[binding];
51 }
52
53 void unbind(void *resource)
54 {
55 for (int index : IndexRange(bound_resources.size())) {
56 if (bound_resources[index] == resource) {
57 bound_resources[index] = nullptr;
58 }
59 }
60 }
61
63 {
64 bound_resources.clear();
65 }
66};
67
71template<int Offset> class BindSpaceImages {
72 public:
74
75 void bind(VKTexture *resource, int binding)
76 {
77 if (binding >= Offset) {
78 binding -= Offset;
79 }
80 if (bound_resources.size() <= binding) {
81 bound_resources.resize(binding + 1);
82 }
83 bound_resources[binding] = resource;
84 }
85
86 VKTexture *get(int binding) const
87 {
88 if (binding >= Offset) {
89 binding -= Offset;
90 }
91 return bound_resources[binding];
92 }
93
94 void unbind(void *resource)
95 {
96 for (int index : IndexRange(bound_resources.size())) {
97 if (bound_resources[index] == resource) {
98 bound_resources[index] = nullptr;
99 }
100 }
101 }
102
104 {
105 bound_resources.clear();
106 }
107};
108
111 public:
120 struct Elem {
122 void *resource;
123 VkDeviceSize offset;
124 };
126
127 void bind(Type resource_type, void *resource, int binding, VkDeviceSize offset)
128 {
129 if (bound_resources.size() <= binding) {
130 bound_resources.resize(binding + 1);
131 }
132 bound_resources[binding].resource_type = resource_type;
133 bound_resources[binding].resource = resource;
134 bound_resources[binding].offset = offset;
135 }
136
137 const Elem &get(int binding) const
138 {
139 return bound_resources[binding];
140 }
141
142 void unbind(void *resource)
143 {
144 for (int index : IndexRange(bound_resources.size())) {
145 if (bound_resources[index].resource == resource) {
146 bound_resources[index].resource = nullptr;
147 bound_resources[index].resource_type = Type::Unused;
148 bound_resources[index].offset = 0u;
149 }
150 }
151 }
152
154 {
155 bound_resources.clear();
156 }
157};
158
161 public:
173
174 void bind(Type resource_type, void *resource, GPUSamplerState sampler, int binding)
175 {
176 if (bound_resources.size() <= binding) {
177 bound_resources.resize(binding + 1, {});
178 }
179 bound_resources[binding].resource_type = resource_type;
180 bound_resources[binding].resource = resource;
181 bound_resources[binding].sampler = sampler;
182 }
183
184 const Elem *get(int binding) const
185 {
186 if (binding >= bound_resources.size()) {
187 /* TODO: Check with @Jeroen-Bakker.
188 * Could we ensure state_manager adds default initialized bindings for each ShaderInterface
189 * resource? (See #142097). */
190 return nullptr;
191 }
192 return &bound_resources[binding];
193 }
194
195 void unbind(void *resource)
196 {
197 for (int index : IndexRange(bound_resources.size())) {
198 if (bound_resources[index].resource == resource) {
199 bound_resources[index].resource = nullptr;
200 bound_resources[index].resource_type = Type::Unused;
202 }
203 }
204 }
205
207 {
208 bound_resources.clear();
209 }
210};
211
214
215 uint texture_unpack_row_length_ = 0;
216
217 BindSpaceTextures textures_;
219 BindSpaceUniformBuffers uniform_buffers_;
220 BindSpaceStorageBuffers storage_buffers_;
221
222 public:
223 bool is_dirty = false;
224
225 void apply_state() override;
226 void force_state() override;
227
228 void issue_barrier(eGPUBarrier barrier_bits) override;
229
230 void texture_bind(Texture *tex, GPUSamplerState sampler, int unit) override;
231 void texture_unbind(Texture *tex) override;
232 void texture_unbind_all() override;
233
234 void image_bind(Texture *tex, int unit) override;
235 void image_unbind(Texture *tex) override;
236 void image_unbind_all() override;
237
238 void uniform_buffer_bind(VKUniformBuffer *uniform_buffer, int slot);
239 void uniform_buffer_unbind(VKUniformBuffer *uniform_buffer);
241
242 void texel_buffer_bind(VKVertexBuffer &vertex_buffer, int slot);
243 void texel_buffer_unbind(VKVertexBuffer &vertex_buffer);
244
246 void *resource,
247 int binding)
248 {
249 storage_buffer_bind(resource_type, resource, binding, 0u);
250 }
252 void *resource,
253 int binding,
254 VkDeviceSize offset);
257
259
261
268};
269} // namespace blender::gpu
unsigned int uint
eGPUBarrier
Definition GPU_state.hh:29
VKTexture * get(int binding) const
Vector< VKTexture * > bound_resources
void bind(VKTexture *resource, int binding)
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)
Vector< VKUniformBuffer * > bound_resources
void bind(VKUniformBuffer *resource, int binding)
VKUniformBuffer * get(int binding) const
void texel_buffer_bind(VKVertexBuffer &vertex_buffer, int slot)
void issue_barrier(eGPUBarrier barrier_bits) override
void image_bind(Texture *tex, int unit) override
void unbind_from_all_namespaces(void *resource)
void uniform_buffer_unbind(VKUniformBuffer *uniform_buffer)
void storage_buffer_unbind(void *resource)
void uniform_buffer_bind(VKUniformBuffer *uniform_buffer, int slot)
void texture_unbind(Texture *tex) override
void texel_buffer_unbind(VKVertexBuffer &vertex_buffer)
void texture_bind(Texture *tex, GPUSamplerState sampler, int unit) override
void image_unbind(Texture *tex) override
void storage_buffer_bind(BindSpaceStorageBuffers::Type resource_type, void *resource, int binding)
void texture_unpack_row_length_set(uint len) override
#define resource
static constexpr int BIND_SPACE_IMAGE_OFFSET
static constexpr GPUSamplerState default_sampler()
uint len