Texture2D.LoadRawTextureData does not work properly with DXT1 and mipmaps

There seems to be a pretty serious bug in Unity’s LoadRawTextureData DXT1 implementation. I’ve already submitted a bug report for this, but figured I’d post here to see if I can get a faster response.

1. What happened

Your LoadRawTextureData method does not function consistently with GetRawTextureData if we’re dealing with DXT1 textures with mipmaps. I have no idea what LoadRawTextureData is doing behind the scenes, but it seems like it’s doing something pretty unreasonable with DXT1 textures.

Important things to note:
These textures have mipmaps. If you call the method with the copied texture set to not use mipmaps, things seem to work, but you won’t have any of the mipmaps.

2. How we can reproduce it using the example you attached
Reproducing this bug is easy - simply download or create some DDS texture with DXT1 and mipmaps. Then create a script as follows:

Texture2D cmpTex = Resources.Load<Texture2D>("test");
Texture2D texCopy = new Texture2D(cmpTex.width, cmpTex.height, cmpTex.format, cmpTex.mipmapCount > 1);
texCopy.LoadRawTextureData(cmpTex.GetRawTextureData());

After executing the script, you should get a UnityException: LoadRawTextureData: not enough data provided (will result in overread).

This is very frustrating, as I desperately need this feature and it’s annoying that the APIs for LoadRawTextureData() and GetRawTextureData() don’t seem to work properly with DXT1. If you use DXT1, but disable mipmaps on texCopy like so, then the LoadRawTextureData() function succeeds:

Texture2D cmpTex = Resources.Load<Texture2D>("test");
Texture2D texCopy = new Texture2D(cmpTex.width, cmpTex.height, cmpTex.format, false);
texCopy.LoadRawTextureData(cmpTex.GetRawTextureData());

Of course, this is a serious disadvantage because you won’t have mipmaps in the copied texture. It’s really frustrating that Unity doesn’t seem to support this properly.

I’m experiencing this as well with user-generated content. Since that content is persistent across multiple app launches I wanted to compress their jpg/png files to DXT1/5 and load them as such to avoid wasting time decompressing all of their images from jpg/png and back to DXT1/5 each time they launch the app.

All images are 256x256 DXT1/5 with mipmaps and I am experiencing the exact same issue described here.

Any chance you can submit a bug-report as described in this document?

It’s important that you report these issues together with a reproduction project if you want them to get fixed. If you don’t do it, it might be a long time until someone else reports them or until Unity Technologies find them.

After you submitted the bug-report, you receive a confirmation email with a bug-report Case number. Please post the Case number (number only, not the link) in this forum thread for Unity staff to pick up.