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

Java swizzle and constant fixes #972

Merged

Conversation

javagl
Copy link
Contributor

@javagl javagl commented Jan 1, 2025

Fixes #969 (see the issue for details).

The fix for the inputSwizzle (for both the KtxAstcParams and KtxBasisParams) is implemented by checking whether the input equals the default, which is a char[] { 0, 0, 0, 0 } array. If this is the case, then this value will be accepted, passed to the native layer, and handled there accordingly. Only when the input is not that default, the elements will be checked to be in rgba01.

*
* @param inputSwizzle The swizzle
*/
public void setInputSwizzle(char[] inputSwizzle) {
char defaultSwizzle[] = new char[4];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does Java initialize new arrays to all 0? Otherwise initialization is needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, all array components are initialized to be 0.

https://docs.oracle.com/javase/specs/jls/se23/html/jls-4.html#jls-4.12.3 :

Array components are unnamed variables that are created and initialized to default values
(link->) For type char, the default value is the null character, that is, '\u0000'.

throw new IllegalArgumentException("The inputSwizzle may only consist of 'rgba01', but contains " + c);
}
}
char defaultSwizzle[] = new char[4];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question as for the AstcParams case.

Copy link
Contributor Author

@javagl javagl Jan 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same answer as above.

With the addendum: I also didn't like the duplication, and considered to pull out this block into a function, so that the 'setter' could boil down to something like
this.inputSwizzle = Swizzling.validate(inputSwizzle);
This would also allow to avoid the duplication of the unit tests (which may be a stronger case to make).

I somehow hesitate(d) to create such a class Swizzling just for this purpose, but will have another look: There might be other duplicated blocks that could justify moving this into some class Utils that does some of the parameter checks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I think that, in this case, it makes sense to move this to a common function. So I just did that, and there now is a KtxUtilities.validateSwizzle function. This avoids the duplication in the BasisParams/AstcParams themself, and it avoids the duplication of the unit tests.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This avoids the duplication in the BasisParams/AstcParams themself, and it avoids the duplication of the unit tests.

Sounds good. I'll be away the next 3 days. I'll review again on my return.

@MarkCallow MarkCallow merged commit 3038f7c into KhronosGroup:main Jan 5, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Java bindings: Invalid check for ASTC input swizzle parameter
2 participants