Make invertY flag more consistent across different texture loading scenarios

Babylon.js supports loading uncompressed images (e.g., from a PNG or JPG) and compressed images (e.g., from a DDS, Basis, KTX1, KTX2).

For uncompressed images, WebGL can store the pixels inverted on the GPU using this code.

this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, value ? 1 : 0);

This pixelStorei function only affects texImage2D and thus has no effect on compressed images.

When creating a Texture object, there is a flag invertY that determines whether the code above is executed or not.

Currently, some of the texture loaders (e.g., Basis and KTX1) will try to respect this invertY by setting an _invertVScale property which will eventually update the texture.vScale and texture.vOffset properties.

if (this._texture._invertVScale) { this.vScale *= -1; this.vOffset += 1; }

This is a bad for a couple of reasons:

  1. Using texture transform to flip the texture doesn’t work for postprocesses.
  2. We are mucking with properties that could have been set by the user. If the user of the vScale or vOffset property is going to compute something using them (e.g., animate them), they will be different once the texture is loaded.

Both DDS and KTX2 loaders do not respect this invertY flag and will load images with different orientation depending on if the resulting data is compressed or not compressed.

The proposal is to make this more consistent.

  1. Any image that is loaded, whether compressed or not, should load with the same orientation and should match the orientation of loading an uncompressed image (e.g., png or jpg).
  2. The invertY flag should be ignored when attempting to load a compressed texture. This includes loading a KTX1 texture using the texture selector or loading a compressed texture directly with DDS, Basis, KTX1, or KTX2. Doing this will ensure compressed textures work consistently across different features since there is no way to update the pixel storage for compressed textures.

Whoever does this work should make sure WebGPU works as well and add tests to ensure textures are consistent when loaded using various loaders with and without compression.

*Ported From GitHub