From 288d7166bc5b1e708d26eb96e5fa1fe1a83248b2 Mon Sep 17 00:00:00 2001 From: Jaime Passos Date: Fri, 15 May 2020 16:41:39 -0300 Subject: [PATCH 1/3] Fix ASTBlendPixel outputting empty pixels if the background pixel was empty, BUT if the foreground pixel had no alpha at all --- src/r_data.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/r_data.c b/src/r_data.c index 831e75bef..21f382845 100644 --- a/src/r_data.c +++ b/src/r_data.c @@ -244,7 +244,13 @@ UINT32 ASTBlendPixel(RGBA_t background, RGBA_t foreground, int style, UINT8 alph // if the background pixel is empty, match software and don't blend anything if (!background.s.alpha) - output.rgba = 0; + { + // ...unless the foreground pixel ISN'T actually translucent. + if (alpha == 0xFF) + output.rgba = foreground.rgba; + else + output.rgba = 0; + } else { UINT8 beta = (0xFF - alpha); From c64e231b2b196da7cb6ae67511365f4ade3a4bdb Mon Sep 17 00:00:00 2001 From: Jaime Passos Date: Sat, 16 May 2020 13:51:10 -0300 Subject: [PATCH 2/3] Attempt to properly match Software texture blending without messing with the original ASTBlendPixel function --- src/hardware/hw_cache.c | 16 ++++++++-------- src/r_data.c | 40 +++++++++++++++++++++++++++++++++++----- src/r_data.h | 5 ++++- 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/src/hardware/hw_cache.c b/src/hardware/hw_cache.c index 059556f15..9109a6e53 100644 --- a/src/hardware/hw_cache.c +++ b/src/hardware/hw_cache.c @@ -135,7 +135,7 @@ static void HWR_DrawColumnInCache(const column_t *patchcol, UINT8 *block, GLMipm { case 2 : // uhhhhhhhh.......... if ((originPatch != NULL) && (originPatch->style != AST_COPY)) - texel = ASTBlendPixel_8bpp(*(dest+1), texel, originPatch->style, originPatch->alpha); + texel = ASTBlendPaletteIndexes(*(dest+1), texel, originPatch->style, originPatch->alpha); texelu16 = (UINT16)((alpha<<8) | texel); memcpy(dest, &texelu16, sizeof(UINT16)); break; @@ -144,7 +144,7 @@ static void HWR_DrawColumnInCache(const column_t *patchcol, UINT8 *block, GLMipm { RGBA_t rgbatexel; rgbatexel.rgba = *(UINT32 *)dest; - colortemp.rgba = ASTBlendPixel(rgbatexel, colortemp, originPatch->style, originPatch->alpha); + colortemp.rgba = ASTBlendTexturePixel(rgbatexel, colortemp, originPatch->style, originPatch->alpha); } memcpy(dest, &colortemp, sizeof(RGBA_t)-sizeof(UINT8)); break; @@ -154,14 +154,14 @@ static void HWR_DrawColumnInCache(const column_t *patchcol, UINT8 *block, GLMipm { RGBA_t rgbatexel; rgbatexel.rgba = *(UINT32 *)dest; - colortemp.rgba = ASTBlendPixel(rgbatexel, colortemp, originPatch->style, originPatch->alpha); + colortemp.rgba = ASTBlendTexturePixel(rgbatexel, colortemp, originPatch->style, originPatch->alpha); } memcpy(dest, &colortemp, sizeof(RGBA_t)); break; // default is 1 default: if ((originPatch != NULL) && (originPatch->style != AST_COPY)) - *dest = ASTBlendPixel_8bpp(*dest, texel, originPatch->style, originPatch->alpha); + *dest = ASTBlendPaletteIndexes(*dest, texel, originPatch->style, originPatch->alpha); else *dest = texel; break; @@ -247,7 +247,7 @@ static void HWR_DrawFlippedColumnInCache(const column_t *patchcol, UINT8 *block, { case 2 : // uhhhhhhhh.......... if ((originPatch != NULL) && (originPatch->style != AST_COPY)) - texel = ASTBlendPixel_8bpp(*(dest+1), texel, originPatch->style, originPatch->alpha); + texel = ASTBlendPaletteIndexes(*(dest+1), texel, originPatch->style, originPatch->alpha); texelu16 = (UINT16)((alpha<<8) | texel); memcpy(dest, &texelu16, sizeof(UINT16)); break; @@ -256,7 +256,7 @@ static void HWR_DrawFlippedColumnInCache(const column_t *patchcol, UINT8 *block, { RGBA_t rgbatexel; rgbatexel.rgba = *(UINT32 *)dest; - colortemp.rgba = ASTBlendPixel(rgbatexel, colortemp, originPatch->style, originPatch->alpha); + colortemp.rgba = ASTBlendTexturePixel(rgbatexel, colortemp, originPatch->style, originPatch->alpha); } memcpy(dest, &colortemp, sizeof(RGBA_t)-sizeof(UINT8)); break; @@ -266,14 +266,14 @@ static void HWR_DrawFlippedColumnInCache(const column_t *patchcol, UINT8 *block, { RGBA_t rgbatexel; rgbatexel.rgba = *(UINT32 *)dest; - colortemp.rgba = ASTBlendPixel(rgbatexel, colortemp, originPatch->style, originPatch->alpha); + colortemp.rgba = ASTBlendTexturePixel(rgbatexel, colortemp, originPatch->style, originPatch->alpha); } memcpy(dest, &colortemp, sizeof(RGBA_t)); break; // default is 1 default: if ((originPatch != NULL) && (originPatch->style != AST_COPY)) - *dest = ASTBlendPixel_8bpp(*dest, texel, originPatch->style, originPatch->alpha); + *dest = ASTBlendPaletteIndexes(*dest, texel, originPatch->style, originPatch->alpha); else *dest = texel; break; diff --git a/src/r_data.c b/src/r_data.c index 21f382845..fc783ad17 100644 --- a/src/r_data.c +++ b/src/r_data.c @@ -227,6 +227,8 @@ static inline void R_DrawFlippedColumnInCache(column_t *patch, UINT8 *cache, tex } } +// Blends two pixels together, using the equation +// that matches the specified alpha style. UINT32 ASTBlendPixel(RGBA_t background, RGBA_t foreground, int style, UINT8 alpha) { RGBA_t output; @@ -307,18 +309,46 @@ UINT32 ASTBlendPixel(RGBA_t background, RGBA_t foreground, int style, UINT8 alph return 0; } -UINT8 ASTBlendPixel_8bpp(UINT8 background, UINT8 foreground, int style, UINT8 alpha) +INT32 ASTTextureBlendingThreshold[2] = {255/11, (10*255/11)}; + +// Blends a pixel for a texture patch. +UINT32 ASTBlendTexturePixel(RGBA_t background, RGBA_t foreground, int style, UINT8 alpha) { // Alpha style set to translucent? if (style == AST_TRANSLUCENT) { // Is the alpha small enough for translucency? - if (alpha <= (10*255/11)) + if (alpha <= ASTTextureBlendingThreshold[1]) + { + // Is the patch way too translucent? Don't blend then. + if (alpha < ASTTextureBlendingThreshold[0]) + return background; + + return foreground; + } + else // just copy the pixel + return foreground; + } + else + return ASTBlendPixel(background, foreground, style, alpha); +} + +// Blends two palette indexes for a texture patch, then +// finds the nearest palette index from the blended output. +UINT8 ASTBlendPaletteIndexes(UINT8 background, UINT8 foreground, int style, UINT8 alpha) +{ + // Alpha style set to translucent? + if (style == AST_TRANSLUCENT) + { + // Is the alpha small enough for translucency? + if (alpha <= ASTTextureBlendingThreshold[1]) { UINT8 *mytransmap; + // Is the patch way too translucent? Don't blend then. - if (alpha < 255/11) + if (alpha < ASTTextureBlendingThreshold[0]) return background; + // The equation's not exact but it works as intended. I'll call it a day for now. mytransmap = transtables + ((8*(alpha) + 255/8)/(255 - 255/11) << FF_TRANSSHIFT); if (background != 0xFF) @@ -383,7 +413,7 @@ static inline void R_DrawBlendColumnInCache(column_t *patch, UINT8 *cache, texpa { for (; dest < cache + position + count; source++, dest++) if (*source != 0xFF) - *dest = ASTBlendPixel_8bpp(*dest, *source, originPatch->style, originPatch->alpha); + *dest = ASTBlendPaletteIndexes(*dest, *source, originPatch->style, originPatch->alpha); } patch = (column_t *)((UINT8 *)patch + patch->length + 4); @@ -427,7 +457,7 @@ static inline void R_DrawBlendFlippedColumnInCache(column_t *patch, UINT8 *cache { for (; dest < cache + position + count; --source, dest++) if (*source != 0xFF) - *dest = ASTBlendPixel_8bpp(*dest, *source, originPatch->style, originPatch->alpha); + *dest = ASTBlendPaletteIndexes(*dest, *source, originPatch->style, originPatch->alpha); } patch = (column_t *)((UINT8 *)patch + patch->length + 4); diff --git a/src/r_data.h b/src/r_data.h index 78ce35a41..8b8d08c52 100644 --- a/src/r_data.h +++ b/src/r_data.h @@ -26,7 +26,10 @@ enum patchalphastyle {AST_COPY, AST_TRANSLUCENT, AST_ADD, AST_SUBTRACT, AST_REVERSESUBTRACT, AST_MODULATE, AST_OVERLAY}; UINT32 ASTBlendPixel(RGBA_t background, RGBA_t foreground, int style, UINT8 alpha); -UINT8 ASTBlendPixel_8bpp(UINT8 background, UINT8 foreground, int style, UINT8 alpha); +UINT32 ASTBlendTexturePixel(RGBA_t background, RGBA_t foreground, int style, UINT8 alpha); +UINT8 ASTBlendPaletteIndexes(UINT8 background, UINT8 foreground, int style, UINT8 alpha); + +extern INT32 ASTTextureBlendingThreshold[2]; UINT8 NearestColor(UINT8 r, UINT8 g, UINT8 b); From 423494381e9366d86b819e5a7c75c0a962444c1a Mon Sep 17 00:00:00 2001 From: Jaime Passos Date: Sat, 16 May 2020 14:01:42 -0300 Subject: [PATCH 3/3] Fix mistake --- src/r_data.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/r_data.c b/src/r_data.c index fc783ad17..302d2ea57 100644 --- a/src/r_data.c +++ b/src/r_data.c @@ -322,12 +322,12 @@ UINT32 ASTBlendTexturePixel(RGBA_t background, RGBA_t foreground, int style, UIN { // Is the patch way too translucent? Don't blend then. if (alpha < ASTTextureBlendingThreshold[0]) - return background; + return background.rgba; - return foreground; + return ASTBlendPixel(background, foreground, style, alpha); } else // just copy the pixel - return foreground; + return foreground.rgba; } else return ASTBlendPixel(background, foreground, style, alpha);