Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add normalise option to command create #977

Merged
merged 20 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c221bcd
First commit of re-adding --normalize option to create
wasimabbas-arm Jan 27, 2025
4b764cb
Moved normalize option from codec common to create only
wasimabbas-arm Jan 27, 2025
9d624d4
Update create docs but feels like this has gone out of sync with the …
wasimabbas-arm Jan 27, 2025
c9c5854
Fix review comments
wasimabbas-arm Jan 28, 2025
fcf0261
Use target format transfer to check for linear, report simultaneous u…
wasimabbas-arm Feb 3, 2025
ccec06b
Minor wording changes
wasimabbas-arm Feb 3, 2025
e31996b
Update cts submodule
wasimabbas-arm Feb 3, 2025
ebbc732
Use target.format().transfer() to check for Linearity for normalizing…
wasimabbas-arm Feb 10, 2025
f433ddb
Fix review comments
wasimabbas-arm Feb 21, 2025
314aec6
Fix build errors
wasimabbas-arm Feb 21, 2025
6c65df0
Add more comprehensive fatal error message for normalise option
wasimabbas-arm Feb 24, 2025
29507d9
Fix typo
wasimabbas-arm Feb 25, 2025
714fd86
Fix build error, renaming [assign|covert]OETF to [assign|covert]TF
wasimabbas-arm Feb 26, 2025
dc7b1e9
Update CTS module
wasimabbas-arm Feb 27, 2025
cb80198
Fix VS warnings
wasimabbas-arm Feb 27, 2025
54369bd
Remove use of reinterpret_cast in C files! woops
wasimabbas-arm Feb 27, 2025
6c36ea3
Disable no-cast-function-type-mismatch warning
wasimabbas-arm Feb 27, 2025
61a312d
Disable no-cast-function-type-mismatch warning
wasimabbas-arm Feb 27, 2025
dba26d8
Disable no-cast-function-type-mismatch warning for specific code lines
wasimabbas-arm Feb 28, 2025
6c04ba3
Fix typo and add the reinterpret_cast back
wasimabbas-arm Feb 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/astc_codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ static int
pthread_create(pthread_t* thread, const pthread_attr_t* attribs,
void* (*threadfunc)(void*), void* thread_arg) {
(void)attribs;
LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)threadfunc;
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wcast-function-type-mismatch"
#endif
LPTHREAD_START_ROUTINE func = reinterpret_cast<LPTHREAD_START_ROUTINE>(threadfunc);
#ifdef __clang__
#pragma clang diagnostic pop
#endif
*thread = CreateThread(nullptr, 0, func, thread_arg, 0, nullptr);
return 0;
}
Expand Down
11 changes: 10 additions & 1 deletion lib/gl_funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,20 @@ ktxFindOpenGL() {
"opengl32.dll",
&module
);

if (found) {
// Need wglGetProcAddr for non-OpenGL-2 functions.
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wcast-function-type-mismatch"
#endif
pfnWglGetProcAddress =
(PFNGLGETPROCADDRESS)GetProcAddress(module,
"wglGetProcAddress");
"wglGetProcAddress");
#ifdef __clang__
#pragma clang diagnostic pop
#endif

