Loading BC7 DDS Textures with LoadRawTextureData

When I execute the following code:

                         var ddsBytes = new byte[stream.Length];
                        stream.Read(ddsBytes, 0, ddsBytes.Length);

                        byte ddsSizeCheck = ddsBytes[4];
                        if (ddsSizeCheck != 124)
                            throw new Exception("Invalid DDS DXTn texture. Unable to read");  //this header byte should be 124 for DDS image files

                        int height = ddsBytes[13] * 256 + ddsBytes[12];
                        int width = ddsBytes[17] * 256 + ddsBytes[16];
                        texture = new Texture2D(width, height, TextureFormat.BC7, false);

                        int DDS_HEADER_SIZE = 128;
                        buffer = new byte[ddsBytes.Length - DDS_HEADER_SIZE];
                        System.Buffer.BlockCopy(ddsBytes, DDS_HEADER_SIZE, buffer, 0, ddsBytes.Length - DDS_HEADER_SIZE);

                         texture.LoadRawTextureData(buffer);

The result I get is a texture that looks like this:

When I change the offset to other various values, the texture appears somewhat more correctly, but the entire texture is not loaded, and it ends up wrapping. Is there an issue with this function or am I loading the texture wrong? I have confirmed the texture loads up in other dds texture loaders.

3281605--253866--odd_texture.PNG

I believe I have figured it out. BC7 has two headers. The DX one and the DX10 one. So the actual header size is 148 not 124, which fixes the issues.

5 Likes