Skip to content

Commit

Permalink
[modelimporter] texture loading now happens via the material (so it c…
Browse files Browse the repository at this point in the history
…an be offloaded to other threads later)
  • Loading branch information
PanosK92 committed Nov 25, 2024
1 parent 9be4a3e commit 18a3ab5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
13 changes: 7 additions & 6 deletions runtime/Rendering/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ namespace Spartan
void Material::Optimize()
{
SP_ASSERT_MSG(m_resource_state != ResourceState::Processing, "The material is already being processed");
SP_ASSERT_MSG(m_resource_state != ResourceState::Ready, "The material is already optimized");
SP_ASSERT_MSG(m_resource_state != ResourceState::Ready, "The material is already optimized");

m_resource_state = ResourceState::Processing;

Expand Down Expand Up @@ -369,9 +369,9 @@ namespace Spartan

if (!texture_packed)
{
bool textures_are_compressed = (texture_occlusion && texture_occlusion->IsCompressedFormat()) ||
(texture_roughness && texture_roughness->IsCompressedFormat()) ||
(texture_metalness && texture_metalness->IsCompressedFormat()) ||
bool textures_are_compressed = (texture_occlusion && texture_occlusion->IsCompressedFormat()) ||
(texture_roughness && texture_roughness->IsCompressedFormat()) ||
(texture_metalness && texture_metalness->IsCompressedFormat()) ||
(texture_height && texture_height->IsCompressedFormat());

RHI_Texture* texture_reference = texture_color ? texture_color :
Expand Down Expand Up @@ -434,9 +434,10 @@ namespace Spartan
}
}

// prepare all textures
future<void> texture_preparation_task = ThreadPool::AddTask([this]()
// PrepareForGpu() generates mips, compresses and uploads to GPU, so we offload it to a thread
ThreadPool::AddTask([this]()
{
// prepare all textures
for (RHI_Texture* texture : m_textures)
{
if (texture && texture->GetResourceState() == ResourceState::Max)
Expand Down
10 changes: 4 additions & 6 deletions runtime/Resource/Import/ModelImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,13 @@ namespace Spartan

if (texture)
{
// set cached texture
material->SetTexture(texture_type, texture);
}
else // if we didn't get a texture, it's not cached, hence we have to load it and cache it now
else
{
// load texture
texture = ResourceCache::Load<RHI_Texture>(deduced_path, RHI_Texture_Srv | RHI_Texture_Compress | RHI_Texture_DontOptimize);

// set the texture to the provided material
material->SetTexture(texture_type, texture);
// load new texture
material->SetTexture(texture_type, deduced_path, 0, RHI_Texture_DontOptimize);
}
}

Expand Down

0 comments on commit 18a3ab5

Please sign in to comment.