Blender V5.3
mtl_vertex_buffer.mm
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include "mtl_debug.hh"
12#include "mtl_storage_buffer.hh"
13#include "mtl_vertex_buffer.hh"
14
15namespace blender::gpu {
16
18
23
25{
26 /* Discard previous data, if any. */
27 MEM_SAFE_DELETE(data_);
29 data_ = nullptr;
30 }
31 else {
33 }
34}
35
37{
39 data_ = nullptr;
40 }
41 else {
43 }
44}
45
47{
48 if (vbo_ != nullptr) {
49 vbo_->free();
50 vbo_ = nullptr;
51 is_wrapper_ = false;
52 }
53
54 GPU_TEXTURE_FREE_SAFE(buffer_texture_);
55
56 MEM_SAFE_DELETE(data_);
57
58 if (ssbo_wrapper_) {
59 delete ssbo_wrapper_;
60 ssbo_wrapper_ = nullptr;
61 }
62}
63
65{
66 this->bind();
67}
68
70{
71 /* Determine allocation size. Set minimum allocation size to be
72 * the maximal of a single attribute to avoid validation and
73 * correctness errors. */
74 uint64_t required_size_raw = sizeof(uchar) * this->size_used_get();
75 uint64_t required_size = max_ulul(required_size_raw, 128);
76
77 if (required_size_raw == 0) {
78 MTL_LOG_DEBUG("Vertex buffer required_size = 0");
79 }
80
81 /* If the vertex buffer has already been allocated, but new data is ready,
82 * or the usage size has changed, we release the existing buffer and
83 * allocate a new buffer to ensure we do not overwrite in-use GPU resources.
84 *
85 * NOTE: We only need to free the existing allocation if contents have been
86 * submitted to the GPU. Otherwise we can simply upload new data to the
87 * existing buffer, if it will fit.
88 *
89 * NOTE: If a buffer is re-sized, but no new data is provided, the previous
90 * contents are copied into the newly allocated buffer. */
91 bool requires_reallocation = (vbo_ != nullptr) && (alloc_size_ != required_size);
92 bool new_data_ready = (this->flag & GPU_VERTBUF_DATA_DIRTY) && this->data_;
93
94 gpu::MTLBuffer *prev_vbo = nullptr;
95 GPUVertBufStatus prev_flag = this->flag;
96
97 if (vbo_ != nullptr) {
98 if (requires_reallocation || (new_data_ready && contents_in_flight_)) {
99 /* Track previous VBO to copy data from. */
100 prev_vbo = vbo_;
101
102 /* Reset current allocation status. */
103 vbo_ = nullptr;
104 is_wrapper_ = false;
105 alloc_size_ = 0;
106
107 /* Flag as requiring data upload. */
108 if (requires_reallocation) {
110 }
111 }
112 }
113
114 /* Create MTLBuffer of requested size. */
115 if (vbo_ == nullptr) {
117 required_size, (this->get_usage_type() != GPU_USAGE_DEVICE_ONLY));
118#ifndef NDEBUG
119 static std::atomic<int> global_counter = 0;
120 int index = global_counter.fetch_add(1);
121 vbo_->set_label([NSString stringWithFormat:@"VBO %i", index]);
122#endif
123
124 BLI_assert(vbo_ != nullptr);
125 BLI_assert(vbo_->get_metal_buffer() != nil);
126
127 is_wrapper_ = false;
128 alloc_size_ = required_size;
129 contents_in_flight_ = false;
130 }
131
132 /* Upload new data, if provided. */
133 if (new_data_ready) {
134
135 /* Only upload data if usage size is greater than zero.
136 * Do not upload data for device-only buffers. */
137 if (required_size_raw > 0 && usage_ != GPU_USAGE_DEVICE_ONLY) {
138
139 /* Debug: Verify allocation is large enough. */
140 BLI_assert(vbo_->get_size() >= required_size_raw);
141
142 /* Fetch mapped buffer host ptr and upload data. */
143 void *dst_data = vbo_->get_host_ptr();
144 memcpy((uint8_t *)dst_data, this->data_, required_size_raw);
145 vbo_->flush_range(0, required_size_raw);
146 }
147
148 /* If static usage, free host-side data. */
149 if (usage_ == GPU_USAGE_STATIC) {
150 MEM_SAFE_DELETE(data_);
151 }
152
153 /* Flag data as having been uploaded. */
156 }
157 else if (requires_reallocation) {
158
159 /* If buffer has been re-sized, copy existing data if host
160 * data had been previously uploaded. */
161 BLI_assert(prev_vbo != nullptr);
162
163 if (prev_flag & GPU_VERTBUF_DATA_UPLOADED) {
164
165 /* Fetch active context. */
167 BLI_assert(ctx);
168
169 id<MTLBuffer> copy_prev_buffer = prev_vbo->get_metal_buffer();
170 id<MTLBuffer> copy_new_buffer = vbo_->get_metal_buffer();
171 BLI_assert(copy_prev_buffer != nil);
172 BLI_assert(copy_new_buffer != nil);
173
174 /* Ensure a blit command encoder is active for buffer copy operation. */
176 [enc copyFromBuffer:copy_prev_buffer
177 sourceOffset:0
178 toBuffer:copy_new_buffer
179 destinationOffset:0
180 size:min_ulul([copy_new_buffer length], [copy_prev_buffer length])];
181
182 /* Flush newly copied data back to host-side buffer, if one exists.
183 * Ensures data and cache coherency for managed MTLBuffers. */
184 if (copy_new_buffer.storageMode == MTLStorageModeManaged) {
185 [enc synchronizeResource:copy_new_buffer];
186 }
187
188 /* For VBOs flagged as static, release host data as it will no longer be needed. */
189 if (usage_ == GPU_USAGE_STATIC) {
190 MEM_SAFE_DELETE(data_);
191 }
192
193 /* Flag data as uploaded. */
195
196 /* Flag as in-use, as contents have been updated via GPU commands. */
197 this->flag_used();
198 }
199 }
200
201 /* Release previous buffer if re-allocated. */
202 if (prev_vbo != nullptr) {
203 prev_vbo->free();
204 }
205
206 /* Ensure buffer has been created. */
207 BLI_assert(vbo_ != nullptr);
208}
209
210/* Update Sub currently only used by hair */
211void MTLVertBuf::update_sub(uint start, uint len, const void *data)
212{
213 /* Fetch and verify active context. */
215 BLI_assert(ctx);
216 BLI_assert(ctx->device);
217
218 /* Ensure vertbuf has been created. */
219 this->bind();
220 BLI_assert(start + len <= alloc_size_);
221
222 /* Create temporary scratch buffer allocation for sub-range of data. */
223 MTLTemporaryBuffer scratch_allocation =
225 memcpy(scratch_allocation.data, data, len);
226 if ([scratch_allocation.metal_buffer storageMode] == MTLStorageModeManaged) {
227 [scratch_allocation.metal_buffer
228 didModifyRange:NSMakeRange(scratch_allocation.buffer_offset, len)];
229 }
230 id<MTLBuffer> data_buffer = scratch_allocation.metal_buffer;
231 uint64_t data_buffer_offset = scratch_allocation.buffer_offset;
232
233 BLI_assert(vbo_ != nullptr && data != nullptr);
234 BLI_assert((start + len) <= vbo_->get_size());
235
236 /* Fetch destination buffer. */
237 id<MTLBuffer> dst_buffer = vbo_->get_metal_buffer();
238
239 /* Ensure blit command encoder for copying data. */
241 [enc copyFromBuffer:data_buffer
242 sourceOffset:data_buffer_offset
243 toBuffer:dst_buffer
244 destinationOffset:start
245 size:len];
246
247 /* Flush modified buffer back to host buffer, if one exists. */
248 if (dst_buffer.storageMode == MTLStorageModeManaged) {
249 [enc synchronizeResource:dst_buffer];
250 }
251}
252
254 uint source_first_vertex,
255 uint dest_first_vertex,
257{
258 BLI_assert(format.stride == source_buf.format.stride);
259 BLI_assert_msg(size_t(source_first_vertex) + vertex_len <= source_buf.vertex_len,
260 "Copy source range exceeds the source vertex buffer bounds");
261 BLI_assert_msg(size_t(dest_first_vertex) + vertex_len <= this->vertex_len,
262 "Copy destination range exceeds the vertex buffer bounds");
263 MTLVertBuf &src = static_cast<MTLVertBuf &>(source_buf);
264 BLI_assert_msg(vbo_, "GPU_vertbuf_use() not called on this buffer");
265 BLI_assert_msg(src.vbo_, "GPU_vertbuf_use() not called on the source buffer");
266
267 const size_t src_offset = size_t(source_first_vertex) * src.format.stride;
268 const size_t dst_offset = size_t(dest_first_vertex) * format.stride;
269 const size_t copy_size = size_t(vertex_len) * format.stride;
270
271 if (copy_size == 0) {
272 return;
273 }
274
275 BLI_assert(src_offset + copy_size <= src.vbo_->get_size());
276 BLI_assert(dst_offset + copy_size <= vbo_->get_size());
277
278 /* Fetch active context. */
280 BLI_assert(ctx);
281
282 /* Fetch destination and source buffers. */
283 id<MTLBuffer> src_buf = src.vbo_->get_metal_buffer();
284 id<MTLBuffer> dst_buf = vbo_->get_metal_buffer();
285 BLI_assert(src_buf != nil);
286 BLI_assert(dst_buf != nil);
287
288 /* Ensure a blit command encoder is active for the buffer copy operation. */
290 [enc copyFromBuffer:src_buf
291 sourceOffset:src_offset
292 toBuffer:dst_buf
293 destinationOffset:dst_offset
294 size:copy_size];
295
296 /* Flush copied data back to the host-side buffer, if one exists.
297 * Ensures data and cache coherency for managed MTLBuffers. */
298 if (dst_buf.storageMode == MTLStorageModeManaged) {
299 [enc synchronizeResource:dst_buf];
300 }
301
302 /* Flag as in-use, as contents are in use by the GPU. */
303 this->flag_used();
304 src.flag_used();
305}
306
308{
309 this->flag_used();
310
311 /* Ensure resource is initialized. */
312 this->bind();
313
314 /* Create MTLStorageBuffer to wrap this resource and use conventional binding. */
315 if (ssbo_wrapper_ == nullptr) {
316 ssbo_wrapper_ = new MTLStorageBuf(this, ceil_to_multiple_u(alloc_size_, 16));
317 }
318 ssbo_wrapper_->bind(binding);
319}
320
322{
323 /* Ensure allocations are ready, and data uploaded. */
324 this->bind();
325 BLI_assert(vbo_ != nullptr);
326
327 /* If vertex buffer updated, release existing texture and re-create. */
328 id<MTLBuffer> buf = this->get_metal_buffer();
329 if (buffer_texture_ != nullptr) {
330 gpu::MTLTexture *mtl_buffer_tex = static_cast<gpu::MTLTexture *>(this->buffer_texture_);
331 id<MTLBuffer> tex_buf = mtl_buffer_tex->get_vertex_buffer();
332 if (tex_buf != buf) {
333 GPU_TEXTURE_FREE_SAFE(buffer_texture_);
334 buffer_texture_ = nullptr;
335 }
336 }
337
338 /* Create texture from vertex buffer. */
339 if (buffer_texture_ == nullptr) {
340 buffer_texture_ = GPU_texture_create_from_vertbuf("vertbuf_as_texture", this);
341 }
342
343 /* Verify successful creation and bind. */
344 BLI_assert(buffer_texture_ != nullptr);
345 GPU_texture_bind(buffer_texture_, binding);
346}
347
348void MTLVertBuf::read(void *data) const
349{
350 /* Fetch active context. */
352 BLI_assert(ctx);
353
354 BLI_assert(vbo_ != nullptr);
355
357
358 /* Ensure data is flushed for host caches. */
359 id<MTLBuffer> source_buffer = vbo_->get_metal_buffer();
360 if (source_buffer.storageMode == MTLStorageModeManaged) {
362 [enc synchronizeResource:source_buffer];
363 }
364
365 /* Ensure GPU has finished operating on commands which may modify data. */
366 GPU_finish();
367
368 /* Simple direct read. */
369 void *host_ptr = vbo_->get_host_ptr();
370 memcpy(data, host_ptr, this->size_used_get());
371 }
372 else {
373 /* Copy private data into temporary staging buffer. */
375 true);
376
377 id<MTLBuffer> source_buffer = vbo_->get_metal_buffer();
378 id<MTLBuffer> dest_buffer = dst_tmp_vbo_->get_metal_buffer();
379 BLI_assert(source_buffer != nil);
380 BLI_assert(dest_buffer != nil);
381
382 /* Ensure a blit command encoder is active for buffer copy operation. */
384 [enc copyFromBuffer:source_buffer
385 sourceOffset:0
386 toBuffer:dest_buffer
387 destinationOffset:0
388 size:min_ulul([source_buffer length], [dest_buffer length])];
389
390 /* Flush newly copied data back to host-side buffer, if one exists.
391 * Ensures data and cache coherency for managed MTLBuffers. */
392 if (dest_buffer.storageMode == MTLStorageModeManaged) {
393 [enc synchronizeResource:dest_buffer];
394 }
395
396 /* wait for GPU. */
397 GPU_finish();
398
399 /* Simple direct read. */
400 void *host_ptr = dst_tmp_vbo_->get_host_ptr();
401 memcpy(data, host_ptr, this->size_used_get());
402 dst_tmp_vbo_->free();
403 }
404}
405
407{
408 BLI_assert(vbo_ == nullptr);
409
410 /* Attempt to cast to Metal buffer handle. */
411 BLI_assert(handle != 0);
412 id<MTLBuffer> buffer = reinterpret_cast<id<MTLBuffer>>((void *)handle);
413
414 is_wrapper_ = true;
415 vbo_ = new gpu::MTLBuffer(buffer);
416
417 /* We assume the data is already on the device, so no need to allocate or send it. */
419}
420
422{
423 contents_in_flight_ = true;
424}
425
426MTLVertexFormat gpu_vertex_format_to_metal(VertAttrType vert_format)
427{
428#define CASE(a, b, c, blender_enum, d, e, mtl_vertex_enum, g, h) \
429 case VertAttrType::blender_enum: \
430 return MTLVertexFormat##mtl_vertex_enum;
431
432#define CASE_DEPRECATED(a, b, c, blender_enum, d, e, mtl_vertex_enum, g, h) \
433 case VertAttrType::blender_enum##_DEPRECATED: \
434 break;
435
436 switch (vert_format) {
440 break;
441 }
442#undef CASE
443#undef CASE_DEPRECATED
444 BLI_assert_msg(false, "Unrecognised GPU vertex format!\n");
445 return MTLVertexFormatInvalid;
446}
447
449{
450 using namespace shader;
451 switch (type) {
452 case Type::float_t:
453 return MTLVertexFormatFloat;
454 case Type::float2_t:
455 return MTLVertexFormatFloat2;
456 case Type::float3_t:
457 return MTLVertexFormatFloat3;
458 case Type::float4_t:
459 return MTLVertexFormatFloat4;
460 case Type::float3x3_t:
461 return MTLVertexFormatInvalid;
462 case Type::float4x4_t:
463 return MTLVertexFormatInvalid;
464 case Type::float3_10_10_10_2_t:
465 return MTLVertexFormatInvalid;
466 case Type::uchar_t:
467 return MTLVertexFormatUChar;
468 case Type::uchar2_t:
469 return MTLVertexFormatUChar2;
470 case Type::uchar3_t:
471 return MTLVertexFormatUChar3;
472 case Type::uchar4_t:
473 return MTLVertexFormatUChar4;
474 case Type::char_t:
475 return MTLVertexFormatChar;
476 case Type::char2_t:
477 return MTLVertexFormatChar2;
478 case Type::char3_t:
479 return MTLVertexFormatChar3;
480 case Type::char4_t:
481 return MTLVertexFormatChar4;
482 case Type::int_t:
483 return MTLVertexFormatInt;
484 case Type::int2_t:
485 return MTLVertexFormatInt2;
486 case Type::int3_t:
487 return MTLVertexFormatInt3;
488 case Type::int4_t:
489 return MTLVertexFormatInt4;
490 case Type::uint_t:
491 return MTLVertexFormatUInt;
492 case Type::uint2_t:
493 return MTLVertexFormatUInt2;
494 case Type::uint3_t:
495 return MTLVertexFormatUInt3;
496 case Type::uint4_t:
497 return MTLVertexFormatUInt4;
498 case Type::ushort_t:
499 return MTLVertexFormatUShort;
500 case Type::ushort2_t:
501 return MTLVertexFormatUShort2;
502 case Type::ushort3_t:
503 return MTLVertexFormatUShort3;
504 case Type::ushort4_t:
505 return MTLVertexFormatUShort4;
506 case Type::short_t:
507 return MTLVertexFormatShort;
508 case Type::short2_t:
509 return MTLVertexFormatShort2;
510 case Type::short3_t:
511 return MTLVertexFormatShort3;
512 case Type::short4_t:
513 return MTLVertexFormatShort4;
514 case Type::bool_t:
515 return MTLVertexFormatInvalid;
516 default:
517 BLI_assert(0);
518 return MTLVertexFormatInvalid;
519 }
520}
521
522} // namespace blender::gpu
#define CASE(a, size, c, blender_enum, d, e, f, g, h)
#define GPU_TEXTURE_FREE_SAFE(texture)
#define GPU_VERTEX_DEPRECATED_FORMAT_EXPAND(impl)
#define GPU_VERTEX_FORMAT_EXPAND(impl)
#define BLI_assert_msg(a, msg)
#define MEM_realloc_uninitialized(vmemh, len)
gpu::MTLBuffer * allocate(uint64_t size, bool cpu_visible)
void * get_host_ptr() const
id< MTLBuffer > get_metal_buffer() const
id< MTLBlitCommandEncoder > ensure_begin_blit_encoder()
static MTLContext * get()
MTLScratchBufferManager & get_scratch_buffer_manager()
MTLCommandBufferManager main_command_buffer
static MTLBufferPool * get_global_memory_manager()
MTLTemporaryBuffer scratch_buffer_allocate_range_aligned(uint64_t alloc_size, uint alignment)
MTLStorageBuf(size_t size, GPUUsageType usage, const char *name)
id< MTLBuffer > get_vertex_buffer() const
void wrap_handle(uint64_t handle) override
void bind_as_ssbo(uint binding) override
void copy_sub(VertBuf &source_buf, uint source_first_vertex, uint dest_first_vertex, uint vertex_len) override
Copy from part of source vertex buffer into the vertex buffer.
void update_sub(uint start, uint len, const void *data) override
void read(void *data) const override
void bind_as_texture(uint binding) override
GPUUsageType get_usage_type() const
MutableSpan< T > data()
float length(VecOp< float, D >) RET
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
#define MTL_LOG_DEBUG(info,...)
Definition mtl_debug.hh:46
#define CASE_DEPRECATED(a, b, c, blender_enum, d, e, mtl_vertex_enum, g, h)
MTLVertexFormat gpu_vertex_format_to_metal(VertAttrType vert_format)
MTLBufferRange MTLTemporaryBuffer
MTLVertexFormat gpu_type_to_metal_vertex_format(shader::Type type)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE unsigned long long max_ulul(unsigned long long a, unsigned long long b)
MINLINE unsigned long long min_ulul(unsigned long long a, unsigned long long b)
unsigned __int64 uint64_t
Definition BKE_text.h:91
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
unsigned char uchar
Definition BKE_text.h:58
gpu::Texture * GPU_texture_create_from_vertbuf(const char *name, gpu::VertBuf *vertex_buf)
unsigned char uint8_t
Definition BKE_text.h:79
@ GPU_VERTBUF_DATA_UPLOADED
void GPU_texture_bind(gpu::Texture *texture, int unit)
MINLINE uint ceil_to_multiple_u(uint a, uint b)
void GPU_finish()
Definition gpu_state.cc:289