sets textures in a compute program * Sets textures in a compute program, if the samplers exist. * * @name compute.set_textures * * @param path [type:hash|string] The path to the resource * @param textures [type:table] A table keyed by sampler name with texture resources as values. * * @examples * Set a texture in a compute program from a resource *
| 529 | * ``` |
| 530 | */ |
| 531 | static int SetComputeTexture(lua_State* L, ComputeResource* compute_res, dmhash_t name_hash, int texture_index) |
| 532 | { |
| 533 | texture_index = texture_index < 0 ? lua_gettop(L) + texture_index + 1 : texture_index; |
| 534 | |
| 535 | uint32_t unit = dmRender::GetComputeProgramSamplerUnit(compute_res->m_Program, name_hash); |
| 536 | if (unit == dmRender::INVALID_SAMPLER_UNIT) |
| 537 | { |
| 538 | return luaL_error(L, "Compute program sampler '%s' not found", dmHashReverseSafe64(name_hash)); |
| 539 | } |
| 540 | |
| 541 | dmhash_t texture_path = dmScript::CheckHashOrString(L, texture_index); |
| 542 | TextureResource* texture_res = (TextureResource*) CheckResource(L, g_ComputeModule.m_Factory, texture_path, "texturec"); |
| 543 | |
| 544 | dmGraphics::HContext graphics_context = dmRender::GetGraphicsContext(dmRender::GetProgramRenderContext(compute_res->m_Program)); |
| 545 | dmGraphics::TextureType texture_type_in = dmGraphics::GetTextureType(graphics_context, texture_res->m_Texture); |
| 546 | dmRender::HSampler sampler = dmRender::GetComputeProgramSampler(compute_res->m_Program, unit); |
| 547 | |
| 548 | dmRender::SamplerInfo sampler_info = {}; |
| 549 | dmRender::GetSamplerInfo(sampler, &sampler_info); |
| 550 | |
| 551 | dmGraphics::TextureType normalized_sampler_type = NormalizeBindableTextureType(sampler_info.m_TextureType); |
| 552 | |
| 553 | if (normalized_sampler_type != texture_type_in) |
| 554 | { |
| 555 | return luaL_error(L, "Texture type mismatch. Can't bind a %s texture to a %s sampler", |
| 556 | dmGraphics::GetTextureTypeLiteral(texture_type_in), |
| 557 | dmGraphics::GetTextureTypeLiteral(normalized_sampler_type)); |
| 558 | } |
| 559 | |
| 560 | compute_res->m_TextureResourcePaths[unit] = texture_path; |
| 561 | compute_res->m_SamplerNames[unit] = name_hash; |
| 562 | |
| 563 | if (compute_res->m_Textures[unit] != texture_res) |
| 564 | { |
| 565 | if (compute_res->m_Textures[unit]) |
| 566 | { |
| 567 | dmResource::Release(g_ComputeModule.m_Factory, compute_res->m_Textures[unit]); |
| 568 | } |
| 569 | dmResource::IncRef(g_ComputeModule.m_Factory, texture_res); |
| 570 | compute_res->m_Textures[unit] = texture_res; |
| 571 | } |
| 572 | |
| 573 | // Recalculate number of textures (similar to res_material.cpp) by checking which samplers exist |
| 574 | compute_res->m_NumTextures = 0; |
| 575 | for (uint32_t i = 0; i < dmRender::RenderObject::MAX_TEXTURE_COUNT; ++i) |
| 576 | { |
| 577 | uint32_t unit = dmRender::GetComputeProgramSamplerUnit(compute_res->m_Program, compute_res->m_SamplerNames[i]); |
| 578 | if (unit == dmRender::INVALID_SAMPLER_UNIT) |
| 579 | { |
| 580 | continue; |
| 581 | } |
| 582 | compute_res->m_NumTextures++; |
| 583 | } |
| 584 | |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | static int Compute_SetTextures(lua_State* L) |
no test coverage detected