Skip to content

Device::create_texture_with_data is not possible to use correctly with mipmapped nPOT textures. #7677

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

Open
atlv24 opened this issue May 8, 2025 · 0 comments · May be fixed by #7678
Open

Comments

@atlv24
Copy link
Collaborator

atlv24 commented May 8, 2025

create_texture_with_data is not possible to use correctly with mip-mapped non-power-of-two textures:

the mip sizes are calculated from the texture dimensions, assuming that the provided dimensions are logical dimensions (e.g. not necessarily a multiple of blocksize). this is correct. but the validation step requires that the texture dimensions be physical dimensions (e.g. a multiple of blocksize).

this creates a dilemma where if you have a nPOT texture with mips stored (for example, ktx2) and try to give it to wgpu, you either

  1. tell wgpu the true logical dimensions of the texture and give it the data, so that it can correctly calculate the mip sizes to read from the data block. except wgpu immediately sees that the dimensions are not a multiple of blocksize and rejects it
  2. lie to wgpu and round to physical dimensions, and give it the data. wgpu happily calculates the wrong mip sizes based off of the physical dimensions, which inevitably end up larger than they should be, and tries to index into data that doesnt exist, panicking.

In bevy, we currently do option 2 by just calling .physical_size(texture_format); before passing it in: https://github.com/bevyengine/bevy/blob/main/crates/bevy_image/src/ktx2.rs#L280

I believe the fix is to remove the validation and handle the conversion to physical dimensions somewhere internally, but i dont know all the places that would be affected by this.

This is causing bevy users crashes:
bevyengine/bevy#13289
bevyengine/bevy#19124

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Todo
Development

Successfully merging a pull request may close this issue.

1 participant