Skip to content

Commit bbbc4d6

Browse files
committed
Change: Cleanup texture pipeline
1 parent efa9597 commit bbbc4d6

4 files changed

Lines changed: 26 additions & 97 deletions

File tree

examples/Common/ModelLoader.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,14 @@ inline EOS::Holder<EOS::TextureHandle> LoadGltfTexture(const fastgltf::Asset& as
123123
EOS::IContext* context)
124124
{
125125
const auto texturePath = ResolveTexturePath(asset, modelDirectory, textureIndex);
126-
if (!texturePath.has_value())
127-
{
128-
return {};
129-
}
126+
if (!texturePath.has_value()) return {};
127+
130128

131129
return EOS::TexturePipeline::LoadTexture({
132-
.filePath = texturePath.value(),
133-
.compression = compression,
134-
.context = context,
130+
.InputFilePath = texturePath.value(),
131+
.OutputFilePath = std::filesystem::current_path() / ".cache" / "compressed_textures",
132+
.TextureCompression = compression,
133+
.Context = context,
135134
});
136135
}
137136

src/EOS.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,9 +510,12 @@ namespace EOS
510510
*/
511511
struct TextureLoadingDescription final
512512
{
513-
std::filesystem::path filePath;
514-
Compression compression;
515-
EOS::IContext* context;
513+
std::filesystem::path InputFilePath;
514+
std::filesystem::path OutputFilePath;
515+
Compression TextureCompression;
516+
uint8_t Usage = Sampled;
517+
ImageType Type = ImageType::Image_2D;
518+
IContext* Context;
516519
};
517520

518521
/**

src/texturePipeline.cpp

Lines changed: 13 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -13,69 +13,6 @@
1313

1414
namespace 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+
}

src/texturePipeline.h

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
11
#pragma once
22

3-
#include <filesystem>
43
#include "enums.h"
54
#include "EOS.h"
65

76
namespace EOS::TexturePipeline
87
{
9-
// Return the suffix for a given compression mode, used in the output filename.
10-
[[nodiscard]] const char* GetCompressionSuffix(const Compression compression);
11-
12-
13-
[[nodiscard]] std::filesystem::path BuildCompressedPathFromRoots(
14-
const std::filesystem::path& inputRoot,
15-
const std::filesystem::path& outputRoot,
16-
const std::filesystem::path& sourceFile,
17-
const Compression compression);
18-
19-
[[nodiscard]] std::filesystem::path BuildCompressedPathFromSource(const std::filesystem::path& sourceFile, const Compression compression);
20-
218
[[nodiscard]] static EOS::Format KtxVkFormatToEOSFormat(const uint32_t vkFormat);
229

2310
// Load a texture from source or cache, optionally compressing on demand if tools are built
2411
[[nodiscard]] Holder<EOS::TextureHandle> LoadTexture(const EOS::TextureLoadingDescription& textureLoadingDescription);
25-
}
12+
}

0 commit comments

Comments
 (0)