mirror of
https://github.com/yuzu-emu/FasTC
synced 2024-11-25 15:58:56 +00:00
Use stb dxt instead of 'fast' intel one
This commit is contained in:
parent
964aa4ab8a
commit
2b93a5e16e
3 changed files with 647 additions and 827 deletions
292
DXTEncoder/src/Compressor.cpp
Executable file → Normal file
292
DXTEncoder/src/Compressor.cpp
Executable file → Normal file
|
@ -1,36 +1,27 @@
|
|||
/*
|
||||
This code is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This code is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
*/
|
||||
|
||||
// Refer to "Real-Time DXT Compression" by J.M.P. van Waveren for a more thorough discussion of the
|
||||
// algorithms used in this code.
|
||||
|
||||
#include "FasTC/DXTCompressor.h"
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
#define INSET_SHIFT 4 // Inset the bounding box with (range >> shift).
|
||||
#define C565_5_MASK 0xF8 // 0xFF minus last three bits
|
||||
#define C565_6_MASK 0xFC // 0xFF minus last two bits
|
||||
#define STB_DXT_IMPLEMENTATION
|
||||
#include "stb_dxt.h"
|
||||
|
||||
namespace DXTC
|
||||
{
|
||||
// Function prototypes
|
||||
void ExtractBlock(const uint32* inPtr, uint32 width, uint8* colorBlock);
|
||||
void GetMinMaxColors(const uint8* colorBlock, uint8* minColor, uint8* maxColor);
|
||||
void GetMinMaxColorsWithAlpha(const uint8* colorBlock, uint8* minColor, uint8* maxColor);
|
||||
void EmitColorIndices(const uint8* colorBlock, uint8*& outBuf, const uint8* minColor, const uint8* maxColor);
|
||||
void EmitAlphaIndices(const uint8* colorBlock, uint8*& outBuf, const uint8 minAlpha, const uint8 maxAlpha);
|
||||
|
||||
// Extract a 4 by 4 block of pixels from inPtr and store it in colorBlock. The width parameter
|
||||
// specifies the size of the image in pixels.
|
||||
void ExtractBlock(const uint32* inPtr, uint32 width, uint8* colorBlock)
|
||||
{
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
memcpy(&colorBlock[j * 4 * 4], inPtr, 4 * 4);
|
||||
inPtr += width;
|
||||
}
|
||||
}
|
||||
|
||||
// Compress an image using DXT1 compression. Use the inBuf parameter to point to an image in
|
||||
// 4-byte RGBA format. The width and height parameters specify the size of the image in pixels.
|
||||
|
@ -38,8 +29,6 @@ namespace DXTC
|
|||
// implementation has an 8:1 compression ratio.
|
||||
void CompressImageDXT1(const FasTC::CompressionJob &cj) {
|
||||
uint8 block[64];
|
||||
uint8 minColor[4];
|
||||
uint8 maxColor[4];
|
||||
|
||||
const uint32 kBlockSz = GetBlockSize(FasTC::eCompressionFormat_DXT1);
|
||||
const uint32 startBlock = cj.CoordsToBlockIdx(cj.XStart(), cj.YStart());
|
||||
|
@ -54,10 +43,8 @@ namespace DXTC
|
|||
|
||||
const uint32 kOffset = j*cj.Width() + i;
|
||||
ExtractBlock(inPixels + kOffset, cj.Width(), block);
|
||||
GetMinMaxColors(block, minColor, maxColor);
|
||||
EmitWord(outBuf, ColorTo565(maxColor));
|
||||
EmitWord(outBuf, ColorTo565(minColor));
|
||||
EmitColorIndices(block, outBuf, minColor, maxColor);
|
||||
stb_compress_dxt_block(outBuf, block, 0, STB_DXT_DITHER);
|
||||
outBuf += 8;
|
||||
}
|
||||
startX = 0;
|
||||
}
|
||||
|
@ -69,8 +56,6 @@ namespace DXTC
|
|||
// implementation has an 4:1 compression ratio.
|
||||
void CompressImageDXT5(const FasTC::CompressionJob &cj) {
|
||||
uint8 block[64];
|
||||
uint8 minColor[4];
|
||||
uint8 maxColor[4];
|
||||
|
||||
const uint32 kBlockSz = GetBlockSize(FasTC::eCompressionFormat_DXT5);
|
||||
const uint32 startBlock = cj.CoordsToBlockIdx(cj.XStart(), cj.YStart());
|
||||
|
@ -85,253 +70,10 @@ namespace DXTC
|
|||
|
||||
const uint32 kOffset = j*cj.Width() + i;
|
||||
ExtractBlock(inPixels + kOffset, cj.Width(), block);
|
||||
GetMinMaxColorsWithAlpha(block, minColor, maxColor);
|
||||
EmitByte(outBuf, maxColor[3]);
|
||||
EmitByte(outBuf, minColor[3]);
|
||||
EmitAlphaIndices(block, outBuf, minColor[3], maxColor[3]);
|
||||
EmitWord(outBuf, ColorTo565(maxColor));
|
||||
EmitWord(outBuf, ColorTo565(minColor));
|
||||
EmitColorIndices(block, outBuf, minColor, maxColor);
|
||||
stb_compress_dxt_block(outBuf, block, 1, STB_DXT_DITHER);
|
||||
outBuf += 8;
|
||||
}
|
||||
startX = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Convert a color in 24-bit RGB888 format to 16-bit RGB565 format.
|
||||
uint16 ColorTo565(const uint8* color)
|
||||
{
|
||||
return ((color[0] >> 3) << 11) | ((color[1] >> 2) << 5) | (color[2] >> 3);
|
||||
}
|
||||
|
||||
// Write a single byte to dest.
|
||||
void EmitByte(uint8*& dest, uint8 b)
|
||||
{
|
||||
dest[0] = b;
|
||||
dest += 1;
|
||||
}
|
||||
|
||||
// Write a word to dest.
|
||||
void EmitWord(uint8*& dest, uint16 s)
|
||||
{
|
||||
dest[0] = (s >> 0) & 255;
|
||||
dest[1] = (s >> 8) & 255;
|
||||
dest += 2;
|
||||
}
|
||||
|
||||
// Write a double word to dest.
|
||||
void EmitDoubleWord(uint8*& dest, uint32 i)
|
||||
{
|
||||
dest[0] = (i >> 0) & 255;
|
||||
dest[1] = (i >> 8) & 255;
|
||||
dest[2] = (i >> 16) & 255;
|
||||
dest[3] = (i >> 24) & 255;
|
||||
dest += 4;
|
||||
}
|
||||
|
||||
// Extract a 4 by 4 block of pixels from inPtr and store it in colorBlock. The width parameter
|
||||
// specifies the size of the image in pixels.
|
||||
void ExtractBlock(const uint32* inPtr, uint32 width, uint8* colorBlock)
|
||||
{
|
||||
for(int j = 0; j < 4; j++)
|
||||
{
|
||||
memcpy(&colorBlock[j * 4 * 4], inPtr, 4 * 4);
|
||||
inPtr += width;
|
||||
}
|
||||
}
|
||||
|
||||
// Find a line of best fit through the color space of colorBlock. The line is approximated using
|
||||
// the extents of the bounding box of the color space. This function does not include the alpha
|
||||
// channel.
|
||||
void GetMinMaxColors(const uint8* colorBlock, uint8* minColor, uint8* maxColor)
|
||||
{
|
||||
uint32 i;
|
||||
uint8 inset[3];
|
||||
|
||||
minColor[0] = minColor[1] = minColor[2] = 255;
|
||||
maxColor[0] = maxColor[1] = maxColor[2] = 0;
|
||||
|
||||
// Find the bounding box (defined by minimum and maximum color).
|
||||
for(i = 0; i < 16; i++) {
|
||||
if(colorBlock[i * 4 + 0] < minColor[0]) {
|
||||
minColor[0] = colorBlock[i * 4 + 0];
|
||||
}
|
||||
if(colorBlock[i * 4 + 1] < minColor[1]) {
|
||||
minColor[1] = colorBlock[i * 4 + 1];
|
||||
}
|
||||
if(colorBlock[i * 4 + 2] < minColor[2]) {
|
||||
minColor[2] = colorBlock[i * 4 + 2];
|
||||
}
|
||||
if(colorBlock[i * 4 + 0] > maxColor[0]) {
|
||||
maxColor[0] = colorBlock[i * 4 + 0];
|
||||
}
|
||||
if(colorBlock[i * 4 + 1] > maxColor[1]) {
|
||||
maxColor[1] = colorBlock[i * 4 + 1];
|
||||
}
|
||||
if(colorBlock[i * 4 + 2] > maxColor[2]) {
|
||||
maxColor[2] = colorBlock[i * 4 + 2];
|
||||
}
|
||||
}
|
||||
|
||||
// Inset the bounding box by 1/16 of it's size. (i.e. shift right by 4).
|
||||
inset[0] = (maxColor[0] - minColor[0]) >> INSET_SHIFT;
|
||||
inset[1] = (maxColor[1] - minColor[1]) >> INSET_SHIFT;
|
||||
inset[2] = (maxColor[2] - minColor[2]) >> INSET_SHIFT;
|
||||
|
||||
// Clamp the inset bounding box to 255.
|
||||
minColor[0] = (minColor[0] + inset[0] <= 255) ? minColor[0] + inset[0] : 255;
|
||||
minColor[1] = (minColor[1] + inset[1] <= 255) ? minColor[1] + inset[1] : 255;
|
||||
minColor[2] = (minColor[2] + inset[2] <= 255) ? minColor[2] + inset[2] : 255;
|
||||
|
||||
// Clamp the inset bounding box to 0.
|
||||
maxColor[0] = (maxColor[0] >= inset[0]) ? maxColor[0] - inset[0] : 0;
|
||||
maxColor[1] = (maxColor[1] >= inset[1]) ? maxColor[1] - inset[1] : 0;
|
||||
maxColor[2] = (maxColor[2] >= inset[2]) ? maxColor[2] - inset[2] : 0;
|
||||
}
|
||||
|
||||
// Find a line of best fit through the color space of colorBlock. The line is approximated using
|
||||
// the extents of the bounding box of the color space. This function includes the alpha channel.
|
||||
void GetMinMaxColorsWithAlpha(const uint8* colorBlock, uint8* minColor, uint8* maxColor)
|
||||
{
|
||||
uint32 i;
|
||||
uint8 inset[4];
|
||||
|
||||
minColor[0] = minColor[1] = minColor[2] = minColor[3] = 255;
|
||||
maxColor[0] = maxColor[1] = maxColor[2] = maxColor[3] = 0;
|
||||
|
||||
// Find the bounding box (defined by minimum and maximum color).
|
||||
for(i = 0; i < 16; i++) {
|
||||
if(colorBlock[i * 4 + 0] < minColor[0]) {
|
||||
minColor[0] = colorBlock[i * 4 + 0];
|
||||
}
|
||||
if(colorBlock[i * 4 + 1] < minColor[1]) {
|
||||
minColor[1] = colorBlock[i * 4 + 1];
|
||||
}
|
||||
if(colorBlock[i * 4 + 2] < minColor[2]) {
|
||||
minColor[2] = colorBlock[i * 4 + 2];
|
||||
}
|
||||
if(colorBlock[i * 4 + 3] < minColor[3]) {
|
||||
minColor[3] = colorBlock[i * 4 + 3];
|
||||
}
|
||||
if(colorBlock[i * 4 + 0] > maxColor[0]) {
|
||||
maxColor[0] = colorBlock[i * 4 + 0];
|
||||
}
|
||||
if(colorBlock[i * 4 + 1] > maxColor[1]) {
|
||||
maxColor[1] = colorBlock[i * 4 + 1];
|
||||
}
|
||||
if(colorBlock[i * 4 + 2] > maxColor[2]) {
|
||||
maxColor[2] = colorBlock[i * 4 + 2];
|
||||
}
|
||||
if(colorBlock[i * 4 + 3] > maxColor[3]) {
|
||||
maxColor[3] = colorBlock[i * 4 + 3];
|
||||
}
|
||||
}
|
||||
|
||||
// Inset the bounding box by 1/16 of it's size. (i.e. shift right by 4).
|
||||
inset[0] = (maxColor[0] - minColor[0]) >> INSET_SHIFT;
|
||||
inset[1] = (maxColor[1] - minColor[1]) >> INSET_SHIFT;
|
||||
inset[2] = (maxColor[2] - minColor[2]) >> INSET_SHIFT;
|
||||
inset[3] = (maxColor[3] - minColor[3]) >> INSET_SHIFT;
|
||||
|
||||
// Clamp the inset bounding box to 255.
|
||||
minColor[0] = (minColor[0] + inset[0] <= 255) ? minColor[0] + inset[0] : 255;
|
||||
minColor[1] = (minColor[1] + inset[1] <= 255) ? minColor[1] + inset[1] : 255;
|
||||
minColor[2] = (minColor[2] + inset[2] <= 255) ? minColor[2] + inset[2] : 255;
|
||||
minColor[3] = (minColor[3] + inset[3] <= 255) ? minColor[3] + inset[3] : 255;
|
||||
|
||||
// Clamp the inset bounding box to 0.
|
||||
maxColor[0] = (maxColor[0] >= inset[0]) ? maxColor[0] - inset[0] : 0;
|
||||
maxColor[1] = (maxColor[1] >= inset[1]) ? maxColor[1] - inset[1] : 0;
|
||||
maxColor[2] = (maxColor[2] >= inset[2]) ? maxColor[2] - inset[2] : 0;
|
||||
maxColor[3] = (maxColor[3] >= inset[3]) ? maxColor[3] - inset[3] : 0;
|
||||
}
|
||||
|
||||
// Quantize the pixels of the colorBlock to 4 colors that lie on the line through the color space
|
||||
// of colorBlock. The paramaters minColor and maxColor approximate the line through the color
|
||||
// space. 32 bits (2 bits per pixel) are written to outBuf, which represent the indices of the 4
|
||||
// colors. This function does not include the alpha channel.
|
||||
void EmitColorIndices(const uint8* colorBlock, uint8*& outBuf, const uint8* minColor, const uint8* maxColor)
|
||||
{
|
||||
uint16 colors[4][4];
|
||||
uint32 result = 0;
|
||||
|
||||
colors[0][0] = (maxColor[0] & C565_5_MASK) | (maxColor[0] >> 5);
|
||||
colors[0][1] = (maxColor[1] & C565_6_MASK) | (maxColor[1] >> 6);
|
||||
colors[0][2] = (maxColor[2] & C565_5_MASK) | (maxColor[2] >> 5);
|
||||
colors[1][0] = (minColor[0] & C565_5_MASK) | (minColor[0] >> 5);
|
||||
colors[1][1] = (minColor[1] & C565_6_MASK) | (minColor[1] >> 6);
|
||||
colors[1][2] = (minColor[2] & C565_5_MASK) | (minColor[2] >> 5);
|
||||
colors[2][0] = (2 * colors[0][0] + 1 * colors[1][0]) / 3;
|
||||
colors[2][1] = (2 * colors[0][1] + 1 * colors[1][1]) / 3;
|
||||
colors[2][2] = (2 * colors[0][2] + 1 * colors[1][2]) / 3;
|
||||
colors[3][0] = (1 * colors[0][0] + 2 * colors[1][0]) / 3;
|
||||
colors[3][1] = (1 * colors[0][1] + 2 * colors[1][1]) / 3;
|
||||
colors[3][2] = (1 * colors[0][2] + 2 * colors[1][2]) / 3;
|
||||
|
||||
for(int i = 15; i >= 0; i--) {
|
||||
int32 c0 = colorBlock[i * 4 + 0];
|
||||
int32 c1 = colorBlock[i * 4 + 1];
|
||||
int32 c2 = colorBlock[i * 4 + 2];
|
||||
|
||||
int32 d0 = abs(colors[0][0] - c0) + abs(colors[0][1] - c1) + abs(colors[0][2] - c2);
|
||||
int32 d1 = abs(colors[1][0] - c0) + abs(colors[1][1] - c1) + abs(colors[1][2] - c2);
|
||||
int32 d2 = abs(colors[2][0] - c0) + abs(colors[2][1] - c1) + abs(colors[2][2] - c2);
|
||||
int32 d3 = abs(colors[3][0] - c0) + abs(colors[3][1] - c1) + abs(colors[3][2] - c2);
|
||||
|
||||
int32 b0 = d0 > d3;
|
||||
int32 b1 = d1 > d2;
|
||||
int32 b2 = d0 > d2;
|
||||
int32 b3 = d1 > d3;
|
||||
int32 b4 = d2 > d3;
|
||||
|
||||
int32 x0 = b1 & b2;
|
||||
int32 x1 = b0 & b3;
|
||||
int32 x2 = b0 & b4;
|
||||
|
||||
result |= (x2 | ((x0 | x1) << 1)) << (i << 1);
|
||||
}
|
||||
|
||||
EmitDoubleWord(outBuf, result);
|
||||
}
|
||||
|
||||
// Quantize the alpha channel of the pixels in colorBlock to 8 alpha values that are equally
|
||||
// spaced along the interval defined by minAlpha and maxAlpha. 48 bits (3 bits per alpha) are
|
||||
// written to outBuf, which represent the indices of the 8 alpha values.
|
||||
void EmitAlphaIndices(const uint8* colorBlock, uint8*& outBuf, const uint8 minAlpha, const uint8 maxAlpha)
|
||||
{
|
||||
assert(maxAlpha >= minAlpha);
|
||||
|
||||
uint8 indices[16];
|
||||
|
||||
uint8 mid = (maxAlpha - minAlpha) / (2 * 7);
|
||||
|
||||
uint8 ab1 = minAlpha + mid;
|
||||
uint8 ab2 = (6 * maxAlpha + 1 * minAlpha) / 7 + mid;
|
||||
uint8 ab3 = (5 * maxAlpha + 2 * minAlpha) / 7 + mid;
|
||||
uint8 ab4 = (4 * maxAlpha + 3 * minAlpha) / 7 + mid;
|
||||
uint8 ab5 = (3 * maxAlpha + 4 * minAlpha) / 7 + mid;
|
||||
uint8 ab6 = (2 * maxAlpha + 5 * minAlpha) / 7 + mid;
|
||||
uint8 ab7 = (1 * maxAlpha + 6 * minAlpha) / 7 + mid;
|
||||
|
||||
colorBlock += 3;
|
||||
|
||||
for(uint32 i = 0; i < 16; i++) {
|
||||
uint8 a = colorBlock[i * 4];
|
||||
int32 b1 = (a <= ab1);
|
||||
int32 b2 = (a <= ab2);
|
||||
int32 b3 = (a <= ab3);
|
||||
int32 b4 = (a <= ab4);
|
||||
int32 b5 = (a <= ab5);
|
||||
int32 b6 = (a <= ab6);
|
||||
int32 b7 = (a <= ab7);
|
||||
int32 index = (b1 + b2 + b3 + b4 + b5 + b6 + b7 + 1) & 7;
|
||||
indices[i] = index ^ (2 > index);
|
||||
}
|
||||
|
||||
EmitByte(outBuf, (indices[0] >> 0) | (indices[1] << 3) | (indices[2] << 6));
|
||||
EmitByte(outBuf, (indices[2] >> 2) | (indices[3] << 1) | (indices[4] << 4) | (indices[ 5] << 7));
|
||||
EmitByte(outBuf, (indices[5] >> 1) | (indices[6] << 2) | (indices[7] << 5));
|
||||
EmitByte(outBuf, (indices[8] >> 0) | (indices[9] << 3) | (indices[10] << 6));
|
||||
EmitByte(outBuf, (indices[10] >> 2) | (indices[11] << 1) | (indices[12] << 4) | (indices[13] << 7));
|
||||
EmitByte(outBuf, (indices[13] >> 1) | (indices[14] << 2) | (indices[15] << 5));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,552 +0,0 @@
|
|||
/*
|
||||
This code is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This code is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
*/
|
||||
|
||||
// Refer to "Real-Time DXT Compression" by J.M.P. van Waveren for a more thorough discussion of the
|
||||
// algorithms used in this code.
|
||||
|
||||
#include "DXTCompressorDLL.h"
|
||||
#include <cassert>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <smmintrin.h>
|
||||
|
||||
#define ALIGN16(x) __declspec(align(16)) x
|
||||
#define INSET_SHIFT 4 // Inset the bounding box with (range >> shift).
|
||||
#define C565_5_MASK 0xF8 // 0xFF minus last three bits
|
||||
#define C565_6_MASK 0xFC // 0xFF minus last two bits
|
||||
#define R_SHUFFLE_D( x, y, z, w ) (( (w) & 3 ) << 6 | ( (z) & 3 ) << 4 | ( (y) & 3 ) << 2 | ( (x) & 3 ))
|
||||
|
||||
namespace DXTC
|
||||
{
|
||||
// SSE2 Constants
|
||||
ALIGN16(static const BYTE SIMD_byte_0[16]) = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
||||
ALIGN16(static const BYTE SIMD_byte_1[16]) = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
|
||||
ALIGN16(static const BYTE SIMD_byte_2[16]) = { 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02 };
|
||||
ALIGN16(static const BYTE SIMD_byte_7[16]) = { 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07 };
|
||||
|
||||
ALIGN16(static const BYTE SIMD_byte_colorMask[16]) = { C565_5_MASK, C565_6_MASK, C565_5_MASK, 0x00, 0x00, 0x00, 0x00, 0x00, C565_5_MASK, C565_6_MASK, C565_5_MASK, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
||||
ALIGN16(static const WORD SIMD_word_0[8]) = { 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 };
|
||||
|
||||
ALIGN16(static const WORD SIMD_word_1[8]) = { 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001 };
|
||||
ALIGN16(static const WORD SIMD_word_2[8]) = { 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002 };
|
||||
ALIGN16(static const WORD SIMD_word_div_by_3[8]) = { (1 << 16) / 3 + 1, (1 << 16) / 3 + 1, (1 << 16) / 3 + 1, (1 << 16) / 3 + 1, (1 << 16) / 3 + 1, (1 << 16) / 3 + 1, (1 << 16) / 3 + 1, (1 << 16) / 3 + 1 };
|
||||
|
||||
ALIGN16(static const WORD SIMD_word_div_by_7[8]) = { (1 << 16) / 7 + 1, (1 << 16) / 7 + 1, (1 << 16) / 7 + 1, (1 << 16) / 7 + 1, (1 << 16) / 7 + 1, (1 << 16) / 7 + 1, (1 << 16) / 7 + 1, (1 << 16) / 7 + 1 };
|
||||
ALIGN16(static const WORD SIMD_word_div_by_14[8]) = { (1 << 16) / 14 + 1, (1 << 16) / 14 + 1, (1 << 16) / 14 + 1, (1 << 16) / 14 + 1, (1 << 16) / 14 + 1, (1 << 16) / 14 + 1, (1 << 16) / 14 + 1, (1 << 16) / 14 + 1 };
|
||||
|
||||
ALIGN16(static const WORD SIMD_word_scale66554400[8]) = { 6, 6, 5, 5, 4, 4, 0, 0 };
|
||||
ALIGN16(static const WORD SIMD_word_scale11223300[8]) = { 1, 1, 2, 2, 3, 3, 0, 0 };
|
||||
|
||||
ALIGN16(static const DWORD SIMD_dword_alpha_bit_mask0[4]) = { 7 << 0, 0, 7 << 0, 0 };
|
||||
ALIGN16(static const DWORD SIMD_dword_alpha_bit_mask1[4]) = { 7 << 3, 0, 7 << 3, 0 };
|
||||
ALIGN16(static const DWORD SIMD_dword_alpha_bit_mask2[4]) = { 7 << 6, 0, 7 << 6, 0 };
|
||||
ALIGN16(static const DWORD SIMD_dword_alpha_bit_mask3[4]) = { 7 << 9, 0, 7 << 9, 0 };
|
||||
ALIGN16(static const DWORD SIMD_dword_alpha_bit_mask4[4]) = { 7 << 12, 0, 7 << 12, 0 };
|
||||
ALIGN16(static const DWORD SIMD_dword_alpha_bit_mask5[4]) = { 7 << 15, 0, 7 << 15, 0 };
|
||||
ALIGN16(static const DWORD SIMD_dword_alpha_bit_mask6[4]) = { 7 << 18, 0, 7 << 18, 0 };
|
||||
ALIGN16(static const DWORD SIMD_dword_alpha_bit_mask7[4]) = { 7 << 21, 0, 7 << 21, 0 };
|
||||
|
||||
static void ExtractBlock(const BYTE* inPtr, int width, BYTE* colorBlock);
|
||||
static void GetMinMaxColors(const BYTE* colorBlock, BYTE* minColor, BYTE* maxColor);
|
||||
static void EmitColorIndices(const BYTE* colorBlock, BYTE*& outBuf, const BYTE* minColor, const BYTE* maxColor);
|
||||
static void EmitAlphaIndices(const BYTE* colorBlock, BYTE*& outBuf, const BYTE minAlpha, const BYTE maxAlpha);
|
||||
|
||||
// Compress an image using SSE2-optimized DXT1 compression. Use the inBuf parameter to point to an
|
||||
// image in 4-byte RGBA format. The address pointed to by inBuf must be 16-byte aligned. The width
|
||||
// and height parameters specify the size of the image in pixels. The buffer pointed to by outBuf
|
||||
// must be 16-byte aligned and should be large enough to store the compressed image. This
|
||||
// implementation has an 8:1 compression ratio.
|
||||
void CompressImageDXT1SSE2(const BYTE* inBuf, BYTE* outBuf, int width, int height)
|
||||
{
|
||||
ALIGN16(BYTE block[64]);
|
||||
ALIGN16(BYTE minColor[4]);
|
||||
ALIGN16(BYTE maxColor[4]);
|
||||
|
||||
for(int j = 0; j < height; j += 4, inBuf += width * 4 * 4)
|
||||
{
|
||||
for(int i = 0; i < width; i += 4)
|
||||
{
|
||||
ExtractBlock(inBuf + i * 4, width, block);
|
||||
GetMinMaxColors(block, minColor, maxColor);
|
||||
EmitWord(outBuf, ColorTo565(maxColor));
|
||||
EmitWord(outBuf, ColorTo565(minColor));
|
||||
EmitColorIndices(block, outBuf, minColor, maxColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compress an image using SSE2-optimized DXT5 compression. Use the inBuf parameter to point to an
|
||||
// image in 4-byte RGBA format. The address pointed to by inBuf must be 16-byte aligned. The width
|
||||
// and height parameters specify the size of the image in pixels. The buffer pointed to by outBuf
|
||||
// must be 16-byte aligned and should be large enough to store the compressed image. This
|
||||
// implementation has an 4:1 compression ratio.
|
||||
void CompressImageDXT5SSE2(const BYTE* inBuf, BYTE* outBuf, int width, int height)
|
||||
{
|
||||
ALIGN16(BYTE block[64]);
|
||||
ALIGN16(BYTE minColor[4]);
|
||||
ALIGN16(BYTE maxColor[4]);
|
||||
|
||||
for(int j = 0; j < height; j += 4, inBuf += width * 4 * 4)
|
||||
{
|
||||
for(int i = 0; i < width; i += 4)
|
||||
{
|
||||
ExtractBlock(inBuf + i * 4, width, block);
|
||||
GetMinMaxColors(block, minColor, maxColor);
|
||||
EmitByte(outBuf, maxColor[3]);
|
||||
EmitByte(outBuf, minColor[3]);
|
||||
EmitAlphaIndices(block, outBuf, minColor[3], maxColor[3]);
|
||||
EmitWord(outBuf, ColorTo565(maxColor));
|
||||
EmitWord(outBuf, ColorTo565(minColor));
|
||||
EmitColorIndices(block, outBuf, minColor, maxColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compress the blocks assigned to this task using SSE2-optimized DXT1 compression.
|
||||
VOID CompressImageDXT1SSE2Task(VOID* taskData, INT taskContext, UINT taskId, UINT taskCount)
|
||||
{
|
||||
const DXTTaskData* data = (const DXTTaskData*)taskData;
|
||||
|
||||
// Compress the block.
|
||||
ALIGN16(BYTE block[64]);
|
||||
ALIGN16(BYTE minColor[4]);
|
||||
ALIGN16(BYTE maxColor[4]);
|
||||
|
||||
// Interate over the block set.
|
||||
for (int blockOffset = 0; blockOffset < data->kBlocksPerTask; ++blockOffset)
|
||||
{
|
||||
// Check for out of bounds.
|
||||
const INT blockIndex = (INT)taskId * data->kBlocksPerTask + blockOffset;
|
||||
if(blockIndex >= data->numBlocks)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// Compute the offsets into the input and output buffers.
|
||||
const INT blockWidth = data->width / 4;
|
||||
const INT blockRow = blockIndex / blockWidth;
|
||||
const INT blockCol = blockIndex % blockWidth;
|
||||
const INT inOffset = blockRow * blockWidth * 4 * 4 * 4 + blockCol * 4 * 4;
|
||||
const INT outOffset = blockIndex * 8;
|
||||
const BYTE* inBuf = data->inBuf + inOffset;
|
||||
BYTE* outBuf = data->outBuf + outOffset;
|
||||
|
||||
ExtractBlock(inBuf, data->width, block);
|
||||
GetMinMaxColors(block, minColor, maxColor);
|
||||
EmitWord(outBuf, ColorTo565(maxColor));
|
||||
EmitWord(outBuf, ColorTo565(minColor));
|
||||
EmitColorIndices(block, outBuf, minColor, maxColor);
|
||||
}
|
||||
}
|
||||
|
||||
// Compress the blocks assigned to this task using SSE2-optimized DXT5 compression.
|
||||
VOID CompressImageDXT5SSE2Task(VOID* taskData, INT taskContext, UINT taskId, UINT taskCount)
|
||||
{
|
||||
const DXTTaskData* data = (const DXTTaskData*)taskData;
|
||||
|
||||
// Compress the block.
|
||||
ALIGN16(BYTE block[64]);
|
||||
ALIGN16(BYTE minColor[4]);
|
||||
ALIGN16(BYTE maxColor[4]);
|
||||
|
||||
// Interate over the block set.
|
||||
for (int blockOffset = 0; blockOffset < data->kBlocksPerTask; ++blockOffset)
|
||||
{
|
||||
// Check for out of bounds.
|
||||
const INT blockIndex = (INT)taskId * data->kBlocksPerTask + blockOffset;
|
||||
if(blockIndex >= data->numBlocks)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// Compute the offsets into the input and output buffers.
|
||||
const INT blockWidth = data->width / 4;
|
||||
const INT blockRow = blockIndex / blockWidth;
|
||||
const INT blockCol = blockIndex % blockWidth;
|
||||
const INT inOffset = blockRow * blockWidth * 4 * 4 * 4 + blockCol * 4 * 4;
|
||||
const INT outOffset = blockIndex * 16;
|
||||
const BYTE* inBuf = data->inBuf + inOffset;
|
||||
BYTE* outBuf = data->outBuf + outOffset;
|
||||
|
||||
ExtractBlock(inBuf, data->width, block);
|
||||
GetMinMaxColors(block, minColor, maxColor);
|
||||
EmitByte(outBuf, maxColor[3]);
|
||||
EmitByte(outBuf, minColor[3]);
|
||||
EmitAlphaIndices(block, outBuf, minColor[3], maxColor[3]);
|
||||
EmitWord(outBuf, ColorTo565(maxColor));
|
||||
EmitWord(outBuf, ColorTo565(minColor));
|
||||
EmitColorIndices(block, outBuf, minColor, maxColor);
|
||||
}
|
||||
}
|
||||
|
||||
// Extract a 4 by 4 block of pixels from inPtr and store it in colorBlock. The width parameter
|
||||
// specifies the size of the image in pixels.
|
||||
void ExtractBlock(const BYTE* inPtr, int width, BYTE* colorBlock)
|
||||
{
|
||||
// Compute the stride.
|
||||
const int stride = width * 4;
|
||||
|
||||
// Copy the first row of pixels from inPtr into colorBlock.
|
||||
_mm_store_si128((__m128i*)colorBlock, _mm_load_si128((__m128i*)inPtr));
|
||||
inPtr += stride;
|
||||
|
||||
// Copy the second row of pixels from inPtr into colorBlock.
|
||||
_mm_store_si128((__m128i*)(colorBlock + 16), _mm_load_si128((__m128i*)inPtr));
|
||||
inPtr += stride;
|
||||
|
||||
// Copy the third row of pixels from inPtr into colorBlock.
|
||||
_mm_store_si128((__m128i*)(colorBlock + 32), _mm_load_si128((__m128i*)inPtr));
|
||||
inPtr += stride;
|
||||
|
||||
// Copy the forth row of pixels from inPtr into colorBlock.
|
||||
_mm_store_si128((__m128i*)(colorBlock + 48), _mm_load_si128((__m128i*)inPtr));
|
||||
}
|
||||
|
||||
// Find a line of best fit through the color space of colorBlock. The line is approximated using
|
||||
// the extents of the bounding box of the color space. This function does not include the alpha
|
||||
// channel.
|
||||
void GetMinMaxColors(const BYTE* colorBlock, BYTE* minColor, BYTE* maxColor)
|
||||
{
|
||||
// Compute the min/max of each column of pixels.
|
||||
__m128i min = _mm_load_si128((__m128i*)colorBlock);
|
||||
__m128i max = _mm_load_si128((__m128i*)colorBlock);
|
||||
min = _mm_min_epu8(min, *((__m128i*)(colorBlock + 16)));
|
||||
max = _mm_max_epu8(max, *((__m128i*)(colorBlock + 16)));
|
||||
min = _mm_min_epu8(min, *((__m128i*)(colorBlock + 32)));
|
||||
max = _mm_max_epu8(max, *((__m128i*)(colorBlock + 32)));
|
||||
min = _mm_min_epu8(min, *((__m128i*)(colorBlock + 48)));
|
||||
max = _mm_max_epu8(max, *((__m128i*)(colorBlock + 48)));
|
||||
|
||||
// Compute the min/max of the 1st and 3rd DWORD and the 2nd and 4th DWORD.
|
||||
__m128i minShuf = _mm_shuffle_epi32(min, R_SHUFFLE_D(2, 3, 2, 3));
|
||||
__m128i maxShuf = _mm_shuffle_epi32(max, R_SHUFFLE_D(2, 3, 2, 3));
|
||||
min = _mm_min_epu8(min, minShuf);
|
||||
max = _mm_max_epu8(max, maxShuf);
|
||||
|
||||
// Compute the min/max of the 1st and 2nd DWORD.
|
||||
minShuf = _mm_shufflelo_epi16(min, R_SHUFFLE_D(2, 3, 2, 3));
|
||||
maxShuf = _mm_shufflelo_epi16(max, R_SHUFFLE_D(2, 3, 2, 3));
|
||||
min = _mm_min_epu8(min, minShuf);
|
||||
max = _mm_max_epu8(max, maxShuf);
|
||||
|
||||
// Compute the inset value.
|
||||
const __m128i zero = _mm_setzero_si128();
|
||||
min = _mm_unpacklo_epi8(min, zero);
|
||||
max = _mm_unpacklo_epi8(max, zero);
|
||||
__m128i inset = _mm_sub_epi16(max, min);
|
||||
inset = _mm_srli_epi16(inset, INSET_SHIFT);
|
||||
|
||||
// Inset the bounding box.
|
||||
min = _mm_add_epi16(min, inset);
|
||||
max = _mm_sub_epi16(max, inset);
|
||||
|
||||
// Store the bounding box.
|
||||
min = _mm_packus_epi16(min, min);
|
||||
max = _mm_packus_epi16(max, max);
|
||||
*((int*)minColor) = _mm_cvtsi128_si32(min);
|
||||
*((int*)maxColor) = _mm_cvtsi128_si32(max);
|
||||
}
|
||||
|
||||
// Quantize the pixels of the colorBlock to 4 colors that lie on the line through the color space
|
||||
// of colorBlock. The paramaters minColor and maxColor approximate the line through the color
|
||||
// space. 32 bits (2 bits per pixel) are written to outBuf, which represent the indices of the 4
|
||||
// colors. This function does not include the alpha channel.
|
||||
void EmitColorIndices(const BYTE* colorBlock, BYTE*& outBuf, const BYTE* minColor, const BYTE* maxColor)
|
||||
{
|
||||
const __m128i RGB565Mask = _mm_load_si128((__m128i*)SIMD_byte_colorMask);
|
||||
const __m128i zero = _mm_setzero_si128();
|
||||
|
||||
// Find 4 colors on the line through maxColor and minColor.
|
||||
// Compute color0 (maxColor).
|
||||
__m128i color0 = _mm_cvtsi32_si128(*((int*)maxColor));
|
||||
color0 = _mm_and_si128(color0, RGB565Mask);
|
||||
color0 = _mm_unpacklo_epi8(color0, zero);
|
||||
__m128i redBlue = _mm_shufflelo_epi16(color0, R_SHUFFLE_D(0, 3, 2, 3));
|
||||
__m128i green = _mm_shufflelo_epi16(color0, R_SHUFFLE_D(3, 1, 3, 3));
|
||||
redBlue = _mm_srli_epi16(redBlue, 5);
|
||||
green = _mm_srli_epi16(green, 6);
|
||||
color0 = _mm_or_si128(color0, redBlue);
|
||||
color0 = _mm_or_si128(color0, green);
|
||||
|
||||
// Compute color1 (minColor).
|
||||
__m128i color1 = _mm_cvtsi32_si128(*((int*)minColor));
|
||||
color1 = _mm_and_si128(color1, RGB565Mask);
|
||||
color1 = _mm_unpacklo_epi8(color1, zero);
|
||||
redBlue = _mm_shufflelo_epi16(color1, R_SHUFFLE_D(0, 3, 2, 3));
|
||||
green = _mm_shufflelo_epi16(color1, R_SHUFFLE_D(3, 1, 3, 3));
|
||||
redBlue = _mm_srli_epi16(redBlue, 5);
|
||||
green = _mm_srli_epi16(green, 6);
|
||||
color1 = _mm_or_si128(color1, redBlue);
|
||||
color1 = _mm_or_si128(color1, green);
|
||||
|
||||
// Compute and pack color3.
|
||||
__m128i color3 = _mm_add_epi16(color1, color1);
|
||||
color3 = _mm_add_epi16(color0, color3);
|
||||
color3 = _mm_mulhi_epi16(color3, *((__m128i*)SIMD_word_div_by_3));
|
||||
color3 = _mm_packus_epi16(color3, zero);
|
||||
color3 = _mm_shuffle_epi32(color3, R_SHUFFLE_D(0, 1, 0, 1));
|
||||
|
||||
// Compute and pack color2.
|
||||
__m128i color2 = _mm_add_epi16(color0, color0);
|
||||
color2 = _mm_add_epi16(color2, color1);
|
||||
color2 = _mm_mulhi_epi16(color2, *((__m128i*)SIMD_word_div_by_3));
|
||||
color2 = _mm_packus_epi16(color2, zero);
|
||||
color2 = _mm_shuffle_epi32(color2, R_SHUFFLE_D(0, 1, 0, 1));
|
||||
|
||||
// Pack color1.
|
||||
color1 = _mm_packus_epi16(color1, zero);
|
||||
color1 = _mm_shuffle_epi32(color1, R_SHUFFLE_D(0, 1, 0, 1));
|
||||
|
||||
// Pack color0.
|
||||
color0 = _mm_packus_epi16(color0, zero);
|
||||
color0 = _mm_shuffle_epi32(color0, R_SHUFFLE_D(0, 1, 0, 1));
|
||||
|
||||
// Assign a color index for each of the 16 colors in the colorblock.
|
||||
// This loop iterates twice (computes 8 indexes per iteration).
|
||||
__m128i result = zero;
|
||||
for(int i = 32; i >= 0; i -= 32)
|
||||
{
|
||||
// Load 4 colors.
|
||||
__m128i colorHi = _mm_loadl_epi64((__m128i*)(colorBlock + i));
|
||||
colorHi = _mm_shuffle_epi32(colorHi, R_SHUFFLE_D(0, 2, 1, 3));
|
||||
__m128i colorLo = _mm_loadl_epi64((__m128i*)(colorBlock + i + 8));
|
||||
colorLo = _mm_shuffle_epi32(colorLo, R_SHUFFLE_D(0, 2, 1, 3));
|
||||
|
||||
// Compute the sum of absolute differences for each color.
|
||||
__m128i dHi = _mm_sad_epu8(colorHi, color0);
|
||||
__m128i dLo = _mm_sad_epu8(colorLo, color0);
|
||||
__m128i d0 = _mm_packs_epi32(dHi, dLo);
|
||||
dHi = _mm_sad_epu8(colorHi, color1);
|
||||
dLo = _mm_sad_epu8(colorLo, color1);
|
||||
__m128i d1 = _mm_packs_epi32(dHi, dLo);
|
||||
dHi = _mm_sad_epu8(colorHi, color2);
|
||||
dLo = _mm_sad_epu8(colorLo, color2);
|
||||
__m128i d2 = _mm_packs_epi32(dHi, dLo);
|
||||
dHi = _mm_sad_epu8(colorHi, color3);
|
||||
dLo = _mm_sad_epu8(colorLo, color3);
|
||||
__m128i d3 = _mm_packs_epi32(dHi, dLo);
|
||||
|
||||
// Load 4 more colors.
|
||||
colorHi = _mm_loadl_epi64((__m128i*)(colorBlock + i + 16));
|
||||
colorHi = _mm_shuffle_epi32(colorHi, R_SHUFFLE_D(0, 2, 1, 3));
|
||||
colorLo = _mm_loadl_epi64((__m128i*)(colorBlock + i + 24));
|
||||
colorLo = _mm_shuffle_epi32(colorLo, R_SHUFFLE_D(0, 2, 1, 3));
|
||||
|
||||
// Compute the sum of absolute differences for each color. Pack result into previous 4 results.
|
||||
dHi = _mm_sad_epu8(colorHi, color0);
|
||||
dLo = _mm_sad_epu8(colorLo, color0);
|
||||
dLo = _mm_packs_epi32(dHi, dLo);
|
||||
d0 = _mm_packs_epi32(d0, dLo);
|
||||
dHi = _mm_sad_epu8(colorHi, color1);
|
||||
dLo = _mm_sad_epu8(colorLo, color1);
|
||||
dLo = _mm_packs_epi32(dHi, dLo);
|
||||
d1 = _mm_packs_epi32(d1, dLo);
|
||||
dHi = _mm_sad_epu8(colorHi, color2);
|
||||
dLo = _mm_sad_epu8(colorLo, color2);
|
||||
dLo = _mm_packs_epi32(dHi, dLo);
|
||||
d2 = _mm_packs_epi32(d2, dLo);
|
||||
dHi = _mm_sad_epu8(colorHi, color3);
|
||||
dLo = _mm_sad_epu8(colorLo, color3);
|
||||
dLo = _mm_packs_epi32(dHi, dLo);
|
||||
d3 = _mm_packs_epi32(d3, dLo);
|
||||
|
||||
// Compare the distances.
|
||||
__m128i b0 = _mm_cmpgt_epi16(d0, d3);
|
||||
__m128i b1 = _mm_cmpgt_epi16(d1, d2);
|
||||
__m128i b2 = _mm_cmpgt_epi16(d0, d2);
|
||||
__m128i b3 = _mm_cmpgt_epi16(d1, d3);
|
||||
__m128i b4 = _mm_cmpgt_epi16(d2, d3);
|
||||
|
||||
// Compute the color index.
|
||||
__m128i x0 = _mm_and_si128(b2, b1);
|
||||
__m128i x1 = _mm_and_si128(b3, b0);
|
||||
__m128i x2 = _mm_and_si128(b4, b0);
|
||||
__m128i indexBit0 = _mm_or_si128(x0, x1);
|
||||
indexBit0 = _mm_and_si128(indexBit0, *((__m128i*)SIMD_word_2));
|
||||
__m128i indexBit1 = _mm_and_si128(x2, *((__m128i*)SIMD_word_1));
|
||||
__m128i index = _mm_or_si128(indexBit1, indexBit0);
|
||||
|
||||
// Pack the index into the result.
|
||||
__m128i indexHi = _mm_shuffle_epi32(index, R_SHUFFLE_D(2, 3, 0, 1));
|
||||
indexHi = _mm_unpacklo_epi16(indexHi, *((__m128i*)SIMD_word_0));
|
||||
indexHi = _mm_slli_epi32(indexHi, 8);
|
||||
__m128i indexLo = _mm_unpacklo_epi16(index, *((__m128i*)SIMD_word_0));
|
||||
result = _mm_slli_epi32(result, 16);
|
||||
result = _mm_or_si128(result, indexHi);
|
||||
result = _mm_or_si128(result, indexLo);
|
||||
}
|
||||
|
||||
// Pack the 16 2-bit color indices into a single 32-bit value.
|
||||
__m128i result1 = _mm_shuffle_epi32(result, R_SHUFFLE_D(1, 2, 3, 0));
|
||||
__m128i result2 = _mm_shuffle_epi32(result, R_SHUFFLE_D(2, 3, 0, 1));
|
||||
__m128i result3 = _mm_shuffle_epi32(result, R_SHUFFLE_D(3, 0, 1, 2));
|
||||
result1 = _mm_slli_epi32(result1, 2);
|
||||
result2 = _mm_slli_epi32(result2, 4);
|
||||
result3 = _mm_slli_epi32(result3, 6);
|
||||
result = _mm_or_si128(result, result1);
|
||||
result = _mm_or_si128(result, result2);
|
||||
result = _mm_or_si128(result, result3);
|
||||
|
||||
// Store the result.
|
||||
*((int*)outBuf) = _mm_cvtsi128_si32(result);
|
||||
|
||||
outBuf += 4;
|
||||
}
|
||||
|
||||
// Quantize the alpha channel of the pixels in colorBlock to 8 alpha values that are equally
|
||||
// spaced along the interval defined by minAlpha and maxAlpha. 48 bits (3 bits per alpha) are
|
||||
// written to outBuf, which represent the indices of the 8 alpha values.
|
||||
void EmitAlphaIndices(const BYTE* colorBlock, BYTE*& outBuf, const BYTE minAlpha, const BYTE maxAlpha)
|
||||
{
|
||||
// Pack the alpha values of the first two rows of colorBlock.
|
||||
__m128i alpha1 = _mm_load_si128((__m128i*)colorBlock);
|
||||
alpha1 = _mm_srli_epi32(alpha1, 24);
|
||||
__m128i alpha2 = _mm_load_si128((__m128i*)(colorBlock + 16));
|
||||
alpha2 = _mm_srli_epi32(alpha2, 24);
|
||||
alpha1 = _mm_packus_epi16(alpha1, alpha2);
|
||||
|
||||
// Pack the alpha values of the last two rows of colorBlock.
|
||||
__m128i alpha3 = _mm_load_si128((__m128i*)(colorBlock + 32));
|
||||
alpha3 = _mm_srli_epi32(alpha3, 24);
|
||||
__m128i alpha4 = _mm_load_si128((__m128i*)(colorBlock + 48));
|
||||
alpha4 = _mm_srli_epi32(alpha4, 24);
|
||||
alpha3 = _mm_packus_epi16(alpha3, alpha4);
|
||||
|
||||
// Pack all 16 alpha values together.
|
||||
__m128i alpha = _mm_packus_epi16(alpha1, alpha3);
|
||||
|
||||
// Unpack the maximum alpha value.
|
||||
__m128i max = _mm_cvtsi32_si128(maxAlpha);
|
||||
max = _mm_shufflelo_epi16(max, R_SHUFFLE_D(0, 0, 0, 0));
|
||||
max = _mm_shuffle_epi32(max, R_SHUFFLE_D(0, 0, 0, 0));
|
||||
|
||||
// Unpack the minimum alpha value.
|
||||
__m128i min = _mm_cvtsi32_si128(minAlpha);
|
||||
min = _mm_shufflelo_epi16(min, R_SHUFFLE_D(0, 0, 0, 0));
|
||||
min = _mm_shuffle_epi32(min, R_SHUFFLE_D(0, 0, 0, 0));
|
||||
|
||||
// Compute the midpoint offset between any two interpolated alpha values.
|
||||
__m128i mid = _mm_sub_epi16(max, min);
|
||||
mid = _mm_mulhi_epi16(mid, *((__m128i*)SIMD_word_div_by_14));
|
||||
|
||||
// Compute the first midpoint.
|
||||
__m128i ab1 = min;
|
||||
ab1 = _mm_add_epi16(ab1, mid);
|
||||
ab1 = _mm_packus_epi16(ab1, ab1);
|
||||
|
||||
// Compute the next three midpoints.
|
||||
__m128i max456 = _mm_mullo_epi16(max, *((__m128i*)SIMD_word_scale66554400));
|
||||
__m128i min123 = _mm_mullo_epi16(min, *((__m128i*)SIMD_word_scale11223300));
|
||||
__m128i ab234 = _mm_add_epi16(max456, min123);
|
||||
ab234 = _mm_mulhi_epi16(ab234, *((__m128i*)SIMD_word_div_by_7));
|
||||
ab234 = _mm_add_epi16(ab234, mid);
|
||||
__m128i ab2 = _mm_shuffle_epi32(ab234, R_SHUFFLE_D(0, 0, 0, 0));
|
||||
ab2 = _mm_packus_epi16(ab2, ab2);
|
||||
__m128i ab3 = _mm_shuffle_epi32(ab234, R_SHUFFLE_D(1, 1, 1, 1));
|
||||
ab3 = _mm_packus_epi16(ab3, ab3);
|
||||
__m128i ab4 = _mm_shuffle_epi32(ab234, R_SHUFFLE_D(2, 2, 2, 2));
|
||||
ab4 = _mm_packus_epi16(ab4, ab4);
|
||||
|
||||
// Compute the last three midpoints.
|
||||
__m128i max123 = _mm_mullo_epi16(max, *((__m128i*)SIMD_word_scale11223300));
|
||||
__m128i min456 = _mm_mullo_epi16(min, *((__m128i*)SIMD_word_scale66554400));
|
||||
__m128i ab567 = _mm_add_epi16(max123, min456);
|
||||
ab567 = _mm_mulhi_epi16(ab567, *((__m128i*)SIMD_word_div_by_7));
|
||||
ab567 = _mm_add_epi16(ab567, mid);
|
||||
__m128i ab5 = _mm_shuffle_epi32(ab567, R_SHUFFLE_D(2, 2, 2, 2));
|
||||
ab5 = _mm_packus_epi16(ab5, ab5);
|
||||
__m128i ab6 = _mm_shuffle_epi32(ab567, R_SHUFFLE_D(1, 1, 1, 1));
|
||||
ab6 = _mm_packus_epi16(ab6, ab6);
|
||||
__m128i ab7 = _mm_shuffle_epi32(ab567, R_SHUFFLE_D(0, 0, 0, 0));
|
||||
ab7 = _mm_packus_epi16(ab7, ab7);
|
||||
|
||||
// Compare the alpha values to the midpoints.
|
||||
__m128i b1 = _mm_min_epu8(ab1, alpha);
|
||||
b1 = _mm_cmpeq_epi8(b1, alpha);
|
||||
b1 = _mm_and_si128(b1, *((__m128i*)SIMD_byte_1));
|
||||
__m128i b2 = _mm_min_epu8(ab2, alpha);
|
||||
b2 = _mm_cmpeq_epi8(b2, alpha);
|
||||
b2 = _mm_and_si128(b2, *((__m128i*)SIMD_byte_1));
|
||||
__m128i b3 = _mm_min_epu8(ab3, alpha);
|
||||
b3 = _mm_cmpeq_epi8(b3, alpha);
|
||||
b3 = _mm_and_si128(b3, *((__m128i*)SIMD_byte_1));
|
||||
__m128i b4 = _mm_min_epu8(ab4, alpha);
|
||||
b4 = _mm_cmpeq_epi8(b4, alpha);
|
||||
b4 = _mm_and_si128(b4, *((__m128i*)SIMD_byte_1));
|
||||
__m128i b5 = _mm_min_epu8(ab5, alpha);
|
||||
b5 = _mm_cmpeq_epi8(b5, alpha);
|
||||
b5 = _mm_and_si128(b5, *((__m128i*)SIMD_byte_1));
|
||||
__m128i b6 = _mm_min_epu8(ab6, alpha);
|
||||
b6 = _mm_cmpeq_epi8(b6, alpha);
|
||||
b6 = _mm_and_si128(b6, *((__m128i*)SIMD_byte_1));
|
||||
__m128i b7 = _mm_min_epu8(ab7, alpha);
|
||||
b7 = _mm_cmpeq_epi8(b7, alpha);
|
||||
b7 = _mm_and_si128(b7, *((__m128i*)SIMD_byte_1));
|
||||
|
||||
// Compute the alpha indexes.
|
||||
__m128i index = _mm_adds_epu8(b1, b2);
|
||||
index = _mm_adds_epu8(index, b3);
|
||||
index = _mm_adds_epu8(index, b4);
|
||||
index = _mm_adds_epu8(index, b5);
|
||||
index = _mm_adds_epu8(index, b6);
|
||||
index = _mm_adds_epu8(index, b7);
|
||||
|
||||
// Convert natural index ordering to DXT index ordering.
|
||||
__m128i byte1 = _mm_load_si128((__m128i*)SIMD_byte_1);
|
||||
index = _mm_adds_epu8(index, byte1);
|
||||
__m128i byte7 = _mm_load_si128((__m128i*)SIMD_byte_7);
|
||||
index = _mm_and_si128(index, byte7);
|
||||
__m128i byte2 = _mm_load_si128((__m128i*)SIMD_byte_2);
|
||||
__m128i swapMinMax = _mm_cmpgt_epi8(byte2, index);
|
||||
swapMinMax = _mm_and_si128(swapMinMax, byte1);
|
||||
index = _mm_xor_si128(index, swapMinMax);
|
||||
|
||||
// Pack the 16 3-bit indices into 6 bytes.
|
||||
__m128i alphaBitMask0 = _mm_load_si128((__m128i*)SIMD_dword_alpha_bit_mask0);
|
||||
__m128i index0 = _mm_and_si128(index, alphaBitMask0);
|
||||
__m128i index1 = _mm_srli_epi64(index, 8 - 3);
|
||||
__m128i alphaBitMask1 = _mm_load_si128((__m128i*)SIMD_dword_alpha_bit_mask1);
|
||||
index1 = _mm_and_si128(index1, alphaBitMask1);
|
||||
__m128i index2 = _mm_srli_epi64(index, 16 - 6);
|
||||
__m128i alphaBitMask2 = _mm_load_si128((__m128i*)SIMD_dword_alpha_bit_mask2);
|
||||
index2 = _mm_and_si128(index2, alphaBitMask2);
|
||||
__m128i index3 = _mm_srli_epi64(index, 24 - 9);
|
||||
__m128i alphaBitMask3 = _mm_load_si128((__m128i*)SIMD_dword_alpha_bit_mask3);
|
||||
index3 = _mm_and_si128(index3, alphaBitMask3);
|
||||
__m128i index4 = _mm_srli_epi64(index, 32 - 12);
|
||||
__m128i alphaBitMask4 = _mm_load_si128((__m128i*)SIMD_dword_alpha_bit_mask4);
|
||||
index4 = _mm_and_si128(index4, alphaBitMask4);
|
||||
__m128i index5 = _mm_srli_epi64(index, 40 - 15);
|
||||
__m128i alphaBitMask5 = _mm_load_si128((__m128i*)SIMD_dword_alpha_bit_mask5);
|
||||
index5 = _mm_and_si128(index5, alphaBitMask5);
|
||||
__m128i index6 = _mm_srli_epi64(index, 48 - 18);
|
||||
__m128i alphaBitMask6 = _mm_load_si128((__m128i*)SIMD_dword_alpha_bit_mask6);
|
||||
index6 = _mm_and_si128(index6, alphaBitMask6);
|
||||
__m128i index7 = _mm_srli_epi64(index, 56 - 21);
|
||||
__m128i alphaBitMask7 = _mm_load_si128((__m128i*)SIMD_dword_alpha_bit_mask7);
|
||||
index7 = _mm_and_si128(index7, alphaBitMask7);
|
||||
index = _mm_or_si128(index0, index1);
|
||||
index = _mm_or_si128(index, index2);
|
||||
index = _mm_or_si128(index, index3);
|
||||
index = _mm_or_si128(index, index4);
|
||||
index = _mm_or_si128(index, index5);
|
||||
index = _mm_or_si128(index, index6);
|
||||
index = _mm_or_si128(index, index7);
|
||||
|
||||
// Store the indexes.
|
||||
*((int*)outBuf) = _mm_cvtsi128_si32(index);
|
||||
index = _mm_shuffle_epi32(index, R_SHUFFLE_D(2, 3, 0, 1));
|
||||
*((int*)(outBuf + 3)) = _mm_cvtsi128_si32(index);
|
||||
|
||||
outBuf += 6;
|
||||
}
|
||||
}
|
630
DXTEncoder/src/stb_dxt.h
Normal file
630
DXTEncoder/src/stb_dxt.h
Normal file
|
@ -0,0 +1,630 @@
|
|||
// stb_dxt.h - v1.04 - DXT1/DXT5 compressor - public domain
|
||||
// original by fabian "ryg" giesen - ported to C by stb
|
||||
// use '#define STB_DXT_IMPLEMENTATION' before including to create the implementation
|
||||
//
|
||||
// USAGE:
|
||||
// call stb_compress_dxt_block() for every block (you must pad)
|
||||
// source should be a 4x4 block of RGBA data in row-major order;
|
||||
// A is ignored if you specify alpha=0; you can turn on dithering
|
||||
// and "high quality" using mode.
|
||||
//
|
||||
// version history:
|
||||
// v1.04 - (ryg) default to no rounding bias for lerped colors (as per S3TC/DX10 spec);
|
||||
// single color match fix (allow for inexact color interpolation);
|
||||
// optimal DXT5 index finder; "high quality" mode that runs multiple refinement steps.
|
||||
// v1.03 - (stb) endianness support
|
||||
// v1.02 - (stb) fix alpha encoding bug
|
||||
// v1.01 - (stb) fix bug converting to RGB that messed up quality, thanks ryg & cbloom
|
||||
// v1.00 - (stb) first release
|
||||
//
|
||||
// LICENSE
|
||||
//
|
||||
// This software is dual-licensed to the public domain and under the following
|
||||
// license: you are granted a perpetual, irrevocable license to copy, modify,
|
||||
// publish, and distribute this file as you see fit.
|
||||
|
||||
#ifndef STB_INCLUDE_STB_DXT_H
|
||||
#define STB_INCLUDE_STB_DXT_H
|
||||
|
||||
// compression mode (bitflags)
|
||||
#define STB_DXT_NORMAL 0
|
||||
#define STB_DXT_DITHER 1 // use dithering. dubious win. never use for normal maps and the like!
|
||||
#define STB_DXT_HIGHQUAL 2 // high quality mode, does two refinement steps instead of 1. ~30-40% slower.
|
||||
|
||||
void stb_compress_dxt_block(unsigned char *dest, const unsigned char *src, int alpha, int mode);
|
||||
#define STB_COMPRESS_DXT_BLOCK
|
||||
|
||||
#ifdef STB_DXT_IMPLEMENTATION
|
||||
|
||||
// configuration options for DXT encoder. set them in the project/makefile or just define
|
||||
// them at the top.
|
||||
|
||||
// STB_DXT_USE_ROUNDING_BIAS
|
||||
// use a rounding bias during color interpolation. this is closer to what "ideal"
|
||||
// interpolation would do but doesn't match the S3TC/DX10 spec. old versions (pre-1.03)
|
||||
// implicitly had this turned on.
|
||||
//
|
||||
// in case you're targeting a specific type of hardware (e.g. console programmers):
|
||||
// NVidia and Intel GPUs (as of 2010) as well as DX9 ref use DXT decoders that are closer
|
||||
// to STB_DXT_USE_ROUNDING_BIAS. AMD/ATI, S3 and DX10 ref are closer to rounding with no bias.
|
||||
// you also see "(a*5 + b*3) / 8" on some old GPU designs.
|
||||
// #define STB_DXT_USE_ROUNDING_BIAS
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <string.h> // memset
|
||||
|
||||
static unsigned char stb__Expand5[32];
|
||||
static unsigned char stb__Expand6[64];
|
||||
static unsigned char stb__OMatch5[256][2];
|
||||
static unsigned char stb__OMatch6[256][2];
|
||||
static unsigned char stb__QuantRBTab[256+16];
|
||||
static unsigned char stb__QuantGTab[256+16];
|
||||
|
||||
static int stb__Mul8Bit(int a, int b)
|
||||
{
|
||||
int t = a*b + 128;
|
||||
return (t + (t >> 8)) >> 8;
|
||||
}
|
||||
|
||||
static void stb__From16Bit(unsigned char *out, unsigned short v)
|
||||
{
|
||||
int rv = (v & 0xf800) >> 11;
|
||||
int gv = (v & 0x07e0) >> 5;
|
||||
int bv = (v & 0x001f) >> 0;
|
||||
|
||||
out[0] = stb__Expand5[rv];
|
||||
out[1] = stb__Expand6[gv];
|
||||
out[2] = stb__Expand5[bv];
|
||||
out[3] = 0;
|
||||
}
|
||||
|
||||
static unsigned short stb__As16Bit(int r, int g, int b)
|
||||
{
|
||||
return (stb__Mul8Bit(r,31) << 11) + (stb__Mul8Bit(g,63) << 5) + stb__Mul8Bit(b,31);
|
||||
}
|
||||
|
||||
// linear interpolation at 1/3 point between a and b, using desired rounding type
|
||||
static int stb__Lerp13(int a, int b)
|
||||
{
|
||||
#ifdef STB_DXT_USE_ROUNDING_BIAS
|
||||
// with rounding bias
|
||||
return a + stb__Mul8Bit(b-a, 0x55);
|
||||
#else
|
||||
// without rounding bias
|
||||
// replace "/ 3" by "* 0xaaab) >> 17" if your compiler sucks or you really need every ounce of speed.
|
||||
return (2*a + b) / 3;
|
||||
#endif
|
||||
}
|
||||
|
||||
// lerp RGB color
|
||||
static void stb__Lerp13RGB(unsigned char *out, unsigned char *p1, unsigned char *p2)
|
||||
{
|
||||
out[0] = stb__Lerp13(p1[0], p2[0]);
|
||||
out[1] = stb__Lerp13(p1[1], p2[1]);
|
||||
out[2] = stb__Lerp13(p1[2], p2[2]);
|
||||
}
|
||||
|
||||
/****************************************************************************/
|
||||
|
||||
// compute table to reproduce constant colors as accurately as possible
|
||||
static void stb__PrepareOptTable(unsigned char *Table,const unsigned char *expand,int size)
|
||||
{
|
||||
int i,mn,mx;
|
||||
for (i=0;i<256;i++) {
|
||||
int bestErr = 256;
|
||||
for (mn=0;mn<size;mn++) {
|
||||
for (mx=0;mx<size;mx++) {
|
||||
int mine = expand[mn];
|
||||
int maxe = expand[mx];
|
||||
int err = abs(stb__Lerp13(maxe, mine) - i);
|
||||
|
||||
// DX10 spec says that interpolation must be within 3% of "correct" result,
|
||||
// add this as error term. (normally we'd expect a random distribution of
|
||||
// +-1.5% error, but nowhere in the spec does it say that the error has to be
|
||||
// unbiased - better safe than sorry).
|
||||
err += abs(maxe - mine) * 3 / 100;
|
||||
|
||||
if(err < bestErr)
|
||||
{
|
||||
Table[i*2+0] = mx;
|
||||
Table[i*2+1] = mn;
|
||||
bestErr = err;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void stb__EvalColors(unsigned char *color,unsigned short c0,unsigned short c1)
|
||||
{
|
||||
stb__From16Bit(color+ 0, c0);
|
||||
stb__From16Bit(color+ 4, c1);
|
||||
stb__Lerp13RGB(color+ 8, color+0, color+4);
|
||||
stb__Lerp13RGB(color+12, color+4, color+0);
|
||||
}
|
||||
|
||||
// Block dithering function. Simply dithers a block to 565 RGB.
|
||||
// (Floyd-Steinberg)
|
||||
static void stb__DitherBlock(unsigned char *dest, unsigned char *block)
|
||||
{
|
||||
int err[8],*ep1 = err,*ep2 = err+4, *et;
|
||||
int ch,y;
|
||||
|
||||
// process channels seperately
|
||||
for (ch=0; ch<3; ++ch) {
|
||||
unsigned char *bp = block+ch, *dp = dest+ch;
|
||||
unsigned char *quant = (ch == 1) ? stb__QuantGTab+8 : stb__QuantRBTab+8;
|
||||
memset(err, 0, sizeof(err));
|
||||
for(y=0; y<4; ++y) {
|
||||
dp[ 0] = quant[bp[ 0] + ((3*ep2[1] + 5*ep2[0]) >> 4)];
|
||||
ep1[0] = bp[ 0] - dp[ 0];
|
||||
dp[ 4] = quant[bp[ 4] + ((7*ep1[0] + 3*ep2[2] + 5*ep2[1] + ep2[0]) >> 4)];
|
||||
ep1[1] = bp[ 4] - dp[ 4];
|
||||
dp[ 8] = quant[bp[ 8] + ((7*ep1[1] + 3*ep2[3] + 5*ep2[2] + ep2[1]) >> 4)];
|
||||
ep1[2] = bp[ 8] - dp[ 8];
|
||||
dp[12] = quant[bp[12] + ((7*ep1[2] + 5*ep2[3] + ep2[2]) >> 4)];
|
||||
ep1[3] = bp[12] - dp[12];
|
||||
bp += 16;
|
||||
dp += 16;
|
||||
et = ep1, ep1 = ep2, ep2 = et; // swap
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The color matching function
|
||||
static unsigned int stb__MatchColorsBlock(unsigned char *block, unsigned char *color,int dither)
|
||||
{
|
||||
unsigned int mask = 0;
|
||||
int dirr = color[0*4+0] - color[1*4+0];
|
||||
int dirg = color[0*4+1] - color[1*4+1];
|
||||
int dirb = color[0*4+2] - color[1*4+2];
|
||||
int dots[16];
|
||||
int stops[4];
|
||||
int i;
|
||||
int c0Point, halfPoint, c3Point;
|
||||
|
||||
for(i=0;i<16;i++)
|
||||
dots[i] = block[i*4+0]*dirr + block[i*4+1]*dirg + block[i*4+2]*dirb;
|
||||
|
||||
for(i=0;i<4;i++)
|
||||
stops[i] = color[i*4+0]*dirr + color[i*4+1]*dirg + color[i*4+2]*dirb;
|
||||
|
||||
// think of the colors as arranged on a line; project point onto that line, then choose
|
||||
// next color out of available ones. we compute the crossover points for "best color in top
|
||||
// half"/"best in bottom half" and then the same inside that subinterval.
|
||||
//
|
||||
// relying on this 1d approximation isn't always optimal in terms of euclidean distance,
|
||||
// but it's very close and a lot faster.
|
||||
// http://cbloomrants.blogspot.com/2008/12/12-08-08-dxtc-summary.html
|
||||
|
||||
c0Point = (stops[1] + stops[3]) >> 1;
|
||||
halfPoint = (stops[3] + stops[2]) >> 1;
|
||||
c3Point = (stops[2] + stops[0]) >> 1;
|
||||
|
||||
if(!dither) {
|
||||
// the version without dithering is straightforward
|
||||
for (i=15;i>=0;i--) {
|
||||
int dot = dots[i];
|
||||
mask <<= 2;
|
||||
|
||||
if(dot < halfPoint)
|
||||
mask |= (dot < c0Point) ? 1 : 3;
|
||||
else
|
||||
mask |= (dot < c3Point) ? 2 : 0;
|
||||
}
|
||||
} else {
|
||||
// with floyd-steinberg dithering
|
||||
int err[8],*ep1 = err,*ep2 = err+4;
|
||||
int *dp = dots, y;
|
||||
|
||||
c0Point <<= 4;
|
||||
halfPoint <<= 4;
|
||||
c3Point <<= 4;
|
||||
for(i=0;i<8;i++)
|
||||
err[i] = 0;
|
||||
|
||||
for(y=0;y<4;y++)
|
||||
{
|
||||
int dot,lmask,step;
|
||||
|
||||
dot = (dp[0] << 4) + (3*ep2[1] + 5*ep2[0]);
|
||||
if(dot < halfPoint)
|
||||
step = (dot < c0Point) ? 1 : 3;
|
||||
else
|
||||
step = (dot < c3Point) ? 2 : 0;
|
||||
ep1[0] = dp[0] - stops[step];
|
||||
lmask = step;
|
||||
|
||||
dot = (dp[1] << 4) + (7*ep1[0] + 3*ep2[2] + 5*ep2[1] + ep2[0]);
|
||||
if(dot < halfPoint)
|
||||
step = (dot < c0Point) ? 1 : 3;
|
||||
else
|
||||
step = (dot < c3Point) ? 2 : 0;
|
||||
ep1[1] = dp[1] - stops[step];
|
||||
lmask |= step<<2;
|
||||
|
||||
dot = (dp[2] << 4) + (7*ep1[1] + 3*ep2[3] + 5*ep2[2] + ep2[1]);
|
||||
if(dot < halfPoint)
|
||||
step = (dot < c0Point) ? 1 : 3;
|
||||
else
|
||||
step = (dot < c3Point) ? 2 : 0;
|
||||
ep1[2] = dp[2] - stops[step];
|
||||
lmask |= step<<4;
|
||||
|
||||
dot = (dp[3] << 4) + (7*ep1[2] + 5*ep2[3] + ep2[2]);
|
||||
if(dot < halfPoint)
|
||||
step = (dot < c0Point) ? 1 : 3;
|
||||
else
|
||||
step = (dot < c3Point) ? 2 : 0;
|
||||
ep1[3] = dp[3] - stops[step];
|
||||
lmask |= step<<6;
|
||||
|
||||
dp += 4;
|
||||
mask |= lmask << (y*8);
|
||||
{ int *et = ep1; ep1 = ep2; ep2 = et; } // swap
|
||||
}
|
||||
}
|
||||
|
||||
return mask;
|
||||
}
|
||||
|
||||
// The color optimization function. (Clever code, part 1)
|
||||
static void stb__OptimizeColorsBlock(unsigned char *block, unsigned short *pmax16, unsigned short *pmin16)
|
||||
{
|
||||
int mind = 0x7fffffff,maxd = -0x7fffffff;
|
||||
unsigned char *minp, *maxp;
|
||||
double magn;
|
||||
int v_r,v_g,v_b;
|
||||
static const int nIterPower = 4;
|
||||
float covf[6],vfr,vfg,vfb;
|
||||
|
||||
// determine color distribution
|
||||
int cov[6];
|
||||
int mu[3],min[3],max[3];
|
||||
int ch,i,iter;
|
||||
|
||||
for(ch=0;ch<3;ch++)
|
||||
{
|
||||
const unsigned char *bp = ((const unsigned char *) block) + ch;
|
||||
int muv,minv,maxv;
|
||||
|
||||
muv = minv = maxv = bp[0];
|
||||
for(i=4;i<64;i+=4)
|
||||
{
|
||||
muv += bp[i];
|
||||
if (bp[i] < minv) minv = bp[i];
|
||||
else if (bp[i] > maxv) maxv = bp[i];
|
||||
}
|
||||
|
||||
mu[ch] = (muv + 8) >> 4;
|
||||
min[ch] = minv;
|
||||
max[ch] = maxv;
|
||||
}
|
||||
|
||||
// determine covariance matrix
|
||||
for (i=0;i<6;i++)
|
||||
cov[i] = 0;
|
||||
|
||||
for (i=0;i<16;i++)
|
||||
{
|
||||
int r = block[i*4+0] - mu[0];
|
||||
int g = block[i*4+1] - mu[1];
|
||||
int b = block[i*4+2] - mu[2];
|
||||
|
||||
cov[0] += r*r;
|
||||
cov[1] += r*g;
|
||||
cov[2] += r*b;
|
||||
cov[3] += g*g;
|
||||
cov[4] += g*b;
|
||||
cov[5] += b*b;
|
||||
}
|
||||
|
||||
// convert covariance matrix to float, find principal axis via power iter
|
||||
for(i=0;i<6;i++)
|
||||
covf[i] = cov[i] / 255.0f;
|
||||
|
||||
vfr = (float) (max[0] - min[0]);
|
||||
vfg = (float) (max[1] - min[1]);
|
||||
vfb = (float) (max[2] - min[2]);
|
||||
|
||||
for(iter=0;iter<nIterPower;iter++)
|
||||
{
|
||||
float r = vfr*covf[0] + vfg*covf[1] + vfb*covf[2];
|
||||
float g = vfr*covf[1] + vfg*covf[3] + vfb*covf[4];
|
||||
float b = vfr*covf[2] + vfg*covf[4] + vfb*covf[5];
|
||||
|
||||
vfr = r;
|
||||
vfg = g;
|
||||
vfb = b;
|
||||
}
|
||||
|
||||
magn = fabs(vfr);
|
||||
if (fabs(vfg) > magn) magn = fabs(vfg);
|
||||
if (fabs(vfb) > magn) magn = fabs(vfb);
|
||||
|
||||
if(magn < 4.0f) { // too small, default to luminance
|
||||
v_r = 299; // JPEG YCbCr luma coefs, scaled by 1000.
|
||||
v_g = 587;
|
||||
v_b = 114;
|
||||
} else {
|
||||
magn = 512.0 / magn;
|
||||
v_r = (int) (vfr * magn);
|
||||
v_g = (int) (vfg * magn);
|
||||
v_b = (int) (vfb * magn);
|
||||
}
|
||||
|
||||
// Pick colors at extreme points
|
||||
for(i=0;i<16;i++)
|
||||
{
|
||||
int dot = block[i*4+0]*v_r + block[i*4+1]*v_g + block[i*4+2]*v_b;
|
||||
|
||||
if (dot < mind) {
|
||||
mind = dot;
|
||||
minp = block+i*4;
|
||||
}
|
||||
|
||||
if (dot > maxd) {
|
||||
maxd = dot;
|
||||
maxp = block+i*4;
|
||||
}
|
||||
}
|
||||
|
||||
*pmax16 = stb__As16Bit(maxp[0],maxp[1],maxp[2]);
|
||||
*pmin16 = stb__As16Bit(minp[0],minp[1],minp[2]);
|
||||
}
|
||||
|
||||
static int stb__sclamp(float y, int p0, int p1)
|
||||
{
|
||||
int x = (int) y;
|
||||
if (x < p0) return p0;
|
||||
if (x > p1) return p1;
|
||||
return x;
|
||||
}
|
||||
|
||||
// The refinement function. (Clever code, part 2)
|
||||
// Tries to optimize colors to suit block contents better.
|
||||
// (By solving a least squares system via normal equations+Cramer's rule)
|
||||
static int stb__RefineBlock(unsigned char *block, unsigned short *pmax16, unsigned short *pmin16, unsigned int mask)
|
||||
{
|
||||
static const int w1Tab[4] = { 3,0,2,1 };
|
||||
static const int prods[4] = { 0x090000,0x000900,0x040102,0x010402 };
|
||||
// ^some magic to save a lot of multiplies in the accumulating loop...
|
||||
// (precomputed products of weights for least squares system, accumulated inside one 32-bit register)
|
||||
|
||||
float frb,fg;
|
||||
unsigned short oldMin, oldMax, min16, max16;
|
||||
int i, akku = 0, xx,xy,yy;
|
||||
int At1_r,At1_g,At1_b;
|
||||
int At2_r,At2_g,At2_b;
|
||||
unsigned int cm = mask;
|
||||
|
||||
oldMin = *pmin16;
|
||||
oldMax = *pmax16;
|
||||
|
||||
if((mask ^ (mask<<2)) < 4) // all pixels have the same index?
|
||||
{
|
||||
// yes, linear system would be singular; solve using optimal
|
||||
// single-color match on average color
|
||||
int r = 8, g = 8, b = 8;
|
||||
for (i=0;i<16;++i) {
|
||||
r += block[i*4+0];
|
||||
g += block[i*4+1];
|
||||
b += block[i*4+2];
|
||||
}
|
||||
|
||||
r >>= 4; g >>= 4; b >>= 4;
|
||||
|
||||
max16 = (stb__OMatch5[r][0]<<11) | (stb__OMatch6[g][0]<<5) | stb__OMatch5[b][0];
|
||||
min16 = (stb__OMatch5[r][1]<<11) | (stb__OMatch6[g][1]<<5) | stb__OMatch5[b][1];
|
||||
} else {
|
||||
At1_r = At1_g = At1_b = 0;
|
||||
At2_r = At2_g = At2_b = 0;
|
||||
for (i=0;i<16;++i,cm>>=2) {
|
||||
int step = cm&3;
|
||||
int w1 = w1Tab[step];
|
||||
int r = block[i*4+0];
|
||||
int g = block[i*4+1];
|
||||
int b = block[i*4+2];
|
||||
|
||||
akku += prods[step];
|
||||
At1_r += w1*r;
|
||||
At1_g += w1*g;
|
||||
At1_b += w1*b;
|
||||
At2_r += r;
|
||||
At2_g += g;
|
||||
At2_b += b;
|
||||
}
|
||||
|
||||
At2_r = 3*At2_r - At1_r;
|
||||
At2_g = 3*At2_g - At1_g;
|
||||
At2_b = 3*At2_b - At1_b;
|
||||
|
||||
// extract solutions and decide solvability
|
||||
xx = akku >> 16;
|
||||
yy = (akku >> 8) & 0xff;
|
||||
xy = (akku >> 0) & 0xff;
|
||||
|
||||
frb = 3.0f * 31.0f / 255.0f / (xx*yy - xy*xy);
|
||||
fg = frb * 63.0f / 31.0f;
|
||||
|
||||
// solve.
|
||||
max16 = stb__sclamp((At1_r*yy - At2_r*xy)*frb+0.5f,0,31) << 11;
|
||||
max16 |= stb__sclamp((At1_g*yy - At2_g*xy)*fg +0.5f,0,63) << 5;
|
||||
max16 |= stb__sclamp((At1_b*yy - At2_b*xy)*frb+0.5f,0,31) << 0;
|
||||
|
||||
min16 = stb__sclamp((At2_r*xx - At1_r*xy)*frb+0.5f,0,31) << 11;
|
||||
min16 |= stb__sclamp((At2_g*xx - At1_g*xy)*fg +0.5f,0,63) << 5;
|
||||
min16 |= stb__sclamp((At2_b*xx - At1_b*xy)*frb+0.5f,0,31) << 0;
|
||||
}
|
||||
|
||||
*pmin16 = min16;
|
||||
*pmax16 = max16;
|
||||
return oldMin != min16 || oldMax != max16;
|
||||
}
|
||||
|
||||
// Color block compression
|
||||
static void stb__CompressColorBlock(unsigned char *dest, unsigned char *block, int mode)
|
||||
{
|
||||
unsigned int mask;
|
||||
int i;
|
||||
int dither;
|
||||
int refinecount;
|
||||
unsigned short max16, min16;
|
||||
unsigned char dblock[16*4],color[4*4];
|
||||
|
||||
dither = mode & STB_DXT_DITHER;
|
||||
refinecount = (mode & STB_DXT_HIGHQUAL) ? 2 : 1;
|
||||
|
||||
// check if block is constant
|
||||
for (i=1;i<16;i++)
|
||||
if (((unsigned int *) block)[i] != ((unsigned int *) block)[0])
|
||||
break;
|
||||
|
||||
if(i == 16) { // constant color
|
||||
int r = block[0], g = block[1], b = block[2];
|
||||
mask = 0xaaaaaaaa;
|
||||
max16 = (stb__OMatch5[r][0]<<11) | (stb__OMatch6[g][0]<<5) | stb__OMatch5[b][0];
|
||||
min16 = (stb__OMatch5[r][1]<<11) | (stb__OMatch6[g][1]<<5) | stb__OMatch5[b][1];
|
||||
} else {
|
||||
// first step: compute dithered version for PCA if desired
|
||||
if(dither)
|
||||
stb__DitherBlock(dblock,block);
|
||||
|
||||
// second step: pca+map along principal axis
|
||||
stb__OptimizeColorsBlock(dither ? dblock : block,&max16,&min16);
|
||||
if (max16 != min16) {
|
||||
stb__EvalColors(color,max16,min16);
|
||||
mask = stb__MatchColorsBlock(block,color,dither);
|
||||
} else
|
||||
mask = 0;
|
||||
|
||||
// third step: refine (multiple times if requested)
|
||||
for (i=0;i<refinecount;i++) {
|
||||
unsigned int lastmask = mask;
|
||||
|
||||
if (stb__RefineBlock(dither ? dblock : block,&max16,&min16,mask)) {
|
||||
if (max16 != min16) {
|
||||
stb__EvalColors(color,max16,min16);
|
||||
mask = stb__MatchColorsBlock(block,color,dither);
|
||||
} else {
|
||||
mask = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(mask == lastmask)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// write the color block
|
||||
if(max16 < min16)
|
||||
{
|
||||
unsigned short t = min16;
|
||||
min16 = max16;
|
||||
max16 = t;
|
||||
mask ^= 0x55555555;
|
||||
}
|
||||
|
||||
dest[0] = (unsigned char) (max16);
|
||||
dest[1] = (unsigned char) (max16 >> 8);
|
||||
dest[2] = (unsigned char) (min16);
|
||||
dest[3] = (unsigned char) (min16 >> 8);
|
||||
dest[4] = (unsigned char) (mask);
|
||||
dest[5] = (unsigned char) (mask >> 8);
|
||||
dest[6] = (unsigned char) (mask >> 16);
|
||||
dest[7] = (unsigned char) (mask >> 24);
|
||||
}
|
||||
|
||||
// Alpha block compression (this is easy for a change)
|
||||
static void stb__CompressAlphaBlock(unsigned char *dest,unsigned char *src,int mode)
|
||||
{
|
||||
int i,dist,bias,dist4,dist2,bits,mask;
|
||||
|
||||
// find min/max color
|
||||
int mn,mx;
|
||||
mn = mx = src[3];
|
||||
|
||||
for (i=1;i<16;i++)
|
||||
{
|
||||
if (src[i*4+3] < mn) mn = src[i*4+3];
|
||||
else if (src[i*4+3] > mx) mx = src[i*4+3];
|
||||
}
|
||||
|
||||
// encode them
|
||||
((unsigned char *)dest)[0] = mx;
|
||||
((unsigned char *)dest)[1] = mn;
|
||||
dest += 2;
|
||||
|
||||
// determine bias and emit color indices
|
||||
// given the choice of mx/mn, these indices are optimal:
|
||||
// http://fgiesen.wordpress.com/2009/12/15/dxt5-alpha-block-index-determination/
|
||||
dist = mx-mn;
|
||||
dist4 = dist*4;
|
||||
dist2 = dist*2;
|
||||
bias = (dist < 8) ? (dist - 1) : (dist/2 + 2);
|
||||
bias -= mn * 7;
|
||||
bits = 0,mask=0;
|
||||
|
||||
for (i=0;i<16;i++) {
|
||||
int a = src[i*4+3]*7 + bias;
|
||||
int ind,t;
|
||||
|
||||
// select index. this is a "linear scale" lerp factor between 0 (val=min) and 7 (val=max).
|
||||
t = (a >= dist4) ? -1 : 0; ind = t & 4; a -= dist4 & t;
|
||||
t = (a >= dist2) ? -1 : 0; ind += t & 2; a -= dist2 & t;
|
||||
ind += (a >= dist);
|
||||
|
||||
// turn linear scale into DXT index (0/1 are extremal pts)
|
||||
ind = -ind & 7;
|
||||
ind ^= (2 > ind);
|
||||
|
||||
// write index
|
||||
mask |= ind << bits;
|
||||
if((bits += 3) >= 8) {
|
||||
*dest++ = mask;
|
||||
mask >>= 8;
|
||||
bits -= 8;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void stb__InitDXT()
|
||||
{
|
||||
int i;
|
||||
for(i=0;i<32;i++)
|
||||
stb__Expand5[i] = (i<<3)|(i>>2);
|
||||
|
||||
for(i=0;i<64;i++)
|
||||
stb__Expand6[i] = (i<<2)|(i>>4);
|
||||
|
||||
for(i=0;i<256+16;i++)
|
||||
{
|
||||
int v = i-8 < 0 ? 0 : i-8 > 255 ? 255 : i-8;
|
||||
stb__QuantRBTab[i] = stb__Expand5[stb__Mul8Bit(v,31)];
|
||||
stb__QuantGTab[i] = stb__Expand6[stb__Mul8Bit(v,63)];
|
||||
}
|
||||
|
||||
stb__PrepareOptTable(&stb__OMatch5[0][0],stb__Expand5,32);
|
||||
stb__PrepareOptTable(&stb__OMatch6[0][0],stb__Expand6,64);
|
||||
}
|
||||
|
||||
void stb_compress_dxt_block(unsigned char *dest, const unsigned char *src, int alpha, int mode)
|
||||
{
|
||||
static int init=1;
|
||||
if (init) {
|
||||
stb__InitDXT();
|
||||
init=0;
|
||||
}
|
||||
|
||||
if (alpha) {
|
||||
stb__CompressAlphaBlock(dest,(unsigned char*) src,mode);
|
||||
dest += 8;
|
||||
}
|
||||
|
||||
stb__CompressColorBlock(dest,(unsigned char*) src,mode);
|
||||
}
|
||||
#endif // STB_DXT_IMPLEMENTATION
|
||||
|
||||
#endif // STB_INCLUDE_STB_DXT_H
|
Loading…
Reference in a new issue