Blender V5.3
mtl_index_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
9#include "mtl_index_buffer.hh"
10#include "mtl_context.hh"
11#include "mtl_debug.hh"
12#include "mtl_storage_buffer.hh"
13
14#include "BLI_span.hh"
15
16namespace blender::gpu {
17
18/* -------------------------------------------------------------------- */
21
23{
24 if (ibo_ != nullptr && !this->is_subrange_) {
25 ibo_->free();
26 }
27 this->free_optimized_buffer();
28
29 if (ssbo_wrapper_) {
30 delete ssbo_wrapper_;
31 ssbo_wrapper_ = nullptr;
32 }
33}
34
35void MTLIndexBuf::free_optimized_buffer()
36{
37 if (optimized_ibo_) {
38 optimized_ibo_->free();
39 optimized_ibo_ = nullptr;
40 }
41}
42
44{
45 /* Flag buffer as incompatible with optimized/patched buffers as contents
46 * can now have partial modifications from the GPU. */
47 this->flag_can_optimize(false);
48 this->free_optimized_buffer();
49
50 /* Ensure resource is initialized. */
51 this->upload_data();
52
53 /* Ensure we have a valid IBO. */
54 BLI_assert(this->ibo_);
55
56 /* Create MTLStorageBuffer to wrap this resource and use conventional binding. */
57 if (ssbo_wrapper_ == nullptr) {
58 /* Buffer's size in bytes is required to be multiple of 16. */
59 int multiple_of_16 = ceil_to_multiple_u(alloc_size_, 16);
60 ssbo_wrapper_ = new MTLStorageBuf(this, multiple_of_16);
61 }
62 ssbo_wrapper_->bind(binding);
63}
64
66{
67 if (ibo_ != nullptr) {
68 /* Fetch active context. */
70 BLI_assert(ctx);
71
72 /* Ensure data is flushed for host caches. */
73 id<MTLBuffer> source_buffer = ibo_->get_metal_buffer();
74 if (source_buffer.storageMode == MTLStorageModeManaged) {
76 [enc synchronizeResource:source_buffer];
77 }
78
79 /* Ensure GPU has finished operating on commands which may modify data. */
80 GPU_finish();
81
82 /* Read data. */
83 void *host_ptr = ibo_->get_host_ptr();
84 memcpy(data, host_ptr, size_get());
85 return;
86 }
87 BLI_assert(false && "Index buffer not ready to be read.");
88}
89
91{
92 /* Handle sub-range upload. */
93 if (is_subrange_) {
94 MTLIndexBuf *mtlsrc = static_cast<MTLIndexBuf *>(src_);
95 mtlsrc->upload_data();
96
97#ifndef NDEBUG
98 BLI_assert_msg(!mtlsrc->point_restarts_stripped_,
99 "Cannot use sub-range on stripped point buffer.");
100#endif
101
102 /* If parent sub-range allocation has changed,
103 * update our index buffer. */
104 if (alloc_size_ != mtlsrc->alloc_size_ || ibo_ != mtlsrc->ibo_) {
105
106 /* Update index buffer and allocation from source. */
107 alloc_size_ = mtlsrc->alloc_size_;
108 ibo_ = mtlsrc->ibo_;
109
110 /* Reset any allocated patched or optimized index buffers. */
111 this->free_optimized_buffer();
112 }
113 return;
114 }
115
116 /* If new data ready, and index buffer already exists, release current. */
117 if ((ibo_ != nullptr) && (this->data_ != nullptr)) {
118 MTL_LOG_DEBUG("Re-creating index buffer with new data. IndexBuf %p", this);
119 ibo_->free();
120 ibo_ = nullptr;
121 }
122
123 /* Prepare Buffer and Upload Data. */
124 if (ibo_ == nullptr) {
125 alloc_size_ = this->size_get();
126 if (alloc_size_ == 0) {
127 MTL_LOG_WARNING("Warning! Trying to allocate index buffer with size=0 bytes");
128 }
129 else {
130 if (data_) {
132 alloc_size_, true, data_);
133 }
134 else {
135 ibo_ = MTLContext::get_global_memory_manager()->allocate(alloc_size_, true);
136 }
137 BLI_assert(ibo_);
138#ifndef NDEBUG
139 static std::atomic<int> global_counter = 0;
140 int index = global_counter.fetch_add(1);
141 ibo_->set_label([NSString stringWithFormat:@"IBO %i", index]);
142#endif
143 }
144
145 /* No need to keep copy of data_ in system memory. */
146 if (data_) {
147 MEM_SAFE_DELETE_VOID(data_);
148 }
149 }
150}
151
153{
155
156 /* If host-side data still exists, modify and upload as normal */
157 if (data_ != nullptr) {
158
159 /* Free index buffer if one exists. */
160 if (ibo_ != nullptr && !this->is_subrange_) {
161 ibo_->free();
162 ibo_ = nullptr;
163 }
164
166
167 /* Apply start byte offset to data pointer. */
168 void *modified_base_ptr = data_;
169 uint8_t *ptr = static_cast<uint8_t *>(modified_base_ptr);
170 ptr += start;
171 modified_base_ptr = static_cast<void *>(ptr);
172
173 /* Modify host-side data. */
174 memcpy(modified_base_ptr, data, len);
175 return;
176 }
177
178 /* Verify buffer. */
179 BLI_assert(ibo_ != nullptr);
180
181 /* Otherwise, we will inject a data update, using staged data, into the command stream.
182 * Stage update contents in temporary buffer. */
184 BLI_assert(ctx);
186 memcpy(range.data, data, len);
187
188 /* Copy updated contents into primary buffer.
189 * These changes need to be uploaded via blit to ensure the data copies happen in-order. */
190 id<MTLBuffer> dest_buffer = ibo_->get_metal_buffer();
191 BLI_assert(dest_buffer != nil);
192
194 [enc copyFromBuffer:range.metal_buffer
195 sourceOffset:(uint32_t)range.buffer_offset
196 toBuffer:dest_buffer
197 destinationOffset:start
198 size:len];
199
200 /* Synchronize changes back to host to ensure CPU-side data is up-to-date for non
201 * Shared buffers. */
202 if (dest_buffer.storageMode == MTLStorageModeManaged) {
203 [enc synchronizeResource:dest_buffer];
204 }
205
206 /* Invalidate patched/optimized buffers. */
207 this->free_optimized_buffer();
208
209 /* Flag buffer as incompatible with optimized/patched buffers as contents
210 * have partial modifications. */
211 this->flag_can_optimize(false);
212}
213
215 uint source_first_index,
216 uint dest_first_index,
217 uint index_len)
218{
219 MTLIndexBuf &src = static_cast<MTLIndexBuf &>(source_buf);
223 "Index type mismatch between source and destination");
224 BLI_assert_msg(size_t(source_first_index) + index_len <= src.index_len_,
225 "Copy source range exceeds the source index buffer bounds");
226 BLI_assert_msg(size_t(dest_first_index) + index_len <= index_len_,
227 "Copy destination range exceeds the destination index buffer bounds");
228
229 if (index_len == 0) {
230 return;
231 }
232
233 BLI_assert(src.ibo_ != nullptr);
234 BLI_assert(ibo_ != nullptr);
235
236 const size_t src_offset = size_t(source_first_index) * to_bytesize(src.index_type_);
237 const size_t dst_offset = size_t(dest_first_index) * to_bytesize(index_type_);
238 const size_t copy_size = size_t(index_len) * to_bytesize(index_type_);
239
240 BLI_assert(src_offset + copy_size <= src.ibo_->get_size());
241 BLI_assert(dst_offset + copy_size <= ibo_->get_size());
242
244 BLI_assert(ctx);
245
246 id<MTLBuffer> src_buf = src.ibo_->get_metal_buffer();
247 id<MTLBuffer> dst_buf = ibo_->get_metal_buffer();
248 BLI_assert(src_buf != nil);
249 BLI_assert(dst_buf != nil);
250
252 [enc copyFromBuffer:src_buf
253 sourceOffset:src_offset
254 toBuffer:dst_buf
255 destinationOffset:dst_offset
256 size:copy_size];
257
258 if (dst_buf.storageMode == MTLStorageModeManaged) {
259 [enc synchronizeResource:dst_buf];
260 }
261
262 /* Invalidate patched/optimized buffers. */
263 this->free_optimized_buffer();
264
265 /* Flag buffer as incompatible with optimized/patched buffers as contents
266 * have partial modifications. */
267 this->flag_can_optimize(false);
268}
269
270void MTLIndexBuf::flag_can_optimize(bool can_optimize)
271{
272 can_optimize_ = can_optimize;
273
274 /* NOTE: Index buffer optimization needs to be disabled for Indirect draws, as the index count is
275 * unknown at submission time. However, if the index buffer has already been optimized by a
276 * separate draw pass, errors will occur and these cases need to be resolved at the high-level,
277 * ensuring primitive types without primitive restart are used instead, as these perform far
278 * more optimally on hardware. */
279 BLI_assert_msg(can_optimize_ || (optimized_ibo_ == nullptr),
280 "Index buffer optimization disabled, but optimal buffer already generated.");
281}
282
284
285/* -------------------------------------------------------------------- */
292
293/* Returns total vertices in new buffer. */
294template<typename T>
296 MutableSpan<T> output_data,
297 uint32_t input_index_len)
298{
299 /* Generate #TriangleList from #TriangleStrip. */
300 uint32_t current_vert_len = 0;
301 uint32_t current_output_ind = 0;
302 T indices[3];
303
304 for (int c_index = 0; c_index < input_index_len; c_index++) {
305 T current_index = original_data[c_index];
306 if (current_index == T(-1)) {
307 /* Stop current primitive. Move onto next. */
308 current_vert_len = 0;
309 }
310 else {
311 if (current_vert_len < 3) {
312 /* Prepare first triangle.
313 * Cache indices before generating a triangle, in case we have bad primitive-restarts. */
314 indices[current_vert_len] = current_index;
315 }
316
317 /* Emit triangle once we reach 3 input verts in current strip. */
318 if (current_vert_len == 3) {
319 /* First triangle in strip. */
320 output_data[current_output_ind++] = indices[0];
321 output_data[current_output_ind++] = indices[1];
322 output_data[current_output_ind++] = indices[2];
323 }
324 else if (current_vert_len > 3) {
325 /* All other triangles in strip.
326 * These triangles are populated using data from previous 2 vertices
327 * and the latest index. */
328 uint32_t tri_id = current_vert_len - 3;
329 uint32_t base_output_ind = current_output_ind;
330 if ((tri_id % 2) == 0) {
331 output_data[base_output_ind + 0] = output_data[base_output_ind - 2];
332 output_data[base_output_ind + 1] = current_index;
333 output_data[base_output_ind + 2] = output_data[base_output_ind - 1];
334 }
335 else {
336 output_data[base_output_ind + 0] = output_data[base_output_ind - 1];
337 output_data[base_output_ind + 1] = output_data[base_output_ind - 2];
338 output_data[base_output_ind + 2] = current_index;
339 }
340 current_output_ind += 3;
341 }
342
343 /* Increment relative vertex index. */
344 current_vert_len++;
345 }
346 }
347 return current_output_ind;
348}
349
350/* Returns total vertices in new buffer. */
351template<typename T>
353 MutableSpan<T> output_data,
354 uint32_t input_index_len)
355{
356 /* Generate #TriangleList from #TriangleFan. */
357 T base_prim_ind_val = 0;
358 uint32_t current_vert_len = 0;
359 uint32_t current_output_ind = 0;
360 T indices[3];
361
362 for (int c_index = 0; c_index < input_index_len; c_index++) {
363 T current_index = original_data[c_index];
364 if (current_index == T(-1)) {
365 /* Stop current primitive. Move onto next. */
366 current_vert_len = 0;
367 }
368 else {
369 if (current_vert_len < 3) {
370 /* Prepare first triangle.
371 * Cache indices before generating a triangle, in case we have bad primitive-restarts. */
372 indices[current_vert_len] = current_index;
373 }
374
375 /* emit triangle once we reach 3 input verts in current strip. */
376 if (current_vert_len == 3) {
377 /* First triangle in strip. */
378 output_data[current_output_ind++] = indices[0];
379 output_data[current_output_ind++] = indices[1];
380 output_data[current_output_ind++] = indices[2];
381 base_prim_ind_val = indices[0];
382 }
383 else if (current_vert_len > 3) {
384 /* All other triangles in strip.
385 * These triangles are populated using data from previous 2 vertices
386 * and the latest index. */
387 uint32_t base_output_ind = current_output_ind;
388
389 output_data[base_output_ind + 0] = base_prim_ind_val;
390 output_data[base_output_ind + 1] = output_data[base_output_ind - 1];
391 output_data[base_output_ind + 2] = current_index;
392 current_output_ind += 3;
393 }
394
395 /* Increment relative vertex index. */
396 current_vert_len++;
397 }
398 }
399 return current_output_ind;
400}
401
403 uint32_t &in_out_v_count)
404{
405 /* Determine whether to return the original index buffer, or whether we
406 * should emulate an unsupported primitive type, or optimize a restart-
407 * compatible type for faster performance. */
408 bool should_optimize_or_emulate = (in_out_primitive_type == GPU_PRIM_TRI_FAN) ||
409 (in_out_primitive_type == GPU_PRIM_TRI_STRIP);
410 if (!should_optimize_or_emulate || is_subrange_ || !can_optimize_) {
411 /* Ensure we are not optimized. */
412 BLI_assert(this->optimized_ibo_ == nullptr);
413
414 /* Return regular index buffer. */
415 BLI_assert(this->ibo_ && this->ibo_->get_metal_buffer());
416 return this->ibo_->get_metal_buffer();
417 }
418
419 /* Perform optimization on type. */
420 GPUPrimType input_prim_type = in_out_primitive_type;
421 this->upload_data();
422 if (!ibo_ && optimized_ibo_ == nullptr) {
423 /* Cannot optimize buffer if no source IBO exists. */
424 return nil;
425 }
426
427 /* Verify whether existing index buffer is valid. */
428 if (optimized_ibo_ != nullptr && optimized_primitive_type_ != input_prim_type) {
429 BLI_assert_msg(false,
430 "Cannot change the optimized primitive format after generation, as source "
431 "index buffer data is discarded.");
432 return nil;
433 }
434
435 /* Generate optimized index buffer. */
436 if (optimized_ibo_ == nullptr) {
437
438 /* Generate unwrapped index buffer. */
439 switch (input_prim_type) {
440 case GPU_PRIM_TRI_FAN: {
441
442 /* Calculate maximum size. */
443 uint32_t max_possible_verts = (this->index_len_ - 2) * 3;
444 BLI_assert(max_possible_verts > 0);
445
446 /* Allocate new buffer. */
448 max_possible_verts *
449 ((index_type_ == GPU_INDEX_U16) ? sizeof(uint16_t) : sizeof(uint32_t)),
450 true);
451
452 /* Populate new index buffer. */
453 if (index_type_ == GPU_INDEX_U16) {
454 Span<uint16_t> orig_data(static_cast<const uint16_t *>(ibo_->get_host_ptr()),
455 this->index_len_);
456 MutableSpan<uint16_t> output_data(
457 static_cast<uint16_t *>(optimized_ibo_->get_host_ptr()), max_possible_verts);
459 orig_data, output_data, this->index_len_);
460 }
461 else {
462 Span<uint32_t> orig_data(static_cast<const uint32_t *>(ibo_->get_host_ptr()),
463 this->index_len_);
464 MutableSpan<uint32_t> output_data(
465 static_cast<uint32_t *>(optimized_ibo_->get_host_ptr()), max_possible_verts);
467 orig_data, output_data, this->index_len_);
468 }
469
470 BLI_assert(emulated_v_count <= max_possible_verts);
471
472 /* Flush buffer and output. */
473 optimized_ibo_->flush();
474 optimized_primitive_type_ = input_prim_type;
475 in_out_v_count = emulated_v_count;
476 in_out_primitive_type = GPU_PRIM_TRIS;
477 }
478
479 case GPU_PRIM_TRI_STRIP: {
480
481 /* Calculate maximum size. */
482 uint32_t max_possible_verts = (this->index_len_ - 2) * 3;
483 BLI_assert(max_possible_verts > 0);
484
485 /* Allocate new buffer. */
487 max_possible_verts *
488 ((index_type_ == GPU_INDEX_U16) ? sizeof(uint16_t) : sizeof(uint32_t)),
489 true);
490
491 /* Populate new index buffer. */
492 if (index_type_ == GPU_INDEX_U16) {
493 Span<uint16_t> orig_data(static_cast<const uint16_t *>(ibo_->get_host_ptr()),
494 this->index_len_);
495 MutableSpan<uint16_t> output_data(
496 static_cast<uint16_t *>(optimized_ibo_->get_host_ptr()), max_possible_verts);
498 orig_data, output_data, this->index_len_);
499 }
500 else {
501 Span<uint32_t> orig_data(static_cast<const uint32_t *>(ibo_->get_host_ptr()),
502 this->index_len_);
503 MutableSpan<uint32_t> output_data(
504 static_cast<uint32_t *>(optimized_ibo_->get_host_ptr()), max_possible_verts);
506 orig_data, output_data, this->index_len_);
507 }
508
509 BLI_assert(emulated_v_count <= max_possible_verts);
510
511 /* Flush buffer and output. */
512 optimized_ibo_->flush();
513 optimized_primitive_type_ = input_prim_type;
514 in_out_v_count = emulated_v_count;
515 in_out_primitive_type = GPU_PRIM_TRIS;
516 } break;
517
518 case GPU_PRIM_LINE_STRIP: {
519 /* TODO(Metal): Line strip topology types would benefit from optimization to remove
520 * primitive restarts, however, these do not occur frequently, nor with
521 * significant geometry counts. */
522 MTL_LOG_DEBUG("TODO: Primitive topology: Optimize line strip topology types");
523 } break;
524
525 case GPU_PRIM_LINE_LOOP: {
526 /* TODO(Metal): Line Loop primitive type requires use of optimized index buffer for
527 * emulation, if used with indexed rendering. This path is currently not hit as #LineLoop
528 * does not currently appear to be used alongside an index buffer. */
530 "TODO: Primitive topology: Line Loop Index buffer optimization required for "
531 "emulation.");
532 } break;
533
534 case GPU_PRIM_TRIS:
535 case GPU_PRIM_LINES:
536 case GPU_PRIM_POINTS: {
537 /* Should not get here - TRIS/LINES/POINTS do not require emulation or optimization. */
539 return nil;
540 }
541
542 default:
543 /* Should not get here - Invalid primitive type. */
545 break;
546 }
547 }
548
549 /* Return optimized buffer. */
550 if (optimized_ibo_ != nullptr) {
551
552 /* Delete original buffer if one still exists, as we do no need it. */
553 if (ibo_ != nullptr) {
554 ibo_->free();
555 ibo_ = nullptr;
556 }
557
558 /* Output parameters. */
559 in_out_v_count = emulated_v_count;
560 in_out_primitive_type = GPU_PRIM_TRIS;
561 return optimized_ibo_->get_metal_buffer();
562 }
563 return nil;
564}
565
566void MTLIndexBuf::strip_restart_indices()
567{
568 /* We remove point buffer primitive restart indices by swapping restart indices
569 * with the first valid index at the end of the index buffer and reducing the
570 * length. Primitive restarts are invalid in Metal for non-restart-compatible
571 * primitive types. We also cannot just use zero unlike for Lines and Triangles,
572 * as we cannot create de-generative point primitives to hide geometry, as each
573 * point is independent.
574 * Instead, we must remove these hidden indices from the index buffer.
575 * NOTE: This happens prior to index squeezing so operate on 32-bit indices. */
576 MutableSpan<uint32_t> uint_idx(static_cast<uint32_t *>(data_), index_len_);
577 for (uint i = 0; i < index_len_; i++) {
578 if (uint_idx[i] == 0xFFFFFFFFu) {
579
580 /* Find swap index at end of index buffer. */
581 int swap_index = -1;
582 for (uint j = index_len_ - 1; j >= i && index_len_ > 0; j--) {
583 /* If end index is restart, just reduce length. */
584 if (uint_idx[j] == 0xFFFFFFFFu) {
585 index_len_--;
586 continue;
587 }
588 /* Otherwise assign swap index. */
589 swap_index = j;
590 break;
591 }
592
593 /* If index_len_ == 0, this means all indices were flagged as hidden, with restart index
594 * values. Hence we will entirely skip the draw. */
595 if (index_len_ > 0) {
596 /* If swap index is not valid, then there were no valid non-restart indices
597 * to swap with. However, the above loop will have removed these indices by
598 * reducing the length of indices. Debug assertions verify that the restart
599 * index is no longer included. */
600 if (swap_index == -1) {
602 }
603 else {
604 /* If we have found an index we can swap with, flip the values.
605 * We also reduce the length. As per above loop, swap_index should
606 * now be outside the index length range. */
607 uint32_t swap_index_value = uint_idx[swap_index];
608 uint_idx[i] = swap_index_value;
609 uint_idx[swap_index] = 0xFFFFFFFFu;
610 index_len_--;
611 BLI_assert(index_len_ <= swap_index);
612 }
613 }
614 }
615 }
616
617#ifndef NDEBUG
618 /* Flag as having been stripped to ensure invalid usage is tracked. */
619 point_restarts_stripped_ = true;
620#endif
621}
622
624
625} // namespace blender::gpu
#define BLI_assert_unreachable()
#define BLI_assert(a)
#define BLI_assert_msg(a, msg)
GPUIndexBufType index_type_
gpu::MTLBuffer * allocate_with_data(uint64_t size, bool cpu_visible, const void *data=nullptr)
gpu::MTLBuffer * allocate(uint64_t size, bool cpu_visible)
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()
void copy_sub(IndexBuf &source_buf, uint source_first_index, uint dest_first_index, uint index_len) override
Copy part of source index buffer into this index buffer.
void read(uint32_t *data) const override
id< MTLBuffer > get_index_buffer(GPUPrimType &in_out_primitive_type, uint &in_out_v_count)
void bind_as_ssbo(uint32_t binding) override
void update_sub(uint32_t start, uint32_t len, const void *data) override
void flag_can_optimize(bool can_optimize)
MTLTemporaryBuffer scratch_buffer_allocate_range(uint64_t alloc_size)
MTLStorageBuf(size_t size, GPUUsageType usage, const char *name)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
#define T
#define MTL_LOG_WARNING(info,...)
Definition mtl_debug.hh:39
#define MTL_LOG_DEBUG(info,...)
Definition mtl_debug.hh:46
static uint32_t populate_emulated_tri_fan_buf(Span< T > original_data, MutableSpan< T > output_data, uint32_t input_index_len)
static uint32_t populate_optimized_tri_strip_buf(Span< T > original_data, MutableSpan< T > output_data, uint32_t input_index_len)
MTLBufferRange MTLTemporaryBuffer
int to_bytesize(const DataFormat format)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned int uint
Definition BKE_text.h:55
@ GPU_PRIM_TRI_STRIP
@ GPU_PRIM_LINE_STRIP
@ GPU_PRIM_LINE_LOOP
unsigned short uint16_t
Definition BKE_text.h:80
unsigned int uint32_t
Definition BKE_text.h:81
BMesh const char void * data
unsigned char uint8_t
Definition BKE_text.h:79
MINLINE uint ceil_to_multiple_u(uint a, uint b)
void GPU_finish()
Definition gpu_state.cc:289