Skip to content

Commit dcdcf6b

Browse files
committed
togles: don't use alpha channel in dxt1 decompression for textures without it
1 parent ceda7d8 commit dcdcf6b

File tree

3 files changed

+111
-679
lines changed

3 files changed

+111
-679
lines changed

togles/linuxwin/cglmtex.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -3409,8 +3409,8 @@ GLvoid *uncompressDXTc(GLsizei width, GLsizei height, GLenum format, GLsizei ima
34093409
// uncompress a DXTc image
34103410
// get pixel size of uncompressed image => fixed RGBA
34113411
int pixelsize = 4;
3412-
/* if (format==COMPRESSED_RGB_S3TC_DXT1_EXT)
3413-
pixelsize = 3;*/
3412+
if (format == GL_COMPRESSED_RGB_S3TC_DXT1_EXT || format == GL_COMPRESSED_SRGB_S3TC_DXT1_EXT)
3413+
pixelsize = 3;
34143414
// check with the size of the input data stream if the stream is in fact uncompressed
34153415
if (imageSize == width*height*pixelsize || data==NULL) {
34163416
// uncompressed stream
@@ -3469,16 +3469,16 @@ void CompressedTexImage2D(GLenum target, GLint level, GLenum internalformat,
34693469
if ((width<=0) || (height<=0)) {
34703470
return;
34713471
}
3472-
3473-
GLenum format = GL_RGBA;
3474-
GLenum intformat = GL_RGBA;
3475-
GLenum type = GL_UNSIGNED_BYTE;
3476-
GLvoid *pixels = NULL;
3477-
3478-
if (isDXTc(internalformat)) {
3479-
pixels = NULL;
3480-
type = GL_UNSIGNED_BYTE;
34813472

3473+
bool hasAlpha = (internalformat != GL_COMPRESSED_RGB_S3TC_DXT1_EXT) && (internalformat != GL_COMPRESSED_SRGB_S3TC_DXT1_EXT);
3474+
3475+
GLenum format = hasAlpha ? GL_RGBA : GL_RGB;
3476+
GLenum intformat = hasAlpha ? GL_RGBA8 : GL_RGB8;
3477+
GLenum type = GL_UNSIGNED_BYTE;
3478+
GLvoid *pixels = NULL;
3479+
3480+
if (isDXTc(internalformat))
3481+
{
34823482
int srgb = isDXTcSRGB(internalformat);
34833483
int simpleAlpha = 0;
34843484
int complexAlpha = 0;
@@ -3492,7 +3492,7 @@ void CompressedTexImage2D(GLenum target, GLint level, GLenum internalformat,
34923492
}
34933493

34943494
if( srgb )
3495-
intformat = GL_SRGB8_ALPHA8;
3495+
intformat = hasAlpha ? GL_SRGB8_ALPHA8 : GL_SRGB8;
34963496
}
34973497

34983498
gGL->glTexImage2D(target, level, intformat, width, height, border, format, type, pixels);

togles/linuxwin/decompress.c

+99-43
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,17 @@ static void DecompressBlockDXT1Internal (const uint8_t* block,
9292

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

95-
if (color0 > color1) {
96-
for (j = 0; j < 4; ++j) {
97-
for (i = 0; i < 4; ++i) {
98-
uint32_t finalColor, positionCode;
99-
uint8_t alpha;
95+
for (j = 0; j < 4; ++j) {
96+
for (i = 0; i < 4; ++i) {
97+
uint32_t finalColor, positionCode;
98+
uint8_t alpha;
10099

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

103-
finalColor = 0;
104-
positionCode = (code >> 2*(4*j+i)) & 0x03;
105-
106-
switch (positionCode) {
103+
if (color0 > color1) {
104+
switch (positionCode)
105+
{
107106
case 0:
108107
finalColor = PackRGBA(r0, g0, b0, alpha);
109108
break;
@@ -117,24 +116,7 @@ static void DecompressBlockDXT1Internal (const uint8_t* block,
117116
finalColor = PackRGBA((r0+2*r1)/3, (g0+2*g1)/3, (b0+2*b1)/3, alpha);
118117
break;
119118
}
120-
if(!alpha)
121-
*simpleAlpha = 1;
122-
else if(alpha<0xff)
123-
*complexAlpha = 1;
124-
output [j*outputStride + i] = finalColor;
125-
}
126-
}
127-
} else {
128-
for (j = 0; j < 4; ++j) {
129-
for (i = 0; i < 4; ++i) {
130-
uint32_t finalColor, positionCode;
131-
uint8_t alpha;
132-
133-
alpha = alphaValues [j*4+i];
134-
135-
finalColor = 0;
136-
positionCode = (code >> 2*(4*j+i)) & 0x03;
137-
119+
} else {
138120
switch (positionCode) {
139121
case 0:
140122
finalColor = PackRGBA(r0, g0, b0, alpha);
@@ -150,18 +132,95 @@ static void DecompressBlockDXT1Internal (const uint8_t* block,
150132
finalColor = PackRGBA(0, 0, 0, alpha);
151133
break;
152134
}
135+
}
136+
137+
if(!alpha)
138+
*simpleAlpha = 1;
139+
else if(alpha<0xff)
140+
*complexAlpha = 1;
153141

154-
if(!alpha)
155-
*simpleAlpha = 1;
156-
else if(alpha<0xff)
157-
*complexAlpha = 1;
142+
output [j*outputStride + i] = finalColor;
143+
}
144+
}
145+
}
146+
147+
static void DecompressBlockDXT1InternalRGB(const uint8_t* block, uint8_t* output, uint32_t outputStride)
148+
{
149+
uint32_t temp, code;
150+
151+
uint16_t color0, color1;
152+
uint8_t r0, g0, b0, r1, g1, b1;
153+
154+
int i, j;
155+
156+
color0 = *(const uint16_t*)(block);
157+
color1 = *(const uint16_t*)(block + 2);
158+
159+
temp = (color0 >> 11) * 255 + 16;
160+
r0 = (uint8_t)((temp/32 + temp)/32);
161+
temp = ((color0 & 0x07E0) >> 5) * 255 + 32;
162+
g0 = (uint8_t)((temp/64 + temp)/64);
163+
temp = (color0 & 0x001F) * 255 + 16;
164+
b0 = (uint8_t)((temp/32 + temp)/32);
165+
166+
temp = (color1 >> 11) * 255 + 16;
167+
r1 = (uint8_t)((temp/32 + temp)/32);
168+
temp = ((color1 & 0x07E0) >> 5) * 255 + 32;
169+
g1 = (uint8_t)((temp/64 + temp)/64);
170+
temp = (color1 & 0x001F) * 255 + 16;
171+
b1 = (uint8_t)((temp/32 + temp)/32);
172+
173+
code = *(const uint32_t*)(block + 4);
174+
175+
for (j = 0; j < 4; ++j) {
176+
for (i = 0; i < 4; ++i) {
177+
uint8_t positionCode, finalR, finalG, finalB;
178+
179+
positionCode = (code >> 2*(4*j+i)) & 0x03;
180+
181+
if (color0 > color1) {
182+
183+
switch (positionCode) {
184+
case 0:
185+
finalR = r0; finalG = g0; finalB = b0;
186+
break;
187+
case 1:
188+
finalR = r1; finalG = g1; finalB = b1;
189+
break;
190+
case 2:
191+
192+
finalR = (2*r0+r1)/3; finalG = (2*g0+g1)/3; finalB = (2*b0+b1)/3;
193+
break;
194+
case 3:
195+
finalR = (r0+2*r1)/3; finalG = (g0+2*g1)/3; finalB = (b0+2*b1)/3;
196+
break;
197+
}
198+
} else {
199+
switch (positionCode) {
200+
case 0:
201+
finalR = r0; finalG = g0; finalB = b0;
202+
break;
203+
case 1:
204+
finalR = r1; finalG = g1; finalB = b1;
205+
break;
206+
case 2:
207+
finalR = (r0+r1)/2; finalG = (g0+g1)/2; finalB = (b0+b1)/2;
208+
break;
209+
case 3:
210+
finalR = finalG = finalB = 0;
211+
break;
212+
}
158213

159-
output [j*outputStride + i] = finalColor;
160214
}
215+
216+
output[j*outputStride*3 + i*3] = finalR;
217+
output[j*outputStride*3 + i*3+1] = finalG;
218+
output[j*outputStride*3 + i*3+2] = finalB;
161219
}
162220
}
163221
}
164222

223+
165224
/*
166225
void DecompressBlockDXT1(): Decompresses one block of a DXT1 texture and stores the resulting pixels at the appropriate offset in 'image'.
167226
@@ -176,15 +235,20 @@ void DecompressBlockDXT1(uint32_t x, uint32_t y, uint32_t width,
176235
int transparent0, int* simpleAlpha, int *complexAlpha,
177236
uint32_t* image)
178237
{
238+
179239
static const uint8_t const_alpha [] = {
180240
255, 255, 255, 255,
181241
255, 255, 255, 255,
182242
255, 255, 255, 255,
183243
255, 255, 255, 255
184244
};
185245

186-
DecompressBlockDXT1Internal (blockStorage,
187-
image + x + (y * width), width, transparent0, simpleAlpha, complexAlpha, const_alpha);
246+
247+
if( transparent0 )
248+
DecompressBlockDXT1Internal (blockStorage,
249+
image + x + (y * width), width, transparent0, simpleAlpha, complexAlpha, const_alpha);
250+
else
251+
DecompressBlockDXT1InternalRGB(blockStorage, ((uint8_t*)image) + x*3 + (y*3 * width), width);
188252
}
189253

190254
/*
@@ -331,11 +395,3 @@ void DecompressBlockDXT3(uint32_t x, uint32_t y, uint32_t width,
331395
DecompressBlockDXT1Internal (blockStorage,
332396
image + x + (y * width), width, transparent0, simpleAlpha, complexAlpha, alphaValues);
333397
}
334-
335-
// Texture DXT1 / DXT5 compression
336-
// Using STB "on file" library
337-
// go there https://github.com/nothings/stb
338-
// for more details and other libs
339-
340-
#define STB_DXT_IMPLEMENTATION
341-
#include "stb_dxt_104.h"

0 commit comments

Comments
 (0)