Skip to content

Commit

Permalink
Flag to force export as external texture
Browse files Browse the repository at this point in the history
  • Loading branch information
sultim-t committed Nov 10, 2023
1 parent 7524b8f commit fd6b97a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 19 deletions.
13 changes: 10 additions & 3 deletions Include/RTGL1/RTGL1.h
Original file line number Diff line number Diff line change
Expand Up @@ -655,12 +655,19 @@ typedef enum RgFormat
RG_FORMAT_B8G8R8A8_SRGB = 50,
} RgFormat;

typedef enum RgOriginalTextureInfoFlagBits
{
RG_ORIGINAL_TEXTURE_INFO_FORCE_EXPORT_AS_EXTERNAL = 1,
} RgOriginalTextureInfoFlagBits;
typedef uint32_t RgOriginalTextureInfoFlags;

// Can be linked after RgOriginalTextureInfo.
typedef struct RgOriginalTextureDetailsEXT
{
RgStructureType sType;
void* pNext;
RgFormat format;
RgStructureType sType;
void* pNext;
RgOriginalTextureInfoFlags flags;
RgFormat format;
} RgOriginalTextureDetailsEXT;

typedef struct RgOriginalTextureInfo
Expand Down
40 changes: 25 additions & 15 deletions Source/GltfExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ std::filesystem::path GetGltfFolder( const std::filesystem::path& gltfPath )

std::filesystem::path GetOriginalTexturesFolder( const std::filesystem::path& gltfPath )
{
return GetGltfFolder( gltfPath ) / RTGL1::TEXTURES_FOLDER_JUNCTION_PREFIX;
return GetGltfFolder( gltfPath ) / RTGL1::TEXTURES_FOLDER_JUNCTION;
}

