Changeset - r28330:f8a96633c381
[Not reviewed]
master
0 10 0
Peter Nelson - 12 months ago 2023-12-22 16:01:33
peter1138@openttd.org
Change: Support custom transparency remaps with 32bpp blitters.

This closes a 15 year old TODO...
10 files changed with 114 insertions and 24 deletions:
0 comments (0 inline, 0 general)
src/blitter/32bpp_anim.cpp
Show inline comments
 
@@ -6,12 +6,13 @@
 
 */
 

	
 
/** @file 32bpp_anim.cpp Implementation of the optimized 32 bpp blitter with animation support. */
 

	
 
#include "../stdafx.h"
 
#include "../video/video_driver.hpp"
 
#include "../palette_func.h"
 
#include "32bpp_anim.hpp"
 
#include "common.hpp"
 

	
 
#include "../table/sprites.h"
 

	
 
#include "../safeguards.h"
 
@@ -187,16 +188,12 @@ inline void Blitter_32bppAnim::Draw(cons
 
						src_px++;
 
						src_n++;
 
					} while (--n != 0);
 
					break;
 

	
 
				case BM_TRANSPARENT:
 
					/* TODO -- We make an assumption here that the remap in fact is transparency, not some colour.
 
					 *  This is never a problem with the code we produce, but newgrfs can make it fail... or at least:
 
					 *  we produce a result the newgrf maker didn't expect ;) */
 

	
 
					/* Make the current colour a bit more black, so it looks like this image is transparent */
 
					src_n += n;
 
					if (src_px->a == 255) {
 
						src_px += n;
 
						do {
 
							*dst = MakeTransparent(*dst, 3, 4);
 
@@ -212,12 +209,30 @@ inline void Blitter_32bppAnim::Draw(cons
 
							dst++;
 
							src_px++;
 
						} while (--n != 0);
 
					}
 
					break;
 

	
 
				case BM_TRANSPARENT_REMAP:
 
					/* Apply custom transparency remap. */
 
					src_n += n;
 
					if (src_px->a != 0) {
 
						src_px += n;
 
						do {
 
							*dst = this->LookupColourInPalette(bp->remap[GetNearestColourIndex(*dst)]);
 
							*anim = 0;
 
							anim++;
 
							dst++;
 
						} while (--n != 0);
 
					} else {
 
						dst += n;
 
						anim += n;
 
						src_px += n;
 
					}
 
					break;
 

	
 
				default:
 
					if (src_px->a == 255) {
 
						do {
 
							/* Compiler assumes pointer aliasing, can't optimise this on its own */
 
							uint m = GB(*src_n, 0, 8);
 
							/* Above PALETTE_ANIM_START is palette animation */
 
@@ -261,12 +276,13 @@ void Blitter_32bppAnim::Draw(Blitter::Bl
 

	
 
	switch (mode) {
 
		default: NOT_REACHED();
 
		case BM_NORMAL:       Draw<BM_NORMAL>      (bp, zoom); return;
 
		case BM_COLOUR_REMAP: Draw<BM_COLOUR_REMAP>(bp, zoom); return;
 
		case BM_TRANSPARENT:  Draw<BM_TRANSPARENT> (bp, zoom); return;
 
		case BM_TRANSPARENT_REMAP: Draw<BM_TRANSPARENT_REMAP>(bp, zoom); return;
 
		case BM_CRASH_REMAP:  Draw<BM_CRASH_REMAP> (bp, zoom); return;
 
		case BM_BLACK_REMAP:  Draw<BM_BLACK_REMAP> (bp, zoom); return;
 
	}
 
}
 

	
 
void Blitter_32bppAnim::DrawColourMappingRect(void *dst, int width, int height, PaletteID pal)
src/blitter/32bpp_anim_sse4.cpp
Show inline comments
 
@@ -4,12 +4,13 @@
 
 * OpenTTD 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 General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
/** @file 32bpp_anim_sse4.cpp Implementation of the SSE4 32 bpp blitter with animation support. */
 

	
 
#include "palette_func.h"
 
#ifdef WITH_SSE
 

	
 
#include "../stdafx.h"
 
#include "../video/video_driver.hpp"
 
#include "../table/sprites.h"
 
#include "32bpp_anim_sse4.hpp"
 
@@ -314,12 +315,27 @@ bmcr_alpha_blend_single:
 
					__m128i dstABCD = _mm_cvtsi32_si128(dst->data);
 
					dst->data = _mm_cvtsi128_si32(DarkenTwoPixels(srcABCD, dstABCD, a_cm, tr_nom_base));
 
					if (src[0].a) anim[0] = 0;
 
				}
 
				break;
 

	
 
			case BM_TRANSPARENT_REMAP:
 
				/* Apply custom transparency remap. */
 
				for (uint x = (uint) bp->width; x > 0; x--) {
 
					if (src->a != 0) {
 
						*dst = this->LookupColourInPalette(bp->remap[GetNearestColourIndex(*dst)]);
 
						*anim = 0;
 
					}
 
					src_mv++;
 
					dst++;
 
					src++;
 
					anim++;
 
				}
 
				break;
 

	
 

	
 
			case BM_CRASH_REMAP:
 
				for (uint x = (uint) bp->width; x > 0; x--) {
 
					if (src_mv->m == 0) {
 
						if (src->a != 0) {
 
							uint8_t g = MakeDark(src->r, src->g, src->b);
 
							*dst = ComposeColourRGBA(g, g, g, src->a, *dst);
 
@@ -348,13 +364,13 @@ bmcr_alpha_blend_single:
 
					anim++;
 
				}
 
				break;
 
		}
 

	
 
next_line:
 
		if (mode != BM_TRANSPARENT) src_mv_line += si->sprite_width;
 
		if (mode != BM_TRANSPARENT && mode != BM_TRANSPARENT_REMAP) src_mv_line += si->sprite_width;
 
		src_rgba_line = (const Colour*) ((const byte*) src_rgba_line + si->sprite_line_size);
 
		dst_line += bp->pitch;
 
		anim_line += this->anim_buf_pitch;
 
	}
 
}
 
IGNORE_UNINITIALIZED_WARNING_STOP
 
@@ -411,12 +427,13 @@ bm_normal:
 
			} else {
 
				if (sprite_flags & SF_NO_ANIM) Draw<BM_COLOUR_REMAP, RM_WITH_MARGIN, BT_NONE, true, false>(bp, zoom);
 
				else                           Draw<BM_COLOUR_REMAP, RM_WITH_MARGIN, BT_NONE, true, true>(bp, zoom);
 
			}
 
			break;
 
		case BM_TRANSPARENT:  Draw<BM_TRANSPARENT, RM_NONE, BT_NONE, true, true>(bp, zoom); return;
 
		case BM_TRANSPARENT_REMAP: Draw<BM_TRANSPARENT_REMAP, RM_NONE, BT_NONE, true, true>(bp, zoom); return;
 
		case BM_CRASH_REMAP:  Draw<BM_CRASH_REMAP, RM_NONE, BT_NONE, true, true>(bp, zoom); return;
 
		case BM_BLACK_REMAP:  Draw<BM_BLACK_REMAP, RM_NONE, BT_NONE, true, true>(bp, zoom); return;
 
	}
 
}
 

	
 
