Skip to content

Device::create_texture_with_data is not possible to use correctly with mipmapped non-block aligned textures. #7677

Open
@atlv24

Description

@atlv24

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions