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

Expose supercompression functions via JNI #879

Merged
merged 15 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
12 changes: 12 additions & 0 deletions interface/java_binding/src/main/cpp/KtxTexture2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,16 @@ extern "C" JNIEXPORT jobject JNICALL Java_org_khronos_ktx_KtxTexture2_createFrom
return make_ktx2_wrapper(env, instance);
}

extern "C" JNIEXPORT jint JNICALL Java_org_khronos_ktx_KtxTexture2_deflateZstd(JNIEnv *env,
jobject thiz,
jint level)
{
return ktxTexture2_DeflateZstd(get_ktx2_texture(env, thiz), static_cast<ktx_uint32_t>(level));
}

extern "C" JNIEXPORT jint JNICALL Java_org_khronos_ktx_KtxTexture2_deflateZLIB(JNIEnv *env,
jobject thiz,
jint level)
{
return ktxTexture2_DeflateZLIB(get_ktx2_texture(env, thiz), static_cast<ktx_uint32_t>(level));
}
30 changes: 18 additions & 12 deletions interface/java_binding/src/main/cpp/libktx-jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@ void copy_ktx_astc_params(JNIEnv *env, jobject params, ktxAstcParams &out)
out.normalMap = env->GetBooleanField(params, normalMap);
out.perceptual = env->GetBooleanField(params, perceptual);

env->GetByteArrayRegion(
static_cast<jbyteArray>(env->GetObjectField(params, inputSwizzle)),
0,
4,
reinterpret_cast<jbyte*>(&out.inputSwizzle)
);
jobject inputSwizzleObject = env->GetObjectField(params, inputSwizzle);
MarkCallow marked this conversation as resolved.
Show resolved Hide resolved
jcharArray inputSwizzleArray = static_cast<jcharArray>(inputSwizzleObject);
jchar *inputSwizzleValues = env->GetCharArrayElements( inputSwizzleArray, NULL);
for (int i=0; i<4; i++)
{
out.inputSwizzle[i] = static_cast<char>(inputSwizzleValues[i]);
}
env->ReleaseCharArrayElements(inputSwizzleArray, inputSwizzleValues, JNI_ABORT);
}

void copy_ktx_basis_params(JNIEnv *env, jobject params, ktxBasisParams &out)
Expand Down Expand Up @@ -145,12 +147,16 @@ void copy_ktx_basis_params(JNIEnv *env, jobject params, ktxBasisParams &out)
out.endpointRDOThreshold = env->GetFloatField(params, endpointRDOThreshold);
out.maxSelectors = env->GetIntField(params, maxSelectors);
out.selectorRDOThreshold = env->GetFloatField(params, selectorRDOThreshold);
env->GetByteArrayRegion(
static_cast<jbyteArray>(env->GetObjectField(params, inputSwizzle)),
0,
4,
reinterpret_cast<jbyte*>(&out.inputSwizzle)
);

jobject inputSwizzleObject = env->GetObjectField(params, inputSwizzle);
jcharArray inputSwizzleArray = static_cast<jcharArray>(inputSwizzleObject);
jchar *inputSwizzleValues = env->GetCharArrayElements( inputSwizzleArray, NULL);
for (int i=0; i<4; i++)
{
out.inputSwizzle[i] = static_cast<char>(inputSwizzleValues[i]);
}
env->ReleaseCharArrayElements(inputSwizzleArray, inputSwizzleValues, JNI_ABORT);

