private uint GetHeaderSize() { uint size = 0; // DDS.DDS_MAGIC size += 4; size += DDS_HEADER.GetSize(); // If DXT10 add that size too switch ((DXGI_FORMAT)format) { case DXGI_FORMAT.BC1_UNORM_SRGB: case DXGI_FORMAT.BC3_UNORM_SRGB: case DXGI_FORMAT.BC4_UNORM: case DXGI_FORMAT.BC5_SNORM: case DXGI_FORMAT.BC7_UNORM: case DXGI_FORMAT.BC7_UNORM_SRGB: size += DDS_HEADER_DXT10.GetSize(); break; } return(size); }
private void WriteHeader(BinaryWriter bw) { var ddsHeader = new DDS_HEADER(); ddsHeader.dwSize = DDS_HEADER.GetSize(); ddsHeader.dwHeaderFlags = DDS.DDS_HEADER_FLAGS_TEXTURE | DDS.DDS_HEADER_FLAGS_LINEARSIZE | DDS.DDS_HEADER_FLAGS_MIPMAP; ddsHeader.dwHeight = height; ddsHeader.dwWidth = width; ddsHeader.dwMipMapCount = numMips; ddsHeader.PixelFormat.dwSize = DDS_PIXELFORMAT.GetSize(); ddsHeader.dwSurfaceFlags = DDS.DDS_SURFACE_FLAGS_TEXTURE | DDS.DDS_SURFACE_FLAGS_MIPMAP; switch ((DXGI_FORMAT)format) { case DXGI_FORMAT.BC1_UNORM: ddsHeader.PixelFormat.dwFlags = DDS.DDS_FOURCC; ddsHeader.PixelFormat.dwFourCC = DDS.MAKEFOURCC('D', 'X', 'T', '1'); ddsHeader.dwPitchOrLinearSize = (uint)(width * height / 2); // 4bpp break; case DXGI_FORMAT.BC2_UNORM: ddsHeader.PixelFormat.dwFlags = DDS.DDS_FOURCC; ddsHeader.PixelFormat.dwFourCC = DDS.MAKEFOURCC('D', 'X', 'T', '3'); ddsHeader.dwPitchOrLinearSize = (uint)(width * height); // 8bpp break; case DXGI_FORMAT.BC3_UNORM: ddsHeader.PixelFormat.dwFlags = DDS.DDS_FOURCC; ddsHeader.PixelFormat.dwFourCC = DDS.MAKEFOURCC('D', 'X', 'T', '5'); ddsHeader.dwPitchOrLinearSize = (uint)(width * height); // 8bpp break; case DXGI_FORMAT.BC5_UNORM: ddsHeader.PixelFormat.dwFlags = DDS.DDS_FOURCC; ddsHeader.PixelFormat.dwFourCC = DDS.MAKEFOURCC('A', 'T', 'I', '2'); ddsHeader.dwPitchOrLinearSize = (uint)(width * height); // 8bpp break; case DXGI_FORMAT.BC1_UNORM_SRGB: ddsHeader.PixelFormat.dwFlags = DDS.DDS_FOURCC; ddsHeader.PixelFormat.dwFourCC = DDS.MAKEFOURCC('D', 'X', '1', '0'); ddsHeader.dwPitchOrLinearSize = (uint)(width * height / 2); // 4bpp break; case DXGI_FORMAT.BC3_UNORM_SRGB: case DXGI_FORMAT.BC4_UNORM: case DXGI_FORMAT.BC5_SNORM: case DXGI_FORMAT.BC7_UNORM: case DXGI_FORMAT.BC7_UNORM_SRGB: ddsHeader.PixelFormat.dwFlags = DDS.DDS_FOURCC; ddsHeader.PixelFormat.dwFourCC = DDS.MAKEFOURCC('D', 'X', '1', '0'); ddsHeader.dwPitchOrLinearSize = (uint)(width * height); // 8bpp break; case DXGI_FORMAT.R8G8B8A8_UNORM: case DXGI_FORMAT.R8G8B8A8_UNORM_SRGB: ddsHeader.PixelFormat.dwFlags = DDS.DDS_RGBA; ddsHeader.PixelFormat.dwRGBBitCount = 32; ddsHeader.PixelFormat.dwRBitMask = 0x000000FF; ddsHeader.PixelFormat.dwGBitMask = 0x0000FF00; ddsHeader.PixelFormat.dwBBitMask = 0x00FF0000; ddsHeader.PixelFormat.dwABitMask = 0xFF000000; ddsHeader.dwPitchOrLinearSize = (uint)(width * height * 4); // 32bpp break; case DXGI_FORMAT.B8G8R8A8_UNORM: case DXGI_FORMAT.B8G8R8X8_UNORM: ddsHeader.PixelFormat.dwFlags = DDS.DDS_RGBA; ddsHeader.PixelFormat.dwRGBBitCount = 32; ddsHeader.PixelFormat.dwRBitMask = 0x00FF0000; ddsHeader.PixelFormat.dwGBitMask = 0x0000FF00; ddsHeader.PixelFormat.dwBBitMask = 0x000000FF; ddsHeader.PixelFormat.dwABitMask = 0xFF000000; ddsHeader.dwPitchOrLinearSize = (uint)(width * height * 4); // 32bpp break; case DXGI_FORMAT.R8_UNORM: ddsHeader.PixelFormat.dwFlags = DDS.DDS_RGB; ddsHeader.PixelFormat.dwRGBBitCount = 8; ddsHeader.PixelFormat.dwRBitMask = 0xFF; ddsHeader.dwPitchOrLinearSize = (uint)(width * height); // 8bpp break; default: throw new UnsupportedDDSException("Unsupported DDS header format. File: " + this.FullPath); } bw.Write((uint)DDS.DDS_MAGIC); ddsHeader.Write(bw); switch ((DXGI_FORMAT)format) { case DXGI_FORMAT.BC1_UNORM_SRGB: case DXGI_FORMAT.BC3_UNORM_SRGB: case DXGI_FORMAT.BC4_UNORM: case DXGI_FORMAT.BC5_SNORM: case DXGI_FORMAT.BC7_UNORM: case DXGI_FORMAT.BC7_UNORM_SRGB: var dxt10 = new DDS_HEADER_DXT10() { dxgiFormat = format, resourceDimension = (uint)DXT10_RESOURCE_DIMENSION.DIMENSION_TEXTURE2D, miscFlag = 0, arraySize = 1, miscFlags2 = DDS.DDS_ALPHA_MODE_UNKNOWN }; dxt10.Write(bw); break; } }