3 # A script to generate optimized C blitters for Simple DirectMedia Layer
4 # http://www.libsdl.org/
11 # The formats potentially supported by this script:
12 # SDL_PIXELFORMAT_RGB332
13 # SDL_PIXELFORMAT_RGB444
14 # SDL_PIXELFORMAT_RGB555
15 # SDL_PIXELFORMAT_ARGB4444
16 # SDL_PIXELFORMAT_ARGB1555
17 # SDL_PIXELFORMAT_RGB565
18 # SDL_PIXELFORMAT_RGB24
19 # SDL_PIXELFORMAT_BGR24
20 # SDL_PIXELFORMAT_RGB888
21 # SDL_PIXELFORMAT_BGR888
22 # SDL_PIXELFORMAT_ARGB8888
23 # SDL_PIXELFORMAT_RGBA8888
24 # SDL_PIXELFORMAT_ABGR8888
25 # SDL_PIXELFORMAT_BGRA8888
26 # SDL_PIXELFORMAT_ARGB2101010
28 # The formats we're actually creating blitters for:
55 "ARGB8888" => "Uint32",
56 "RGBA8888" => "Uint32",
57 "ABGR8888" => "Uint32",
58 "BGRA8888" => "Uint32",
61 my %get_rgba_string = (
62 "RGB888" => "_R = (Uint8)(_pixel >> 16); _G = (Uint8)(_pixel >> 8); _B = (Uint8)_pixel; _A = 0xFF;",
63 "BGR888" => "_B = (Uint8)(_pixel >> 16); _G = (Uint8)(_pixel >> 8); _R = (Uint8)_pixel; _A = 0xFF;", "ARGB8888" => "_A = (Uint8)(_pixel >> 24); _R = (Uint8)(_pixel >> 16); _G = (Uint8)(_pixel >> 8); _B = (Uint8)_pixel;",
64 "RGBA8888" => "_R = (Uint8)(_pixel >> 24); _G = (Uint8)(_pixel >> 16); _B = (Uint8)(_pixel >> 8); _A = (Uint8)_pixel;",
65 "ABGR8888" => "_A = (Uint8)(_pixel >> 24); _B = (Uint8)(_pixel >> 16); _G = (Uint8)(_pixel >> 8); _R = (Uint8)_pixel;",
66 "BGRA8888" => "_B = (Uint8)(_pixel >> 24); _G = (Uint8)(_pixel >> 16); _R = (Uint8)(_pixel >> 8); _A = (Uint8)_pixel;",
69 my %set_rgba_string = (
70 "RGB888" => "_pixel = ((Uint32)_R << 16) | ((Uint32)_G << 8) | _B;",
71 "BGR888" => "_pixel = ((Uint32)_B << 16) | ((Uint32)_G << 8) | _R;",
72 "ARGB8888" => "_pixel = ((Uint32)_A << 24) | ((Uint32)_R << 16) | ((Uint32)_G << 8) | _B;",
73 "RGBA8888" => "_pixel = ((Uint32)_R << 24) | ((Uint32)_G << 16) | ((Uint32)_B << 8) | _A;",
74 "ABGR8888" => "_pixel = ((Uint32)_A << 24) | ((Uint32)_B << 16) | ((Uint32)_G << 8) | _R;",
75 "BGRA8888" => "_pixel = ((Uint32)_B << 24) | ((Uint32)_G << 16) | ((Uint32)_R << 8) | _A;",
80 open(FILE, ">$name.new") || die "Cant' open $name.new: $!";
82 /* DO NOT EDIT! This file is generated by sdlgenblit.pl */
84 Simple DirectMedia Layer
85 Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
87 This software is provided 'as-is', without any express or implied
88 warranty. In no event will the authors be held liable for any damages
89 arising from the use of this software.
91 Permission is granted to anyone to use this software for any purpose,
92 including commercial applications, and to alter it and redistribute it
93 freely, subject to the following restrictions:
95 1. The origin of this software must not be misrepresented; you must not
96 claim that you wrote the original software. If you use this software
97 in a product, an acknowledgment in the product documentation would be
98 appreciated but is not required.
99 2. Altered source versions must be plainly marked as such, and must not be
100 misrepresented as being the original software.
101 3. This notice may not be removed or altered from any source distribution.
103 #include "SDL_config.h"
112 print FILE <<__EOF__;
115 /* vi: set ts=4 sw=4 expandtab: */
118 if ( ! -f $name || system("cmp -s $name $name.new") != 0 ) {
119 rename("$name.new", "$name");
127 print FILE <<__EOF__;
128 extern SDL_BlitFuncEntry SDL_GeneratedBlitFuncTable[];
132 sub output_copyfuncname
137 my $modulate = shift;
143 print FILE "$prefix SDL_Blit_${src}_${dst}";
145 print FILE "_Modulate";
154 print FILE "(SDL_BlitInfo *info)";
156 print FILE "$suffix";
163 my $string = $get_rgba_string{$format};
164 $string =~ s/_/$prefix/g;
165 if ( $prefix ne "" ) {
166 print FILE <<__EOF__;
167 ${prefix}pixel = *$prefix;
170 print FILE <<__EOF__;
174 print FILE <<__EOF__;
183 my $string = $set_rgba_string{$format};
184 $string =~ s/_/$prefix/g;
185 print FILE <<__EOF__;
187 *dst = ${prefix}pixel;
195 my $modulate = shift;
201 if ( $src eq $dst && !$modulate && !$blend ) {
202 print FILE <<__EOF__;
209 get_rgba("src", $src);
210 get_rgba("dst", $dst);
218 print FILE <<__EOF__;
219 if (flags & SDL_COPY_MODULATE_COLOR) {
220 ${s}R = (${s}R * modulateR) / 255;
221 ${s}G = (${s}G * modulateG) / 255;
222 ${s}B = (${s}B * modulateB) / 255;
224 if (flags & SDL_COPY_MODULATE_ALPHA) {
225 ${s}A = (${s}A * modulateA) / 255;
230 print FILE <<__EOF__;
231 if (flags & (SDL_COPY_BLEND|SDL_COPY_ADD)) {
232 /* This goes away if we ever use premultiplied alpha */
234 ${s}R = (${s}R * ${s}A) / 255;
235 ${s}G = (${s}G * ${s}A) / 255;
236 ${s}B = (${s}B * ${s}A) / 255;
239 switch (flags & (SDL_COPY_BLEND|SDL_COPY_ADD|SDL_COPY_MOD)) {
241 ${d}R = ${s}R + ((255 - ${s}A) * ${d}R) / 255;
242 ${d}G = ${s}G + ((255 - ${s}A) * ${d}G) / 255;
243 ${d}B = ${s}B + ((255 - ${s}A) * ${d}B) / 255;
246 ${d}R = ${s}R + ${d}R; if (${d}R > 255) ${d}R = 255;
247 ${d}G = ${s}G + ${d}G; if (${d}G > 255) ${d}G = 255;
248 ${d}B = ${s}B + ${d}B; if (${d}B > 255) ${d}B = 255;
251 ${d}R = (${s}R * ${d}R) / 255;
252 ${d}G = (${s}G * ${d}G) / 255;
253 ${d}B = (${s}B * ${d}B) / 255;
259 set_rgba("dst", $dst);
269 my $modulate = shift;
273 output_copyfuncname("static void", $src, $dst, $modulate, $blend, $scale, 1, "\n");
274 print FILE <<__EOF__;
277 if ( $modulate || $blend ) {
278 print FILE <<__EOF__;
279 const int flags = info->flags;
283 print FILE <<__EOF__;
284 const Uint32 modulateR = info->r;
285 const Uint32 modulateG = info->g;
286 const Uint32 modulateB = info->b;
287 const Uint32 modulateA = info->a;
291 print FILE <<__EOF__;
293 Uint32 srcR, srcG, srcB, srcA;
295 Uint32 dstR, dstG, dstB, dstA;
297 } elsif ( $modulate || $src ne $dst ) {
298 print FILE <<__EOF__;
304 print FILE <<__EOF__;
311 incy = (info->src_h << 16) / info->dst_h;
312 incx = (info->src_w << 16) / info->dst_w;
314 while (info->dst_h--) {
315 $format_type{$src} *src = 0;
316 $format_type{$dst} *dst = ($format_type{$dst} *)info->dst;
320 while (posy >= 0x10000L) {
325 if (posx >= 0x10000L) {
326 while (posx >= 0x10000L) {
330 src = ($format_type{$src} *)(info->src + (srcy * info->src_pitch) + (srcx * $format_size{$src}));
332 print FILE <<__EOF__;
335 output_copycore($src, $dst, $modulate, $blend);
336 print FILE <<__EOF__;
341 info->dst += info->dst_pitch;
345 print FILE <<__EOF__;
347 while (info->dst_h--) {
348 $format_type{$src} *src = ($format_type{$src} *)info->src;
349 $format_type{$dst} *dst = ($format_type{$dst} *)info->dst;
353 output_copycore($src, $dst, $modulate, $blend);
354 print FILE <<__EOF__;
358 info->src += info->src_pitch;
359 info->dst += info->dst_pitch;
363 print FILE <<__EOF__;
369 sub output_copyfunc_h
375 print FILE <<__EOF__;
376 #include "SDL_video.h"
377 #include "SDL_blit.h"
378 #include "SDL_blit_auto.h"
383 sub output_copyfunctable
385 print FILE <<__EOF__;
386 SDL_BlitFuncEntry SDL_GeneratedBlitFuncTable[] = {
388 for (my $i = 0; $i <= $#src_formats; ++$i) {
389 my $src = $src_formats[$i];
390 for (my $j = 0; $j <= $#dst_formats; ++$j) {
391 my $dst = $dst_formats[$j];
392 for (my $modulate = 0; $modulate <= 1; ++$modulate) {
393 for (my $blend = 0; $blend <= 1; ++$blend) {
394 for (my $scale = 0; $scale <= 1; ++$scale) {
395 if ( $modulate || $blend || $scale ) {
396 print FILE " { SDL_PIXELFORMAT_$src, SDL_PIXELFORMAT_$dst, ";
400 $flag = "SDL_COPY_MODULATE_COLOR | SDL_COPY_MODULATE_ALPHA";
401 if ( $flags eq "" ) {
404 $flags = "$flags | $flag";
408 $flag = "SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD";
409 if ( $flags eq "" ) {
412 $flags = "$flags | $flag";
416 $flag = "SDL_COPY_NEAREST";
417 if ( $flags eq "" ) {
420 $flags = "$flags | $flag";
423 if ( $flags eq "" ) {
426 print FILE "($flags), SDL_CPU_ANY,";
427 output_copyfuncname("", $src_formats[$i], $dst_formats[$j], $modulate, $blend, $scale, 0, " },\n");
434 print FILE <<__EOF__;
441 sub output_copyfunc_c
446 for (my $modulate = 0; $modulate <= 1; ++$modulate) {
447 for (my $blend = 0; $blend <= 1; ++$blend) {
448 for (my $scale = 0; $scale <= 1; ++$scale) {
449 if ( $modulate || $blend || $scale ) {
450 output_copyfunc($src, $dst, $modulate, $blend, $scale);
457 open_file("SDL_blit_auto.h");
459 for (my $i = 0; $i <= $#src_formats; ++$i) {
460 for (my $j = 0; $j <= $#dst_formats; ++$j) {
461 output_copyfunc_h($src_formats[$i], $dst_formats[$j]);
465 close_file("SDL_blit_auto.h");
467 open_file("SDL_blit_auto.c");
469 for (my $i = 0; $i <= $#src_formats; ++$i) {
470 for (my $j = 0; $j <= $#dst_formats; ++$j) {
471 output_copyfunc_c($src_formats[$i], $dst_formats[$j]);
474 output_copyfunctable();
475 close_file("SDL_blit_auto.c");