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

Add ability to compress textures with Basis Universal #16142

Open
wants to merge 16 commits into
base: master
Choose a base branch
from

Conversation

alexchuber
Copy link
Contributor

@alexchuber alexchuber commented Feb 4, 2025

This PR exposes the method EncodeTextureToBasisAsync which will encode a non-HDR, non-cube texture data to a KTX v2 image with Basis Universal supercompression.

@bjsplat
Copy link
Collaborator

bjsplat commented Feb 4, 2025

Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s).
To prevent this PR from going to the changelog marked it with the "skip changelog" label.

@bjsplat
Copy link
Collaborator

bjsplat commented Feb 4, 2025

@bjsplat
Copy link
Collaborator

bjsplat commented Feb 4, 2025

@bjsplat
Copy link
Collaborator

bjsplat commented Feb 4, 2025

@bjsplat
Copy link
Collaborator

bjsplat commented Feb 4, 2025

@bjsplat
Copy link
Collaborator

bjsplat commented Feb 4, 2025

@bjsplat
Copy link
Collaborator

bjsplat commented Feb 5, 2025

@bjsplat
Copy link
Collaborator

bjsplat commented Feb 5, 2025

@bjsplat
Copy link
Collaborator

bjsplat commented Feb 5, 2025

@bjsplat
Copy link
Collaborator

bjsplat commented Feb 5, 2025

@bjsplat
Copy link
Collaborator

bjsplat commented Feb 5, 2025

@bjsplat
Copy link
Collaborator

bjsplat commented Feb 5, 2025

@bjsplat
Copy link
Collaborator

bjsplat commented Feb 6, 2025

@bjsplat
Copy link
Collaborator

bjsplat commented Feb 6, 2025

@bjsplat
Copy link
Collaborator

bjsplat commented Feb 6, 2025

@bjsplat
Copy link
Collaborator

bjsplat commented Feb 6, 2025

* Initialize resources for the Basis Universal encoder.
* @returns a promise that resolves when the Basis Universal encoder resources are initialized
*/
export async function InitializeBasisEncoderAsync(): Promise<void> {
Copy link
Contributor Author

@alexchuber alexchuber Feb 7, 2025

Choose a reason for hiding this comment

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

Nitty, but any opinions about whether the encoder should be a class or not?

On the one hand,

  • It is stateful (workerpool management)
  • Module level functions clutter the BABYLON namespace and (I assume) require unique names

On the other,

  • Since this is a singleton, it makes sense to leave things at the module-level
  • Opportunity to move this all into basis.ts, our transcoder module that is also module-level

Logger.Warn("Texture data will be converted into unsigned bytes for Basis encoding. This may result in loss of precision.");
}

const pixels = await GetTextureDataAsync(babylonTexture, size.width, size.height);
Copy link
Contributor Author

@alexchuber alexchuber Feb 7, 2025

Choose a reason for hiding this comment

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

I can not figure out why GetTextureDataAsync() doesn't actually wait for the texture to be loaded, hence why I'm forced to call this function like:

texture.onLoadObservable.addOnce(() => EncodeTextureToBasis(...))

Does anyone have any ideas?

For a repro of the issue, see https://playground.babylonjs.com/#OHUP0G

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sebavan @Popov72 for advice :)

@alexchuber alexchuber marked this pull request as ready for review February 7, 2025 18:11
@alexchuber alexchuber requested review from bghgary and RaananW February 7, 2025 18:32
* @returns a promise that resolves when the worker is initialized
* @internal
*/
export function initializeWebWorker(worker: Worker, wasmBinary: ArrayBuffer, moduleUrl?: string) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

NOTE: I'd like to eventually move this to workerUtils and reuse, if possible, across other places... but perhaps sometime after this PR. I don't think this makes it to the public API right now, anyway

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.

2 participants