Skip to content

Commit 0fdd105

Browse files
committed
added global parameters to image loader that allow addition of file format specific paramters. Here:
normalmap paramter for ktx2 and uastc srgb parameter for forced uastc export
1 parent c140520 commit 0fdd105

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

DxImageLoader/interface.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ static uint32_t s_last_progress = -1;
2222

2323
// key = extension (e.g. png), value = DXGI formats
2424
static std::map<std::string, std::vector<uint32_t>> s_exportFormats;
25+
static std::map<std::string, int> s_globalParameteri;
2526

2627
bool hasEnding(std::string const& fullString, std::string const& ending) {
2728
if (fullString.length() >= ending.length()) {
@@ -302,6 +303,39 @@ const uint32_t* get_export_formats(const char* extension, int& numFormats)
302303
return it->second.data();
303304
}
304305

306+
bool get_global_parameter_i(const char* name, int& results)
307+
{
308+
if (s_globalParameteri.empty())
309+
{
310+
// insert default values
311+
set_global_parameter_i("normalmap", 0);
312+
set_global_parameter_i("uastc srgb", 0);
313+
}
314+
315+
auto it = s_globalParameteri.find(name);
316+
if (it == s_globalParameteri.end())
317+
{
318+
set_error("could not find global parameter: " + std::string(name));
319+
return false;
320+
}
321+
322+
results = it->second;
323+
return true;
324+
}
325+
326+
int get_global_parameter_i(const char* name)
327+
{
328+
int res = 0;
329+
if(get_global_parameter_i(name, res)) return res;
330+
331+
throw std::runtime_error(s_error);
332+
}
333+
334+
void set_global_parameter_i(const char* name, int value)
335+
{
336+
s_globalParameteri[name] = value;
337+
}
338+
305339
void set_progress_callback(ProgressCallback cb)
306340
{
307341
s_progress_callback = cb;

DxImageLoader/interface.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ EXPORT(bool) image_save(int id, const char* filename, const char* extension, uin
4747
/// \brief retrieves an array with all supported dxgi formats that are available for export with the extension
4848
EXPORT(const uint32_t*) get_export_formats(const char* extension, int& numFormats);
4949

50+
/// List of global parameters:
51+
/// "uastc srgb" - for .ktx2 export => use uastc for srgb compression (otherwise etc1 is used). Valid for srgb uastc compressable textures
52+
/// "normalmap" - for .ktx2 export => indicate that the exporter/compressor should optimize data for normal maps. Valid for linear (non-srgb) uastc compressable textures
53+
54+
/// \brief stores the value of the parameter in the results argument and returns true if found. Returns false otherwise
55+
EXPORT(bool) get_global_parameter_i(const char* name, int& results);
56+
57+
/// \brief returns the value of the parameter if found. Throws an exception otherwise
58+
int get_global_parameter_i(const char* name);
59+
60+
/// \brief sets the value of a global parameter or 0 if not found
61+
EXPORT(void) set_global_parameter_i(const char* name, int value);
62+
5063
typedef uint32_t(__stdcall* ProgressCallback)(float, const char*);
5164

5265
/// \brief sets the progress report callback

DxImageLoader/ktx_interface.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,17 @@ void ktx2_save_image(const char* filename, GliImage& image, gli::format format,
8585
params.threadCount = std::thread::hardware_concurrency();
8686
params.compressionLevel = KTX_ETC1S_DEFAULT_COMPRESSION_LEVEL;
8787
params.qualityLevel = std::max((quality * 254) / 99 + 1, 1); // scale quality [0, 99] between [1, 255]
88-
// params.normalMap // TODO add this later?
88+
params.normalMap = KTX_FALSE;
89+
if (!gli::is_srgb(format)) // only valid for linear textures
90+
params.normalMap = get_global_parameter_i("normalmap") ? KTX_TRUE : KTX_FALSE;
8991

9092
// select uastc for everything that is not color (here: for everyhing that is not SRGB)
91-
if(!gli::is_srgb(format))
93+
// unless the "uastc srgb" flag is set => then use usastc as well
94+
if(!gli::is_srgb(format) || get_global_parameter_i("uastc srgb"))
9295
{
9396
params.uastc = KTX_TRUE;
9497
params.uastcFlags = KTX_PACK_UASTC_MAX_LEVEL; // maximum supported quality
95-
params.uastcRDO = KTX_TRUE; // Enable Rate Distortion Optimization (RDO) post-processing.
98+
params.uastcRDO = params.normalMap ? KTX_FALSE : KTX_TRUE;
9699
}
97100

98101
// optional if compression

0 commit comments

Comments
 (0)