-
Notifications
You must be signed in to change notification settings - Fork 235
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
Improvements for the Java/JNI bindings #880
Comments
Thank you for starting this discussion and most especially for your willingness to improve the JNI. Re. Re There are functions in |
Trying to pull together a few "TODOs" from related issues and this one. (EDIT: A summary of the tasks is now in #886 ) In recent comments here and here, you mentioned
I'll start with In another recent comment, you mentioned
In that context, I noticed that the Somewhat related: The current documentation at https://github.khronos.org/KTX-Software/libktx/group__reader.html#ga90cc33928a2dae962fb94b3fa2f6575d says
That's it. It doesn't say what they are. Line 1731 in 5414bd0
In https://github.com/KhronosGroup/KTX-Software/pull/879/files/a512cc5373931777c9bf3f4d831148f770dd784a#r1537201514 you mentioned that a part of the documentation should be updated, even on the native side. This will probably affect the documentation of the JS/Python/Java bindings, so I'll have to review that carefully. In #879 (comment) , you mentioned that one of the new tests could also compare the image data. And I agree that the tests should be more thorough (and many of the existing tests could be improved in this regard). But it's sometimes hard to know what can reasonably be tested, and how exactly the required data can be obtained. The improvements here will therefore (have) to be iterative. In the same comment ( #879 (comment) ) you mentioned
I'll have to look at the details of the 'inflate...' functions. I already started implementing the The
Yeah. I'm not sure what to do with that. I'll consider it broadly as "one of these things that should be part of an overall improvement"....
Regardless of where this is offered in the native part: It would very likely make sense to offer that translation directly on the Java side. Having a Some tasks - preliminary: (EDIT: These have been moved to #886 to better keep track of them in the context of a PR) |
At the native level there is only one VulkanSDK which reduces the issues considerably compared to OpenGL. In #877 you mentioned "mix(ing) different JNI libraries that only communicated via some GLuint someOpenGLObject". I think that is the major issue. If that works then the upload functions should be usable in Java. For GL #708 will likely need to be fixed. The fix involves adding a function to set the address of the GetProcAddress function to use.
Need @ShukantPal's help re. this.
Any such file should be generated as we do with Thanks for the task list. |
I fully agree for the |
The tl;dr of this issue could be:
I'd try to allocate some time to clean up and refactor the JNI part in some ways.
Anybody opposed?
Details:
I recently wanted to use the Java bindings that are offered via https://github.com/KhronosGroup/KTX-Software/tree/main/interface/java_binding . I noticed some issues ( #624, #875 , #877 ...), and started fixing some of them ( #876 , #879 ...). But while diving into the implementation and the JNI part, I noticed that there are some more general questions that might warrant a dedicated discussion. Some of this might have to be broken down into dedicated issues, but I'll start with an overview here.
The current approach for the JNI bindings aims at offering the functionality of the KTX-Software - mainly, the
ktxTexture2
class - with a 1:1 mapping of the original API. This is a reasonable approach. Any attempt to introduce a "convenience layer", abstractions, or functionality that is more "Java-idiomatic" raises a bunch of questions, and bears the risk of introducing bugs in the translation layer or limitations compared to the original API. However, there are some questions that have to be answered even for the 1:1 translation (and I think that they should be solved differently than they are right now).Some questions are fine-grained and specific. For example: I think that the
createFromNamedFile
andwriteToNamedFile
methods should not be offered as part of theKtxTexture2
class in the current form. File systems and IO are too language- and platform-specific. (There may be convenience methods for stuff like that, but the underlying C/C++-functions should not be routed through via JNI). Some of these points may have to be discussed on a case-by-case basis, though.One broader, general point is that of ...
Error handling
To get this straight: Error handling in JNI is a pain in the back. It bloats the code by a factor of 5, and is "frustrating" insofar that the developer knows in 99% of the cases that the error will not happen 99% of the time.
But right now, there is zero error handling for the Java bindings.
A call like
KtxTexture2 t = KtxTexture2.create(null, KtxCreateStorage.ALLOC);
will crash the JVM.
A call like
t.setImageFromMemory(0, 0, 0, null);
will crash the JVM.
A call like
t.writeToNamedFile(null);
will crash the JVM.
Whatever we are doing: It should be impossible to provoke a JVM crash (unless the crash is directly caused by the native implementation). And all the above cases could trivially be avoided, and this wouldn't even have to happen in the JNI layer, but could just be an
Objecs.requireNonNull
call in the Java layer itself. (Whether or not the JNI layer should then still check fornull
is up for debate - because, ... "We know that this cannot happen", right? 😶 )Other error cases are more subtle.
For example,
t.writeToNamedFile("C:/Does/Not/Exist.ktx");
will do ... nothing. Sure, it will return
3
. And one can look this up, and see that this meansFILE_OPEN_FAILED
. But whether or not this method should exist in the first place, or how to handle this error, is up for debate.More generally: The underlying C functions usually return an error code. (The documentation talks about
@exception
, but given that this is supposed to be plain C (not C++), I assume that this refers to "non-KTX_SUCCESS
error codes", and not to any form ofthrow
...). This is handled somewhat inconsistently in the JNI layer. For example, in thecreate...
family of methods, these cases are handled by printing an error message tocout
, and returningNULL
(for example, increateFromNamedFile
). In other cases, the error code is returned. Maybe this could be made more consistent. And we could also (very carefully!) consider to actually throw a (Java) exception from the JNI layer in some cases.Convenience
There are some aspects that fell out of the 1:1 translation, and that are OK. For example, the concept of "flag bits" like
KtxTextureCreateFlagBits.LOAD_IMAGE_DATA_BIT
orenum
-emulations like theKtxPackAstcEncoderMode.LDR
. However, one should/could consider offering some convenience functionality here, to convert these into human-readable strings. This is particularly important for theKtxErrorCode
(but may find its limit atVkFormat
, which seems to be auto-generated during the build).Documentation
There is basically zero documentation in the Java part. Collecting the documentation from
ktx.h
(and the documentation that is smeared over the implementation files), and "translating" it into JavaDoc (with proper cross-linking likeThe create flag {@link KtxTextureCreateFlagBits#LOAD_IMAGE_DATA_BIT} ...
is a tedious, boring, and thankless task. But ... I'm a useful idiot, so I'd do some of that, at least...
The text was updated successfully, but these errors were encountered: