OpenTTD Source 20241224-master-gf74b0cf984
32bpp_optimized.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10#include "../stdafx.h"
11#include "../zoom_func.h"
12#include "../settings_type.h"
13#include "../palette_func.h"
14#include "32bpp_optimized.hpp"
15
16#include "../safeguards.h"
17
20
28template <BlitterMode mode, bool Tpal_to_rgb>
30{
31 const SpriteData *src = (const SpriteData *)bp->sprite;
32
33 /* src_px : each line begins with uint32_t n = 'number of bytes in this line',
34 * then n times is the Colour struct for this line */
35 const Colour *src_px = (const Colour *)(src->data + src->offset[zoom][0]);
36 /* src_n : each line begins with uint32_t n = 'number of bytes in this line',
37 * then interleaved stream of 'm' and 'n' channels. 'm' is remap,
38 * 'n' is number of bytes with the same alpha channel class */
39 const uint16_t *src_n = (const uint16_t *)(src->data + src->offset[zoom][1]);
40
41 /* skip upper lines in src_px and src_n */
42 for (uint i = bp->skip_top; i != 0; i--) {
43 src_px = (const Colour *)((const uint8_t *)src_px + *(const uint32_t *)src_px);
44 src_n = (const uint16_t *)((const uint8_t *)src_n + *(const uint32_t *)src_n);
45 }
46
47 /* skip lines in dst */
48 Colour *dst = (Colour *)bp->dst + bp->top * bp->pitch + bp->left;
49
50 /* store so we don't have to access it via bp every time (compiler assumes pointer aliasing) */
51 const uint8_t *remap = bp->remap;
52
53 for (int y = 0; y < bp->height; y++) {
54 /* next dst line begins here */
55 Colour *dst_ln = dst + bp->pitch;
56
57 /* next src line begins here */
58 const Colour *src_px_ln = (const Colour *)((const uint8_t *)src_px + *(const uint32_t *)src_px);
59 src_px++;
60
61 /* next src_n line begins here */
62 const uint16_t *src_n_ln = (const uint16_t *)((const uint8_t *)src_n + *(const uint32_t *)src_n);
63 src_n += 2;
64
65 /* we will end this line when we reach this point */
66 Colour *dst_end = dst + bp->skip_left;
67
68 /* number of pixels with the same alpha channel class */
69 uint n;
70
71 while (dst < dst_end) {
72 n = *src_n++;
73
74 if (src_px->a == 0) {
75 dst += n;
76 src_px ++;
77 src_n++;
78 } else {
79 if (dst + n > dst_end) {
80 uint d = dst_end - dst;
81 src_px += d;
82 src_n += d;
83
84 dst = dst_end - bp->skip_left;
85 dst_end = dst + bp->width;
86
87 n = std::min(n - d, (uint)bp->width);
88 goto draw;
89 }
90 dst += n;
91 src_px += n;
92 src_n += n;
93 }
94 }
95
96 dst -= bp->skip_left;
97 dst_end -= bp->skip_left;
98
99 dst_end += bp->width;
100
101 while (dst < dst_end) {
102 n = std::min<uint>(*src_n++, dst_end - dst);
103
104 if (src_px->a == 0) {
105 dst += n;
106 src_px++;
107 src_n++;
108 continue;
109 }
110
111 draw:;
112
113 switch (mode) {
114 case BM_COLOUR_REMAP:
115 if (src_px->a == 255) {
116 do {
117 uint m = *src_n;
118 /* In case the m-channel is zero, do not remap this pixel in any way */
119 if (m == 0) {
120 *dst = src_px->data;
121 } else {
122 uint r = remap[GB(m, 0, 8)];
123 if (r != 0) *dst = this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8));
124 }
125 dst++;
126 src_px++;
127 src_n++;
128 } while (--n != 0);
129 } else {
130 do {
131 uint m = *src_n;
132 if (m == 0) {
133 *dst = ComposeColourRGBANoCheck(src_px->r, src_px->g, src_px->b, src_px->a, *dst);
134 } else {
135 uint r = remap[GB(m, 0, 8)];
136 if (r != 0) *dst = ComposeColourPANoCheck(this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8)), src_px->a, *dst);
137 }
138 dst++;
139 src_px++;
140 src_n++;
141 } while (--n != 0);
142 }
143 break;
144
145 case BM_CRASH_REMAP:
146 if (src_px->a == 255) {
147 do {
148 uint m = *src_n;
149 if (m == 0) {
150 uint8_t g = MakeDark(src_px->r, src_px->g, src_px->b);
151 *dst = ComposeColourRGBA(g, g, g, src_px->a, *dst);
152 } else {
153 uint r = remap[GB(m, 0, 8)];
154 if (r != 0) *dst = this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8));
155 }
156 dst++;
157 src_px++;
158 src_n++;
159 } while (--n != 0);
160 } else {
161 do {
162 uint m = *src_n;
163 if (m == 0) {
164 if (src_px->a != 0) {
165 uint8_t g = MakeDark(src_px->r, src_px->g, src_px->b);
166 *dst = ComposeColourRGBA(g, g, g, src_px->a, *dst);
167 }
168 } else {
169 uint r = remap[GB(m, 0, 8)];
170 if (r != 0) *dst = ComposeColourPANoCheck(this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8)), src_px->a, *dst);
171 }
172 dst++;
173 src_px++;
174 src_n++;
175 } while (--n != 0);
176 }
177 break;
178
179 case BM_BLACK_REMAP:
180 do {
181 *dst = Colour(0, 0, 0);
182 dst++;
183 src_px++;
184 src_n++;
185 } while (--n != 0);
186 break;
187
188 case BM_TRANSPARENT:
189 /* Make the current colour a bit more black, so it looks like this image is transparent */
190 src_n += n;
191 if (src_px->a == 255) {
192 src_px += n;
193 do {
194 *dst = MakeTransparent(*dst, 3, 4);
195 dst++;
196 } while (--n != 0);
197 } else {
198 do {
199 *dst = MakeTransparent(*dst, (256 * 4 - src_px->a), 256 * 4);
200 dst++;
201 src_px++;
202 } while (--n != 0);
203 }
204 break;
205
207 /* Apply custom transparency remap. */
208 src_n += n;
209 if (src_px->a != 0) {
210 src_px += n;
211 do {
212 *dst = this->LookupColourInPalette(remap[GetNearestColourIndex(*dst)]);
213 dst++;
214 } while (--n != 0);
215 } else {
216 dst += n;
217 src_px += n;
218 }
219 break;
220
221 default:
222 if (src_px->a == 255) {
223 /* faster than memcpy(), n is usually low */
224 do {
225 if (Tpal_to_rgb && *src_n != 0) {
226 /* Convert the mapping channel to a RGB value */
227 *dst = this->AdjustBrightness(this->LookupColourInPalette(GB(*src_n, 0, 8)), GB(*src_n, 8, 8)).data;
228 } else {
229 *dst = src_px->data;
230 }
231 dst++;
232 src_px++;
233 src_n++;
234 } while (--n != 0);
235 } else {
236 do {
237 if (Tpal_to_rgb && *src_n != 0) {
238 /* Convert the mapping channel to a RGB value */
239 Colour colour = this->AdjustBrightness(this->LookupColourInPalette(GB(*src_n, 0, 8)), GB(*src_n, 8, 8));
240 *dst = ComposeColourRGBANoCheck(colour.r, colour.g, colour.b, src_px->a, *dst);
241 } else {
242 *dst = ComposeColourRGBANoCheck(src_px->r, src_px->g, src_px->b, src_px->a, *dst);
243 }
244 dst++;
245 src_px++;
246 src_n++;
247 } while (--n != 0);
248 }
249 break;
250 }
251 }
252
253 dst = dst_ln;
254 src_px = src_px_ln;
255 src_n = src_n_ln;
256 }
257}
258
259template <bool Tpal_to_rgb>
261{
262 switch (mode) {
263 default: NOT_REACHED();
264 case BM_NORMAL: Draw<BM_NORMAL, Tpal_to_rgb>(bp, zoom); return;
265 case BM_COLOUR_REMAP: Draw<BM_COLOUR_REMAP, Tpal_to_rgb>(bp, zoom); return;
266 case BM_TRANSPARENT: Draw<BM_TRANSPARENT, Tpal_to_rgb>(bp, zoom); return;
267 case BM_TRANSPARENT_REMAP: Draw<BM_TRANSPARENT_REMAP, Tpal_to_rgb>(bp, zoom); return;
268 case BM_CRASH_REMAP: Draw<BM_CRASH_REMAP, Tpal_to_rgb>(bp, zoom); return;
269 case BM_BLACK_REMAP: Draw<BM_BLACK_REMAP, Tpal_to_rgb>(bp, zoom); return;
270 }
271}
272
273template void Blitter_32bppOptimized::Draw<true>(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom);
274template void Blitter_32bppOptimized::Draw<false>(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom);
275
284{
285 this->Draw<false>(bp, mode, zoom);
286}
287
288template <bool Tpal_to_rgb> Sprite *Blitter_32bppOptimized::EncodeInternal(const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator)
289{
290 /* streams of pixels (a, r, g, b channels)
291 *
292 * stored in separated stream so data are always aligned on 4B boundary */
293 Colour *dst_px_orig[ZOOM_LVL_END];
294
295 /* interleaved stream of 'm' channel and 'n' channel
296 * 'n' is number of following pixels with the same alpha channel class
297 * there are 3 classes: 0, 255, others
298 *
299 * it has to be stored in one stream so fewer registers are used -
300 * x86 has problems with register allocation even with this solution */
301 uint16_t *dst_n_orig[ZOOM_LVL_END];
302
303 /* lengths of streams */
304 uint32_t lengths[ZOOM_LVL_END][2];
305
306 ZoomLevel zoom_min;
307 ZoomLevel zoom_max;
308
309 if (sprite[ZOOM_LVL_MIN].type == SpriteType::Font) {
310 zoom_min = ZOOM_LVL_MIN;
311 zoom_max = ZOOM_LVL_MIN;
312 } else {
313 zoom_min = _settings_client.gui.zoom_min;
314 zoom_max = _settings_client.gui.zoom_max;
315 if (zoom_max == zoom_min) zoom_max = ZOOM_LVL_MAX;
316 }
317
318 for (ZoomLevel z = zoom_min; z <= zoom_max; z++) {
319 const SpriteLoader::Sprite *src_orig = &sprite[z];
320
321 uint size = src_orig->height * src_orig->width;
322
323 dst_px_orig[z] = CallocT<Colour>(size + src_orig->height * 2);
324 dst_n_orig[z] = CallocT<uint16_t>(size * 2 + src_orig->height * 4 * 2);
325
326 uint32_t *dst_px_ln = (uint32_t *)dst_px_orig[z];
327 uint32_t *dst_n_ln = (uint32_t *)dst_n_orig[z];
328
329 const SpriteLoader::CommonPixel *src = (const SpriteLoader::CommonPixel *)src_orig->data;
330
331 for (uint y = src_orig->height; y > 0; y--) {
332 /* Index 0 of dst_px and dst_n is left as space to save the length of the row to be filled later. */
333 Colour *dst_px = (Colour *)&dst_px_ln[1];
334 uint16_t *dst_n = (uint16_t *)&dst_n_ln[1];
335
336 uint16_t *dst_len = dst_n++;
337
338 uint last = 3;
339 int len = 0;
340
341 for (uint x = src_orig->width; x > 0; x--) {
342 uint8_t a = src->a;
343 uint t = a > 0 && a < 255 ? 1 : a;
344
345 if (last != t || len == 65535) {
346 if (last != 3) {
347 *dst_len = len;
348 dst_len = dst_n++;
349 }
350 len = 0;
351 }
352
353 last = t;
354 len++;
355
356 if (a != 0) {
357 dst_px->a = a;
358 *dst_n = src->m;
359 if (src->m != 0) {
360 /* Get brightest value */
361 uint8_t rgb_max = std::max({ src->r, src->g, src->b });
362
363 /* Black pixel (8bpp or old 32bpp image), so use default value */
364 if (rgb_max == 0) rgb_max = DEFAULT_BRIGHTNESS;
365 *dst_n |= rgb_max << 8;
366
367 if (Tpal_to_rgb) {
368 /* Pre-convert the mapping channel to a RGB value */
369 Colour colour = this->AdjustBrightness(this->LookupColourInPalette(src->m), rgb_max);
370 dst_px->r = colour.r;
371 dst_px->g = colour.g;
372 dst_px->b = colour.b;
373 } else {
374 dst_px->r = src->r;
375 dst_px->g = src->g;
376 dst_px->b = src->b;
377 }
378 } else {
379 dst_px->r = src->r;
380 dst_px->g = src->g;
381 dst_px->b = src->b;
382 }
383 dst_px++;
384 dst_n++;
385 } else if (len == 1) {
386 dst_px++;
387 *dst_n = src->m;
388 dst_n++;
389 }
390
391 src++;
392 }
393
394 if (last != 3) {
395 *dst_len = len;
396 }
397
398 dst_px = (Colour *)AlignPtr(dst_px, 4);
399 dst_n = (uint16_t *)AlignPtr(dst_n, 4);
400
401 *dst_px_ln = (uint8_t *)dst_px - (uint8_t *)dst_px_ln;
402 *dst_n_ln = (uint8_t *)dst_n - (uint8_t *)dst_n_ln;
403
404 dst_px_ln = (uint32_t *)dst_px;
405 dst_n_ln = (uint32_t *)dst_n;
406 }
407
408 lengths[z][0] = (uint8_t *)dst_px_ln - (uint8_t *)dst_px_orig[z]; // all are aligned to 4B boundary
409 lengths[z][1] = (uint8_t *)dst_n_ln - (uint8_t *)dst_n_orig[z];
410 }
411
412 uint len = 0; // total length of data
413 for (ZoomLevel z = zoom_min; z <= zoom_max; z++) {
414 len += lengths[z][0] + lengths[z][1];
415 }
416
417 Sprite *dest_sprite = allocator.Allocate<Sprite>(sizeof(*dest_sprite) + sizeof(SpriteData) + len);
418
419 dest_sprite->height = sprite[ZOOM_LVL_MIN].height;
420 dest_sprite->width = sprite[ZOOM_LVL_MIN].width;
421 dest_sprite->x_offs = sprite[ZOOM_LVL_MIN].x_offs;
422 dest_sprite->y_offs = sprite[ZOOM_LVL_MIN].y_offs;
423
424 SpriteData *dst = (SpriteData *)dest_sprite->data;
425 memset(dst, 0, sizeof(*dst));
426
427 for (ZoomLevel z = zoom_min; z <= zoom_max; z++) {
428 dst->offset[z][0] = z == zoom_min ? 0 : lengths[z - 1][1] + dst->offset[z - 1][1];
429 dst->offset[z][1] = lengths[z][0] + dst->offset[z][0];
430
431 memcpy(dst->data + dst->offset[z][0], dst_px_orig[z], lengths[z][0]);
432 memcpy(dst->data + dst->offset[z][1], dst_n_orig[z], lengths[z][1]);
433
434 free(dst_px_orig[z]);
435 free(dst_n_orig[z]);
436 }
437
438 return dest_sprite;
439}
440
441template Sprite *Blitter_32bppOptimized::EncodeInternal<true>(const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator);
442template Sprite *Blitter_32bppOptimized::EncodeInternal<false>(const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator);
443
445{
446 return this->EncodeInternal<true>(sprite, allocator);
447}
static FBlitter_32bppOptimized iFBlitter_32bppOptimized
Instantiation of the optimized 32bpp blitter factory.
Optimized 32 bpp blitter.
BlitterMode
The modes of blitting we can do.
Definition base.hpp:17
@ BM_BLACK_REMAP
Perform remapping to a completely blackened sprite.
Definition base.hpp:23
@ BM_COLOUR_REMAP
Perform a colour remapping.
Definition base.hpp:19
@ BM_TRANSPARENT_REMAP
Perform transparency colour remapping.
Definition base.hpp:21
@ BM_TRANSPARENT
Perform transparency darkening remapping.
Definition base.hpp:20
@ BM_NORMAL
Perform the simple blitting.
Definition base.hpp:18
@ BM_CRASH_REMAP
Perform a crash remapping.
Definition base.hpp:22
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
static Colour ComposeColourRGBANoCheck(uint r, uint g, uint b, uint a, Colour current)
Compose a colour based on RGBA values and the current pixel value.
static Colour ComposeColourPANoCheck(Colour colour, uint a, Colour current)
Compose a colour based on Pixel value, alpha value, and the current pixel value.
static Colour LookupColourInPalette(uint index)
Look up the colour in the current palette.
static Colour MakeTransparent(Colour colour, uint nom, uint denom=256)
Make a pixel looks like it is transparent.
static uint8_t MakeDark(uint8_t r, uint8_t g, uint8_t b)
Make a colour dark grey, for specialized 32bpp remapping.
static Colour ComposeColourRGBA(uint r, uint g, uint b, uint a, Colour current)
Compose a colour based on RGBA values and the current pixel value.
void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom) override
Draws a sprite to a (screen) buffer.
Sprite * Encode(const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator) override
Convert a sprite from the loader to our own format.
Factory for the optimised 32 bpp blitter (without palette animation).
Interface for something that can allocate memory for a sprite.
T * Allocate(size_t size)
Allocate memory for a sprite.
std::array< Sprite, ZOOM_LVL_END > SpriteCollection
Type defining a collection of sprites, one for each zoom level.
@ Font
A sprite used for fonts.
constexpr T * AlignPtr(T *x, uint n)
Return the smallest multiple of n equal or greater than x Applies to pointers only.
Definition math_func.hpp:55
uint8_t GetNearestColourIndex(uint8_t r, uint8_t g, uint8_t b)
Get nearest colour palette index from an RGB colour.
Definition palette.cpp:127
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:56
void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition stdafx.h:334
Parameters related to blitting.
Definition base.hpp:32
int skip_top
How much pixels of the source to skip on the top (based on zoom of dst)
Definition base.hpp:37
void * dst
Destination buffer.
Definition base.hpp:45
int left
The left offset in the 'dst' in pixels to start drawing.
Definition base.hpp:42
int pitch
The pitch of the destination buffer.
Definition base.hpp:46
int skip_left
How much pixels of the source to skip on the left (based on zoom of dst)
Definition base.hpp:36
int height
The height in pixels that needs to be drawn to dst.
Definition base.hpp:39
const uint8_t * remap
XXX – Temporary storage for remap array.
Definition base.hpp:34
int width
The width in pixels that needs to be drawn to dst.
Definition base.hpp:38
const void * sprite
Pointer to the sprite how ever the encoder stored it.
Definition base.hpp:33
int top
The top offset in the 'dst' in pixels to start drawing.
Definition base.hpp:43
Data stored about a (single) sprite.
uint8_t data[]
Data, all zoomlevels.
uint32_t offset[ZOOM_LVL_END][2]
Offsets (from .data) to streams for different zoom levels, and the normal and remap image information...
GUISettings gui
settings related to the GUI
ZoomLevel zoom_min
minimum zoom out level
ZoomLevel zoom_max
maximum zoom out level
Definition of a common pixel in OpenTTD's realm.
Structure for passing information from the sprite loader to the blitter.
uint16_t width
Width of the sprite.
SpriteLoader::CommonPixel * data
The sprite itself.
uint16_t height
Height of the sprite.
Data structure describing a sprite.
Definition spritecache.h:17
Structure to access the alpha, red, green, and blue channels from a 32 bit number.
Definition gfx_type.h:165
uint32_t data
Conversion of the channel information to a 32 bit number.
Definition gfx_type.h:166
uint8_t b
colour channels in BE order
Definition gfx_type.h:171
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:16
@ ZOOM_LVL_MAX
Maximum zoom level.
Definition zoom_type.h:42
@ ZOOM_LVL_END
End for iteration.
Definition zoom_type.h:25
@ ZOOM_LVL_MIN
Minimum zoom level.
Definition zoom_type.h:41