Blender V5.3
gpu_uniform_buffer.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "MEM_guardedalloc.h"
10#include <cstdint>
11#include <cstring>
12
13#include "BLI_listbase.hh"
14#include "BLI_map.hh"
15#include "BLI_math_base_c.hh"
16#include "BLI_string.hh"
17
18#include "BKE_global.hh"
19
20#include "gpu_backend.hh"
21#include "gpu_node_graph.hh"
22
23#include "GPU_capabilities.hh"
24#include "GPU_context.hh"
25#include "GPU_material.hh"
27
28#include "GPU_uniform_buffer.hh"
31
32namespace blender {
33
34/* -------------------------------------------------------------------- */
37
38namespace gpu {
39
40UniformBuf::UniformBuf(size_t size, const char *name)
41{
42 /* Make sure that UBO is padded to size of vec4 */
43 BLI_assert((size % 16) == 0);
44
45 size_in_bytes_ = size;
46
47 STRNCPY(name_, name);
48}
49
51{
52 MEM_SAFE_DELETE_VOID(data_);
53}
54
55} // namespace gpu
56
58
59/* -------------------------------------------------------------------- */
62
67
69static constexpr size_t GPU_UBO_ALIGNMENT = 4;
70
71static bool gpu_type_is_ubo_scalar(const GPUType type)
72{
73 return gpu_type_element_count(type) == 1;
74}
75
77{
78 GPUInput *input = static_cast<GPUInput *>(link->data);
79 GPUType gputype = input->type;
80 /* Metal cannot pack scalars after vec3/int3. */
82 switch (gputype) {
83 case GPU_VEC3:
84 return GPU_VEC4;
85 case GPU_INT3:
86 return GPU_INT4;
87 default:
88 return gputype;
89 }
90 }
91 /* Unless vec3/int3 is followed by a scalar we need to treat it as a vec4/int4. */
92 if (ELEM(gputype, GPU_VEC3, GPU_INT3) && (link->next != nullptr)) {
93 const GPUType next_type = static_cast<GPUInput *>(link->next->data)->type;
94 if (!gpu_type_is_ubo_scalar(next_type)) {
95 return (gputype == GPU_VEC3) ? GPU_VEC4 : GPU_INT4;
96 }
97 }
98 return gputype;
99}
100
101static void gpu_constant_populate_ubo(void *destination,
103 const GPUType type)
104{
105 switch (type) {
106 case GPU_FLOAT:
107 case GPU_VEC2:
108 case GPU_VEC3:
109 case GPU_VEC4:
110 case GPU_MAT4: {
111 const Span<float> span = gpu_constant_to_float_span(data, type);
112 memcpy(destination, span.data(), size_t(span.size_in_bytes()));
113 return;
114 }
115 case GPU_INT:
116 case GPU_INT2:
117 case GPU_INT3:
118 case GPU_INT4: {
119 const Span<int> span = gpu_constant_to_int_span(data, type);
120 memcpy(destination, span.data(), size_t(span.size_in_bytes()));
121 return;
122 }
123 case GPU_BOOL: {
124 /* Pad bool to 4 bytes in UBO. */
125 const bool32_t value = gpu_constant_to_bool(data) ? 1 : 0;
126 memcpy(destination, &value, sizeof(value));
127 return;
128 }
129 default:
130 break;
131 }
132
134}
135
136static void buffer_reorder_scalar_after_size3_vec(ListBaseT<LinkData> *inputs,
137 LinkData *size3_vec_link,
138 Map<GPUType, LinkData *> &first_links)
139{
140 const GPUType size3_vec_type = static_cast<GPUInput *>(size3_vec_link->data)->type;
141 BLI_assert(ELEM(size3_vec_type, GPU_VEC3, GPU_INT3));
142
143 LinkData *link = size3_vec_link;
144 while (link != nullptr && static_cast<GPUInput *>(link->data)->type == size3_vec_type) {
145 LinkData *link_next = link->next;
146
147 /* If followed by nothing or a scalar, no need for alignment. */
148 if ((link_next == nullptr) ||
149 gpu_type_is_ubo_scalar(static_cast<GPUInput *>(link_next->data)->type))
150 {
151 break;
152 }
153
154 for (const GPUType scalar_type : {GPU_FLOAT, GPU_INT, GPU_BOOL}) {
155 LinkData **scalar_link_ptr = first_links.lookup_ptr(scalar_type);
156 if (scalar_link_ptr != nullptr && *scalar_link_ptr != nullptr) {
157 LinkData *scalar_input = *scalar_link_ptr;
158 first_links.add_overwrite(scalar_type, scalar_input->next);
159
160 BLI_remlink(inputs, scalar_input);
161 BLI_insertlinkafter(inputs, link, scalar_input);
162 break;
163 }
164 }
165
166 link = link_next;
167 }
168}
169
176static int inputs_cmp(const void *a, const void *b)
177{
178 const LinkData *link_a = static_cast<const LinkData *>(a),
179 *link_b = static_cast<const LinkData *>(b);
180 const GPUInput *input_a = static_cast<const GPUInput *>(link_a->data);
181 const GPUInput *input_b = static_cast<const GPUInput *>(link_b->data);
182 const int count_a = gpu_type_element_count(input_a->type);
183 const int count_b = gpu_type_element_count(input_b->type);
184 if (count_a != count_b) {
185 return count_a < count_b ? 1 : 0;
186 }
187 return input_a->type > input_b->type ? 1 : 0;
188}
189
190static inline bool is_ubo_supported_type(const GPUType type)
191{
192 switch (type) {
193 case GPU_FLOAT:
194 case GPU_VEC2:
195 case GPU_VEC3:
196 case GPU_VEC4:
197 case GPU_MAT4:
198 case GPU_INT:
199 case GPU_INT2:
200 case GPU_INT3:
201 case GPU_INT4:
202 case GPU_BOOL:
203 return true;
204 case GPU_NONE:
205 case GPU_MAT3:
206 case GPU_TEX1D_ARRAY:
207 case GPU_TEX2D:
208 case GPU_TEX2D_ARRAY:
209 case GPU_TEX3D:
210 case GPU_CLOSURE:
211 case GPU_ATTR:
213 case GPU_SHADING_DATA:
214 return false;
215 }
216
218 return false;
219}
220
225void GPU_uniformbuf_inputs_sort(ListBaseT<LinkData> *inputs)
226{
227 /* Order them as mat4, vec4, int4, vec3, int3, vec2, int2, float, int, bool. */
229
230 /* Metal cannot pack scalars after vec3/int3. */
232 return;
233 }
234
235 Map<GPUType, LinkData *> first_links;
236 GPUType cur_type = GPU_NONE;
237
238 for (LinkData &link : *inputs) {
239 GPUInput *input = static_cast<GPUInput *>(link.data);
240
241 if (input->type == GPU_MAT3) {
242 /* Alignment for mat3 is not handled currently, so not supported */
243 BLI_assert_msg(0, "mat3 not supported in UBO");
244 continue;
245 }
246
247 if (!is_ubo_supported_type(input->type)) {
248 BLI_assert_msg(0, "GPU type not supported in UBO");
249 continue;
250 }
251
252 if (input->type == cur_type) {
253 continue;
254 }
255
256 first_links.add_new(input->type, &link);
257 cur_type = input->type;
258 }
259
260 if (first_links.contains(GPU_VEC3)) {
261 buffer_reorder_scalar_after_size3_vec(inputs, first_links.lookup(GPU_VEC3), first_links);
262 }
263 if (first_links.contains(GPU_INT3)) {
264 buffer_reorder_scalar_after_size3_vec(inputs, first_links.lookup(GPU_INT3), first_links);
265 }
266}
267
268static inline size_t buffer_size_from_list(ListBaseT<LinkData> *inputs)
269{
270 size_t buffer_size = 0;
271 for (LinkData &link : *inputs) {
272 const GPUType gputype = get_padded_gpu_type(&link);
273 buffer_size += gpu_type_element_count(gputype) * GPU_UBO_ALIGNMENT;
274 }
275 /* Round up to size of vec4. (Opengl Requirement) */
276 size_t alignment = GPU_UBO_ALIGNMENT * 4;
277 buffer_size = divide_ceil_u(buffer_size, alignment) * alignment;
278
279 return buffer_size;
280}
281
282static inline void buffer_fill_from_list(void *data, ListBaseT<LinkData> *inputs)
283{
284 /* Now that we know the total ubo size we can start populating it. */
285 char *offset = static_cast<char *>(data);
286 for (LinkData &link : *inputs) {
287 GPUInput *input = static_cast<GPUInput *>(link.data);
288 const GPUType padded_type = get_padded_gpu_type(&link);
289 gpu_constant_populate_ubo(offset, input->constant_data, input->type);
290 offset += gpu_type_element_count(padded_type) * GPU_UBO_ALIGNMENT;
291 }
292}
293
295
296/* -------------------------------------------------------------------- */
299
300using namespace blender::gpu;
301
302gpu::UniformBuf *GPU_uniformbuf_create_ex(size_t size, const void *data, const char *name)
303{
304 UniformBuf *ubo = GPUBackend::get()->uniformbuf_alloc(size, name);
305 /* Direct init. */
306 if (data != nullptr) {
307 ubo->update(data);
308 }
309 else if (G.debug & G_DEBUG_GPU) {
310 /* Fill the buffer with poison values.
311 * (NaN for floats, -1 for `int` and "max value" for `uint`). */
312 Vector<uchar> uninitialized_data(size, 0xFF);
313 ubo->update(uninitialized_data.data());
314 }
315 return ubo;
316}
317
318gpu::UniformBuf *GPU_uniformbuf_create_from_list(ListBaseT<LinkData> *inputs, const char *name)
319{
320 /* There is no point on creating an UBO if there is no arguments. */
321 if (inputs->is_empty()) {
322 return nullptr;
323 }
324
326 size_t buffer_size = buffer_size_from_list(inputs);
327 void *data = MEM_new_uninitialized(buffer_size, __func__);
329
330 UniformBuf *ubo = nullptr;
331 if (buffer_size <= GPU_max_uniform_buffer_size()) {
332 ubo = GPUBackend::get()->uniformbuf_alloc(buffer_size, name);
333 /* Defer data upload. */
334 ubo->attach_data(data);
335 }
336 return ubo;
337}
338
340{
341 delete ubo;
342}
343
345{
346 ubo->update(data);
347}
348
350{
351 ubo->bind(slot);
352}
353
355{
356 ubo->bind_as_ssbo(slot);
357}
358
360{
361 ubo->unbind();
362}
363
368
373
375
376} // namespace blender
int32_t bool32_t
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define ELEM(...)
#define BLI_assert_msg(a, msg)
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
bool add_overwrite(const Key &key, const Value &value)
Definition BLI_map.hh:325
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:265
bool contains(const Key &key) const
Definition BLI_map.hh:353
constexpr int64_t size_in_bytes() const
Definition BLI_span.hh:272
constexpr const T * data() const
Definition BLI_span.hh:219
static Context * get()
virtual void debug_unbind_all_ubo()=0
virtual UniformBuf * uniformbuf_alloc(size_t size, const char *name)=0
static GPUBackend * get()
virtual void unbind()=0
virtual void update(const void *data)=0
UniformBuf(size_t size, const char *name)
virtual void bind(int slot)=0
virtual void bind_as_ssbo(int slot)=0
virtual void clear_to_zero()=0
BMesh Iterators.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void buffer_fill_from_list(void *data, ListBaseT< LinkData > *inputs)
static int inputs_cmp(const void *a, const void *b)
char * STRNCPY(char(&dst)[N], const char *src)
static constexpr size_t GPU_UBO_ALIGNMENT
Span< float > gpu_constant_to_float_span(const GPUInputConstantData &data, const GPUType type)
void GPU_uniformbuf_free(gpu::UniformBuf *ubo)
Span< int > gpu_constant_to_int_span(const GPUInputConstantData &data, const GPUType type)
GPUBackendType GPU_backend_get_type()
@ GPU_KERNEL_GLOBALS
size_t GPU_max_uniform_buffer_size()
Global G
Definition blender.cc:57
static void buffer_reorder_scalar_after_size3_vec(ListBaseT< LinkData > *inputs, LinkData *size3_vec_link, Map< GPUType, LinkData * > &first_links)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
BMesh const char void * data
void BLI_insertlinkafter(ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:339
bool gpu_constant_to_bool(const GPUInputConstantData &data)
static size_t buffer_size_from_list(ListBaseT< LinkData > *inputs)
void GPU_uniformbuf_bind_as_ssbo(gpu::UniformBuf *ubo, int slot)
void GPU_uniformbuf_bind(gpu::UniformBuf *ubo, int slot)
gpu::UniformBuf * GPU_uniformbuf_create_ex(size_t size, const void *data, const char *name)
void GPU_uniformbuf_update(gpu::UniformBuf *ubo, const void *data)
void void BLI_listbase_sort(ListBase *listbase, int(*cmp)(const void *, const void *)) ATTR_NONNULL(1
Definition listbase.cc:319
gpu::UniformBuf * GPU_uniformbuf_create_from_list(ListBaseT< LinkData > *inputs, const char *name)
static bool is_ubo_supported_type(const GPUType type)
MINLINE uint divide_ceil_u(uint a, uint b)
void GPU_uniformbuf_clear_to_zero(gpu::UniformBuf *ubo)
static void gpu_constant_populate_ubo(void *destination, const GPUInputConstantData &data, const GPUType type)
void GPU_uniformbuf_inputs_sort(ListBaseT< LinkData > *inputs)
void GPU_uniformbuf_debug_unbind_all()
static GPUType get_padded_gpu_type(LinkData *link)
void GPU_uniformbuf_unbind(gpu::UniformBuf *ubo)
static bool gpu_type_is_ubo_scalar(const GPUType type)
constexpr int gpu_type_element_count(const GPUType type)
std::variant< float, float2, float3, float4, float3x3, float4x4, int, int2, int3, int4, bool > GPUInputConstantData
struct LinkData * next