27 this->free_optimized_buffer();
31 ssbo_wrapper_ =
nullptr;
35void MTLIndexBuf::free_optimized_buffer()
38 optimized_ibo_->
free();
39 optimized_ibo_ =
nullptr;
48 this->free_optimized_buffer();
57 if (ssbo_wrapper_ ==
nullptr) {
62 ssbo_wrapper_->bind(binding);
67 if (ibo_ !=
nullptr) {
74 if (source_buffer.storageMode == MTLStorageModeManaged) {
76 [enc synchronizeResource:source_buffer];
83 void *host_ptr = ibo_->get_host_ptr();
87 BLI_assert(
false &&
"Index buffer not ready to be read.");
99 "Cannot use sub-range on stripped point buffer.");
104 if (alloc_size_ != mtlsrc->alloc_size_ || ibo_ != mtlsrc->ibo_) {
107 alloc_size_ = mtlsrc->alloc_size_;
111 this->free_optimized_buffer();
117 if ((ibo_ !=
nullptr) && (this->data_ !=
nullptr)) {
118 MTL_LOG_DEBUG(
"Re-creating index buffer with new data. IndexBuf %p",
this);
124 if (ibo_ ==
nullptr) {
126 if (alloc_size_ == 0) {
127 MTL_LOG_WARNING(
"Warning! Trying to allocate index buffer with size=0 bytes");
132 alloc_size_,
true, data_);
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]);
147 MEM_SAFE_DELETE_VOID(data_);
157 if (data_ !=
nullptr) {
168 void *modified_base_ptr = data_;
171 modified_base_ptr =
static_cast<void *
>(ptr);
174 memcpy(modified_base_ptr,
data,
len);
197 destinationOffset:start
202 if (dest_buffer.storageMode == MTLStorageModeManaged) {
203 [enc synchronizeResource:dest_buffer];
207 this->free_optimized_buffer();
215 uint source_first_index,
216 uint dest_first_index,
223 "Index type mismatch between source and destination");
225 "Copy source range exceeds the source index buffer bounds");
227 "Copy destination range exceeds the destination index buffer bounds");
229 if (index_len == 0) {
240 BLI_assert(src_offset + copy_size <= src.ibo_->get_size());
241 BLI_assert(dst_offset + copy_size <= ibo_->get_size());
252 [enc copyFromBuffer:src_buf
253 sourceOffset:src_offset
255 destinationOffset:dst_offset
258 if (dst_buf.storageMode == MTLStorageModeManaged) {
259 [enc synchronizeResource:dst_buf];
263 this->free_optimized_buffer();
272 can_optimize_ = can_optimize;
280 "Index buffer optimization disabled, but optimal buffer already generated.");
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)) {
308 current_vert_len = 0;
311 if (current_vert_len < 3) {
314 indices[current_vert_len] = current_index;
318 if (current_vert_len == 3) {
320 output_data[current_output_ind++] = indices[0];
321 output_data[current_output_ind++] = indices[1];
322 output_data[current_output_ind++] = indices[2];
324 else if (current_vert_len > 3) {
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];
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;
340 current_output_ind += 3;
347 return current_output_ind;
357 T base_prim_ind_val = 0;
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)) {
366 current_vert_len = 0;
369 if (current_vert_len < 3) {
372 indices[current_vert_len] = current_index;
376 if (current_vert_len == 3) {
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];
383 else if (current_vert_len > 3) {
387 uint32_t base_output_ind = current_output_ind;
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;
399 return current_output_ind;
408 bool should_optimize_or_emulate = (in_out_primitive_type ==
GPU_PRIM_TRI_FAN) ||
410 if (!should_optimize_or_emulate ||
is_subrange_ || !can_optimize_) {
415 BLI_assert(this->ibo_ && this->ibo_->get_metal_buffer());
416 return this->ibo_->get_metal_buffer();
420 GPUPrimType input_prim_type = in_out_primitive_type;
422 if (!ibo_ && optimized_ibo_ ==
nullptr) {
428 if (optimized_ibo_ !=
nullptr && optimized_primitive_type_ != input_prim_type) {
430 "Cannot change the optimized primitive format after generation, as source "
431 "index buffer data is discarded.");
436 if (optimized_ibo_ ==
nullptr) {
439 switch (input_prim_type) {
457 static_cast<uint16_t *
>(optimized_ibo_->get_host_ptr()), max_possible_verts);
465 static_cast<uint32_t *
>(optimized_ibo_->get_host_ptr()), max_possible_verts);
470 BLI_assert(emulated_v_count <= max_possible_verts);
473 optimized_ibo_->flush();
474 optimized_primitive_type_ = input_prim_type;
475 in_out_v_count = emulated_v_count;
496 static_cast<uint16_t *
>(optimized_ibo_->get_host_ptr()), max_possible_verts);
504 static_cast<uint32_t *
>(optimized_ibo_->get_host_ptr()), max_possible_verts);
509 BLI_assert(emulated_v_count <= max_possible_verts);
512 optimized_ibo_->flush();
513 optimized_primitive_type_ = input_prim_type;
514 in_out_v_count = emulated_v_count;
522 MTL_LOG_DEBUG(
"TODO: Primitive topology: Optimize line strip topology types");
530 "TODO: Primitive topology: Line Loop Index buffer optimization required for "
550 if (optimized_ibo_ !=
nullptr) {
553 if (ibo_ !=
nullptr) {
559 in_out_v_count = emulated_v_count;
561 return optimized_ibo_->get_metal_buffer();
566void MTLIndexBuf::strip_restart_indices()
578 if (uint_idx[i] == 0xFFFFFFFFu) {
584 if (uint_idx[j] == 0xFFFFFFFFu) {
600 if (swap_index == -1) {
607 uint32_t swap_index_value = uint_idx[swap_index];
608 uint_idx[i] = swap_index_value;
609 uint_idx[swap_index] = 0xFFFFFFFFu;
619 point_restarts_stripped_ =
true;
#define BLI_assert_unreachable()
#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 upload_data() override
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)
#define MTL_LOG_WARNING(info,...)
#define MTL_LOG_DEBUG(info,...)
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)
BMesh const char void * data
MINLINE uint ceil_to_multiple_u(uint a, uint b)
id< MTLBuffer > metal_buffer