Added the ability to use ASTC compressed textures, when the system supports them.

This commit is contained in:
Alex Szpakowski
2015-11-28 14:55:26 -04:00
parent 7265f891de
commit 77998c707d
10 changed files with 567 additions and 139 deletions
+166
View File
@@ -0,0 +1,166 @@
/**
* Copyright (c) 2006-2015 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
// LOVE
#include "ASTCHandler.h"
#include "common/int.h"
namespace love
{
namespace image
{
namespace magpie
{
namespace
{
static const uint32 ASTC_IDENTIFIER = 0x5CA1AB13;
#pragma pack(push, 1)
struct ASTCHeader
{
uint8 identifier[4];
uint8 blockdimX;
uint8 blockdimY;
uint8 blockdimZ;
uint8 sizeX[3];
uint8 sizeY[3];
uint8 sizeZ[3];
};
#pragma pack(pop)
static CompressedImageData::Format convertFormat(uint32 blockX, uint32 blockY, uint32 blockZ)
{
if (blockZ > 1)
return CompressedImageData::FORMAT_UNKNOWN;
if (blockX == 4 && blockY == 4)
return CompressedImageData::FORMAT_ASTC_4x4;
else if (blockX == 5 && blockY == 4)
return CompressedImageData::FORMAT_ASTC_5x4;
else if (blockX == 5 && blockY == 5)
return CompressedImageData::FORMAT_ASTC_5x5;
else if (blockX == 6 && blockY == 5)
return CompressedImageData::FORMAT_ASTC_6x5;
else if (blockX == 6 && blockY == 6)
return CompressedImageData::FORMAT_ASTC_6x6;
else if (blockX == 8 && blockY == 5)
return CompressedImageData::FORMAT_ASTC_8x5;
else if (blockX == 8 && blockY == 6)
return CompressedImageData::FORMAT_ASTC_8x6;
else if (blockX == 8 && blockY == 8)
return CompressedImageData::FORMAT_ASTC_8x8;
else if (blockX == 10 && blockY == 5)
return CompressedImageData::FORMAT_ASTC_10x5;
else if (blockX == 10 && blockY == 6)
return CompressedImageData::FORMAT_ASTC_10x6;
else if (blockX == 10 && blockY == 8)
return CompressedImageData::FORMAT_ASTC_10x8;
else if (blockX == 10 && blockY == 10)
return CompressedImageData::FORMAT_ASTC_10x10;
else if (blockX == 12 && blockY == 10)
return CompressedImageData::FORMAT_ASTC_12x10;
else if (blockX == 12 && blockY == 12)
return CompressedImageData::FORMAT_ASTC_12x12;
return CompressedImageData::FORMAT_UNKNOWN;
}
} // Anonymous namespace.
bool ASTCHandler::canParse(const filesystem::FileData *data)
{
if (data->getSize() <= sizeof(ASTCHeader))
return false;
const ASTCHeader *header = (const ASTCHeader *) data->getData();
uint32 identifier = (uint32) header->identifier[0]
+ ((uint32) header->identifier[1] << 8)
+ ((uint32) header->identifier[2] << 16)
+ ((uint32) header->identifier[3] << 24);
if (identifier != ASTC_IDENTIFIER)
return false;
return true;
}
uint8 *ASTCHandler::parse(filesystem::FileData *filedata, std::vector<CompressedImageData::SubImage> &images, size_t &dataSize, CompressedImageData::Format &format, bool &sRGB)
{
if (!canParse(filedata))
throw love::Exception("Could not decode compressed data (not an .astc file?)");
ASTCHeader header = *(const ASTCHeader *) filedata->getData();
CompressedImageData::Format cformat = convertFormat(header.blockdimX, header.blockdimY, header.blockdimZ);
if (cformat == CompressedImageData::FORMAT_UNKNOWN)
throw love::Exception("Could not parse .astc file: unsupported ASTC format %dx%dx%d.", header.blockdimX, header.blockdimY, header.blockdimZ);
uint32 sizeX = header.sizeX[0] + (header.sizeX[1] << 8) + (header.sizeX[2] << 16);
uint32 sizeY = header.sizeY[0] + (header.sizeY[1] << 8) + (header.sizeY[2] << 16);
uint32 sizeZ = header.sizeZ[0] + (header.sizeZ[1] << 8) + (header.sizeZ[2] << 16);
uint32 blocksX = (sizeX + header.blockdimX - 1) / header.blockdimX;
uint32 blocksY = (sizeY + header.blockdimY - 1) / header.blockdimY;
uint32 blocksZ = (sizeZ + header.blockdimZ - 1) / header.blockdimZ;
size_t totalsize = blocksX * blocksY * blocksZ * 16;
if (totalsize + sizeof(header) > filedata->getSize())
throw love::Exception("Could not parse .astc file: file is too small.");
uint8 *data = nullptr;
try
{
data = new uint8[totalsize];
}
catch (std::bad_alloc &)
{
throw love::Exception("Out of memory.");
}
// .astc files only store a single mipmap level.
memcpy(data, (uint8 *) filedata->getData() + sizeof(ASTCHeader), totalsize);
CompressedImageData::SubImage mip;
mip.width = sizeX;
mip.height = sizeY;
mip.size = totalsize;
mip.data = data;
images.push_back(mip);
dataSize = totalsize;
format = cformat;
sRGB = false;
return data;
}
} // magpie
} // image
} // love
+54
View File
@@ -0,0 +1,54 @@
/**
* Copyright (c) 2006-2015 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_IMAGE_MAGPIE_ASTC_HANDLER_H
#define LOVE_IMAGE_MAGPIE_ASTC_HANDLER_H
#include "common/config.h"
#include "CompressedFormatHandler.h"
namespace love
{
namespace image
{
namespace magpie
{
/**
* Handles simple .astc files (generated by ARM's astcenc tool) with compressed
* ASTC data inside.
**/
class ASTCHandler : public CompressedFormatHandler
{
public:
virtual ~ASTCHandler() {}
// Implements CompressedFormatHandler.
virtual bool canParse(const filesystem::FileData *data);
virtual uint8 *parse(filesystem::FileData *filedata, std::vector<CompressedImageData::SubImage> &images, size_t &dataSize, CompressedImageData::Format &format, bool &sRGB);
}; // ASTCHandler
} // magpie
} // image
} // love
#endif // LOVE_IMAGE_MAGPIE_ASTC_HANDLER_H
+2
View File
@@ -30,6 +30,7 @@
#include "PVRHandler.h"
#include "KTXHandler.h"
#include "PKMHandler.h"
#include "ASTCHandler.h"
namespace love
{
@@ -47,6 +48,7 @@ Image::Image()
compressedFormatHandlers.push_back(new PVRHandler);
compressedFormatHandlers.push_back(new KTXHandler);
compressedFormatHandlers.push_back(new PKMHandler);
compressedFormatHandlers.push_back(new ASTCHandler);
}
Image::~Image()
+88 -1
View File
@@ -87,13 +87,44 @@ enum KTXGLInternalFormat
// Same with DXT1/3/5.
KTX_GL_COMPRESSED_RGB_S3TC_DXT1_EXT = 0x83F0,
KTX_GL_COMPRESSED_RGBA_S3TC_DXT3_EXT = 0x83F2,
KTX_GL_COMPRESSED_RGBA_S3TC_DXT5_EXT = 0x83F3
KTX_GL_COMPRESSED_RGBA_S3TC_DXT5_EXT = 0x83F3,
KTX_GL_COMPRESSED_RGBA_ASTC_4x4_KHR = 0x93B0,
KTX_GL_COMPRESSED_RGBA_ASTC_5x4_KHR = 0x93B1,
KTX_GL_COMPRESSED_RGBA_ASTC_5x5_KHR = 0x93B2,
KTX_GL_COMPRESSED_RGBA_ASTC_6x5_KHR = 0x93B3,
KTX_GL_COMPRESSED_RGBA_ASTC_6x6_KHR = 0x93B4,
KTX_GL_COMPRESSED_RGBA_ASTC_8x5_KHR = 0x93B5,
KTX_GL_COMPRESSED_RGBA_ASTC_8x6_KHR = 0x93B6,
KTX_GL_COMPRESSED_RGBA_ASTC_8x8_KHR = 0x93B7,
KTX_GL_COMPRESSED_RGBA_ASTC_10x5_KHR = 0x93B8,
KTX_GL_COMPRESSED_RGBA_ASTC_10x6_KHR = 0x93B9,
KTX_GL_COMPRESSED_RGBA_ASTC_10x8_KHR = 0x93BA,
KTX_GL_COMPRESSED_RGBA_ASTC_10x10_KHR = 0x93BB,
KTX_GL_COMPRESSED_RGBA_ASTC_12x10_KHR = 0x93BC,
KTX_GL_COMPRESSED_RGBA_ASTC_12x12_KHR = 0x93BD,
KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR = 0x93D0,
KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR = 0x93D1,
KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR = 0x93D2,
KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR = 0x93D3,
KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR = 0x93D4,
KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR = 0x93D5,
KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR = 0x93D6,
KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR = 0x93D7,
KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR = 0x93D8,
KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR = 0x93D9,
KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR = 0x93DA,
KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR = 0x93DB,
KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR = 0x93DC,
KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR = 0x93DD
};
CompressedImageData::Format convertFormat(uint32 glformat, bool &sRGB)
{
sRGB = false;
// hnnngg ASTC...
switch (glformat)
{
case KTX_GL_ETC1_RGB8_OES:
@@ -135,6 +166,62 @@ CompressedImageData::Format convertFormat(uint32 glformat, bool &sRGB)
return CompressedImageData::FORMAT_DXT3;
case KTX_GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
return CompressedImageData::FORMAT_DXT5;
case KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
sRGB = true;
case KTX_GL_COMPRESSED_RGBA_ASTC_4x4_KHR:
return CompressedImageData::FORMAT_ASTC_4x4;
case KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
sRGB = true;
case KTX_GL_COMPRESSED_RGBA_ASTC_5x4_KHR:
return CompressedImageData::FORMAT_ASTC_5x4;
case KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
sRGB = true;
case KTX_GL_COMPRESSED_RGBA_ASTC_5x5_KHR:
return CompressedImageData::FORMAT_ASTC_5x5;
case KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
sRGB = true;
case KTX_GL_COMPRESSED_RGBA_ASTC_6x5_KHR:
return CompressedImageData::FORMAT_ASTC_6x5;
case KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
sRGB = true;
case KTX_GL_COMPRESSED_RGBA_ASTC_6x6_KHR:
return CompressedImageData::FORMAT_ASTC_6x6;
case KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
sRGB = true;
case KTX_GL_COMPRESSED_RGBA_ASTC_8x5_KHR:
return CompressedImageData::FORMAT_ASTC_8x5;
case KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
sRGB = true;
case KTX_GL_COMPRESSED_RGBA_ASTC_8x6_KHR:
return CompressedImageData::FORMAT_ASTC_8x6;
case KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
sRGB = true;
case KTX_GL_COMPRESSED_RGBA_ASTC_8x8_KHR:
return CompressedImageData::FORMAT_ASTC_8x8;
case KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
sRGB = true;
case KTX_GL_COMPRESSED_RGBA_ASTC_10x5_KHR:
return CompressedImageData::FORMAT_ASTC_10x5;
case KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
sRGB = true;
case KTX_GL_COMPRESSED_RGBA_ASTC_10x6_KHR:
return CompressedImageData::FORMAT_ASTC_10x6;
case KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
sRGB = true;
case KTX_GL_COMPRESSED_RGBA_ASTC_10x8_KHR:
return CompressedImageData::FORMAT_ASTC_10x8;
case KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
sRGB = true;
case KTX_GL_COMPRESSED_RGBA_ASTC_10x10_KHR:
return CompressedImageData::FORMAT_ASTC_10x10;
case KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
sRGB = true;
case KTX_GL_COMPRESSED_RGBA_ASTC_12x10_KHR:
return CompressedImageData::FORMAT_ASTC_12x10;
case KTX_GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
sRGB = true;
case KTX_GL_COMPRESSED_RGBA_ASTC_12x12_KHR:
return CompressedImageData::FORMAT_ASTC_12x12;
default:
return CompressedImageData::FORMAT_UNKNOWN;
}
+101 -25
View File
@@ -237,20 +237,11 @@ static CompressedImageData::Format convertFormat(PVRV3PixelFormat format, PVRV3C
case ePVRTPF_DXT5:
return CompressedImageData::FORMAT_DXT5;
case ePVRTPF_BC4:
if (snorm)
return CompressedImageData::FORMAT_BC4s;
else
return CompressedImageData::FORMAT_BC4;
return snorm ? CompressedImageData::FORMAT_BC4s : CompressedImageData::FORMAT_BC4;
case ePVRTPF_BC5:
if (snorm)
return CompressedImageData::FORMAT_BC5s;
else
return CompressedImageData::FORMAT_BC5;
return snorm ? CompressedImageData::FORMAT_BC5s : CompressedImageData::FORMAT_BC5;
case ePVRTPF_BC6:
if (snorm)
return CompressedImageData::FORMAT_BC6Hs;
else
return CompressedImageData::FORMAT_BC6H;
return snorm ? CompressedImageData::FORMAT_BC6Hs : CompressedImageData::FORMAT_BC6H;
case ePVRTPF_BC7:
return CompressedImageData::FORMAT_BC7;
case ePVRTPF_ETC2_RGB:
@@ -260,15 +251,37 @@ static CompressedImageData::Format convertFormat(PVRV3PixelFormat format, PVRV3C
case ePVRTPF_ETC2_RGBA1:
return CompressedImageData::FORMAT_ETC2_RGBA1;
case ePVRTPF_EAC_R:
if (snorm)
return CompressedImageData::FORMAT_EAC_Rs;
else
return CompressedImageData::FORMAT_EAC_R;
return snorm ? CompressedImageData::FORMAT_EAC_Rs : CompressedImageData::FORMAT_EAC_R;
case ePVRTPF_EAC_RG:
if (snorm)
return CompressedImageData::FORMAT_EAC_RGs;
else
return CompressedImageData::FORMAT_EAC_RG;
return snorm ? CompressedImageData::FORMAT_EAC_RGs : CompressedImageData::FORMAT_EAC_RG;
case ePVRTPF_ASTC_4x4:
return CompressedImageData::FORMAT_ASTC_4x4;
case ePVRTPF_ASTC_5x4:
return CompressedImageData::FORMAT_ASTC_5x4;
case ePVRTPF_ASTC_5x5:
return CompressedImageData::FORMAT_ASTC_5x5;
case ePVRTPF_ASTC_6x5:
return CompressedImageData::FORMAT_ASTC_6x5;
case ePVRTPF_ASTC_6x6:
return CompressedImageData::FORMAT_ASTC_6x6;
case ePVRTPF_ASTC_8x5:
return CompressedImageData::FORMAT_ASTC_8x5;
case ePVRTPF_ASTC_8x6:
return CompressedImageData::FORMAT_ASTC_8x6;
case ePVRTPF_ASTC_8x8:
return CompressedImageData::FORMAT_ASTC_8x8;
case ePVRTPF_ASTC_10x5:
return CompressedImageData::FORMAT_ASTC_10x5;
case ePVRTPF_ASTC_10x6:
return CompressedImageData::FORMAT_ASTC_10x6;
case ePVRTPF_ASTC_10x8:
return CompressedImageData::FORMAT_ASTC_10x8;
case ePVRTPF_ASTC_10x10:
return CompressedImageData::FORMAT_ASTC_10x10;
case ePVRTPF_ASTC_12x10:
return CompressedImageData::FORMAT_ASTC_12x10;
case ePVRTPF_ASTC_12x12:
return CompressedImageData::FORMAT_ASTC_12x12;
default:
return CompressedImageData::FORMAT_UNKNOWN;
}
@@ -314,8 +327,10 @@ int getBitsPerPixel(uint64 pixelformat)
}
}
void getFormatMinDimensions(uint64 pixelformat, int &minX, int &minY)
void getFormatMinDimensions(uint64 pixelformat, int &minX, int &minY, int &minZ)
{
minZ = 1;
switch (pixelformat)
{
case ePVRTPF_PVRTCI_2bpp_RGB:
@@ -351,6 +366,62 @@ void getFormatMinDimensions(uint64 pixelformat, int &minX, int &minY)
case ePVRTPF_EAC_RG:
minX = minY = 4;
break;
case ePVRTPF_ASTC_4x4:
minX = 4;
minY = 4;
break;
case ePVRTPF_ASTC_5x4:
minX = 5;
minY = 4;
break;
case ePVRTPF_ASTC_5x5:
minX = 5;
minY = 5;
break;
case ePVRTPF_ASTC_6x5:
minX = 6;
minY = 5;
break;
case ePVRTPF_ASTC_6x6:
minX = 6;
minY = 6;
break;
case ePVRTPF_ASTC_8x5:
minX = 8;
minY = 5;
break;
case ePVRTPF_ASTC_8x6:
minX = 8;
minY = 6;
break;
case ePVRTPF_ASTC_8x8:
minX = 8;
minY = 8;
break;
case ePVRTPF_ASTC_10x5:
minX = 10;
minY = 5;
break;
case ePVRTPF_ASTC_10x6:
minX = 10;
minY = 6;
break;
case ePVRTPF_ASTC_10x8:
minX = 10;
minY = 8;
break;
case ePVRTPF_ASTC_10x10:
minX = 10;
minY = 10;
break;
case ePVRTPF_ASTC_12x10:
minX = 12;
minY = 10;
break;
case ePVRTPF_ASTC_12x12:
minX = 12;
minY = 12;
break;
default: // We don't handle all possible formats, but that's fine.
minX = minY = 1;
break;
@@ -361,17 +432,22 @@ size_t getMipLevelSize(const PVRTexHeaderV3 &header, int miplevel)
{
int smallestwidth = 1;
int smallestheight = 1;
getFormatMinDimensions(header.pixelFormat, smallestwidth, smallestheight);
int smallestdepth = 1;
getFormatMinDimensions(header.pixelFormat, smallestwidth, smallestheight, smallestdepth);
int width = std::max((int) header.width >> miplevel, 1);
int height = std::max((int) header.height >> miplevel, 1);
int depth = std::max((int) header.depth >> miplevel, 1);
// Pad the dimensions.
width += (-width) % smallestwidth;
height += (-height) % smallestheight;
width = ((width + smallestwidth - 1) / smallestwidth) * smallestwidth;
height = ((height + smallestheight - 1) / smallestheight) * smallestheight;
depth = ((depth + smallestdepth - 1) / smallestdepth) * smallestdepth;
return getBitsPerPixel(header.pixelFormat) * width * height * depth / 8;
if (header.pixelFormat >= ePVRTPF_ASTC_4x4 && header.pixelFormat <= ePVRTPF_ASTC_12x12)
return (width / smallestwidth) * (height / smallestheight) * (depth / smallestdepth) * (128 / 8);
else
return getBitsPerPixel(header.pixelFormat) * width * height * depth / 8;
}
} // Anonymous namespace.