std::filesystem::path GetGltfBinPath( std::filesystem::path gltfPath )
Expand Down Expand Up @@ -759,7 +759,7 @@ struct GltfStorage
struct GltfTextures
{
explicit GltfTextures( const std::set< std::string >& sceneMaterials,
const std::filesystem::path& texturesFolder,
const std::filesystem::path& gltfPath,
const RTGL1::TextureManager& textureManager,
const std::filesystem::path& texLookupFolder )
{
Expand Down Expand Up @@ -804,6 +804,13 @@ struct GltfTextures
};


const auto originalsFolder = std::string{ RTGL1::TEXTURES_FOLDER_JUNCTION_PREFIX };
const auto externalFolder = std::string{ "ext/" };

const auto originalsFolderExportPath = GetGltfFolder( gltfPath ) / originalsFolder;
const auto externalFolderExportPath = GetGltfFolder( gltfPath ) / externalFolder;


// resolve
for( const auto& materialName : sceneMaterials )
{
Expand All @@ -812,17 +819,22 @@ struct GltfTextures
continue;
}

const bool asExternal = textureManager.ShouldExportAsExternal( materialName.c_str() );

static_assert( RTGL1::TEXTURES_PER_MATERIAL_COUNT == 4 );
static_assert( RTGL1::TEXTURE_ALBEDO_ALPHA_INDEX == 0 );
static_assert( RTGL1::TEXTURE_OCCLUSION_ROUGHNESS_METALLIC_INDEX == 1 );
static_assert( RTGL1::TEXTURE_NORMAL_INDEX == 2 );
static_assert( RTGL1::TEXTURE_EMISSIVE_INDEX == 3 );
auto [ albedo, orm, normal, emissive ] = textureManager.ExportMaterialTextures(
materialName.c_str(), texturesFolder, false, &texLookupFolder );

auto tryMakeCgltfTexture = [ this, &findSampler, &materialName ](
RTGL1::TextureManager::ExportResult&& r,
bool nameMustBeMaterialName = false ) -> cgltf_texture* {
materialName.c_str(),
asExternal ? externalFolderExportPath : originalsFolderExportPath,
false,
&texLookupFolder );

auto tryMakeCgltfTexture = [ & ]( RTGL1::TextureManager::ExportResult&& r,
bool nameMustBeMaterialName =
false ) -> cgltf_texture* {
if( !r.relativePath.empty() )
{
std::string& str = strings.increment_and_get();
Expand All @@ -831,7 +843,7 @@ struct GltfTextures
cgltf_texture& txd = textures.increment_and_get();

// need to protect a string, to avoid dangling pointers
str = std::string( RTGL1::TEXTURES_FOLDER_JUNCTION_PREFIX ) + r.relativePath;
str = ( asExternal ? externalFolder : originalsFolder ) + r.relativePath;
std::ranges::replace( str, '\\', '/' );

if( nameMustBeMaterialName )
Expand Down Expand Up @@ -1648,13 +1660,11 @@ void RTGL1::GltfExporter::ExportToFiles( const std::filesystem::path& gltfPath,


// lock pointers
auto fbin = GltfBin{ gltfPath };
auto storage = GltfStorage{ scene, sceneLights.size() };
auto textureStorage = GltfTextures{ sceneMaterials,
GetOriginalTexturesFolder( gltfPath ),
textureManager,
ovrdFolder / TEXTURES_FOLDER_DEV };
auto lightStorage = GltfLights{ sceneLights, storage.lightNodes };
auto fbin = GltfBin{ gltfPath };
auto storage = GltfStorage{ scene, sceneLights.size() };
auto textureStorage =
GltfTextures{ sceneMaterials, gltfPath, textureManager, ovrdFolder / TEXTURES_FOLDER_DEV };
auto lightStorage = GltfLights{ sceneLights, storage.lightNodes };


auto materialcount = 0u;
Expand Down
23 changes: 22 additions & 1 deletion Source/TextureManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ bool TextureManager::TryCreateMaterial( VkCommandBuffer cmd,
debug::Verbose( R"(Material is promoted from 'Imported' to 'Original': {})",
info.pTextureName );
}


if( PreferExistingMaterials )
{
Expand Down Expand Up @@ -486,6 +486,16 @@ bool TextureManager::TryCreateMaterial( VkCommandBuffer cmd,
static_assert( TEXTURE_OCCLUSION_ROUGHNESS_METALLIC_INDEX == 1 );



if( details )
{
if( details->flags & RG_ORIGINAL_TEXTURE_INFO_FORCE_EXPORT_AS_EXTERNAL )
{
forceExportAsExternal.insert( info.pTextureName );
}
}


MakeMaterial( cmd, frameIndex, info.pTextureName, ovrd, samplers, swizzlings );
return true;
}
Expand Down Expand Up @@ -748,6 +758,8 @@ bool TextureManager::TryDestroyMaterial( uint32_t frameIndex, const char* materi
DestroyMaterialTextures( frameIndex, it->second );
materials.erase( it );

forceExportAsExternal.erase( materialName );

return true;
}

Expand Down Expand Up @@ -1028,3 +1040,12 @@ std::vector< TextureManager::Debug_MaterialInfo > TextureManager::Debug_GetMater
}
return r;
}

bool TextureManager::ShouldExportAsExternal( const char* materialName ) const
{
if( Utils::IsCstrEmpty( materialName ) )
{
return false;
}
return forceExportAsExternal.contains( materialName );
}
4 changes: 4 additions & 0 deletions Source/TextureManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ class TextureManager : public IFileDependency

void ExportOriginalMaterialTextures( const std::filesystem::path& folder ) const;

bool ShouldExportAsExternal( const char* materialName ) const;


void OnFileChanged( FileType type, const std::filesystem::path& filepath ) override;

Expand Down Expand Up @@ -226,6 +228,8 @@ class TextureManager : public IFileDependency

bool forceNormalMapFilterLinear;

rgl::string_set forceExportAsExternal;

public:
struct Debug_MaterialInfo
{
Expand Down

0 comments on commit fd6b97a

Please sign in to comment.