#endif /* WITH_SSE */
src/blitter/32bpp_optimized.cpp
Show inline comments
 
@@ -7,12 +7,13 @@
 

	
 
/** @file 32bpp_optimized.cpp Implementation of the optimized 32 bpp blitter. */
 

	
 
#include "../stdafx.h"
 
#include "../zoom_func.h"
 
#include "../settings_type.h"
 
#include "../palette_func.h"
 
#include "32bpp_optimized.hpp"
 

	
 
#include "../safeguards.h"
 

	
 
/** Instantiation of the optimized 32bpp blitter factory. */
 
static FBlitter_32bppOptimized iFBlitter_32bppOptimized;
 
@@ -182,16 +183,12 @@ inline void Blitter_32bppOptimized::Draw
 
						src_px++;
 
						src_n++;
 
					} while (--n != 0);
 
					break;
 

	
 
				case BM_TRANSPARENT:
 
					/* TODO -- We make an assumption here that the remap in fact is transparency, not some colour.
 
					 *  This is never a problem with the code we produce, but newgrfs can make it fail... or at least:
 
					 *  we produce a result the newgrf maker didn't expect ;) */
 

	
 
					/* Make the current colour a bit more black, so it looks like this image is transparent */
 
					src_n += n;
 
					if (src_px->a == 255) {
 
						src_px += n;
 
						do {
 
							*dst = MakeTransparent(*dst, 3, 4);
 
@@ -203,12 +200,27 @@ inline void Blitter_32bppOptimized::Draw
 
							dst++;
 
							src_px++;
 
						} while (--n != 0);
 
					}
 
					break;
 

	
 
				case BM_TRANSPARENT_REMAP:
 
					/* Apply custom transparency remap. */
 
					src_n += n;
 
					if (src_px->a != 0) {
 
						src_px += n;
 
						do {
 
							*dst = this->LookupColourInPalette(bp->remap[GetNearestColourIndex(*dst)]);
 
							dst++;
 
						} while (--n != 0);
 
					} else {
 
						dst += n;
 
						src_px += n;
 
					}
 
					break;
 

	
 
				default:
 
					if (src_px->a == 255) {
 
						/* faster than memcpy(), n is usually low */
 
						do {
 
							if (Tpal_to_rgb && *src_n != 0) {
 
								/* Convert the mapping channel to a RGB value */
 
@@ -249,12 +261,13 @@ void Blitter_32bppOptimized::Draw(Blitte
 
{
 
	switch (mode) {
 
		default: NOT_REACHED();
 
		case BM_NORMAL:       Draw<BM_NORMAL, Tpal_to_rgb>(bp, zoom); return;
 
		case BM_COLOUR_REMAP: Draw<BM_COLOUR_REMAP, Tpal_to_rgb>(bp, zoom); return;
 
		case BM_TRANSPARENT:  Draw<BM_TRANSPARENT, Tpal_to_rgb>(bp, zoom); return;
 
		case BM_TRANSPARENT_REMAP: Draw<BM_TRANSPARENT_REMAP, Tpal_to_rgb>(bp, zoom); return;
 
		case BM_CRASH_REMAP:  Draw<BM_CRASH_REMAP, Tpal_to_rgb>(bp, zoom); return;
 
		case BM_BLACK_REMAP:  Draw<BM_BLACK_REMAP, Tpal_to_rgb>(bp, zoom); return;
 
	}
 
}
 

	
 
template void Blitter_32bppOptimized::Draw<true>(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom);
src/blitter/32bpp_simple.cpp
Show inline comments
 
@@ -6,12 +6,13 @@
 
 */
 

	
 
/** @file 32bpp_simple.cpp Implementation of the simple 32 bpp blitter. */
 

	
 
#include "../stdafx.h"
 
#include "../zoom_func.h"
 
#include "../palette_func.h"
 
#include "32bpp_simple.hpp"
 

	
 
#include "../table/sprites.h"
 

	
 
#include "../safeguards.h"
 

	
 
@@ -60,18 +61,23 @@ void Blitter_32bppSimple::Draw(Blitter::
 
					if (src->a != 0) {
 
						*dst = Colour(0, 0, 0);
 
					}
 
					break;
 

	
 
				case BM_TRANSPARENT:
 
					/* TODO -- We make an assumption here that the remap in fact is transparency, not some colour.
 
					 *  This is never a problem with the code we produce, but newgrfs can make it fail... or at least:
 
					 *  we produce a result the newgrf maker didn't expect ;) */
 
					/* Make the current colour a bit more black, so it looks like this image is transparent */
 
					if (src->a != 0) {
 
						*dst = MakeTransparent(*dst, 192);
 
					}
 
					break;
 

	
 
					/* Make the current colour a bit more black, so it looks like this image is transparent */
 
					if (src->a != 0) *dst = MakeTransparent(*dst, 192);
 
				case BM_TRANSPARENT_REMAP:
 
					/* Apply custom transparency remap. */
 
					if (src->a != 0) {
 
						*dst = this->LookupColourInPalette(bp->remap[GetNearestColourIndex(*dst)]);
 
					}
 
					break;
 

	
 
				default:
 
					if (src->a != 0) *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst);
 
					break;
 
			}
src/blitter/32bpp_sse_func.hpp
Show inline comments
 
@@ -389,12 +389,24 @@ bmcr_alpha_blend_single:
 
					__m128i srcABCD = _mm_cvtsi32_si128(src->data);
 
					__m128i dstABCD = _mm_cvtsi32_si128(dst->data);
 
					dst->data = _mm_cvtsi128_si32(DarkenTwoPixels(srcABCD, dstABCD, DARKEN_PARAM_1, DARKEN_PARAM_2));
 
				}
 
				break;
 

	
 
			case BM_TRANSPARENT_REMAP:
 
				/* Apply custom transparency remap. */
 
				for (uint x = (uint) bp->width; x > 0; x--) {
 
					if (src->a != 0) {
 
						*dst = this->LookupColourInPalette(bp->remap[GetNearestColourIndex(*dst)]);
 
					}
 
					src_mv++;
 
					dst++;
 
					src++;
 
				}
 
				break;
 

	
 
			case BM_CRASH_REMAP:
 
				for (uint x = (uint) bp->width; x > 0; x--) {
 
					if (src_mv->m == 0) {
 
						if (src->a != 0) {
 
							uint8_t g = MakeDark(src->r, src->g, src->b);
 
							*dst = ComposeColourRGBA(g, g, g, src->a, *dst);
 
@@ -468,12 +480,13 @@ bm_normal:
 
			if (bp->skip_left != 0 || bp->width <= MARGIN_REMAP_THRESHOLD) {
 
				Draw<BM_COLOUR_REMAP, RM_WITH_SKIP, BT_NONE, true>(bp, zoom); return;
 
			} else {
 
				Draw<BM_COLOUR_REMAP, RM_WITH_MARGIN, BT_NONE, true>(bp, zoom); return;
 
			}
 
		case BM_TRANSPARENT:  Draw<BM_TRANSPARENT, RM_NONE, BT_NONE, true>(bp, zoom); return;
 
		case BM_TRANSPARENT_REMAP: Draw<BM_TRANSPARENT_REMAP, RM_NONE, BT_NONE, true>(bp, zoom); return;
 
		case BM_CRASH_REMAP:  Draw<BM_CRASH_REMAP, RM_NONE, BT_NONE, true>(bp, zoom); return;
 
		case BM_BLACK_REMAP:  Draw<BM_BLACK_REMAP, RM_NONE, BT_NONE, true>(bp, zoom); return;
 
	}
 
}
 
#endif /* FULL_ANIMATION */
 

	
src/blitter/40bpp_anim.cpp
Show inline comments
 
@@ -8,12 +8,13 @@
 
/** @file 40bpp_optimized.cpp Implementation of the optimized 40 bpp blitter. */
 

	
 
#include "../stdafx.h"
 
#include "../zoom_func.h"
 
#include "../settings_type.h"
 
#include "../video/video_driver.hpp"
 
#include "../palette_func.h"
 
#include "40bpp_anim.hpp"
 
#include "common.hpp"
 

	
 
#include "../table/sprites.h"
 

	
 
#include "../safeguards.h"
 
@@ -231,16 +232,12 @@ inline void Blitter_40bppAnim::Draw(cons
 
						src_px++;
 
						src_n++;
 
					} while (--n != 0);
 
					break;
 

	
 
				case BM_TRANSPARENT:
 
					/* TODO -- We make an assumption here that the remap in fact is transparency, not some colour.
 
					 *  This is never a problem with the code we produce, but newgrfs can make it fail... or at least:
 
					 *  we produce a result the newgrf maker didn't expect ;) */
 

	
 
					/* Make the current colour a bit more black, so it looks like this image is transparent */
 
					src_n += n;
 
					if (src_px->a == 255) {
 
						src_px += n;
 
						do {
 
							/* If the anim buffer contains a color value, the image composition will
 
@@ -260,12 +257,34 @@ inline void Blitter_40bppAnim::Draw(cons
 
							dst++;
 
							src_px++;
 
						} while (--n != 0);
 
					}
 
					break;
 

	
 
				case BM_TRANSPARENT_REMAP:
 
					/* Apply custom transparency remap. */
 
					src_n += n;
 
					if (src_px->a != 0) {
 
						src_px += n;
 
						do {
 
							if (*anim != 0) {
 
								*anim = bp->remap[*anim];
 
							} else {
 
								*dst = this->LookupColourInPalette(bp->remap[GetNearestColourIndex(*dst)]);
 
								*anim = 0;
 
							}
 
							anim++;
 
							dst++;
 
						} while (--n != 0);
 
					} else {
 
						dst += n;
 
						anim += n;
 
						src_px += n;
 
					}
 
					break;
 

	
 
				default:
 
					if (src_px->a == 255) {
 
						do {
 
							*anim++ = GB(*src_n, 0, 8);
 
							*dst++ = src_px->data;
 
							src_px++;
 
@@ -320,12 +339,13 @@ void Blitter_40bppAnim::Draw(Blitter::Bl
 

	
 
	switch (mode) {
 
		default: NOT_REACHED();
 
		case BM_NORMAL:       Draw<BM_NORMAL>      (bp, zoom); return;
 
		case BM_COLOUR_REMAP: Draw<BM_COLOUR_REMAP>(bp, zoom); return;
 
		case BM_TRANSPARENT:  Draw<BM_TRANSPARENT> (bp, zoom); return;
 
		case BM_TRANSPARENT_REMAP: Draw<BM_TRANSPARENT_REMAP>(bp, zoom); return;
 
		case BM_CRASH_REMAP:  Draw<BM_CRASH_REMAP> (bp, zoom); return;
 
		case BM_BLACK_REMAP:  Draw<BM_BLACK_REMAP> (bp, zoom); return;
 
	}
 
}
 

	
 
void Blitter_40bppAnim::DrawColourMappingRect(void *dst, int width, int height, PaletteID pal)
src/blitter/8bpp_optimized.cpp
Show inline comments
 
@@ -97,13 +97,14 @@ void Blitter_8bppOptimized::Draw(Blitter
 

	
 
				case BM_BLACK_REMAP:
 
					MemSetT(dst, 0, pixels);
 
					dst += pixels;
 
					break;
 

	
 
				case BM_TRANSPARENT: {
 
				case BM_TRANSPARENT:
 
				case BM_TRANSPARENT_REMAP: {
 
					const uint8_t *remap = bp->remap;
 
					src += pixels;
 
					do {
 
						*dst = remap[*dst];
 
						dst++;
 
					} while (--pixels != 0);
src/blitter/8bpp_simple.cpp
Show inline comments
 
@@ -39,12 +39,13 @@ void Blitter_8bppSimple::Draw(Blitter::B
 
				case BM_COLOUR_REMAP:
 
				case BM_CRASH_REMAP:
 
					colour = bp->remap[*src];
 
					break;
 

	
 
				case BM_TRANSPARENT:
 
				case BM_TRANSPARENT_REMAP:
 
					if (*src != 0) colour = bp->remap[*dst];
 
					break;
 

	
 
				case BM_BLACK_REMAP:
 
					if (*src != 0) *dst = 0;
 
					break;
src/blitter/base.hpp
Show inline comments
 
@@ -14,13 +14,14 @@
 
#include "../spriteloader/spriteloader.hpp"
 

	
 
/** The modes of blitting we can do. */
 
enum BlitterMode {
 
	BM_NORMAL,       ///< Perform the simple blitting.
 
	BM_COLOUR_REMAP, ///< Perform a colour remapping.
 
	BM_TRANSPARENT,  ///< Perform transparency colour remapping.
 
	BM_TRANSPARENT,  ///< Perform transparency darkening remapping.
 
	BM_TRANSPARENT_REMAP, ///< Perform transparency colour remapping.
 
	BM_CRASH_REMAP,  ///< Perform a crash remapping.
 
	BM_BLACK_REMAP,  ///< Perform remapping to a completely blackened sprite
 
};
 

	
 
/**
 
 * How all blitters should look like. Extend this class to make your own.
src/gfx.cpp
Show inline comments
 
@@ -975,14 +975,15 @@ static BlitterMode GetBlitterMode(Palett
 
 * @param sub  If available, draw only specified part of the sprite
 
 */
 
void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub)
 
{
 
	SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
 
	if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
 
		_colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), SpriteType::Recolour) + 1;
 
		GfxMainBlitterViewport(GetSprite(real_sprite, SpriteType::Normal), x, y, BM_TRANSPARENT, sub, real_sprite);
 
		pal = GB(pal, 0, PALETTE_WIDTH);
 
		_colour_remap_ptr = GetNonSprite(pal, SpriteType::Recolour) + 1;
 
		GfxMainBlitterViewport(GetSprite(real_sprite, SpriteType::Normal), x, y, pal == PALETTE_TO_TRANSPARENT ? BM_TRANSPARENT : BM_TRANSPARENT_REMAP, sub, real_sprite);
 
	} else if (pal != PAL_NONE) {
 
		if (HasBit(pal, PALETTE_TEXT_RECOLOUR)) {
 
			SetColourRemap((TextColour)GB(pal, 0, PALETTE_WIDTH));
 
		} else {
 
			_colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), SpriteType::Recolour) + 1;
 
		}
 
@@ -1002,14 +1003,15 @@ void DrawSpriteViewport(SpriteID img, Pa
 
 * @param zoom Zoom level of sprite
 
 */
 
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
 
{
 
	SpriteID real_sprite = GB(img, 0, SPRITE_WIDTH);
 
	if (HasBit(img, PALETTE_MODIFIER_TRANSPARENT)) {
 
		_colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), SpriteType::Recolour) + 1;
 
		GfxMainBlitter(GetSprite(real_sprite, SpriteType::Normal), x, y, BM_TRANSPARENT, sub, real_sprite, zoom);
 
		pal = GB(pal, 0, PALETTE_WIDTH);
 
		_colour_remap_ptr = GetNonSprite(pal, SpriteType::Recolour) + 1;
 
		GfxMainBlitter(GetSprite(real_sprite, SpriteType::Normal), x, y, pal == PALETTE_TO_TRANSPARENT ? BM_TRANSPARENT : BM_TRANSPARENT_REMAP, sub, real_sprite, zoom);
 
	} else if (pal != PAL_NONE) {
 
		if (HasBit(pal, PALETTE_TEXT_RECOLOUR)) {
 
			SetColourRemap((TextColour)GB(pal, 0, PALETTE_WIDTH));
 
		} else {
 
			_colour_remap_ptr = GetNonSprite(GB(pal, 0, PALETTE_WIDTH), SpriteType::Recolour) + 1;
 
		}
0 comments (0 inline, 0 general)