aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/gfx/alpha_mvi_crossblit.cpp
blob: f39561a1c00cecad9505b6a4e214be1eb5191e53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/***************************************************************************
 alpha_mvi_crossblit.c Copyright (C) 2001 Christoph Reichenbach


 This program may be modified and copied freely according to the terms of
 the GNU general public license (GPL), as long as the above copyright
 notice and the licensing information contained herein are preserved.

 Please refer to www.gnu.org for licensing details.

 This work is provided AS IS, without warranty of any kind, expressed or
 implied, including but not limited to the warranties of merchantibility,
 noninfringement, and fitness for a specific purpose. The author will not
 be held liable for any damage caused by this work or derivatives of it.

 By using this source code, you agree to the licensing terms as stated
 above.


 Please contact the maintainer for bug reports or inquiries.

 Current Maintainer:

    Christoph Reichenbach (CR) <jameson@linuxgames.com>

***************************************************************************/

/** MUST be compiled with -mcpu=cv6! */

#include <gfx_tools.h>


#ifdef HAVE_ALPHA_EV6_SUPPORT

#ifdef __DECC
#  include "c_asm.h"
#  define USE_C_CODE
#else /* GNU C */
#  undef USE_C_CODE
#endif

void
FUNCT_NAME(byte *dest, byte *src, int bytes_per_dest_line, int bytes_per_src_line,
           int xl, int yl, byte *alpha, int bytes_per_alpha_line, int bytes_per_alpha_pixel,
           unsigned int alpha_test_mask, int alpha_shift
#ifdef PRIORITY
           , byte *priority_pos , int bytes_per_priority_line, int bytes_per_priority_pixel, int priority
#endif
          ) {
#ifdef USE_C_CODE
#ifdef PRIORITY
	int left_mask = (255  << (((unsigned long) dest) & 7)) & 255;
	int right_mask = (255  >> (((unsigned long) dest + bytes_per_dest_line) & 7));
#endif
	unsigned long data;

	assert(!(bytes_per_alpha_line & 7));
	assert(!(((unsigned long) src) & 3));
	assert(!(((unsigned long) dest) & 3));
	assert(bytes_per_alpha_pixel < 2);

	yl++;
	while (--yl) {
		int x;
		byte *dest_next = dest + bytes_per_dest_line;
		byte *src_next = src + bytes_per_src_line;
#ifdef PRIORITY
		byte *pri_next = priority_pos + bytes_per_priority_line;
#endif
		asm("ldl $31, 0($31)\n\t"
		    "ldl $31, 0($31)\n\t"); /* Prefetch memory for next line */

		if (((unsigned long)src) & 4)
			data = *((unsigned int *) src);

		for (x = xl; x > 0; x--) {
			unsigned long alpha;

			if (!(((unsigned long)src) & 4))
				data = *((unsigned long *) src);
			alpha = (data & alpha_test_mask) >> alpha_shift;

			if (
#ifdef PRIORITY
			    (*priority_pos <= priority) &&
#endif
			    alpha != 255) {
				unsigned long result;
				unsigned long orig;
				unsigned long unpkdata;

				unpkdata = asm("unpkbw %0, %v0\n\t", data);

				result = unpkdata * (255 - alpha);

				orig = *((unsigned int *) dest);

				orig = asm("unpkbw %0, %v0\n\t", orig);

				result += orig * alpha;
				src += 4;

				result >>= 8;

				result = asm("pkwb %0, %v0\n\t", result);

				data >>= 32;
				*((unsigned int *) dest) = result;
#ifdef PRIORITY
				*priority_pos = priority;
#endif
				dest += 4;
			} else {
				data >>= 32;
				src += 4;
				dest += 4;
			}

#ifdef PRIORITY
			priority_pos++;
#endif
		}

		dest = dest_next;
		src = src_next;
#ifdef PRIORITY
		priority_pos = pri_next;
#endif
	}
#else
	unsigned long real_alpha_shift = alpha_shift;
#ifdef PRIORITY
	assert(!(bytes_per_alpha_line & 7));
	assert(bytes_per_alpha_pixel < 2);
	real_alpha_shift |= (unsigned long)(priority << 16) | ((unsigned long) bytes_per_priority_pixel << 32);
	/* Work around gcc design bug allowing only 10 asm parameters */
#endif
	__asm__	__volatile__(
#ifdef PRIORITY
	    /*
	    ** dest: $16
	    ** src: $17
	    ** bytes_per_dest_line: $18
	    ** bytes_per_src_line: $19
	    ** xl : $20
	    ** yl : $21
	    ** alpha_test_mask: $24
	    ** alpha_shift: $25
	    ** 255: $8
	    **
	    ** bytes_per_priority_line: $9
	    ** priority_pos: $10
	    ** priority extended to 8 bytes: $7
	    ** bytes_per_priority_pixel: $6
	    **
	    ** temp_priority_collection: $11
	    ** priority_pos backup: $12
	    ** left border mask: $13
	    ** right border mask: $28
	    ** priority test bit: $15
	    ** ldq4priority result: $26
	    */

	    "lda $30, -88($30) \n\t"
	    "stq $9, 0($30)    \n\t"
	    "stq $10, 8($30)   \n\t"
	    "stq $11, 16($30)  \n\t"
	    "stq $12, 24($30)  \n\t"
	    "stq $13, 32($30)  \n\t"
	    "stq $15, 40($30)  \n\t"
	    "stq $26, 48($30)  \n\t"
	    "stq %8, 56($30)   \n\t"
	    "stq %9, 64($30)   \n\t"
	    "stq $7, 72($30)   \n\t"
	    "stq $6, 80($30)   \n\t"

	    "mov %8, $9        \n\t"
	    "mov %9, $10       \n\t"
#endif
	    "mov %6, $24       \n\t"
	    "mov %7, $25       \n\t"
	    "mov 255, $8       \n\t"
	    "subl $21, 1, $21  \n\t"
#ifdef PRIORITY

	    /* Defrobnicate real_alpha_shift data */
	    "srl $25, 32, $6   \n\t"
	    "srl $25, 16, $7   \n\t"
	    "and $7, $8, $7    \n\t"

	    /* Load first priority data quad */
	    "andnot $10, 7, $0 \n\t"
	    "ldq $26, 0($0)    \n\t" /* Load priority */
	    "and $10, 7, $13   \n\t"
	    "sll $13, 3, $0    \n\t"

	    /* Calculate right border mask */
	    "addl $13, $20, $28\n\t"
	    "and $28, 7, $28   \n\t"
	    "beq $28, 7f       \n\t"
	    "mov 8, $0         \n\t"
	    "subl $0, $28, $28 \n"
	    "7:\n\t"
	    "srl $8, $28, $28  \n\t"
	    /* Left border mask */
	    "sll $8, $13, $13  \n\t"
	    "and $13, $8, $13  \n\t"

	    "mov $10, $12      \n\t"

	    "sll $7, 8, $0          \n\t"
	    "or $7, $0, $7          \n\t"
	    "sll $7, 16, $0         \n\t"
	    "or $7, $0, $7          \n\t"
	    "sll $7, 32, $0         \n\t"
	    "or $7, $0, $7          \n\t"
	    "cmpbge $7, $26, $3     \n\t"

	    "and $10, 7, $0         \n\t" /* Init priority bitptr */
	    "mov 1, $15             \n\t" /* .. */
	    "sll $15, $0, $15       \n\t" /* .. */

	    /* -> Priority buffer */
	    "and $3, $13, $11       \n\t"
	    "cmplt $20, 8, $0       \n\t"
	    "beq $0, 6f             \n\t"
	    "and $11, $28, $11      \n\t"
	    "6:\n\t"
#endif
	    "and $25, $8, $25       \n\t"

	    /***/
	    /*** Variable settings apply NOW ***/
	    /***/

	    "mov $20, $1       \n\t"
	    "mov $16, $2       \n\t"
	    "mov $17, $3       \n"
	    "8:\n\t"
	    "addq $2, $18, $2  \n\t"
	    "ldl $31, 0($2)    \n\t" /* Prefetch dest */
	    "addq $3, $19, $3  \n\t"
	    "ldl $31, 0($3)    \n"   /* Prefetch src */
	    "1:\n\t"
	    "addq $17, 4, $17  \n\t"
	    "beq $20, 3f       \n\t"
	    "subl $20, 1, $20  \n\t"
#ifdef PRIORITY
	    "5:\n\t"
	    "addq $10, 1, $10  \n\t"
	    "and $11, $15, $0  \n\t" /* Other priority beat our priority? */
	    "beq $0, 2f        \n\t"
#endif
	    "ldl $0, -4($17)   \n\t"
	    "unpkbw $0, $5     \n\t"
	    "and $0, $24, $0   \n\t"

	    "xor $0, $24, $4   \n\t"
	    "beq $4, 2f        \n\t"

	    "ldl $4, 0($16)    \n\t"
	    "unpkbw $4, $4     \n\t"
	    "srl $0, $25, $0   \n\t"
	    "mulq $4, $0, $4   \n\t"
	    "subl $8, $0, $0   \n\t"
	    "mulq $5, $0, $5   \n\t"
	    "addq  $4, $5, $4  \n\t"
	    "srl $4, 8, $4     \n\t"
	    "pkwb $4, $0       \n\t"
	    "stl $0, 0($16)    \n\t"
	    "br 9f             \n\t"
	    "2:\n"
	    "andnot $11, $15, $11 \n\t" /* Don't draw priority if we're fully transparent */
	    "9:\n\t"
	    "addq $16, 4, $16  \n\t"
#ifdef PRIORITY
	    "sll $15, 1, $15     \n\t"

	    "and $10, 7, $0    \n\t" /* Do we need to re-load priority mask? */
	    /**/		 "bne $0, 1b        \n\t"

	    /* Write back to priority buffer */
	    "zap $26, $11, $26      \n\t"
	    "zapnot $7, $11, $0     \n\t"
	    "or $0, $26, $0         \n\t"
	    "stq $0, -8($10)        \n\t"

	    "ldq $26, 0($10)    \n\t"  /* Load priority */
	    "cmpbge $7, $26, $11\n\t"

	    "mov 1, $15         \n\t" /* Init bitcmpmask */

	    "cmplt $20, 8, $0  \n\t"
	    /**/		 "beq $0, 1b        \n\t"
	    "and $11, $28, $11 \n\t"
#endif
	    "br 1b             \n"
	    "3:\n\t"
#ifdef PRIORITY
	    "and $10, 7, $16        \n\t"
	    "beq $16, 7f            \n\t"
	    "and $11, $28, $11      \n\t"
	    "zap $26, $11, $26      \n\t"
	    "zapnot $7, $11, $0     \n\t"
	    "or $0, $26, $0         \n\t"
	    "andnot $10, 7, $16     \n\t"
	    "stq $0, 0($16)         \n" /* Write back */
	    "7:\n\t"

	    "addq $9, $12, $12 \n\t"
	    "mov $12, $10      \n\t"
	    "andnot $10, 7, $0 \n\t"
	    "ldq $26, 0($0)    \n\t"

	    "and $10, 7, $0         \n\t"
	    "mov 1, $15             \n\t"
	    "sll $15, $0, $15       \n\t" /* Store priority-induced write-enable mask */

	    "cmpbge $7, $26, $16    \n\t"
	    /* -> Priority buffer */
	    "and $16, $13, $11      \n\t"
#endif
	    "beq $21, 4f       \n\t"
	    "subl $21, 1, $21  \n\t"
	    "mov $1, $20       \n\t"
	    "mov $2, $16       \n\t" /* Set line numbers for next line */
	    "mov $3, $17       \n\t"
	    "br 8b             \n"
	    "4:\n\t"
#ifdef PRIORITY
	    "ldq $9, 0($30)    \n\t"
	    "ldq $10, 8($30)   \n\t"
	    "ldq $11, 16($30)  \n\t"
	    "ldq $12, 24($30)  \n\t"
	    "ldq $13, 32($30)  \n\t"
	    "ldq $15, 40($30)  \n\t"
	    "ldq $26, 48($30)  \n\t"
	    "ldq %8, 56($30)   \n\t"
	    "ldq %9, 64($30)   \n\t"
	    "ldq $7, 72($30)   \n\t"
	    "ldq $6, 80($30)   \n\t"
	    "lda $30, 88($30)  \n\t"
#endif
    :
    :
	    "r"(dest), "r"(src), "r"(bytes_per_dest_line),
	    /*3*/ "r"(bytes_per_src_line), "r"(xl), "r"(yl),
	    /*6*/ "r"((unsigned long) alpha_test_mask), "r"(real_alpha_shift)
#ifdef PRIORITY
	    , "r"(bytes_per_priority_line), "r"(priority_pos)
#endif
			    : "%0", "%1", "%2", "%3", "%4", "%5", "%6",
			    "%7", "$16", "$17", "$18", "$19", "$20", "$21", "$24", "$25",
			    "$8", "memory"
			);
#endif
}

#endif /* __alpha__ */