1313
1414namespace EOS ::TexturePipeline
1515{
16- const char * GetCompressionSuffix (const Compression compression)
17- {
18- switch (compression)
19- {
20- case Compression::NoCompression: return " raw" ;
21- case Compression::ETC2: return " etc2" ;
22- case Compression::BC5: return " bc5" ;
23- case Compression::BC7: return " bc7" ;
24- default : return " unknown" ;
25- }
26- }
27-
28- std::filesystem::path BuildCompressedPathFromRoots (const std::filesystem::path& inputRoot,
29- const std::filesystem::path& outputRoot, const std::filesystem::path& sourceFile, const Compression compression)
30- {
31- // Fall back to filename only if we cannot find a relative path.
32- std::filesystem::path relative = sourceFile.filename ();
33- std::error_code errorCode;
34- const std::filesystem::path weakInputRoot = std::filesystem::weakly_canonical (inputRoot, errorCode);
35- const std::filesystem::path weakSource = std::filesystem::weakly_canonical (sourceFile, errorCode);
36- if (!errorCode && !weakInputRoot.empty () && !weakSource.empty ())
37- {
38- std::filesystem::path candidate = std::filesystem::relative (weakSource, weakInputRoot, errorCode);
39- if (!errorCode)
40- {
41- relative = candidate;
42- }
43- }
44-
45- // Output path layout and appends a compression tag.
46- std::filesystem::path outputPath = outputRoot / relative;
47- outputPath.replace_extension (std::string (" ." ) + GetCompressionSuffix (compression) + " .ktx2" );
48- return outputPath;
49- }
50-
51- std::filesystem::path BuildCompressedPathFromSource (const std::filesystem::path& sourceFile,
52- const Compression compression)
53- {
54- std::filesystem::path dataRoot;
55- for (std::filesystem::path current = sourceFile.parent_path (); !current.empty (); current = current.parent_path ())
56- {
57- if (current.filename () == " data" )
58- {
59- dataRoot = current;
60- break ;
61- }
62-
63- if (current == current.root_path ())
64- {
65- break ;
66- }
67- }
68-
69- if (!dataRoot.empty ())
70- {
71- return BuildCompressedPathFromRoots (dataRoot, dataRoot / " .compressed" , sourceFile, compression);
72- }
73-
74- // Fallback for external assets: create a .compressed folder near the source.
75- const std::filesystem::path fallbackOutputRoot = sourceFile.parent_path () / " .compressed" ;
76- return BuildCompressedPathFromRoots (sourceFile.parent_path (), fallbackOutputRoot, sourceFile, compression);
77- }
78-
7916 EOS::Format KtxVkFormatToEOSFormat (const uint32_t vkFormat)
8017 {
8118 switch (vkFormat)
@@ -89,22 +26,25 @@ namespace EOS::TexturePipeline
8926 }
9027 }
9128
92-
9329 EOS::Holder<EOS::TextureHandle> LoadTexture (const EOS::TextureLoadingDescription& textureLoadingDescription)
9430 {
95- ktxTexture2* texture = nullptr ;
96- const std::filesystem::path compressedPath = BuildCompressedPathFromSource (textureLoadingDescription.filePath , textureLoadingDescription.compression );
31+ CHECK (!textureLoadingDescription.InputFilePath .empty () || std::filesystem::exists (textureLoadingDescription.InputFilePath ), " {} : Is not a valid Texture Path." , textureLoadingDescription.InputFilePath .string ());
32+ CHECK (std::filesystem::is_regular_file (textureLoadingDescription.InputFilePath ), " {} : Is not a valid Texture file." , textureLoadingDescription.InputFilePath .string ());
33+ CHECK (!textureLoadingDescription.OutputFilePath .empty (), " Please Specify a output path for the texture compression" );
34+ CHECK (std::filesystem::is_directory (textureLoadingDescription.OutputFilePath ), " {} : Is not a valid compression directory" , textureLoadingDescription.OutputFilePath .string ());
9735
36+ ktxTexture2* texture = nullptr ;
37+ const std::filesystem::path compressedPath = textureLoadingDescription.OutputFilePath / textureLoadingDescription.InputFilePath .filename ().replace_extension (" ktx2" );
9838 if (!std::filesystem::exists (compressedPath))
9939 {
10040#if defined(EOS_BUILD_TEXTURE_TOOLS)
10141 CHECK (EOS::TexturePacking::CompressTextureToCache (
102- textureLoadingDescription.filePath ,
42+ textureLoadingDescription.InputFilePath ,
10343 compressedPath,
104- textureLoadingDescription.compression ),
44+ textureLoadingDescription.TextureCompression ),
10545 " Failed to build compressed texture cache '{}' from source '{}'" ,
10646 compressedPath.string (),
107- textureLoadingDescription.filePath .string ());
47+ textureLoadingDescription.InputFilePath .string ());
10848#else
10949 CHECK (false ,
11050 " Missing compressed texture '{}'. Build with EOS_BUILD_TEXTURE_TOOLS=ON or precompress textures before shipping." ,
@@ -137,13 +77,13 @@ namespace EOS::TexturePipeline
13777 }
13878
13979 // Upload the texture to the GPU
140- EOS::Holder<EOS::TextureHandle> loadedTexture = textureLoadingDescription.context ->CreateTexture (
80+ TextureHolder loadedTexture = textureLoadingDescription.Context ->CreateTexture (
14181 {
142- .Type = EOS::ImageType::Image_2D ,
82+ .Type = textureLoadingDescription. Type ,
14383 .TextureFormat = textureFormat,
14484 .TextureDimensions = {texture->baseWidth , texture->baseHeight },
14585 .NumberOfMipLevels = texture->numLevels ,
146- .Usage = EOS::Sampled ,
86+ .Usage = textureLoadingDescription. Usage ,
14787 .Data = packedData.data (),
14888 .DataNumberOfMipLevels = texture->numLevels ,
14989 .DebugName = compressedPath.string ().c_str (),
@@ -153,4 +93,4 @@ namespace EOS::TexturePipeline
15393
15494 return std::move (loadedTexture);
15595 }
156- }
96+ }
0 commit comments