set resource buffer * Sets the buffer of a resource. By default, setting the resource buffer will either copy the data from the incoming buffer object * to the buffer stored in the destination resource, or make a new buffer object if the sizes between the source buffer and the destination buffer * stored in the resource differs. In some cases, e.g performance reasons, it might be beneficial to
| 3362 | * ``` |
| 3363 | */ |
| 3364 | static int SetBuffer(lua_State* L) |
| 3365 | { |
| 3366 | int top = lua_gettop(L); |
| 3367 | dmhash_t path_hash = dmScript::CheckHashOrString(L, 1); |
| 3368 | dmScript::LuaHBuffer* luabuf = dmScript::CheckBuffer(L, 2); |
| 3369 | bool transfer_ownership = false; |
| 3370 | |
| 3371 | if (lua_istable(L, 3)) |
| 3372 | { |
| 3373 | lua_pushvalue(L, 3); |
| 3374 | transfer_ownership = CheckFieldValue<bool>(L, -1, "transfer_ownership", false); |
| 3375 | lua_pop(L, 1); // args table |
| 3376 | } |
| 3377 | |
| 3378 | dmBuffer::HBuffer src_buffer = dmGameSystem::UnpackLuaBuffer(luabuf); |
| 3379 | void* resource = CheckResource(L, g_ResourceModule.m_Factory, path_hash, "bufferc"); |
| 3380 | dmGameSystem::BufferResource* buffer_resource = (dmGameSystem::BufferResource*)resource; |
| 3381 | dmBuffer::HBuffer dst_buffer = buffer_resource->m_Buffer; |
| 3382 | |
| 3383 | if (transfer_ownership) |
| 3384 | { |
| 3385 | if (src_buffer != dst_buffer) |
| 3386 | { |
| 3387 | uint32_t src_count = 0; |
| 3388 | dmBuffer::Result br = dmBuffer::GetCount(src_buffer, &src_count); |
| 3389 | if (br != dmBuffer::RESULT_OK) |
| 3390 | { |
| 3391 | return luaL_error(L, "Unable to get buffer size for source buffer: %s (%d).", dmBuffer::GetResultString(br), br); |
| 3392 | } |
| 3393 | |
| 3394 | dmBuffer::Destroy(buffer_resource->m_Buffer); |
| 3395 | buffer_resource->m_Buffer = src_buffer; |
| 3396 | buffer_resource->m_ElementCount = src_count; |
| 3397 | buffer_resource->m_Stride = dmBuffer::GetStructSize(src_buffer); |
| 3398 | |
| 3399 | if (luabuf->m_Owner == dmScript::OWNER_RES) |
| 3400 | { |
| 3401 | // We are transferring the ownership of the resource in the lua buffer |
| 3402 | // to the destination resource, so we decref the source resource. |
| 3403 | dmResource::Release(g_ResourceModule.m_Factory, luabuf->m_BufferRes); |
| 3404 | } |
| 3405 | |
| 3406 | // We need to incref the destination resource in this case, |
| 3407 | // because otherwise the GC for the lua buffer will decref the resourec |
| 3408 | // and eventually prematurely destroy the resource. |
| 3409 | dmResource::IncRef(g_ResourceModule.m_Factory, resource); |
| 3410 | } |
| 3411 | |
| 3412 | luabuf->m_Owner = dmScript::OWNER_RES; |
| 3413 | luabuf->m_BufferRes = resource; |
| 3414 | luabuf->m_BufferResPathHash = path_hash; |
| 3415 | luabuf->m_BufferResVersion = dmResource::GetVersion(g_ResourceModule.m_Factory, resource); |
| 3416 | } |
| 3417 | else |
| 3418 | { |
| 3419 | // Make sure the destination buffer has enough size (otherwise, resize it). |
| 3420 | // TODO: Check if incoming buffer size is smaller than current size -> don't allocate new dmbuffer, |
| 3421 | // but copy smaller data and change "size". |
nothing calls this directly
no test coverage detected