Utility/Loaders: Fix and enable DDS loader (experimental)
Former-commit-id: 647e4527d47bc82b25eb713b8e6ffc4f424ba6c3
This commit is contained in:
@@ -3,129 +3,275 @@
|
||||
// For conditions of distribution and use, see copyright notice in Config.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/Stream.hpp>
|
||||
#include <Nazara/Utility/Image.hpp>
|
||||
#include <Nazara/Utility/PixelFormat.hpp>
|
||||
#include <Nazara/Utility/Formats/DDSConstants.hpp>
|
||||
#include <memory>
|
||||
#include <Nazara/Utility/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
namespace
|
||||
class DDSLoader
|
||||
{
|
||||
bool IsSupported(const String& extension)
|
||||
{
|
||||
return (extension == "dds");
|
||||
}
|
||||
public:
|
||||
DDSLoader() = delete;
|
||||
~DDSLoader() = delete;
|
||||
|
||||
Ternary Check(Stream& stream, const ImageParams& parameters)
|
||||
{
|
||||
bool skip;
|
||||
if (parameters.custom.GetBooleanParameter("SkipNativeDDSLoader", &skip) && skip)
|
||||
return Ternary_False;
|
||||
|
||||
UInt32 magic;
|
||||
if (stream.Read(&magic, sizeof(UInt32)) == sizeof(UInt32))
|
||||
static bool IsSupported(const String& extension)
|
||||
{
|
||||
#ifdef NAZARA_BIG_ENDIAN
|
||||
ByteSwap(&magic, sizeof(UInt32));
|
||||
#endif
|
||||
|
||||
if (magic == DDS_MAGIC)
|
||||
return Ternary_True;
|
||||
return (extension == "dds");
|
||||
}
|
||||
|
||||
return Ternary_False;
|
||||
}
|
||||
|
||||
bool Load(Image* image, Stream& stream, const ImageParams& parameters)
|
||||
{
|
||||
NazaraUnused(parameters);
|
||||
|
||||
DDSHeader header;
|
||||
if (stream.Read(&header, sizeof(DDSHeader)) != sizeof(DDSHeader))
|
||||
static Ternary Check(Stream& stream, const ImageParams& parameters)
|
||||
{
|
||||
NazaraError("Failed to read DDS header");
|
||||
return false;
|
||||
bool skip;
|
||||
if (parameters.custom.GetBooleanParameter("SkipNativeDDSLoader", &skip) && skip)
|
||||
return Ternary_False;
|
||||
|
||||
ByteStream byteStream(&stream);
|
||||
byteStream.SetDataEndianness(Endianness_LittleEndian);
|
||||
|
||||
UInt32 magic;
|
||||
byteStream >> magic;
|
||||
|
||||
return (magic == DDS_Magic) ? Ternary_True : Ternary_False;
|
||||
}
|
||||
|
||||
DDSHeaderDX10Ext headerDX10;
|
||||
if (header.format.flags & DDPF_FOURCC && header.format.fourCC == D3DFMT_DX10)
|
||||
static bool Load(Image* image, Stream& stream, const ImageParams& parameters)
|
||||
{
|
||||
if (stream.Read(&headerDX10, sizeof(DDSHeaderDX10Ext)) != sizeof(DDSHeaderDX10Ext))
|
||||
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;
|
||||
byteStream >> header;
|
||||
|
||||
DDSHeaderDX10Ext headerDX10;
|
||||
if (header.format.flags & DDPF_FOURCC && header.format.fourCC == D3DFMT_DX10)
|
||||
byteStream >> headerDX10;
|
||||
else
|
||||
{
|
||||
NazaraError("Failed to read DDS DX10 extension header");
|
||||
headerDX10.arraySize = 1;
|
||||
headerDX10.dxgiFormat = DXGI_FORMAT_UNKNOWN;
|
||||
headerDX10.miscFlag = 0;
|
||||
headerDX10.resourceDimension = D3D10_RESOURCE_DIMENSION_UNKNOWN;
|
||||
}
|
||||
|
||||
if (header.flags & DDSD_WIDTH == 0)
|
||||
NazaraWarning("Ill-formed DDS file, doesn't have a width flag");
|
||||
|
||||
unsigned int width = std::max(header.width, 1U);
|
||||
|
||||
unsigned int height = 1U;
|
||||
if (header.flags & DDSD_HEIGHT)
|
||||
height = std::max(header.height, 1U);
|
||||
|
||||
unsigned int depth = 1U;
|
||||
if (header.flags & DDSD_DEPTH)
|
||||
depth = std::max(header.depth, 1U);
|
||||
|
||||
unsigned int levelCount = (parameters.levelCount > 0) ? std::min(parameters.levelCount, static_cast<UInt8>(header.levelCount)) : header.levelCount;
|
||||
|
||||
// First, identify the type
|
||||
ImageType type;
|
||||
if (!IdentifyImageType(header, headerDX10, &type))
|
||||
return false;
|
||||
|
||||
// Then the 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)
|
||||
image->Convert(parameters.loadFormat);
|
||||
|
||||
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;
|
||||
}
|
||||
else
|
||||
{
|
||||
headerDX10.arraySize = 1;
|
||||
headerDX10.dxgiFormat = DXGI_FORMAT_UNKNOWN;
|
||||
headerDX10.miscFlag = 0;
|
||||
headerDX10.resourceDimension = D3D10_RESOURCE_DIMENSION_UNKNOWN;
|
||||
}
|
||||
|
||||
#ifdef NAZARA_BIG_ENDIAN
|
||||
// Les fichiers DDS sont en little endian
|
||||
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
|
||||
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));
|
||||
ByteSwap(&header.ddsCaps2, sizeof(UInt32));
|
||||
ByteSwap(&header.ddsCaps3, sizeof(UInt32));
|
||||
ByteSwap(&header.ddsCaps4, sizeof(UInt32));
|
||||
#endif
|
||||
|
||||
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;
|
||||
|
||||
// Détermination du type
|
||||
ImageType type;
|
||||
if (header.ddsCaps2 & DDSCAPS2_CUBEMAP)
|
||||
type = ImageType_Cubemap;
|
||||
else if (header.ddsCaps2 & DDSCAPS2_VOLUME)
|
||||
type = ImageType_3D;
|
||||
|
||||
// Détermination du format
|
||||
PixelFormatType format;
|
||||
|
||||
if (parameters.loadFormat != PixelFormatType_Undefined)
|
||||
image->Convert(parameters.loadFormat);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user