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.