Skip to content

Commit

Permalink
Don't free imported materials for replacements on map change
Browse files Browse the repository at this point in the history
  • Loading branch information
sultim-t committed Nov 18, 2023
1 parent ec897d6 commit 7180682
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 18 deletions.
7 changes: 5 additions & 2 deletions Source/GltfImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ namespace
uint32_t frameIndex,
const cgltf_material* mat,
TextureManager& textureManager,
bool isReplacement,
const std::filesystem::path& gltfFolder,
std::string_view gltfPath )
{
Expand Down Expand Up @@ -726,7 +727,7 @@ namespace
if( !materialName.empty() )
{
textureManager.TryCreateImportedMaterial(
cmd, frameIndex, materialName, fullPaths, samplers, pbrSwizzling );
cmd, frameIndex, materialName, fullPaths, samplers, pbrSwizzling, isReplacement );
}

if( auto t = mat->pbr_metallic_roughness.metallic_roughness_texture.texture )
Expand Down Expand Up @@ -967,6 +968,7 @@ RTGL1::GltfImporter::~GltfImporter()
auto RTGL1::GltfImporter::ParseFile( VkCommandBuffer cmdForTextures,
uint32_t frameIndex,
TextureManager& textureManager,
bool isReplacement,
const TextureMetaManager& textureMeta ) const -> WholeModelFile
{
cgltf_node* mainNode = FindMainRootNode( data );
Expand Down Expand Up @@ -1042,7 +1044,7 @@ auto RTGL1::GltfImporter::ParseFile( VkCommandBuffer cmdForTextures,

// mesh
auto AppendMeshPrimitives =
[ this, &cmdForTextures, &frameIndex, &textureManager, &textureMeta ](
[ this, &cmdForTextures, &frameIndex, &textureManager, &isReplacement, & textureMeta ](
std::vector< WholeModelFile::RawModelData::RawPrimitiveData >& target,
const cgltf_node* atnode,
const RgTransform* transform ) {
Expand Down Expand Up @@ -1112,6 +1114,7 @@ auto RTGL1::GltfImporter::ParseFile( VkCommandBuffer cmdForTextures,
frameIndex,
srcPrim.material,
textureManager,
isReplacement,
gltfFolder,
gltfPath );

Expand Down
1 change: 1 addition & 0 deletions Source/GltfImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class GltfImporter
[[nodiscard]] auto ParseFile( VkCommandBuffer cmdForTextures,
uint32_t frameIndex,
TextureManager& textureManager,
bool isReplacement,
const TextureMetaManager& textureMeta ) const -> WholeModelFile;

explicit operator bool() const;
Expand Down
7 changes: 3 additions & 4 deletions Source/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,7 @@ void RTGL1::Scene::NewScene( VkCommandBuffer cmd,
staticLights.clear();

{
// TODO: free only static-scene related if !reimportReplacements
textureManager.FreeAllImportedMaterials( frameIndex );
textureManager.FreeAllImportedMaterials( frameIndex, reimportReplacements );
}

assert( !makingStatic );
Expand All @@ -536,7 +535,7 @@ void RTGL1::Scene::NewScene( VkCommandBuffer cmd,
{
if( auto i = GltfImporter{ path, worldTransform, worldScale } )
{
auto wholeGltf = i.ParseFile( cmd, frameIndex, textureManager, textureMeta );
auto wholeGltf = i.ParseFile( cmd, frameIndex, textureManager, true, textureMeta );

if( !wholeGltf.lights.empty() )
{
Expand Down Expand Up @@ -592,7 +591,7 @@ void RTGL1::Scene::NewScene( VkCommandBuffer cmd,
{
debug::Verbose( "Starting new static scene..." );
const auto sceneFile =
staticScene.ParseFile( cmd, frameIndex, textureManager, textureMeta );
staticScene.ParseFile( cmd, frameIndex, textureManager, false, textureMeta );

for( const auto& [ name, m ] : sceneFile.models )
{
Expand Down
44 changes: 36 additions & 8 deletions Source/TextureManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "Generated/ShaderCommonC.h"

#include <numeric>
#include <ranges>

using namespace RTGL1;

Expand Down Expand Up @@ -541,7 +542,8 @@ bool TextureManager::TryCreateImportedMaterial( VkCommandBuffer
const std::string& materialName,
std::span< std::filesystem::path > fullPaths,
std::span< SamplerManager::Handle > samplers,
RgTextureSwizzling customPbrSwizzling )
RgTextureSwizzling customPbrSwizzling,
bool isReplacement )
{
assert( fullPaths.size() == TEXTURES_PER_MATERIAL_COUNT );
assert( samplers.size() == TEXTURES_PER_MATERIAL_COUNT );
Expand All @@ -552,9 +554,18 @@ bool TextureManager::TryCreateImportedMaterial( VkCommandBuffer
return false;
}

if( importedMaterials.contains( materialName ) )
// check if already uploaded
{
return true;
auto found = importedMaterials.find( materialName );
if( found != importedMaterials.end() )
{
if( isReplacement )
{
// promote to a stronger type
found->second = ImportedType::ForReplacement;
}
return true;
}
}

if( PreferExistingMaterials )
Expand Down Expand Up @@ -607,19 +618,36 @@ bool TextureManager::TryCreateImportedMaterial( VkCommandBuffer
static_assert( TEXTURE_OCCLUSION_ROUGHNESS_METALLIC_INDEX == 1 );

// to free later / to prevent export from ExportOriginalMaterialTextures
importedMaterials.insert( materialName );
auto [ iter, isNew ] = importedMaterials.emplace(
materialName, isReplacement ? ImportedType::ForReplacement : ImportedType::ForStatic );
assert( isNew );

MakeMaterial( cmd, frameIndex, materialName, ovrd, samplers, swizzlings );
return true;
}

void TextureManager::FreeAllImportedMaterials( uint32_t frameIndex )
void TextureManager::FreeAllImportedMaterials( uint32_t frameIndex, bool freeReplacements )
{
for( const auto& materialName : importedMaterials )
if( freeReplacements )
{
TryDestroyMaterial( frameIndex, materialName.c_str() );
for( const auto& materialName : importedMaterials | std::views::keys )
{
TryDestroyMaterial( frameIndex, materialName.c_str() );
}
importedMaterials.clear();
}
else
{
for( const auto& [ materialName, importType ] : importedMaterials )
{
if( importType != ImportedType::ForReplacement )
{
TryDestroyMaterial( frameIndex, materialName.c_str() );
}
}
erase_if( importedMaterials,
[]( const auto& kv ) { return kv.second != ImportedType::ForReplacement; } );
}
importedMaterials.clear();
}

uint32_t TextureManager::PrepareTexture( VkCommandBuffer cmd,
Expand Down
15 changes: 11 additions & 4 deletions Source/TextureManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ class TextureManager : public IFileDependency
const std::string& materialName,
std::span< std::filesystem::path > fullPaths,
std::span< SamplerManager::Handle > samplers,
RgTextureSwizzling customPbrSwizzling );
void FreeAllImportedMaterials( uint32_t frameIndex );
RgTextureSwizzling customPbrSwizzling,
bool isReplacement );
void FreeAllImportedMaterials( uint32_t frameIndex, bool freeReplacements );

bool TryDestroyMaterial( uint32_t frameIndex, const char* materialName );

Expand Down Expand Up @@ -216,8 +217,14 @@ class TextureManager : public IFileDependency
std::vector< Texture > texturesToDestroy[ MAX_FRAMES_IN_FLIGHT ];
std::vector< std::filesystem::path > texturesToReload;

rgl::string_map< Material > materials;
rgl::string_set importedMaterials;
enum class ImportedType
{
ForReplacement,
ForStatic,
};

rgl::string_map< Material > materials;
rgl::string_map< ImportedType > importedMaterials;

uint32_t waterNormalTextureIndex;
uint32_t dirtMaskTextureIndex;
Expand Down

0 comments on commit 7180682

Please sign in to comment.