public static void LoadTextureData(ITexture texture, string fileName) { DirectXTex.ImageStruct img = DirectXTex.DDSIO.ReadDDS(fileName); switch ((DXGI_FORMAT)img.Format) { // compressed case DXGI_FORMAT.DXGI_FORMAT_BC1_UNORM: texture.Reset(img.Width, img.Height, img.MipMapLevels, img.GetRowPitch() / 4, TextureFormat.D3DFMT_DXT1); texture.SetTextureData(img.Data); break; case DXGI_FORMAT.DXGI_FORMAT_BC2_UNORM: texture.Reset(img.Width, img.Height, img.MipMapLevels, img.GetRowPitch() / 4, TextureFormat.D3DFMT_DXT3); texture.SetTextureData(img.Data); break; case DXGI_FORMAT.DXGI_FORMAT_BC3_UNORM: texture.Reset(img.Width, img.Height, img.MipMapLevels, img.GetRowPitch() / 4, TextureFormat.D3DFMT_DXT5); texture.SetTextureData(img.Data); break; case DXGI_FORMAT.DXGI_FORMAT_BC4_UNORM: texture.Reset(img.Width, img.Height, img.MipMapLevels, img.GetRowPitch() / 4, TextureFormat.D3DFMT_ATI1); texture.SetTextureData(img.Data); break; case DXGI_FORMAT.DXGI_FORMAT_BC5_UNORM: texture.Reset(img.Width, img.Height, img.MipMapLevels, img.GetRowPitch() / 4, TextureFormat.D3DFMT_ATI2); texture.SetTextureData(img.Data); break; case DXGI_FORMAT.DXGI_FORMAT_BC7_UNORM: texture.Reset(img.Width, img.Height, img.MipMapLevels, img.GetRowPitch() / 4, TextureFormat.D3DFMT_BC7); texture.SetTextureData(img.Data); break; // uncompressed case DXGI_FORMAT.DXGI_FORMAT_B5G5R5A1_UNORM: texture.Reset(img.Width, img.Height, img.MipMapLevels, img.GetRowPitch(), TextureFormat.D3DFMT_A1R5G5B5); texture.SetTextureData(img.Data); break; case DXGI_FORMAT.DXGI_FORMAT_A8_UNORM: texture.Reset(img.Width, img.Height, img.MipMapLevels, img.GetRowPitch(), TextureFormat.D3DFMT_A8); texture.SetTextureData(img.Data); break; case DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM: texture.Reset(img.Width, img.Height, img.MipMapLevels, img.GetRowPitch(), TextureFormat.D3DFMT_A8B8G8R8); texture.SetTextureData(img.Data); break; case DXGI_FORMAT.DXGI_FORMAT_R8_UNORM: texture.Reset(img.Width, img.Height, img.MipMapLevels, img.GetRowPitch(), TextureFormat.D3DFMT_L8); texture.SetTextureData(img.Data); break; case DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM: texture.Reset(img.Width, img.Height, img.MipMapLevels, img.GetRowPitch(), TextureFormat.D3DFMT_A8R8G8B8); texture.SetTextureData(img.Data); break; default: MessageBox.Show("format not supported!"); throw new Exception("unsupported format"); } }
public static void LoadTextureData(ITexture texture, Stream stream) { byte[] readbuf = new byte[16777216]; IntPtr p = IntPtr.Zero; int width = 0; int height = 0; int mipLevels = 0; int format = 0; int stride = 0; int readsize = 16777216; var strBuf = new byte[stream.Length]; stream.Position = 0; stream.Read(strBuf, 0, strBuf.Length); DXTexdds.LoadDDSImageFromStream(strBuf, strBuf.Length, readbuf, ref readsize, ref width, ref height, ref stride, ref mipLevels, ref format); if (readsize > 16777216) { readbuf = new byte[readsize]; DXTexdds.LoadDDSImageFromStream(strBuf, strBuf.Length, readbuf, ref readsize, ref width, ref height, ref stride, ref mipLevels, ref format); } Array.Resize<byte>(ref readbuf, readsize); //int length = stride * height / 4; //int totalLength = 0; //for (int i = 0; i < mipLevels; i++) //{ // totalLength += length; // length /= 4; //} //var buffer = new byte[totalLength/2]; //Marshal.Copy(p, buffer, 0, buffer.Length); switch ((DXGI_FORMAT)format) { // compressed case DXGI_FORMAT.DXGI_FORMAT_BC1_UNORM: texture.Reset(width, height, mipLevels, stride / 4, TextureFormat.D3DFMT_DXT1); texture.SetTextureData(readbuf); break; case DXGI_FORMAT.DXGI_FORMAT_BC2_UNORM: texture.Reset(width, height, mipLevels, stride / 4, TextureFormat.D3DFMT_DXT3); texture.SetTextureData(readbuf); break; case DXGI_FORMAT.DXGI_FORMAT_BC3_UNORM: texture.Reset(width, height, mipLevels, stride / 4, TextureFormat.D3DFMT_DXT5); texture.SetTextureData(readbuf); break; case DXGI_FORMAT.DXGI_FORMAT_BC4_UNORM: texture.Reset(width, height, mipLevels, stride / 4, TextureFormat.D3DFMT_ATI1); texture.SetTextureData(readbuf); break; case DXGI_FORMAT.DXGI_FORMAT_BC5_UNORM: texture.Reset(width, height, mipLevels, stride / 4, TextureFormat.D3DFMT_ATI2); texture.SetTextureData(readbuf); break; case DXGI_FORMAT.DXGI_FORMAT_BC7_UNORM: texture.Reset(width, height, mipLevels, stride / 4, TextureFormat.D3DFMT_BC7); texture.SetTextureData(readbuf); break; // uncompressed case DXGI_FORMAT.DXGI_FORMAT_B5G5R5A1_UNORM: texture.Reset(width, height, mipLevels, stride, TextureFormat.D3DFMT_A1R5G5B5); texture.SetTextureData(readbuf); break; case DXGI_FORMAT.DXGI_FORMAT_A8_UNORM: texture.Reset(width, height, mipLevels, stride, TextureFormat.D3DFMT_A8); texture.SetTextureData(readbuf); break; case DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM: texture.Reset(width, height, mipLevels, stride, TextureFormat.D3DFMT_A8B8G8R8); texture.SetTextureData(readbuf); break; case DXGI_FORMAT.DXGI_FORMAT_R8_UNORM: texture.Reset(width, height, mipLevels, stride, TextureFormat.D3DFMT_L8); texture.SetTextureData(readbuf); break; case DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM: texture.Reset(width, height, mipLevels, stride, TextureFormat.D3DFMT_A8R8G8B8); texture.SetTextureData(readbuf); break; default: MessageBox.Show("format not supported!"); throw new Exception("unsupported format"); } }