out.normalMap = env->GetBooleanField(params, normalMap);
out.preSwizzle = env->GetBooleanField(params, preSwizzle);
out.noEndpointRDO = env->GetBooleanField(params, noEndpointRDO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,30 @@ public static native KtxTexture2 createFromNamedFile(String filename,
public static KtxTexture2 createFromNamedFile(String filename) {
return createFromNamedFile(filename, KtxTextureCreateFlagBits.LOAD_IMAGE_DATA_BIT);
}

/**
* Deflate the data in a {@link KtxTexture2} object using Zstandard.
*
* The texture's levelIndex, dataSize, DFD and supercompressionScheme will
MarkCallow marked this conversation as resolved.
Show resolved Hide resolved
* all be updated after successful deflation to reflect the deflated data.
*
* @param level Set speed vs compression ratio trade-off. Values
* between 1 and 22 are accepted. The lower the level the faster. Values
* above 20 should be used with caution as they require more memory.
* @return A {@link KtxErrorCode} value
*/
public native int deflateZstd(int level);

/**
* Deflate the data in a {@link KtxTexture2} object using miniz (ZLIB)
*
* The texture's levelIndex, dataSize, DFD and supercompressionScheme will
* all be updated after successful deflation to reflect the deflated data.
*
* @param level Set speed vs compression ratio trade-off. Values
* between 1 and 9 are accepted. The lower the level the faster.
* @return A {@link KtxErrorCode} value
*/
public native int deflateZLIB(int level);

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;

import static org.junit.jupiter.api.extension.ExtensionContext.Namespace.GLOBAL;

public class KtxTestLibraryLoader
implements BeforeAllCallback, ExtensionContext.Store.CloseableResource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,126 @@ public void testCreate() {

texture.destroy();
}

@Test
public void testInputSwizzleBasisEx() throws IOException {

// Create RGBA pixels for an image with 32x32 pixels,
// filled with
// 8 rows of red pixels
// 8 rows of green pixels
// 8 rows of blue pixels
// 8 rows of white pixels
int sizeX = 32;
int sizeY = 32;
byte[] rgba = new byte[sizeX * sizeY * 4];
fillRows(rgba, sizeX, sizeY, 0, 8, 255, 0, 0, 255); // Red
fillRows(rgba, sizeX, sizeY, 8, 16, 0, 255, 0, 255); // Green
fillRows(rgba, sizeX, sizeY, 16, 24, 0, 0, 255, 255); // Blue
fillRows(rgba, sizeX, sizeY, 24, 32, 255, 255, 255, 255); // White

// Create a texture and fill it with the RGBA pixel data
KtxTextureCreateInfo info = new KtxTextureCreateInfo();
info.setBaseWidth(sizeX);
info.setBaseHeight(sizeY);
info.setVkFormat(VkFormat.VK_FORMAT_R8G8B8A8_SRGB);
KtxTexture2 t = KtxTexture2.create(info, KtxCreateStorage.ALLOC);
t.setImageFromMemory(0, 0, 0, rgba);

// Apply basis compression with an input swizzle, BRGA, so that
// the former B channel becomes the R channel
// the former R channel becomes the G channel
// the former G channel becomes the B channel
// the former A channel remains the A channel
KtxBasisParams p = new KtxBasisParams();
p.setUastc(false);
p.setInputSwizzle(new char[] { 'b', 'r', 'g', 'a' });
t.compressBasisEx(p);

t.destroy();
}

@Test
public void testSupercompressionZstd() throws IOException {

int sizeX = 32;
int sizeY = 32;

// Create a dummy texture
KtxTextureCreateInfo info = new KtxTextureCreateInfo();
info.setBaseWidth(sizeX);
info.setBaseHeight(sizeY);
info.setVkFormat(VkFormat.VK_FORMAT_R8G8B8A8_SRGB);
KtxTexture2 t = KtxTexture2.create(info, KtxCreateStorage.ALLOC);
byte[] rgba = new byte[sizeX * sizeY * 4];
t.setImageFromMemory(0, 0, 0, rgba);

// Apply default UASTC compression
KtxBasisParams p = new KtxBasisParams();
p.setUastc(true);
t.compressBasisEx(p);

// The supercompression scheme should be NONE here
int scBefore = t.getSupercompressionScheme();
assertEquals(KtxSupercmpScheme.NONE, scBefore);

// Apply Zstd compression
t.deflateZstd(10);

// The supercompression scheme should now be ZSTD
int scAfter = t.getSupercompressionScheme();
assertEquals(KtxSupercmpScheme.ZSTD, scAfter);

MarkCallow marked this conversation as resolved.
Show resolved Hide resolved
t.destroy();
}

@Test
public void testSupercompressionZLIB() throws IOException {

int sizeX = 32;
int sizeY = 32;

// Create a dummy texture
KtxTextureCreateInfo info = new KtxTextureCreateInfo();
info.setBaseWidth(sizeX);
info.setBaseHeight(sizeY);
info.setVkFormat(VkFormat.VK_FORMAT_R8G8B8A8_SRGB);
KtxTexture2 t = KtxTexture2.create(info, KtxCreateStorage.ALLOC);
byte[] rgba = new byte[sizeX * sizeY * 4];
t.setImageFromMemory(0, 0, 0, rgba);

// Apply default UASTC compression
KtxBasisParams p = new KtxBasisParams();
p.setUastc(true);
t.compressBasisEx(p);

// The supercompression scheme should be NONE here
int scBefore = t.getSupercompressionScheme();
assertEquals(KtxSupercmpScheme.NONE, scBefore);

// Apply ZLIB compression
t.deflateZLIB(10);

// The supercompression scheme should now be ZLIB
int scAfter = t.getSupercompressionScheme();
assertEquals(KtxSupercmpScheme.ZLIB, scAfter);

t.destroy();
}

// Fill the specified range of rows of the given RGBA pixels
// array with the given RGBA components
private static void fillRows(byte rgba[], int sizeX, int sizeY,
int minRow, int maxRow,
int r, int g, int b, int a) {
for (int y = minRow; y < maxRow; y++) {
for (int x = 0; x < sizeX; x++) {
int index = (y * sizeX) + x;
rgba[index * 4 + 0] = (byte) r;
rgba[index * 4 + 1] = (byte) g;
rgba[index * 4 + 2] = (byte) b;
rgba[index * 4 + 3] = (byte) a;
}
}
}
}
Loading