Simple but fast 16bpp renderer

This commit is contained in:
Sergii Pylypenko
2014-05-27 20:42:23 +03:00
parent fed40b76ff
commit e4e3db8a85
5 changed files with 542 additions and 0 deletions

View File

@@ -904,6 +904,12 @@ script/api/script_window.cpp
# Blitters
#if DEDICATED
#else
blitter/16bpp_base.cpp
blitter/16bpp_base.hpp
# blitter/16bpp_anim.cpp
# blitter/16bpp_anim.hpp
blitter/16bpp_simple.cpp
blitter/16bpp_simple.hpp
blitter/32bpp_anim.cpp
blitter/32bpp_anim.hpp
#if SSE

147
src/blitter/16bpp_base.cpp Normal file
View File

@@ -0,0 +1,147 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* 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 16bpp_base.cpp Implementation of base for 32 bpp blitters. */
#include "../stdafx.h"
#include "16bpp_base.hpp"
void *Blitter_16bppBase::MoveTo(void *video, int x, int y)
{
return (uint16 *)video + x + y * _screen.pitch;
}
void Blitter_16bppBase::SetPixel(void *video, int x, int y, uint8 colour)
{
*((Colour16 *)video + x + y * _screen.pitch) = LookupColourInPalette(colour);
}
void Blitter_16bppBase::DrawRect(void *video, int width, int height, uint8 colour)
{
Colour16 target = LookupColourInPalette(colour);
do {
Colour16 *dst = (Colour16 *)video;
for (int i = width; i > 0; i--) {
*dst = target;
dst++;
}
video = (uint16 *)video + _screen.pitch;
} while (--height);
}
void Blitter_16bppBase::CopyFromBuffer(void *video, const void *src, int width, int height)
{
uint16 *dst = (uint16 *)video;
const uint16 *usrc = (const uint16 *)src;
for (; height > 0; height--) {
memcpy(dst, usrc, width * sizeof(uint16));
usrc += width;
dst += _screen.pitch;
}
}
void Blitter_16bppBase::CopyToBuffer(const void *video, void *dst, int width, int height)
{
uint16 *udst = (uint16 *)dst;
const uint16 *src = (const uint16 *)video;
for (; height > 0; height--) {
memcpy(udst, src, width * sizeof(uint16));
src += _screen.pitch;
udst += width;
}
}
void Blitter_16bppBase::CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch)
{
uint16 *udst = (uint16 *)dst;
const uint16 *src = (const uint16 *)video;
for (; height > 0; height--) {
memcpy(udst, src, width * sizeof(uint16));
src += _screen.pitch;
udst += dst_pitch;
}
}
void Blitter_16bppBase::ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y)
{
const uint16 *src;
uint16 *dst;
if (scroll_y > 0) {
/* Calculate pointers */
dst = (uint16 *)video + left + (top + height - 1) * _screen.pitch;
src = dst - scroll_y * _screen.pitch;
/* Decrease height and increase top */
top += scroll_y;
height -= scroll_y;
assert(height > 0);
/* Adjust left & width */
if (scroll_x >= 0) {
dst += scroll_x;
left += scroll_x;
width -= scroll_x;
} else {
src -= scroll_x;
width += scroll_x;
}
for (int h = height; h > 0; h--) {
memcpy(dst, src, width * sizeof(uint16));
src -= _screen.pitch;
dst -= _screen.pitch;
}
} else {
/* Calculate pointers */
dst = (uint16 *)video + left + top * _screen.pitch;
src = dst - scroll_y * _screen.pitch;
/* Decrease height. (scroll_y is <=0). */
height += scroll_y;
assert(height > 0);
/* Adjust left & width */
if (scroll_x >= 0) {
dst += scroll_x;
left += scroll_x;
width -= scroll_x;
} else {
src -= scroll_x;
width += scroll_x;
}
/* the y-displacement may be 0 therefore we have to use memmove,
* because source and destination may overlap */
for (int h = height; h > 0; h--) {
memmove(dst, src, width * sizeof(uint16));
src += _screen.pitch;
dst += _screen.pitch;
}
}
}
int Blitter_16bppBase::BufferSize(int width, int height)
{
return width * height * sizeof(uint16);
}
void Blitter_16bppBase::PaletteAnimate(const Palette &palette)
{
/* By default, 16bpp doesn't have palette animation */
}
Blitter::PaletteAnimation Blitter_16bppBase::UsePaletteAnimation()
{
return Blitter::PALETTE_ANIMATION_NONE;
}

