Skip to content

Commit 64a6900

Browse files
authored
Export ktxTexture2_DecodeAstc in JS bindings (#1034)
Add ktxTexture2_DecodeAstc to `libktx_read`, a previous omission. Needed too so the function can be included in the read-only JS bindings. Add tests of ASTC encode and decode functionality to `texturetest`. The change includes needing to pass the path to `ktxdiff` to `texturetests` as the new tests use it to compare the decoded result with the original image. Change `texturetest` to use std::filesystem::path and c++17. Fix issue in `astc_codec.cpp` where conversion of `params.inputSwizzle' could fail, asserting in a debug build.
1 parent f753fca commit 64a6900

12 files changed

Lines changed: 1335 additions & 973 deletions

File tree

.github/workflows/mingw.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ jobs:
7676
run: cmake --build build --config Release
7777
- name: Test Mingw build
7878
run: ctest --output-on-failure --test-dir build -C Release
79-
- name: Upload test log
80-
shell: pwsh
81-
if: ${{ failure() }}
82-
run: scripts/on_failure.ps1
79+
# - name: Upload test log
80+
# shell: pwsh
81+
# if: ${{ failure() }}
82+
# run: scripts/on_failure.ps1

CMakeLists.txt

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ set(KTX_BUILD_DIR "${CMAKE_BINARY_DIR}")
372372
set(KTX_MAIN_SRC
373373
include/KHR/khr_df.h
374374
include/ktx.h
375+
lib/astc_codec.cpp
375376
lib/basis_sgd.h
376377
lib/basis_transcode.cpp
377378
lib/miniz_wrapper.cpp
@@ -763,7 +764,6 @@ macro(common_libktx_settings target enable_write library_type)
763764
${target}
764765
PRIVATE
765766
lib/basis_encode.cpp
766-
lib/astc_codec.cpp
767767
${BASISU_ENCODER_CXX_SRC}
768768
lib/writer1.c
769769
lib/writer2.c
@@ -1267,21 +1267,25 @@ PRIVATE
12671267
$<$<AND:${is_clangcl},$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,17.0.3>>:_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR>
12681268
)
12691269

1270-
if(NOT BUILD_SHARED_LIBS AND APPLE)
1271-
# Make a single static library to simplify linking.
1272-
add_dependencies(ktx ${ASTCENC_LIB_TARGET})
1273-
add_custom_command( TARGET ktx
1274-
POST_BUILD
1275-
COMMAND libtool -static -o $<TARGET_FILE:ktx> $<TARGET_FILE:ktx> $<TARGET_FILE:${ASTCENC_LIB_TARGET}>
1276-
)
1270+
macro(set_astc_dependencies target)
1271+
if(NOT BUILD_SHARED_LIBS AND APPLE)
1272+
# Make a single static library to simplify linking.
1273+
add_dependencies(${target} ${ASTCENC_LIB_TARGET})
1274+
add_custom_command( TARGET ${target}
1275+
POST_BUILD
1276+
COMMAND libtool -static -o $<TARGET_FILE:${target}> $<TARGET_FILE:ktx> $<TARGET_FILE:${ASTCENC_LIB_TARGET}>
1277+
)
12771278

1278-
# Don't know libtool equivalent on Windows or Emscripten. Applications
1279-
# will have to link with both ktx and ${ASTCENC_LIB_TARGET}. Static libs
1280-
# are unlikely to be used on Windows so not a problem there. For Emscripten
1281-
# everything is built into the JS module so not an issue there either.
1282-
else()
1283-
target_link_libraries(ktx PRIVATE ${ASTCENC_LIB_TARGET})
1284-
endif()
1279+
# Don't know libtool equivalent on Windows or Emscripten. Applications
1280+
# will have to link with both ktx and ${ASTCENC_LIB_TARGET}. Static libs
1281+
# are unlikely to be used on Windows so not a problem there. For Emscripten
1282+
# everything is built into the JS module so not an issue there either.
1283+
else()
1284+
target_link_libraries(${target} PRIVATE ${ASTCENC_LIB_TARGET})
1285+
endif()
1286+
endmacro(set_astc_dependencies)
1287+
set_astc_dependencies(ktx)
1288+
set_astc_dependencies(ktx_read)
12851289

12861290
# Other external projects
12871291
# `NOT TARGET`s here are a precaution. They would only be exercised if

interface/js_binding/ktx_wrapper.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ namespace ktx
101101
}
102102

103103
// This is needed because embind cannot do constructor overloads
104-
// where the only the parameter type(s) differ. See
104+
// where only the parameter type(s) differ. See
105105
// class_<ktx::texture> in EMSCRIPTEN_BINDINGS below for more
106106
// details.
107107
texture createCopy()
@@ -256,6 +256,16 @@ namespace ktx
256256
return result;
257257
}
258258

259+
ktx_error_code_e decodeAstc()
260+
{
261+
ktx_error_code_e result;
262+
result = ktxTexture2_DecodeAstc(*this);
263+
if (result != KTX_SUCCESS) {
264+
std::cout << "ERROR: Failed to decodeAstc: " << ktxErrorString(result) << std::endl;
265+
}
266+
return result;
267+
}
268+
259269
// NOTE: WebGLTexture objects are completely opaque so the option of passing in the texture
260270
// to use is not viable.
261271
val glUpload()
@@ -363,7 +373,7 @@ namespace ktx
363373

364374
result = ktxTexture2_CompressAstcEx(*this, &params);
365375
if (result != KTX_SUCCESS) {
366-
std::cout << "ERROR: failed to compressAstc: " << ktxErrorString(result) << std::endl;
376+
std::cout << "ERROR: Failed to compressAstc: " << ktxErrorString(result) << std::endl;
367377
}
368378
return result;
369379
}
@@ -426,7 +436,7 @@ namespace ktx
426436

427437
result = ktxTexture2_DeflateZstd(*this, compression_level);
428438
if (result != KTX_SUCCESS) {
429-
std::cout << "ERROR: failed to deflateZstd: " << ktxErrorString(result) << std::endl;
439+
std::cout << "ERROR: Failed to deflateZstd: " << ktxErrorString(result) << std::endl;
430440
}
431441
return result;
432442
}
@@ -443,7 +453,7 @@ namespace ktx
443453

444454
result = ktxTexture2_DeflateZLIB(*this, compression_level);
445455
if (result != KTX_SUCCESS) {
446-
std::cout << "ERROR: failed to deflateZLIB: " << ktxErrorString(result) << std::endl;
456+
std::cout << "ERROR: Failed to deflateZLIB: " << ktxErrorString(result) << std::endl;
447457
}
448458
return result;
449459
}
@@ -457,7 +467,7 @@ namespace ktx
457467
value.c_str());
458468

459469
if (result != KTX_SUCCESS) {
460-
std::cout << "ERROR: failed to addKVPair (string): " << ktxErrorString(result) << std::endl;
470+
std::cout << "ERROR: Failed to addKVPair (string): " << ktxErrorString(result) << std::endl;
461471
}
462472
return result;
463473
}
@@ -479,7 +489,7 @@ namespace ktx
479489
value.data());
480490

481491
if (result != KTX_SUCCESS) {
482-
std::cout << "ERROR: failed to addKVPair (vector): " << ktxErrorString(result) << std::endl;
492+
std::cout << "ERROR: Failed to addKVPair (vector): " << ktxErrorString(result) << std::endl;
483493
}
484494
return result;
485495
}
@@ -491,7 +501,7 @@ namespace ktx
491501
result = ktxHashList_DeleteKVPair(ht, key.c_str());
492502

493503
if (result != KTX_SUCCESS) {
494-
std::cout << "ERROR: failed to deleteKVPair: " << ktxErrorString(result) << std::endl;
504+
std::cout << "ERROR: Failed to deleteKVPair: " << ktxErrorString(result) << std::endl;
495505
}
496506
return result;
497507
}
@@ -526,11 +536,11 @@ namespace ktx
526536
if (result == KTX_SUCCESS) {
527537
return val(emscripten::typed_memory_view(ktx_data_size, ktx_data));
528538
} else {
529-
std::cout << "ERROR: failed to writeToMemory: " << ktxErrorString(result) << std::endl;
539+
std::cout << "ERROR: Failed to writeToMemory: " << ktxErrorString(result) << std::endl;
530540
return val::null();
531541
}
532542
}
533-
#endif
543+
#endif /* KTX_FEATURE_WRITE */
534544
};
535545
}
536546

@@ -633,6 +643,7 @@ interface texture {
633643
CreateStorageEnum? storage);
634644
635645
error_code compressAstc(ktxAstcParams params); // **
646+
error_code decodeAstc(ktxAstcParams params);
636647
error_code compressBasis(ktxBasisParams params); // **
637648
texture createCopy(); // **
638649
error_code defateZLIB(); // **
@@ -1286,6 +1297,7 @@ EMSCRIPTEN_BINDINGS(ktx)
12861297
.function("findKeyValue", &ktx::texture::findKeyValue)
12871298
.function("getImage", &ktx::texture::getImage)
12881299
.function("glUpload", &ktx::texture::glUpload)
1300+
.function("decodeAstc", &ktx::texture::decodeAstc)
12891301
.function("transcodeBasis", &ktx::texture::transcodeBasis)
12901302
#if KTX_FEATURE_WRITE
12911303
.constructor<const ktxTextureCreateInfo&, ktxTextureCreateStorageEnum>()

0 commit comments

Comments
 (0)