-
Notifications
You must be signed in to change notification settings - Fork 216
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
JNI improvements #886
base: main
Are you sure you want to change the base?
JNI improvements #886
Conversation
# Conflicts: # interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTexture2Test.java
I locally started a few updates, including some basic error handling, so that things like One thing that could/should be done next: The /**
* Destroy the KTX texture and free memory image resources
*
* NOTE: If you try to use a {@link KTXTexture} after it's destroyed, that will cause a segmentation
* fault (the texture pointer is set to NULL).
*/
public native void destroy(); Now, the goal is: No segmentation faults. And we'll probably not be able to completely avoid this I think that trying to use a |
There currently are two functions in the
(The latter has been added as part of this PR) Many libraries that are using JNI expect direct On the Java side, the byte array[] = ...;
t = KtxTexture2.createFromMemory(ByteBuffer.wrap(array), ...); But as a middle-ground, I'd suggest to...
An aside: The documentation of the
This statement is not true, and will be updated accordingly. Remotely related: There currently is a function |
What does "passed data" refer to here? Is it the data at the pointer location that was passed to setImageFromMemory? Not that it matters since you say it isn't true. Indeed the data is copied into the ktxTexture object. |
There are multiple "layers" here, and this revolves exactly about the question where data is copied. Imagine the following Java pseudo code, similar to what could appear in the KTX JNI bindings: byte input[] = new byte[10];
// Create a texture from the input
KtxTexture2 texture = KtxTexture2.createFrom(input);
// XXX Will this affect the result?
input[5] = 123;
// Encode the input data that was given earlier
texture.encode(); The comment suggested that this |
A detail @ShukantPal : Currently, the bindings are set up for Java 11. I don't see a strong reason for that. Unless there is a reason, I'd suggest lowering the version requirement to "the lowest version that works" (and I think that 8 should do it). |
Responding to my own comment from above: I decided to align the constant names with the names that they have in the native layer. The prefixes already are present, for example, in the (auto-generated) classes like I know, something like I also started adding the documentation from the native side to these constant definitions and some of the related methods (i.e. the ones for using and setting them). These might be useful on a certain level - before and after: |
Apparently, they have never been generated before.
The last passes contained a few steps of that dull, repetitive, boring, repetitive, tedius, and repetitive handling of JavaDoc, based on the documentation from the native side. And apparently, the JavaDocs have never been used or created before. Most of the (few) existing ones had been invalid. Now they are fixed. I have added the creation of the JavaDoc to the POM. The current state is attached here: libktx-4.0.0-SNAPSHOT-javadoc.zip There are a few degrees of freedom for reducing (or handling) the redundancy of JavaDoc comments for cases like the
The rationale here is that
Here's an example for the (Yeah, .. who's ever gonna change that anyhow? But I see complete JavaDoc as some sort of minimal baseline, to be at least on par with the native docs, even if the docs might have to be extended to be really useful for end-users...) |
About the current build failures: What's surprising is that I also don't see these errors locally, and they don't happen in the Windows build. Iff I understood that correctly, very roughly: Windows builds are done by GitHub actions. Linux/MacOs are done by Travis - and the error only appears in https://app.travis-ci.com/github/KhronosGroup/KTX-Software/jobs/620089455#L866 but not in https://github.com/KhronosGroup/KTX-Software/actions/runs/8543283946/job/23406762987#step:14:48 I'll probably have to add some quirky |
Don't obsess about Travis vs. Actions. Both macOS and Linux builds are failing. These builds are run on different machines, even though both under Travis. Look instead at the environments on the runners, particularly the Java/JVM versions in use. |
There are 2 circumstances in which
Not clear why either of these would be triggered on Linux and macOS but not Windows. |
# Conflicts: # interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTexture2Test.java
Thanks for the details @MarkCallow . I added some This is fixed now. Let's see whether the build passes. Two points remain open:
The latter is a more general question. Unsigned types are error-prone, particularly in interfaces - see e.g. https://www.aristeia.com/Papers/C++ReportColumns/sep95.pdf , which points out exactly what happened here. And when crossing the language barrier between Java and C++, one has to be particularly careful about that... |
# Conflicts: # interface/java_binding/src/main/cpp/libktx-jni.cpp # interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTestLibraryLoader.java # interface/java_binding/src/test/java/org/khronos/ktx/test/KtxTexture2Test.java
# Conflicts: # interface/java_binding/src/main/cpp/KtxTexture2.cpp # interface/java_binding/src/main/java/org/khronos/ktx/KtxTexture2.java
@MarkCallow We'll have to think about a sensible strategy for reviewing and merging this. Looking at the changes will hardly make sense. Nearly every file of the JNI bindings was modified. I tried to update the bullet point list in the first post with the progress. Beyond that, I can probably try to give a short summary here, with an overview of the most important changes/commits:
Some of this is just "baseline stuff" to follow the recommendations from https://developer.ibm.com/articles/j-jni/ . But some points still have subjective elements to them. I have actively been using the resulting library in a small dummy application where you can drag-and-drop a PNG file, see a preview of the PNG and its KTX version, drag around some sliders for the compression/quality (and see a live preview). So I'm confident that this "mostly works". But there are still a few things to wrap up.
But maybe this can already go through a first review pass, based on the current state. |
How do we make further progress on this? I find having the full KTX_ prefixes rather clunky. Can't we follow the WebGL model which removed all the GL_ prefixes? |
That's the question: What could be a sensible way to review this, given that it is ... nearly a "rewrite" of the JNI bindings? Is the bullet point list above sufficient? Should I invite further potential reviewers? I'm pretty flexible here. If you wish, we could also arrange a short call to go through the changes, what's left to do, and what could be the next steps.
From quickly skimming over some Python binding files, it looks like the Python bindings are also just omitting the |
Addresses #880
I know, the title is too generic. It would be better to clearly lay out a work plan and create a clean sequence of self-contained, specific PRs. But there are just too many (unrelated) things to do. "You can't cross a chasm in two small jumps". The first pass has to be tackled somewhat holistically. The result may not be as 'good' as I'd like it to be, but I hope that it will be 'better' along many dimensions. If this is not considered to be a viable approach, then that's OK. Then I'll just create my own repo with JNI bindings for KTX, with blackjack and error handling.
The preliminary task list that I tried to create in the linked issue is now replicated here, and I'll try to update it and extend it as we move on:
CreateFromMemory
stringFor
... methods to convert them to stringsAligned the names of all constants with the native version
Added documentation and string functions for all constants, except for...
KtxInternalformat
VkFormat
(See Synchronizing auto-generated files with bindings #887 )
NOTES:
ktxTexture2.supercompressionScheme
does not appear in the native documentationKtxPackAstcEncoderMode
are not documented on the native sideKtxTestLibraryLoader
to also work on Windowsenv->GetFieldID(ktx_texture_class, "instance", "J");
are called in each function. ThejfieldID
should be obtained once initially.null
to any method should throw aNullPointerException
.destroy()
was called should throw anIllegalStateException
.create...
methods) now generally throw aKtxException
when the implementation caused an error code that was notKTX_SUCCESS
createInfo.setBaseWidth(-123); // INVALID!
, but ... this is implicitly converted into aktx_uint32_t
. We may have to think about how to handle the absence ofunsigned
in Java in some cases...This PR currently builds on the state from #876 and #879 (so when these PRs are merged, then the first checkboxes above can be marked as 'done')
Not (yet) supposed to be addressed here:
Try to find a clean solution for JAR release to Maven Central #624
Update the documentation ofdeflateZstd
anddeflateZLIB
EDIT: This was now done as part of Expose supercompression functions via JNI #879, but only for the native side and for the Java bindings (other bindings are still missing the doc update or the functions)
deflateZLIB
function is still missing in the Python binding (and thedeflate*
functions are just about to be added to the Java binding), so ... this may have to be tracked and addressed separatelyImprove thoroughness of
deflate*
tests by comparing the deflated valuesdeflate*
functions should compare the result of inflating the data again to the original input. Theinflate*
methods are not yet exposed (anywhere), so this may have to be deferred for nowAdd test for input swizzling in ASTC
The ktxHash... functionalities are not yet mapped through from the native side.