208
src/blitter/16bpp_base.hpp Normal file
View File

@@ -0,0 +1,208 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* 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 16bpp_base.hpp Base for all 16 bits blitters. */
#ifndef BLITTER_16BPP_BASE_HPP
#define BLITTER_16BPP_BASE_HPP
#include "base.hpp"
#include "../core/bitmath_func.hpp"
#include "../core/math_func.hpp"
#include "../gfx_func.h"
/** Base for all 16bpp blitters. */
class Blitter_16bppBase : public Blitter {
public:
// TODO: GCC-specific attributes
struct Colour16 {
unsigned b : 5 __attribute__((packed)); ///< Blue-channel, packed 5 bits
unsigned g : 6 __attribute__((packed)); ///< Green-channel, packed 6 bits
unsigned r : 5 __attribute__((packed)); ///< Red-channel, packed 5 bits
Colour16(uint8 r, uint8 g, uint8 b):
b(b), g(g), r(r)
{
}
};
struct Pixel {
Colour16 c;
unsigned a : 4 __attribute__((packed)); ///< Alpha-channel, packed 4 bits
unsigned v : 4 __attribute__((packed)); ///< Brightness-channel, packed 4 bits
unsigned m : 8 __attribute__((packed)); ///< Remap-channel, cannot pack it, because it's palette lookup index, so it must be in range 0-255
};
/* virtual */ uint8 GetScreenDepth() { return 16; }
/* virtual */ void *MoveTo(void *video, int x, int y);
/* virtual */ void SetPixel(void *video, int x, int y, uint8 colour);
/* virtual */ void DrawRect(void *video, int width, int height, uint8 colour);
/* virtual */ void CopyFromBuffer(void *video, const void *src, int width, int height);
/* virtual */ void CopyToBuffer(const void *video, void *dst, int width, int height);
/* virtual */ void CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch);
/* virtual */ void ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y);
/* virtual */ int BufferSize(int width, int height);
/* virtual */ void PaletteAnimate(const Palette &palette);
/* virtual */ Blitter::PaletteAnimation UsePaletteAnimation();
/* virtual */ int GetBytesPerPixel() { return 2; }
/**
* Convert from rgb values to screen native 16bpp colour
*/
static inline Colour16 To16(uint8 r, uint8 g, uint8 b)
{
return Colour16(r >> 3, g >> 2, b >> 3);
}
/**
* Convert from 32bpp colour to screen native 16bpp colour
*/
static inline Colour16 To16(Colour c)
{
return To16(c.r, c.g, c.b);
}
/**
* Look up the colour in the current palette.
*/
static inline Colour LookupColourInPalette32(uint index)
{
return _cur_palette.palette[index];
}
/**
* Look up the colour in the current palette.
*/
static inline Colour16 LookupColourInPalette(uint index)
{
return To16(LookupColourInPalette32(index));
}
/**
* Compose a colour based on RGBA values and the current pixel value.
* @param r range is from 0 to 31.
* @param g range is from 0 to 63.
* @param b range is from 0 to 31.
* @param a range is from 0 to 15.
*/
static inline Colour16 ComposeColourRGBANoCheck(uint8 r, uint8 g, uint8 b, uint8 a, Colour16 current)
{
/* The 16 is wrong, it should be 15, but 16 is much faster... */
return Colour16 ( ((int)(r - current.r) * a) / 16 + current.r,
((int)(g - current.g) * a) / 16 + current.g,
((int)(b - current.b) * a) / 16 + current.b );
}
/**
* Compose a colour based on RGBA values and the current pixel value.
* Handles fully transparent and solid pixels in a special (faster) way.
* @param r range is from 0 to 31.
* @param g range is from 0 to 63.
* @param b range is from 0 to 31.
* @param a range is from 0 to 15.
*/
static inline Colour16 ComposeColourRGBA(uint8 r, uint8 g, uint8 b, uint8 a, Colour16 current)
{
if (a == 0) return current;
if (a >= 15) return Colour16(r, g, b);
return ComposeColourRGBANoCheck(r, g, b, a, current);
}
/**
* Compose a colour based on Pixel value, alpha value, and the current pixel value.
* @param a range is from 0 to 16.
*/
static inline Colour16 ComposeColourPANoCheck(Colour16 colour, uint8 a, Colour16 current)
{
return ComposeColourRGBANoCheck(colour.r, colour.g, colour.b, a, current);
}
/**
* Compose a colour based on Pixel value, alpha value, and the current pixel value.
* Handles fully transparent and solid pixels in a special (faster) way.
* @param a range is from 0 to 15.
*/
static inline Colour16 ComposeColourPA(Colour16 colour, uint8 a, Colour16 current)
{
if (a == 0) return current;
if (a >= 15) return colour;
return ComposeColourPANoCheck(colour, a, current);
}
/**
* Make a pixel looks like it is transparent.
* @param colour the colour already on the screen.
* @param nom the amount of transparency, nominator, makes colour lighter.
* @param denom denominator, makes colour darker.
* @return the new colour for the screen.
*/
static inline Colour16 MakeTransparent(Colour16 colour, uint nom, uint denom = 256)
{
uint r = colour.r;
uint g = colour.g;
uint b = colour.b;
return Colour16( r * nom / denom,
g * nom / denom,
b * nom / denom );
}
/**
* Make a colour grey - based.
* @param colour the colour to make grey.
* @return the new colour, now grey.
*/
static inline Colour16 MakeGrey(Colour16 colour)
{
uint r = colour.r;
uint g = colour.g;
uint b = colour.b;
/* To avoid doubles and stuff, multiple it with a total of 65536 (16bits), then
* divide by it to normalize the value to a byte again. See heightmap.cpp for
* information about the formula. */
uint grey = (((r << 3) * 19595) + ((g << 2) * 38470) + ((b << 3) * 7471)) / 65536;
return To16(grey, grey, grey);
}
enum { DEFAULT_BRIGHTNESS = 8 };
/**
* @param brightness range is from 0 to 15.
*/
static inline Colour16 AdjustBrightness(Colour16 colour, uint8 brightness)
{
/* Shortcut for normal brightness */
if (brightness == DEFAULT_BRIGHTNESS) return colour;
uint16 ob = 0;
uint16 r = colour.r * brightness / DEFAULT_BRIGHTNESS;
uint16 g = colour.g * brightness / DEFAULT_BRIGHTNESS;
uint16 b = colour.b * brightness / DEFAULT_BRIGHTNESS;
/* Sum overbright */
if (r > 31) ob += r - 31;
if (g > 63) ob += g - 63;
if (b > 31) ob += b - 31;
if (ob == 0) return Colour16(r, g, b);
/* Reduce overbright strength */
ob /= 2;
return Colour16( r >= 31 ? 31 : min(r + ob * (31 - r) / 32, 31),
g >= 63 ? 63 : min(g + ob * (63 - g) / 64, 63),
b >= 31 ? 31 : min(b + ob * (31 - b) / 32, 31) );
}
};
#endif /* BLITTER_16BPP_BASE_HPP */

