Changeset View
Changeset View
Standalone View
Standalone View
source/blender/gpu/metal/mtl_index_buffer.mm
| Show First 20 Lines • Show All 92 Lines • ▼ Show 20 Lines | #endif | ||||
| /* Prepare Buffer and Upload Data. */ | /* Prepare Buffer and Upload Data. */ | ||||
| if (ibo_ == nullptr && data_ != nullptr) { | if (ibo_ == nullptr && data_ != nullptr) { | ||||
| alloc_size_ = this->size_get(); | alloc_size_ = this->size_get(); | ||||
| if (alloc_size_ == 0) { | if (alloc_size_ == 0) { | ||||
| MTL_LOG_WARNING("[Metal] Warning! Trying to allocate index buffer with size=0 bytes\n"); | MTL_LOG_WARNING("[Metal] Warning! Trying to allocate index buffer with size=0 bytes\n"); | ||||
| } | } | ||||
| else { | else { | ||||
| ibo_ = MTLContext::get_global_memory_manager().allocate_with_data(alloc_size_, true, data_); | ibo_ = MTLContext::get_global_memory_manager()->allocate_with_data(alloc_size_, true, data_); | ||||
| BLI_assert(ibo_); | BLI_assert(ibo_); | ||||
| ibo_->set_label(@"Index Buffer"); | ibo_->set_label(@"Index Buffer"); | ||||
| } | } | ||||
| /* No need to keep copy of data_ in system memory. */ | /* No need to keep copy of data_ in system memory. */ | ||||
| MEM_SAFE_FREE(data_); | MEM_SAFE_FREE(data_); | ||||
| } | } | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 225 Lines • ▼ Show 20 Lines | if (optimized_ibo_ == nullptr) { | ||||
| switch (input_prim_type) { | switch (input_prim_type) { | ||||
| case GPU_PRIM_TRI_FAN: { | case GPU_PRIM_TRI_FAN: { | ||||
| /* Calculate maximum size. */ | /* Calculate maximum size. */ | ||||
| uint32_t max_possible_verts = (this->index_len_ - 2) * 3; | uint32_t max_possible_verts = (this->index_len_ - 2) * 3; | ||||
| BLI_assert(max_possible_verts > 0); | BLI_assert(max_possible_verts > 0); | ||||
| /* Allocate new buffer. */ | /* Allocate new buffer. */ | ||||
| optimized_ibo_ = MTLContext::get_global_memory_manager().allocate( | optimized_ibo_ = MTLContext::get_global_memory_manager()->allocate( | ||||
| max_possible_verts * | max_possible_verts * | ||||
| ((index_type_ == GPU_INDEX_U16) ? sizeof(uint16_t) : sizeof(uint32_t)), | ((index_type_ == GPU_INDEX_U16) ? sizeof(uint16_t) : sizeof(uint32_t)), | ||||
| true); | true); | ||||
| /* Populate new index buffer. */ | /* Populate new index buffer. */ | ||||
| if (index_type_ == GPU_INDEX_U16) { | if (index_type_ == GPU_INDEX_U16) { | ||||
| Span<uint16_t> orig_data(static_cast<const uint16_t *>(ibo_->get_host_ptr()), | Span<uint16_t> orig_data(static_cast<const uint16_t *>(ibo_->get_host_ptr()), | ||||
| this->index_len_); | this->index_len_); | ||||
| MutableSpan<uint16_t> output_data( | MutableSpan<uint16_t> output_data( | ||||
| static_cast<uint16_t *>(optimized_ibo_->get_host_ptr()), this->index_len_); | static_cast<uint16_t *>(optimized_ibo_->get_host_ptr()), max_possible_verts); | ||||
| emulated_v_count = populate_emulated_tri_fan_buf<uint16_t>( | emulated_v_count = populate_emulated_tri_fan_buf<uint16_t>( | ||||
| orig_data, output_data, this->index_len_); | orig_data, output_data, this->index_len_); | ||||
| } | } | ||||
| else { | else { | ||||
| Span<uint32_t> orig_data(static_cast<const uint32_t *>(ibo_->get_host_ptr()), | Span<uint32_t> orig_data(static_cast<const uint32_t *>(ibo_->get_host_ptr()), | ||||
| this->index_len_); | this->index_len_); | ||||
| MutableSpan<uint32_t> output_data( | MutableSpan<uint32_t> output_data( | ||||
| static_cast<uint32_t *>(optimized_ibo_->get_host_ptr()), this->index_len_); | static_cast<uint32_t *>(optimized_ibo_->get_host_ptr()), max_possible_verts); | ||||
| emulated_v_count = populate_emulated_tri_fan_buf<uint32_t>( | emulated_v_count = populate_emulated_tri_fan_buf<uint32_t>( | ||||
| orig_data, output_data, this->index_len_); | orig_data, output_data, this->index_len_); | ||||
| } | } | ||||
| BLI_assert(emulated_v_count <= max_possible_verts); | BLI_assert(emulated_v_count <= max_possible_verts); | ||||
| /* Flush buffer and output. */ | /* Flush buffer and output. */ | ||||
| optimized_ibo_->flush(); | optimized_ibo_->flush(); | ||||
| optimized_primitive_type_ = input_prim_type; | optimized_primitive_type_ = input_prim_type; | ||||
| in_out_v_count = emulated_v_count; | in_out_v_count = emulated_v_count; | ||||
| in_out_primitive_type = GPU_PRIM_TRIS; | in_out_primitive_type = GPU_PRIM_TRIS; | ||||
| } | } | ||||
| case GPU_PRIM_TRI_STRIP: { | case GPU_PRIM_TRI_STRIP: { | ||||
| /* Calculate maximum size. */ | /* Calculate maximum size. */ | ||||
| uint32_t max_possible_verts = (this->index_len_ - 2) * 3; | uint32_t max_possible_verts = (this->index_len_ - 2) * 3; | ||||
| BLI_assert(max_possible_verts > 0); | BLI_assert(max_possible_verts > 0); | ||||
| /* Allocate new buffer. */ | /* Allocate new buffer. */ | ||||
| optimized_ibo_ = MTLContext::get_global_memory_manager().allocate( | optimized_ibo_ = MTLContext::get_global_memory_manager()->allocate( | ||||
| max_possible_verts * | max_possible_verts * | ||||
| ((index_type_ == GPU_INDEX_U16) ? sizeof(uint16_t) : sizeof(uint32_t)), | ((index_type_ == GPU_INDEX_U16) ? sizeof(uint16_t) : sizeof(uint32_t)), | ||||
| true); | true); | ||||
| /* Populate new index buffer. */ | /* Populate new index buffer. */ | ||||
| if (index_type_ == GPU_INDEX_U16) { | if (index_type_ == GPU_INDEX_U16) { | ||||
| Span<uint16_t> orig_data(static_cast<const uint16_t *>(ibo_->get_host_ptr()), | Span<uint16_t> orig_data(static_cast<const uint16_t *>(ibo_->get_host_ptr()), | ||||
| this->index_len_); | this->index_len_); | ||||
| MutableSpan<uint16_t> output_data( | MutableSpan<uint16_t> output_data( | ||||
| static_cast<uint16_t *>(optimized_ibo_->get_host_ptr()), this->index_len_); | static_cast<uint16_t *>(optimized_ibo_->get_host_ptr()), max_possible_verts); | ||||
| emulated_v_count = populate_optimized_tri_strip_buf<uint16_t>( | emulated_v_count = populate_optimized_tri_strip_buf<uint16_t>( | ||||
| orig_data, output_data, this->index_len_); | orig_data, output_data, this->index_len_); | ||||
| } | } | ||||
| else { | else { | ||||
| Span<uint32_t> orig_data(static_cast<const uint32_t *>(ibo_->get_host_ptr()), | Span<uint32_t> orig_data(static_cast<const uint32_t *>(ibo_->get_host_ptr()), | ||||
| this->index_len_); | this->index_len_); | ||||
| MutableSpan<uint32_t> output_data( | MutableSpan<uint32_t> output_data( | ||||
| static_cast<uint32_t *>(optimized_ibo_->get_host_ptr()), this->index_len_); | static_cast<uint32_t *>(optimized_ibo_->get_host_ptr()), max_possible_verts); | ||||
| emulated_v_count = populate_optimized_tri_strip_buf<uint32_t>( | emulated_v_count = populate_optimized_tri_strip_buf<uint32_t>( | ||||
| orig_data, output_data, this->index_len_); | orig_data, output_data, this->index_len_); | ||||
| } | } | ||||
| BLI_assert(emulated_v_count <= max_possible_verts); | BLI_assert(emulated_v_count <= max_possible_verts); | ||||
| /* Flush buffer and output. */ | /* Flush buffer and output. */ | ||||
| optimized_ibo_->flush(); | optimized_ibo_->flush(); | ||||
| ▲ Show 20 Lines • Show All 109 Lines • Show Last 20 Lines | |||||