Blender V5.0
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
76 int binding,
78 StateManager *state_manager)
79 {
80 if (binding >= Offset) {
81 binding -= Offset;
82 }
83 if (bound_resources.size() <= binding) {
84 bound_resources.resize(binding + 1);
85 }
86 bound_resources[binding] = resource;
87 state_manager->image_formats[binding] = format;
88 }
89
90 VKTexture *get(int binding) const
91 {
92 if (binding >= Offset) {
93 binding -= Offset;
94 }
95 return bound_resources[binding];
96 }
97
98 void unbind(void *resource, StateManager *state_manager)
99 {
100 for (int index : IndexRange(bound_resources.size())) {
101 if (bound_resources[index] == resource) {
102 bound_resources[index] = nullptr;
103 state_manager->image_formats[index] = TextureWriteFormat::Invalid;
104 }
105 }
106 }
107
109 {
110 bound_resources.clear();
111 }
112};
113
116 public:
125 struct Elem {
127 void *resource;
128 VkDeviceSize offset;
129 };
131
132 void bind(Type resource_type, void *resource, int binding, VkDeviceSize offset)
133 {
134 if (bound_resources.size() <= binding) {
135 bound_resources.resize(binding + 1);
136 }
137 bound_resources[binding].resource_type = resource_type;
138 bound_resources[binding].resource = resource;
139 bound_resources[binding].offset = offset;
140 }
141
142 const Elem &get(int binding) const
143 {
144 return bound_resources[binding];
145 }
146
147 void unbind(void *resource)
148 {
149 for (int index : IndexRange(bound_resources.size())) {
150 if (bound_resources[index].resource == resource) {
151 bound_resources[index].resource = nullptr;
152 bound_resources[index].resource_type = Type::Unused;
153 bound_resources[index].offset = 0u;
154 }
155 }
156 }
157
159 {
160 bound_resources.clear();
161 }
162};
163
166 public:
178
179 void bind(Type resource_type, void *resource, GPUSamplerState sampler, int binding)
180 {
181 if (bound_resources.size() <= binding) {
182 bound_resources.resize(binding + 1, {});
183 }
184 bound_resources[binding].resource_type = resource_type;
185 bound_resources[binding].resource = resource;
186 bound_resources[binding].sampler = sampler;
187 }
188
189 const Elem *get(int binding) const
190 {
191 if (binding >= bound_resources.size()) {
192 /* TODO: Check with @Jeroen-Bakker.
193 * Could we ensure state_manager adds default initialized bindings for each ShaderInterface
194 * resource? (See #142097). */
195 return nullptr;
196 }
197 return &bound_resources[binding];
198 }
199
200 void unbind(void *resource)
201 {
202 for (int index : IndexRange(bound_resources.size())) {
203 if (bound_resources[index].resource == resource) {
204 bound_resources[index].resource = nullptr;
205 bound_resources[index].resource_type = Type::Unused;
207 }
208 }
209 }
210
212 {
213 bound_resources.clear();
214 }
215};
216
219
220 uint texture_unpack_row_length_ = 0;
221
222 BindSpaceTextures textures_;
224 BindSpaceUniformBuffers uniform_buffers_;
225 BindSpaceStorageBuffers storage_buffers_;
226
227 public:
228 bool is_dirty = false;
229
230 void apply_state() override;
231 void force_state() override;
232
233 void issue_barrier(GPUBarrier barrier_bits) override;
234
235 void texture_bind(Texture *tex, GPUSamplerState sampler, int unit) override;
236 void texture_unbind(Texture *tex) override;
237 void texture_unbind_all() override;
238
239 void image_bind(Texture *tex, int unit) override;
240 void image_unbind(Texture *tex) override;
241 void image_unbind_all() override;
242
243 void uniform_buffer_bind(VKUniformBuffer *uniform_buffer, int slot);
244 void uniform_buffer_unbind(VKUniformBuffer *uniform_buffer);
246
247 void texel_buffer_bind(VKVertexBuffer &vertex_buffer, int slot);
248 void texel_buffer_unbind(VKVertexBuffer &vertex_buffer);
249
251 void *resource,
252 int binding)
253 {
254 storage_buffer_bind(resource_type, resource, binding, 0);
255 }
257 void *resource,
258 int binding,
259 VkDeviceSize offset);
262
264
271};
272} // namespace blender::gpu
unsigned int uint
GPUBarrier
Definition GPU_state.hh:29
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)
Vector< VKUniformBuffer * > bound_resources
void bind(VKUniformBuffer *resource, int binding)
VKUniformBuffer * get(int binding) const
std::array< TextureWriteFormat, GPU_MAX_IMAGE > image_formats
void texel_buffer_bind(VKVertexBuffer &vertex_buffer, int slot)
void image_bind(Texture *tex, int unit) override
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 issue_barrier(GPUBarrier barrier_bits) override
void texture_unpack_row_length_set(uint len) override
#define resource
format
static constexpr int BIND_SPACE_IMAGE_OFFSET
static constexpr GPUSamplerState default_sampler()
uint len