| 3302 | } |
| 3303 | |
| 3304 | RID RenderingDevice::index_buffer_create(uint32_t p_index_count, IndexBufferFormat p_format, Span<uint8_t> p_data, bool p_use_restart_indices, BitField<BufferCreationBits> p_creation_bits) { |
| 3305 | ERR_FAIL_COND_V(p_index_count == 0, RID()); |
| 3306 | |
| 3307 | IndexBuffer index_buffer; |
| 3308 | index_buffer.format = p_format; |
| 3309 | index_buffer.supports_restart_indices = p_use_restart_indices; |
| 3310 | index_buffer.index_count = p_index_count; |
| 3311 | uint32_t size_bytes = p_index_count * ((p_format == INDEX_BUFFER_FORMAT_UINT16) ? 2 : 4); |
| 3312 | #ifdef DEBUG_ENABLED |
| 3313 | if (p_data.size()) { |
| 3314 | index_buffer.max_index = 0; |
| 3315 | ERR_FAIL_COND_V_MSG((uint32_t)p_data.size() != size_bytes, RID(), |
| 3316 | "Default index buffer initializer array size (" + itos(p_data.size()) + ") does not match format required size (" + itos(size_bytes) + ")."); |
| 3317 | const uint8_t *r = p_data.ptr(); |
| 3318 | if (p_format == INDEX_BUFFER_FORMAT_UINT16) { |
| 3319 | const uint16_t *index16 = (const uint16_t *)r; |
| 3320 | for (uint32_t i = 0; i < p_index_count; i++) { |
| 3321 | if (p_use_restart_indices && index16[i] == 0xFFFF) { |
| 3322 | continue; // Restart index, ignore. |
| 3323 | } |
| 3324 | index_buffer.max_index = MAX(index16[i], index_buffer.max_index); |
| 3325 | } |
| 3326 | } else { |
| 3327 | const uint32_t *index32 = (const uint32_t *)r; |
| 3328 | for (uint32_t i = 0; i < p_index_count; i++) { |
| 3329 | if (p_use_restart_indices && index32[i] == 0xFFFFFFFF) { |
| 3330 | continue; // Restart index, ignore. |
| 3331 | } |
| 3332 | index_buffer.max_index = MAX(index32[i], index_buffer.max_index); |
| 3333 | } |
| 3334 | } |
| 3335 | } else { |
| 3336 | index_buffer.max_index = 0xFFFFFFFF; |
| 3337 | } |
| 3338 | #else |
| 3339 | index_buffer.max_index = 0xFFFFFFFF; |
| 3340 | #endif |
| 3341 | index_buffer.size = size_bytes; |
| 3342 | index_buffer.usage = (RDD::BUFFER_USAGE_TRANSFER_FROM_BIT | RDD::BUFFER_USAGE_TRANSFER_TO_BIT | RDD::BUFFER_USAGE_INDEX_BIT); |
| 3343 | if (p_creation_bits.has_flag(BUFFER_CREATION_DEVICE_ADDRESS_BIT)) { |
| 3344 | index_buffer.usage.set_flag(RDD::BUFFER_USAGE_DEVICE_ADDRESS_BIT); |
| 3345 | } |
| 3346 | index_buffer.driver_id = driver->buffer_create(index_buffer.size, index_buffer.usage, RDD::MEMORY_ALLOCATION_TYPE_GPU); |
| 3347 | ERR_FAIL_COND_V(!index_buffer.driver_id, RID()); |
| 3348 | |
| 3349 | // Index buffers are assumed to be immutable unless they don't have initial data. |
| 3350 | if (p_data.is_empty()) { |
| 3351 | index_buffer.draw_tracker = RDG::resource_tracker_create(); |
| 3352 | index_buffer.draw_tracker->buffer_driver_id = index_buffer.driver_id; |
| 3353 | } |
| 3354 | |
| 3355 | if (p_data.size()) { |
| 3356 | _buffer_initialize(&index_buffer, p_data); |
| 3357 | } |
| 3358 | |
| 3359 | _THREAD_SAFE_LOCK_ |
| 3360 | buffer_memory += index_buffer.size; |
| 3361 | _THREAD_SAFE_UNLOCK_ |
no test coverage detected