Utility/Loaders: Fix and enable DDS loader (experimental)

Former-commit-id: 647e4527d47bc82b25eb713b8e6ffc4f424ba6c3
This commit is contained in:
Lynix 2016-04-21 13:23:11 +02:00
parent 718713dbdd
commit de76b48fdd
6 changed files with 709 additions and 457 deletions

View File

@ -168,6 +168,17 @@ namespace Nz
PixelFormatType_Max = PixelFormatType_Stencil16 PixelFormatType_Max = PixelFormatType_Stencil16
}; };
enum PixelFormatSubType
{
PixelFormatSubType_Double, // F64
PixelFormatSubType_Float, // F32
PixelFormatSubType_Half, // F16
PixelFormatSubType_Int, // I32
PixelFormatSubType_Unsigned, // U32
PixelFormatSubType_Max = PixelFormatSubType_Unsigned
};
enum PixelFormatTypeType enum PixelFormatTypeType
{ {
PixelFormatTypeType_Undefined = -1, PixelFormatTypeType_Undefined = -1,

View File

@ -0,0 +1,92 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/Formats/DDSConstants.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Utility/Debug.hpp>
namespace Nz
{
bool Unserialize(SerializationContext& context, DDSHeader* header)
{
if (!Unserialize(context, &header->size))
return false;
if (!Unserialize(context, &header->flags))
return false;
if (!Unserialize(context, &header->height))
return false;
if (!Unserialize(context, &header->width))
return false;
if (!Unserialize(context, &header->pitch))
return false;
if (!Unserialize(context, &header->depth))
return false;
if (!Unserialize(context, &header->levelCount))
return false;
for (unsigned int i = 0; i < CountOf(header->reserved1); ++i)
{
if (!Unserialize(context, &header->reserved1[i]))
return false;
}
if (!Unserialize(context, &header->format))
return false;
for (unsigned int i = 0; i < CountOf(header->ddsCaps); ++i)
{
if (!Unserialize(context, &header->ddsCaps[i]))
return false;
}
if (!Unserialize(context, &header->reserved2))
return false;
return true;
}
bool Unserialize(SerializationContext& context, DDSHeaderDX10Ext* header)
{
UInt32 enumValue;
if (!Unserialize(context, &enumValue))
return false;
header->dxgiFormat = static_cast<DXGI_FORMAT>(enumValue);
if (!Unserialize(context, &enumValue))
return false;
header->resourceDimension = static_cast<D3D10_RESOURCE_DIMENSION>(enumValue);
if (!Unserialize(context, &header->miscFlag))
return false;
if (!Unserialize(context, &header->arraySize))
return false;
if (!Unserialize(context, &header->reserved))
return false;
return true;
}
bool Unserialize(SerializationContext& context, DDSPixelFormat* pixelFormat)
{
if (!Unserialize(context, &pixelFormat->size))
return false;
if (!Unserialize(context, &pixelFormat->flags))
return false;
if (!Unserialize(context, &pixelFormat->fourCC))
return false;
if (!Unserialize(context, &pixelFormat->bpp))
return false;
if (!Unserialize(context, &pixelFormat->redMask))
return false;
if (!Unserialize(context, &pixelFormat->greenMask))
return false;
if (!Unserialize(context, &pixelFormat->blueMask))
return false;
if (!Unserialize(context, &pixelFormat->alphaMask))
return false;
return true;
}
}

View File

@ -8,40 +8,41 @@
#define NAZARA_LOADERS_DDS_CONSTANTS_HPP #define NAZARA_LOADERS_DDS_CONSTANTS_HPP
#include <Nazara/Prerequesites.hpp> #include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Serialization.hpp>
#include <Nazara/Utility/Config.hpp>
#define DDS_MAGIC 0x20534444 namespace Nz
#define DDS_DXT1 0x31545844
#define DDS_DXT3 0x33545844
#define DDS_DXT5 0x35545844
inline constexpr Nz::UInt32 FourCC(Nz::UInt32 a, Nz::UInt32 b, Nz::UInt32 c, Nz::UInt32 d)
{ {
inline constexpr UInt32 DDS_FourCC(UInt32 a, UInt32 b, UInt32 c, UInt32 d)
{
return a << 0 | return a << 0 |
b << 8 | b << 8 |
c << 16 | c << 16 |
d << 24; d << 24;
} }
enum D3D10_RESOURCE_DIMENSION : Nz::UInt32 constexpr UInt32 DDS_Magic = DDS_FourCC('D', 'D', 'S', ' ');
{
enum D3D10_RESOURCE_DIMENSION
{
D3D10_RESOURCE_DIMENSION_UNKNOWN = 0, D3D10_RESOURCE_DIMENSION_UNKNOWN = 0,
D3D10_RESOURCE_DIMENSION_BUFFER = 1, D3D10_RESOURCE_DIMENSION_BUFFER = 1,
D3D10_RESOURCE_DIMENSION_TEXTURE1D = 2, D3D10_RESOURCE_DIMENSION_TEXTURE1D = 2,
D3D10_RESOURCE_DIMENSION_TEXTURE2D = 3, D3D10_RESOURCE_DIMENSION_TEXTURE2D = 3,
D3D10_RESOURCE_DIMENSION_TEXTURE3D = 4 D3D10_RESOURCE_DIMENSION_TEXTURE3D = 4
}; };
enum D3D10_RESOURCE_MISC enum D3D10_RESOURCE_MISC
{ {
D3D10_RESOURCE_MISC_GENERATE_MIPS = 0x1L, D3D10_RESOURCE_MISC_GENERATE_MIPS = 0x1L,
D3D10_RESOURCE_MISC_SHARED = 0x2L, D3D10_RESOURCE_MISC_SHARED = 0x2L,
D3D10_RESOURCE_MISC_TEXTURECUBE = 0x4L, D3D10_RESOURCE_MISC_TEXTURECUBE = 0x4L,
D3D10_RESOURCE_MISC_SHARED_KEYEDMUTEX = 0x10L, D3D10_RESOURCE_MISC_SHARED_KEYEDMUTEX = 0x10L,
D3D10_RESOURCE_MISC_GDI_COMPATIBLE = 0x20L D3D10_RESOURCE_MISC_GDI_COMPATIBLE = 0x20L
}; };
enum D3DFMT : Nz::UInt32 enum D3DFMT
{ {
D3DFMT_UNKNOWN = 0, D3DFMT_UNKNOWN = 0,
D3DFMT_R8G8B8 = 20, D3DFMT_R8G8B8 = 20,
@ -76,15 +77,15 @@ enum D3DFMT : Nz::UInt32
D3DFMT_V16U16 = 64, D3DFMT_V16U16 = 64,
D3DFMT_A2W10V10U10 = 67, D3DFMT_A2W10V10U10 = 67,
D3DFMT_UYVY = FourCC('U', 'Y', 'V', 'Y'), D3DFMT_UYVY = DDS_FourCC('U', 'Y', 'V', 'Y'),
D3DFMT_R8G8_B8G8 = FourCC('R', 'G', 'B', 'G'), D3DFMT_R8G8_B8G8 = DDS_FourCC('R', 'G', 'B', 'G'),
D3DFMT_YUY2 = FourCC('Y', 'U', 'Y', '2'), D3DFMT_YUY2 = DDS_FourCC('Y', 'U', 'Y', '2'),
D3DFMT_G8R8_G8B8 = FourCC('G', 'R', 'G', 'B'), D3DFMT_G8R8_G8B8 = DDS_FourCC('G', 'R', 'G', 'B'),
D3DFMT_DXT1 = FourCC('D', 'X', 'T', '1'), D3DFMT_DXT1 = DDS_FourCC('D', 'X', 'T', '1'),
D3DFMT_DXT2 = FourCC('D', 'X', 'T', '2'), D3DFMT_DXT2 = DDS_FourCC('D', 'X', 'T', '2'),
D3DFMT_DXT3 = FourCC('D', 'X', 'T', '3'), D3DFMT_DXT3 = DDS_FourCC('D', 'X', 'T', '3'),
D3DFMT_DXT4 = FourCC('D', 'X', 'T', '4'), D3DFMT_DXT4 = DDS_FourCC('D', 'X', 'T', '4'),
D3DFMT_DXT5 = FourCC('D', 'X', 'T', '5'), D3DFMT_DXT5 = DDS_FourCC('D', 'X', 'T', '5'),
D3DFMT_D16_LOCKABLE = 70, D3DFMT_D16_LOCKABLE = 70,
D3DFMT_D32 = 71, D3DFMT_D32 = 71,
@ -105,7 +106,7 @@ enum D3DFMT : Nz::UInt32
D3DFMT_Q16W16V16U16 = 110, D3DFMT_Q16W16V16U16 = 110,
D3DFMT_MULTI2_ARGB8 = FourCC('M','E','T','1'), D3DFMT_MULTI2_ARGB8 = DDS_FourCC('M','E','T','1'),
D3DFMT_R16F = 111, D3DFMT_R16F = 111,
D3DFMT_G16R16F = 112, D3DFMT_G16R16F = 112,
@ -117,21 +118,21 @@ enum D3DFMT : Nz::UInt32
D3DFMT_CxV8U8 = 117, D3DFMT_CxV8U8 = 117,
D3DFMT_DX10 = FourCC('D', 'X', '1', '0') D3DFMT_DX10 = DDS_FourCC('D', 'X', '1', '0')
}; };
enum DDPF enum DDPF
{ {
DDPF_ALPHAPIXELS = 0x00001, DDPF_ALPHAPIXELS = 0x00001,
DDPF_ALPHA = 0x00002, DDPF_ALPHA = 0x00002,
DDPF_FOURCC = 0x00004, DDPF_FOURCC = 0x00004,
DDPF_RGB = 0x00040, DDPF_RGB = 0x00040,
DDPF_YUV = 0x00200, DDPF_YUV = 0x00200,
DDPF_LUMINANCE = 0x20000 DDPF_LUMINANCE = 0x20000
}; };
enum DDSD enum DDSD
{ {
DDSD_CAPS = 0x00000001, DDSD_CAPS = 0x00000001,
DDSD_HEIGHT = 0x00000002, DDSD_HEIGHT = 0x00000002,
DDSD_WIDTH = 0x00000004, DDSD_WIDTH = 0x00000004,
@ -140,17 +141,17 @@ enum DDSD
DDSD_MIPMAPCOUNT = 0x00020000, DDSD_MIPMAPCOUNT = 0x00020000,
DDSD_LINEARSIZE = 0x00080000, DDSD_LINEARSIZE = 0x00080000,
DDSD_DEPTH = 0x00800000 DDSD_DEPTH = 0x00800000
}; };
enum DDSCAPS enum DDSCAPS
{ {
DDSCAPS_COMPLEX = 0x00000008, DDSCAPS_COMPLEX = 0x00000008,
DDSCAPS_MIPMAP = 0x00400000, DDSCAPS_MIPMAP = 0x00400000,
DDSCAPS_TEXTURE = 0x00001000 DDSCAPS_TEXTURE = 0x00001000
}; };
enum DDSCAPS2 enum DDSCAPS2
{ {
DDSCAPS2_CUBEMAP = 0x00000200, DDSCAPS2_CUBEMAP = 0x00000200,
DDSCAPS2_CUBEMAP_POSITIVEX = 0x00000400, DDSCAPS2_CUBEMAP_POSITIVEX = 0x00000400,
DDSCAPS2_CUBEMAP_NEGATIVEX = 0x00000800, DDSCAPS2_CUBEMAP_NEGATIVEX = 0x00000800,
@ -158,20 +159,24 @@ enum DDSCAPS2
DDSCAPS2_CUBEMAP_NEGATIVEY = 0x00002000, DDSCAPS2_CUBEMAP_NEGATIVEY = 0x00002000,
DDSCAPS2_CUBEMAP_POSITIVEZ = 0x00004000, DDSCAPS2_CUBEMAP_POSITIVEZ = 0x00004000,
DDSCAPS2_CUBEMAP_NEGATIVEZ = 0x00008000, DDSCAPS2_CUBEMAP_NEGATIVEZ = 0x00008000,
DDSCAPS2_VOLUME = 0x00200000 DDSCAPS2_VOLUME = 0x00200000,
};
enum DDS_COLOR DDSCAPS2_CUBEMAP_ALLFACES = DDSCAPS2_CUBEMAP_POSITIVEX | DDSCAPS2_CUBEMAP_NEGATIVEX |
{ DDSCAPS2_CUBEMAP_POSITIVEY | DDSCAPS2_CUBEMAP_NEGATIVEY |
DDSCAPS2_CUBEMAP_POSITIVEZ | DDSCAPS2_CUBEMAP_NEGATIVEZ
};
enum DDS_COLOR
{
DDS_COLOR_DEFAULT = 0, DDS_COLOR_DEFAULT = 0,
DDS_COLOR_DISTANCE, DDS_COLOR_DISTANCE,
DDS_COLOR_LUMINANCE, DDS_COLOR_LUMINANCE,
DDS_COLOR_INSET_BBOX, DDS_COLOR_INSET_BBOX,
DDS_COLOR_MAX DDS_COLOR_MAX
}; };
enum DDS_COMPRESS enum DDS_COMPRESS
{ {
DDS_COMPRESS_NONE = 0, DDS_COMPRESS_NONE = 0,
DDS_COMPRESS_BC1, /* DXT1 */ DDS_COMPRESS_BC1, /* DXT1 */
DDS_COMPRESS_BC2, /* DXT3 */ DDS_COMPRESS_BC2, /* DXT3 */
@ -183,10 +188,10 @@ enum DDS_COMPRESS
DDS_COMPRESS_YCOCG, /* DXT5 */ DDS_COMPRESS_YCOCG, /* DXT5 */
DDS_COMPRESS_YCOCGS, /* DXT5 */ DDS_COMPRESS_YCOCGS, /* DXT5 */
DDS_COMPRESS_MAX DDS_COMPRESS_MAX
}; };
enum DDS_FORMAT enum DDS_FORMAT
{ {
DDS_FORMAT_DEFAULT = 0, DDS_FORMAT_DEFAULT = 0,
DDS_FORMAT_RGB8, DDS_FORMAT_RGB8,
DDS_FORMAT_RGBA8, DDS_FORMAT_RGBA8,
@ -203,10 +208,10 @@ enum DDS_FORMAT
DDS_FORMAT_AEXP, DDS_FORMAT_AEXP,
DDS_FORMAT_YCOCG, DDS_FORMAT_YCOCG,
DDS_FORMAT_MAX DDS_FORMAT_MAX
}; };
enum DDS_MIPMAP enum DDS_MIPMAP
{ {
DDS_MIPMAP_DEFAULT = 0, DDS_MIPMAP_DEFAULT = 0,
DDS_MIPMAP_NEAREST, DDS_MIPMAP_NEAREST,
DDS_MIPMAP_BOX, DDS_MIPMAP_BOX,
@ -214,18 +219,18 @@ enum DDS_MIPMAP
DDS_MIPMAP_BICUBIC, DDS_MIPMAP_BICUBIC,
DDS_MIPMAP_LANCZOS, DDS_MIPMAP_LANCZOS,
DDS_MIPMAP_MAX DDS_MIPMAP_MAX
}; };
enum DDS_SAVE enum DDS_SAVE
{ {
DDS_SAVE_SELECTED_LAYER = 0, DDS_SAVE_SELECTED_LAYER = 0,
DDS_SAVE_CUBEMAP, DDS_SAVE_CUBEMAP,
DDS_SAVE_VOLUMEMAP, DDS_SAVE_VOLUMEMAP,
DDS_SAVE_MAX DDS_SAVE_MAX
}; };
enum DXGI_FORMAT : Nz::UInt32 enum DXGI_FORMAT
{ {
DXGI_FORMAT_UNKNOWN = 0, DXGI_FORMAT_UNKNOWN = 0,
DXGI_FORMAT_R32G32B32A32_TYPELESS = 1, DXGI_FORMAT_R32G32B32A32_TYPELESS = 1,
DXGI_FORMAT_R32G32B32A32_FLOAT = 2, DXGI_FORMAT_R32G32B32A32_FLOAT = 2,
@ -326,51 +331,47 @@ enum DXGI_FORMAT : Nz::UInt32
DXGI_FORMAT_BC7_TYPELESS = 97, DXGI_FORMAT_BC7_TYPELESS = 97,
DXGI_FORMAT_BC7_UNORM = 98, DXGI_FORMAT_BC7_UNORM = 98,
DXGI_FORMAT_BC7_UNORM_SRGB = 99 DXGI_FORMAT_BC7_UNORM_SRGB = 99
}; };
struct DDSPixelFormat // DDPIXELFORMAT struct DDSPixelFormat // DDPIXELFORMAT
{ {
Nz::UInt32 size; UInt32 size;
Nz::UInt32 flags; UInt32 flags;
Nz::UInt32 fourCC; UInt32 fourCC;
Nz::UInt32 bpp; UInt32 bpp;
Nz::UInt32 redMask; UInt32 redMask;
Nz::UInt32 greenMask; UInt32 greenMask;
Nz::UInt32 blueMask; UInt32 blueMask;
Nz::UInt32 alphaMask; UInt32 alphaMask;
}; };
static_assert(sizeof(DDSPixelFormat) == 8*sizeof(Nz::UInt32), "DDSPixelFormat must be packed"); struct DDSHeader
{
struct DDSHeader UInt32 size;
{ UInt32 flags;
Nz::UInt32 size; UInt32 height;
Nz::UInt32 flags; UInt32 width;
Nz::UInt32 height; UInt32 pitch;
Nz::UInt32 width; UInt32 depth;
Nz::UInt32 pitch; UInt32 levelCount;
Nz::UInt32 depth; UInt32 reserved1[11];
Nz::UInt32 levelCount;
Nz::UInt32 reserved1[11];
DDSPixelFormat format; DDSPixelFormat format;
Nz::UInt32 ddsCaps1; UInt32 ddsCaps[4];
Nz::UInt32 ddsCaps2; UInt32 reserved2;
Nz::UInt32 ddsCaps3; };
Nz::UInt32 ddsCaps4;
Nz::UInt32 reserved2;
};
static_assert(sizeof(DDSHeader) == 23*sizeof(Nz::UInt32) + sizeof(DDSPixelFormat), "DDSHeader must be packed"); struct DDSHeaderDX10Ext
{
struct DDSHeaderDX10Ext
{
DXGI_FORMAT dxgiFormat; DXGI_FORMAT dxgiFormat;
D3D10_RESOURCE_DIMENSION resourceDimension; D3D10_RESOURCE_DIMENSION resourceDimension;
Nz::UInt32 miscFlag; UInt32 miscFlag;
Nz::UInt32 arraySize; UInt32 arraySize;
Nz::UInt32 reserved; UInt32 reserved;
}; };
static_assert(sizeof(DDSHeaderDX10Ext) == 5*sizeof(Nz::UInt32), "DDSHeaderDX10Ext must be packed"); NAZARA_UTILITY_API bool Unserialize(SerializationContext& context, DDSHeader* header);
NAZARA_UTILITY_API bool Unserialize(SerializationContext& context, DDSHeaderDX10Ext* header);
NAZARA_UTILITY_API bool Unserialize(SerializationContext& context, DDSPixelFormat* pixelFormat);
}
#endif // NAZARA_LOADERS_DDS_CONSTANTS_HPP #endif // NAZARA_LOADERS_DDS_CONSTANTS_HPP

View File

@ -3,63 +3,60 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/Formats/DDSLoader.hpp> #include <Nazara/Utility/Formats/DDSLoader.hpp>
#include <Nazara/Core/Endianness.hpp> #include <Nazara/Core/ByteStream.hpp>
#include <Nazara/Core/Error.hpp> #include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Stream.hpp> #include <Nazara/Core/Stream.hpp>
#include <Nazara/Utility/Image.hpp> #include <Nazara/Utility/Image.hpp>
#include <Nazara/Utility/PixelFormat.hpp>
#include <Nazara/Utility/Formats/DDSConstants.hpp> #include <Nazara/Utility/Formats/DDSConstants.hpp>
#include <memory> #include <memory>
#include <Nazara/Utility/Debug.hpp> #include <Nazara/Utility/Debug.hpp>
namespace Nz namespace Nz
{ {
namespace class DDSLoader
{ {
bool IsSupported(const String& extension) public:
DDSLoader() = delete;
~DDSLoader() = delete;
static bool IsSupported(const String& extension)
{ {
return (extension == "dds"); return (extension == "dds");
} }
Ternary Check(Stream& stream, const ImageParams& parameters) static Ternary Check(Stream& stream, const ImageParams& parameters)
{ {
bool skip; bool skip;
if (parameters.custom.GetBooleanParameter("SkipNativeDDSLoader", &skip) && skip) if (parameters.custom.GetBooleanParameter("SkipNativeDDSLoader", &skip) && skip)
return Ternary_False; return Ternary_False;
ByteStream byteStream(&stream);
byteStream.SetDataEndianness(Endianness_LittleEndian);
UInt32 magic; UInt32 magic;
if (stream.Read(&magic, sizeof(UInt32)) == sizeof(UInt32)) byteStream >> magic;
{
#ifdef NAZARA_BIG_ENDIAN
ByteSwap(&magic, sizeof(UInt32));
#endif
if (magic == DDS_MAGIC) return (magic == DDS_Magic) ? Ternary_True : Ternary_False;
return Ternary_True;
} }
return Ternary_False; static bool Load(Image* image, Stream& stream, const ImageParams& parameters)
}
bool Load(Image* image, Stream& stream, const ImageParams& parameters)
{ {
NazaraUnused(parameters); NazaraUnused(parameters);
ByteStream byteStream(&stream);
byteStream.SetDataEndianness(Endianness_LittleEndian);
UInt32 magic;
byteStream >> magic;
NazaraAssert(magic == DDS_Magic, "Invalid DDS file"); // The Check function should make sure this doesn't happen
DDSHeader header; DDSHeader header;
if (stream.Read(&header, sizeof(DDSHeader)) != sizeof(DDSHeader)) byteStream >> header;
{
NazaraError("Failed to read DDS header");
return false;
}
DDSHeaderDX10Ext headerDX10; DDSHeaderDX10Ext headerDX10;
if (header.format.flags & DDPF_FOURCC && header.format.fourCC == D3DFMT_DX10) if (header.format.flags & DDPF_FOURCC && header.format.fourCC == D3DFMT_DX10)
{ byteStream >> headerDX10;
if (stream.Read(&headerDX10, sizeof(DDSHeaderDX10Ext)) != sizeof(DDSHeaderDX10Ext))
{
NazaraError("Failed to read DDS DX10 extension header");
return false;
}
}
else else
{ {
headerDX10.arraySize = 1; headerDX10.arraySize = 1;
@ -68,64 +65,213 @@ namespace Nz
headerDX10.resourceDimension = D3D10_RESOURCE_DIMENSION_UNKNOWN; headerDX10.resourceDimension = D3D10_RESOURCE_DIMENSION_UNKNOWN;
} }
#ifdef NAZARA_BIG_ENDIAN if (header.flags & DDSD_WIDTH == 0)
// Les fichiers DDS sont en little endian NazaraWarning("Ill-formed DDS file, doesn't have a width flag");
ByteSwap(&header.size, sizeof(UInt32));
ByteSwap(&header.flags, sizeof(UInt32));
ByteSwap(&header.height, sizeof(UInt32));
ByteSwap(&header.width, sizeof(UInt32));
ByteSwap(&header.pitch, sizeof(UInt32));
ByteSwap(&header.depth, sizeof(UInt32));
ByteSwap(&header.levelCount, sizeof(UInt32));
// DDS_PixelFormat unsigned int width = std::max(header.width, 1U);
ByteSwap(&header.format.size, sizeof(UInt32));
ByteSwap(&header.format.flags, sizeof(UInt32));
ByteSwap(&header.format.fourCC, sizeof(UInt32));
ByteSwap(&header.format.bpp, sizeof(UInt32));
ByteSwap(&header.format.redMask, sizeof(UInt32));
ByteSwap(&header.format.greenMask, sizeof(UInt32));
ByteSwap(&header.format.blueMask, sizeof(UInt32));
ByteSwap(&header.format.alphaMask, sizeof(UInt32));
ByteSwap(&header.ddsCaps1, sizeof(UInt32)); unsigned int height = 1U;
ByteSwap(&header.ddsCaps2, sizeof(UInt32)); if (header.flags & DDSD_HEIGHT)
ByteSwap(&header.ddsCaps3, sizeof(UInt32)); height = std::max(header.height, 1U);
ByteSwap(&header.ddsCaps4, sizeof(UInt32));
#endif unsigned int depth = 1U;
if (header.flags & DDSD_DEPTH)
depth = std::max(header.depth, 1U);
unsigned int width = header.width;
unsigned int height = header.height;
unsigned int depth = std::max(header.depth, 1U);
unsigned int levelCount = (parameters.levelCount > 0) ? std::min(parameters.levelCount, static_cast<UInt8>(header.levelCount)) : header.levelCount; unsigned int levelCount = (parameters.levelCount > 0) ? std::min(parameters.levelCount, static_cast<UInt8>(header.levelCount)) : header.levelCount;
// Détermination du type // First, identify the type
ImageType type; ImageType type;
if (header.ddsCaps2 & DDSCAPS2_CUBEMAP) if (!IdentifyImageType(header, headerDX10, &type))
type = ImageType_Cubemap; return false;
else if (header.ddsCaps2 & DDSCAPS2_VOLUME)
type = ImageType_3D;
// Détermination du format // Then the format
PixelFormatType format; PixelFormatType format;
if (!IdentifyPixelFormat(header, headerDX10, &format))
return false;
image->Create(type, format, width, height, depth, levelCount);
// Read all mipmap levels
for (unsigned int i = 0; i < image->GetLevelCount(); i++)
{
std::size_t byteCount = PixelFormat::ComputeSize(format, width, height, depth);
UInt8* ptr = image->GetPixels(0, 0, 0, i);
if (byteStream.Read(ptr, byteCount) != byteCount)
{
NazaraError("Failed to read level #" + String::Number(i));
return false;
}
if (width > 1)
width >>= 1;
if (height > 1)
height >>= 1;
if (depth > 1)
depth >>= 1;
}
if (parameters.loadFormat != PixelFormatType_Undefined) if (parameters.loadFormat != PixelFormatType_Undefined)
image->Convert(parameters.loadFormat); image->Convert(parameters.loadFormat);
return true; return true;
} }
private:
static bool IdentifyImageType(const DDSHeader& header, const DDSHeaderDX10Ext& headerExt, ImageType* type)
{
if (headerExt.arraySize > 1)
{
if (header.ddsCaps[1] & DDSCAPS2_CUBEMAP)
{
NazaraError("Cubemap arrays are not yet supported, sorry");
return false;
} }
else if (header.flags & DDSD_HEIGHT)
*type = ImageType_2D_Array;
else
*type = ImageType_1D_Array;
}
else
{
if (header.ddsCaps[1] & DDSCAPS2_CUBEMAP)
{
if (header.ddsCaps[1] & DDSCAPS2_CUBEMAP_ALLFACES != DDSCAPS2_CUBEMAP_ALLFACES)
{
NazaraError("Partial cubemap are not yet supported, sorry");
return false;
}
*type = ImageType_Cubemap;
}
else if (headerExt.resourceDimension == D3D10_RESOURCE_DIMENSION_BUFFER)
{
NazaraError("Texture buffers are not yet supported, sorry");
return false;
}
else if (headerExt.resourceDimension == D3D10_RESOURCE_DIMENSION_TEXTURE1D)
*type = ImageType_1D;
else if (header.ddsCaps[1] & DDSCAPS2_VOLUME || header.flags & DDSD_DEPTH || headerExt.resourceDimension == D3D10_RESOURCE_DIMENSION_TEXTURE3D)
*type = ImageType_3D;
else
*type = ImageType_2D;
}
return true;
}
static bool IdentifyPixelFormat(const DDSHeader& header, const DDSHeaderDX10Ext& headerExt, PixelFormatType* format)
{
if (header.format.flags & (DDPF_RGB | DDPF_ALPHA | DDPF_ALPHAPIXELS | DDPF_LUMINANCE))
{
PixelFormatInfo info(header.format.bpp, PixelFormatSubType_Unsigned);
if (header.format.flags & DDPF_RGB)
{
// DDS Masks are in little endian
info.redMask = SwapBytes(header.format.redMask);
info.greenMask = SwapBytes(header.format.greenMask);
info.blueMask = SwapBytes(header.format.blueMask);
}
else if (header.format.flags & DDPF_LUMINANCE)
info.redMask = SwapBytes(header.format.redMask);
if (header.format.flags & (DDPF_ALPHA | DDPF_ALPHAPIXELS))
info.alphaMask = SwapBytes(header.format.alphaMask);
*format = PixelFormat::IdentifyFormat(info);
if (!PixelFormat::IsValid(*format))
return false;
}
else if (header.format.flags & DDPF_FOURCC)
{
switch (header.format.fourCC)
{
case D3DFMT_DXT1:
*format = PixelFormatType_DXT1;
break;
case D3DFMT_DXT3:
*format = PixelFormatType_DXT3;
break;
case D3DFMT_DXT5:
*format = PixelFormatType_DXT3;
break;
case D3DFMT_DX10:
{
switch (headerExt.dxgiFormat)
{
case DXGI_FORMAT_R32G32B32A32_FLOAT:
*format = PixelFormatType_RGBA32F;
break;
case DXGI_FORMAT_R32G32B32A32_UINT:
*format = PixelFormatType_RGBA32UI;
break;
case DXGI_FORMAT_R32G32B32A32_SINT:
*format = PixelFormatType_RGBA32I;
break;
case DXGI_FORMAT_R32G32B32_FLOAT:
*format = PixelFormatType_RGB32F;
break;
case DXGI_FORMAT_R32G32B32_UINT:
//*format = PixelFormatType_RGB32U;
return false;
case DXGI_FORMAT_R32G32B32_SINT:
*format = PixelFormatType_RGB32I;
break;
case DXGI_FORMAT_R16G16B16A16_SNORM:
case DXGI_FORMAT_R16G16B16A16_SINT:
case DXGI_FORMAT_R16G16B16A16_UINT:
*format = PixelFormatType_RGBA16I;
break;
case DXGI_FORMAT_R16G16B16A16_UNORM:
*format = PixelFormatType_RGBA16UI;
break;
}
break;
}
default:
{
char buf[5];
buf[0] = (header.format.fourCC >> 0) & 255;
buf[1] = (header.format.fourCC >> 8) & 255;
buf[2] = (header.format.fourCC >> 16) & 255;
buf[3] = (header.format.fourCC >> 24) & 255;
buf[4] = '\0';
NazaraError("Unhandled format \"" + String(buf) + "\"");
return false;
}
}
}
else
{
NazaraError("Invalid DDS file");
return false;
}
return true;
}
};
namespace Loaders namespace Loaders
{ {
void RegisterDDS() void RegisterDDSLoader()
{ {
ImageLoader::RegisterLoader(IsSupported, Check, Load); ImageLoader::RegisterLoader(DDSLoader::IsSupported, DDSLoader::Check, DDSLoader::Load);
} }
void UnregisterDDS() void UnregisterDDSLoader()
{ {
ImageLoader::UnregisterLoader(IsSupported, Check, Load); ImageLoader::UnregisterLoader(DDSLoader::IsSupported, DDSLoader::Check, DDSLoader::Load);
} }
} }
} }

View File

@ -13,8 +13,8 @@ namespace Nz
{ {
namespace Loaders namespace Loaders
{ {
void RegisterDDS(); void RegisterDDSLoader();
void UnregisterDDS(); void UnregisterDDSLoader();
} }
} }

View File

@ -20,6 +20,7 @@
#include <Nazara/Utility/Skeleton.hpp> #include <Nazara/Utility/Skeleton.hpp>
#include <Nazara/Utility/VertexDeclaration.hpp> #include <Nazara/Utility/VertexDeclaration.hpp>
#include <Nazara/Utility/Window.hpp> #include <Nazara/Utility/Window.hpp>
#include <Nazara/Utility/Formats/DDSLoader.hpp>
#include <Nazara/Utility/Formats/FreeTypeLoader.hpp> #include <Nazara/Utility/Formats/FreeTypeLoader.hpp>
#include <Nazara/Utility/Formats/MD2Loader.hpp> #include <Nazara/Utility/Formats/MD2Loader.hpp>
#include <Nazara/Utility/Formats/MD5AnimLoader.hpp> #include <Nazara/Utility/Formats/MD5AnimLoader.hpp>
@ -113,6 +114,7 @@ namespace Nz
Loaders::RegisterFreeType(); Loaders::RegisterFreeType();
// Image // Image
Loaders::RegisterDDSLoader(); // Generic loader (STB)
Loaders::RegisterSTBLoader(); // Generic loader (STB) Loaders::RegisterSTBLoader(); // Generic loader (STB)
Loaders::RegisterSTBSaver(); // Generic saver (STB) Loaders::RegisterSTBSaver(); // Generic saver (STB)