Skip to content

Commit

Permalink
togles: don't use alpha channel in dxt1 decompression for textures wi…
Browse files Browse the repository at this point in the history
…thout it
  • Loading branch information
nillerusr committed Apr 6, 2023
1 parent ceda7d8 commit dcdcf6b
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 679 deletions.
24 changes: 12 additions & 12 deletions togles/linuxwin/cglmtex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3409,8 +3409,8 @@ GLvoid *uncompressDXTc(GLsizei width, GLsizei height, GLenum format, GLsizei ima
// uncompress a DXTc image
// get pixel size of uncompressed image => fixed RGBA
int pixelsize = 4;
/* if (format==COMPRESSED_RGB_S3TC_DXT1_EXT)
pixelsize = 3;*/
if (format == GL_COMPRESSED_RGB_S3TC_DXT1_EXT || format == GL_COMPRESSED_SRGB_S3TC_DXT1_EXT)
pixelsize = 3;
// check with the size of the input data stream if the stream is in fact uncompressed
if (imageSize == width*height*pixelsize || data==NULL) {
// uncompressed stream
Expand Down Expand Up @@ -3469,16 +3469,16 @@ void CompressedTexImage2D(GLenum target, GLint level, GLenum internalformat,
if ((width<=0) || (height<=0)) {
return;
}

GLenum format = GL_RGBA;
GLenum intformat = GL_RGBA;
GLenum type = GL_UNSIGNED_BYTE;
GLvoid *pixels = NULL;

if (isDXTc(internalformat)) {
pixels = NULL;
type = GL_UNSIGNED_BYTE;

bool hasAlpha = (internalformat != GL_COMPRESSED_RGB_S3TC_DXT1_EXT) && (internalformat != GL_COMPRESSED_SRGB_S3TC_DXT1_EXT);

GLenum format = hasAlpha ? GL_RGBA : GL_RGB;
GLenum intformat = hasAlpha ? GL_RGBA8 : GL_RGB8;
GLenum type = GL_UNSIGNED_BYTE;
GLvoid *pixels = NULL;

if (isDXTc(internalformat))
{
int srgb = isDXTcSRGB(internalformat);
int simpleAlpha = 0;
int complexAlpha = 0;
Expand All @@ -3492,7 +3492,7 @@ void CompressedTexImage2D(GLenum target, GLint level, GLenum internalformat,
}

if( srgb )
intformat = GL_SRGB8_ALPHA8;
intformat = hasAlpha ? GL_SRGB8_ALPHA8 : GL_SRGB8;
}

gGL->glTexImage2D(target, level, intformat, width, height, border, format, type, pixels);
Expand Down
142 changes: 99 additions & 43 deletions togles/linuxwin/decompress.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,17 @@ static void DecompressBlockDXT1Internal (const uint8_t* block,

code = *(const uint32_t*)(block + 4);

if (color0 > color1) {
for (j = 0; j < 4; ++j) {
for (i = 0; i < 4; ++i) {
uint32_t finalColor, positionCode;
uint8_t alpha;
for (j = 0; j < 4; ++j) {
for (i = 0; i < 4; ++i) {
uint32_t finalColor, positionCode;
uint8_t alpha;

alpha = alphaValues [j*4+i];
finalColor = 0; positionCode = (code >> 2*(4*j+i)) & 0x03;
alpha = alphaValues [j*4+i];

finalColor = 0;
positionCode = (code >> 2*(4*j+i)) & 0x03;

switch (positionCode) {
if (color0 > color1) {
switch (positionCode)
{
case 0:
finalColor = PackRGBA(r0, g0, b0, alpha);
break;
Expand All @@ -117,24 +116,7 @@ static void DecompressBlockDXT1Internal (const uint8_t* block,
finalColor = PackRGBA((r0+2*r1)/3, (g0+2*g1)/3, (b0+2*b1)/3, alpha);
break;
}
if(!alpha)
*simpleAlpha = 1;
else if(alpha<0xff)
*complexAlpha = 1;
output [j*outputStride + i] = finalColor;
}
}
} else {
for (j = 0; j < 4; ++j) {
for (i = 0; i < 4; ++i) {
uint32_t finalColor, positionCode;
uint8_t alpha;

alpha = alphaValues [j*4+i];

finalColor = 0;
positionCode = (code >> 2*(4*j+i)) & 0x03;

} else {
switch (positionCode) {
case 0:
finalColor = PackRGBA(r0, g0, b0, alpha);
Expand All @@ -150,18 +132,95 @@ static void DecompressBlockDXT1Internal (const uint8_t* block,
finalColor = PackRGBA(0, 0, 0, alpha);
break;
}
}

if(!alpha)
*simpleAlpha = 1;
else if(alpha<0xff)
*complexAlpha = 1;

if(!alpha)
*simpleAlpha = 1;
else if(alpha<0xff)
*complexAlpha = 1;
output [j*outputStride + i] = finalColor;
}
}
}

static void DecompressBlockDXT1InternalRGB(const uint8_t* block, uint8_t* output, uint32_t outputStride)
{
uint32_t temp, code;

uint16_t color0, color1;
uint8_t r0, g0, b0, r1, g1, b1;

int i, j;

color0 = *(const uint16_t*)(block);
color1 = *(const uint16_t*)(block + 2);

temp = (color0 >> 11) * 255 + 16;
r0 = (uint8_t)((temp/32 + temp)/32);
temp = ((color0 & 0x07E0) >> 5) * 255 + 32;
g0 = (uint8_t)((temp/64 + temp)/64);
temp = (color0 & 0x001F) * 255 + 16;
b0 = (uint8_t)((temp/32 + temp)/32);

temp = (color1 >> 11) * 255 + 16;
r1 = (uint8_t)((temp/32 + temp)/32);
temp = ((color1 & 0x07E0) >> 5) * 255 + 32;
g1 = (uint8_t)((temp/64 + temp)/64);
temp = (color1 & 0x001F) * 255 + 16;
b1 = (uint8_t)((temp/32 + temp)/32);

code = *(const uint32_t*)(block + 4);

for (j = 0; j < 4; ++j) {
for (i = 0; i < 4; ++i) {
uint8_t positionCode, finalR, finalG, finalB;

positionCode = (code >> 2*(4*j+i)) & 0x03;

if (color0 > color1) {

switch (positionCode) {
case 0:
finalR = r0; finalG = g0; finalB = b0;
break;
case 1:
finalR = r1; finalG = g1; finalB = b1;
break;
case 2:

finalR = (2*r0+r1)/3; finalG = (2*g0+g1)/3; finalB = (2*b0+b1)/3;
break;
case 3:
finalR = (r0+2*r1)/3; finalG = (g0+2*g1)/3; finalB = (b0+2*b1)/3;
break;
}
} else {
switch (positionCode) {
case 0:
finalR = r0; finalG = g0; finalB = b0;
break;
case 1:
finalR = r1; finalG = g1; finalB = b1;
break;
case 2:
finalR = (r0+r1)/2; finalG = (g0+g1)/2; finalB = (b0+b1)/2;
break;
case 3:
finalR = finalG = finalB = 0;
break;
}

output [j*outputStride + i] = finalColor;
}

output[j*outputStride*3 + i*3] = finalR;
output[j*outputStride*3 + i*3+1] = finalG;
output[j*outputStride*3 + i*3+2] = finalB;
}
}
}


/*
void DecompressBlockDXT1(): Decompresses one block of a DXT1 texture and stores the resulting pixels at the appropriate offset in 'image'.
Expand All @@ -176,15 +235,20 @@ void DecompressBlockDXT1(uint32_t x, uint32_t y, uint32_t width,
int transparent0, int* simpleAlpha, int *complexAlpha,
uint32_t* image)
{

static const uint8_t const_alpha [] = {
255, 255, 255, 255,
255, 255, 255, 255,
255, 255, 255, 255,
255, 255, 255, 255
};

DecompressBlockDXT1Internal (blockStorage,
image + x + (y * width), width, transparent0, simpleAlpha, complexAlpha, const_alpha);

if( transparent0 )
DecompressBlockDXT1Internal (blockStorage,
image + x + (y * width), width, transparent0, simpleAlpha, complexAlpha, const_alpha);
else
DecompressBlockDXT1InternalRGB(blockStorage, ((uint8_t*)image) + x*3 + (y*3 * width), width);
}

/*
Expand Down Expand Up @@ -331,11 +395,3 @@ void DecompressBlockDXT3(uint32_t x, uint32_t y, uint32_t width,
DecompressBlockDXT1Internal (blockStorage,
image + x + (y * width), width, transparent0, simpleAlpha, complexAlpha, alphaValues);
}

// Texture DXT1 / DXT5 compression
// Using STB "on file" library
// go there https://github.com/nothings/stb
// for more details and other libs

#define STB_DXT_IMPLEMENTATION
#include "stb_dxt_104.h"
Loading

0 comments on commit dcdcf6b

Please sign in to comment.