-
Notifications
You must be signed in to change notification settings - Fork 0
/
tri.h
676 lines (543 loc) · 16 KB
/
tri.h
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
#ifndef TRI_H
#define TRI_H
#include "minmax.h"
#include "fx.h"
//
#define RASTER_BLOCK_SIZE_SHIFT 2
#define RASTER_BLOCK_SIZE 4
#define RASTER_BLOCK_MASK 3
#define RASTER_SUBPIXEL_BITS 4
#define RASTER_SUBPIXEL_ONE 16
#define RASTER_SUBPIXEL_MASK 15
#define RASTER_SUBPIXEL_HALF 8
#define SCREEN_SUBPIXEL_CENTER_X 2560
#define SCREEN_SUBPIXEL_CENTER_Y 1600
//
static inline void tris(
int16_t __far* draw_buffer,
uint32_t __far* sort_buffer,
uint16_t num) {
uint16_t i, j;
int16_t x0, y0, x1, y1, x2, y2;
uint16_t min_x, min_y, max_x, max_y;
int32_t dx0, dx1, dx2;
int32_t dy0, dy1, dy2;
int32_t eo0, eo1, eo2; // Offsets for empty testing
int32_t c0, c1, c2;
#if defined(INLINE_ASM)
uint32_t color32;
#endif
uint8_t color;
uint8_t __far* screen_row;
uint8_t __far* screen_row_end;
for (i = 0; i < num; ++i) {
j = sort_buffer[i] >> 16;
aw_assert(j < num);
j <<= 3;
x0 = draw_buffer[j + 0];
y0 = draw_buffer[j + 1];
x1 = draw_buffer[j + 2];
y1 = draw_buffer[j + 3];
x2 = draw_buffer[j + 4];
y2 = draw_buffer[j + 5];
color = draw_buffer[j + 6];
max_x = clamp16((max16(x0, max16(x1, x2)) + RASTER_SUBPIXEL_MASK) >> RASTER_SUBPIXEL_BITS, 0, SCREEN_X_MAX);
if (max_x == 0)
continue;
max_y = clamp16((max16(y0, max16(y1, y2)) + RASTER_SUBPIXEL_MASK) >> RASTER_SUBPIXEL_BITS, 0, SCREEN_Y_MAX);
if (max_y == 0)
continue;
min_x = clamp16((min16(x0, min16(x1, x2)) + RASTER_SUBPIXEL_MASK) >> RASTER_SUBPIXEL_BITS, 0, SCREEN_X_MAX) & ~RASTER_BLOCK_MASK;
if (min_x == max_x)
continue;
min_y = clamp16((min16(y0, min16(y1, y2)) + RASTER_SUBPIXEL_MASK) >> RASTER_SUBPIXEL_BITS, 0, SCREEN_Y_MAX) & ~RASTER_BLOCK_MASK;
if (min_y == max_y)
continue;
//
#if !defined(INLINE_ASM)
dx0 = x0 - x1;
dy0 = y0 - y1;
dx1 = x1 - x2;
dy1 = y1 - y2;
dx2 = x2 - x0;
dy2 = y2 - y0;
#else
__asm {
.386
movsx eax, x0
movsx ebx, y0
movsx ecx, x1
movsx edx, y1
sub eax, ecx
sub ebx, edx
mov dx0, eax
mov dy0, ebx
//
movsx eax, x2
movsx ebx, y2
sub ecx, eax
sub edx, ebx
mov dx1, ecx
mov dy1, edx
//
movsx ecx, x0
movsx edx, y0
sub eax, ecx
sub ebx, edx
mov dx2, eax
mov dy2, ebx
}
#endif
//
#if !defined(INLINE_ASM)
{
int32_t c = imul32(dy0, x0) - imul32(dx0, y0);
if (dy0 < 0 || (dy0 == 0 && dx0 > 0)) c++;
dx0 <<= RASTER_SUBPIXEL_BITS;
dy0 <<= RASTER_SUBPIXEL_BITS;
c0 = c + imul32(dx0, min_y) - imul32(dy0, min_x);
}
#else
__asm {
mov eax, dy0
movsx ebx, x0
imul ebx
mov ecx, eax
mov eax, dx0
movsx ebx, y0
imul ebx
sub ecx, eax
mov eax, dy0
cmp eax, 0
jl increment
jne done
mov eax, dx0
cmp eax, 0
jle done
increment:
inc ecx
done:
//
mov eax, dx0
shl eax, RASTER_SUBPIXEL_BITS
mov dx0, eax
movzx ebx, min_y
imul ebx
add ecx, eax
mov eax, dy0
shl eax, RASTER_SUBPIXEL_BITS
mov dy0, eax
movzx ebx, min_x
imul ebx
sub ecx, eax
mov c0, ecx
}
#endif
//
#if !defined(INLINE_ASM)
{
int32_t c = imul32(dy1, x1) - imul32(dx1, y1);
if (dy1 < 0 || (dy1 == 0 && dx1 > 0)) c++;
dx1 <<= RASTER_SUBPIXEL_BITS;
dy1 <<= RASTER_SUBPIXEL_BITS;
c1 = c + imul32(dx1, min_y) - imul32(dy1, min_x);
}
#else
__asm {
mov eax, dy1
movsx ebx, x1
imul ebx
mov ecx, eax
mov eax, dx1
movsx ebx, y1
imul ebx
sub ecx, eax
mov eax, dy1
cmp eax, 0
jl increment
jne done
mov eax, dx1
cmp eax, 0
jle done
increment:
inc ecx
done:
//
mov eax, dx1
shl eax, RASTER_SUBPIXEL_BITS
mov dx1, eax
movzx ebx, min_y
imul ebx
add ecx, eax
mov eax, dy1
shl eax, RASTER_SUBPIXEL_BITS
mov dy1, eax
movzx ebx, min_x
imul ebx
sub ecx, eax
mov c1, ecx
}
#endif
//
#if !defined(INLINE_ASM)
{
int32_t c = imul32(dy2, x2) - imul32(dx2, y2);
if (dy2 < 0 || (dy2 == 0 && dx2 > 0)) c++;
dx2 <<= RASTER_SUBPIXEL_BITS;
dy2 <<= RASTER_SUBPIXEL_BITS;
c2 = c + imul32(dx2, min_y) - imul32(dy2, min_x);
}
#else
__asm {
mov eax, dy2
movsx ebx, x2
imul ebx
mov ecx, eax
mov eax, dx2
movsx ebx, y2
imul ebx
sub ecx, eax
mov eax, dy2
cmp eax, 0
jl increment
jne done
mov eax, dx2
cmp eax, 0
jle done
increment:
inc ecx
done:
//
mov eax, dx2
shl eax, RASTER_SUBPIXEL_BITS
mov dx2, eax
movzx ebx, min_y
imul ebx
add ecx, eax
mov eax, dy2
shl eax, RASTER_SUBPIXEL_BITS
mov dy2, eax
movzx ebx, min_x
imul ebx
sub ecx, eax
mov c2, ecx
}
#endif
//
#if !defined(INLINE_ASM)
eo0 = 0;
if (dy0 < 0) eo0 = eo0 - (dy0 << RASTER_BLOCK_SIZE_SHIFT);
if (dx0 > 0) eo0 = eo0 + (dx0 << RASTER_BLOCK_SIZE_SHIFT);
eo1 = 0;
if (dy1 < 0) eo1 = eo1 - (dy1 << RASTER_BLOCK_SIZE_SHIFT);
if (dx1 > 0) eo1 = eo1 + (dx1 << RASTER_BLOCK_SIZE_SHIFT);
eo2 = 0;
if (dy2 < 0) eo2 = eo2 - (dy2 << RASTER_BLOCK_SIZE_SHIFT);
if (dx2 > 0) eo2 = eo2 + (dx2 << RASTER_BLOCK_SIZE_SHIFT);
#else
__asm {
// 0
xor eax, eax
mov ebx, dy0
cmp ebx, 0
jge skip_dy0
shl ebx, RASTER_BLOCK_SIZE_SHIFT
sub eax, ebx
skip_dy0:
mov ebx, dx0
cmp ebx, 0
jle skip_dx0
shl ebx, RASTER_BLOCK_SIZE_SHIFT
add eax, ebx
skip_dx0:
mov eo0, eax
// 1
xor eax, eax
mov ebx, dy1
cmp ebx, 0
jge skip_dy1
shl ebx, RASTER_BLOCK_SIZE_SHIFT
sub eax, ebx
skip_dy1:
mov ebx, dx1
cmp ebx, 0
jle skip_dx1
shl ebx, RASTER_BLOCK_SIZE_SHIFT
add eax, ebx
skip_dx1:
mov eo1, eax
// 2
xor eax, eax
mov ebx, dy2
cmp ebx, 0
jge skip_dy2
shl ebx, RASTER_BLOCK_SIZE_SHIFT
sub eax, ebx
skip_dy2:
mov ebx, dx2
cmp ebx, 0
jle skip_dx2
shl ebx, RASTER_BLOCK_SIZE_SHIFT
add eax, ebx
skip_dx2:
mov eo2, eax
}
#endif
//
#if defined(INLINE_ASM)
__asm {
movzx eax, color
mov ah, al
shl eax, 16
mov ah, color
mov al, color
mov color32, eax
}
#endif
screen_row = dblbuf + mul_by_screen_stride(min_y);
screen_row_end = dblbuf + mul_by_screen_stride(max_y);
while (screen_row < screen_row_end) {
uint16_t x = min_x;
fx_t cx0, cx1, cx2;
#if !defined(INLINE_ASM)
cx0 = c0;
cx1 = c1;
cx2 = c2;
while (!((cx0 + eo0) > 0 &&
(cx1 + eo1) > 0 &&
(cx2 + eo2) > 0)) {
cx0 -= dy0 << RASTER_BLOCK_SIZE_SHIFT;
cx1 -= dy1 << RASTER_BLOCK_SIZE_SHIFT;
cx2 -= dy2 << RASTER_BLOCK_SIZE_SHIFT;
x += RASTER_BLOCK_SIZE;
if (x >= max_x)
break;
}
#else
__asm {
mov eax, c0
mov ebx, c1
mov ecx, c2
mov cx0, eax
mov cx1, ebx
mov cx2, ecx
block_test:
mov eax, cx0
add eax, eo0
cmp eax, 0
jle block_empty
mov eax, cx1
add eax, eo1
cmp eax, 0
jle block_empty
mov eax, cx2
add eax, eo2
cmp eax, 0
jle block_empty
jmp block_found
block_empty:
mov eax, dy0
mov ebx, dy1
mov ecx, dy2
shl eax, RASTER_BLOCK_SIZE_SHIFT
shl ebx, RASTER_BLOCK_SIZE_SHIFT
shl ecx, RASTER_BLOCK_SIZE_SHIFT
sub cx0, eax
sub cx1, ebx
sub cx2, ecx
mov ax, x
add ax, RASTER_BLOCK_SIZE
mov x, ax
cmp ax, max_x
jb block_test
block_found:
}
#endif
if (x < max_x) {
uint8_t __far* screen_block = screen_row;
fx_t ciy0, ciy1, ciy2;
uint8_t iy;
#if !defined(INLINE_ASM)
ciy0 = cx0;
ciy1 = cx1;
ciy2 = cx2;
#else
__asm {
mov eax, cx0
mov ebx, cx1
mov ecx, cx2
mov ciy0, eax
mov ciy1, ebx
mov ciy2, ecx
}
#endif
for (iy = 0; iy < RASTER_BLOCK_SIZE; ++iy) {
uint16_t hleft = x;
fx_t cix0, cix1, cix2;
#if !defined(INLINE_ASM)
cix0 = ciy0;
cix1 = ciy1;
cix2 = ciy2;
#else
__asm {
mov eax, ciy0
mov ebx, ciy1
mov ecx, ciy2
mov cix0, eax
mov cix1, ebx
mov cix2, ecx
}
#endif
#if !defined(INLINE_ASM)
while ((cix0 | cix1 | cix2) <= 0) {
cix0 -= dy0;
cix1 -= dy1;
cix2 -= dy2;
hleft++;
if (hleft > max_x) {
break;
}
}
#else
__asm {
mov eax, cix0
mov ebx, cix1
mov ecx, cix2
cont:
mov edx, eax
or edx, ebx
or edx, ecx
cmp edx, 0
jg done
mov edx, dy0
sub eax, edx
mov edx, dy1
sub ebx, edx
mov edx, dy2
sub ecx, edx
mov dx, hleft
inc dx
mov hleft, dx
cmp dx, max_x
jg done
jmp cont
done:
mov cix0, eax
mov cix1, ebx
mov cix2, ecx
}
#endif
if (hleft <= max_x) {
uint16_t hright = hleft;
#if !defined(INLINE_ASM)
while ((cix0 | cix1 | cix2) > 0) {
cix0 -= dy0;
cix1 -= dy1;
cix2 -= dy2;
hright++;
if (hright > max_x) {
break;
}
}
#else
__asm {
mov eax, cix0
mov ebx, cix1
mov ecx, cix2
cont:
mov edx, eax
or edx, ebx
or edx, ecx
cmp edx, 0
jle done
mov edx, dy0
sub eax, edx
mov edx, dy1
sub ebx, edx
mov edx, dy2
sub ecx, edx
mov dx, hright
inc dx
mov hright, dx
cmp dx, max_x
jg done
jmp cont
done:
}
#endif
#if !defined(INLINE_ASM)
{
uint8_t __far* tgt = screen_block + hleft;
uint8_t __far* tgt_end = screen_block + hright;
while (tgt < tgt_end)
*tgt++ = color;
}
#else
__asm {
movzx eax, hleft
mov edx, screen_block
add edx, eax
movzx edi, dx
shr edx, 16
mov es, dx
mov cx, hright
sub cx, ax // cx = width
mov dx, ax // dx = hleft
mov eax, color32
mov bx, cx
shr cx, 2
rep stosd
mov cx, bx
and cx, 3
rep stosb
}
#endif
}
#if !defined(INLINE_ASM)
ciy0 += dx0;
ciy1 += dx1;
ciy2 += dx2;
#else
__asm {
mov eax, ciy0
mov ebx, ciy1
mov ecx, ciy2
add eax, dx0
add ebx, dx1
add ecx, dx2
mov ciy0, eax
mov ciy1, ebx
mov ciy2, ecx
}
#endif
screen_block += SCREEN_STRIDE;
}
}
#if !defined(INLINE_ASM)
c0 += dx0 << RASTER_BLOCK_SIZE_SHIFT;
c1 += dx1 << RASTER_BLOCK_SIZE_SHIFT;
c2 += dx2 << RASTER_BLOCK_SIZE_SHIFT;
#else
__asm {
mov eax, c0
mov ebx, dx0
shl ebx, RASTER_BLOCK_SIZE_SHIFT
add eax, ebx
mov c0, eax
mov eax, c1
mov ebx, dx1
shl ebx, RASTER_BLOCK_SIZE_SHIFT
add eax, ebx
mov c1, eax
mov eax, c2
mov ebx, dx2
shl ebx, RASTER_BLOCK_SIZE_SHIFT
add eax, ebx
mov c2, eax
}
#endif
screen_row += SCREEN_STRIDE << RASTER_BLOCK_SIZE_SHIFT;
}
}
}
#endif