if (pfnWglGetProcAddress != NULL)
return module;
}
Expand Down
45 changes: 45 additions & 0 deletions tools/ktx/command_create.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ struct OptionsCreate {
inline static const char* kRuntimeMipmap = "runtime-mipmap";
inline static const char* kGenerateMipmap = "generate-mipmap";
inline static const char* kEncode = "encode";
inline static const char* kNormalize = "normalize";
inline static const char* kSwizzle = "swizzle";
inline static const char* kInputSwizzle = "input-swizzle";
inline static const char* kAssignOetf = "assign-oetf";
Expand Down Expand Up @@ -110,6 +111,7 @@ struct OptionsCreate {
bool noWarnOnColorConversions = false;
bool failOnOriginChanges = false;
bool warnOnOriginChanges = false;
bool normalize = false;

void init(cxxopts::Options& opts) {
opts.add_options()
Expand Down Expand Up @@ -145,6 +147,12 @@ struct OptionsCreate {
" This option is mutually exclusive with --runtime-mipmap and cannot be used with UINT or 3D textures.")
(kEncode, "Encode the created KTX file. Case insensitive."
"\nPossible options are: basis-lz | uastc", cxxopts::value<std::string>(), "<codec>")
(kNormalize, "Normalize input normals to have a unit length. Only valid for\n"
"linear normal textures with 2 or more components. For 2-component\n"
"inputs 2D unit normals are calculated. Do not use these 2D unit\n"
"normals to generate X+Y normals with --normal-mode. For 4-component\n"
"inputs a 3D unit normal is calculated. 1.0 is used for the value of\n"
"the 4th component. Cannot be used with --raw.")
(kSwizzle, "KTX swizzle metadata.", cxxopts::value<std::string>(), "[rgba01]{4}")
(kInputSwizzle, "Pre-swizzle input channels.", cxxopts::value<std::string>(), "[rgba01]{4}")
(kAssignTf, "Force the created texture to have the specified transfer function, ignoring"
Expand Down Expand Up @@ -458,6 +466,12 @@ struct OptionsCreate {
mipmapWrap = it->second;
}

if (args[kNormalize].count()) {
if (raw)
report.fatal_usage("Conflicting options: Option --normalize can't be used with --raw.");
normalize = true;
}

if (args[kSwizzle].count()) {
swizzle = to_lower_copy(args[kSwizzle].as<std::string>());
const auto errorFmt = "Invalid --swizzle value: \"{}\". The value must match the \"[rgba01]{{4}}\" regex.";
Expand Down Expand Up @@ -892,6 +906,13 @@ Create a KTX2 file from various input files.
Avoid mipmap generation if the Output TF (see @ref ktx\_create\_tf\_handling
below) is non-linear and is not sRGB.
</dd>
<dt>\--normalize</dt>
<dd>Normalize input normals to have a unit length. Only valid for
linear normal textures with 2 or more components. For 2-component
inputs 2D unit normals are calculated. Do not use these 2D unit
normals to generate X+Y normals with @b --normal-mode. For 4-component
inputs a 3D unit normal is calculated. 1.0 is used for the value of
the 4th component. Cannot be used with @b \--raw.</dd>
<dt>\--swizzle [rgba01]{4}</dt>
<dd>KTX swizzle metadata.</dd>
<dt>\--input-swizzle [rgba01]{4}</dt>
Expand Down Expand Up @@ -1591,6 +1612,27 @@ void CommandCreate::executeCreate() {
image->yflip();
}

if (options.normalize) {
if (target.format().transfer() != KHR_DF_TRANSFER_UNSPECIFIED && target.format().transfer() != KHR_DF_TRANSFER_LINEAR) {
const auto input_error_message = "Input file \"{}\" The transfer function to be applied to the created texture is neither linear nor none. Normalize is only available for these transfer functions.";
const auto assign_error_message = "Input file \"{}\" Use \"{}\" to assign the linear transfer function to the input image, if required.";
const auto convert_error_message = "Input file \"{}\" Modify \"{}\" settings to convert the input image to linear transfer function, if required.";
const auto inputTransfer = inputImageFile->spec().format().transfer();
bool is_file_error = (inputTransfer != KHR_DF_TRANSFER_UNSPECIFIED && inputTransfer != KHR_DF_TRANSFER_LINEAR);
bool is_assign_error = !options.assignTF.has_value();
bool is_convert_error = !options.convertTF.has_value();
if (is_assign_error)
fatal(rc::INVALID_FILE, assign_error_message, fmtInFile(inputFilepath), OptionsCreate::kAssignOetf);
else if (is_convert_error)
fatal(rc::INVALID_FILE, convert_error_message, fmtInFile(inputFilepath), OptionsCreate::kConvertOetf);
else {
assert(is_file_error && "In this branch it must be the input file that has the transfer function issue"); (void)is_file_error;
fatal(rc::INVALID_FILE, input_error_message, fmtInFile(inputFilepath));
}
}
image->normalize();
}

if (options.swizzleInput)
image->swizzle(*options.swizzleInput);

Expand Down Expand Up @@ -2314,6 +2356,9 @@ void CommandCreate::generateMipLevels(KTXTexture2& texture, std::unique_ptr<Imag
fatal(rc::RUNTIME_ERROR, "Mipmap generation failed: {}", e.what());
}

if (options.normalize)
image->normalize();

const auto imageData = convert(image, options.vkFormat, inputFile);

const auto ret = ktxTexture_SetImageFromMemory(
Expand Down