View File

@@ -0,0 +1,146 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* 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_simple.cpp Implementation of the simple 32 bpp blitter. */
#include "../stdafx.h"
#include "../zoom_func.h"
#include "16bpp_simple.hpp"
#include "32bpp_base.hpp"
#include "../table/sprites.h"
#include <SDL_video.h>
/** Instantiation of the simple 16bpp blitter factory. */
static FBlitter_16bppSimple iFBlitter_16bppSimple;
void Blitter_16bppSimple::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
{
const Pixel *src, *src_line;
Colour16 *dst, *dst_line;
/* Find where to start reading in the source sprite */
src_line = (const Pixel *)bp->sprite + (bp->skip_top * bp->sprite_width + bp->skip_left) * ScaleByZoom(1, zoom);
dst_line = (Colour16 *)bp->dst + bp->top * bp->pitch + bp->left;
for (int y = 0; y < bp->height; y++) {
dst = dst_line;
dst_line += bp->pitch;
src = src_line;
src_line += bp->sprite_width * ScaleByZoom(1, zoom);
for (int x = 0; x < bp->width; x++) {
switch (mode) {
case BM_COLOUR_REMAP:
/* In case the m-channel is zero, do not remap this pixel in any way */
if (src->m == 0) {
if (src->a != 0) *dst = ComposeColourPA(src->c, src->a, *dst);
} else {
if (bp->remap[src->m] != 0) *dst = ComposeColourPA(this->AdjustBrightness(this->LookupColourInPalette(bp->remap[src->m]), src->v), src->a, *dst);
}
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;
default:
if (src->a != 0) *dst = ComposeColourPA(src->c, src->a, *dst);
break;
}
dst++;
src += ScaleByZoom(1, zoom);
}
}
}
void Blitter_16bppSimple::DrawColourMappingRect(void *dst, int width, int height, PaletteID pal)
{
Colour16 *udst = (Colour16 *)dst;
if (pal == PALETTE_TO_TRANSPARENT) {
do {
for (int i = 0; i != width; i++) {
*udst = MakeTransparent(*udst, 154);
udst++;
}
udst = udst - width + _screen.pitch;
} while (--height);
return;
}
if (pal == PALETTE_NEWSPAPER) {
do {
for (int i = 0; i != width; i++) {
*udst = MakeGrey(*udst);
udst++;
}
udst = udst - width + _screen.pitch;
} while (--height);
return;
}
DEBUG(misc, 0, "16bpp blitter doesn't know how to draw this colour table ('%d')", pal);
}
Sprite *Blitter_16bppSimple::Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator)
{
Pixel *dst;
Sprite *dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + (size_t)sprite->height * (size_t)sprite->width * sizeof(Pixel));
dest_sprite->height = sprite->height;
dest_sprite->width = sprite->width;
dest_sprite->x_offs = sprite->x_offs;
dest_sprite->y_offs = sprite->y_offs;
dst = (Pixel *)dest_sprite->data;
SpriteLoader::CommonPixel *src = (SpriteLoader::CommonPixel *)sprite->data;
for (int i = 0; i < sprite->height * sprite->width; i++) {
if (src->m == 0) {
dst[i].c = To16(src->r, src->g, src->b);
dst[i].a = src->a / 16;
dst[i].m = 0;
dst[i].v = 0;
} else {
/* Get brightest value */
uint8 rgb_max = max(src->r, max(src->g, src->b));
#if 0
/* Pre-convert the mapping channel to a RGB value,
use 32bpp AdjustBrightness() variant for better colors,
because this function is not called each frame */
if (rgb_max == 0) rgb_max = Blitter_32bppBase::DEFAULT_BRIGHTNESS;
dst[i].c = To16(Blitter_32bppBase::AdjustBrightness(this->LookupColourInPalette32(src->m), rgb_max));
dst[i].v = rgb_max / 16;
#endif
rgb_max /= 16;
/* Black pixel (8bpp or old 16bpp image), so use default value */
if (rgb_max == 0) rgb_max = DEFAULT_BRIGHTNESS;
/* Pre-convert the mapping channel to a RGB value,
use 32bpp AdjustBrightness() variant for better colors,
because this function is not called each frame */
dst[i].c = AdjustBrightness(this->LookupColourInPalette(src->m), rgb_max);
dst[i].v = rgb_max;
dst[i].a = src->a / 16;
dst[i].m = src->m;
}
src++;
}
return dest_sprite;
}

View File

@@ -0,0 +1,35 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* 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 16bpp_simple.hpp Simple 16 bpp blitter. */
#ifndef BLITTER_16BPP_SIMPLE_HPP
#define BLITTER_16BPP_SIMPLE_HPP
#include "16bpp_base.hpp"
#include "factory.hpp"
/** The most trivial 32 bpp blitter (without palette animation). */
class Blitter_16bppSimple : public Blitter_16bppBase {
public:
/* virtual */ void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom);
/* virtual */ void DrawColourMappingRect(void *dst, int width, int height, PaletteID pal);
/* virtual */ Sprite *Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator);
/* virtual */ const char *GetName() { return "16bpp-simple"; }
};
/** Factory for the simple 16 bpp blitter. */
class FBlitter_16bppSimple : public BlitterFactory {
public:
FBlitter_16bppSimple() : BlitterFactory("16bpp-simple", "16bpp Simple Blitter (no palette animation)") {}
/* virtual */ Blitter *CreateInstance() { return new Blitter_16bppSimple(); }
};
#endif /* BLITTER_32BPP_SIMPLE_HPP */