Changeset View
Changeset View
Standalone View
Standalone View
source/blender/gpu/metal/mtl_index_buffer.mm
| Show First 20 Lines • Show All 469 Lines • ▼ Show 20 Lines | void MTLIndexBuf::strip_restart_indices() | ||||
| * Instead, we must remove these hidden indices from the index buffer. | * Instead, we must remove these hidden indices from the index buffer. | ||||
| * NOTE: This happens prior to index squeezing so operate on 32-bit indices. */ | * NOTE: This happens prior to index squeezing so operate on 32-bit indices. */ | ||||
| MutableSpan<uint32_t> uint_idx(static_cast<uint32_t *>(data_), index_len_); | MutableSpan<uint32_t> uint_idx(static_cast<uint32_t *>(data_), index_len_); | ||||
| for (uint i = 0; i < index_len_; i++) { | for (uint i = 0; i < index_len_; i++) { | ||||
| if (uint_idx[i] == 0xFFFFFFFFu) { | if (uint_idx[i] == 0xFFFFFFFFu) { | ||||
| /* Find swap index at end of index buffer. */ | /* Find swap index at end of index buffer. */ | ||||
| int swap_index = -1; | int swap_index = -1; | ||||
| for (uint j = index_len_ - 1; j >= i; j--) { | for (uint j = index_len_ - 1; j >= i && index_len_ > 0; j--) { | ||||
| /* If end index is restart, just reduce length. */ | /* If end index is restart, just reduce length. */ | ||||
| if (uint_idx[j] == 0xFFFFFFFFu) { | if (uint_idx[j] == 0xFFFFFFFFu) { | ||||
| index_len_--; | index_len_--; | ||||
| continue; | continue; | ||||
| } | } | ||||
| /* Otherwise assign swap index. */ | /* Otherwise assign swap index. */ | ||||
| swap_index = j; | swap_index = j; | ||||
| break; | break; | ||||
| } | } | ||||
| /* If index_len_ == 0, this means all indices were flagged as hidden, with restart index | |||||
| * values. Hence we will entirely skip the draw. */ | |||||
| if (index_len_ > 0) { | |||||
| /* If swap index is not valid, then there were no valid non-restart indices | /* If swap index is not valid, then there were no valid non-restart indices | ||||
| * to swap with. However, the above loop will have removed these indices by | * to swap with. However, the above loop will have removed these indices by | ||||
| * reducing the length of indices. Debug assertions verify that the restart | * reducing the length of indices. Debug assertions verify that the restart | ||||
| * index is no longer included. */ | * index is no longer included. */ | ||||
| if (swap_index == -1) { | if (swap_index == -1) { | ||||
| BLI_assert(index_len_ <= i); | BLI_assert(index_len_ <= i); | ||||
| } | } | ||||
| else { | else { | ||||
| /* If we have found an index we can swap with, flip the values. | /* If we have found an index we can swap with, flip the values. | ||||
| * We also reduce the length. As per above loop, swap_index should | * We also reduce the length. As per above loop, swap_index should | ||||
| * now be outside the index length range. */ | * now be outside the index length range. */ | ||||
| uint32_t swap_index_value = uint_idx[swap_index]; | uint32_t swap_index_value = uint_idx[swap_index]; | ||||
| uint_idx[i] = swap_index_value; | uint_idx[i] = swap_index_value; | ||||
| uint_idx[swap_index] = 0xFFFFFFFFu; | uint_idx[swap_index] = 0xFFFFFFFFu; | ||||
| index_len_--; | index_len_--; | ||||
| BLI_assert(index_len_ <= swap_index); | BLI_assert(index_len_ <= swap_index); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| } | |||||
| #ifndef NDEBUG | #ifndef NDEBUG | ||||
| /* Flag as having been stripped to ensure invalid usage is tracked. */ | /* Flag as having been stripped to ensure invalid usage is tracked. */ | ||||
| point_restarts_stripped_ = true; | point_restarts_stripped_ = true; | ||||
| #endif | #endif | ||||
| } | } | ||||
| /** \} */ | /** \} */ | ||||
| } // blender::gpu | } // blender::gpu | ||||