-
Notifications
You must be signed in to change notification settings - Fork 12
/
r_phase7.c
537 lines (437 loc) · 12.1 KB
/
r_phase7.c
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
/*
CALICO
Renderer phase 7 - Visplanes
*/
#include "r_local.h"
#ifdef MARS
#include "mars.h"
#endif
#define LIGHTCOEF (0x7ffffu << SLOPEBITS)
#define MIPSCALE (1<<24)
#define FLATSIZE 64
struct localplane_s;
typedef struct localplane_s
{
visplane_t* pl;
fixed_t height;
angle_t angle;
fixed_t x, y;
int lightmin, lightmax, lightsub, lightcoef;
fixed_t basexscale, baseyscale;
#ifdef MARS
inpixel_t* ds_source[MIPLEVELS];
#else
pixel_t* ds_source[MIPLEVELS];
#endif
#if MIPLEVELS > 1
unsigned maxmip;
int mipsize[MIPLEVELS];
#endif
} localplane_t;
static void R_MapPlane(localplane_t* lpl, int y, int x, int x2) ATTR_DATA_CACHE_ALIGN;
static void R_PlaneLoop(localplane_t* lpl) ATTR_DATA_CACHE_ALIGN;
static void R_DrawPlanes2(void) ATTR_DATA_CACHE_ALIGN;
static int R_TryLockPln(void) ATTR_DATA_CACHE_ALIGN;
static void R_LockPln(void) ATTR_DATA_CACHE_ALIGN;
static void R_UnlockPln(void) ATTR_DATA_CACHE_ALIGN;
static visplane_t* R_GetNextPlane(uint16_t *sortedvisplanes) ATTR_DATA_CACHE_ALIGN;
void R_DrawPlanes(void) __attribute__((noinline));
static char pl_lock = 0;
#ifdef MARS
#define pl_next MARS_SYS_COMM6
#else
static int pl_next = 0;
#endif
//
// Render the horizontal spans determined by R_PlaneLoop
//
static void R_MapPlane(localplane_t* lpl, int y, int x, int x2)
{
int remaining;
fixed_t distance;
fixed_t length, xfrac, yfrac, xstep, ystep;
angle_t angle;
int light;
unsigned scale;
unsigned miplevel, mipsize;
remaining = x2 - x + 1;
if (remaining <= 0)
return; // nothing to draw (shouldn't happen)
distance = FixedMul(lpl->height, yslope[y]);
#ifdef MARS
volatile int32_t t;
__asm volatile (
"mov #-128, r0\n\t"
"add r0, r0 /* r0 is now 0xFFFFFF00 */ \n\t"
"mov #0, %0\n\t"
"mov.l %2, @(16, r0) /* set high bits of the 64-bit dividend */ \n\t"
"mov.l %1, @(0, r0) /* set 32-bit divisor */ \n\t"
"mov #0, %0\n\t"
"mov.l %0, @(20, r0) /* set low bits of the 64-bit dividend, start divide */\n\t"
: "=&r" (t) : "r" (distance), "r"(lpl->lightcoef) : "r0");
#endif
length = FixedMul(distance, distscale[x]);
xstep = FixedMul(distance, lpl->basexscale);
ystep = FixedMul(distance, lpl->baseyscale);
#if MIPLEVELS > 1
miplevel = (unsigned)distance / MIPSCALE;
if (miplevel > lpl->maxmip)
miplevel = lpl->maxmip;
mipsize = lpl->mipsize[miplevel];
#else
miplevel = 0;
mipsize = FLATSIZE;
#endif
angle = (lpl->angle + (xtoviewangle[x]<<FRACBITS)) >> ANGLETOFINESHIFT;
xfrac = FixedMul(finecosine(angle), length);
xfrac = lpl->x + xfrac;
yfrac = FixedMul(finesine(angle), length);
yfrac = lpl->y - yfrac;
#ifdef MARS
yfrac *= FLATSIZE;
#endif
if (miplevel > 0) {
unsigned m = miplevel;
do {
xfrac >>= 1, xstep >>= 1;
yfrac >>= 2, ystep >>= 2;
} while (--m);
}
if (lpl->lightcoef != 0)
{
#ifdef MARS
__asm volatile (
"mov #-128, r0\n\t"
"add r0, r0 /* r0 is now 0xFFFFFF00 */ \n\t"
"mov.l @(20,r0), %0 /* get 32-bit quotient */ \n\t"
: "=r" (scale) : : "r0");
#else
scale = (lpl->lightcoef << SLOPEBITS) / distance;
#endif
light = scale;
light -= lpl->lightsub;
if (light < lpl->lightmin)
light = lpl->lightmin;
else if (light > lpl->lightmax)
light = lpl->lightmax;
light >>= FRACBITS;
// transform to hardware value
light = HWLIGHT(light);
}
else
{
light = lpl->lightmax;
}
drawspan(y, x, x2, light, xfrac, yfrac, xstep, ystep, lpl->ds_source[miplevel], mipsize);
}
//
// Determine the horizontal spans of a single visplane
//
static void R_PlaneLoop(localplane_t *lpl)
{
int pl_x, pl_stopx;
uint16_t *pl_openptr;
unsigned t1, t2, b1, b2, pl_oldtop, pl_oldbottom;
int16_t spanstart[SCREENHEIGHT];
visplane_t* pl = lpl->pl;
pl_x = pl->minx;
pl_stopx = pl->maxx;
// see if there is any open space
if(pl_x > pl_stopx)
return; // nothing to map
pl_openptr = &pl->open[pl_x];
t1 = OPENMARK;
b1 = t1 & 0xff;
t1 >>= 8;
t2 = *pl_openptr;
do
{
int x2;
b2 = t2 & 0xff;
t2 >>= 8;
pl_oldtop = t2;
pl_oldbottom = b2;
x2 = pl_x - 1;
// top diffs
while (t1 < t2 && t1 <= b1)
{
R_MapPlane(lpl, t1, spanstart[t1], x2);
++t1;
}
// bottom diffs
while (b1 > b2 && b1 >= t1)
{
R_MapPlane(lpl, b1, spanstart[b1], x2);
--b1;
}
if (pl_x == pl_stopx + 1)
break;
while (t2 < t1 && t2 <= b2)
{
// top dif spanstarts
spanstart[t2] = pl_x;
++t2;
}
while (b2 > b1 && b2 >= t2)
{
// bottom dif spanstarts
spanstart[b2] = pl_x;
--b2;
}
b1 = pl_oldbottom;
t1 = pl_oldtop;
t2 = pl_x++ < pl_stopx ? *++pl_openptr : OPENMARK;
}
while(1);
}
static int R_TryLockPln(void)
{
int res;
__asm volatile (\
"tas.b %1\n\t" \
"movt %0\n\t" \
: "=&r" (res) \
: "m" (pl_lock) \
);
return res != 0;
}
static void R_LockPln(void)
{
while (!R_TryLockPln());
}
static void R_UnlockPln(void)
{
pl_lock = 0;
}
static visplane_t *R_GetNextPlane(uint16_t *sortedvisplanes)
{
int p;
R_LockPln();
p = pl_next;
pl_next = p + 1;
R_UnlockPln();
#ifdef MARS
if (p + vd.visplanes + 1 >= vd.lastvisplane)
return NULL;
return vd.visplanes + sortedvisplanes[p*2+1];
#else
visplane_t *pl = vd.visplanes + p + 1;
return pl == vd.lastvisplane ? NULL : pl;
#endif
}
static void R_DrawPlanes2(void)
{
angle_t angle;
localplane_t lpl;
visplane_t* pl;
int extralight;
#ifdef MARS
Mars_ClearCacheLine(&vd.lastvisplane);
Mars_ClearCacheLine(&vd.gsortedvisplanes);
Mars_ClearCacheLines(vd.gsortedvisplanes, ((vd.lastvisplane - vd.visplanes - 1) * sizeof(*vd.gsortedvisplanes) + 31) / 16);
#endif
if (vd.gsortedvisplanes == NULL)
return;
lpl.x = vd.viewx;
lpl.y = -vd.viewy;
lpl.angle = vd.viewangle;
angle = (lpl.angle - ANG90) >> ANGLETOFINESHIFT;
lpl.basexscale = FixedDiv(finecosine(angle), centerXFrac);
lpl.baseyscale = -FixedDiv(finesine(angle), centerXFrac);
#ifdef MARS
lpl.baseyscale *= FLATSIZE;
#endif
extralight = vd.extralight;
while ((pl = R_GetNextPlane((uint16_t *)vd.gsortedvisplanes)) != NULL)
{
int light;
int flatnum;
#ifdef MARS
Mars_ClearCacheLines(pl, (sizeof(visplane_t) + 31) / 16);
#endif
if (pl->minx > pl->maxx)
continue;
flatnum = pl->flatandlight&0xffff;
if (flatnum >= col2flat)
I_SetThreadLocalVar(DOOMTLS_COLORMAP, dc_colormaps2);
else
I_SetThreadLocalVar(DOOMTLS_COLORMAP, dc_colormaps);
lpl.pl = pl;
lpl.ds_source[0] = flatpixels[flatnum].data[0];
#if MIPLEVELS > 1
lpl.mipsize[0] = FLATSIZE;
lpl.maxmip = texmips ? MIPLEVELS-1 : 0;
if (lpl.maxmip > 0)
{
int j;
int mipsize = FLATSIZE>>1;
for (j = 1; j <= (int)lpl.maxmip; j++)
{
lpl.ds_source[j] = flatpixels[flatnum].data[j];
lpl.mipsize[j] = mipsize;
mipsize >>= 1;
}
}
#endif
lpl.height = (unsigned)D_abs(pl->height);
if (vd.fixedcolormap)
{
lpl.lightmin = lpl.lightmax = vd.fixedcolormap;
lpl.lightcoef = 0;
}
else
{
light = ((unsigned)pl->flatandlight>>16);
lpl.lightmax = light;
lpl.lightmax += extralight;
if (lpl.lightmax > 255)
lpl.lightmax = 255;
#ifdef MARS
light = light - ((255 - light - light/2) << 1);
#else
light = light - ((255 - light) << 1);
#endif
light += extralight;
if (light < MINLIGHT)
light = MINLIGHT;
if (light > lpl.lightmax)
light = lpl.lightmax;
lpl.lightmin = light;
lpl.lightsub = 0;
if (lpl.lightmin != lpl.lightmax)
{
lpl.lightcoef = (unsigned)(lpl.lightmax - lpl.lightmin) << 8;
lpl.lightsub = (((lpl.lightmax - lpl.lightmin) * 160) << FRACBITS) / (800 - 160);
}
if (lpl.lightsub != 0)
{
lpl.lightmin <<= FRACBITS;
lpl.lightmax <<= FRACBITS;
}
else
{
lpl.lightcoef = 0;
lpl.lightmin = lpl.lightmax = HWLIGHT((unsigned)lpl.lightmax);
}
}
R_PlaneLoop(&lpl);
}
}
#ifdef MARS
static void Mars_R_SplitPlanes(void) ATTR_DATA_CACHE_ALIGN;
static void Mars_R_SortPlanes(void) ATTR_DATA_CACHE_ALIGN;
void Mars_Sec_R_DrawPlanes(void)
{
R_DrawPlanes2();
}
static void Mars_R_SplitPlanes(void)
{
const int minlen = centerX;
const int maxlen = centerX * 2;
visplane_t *pl, *last = vd.lastvisplane;
int numplanes;
numplanes = vd.lastvisplane - vd.visplanes;
if (numplanes >= MAXVISPLANES)
return;
for (pl = vd.visplanes + 1; pl < last; pl++)
{
int start, stop;
visplane_t* newpl;
int newstop;
// see if there is any open space
start = pl->minx, stop = pl->maxx;
if (start > stop)
continue; // nothing to map
// split long visplane into two
int span = stop - start + 1;
if (span < maxlen)
continue;
pl->maxx = start;
newstop = start + minlen;
do {
newstop = start + minlen;
if (newstop > stop || numplanes == MAXVISPLANES - 1)
newstop = stop;
newpl = vd.lastvisplane++;
newpl->open = pl->open;
newpl->height = pl->height;
newpl->flatandlight = pl->flatandlight;
newpl->minx = start + 1;
newpl->maxx = newstop;
numplanes++;
if (numplanes >= MAXVISPLANES)
return;
start = newstop;
} while (start < stop);
}
}
// sort visplanes by flatnum so that texture data
// has a better chance to stay in the CPU cache
static void Mars_R_SortPlanes(void)
{
int i, numplanes;
visplane_t* pl;
uint16_t *sortbuf = (uint16_t *)vd.gsortedvisplanes;
i = 0;
numplanes = 0;
for (pl = vd.visplanes + 1; pl < vd.lastvisplane; pl++)
{
// composite sort key: 1b - sign bit, 3b - reverse span length, 12b - flat+light
// to minimize pipeline stalls, the larger planes must be drawn first, hence length inversion
sortbuf[i + 0] = ((unsigned)(3 - ((pl->maxx - pl->minx - 1) >> 6)) << 12) | (pl->flatandlight & 0xFFF);
sortbuf[i + 1] = ++numplanes;
i += 2;
}
D_isort(vd.gsortedvisplanes, numplanes);
}
static void R_PreDrawPlanes(void)
{
int numplanes;
Mars_ClearCacheLine(&vd.lastvisplane);
Mars_ClearCacheLine(&vd.gsortedvisplanes);
// check to see if we still need to fill the sorted planes list
numplanes = vd.lastvisplane - vd.visplanes; // visplane 0 is a dummy plane
if (numplanes > 1)
{
Mars_ClearCacheLines(vd.visplanes, (numplanes * sizeof(visplane_t) + 31) / 16);
if (vd.gsortedvisplanes == NULL)
{
vd.gsortedvisplanes = (int *)vd.viswallextras;
Mars_R_SplitPlanes();
Mars_R_SortPlanes();
}
}
}
void Mars_Sec_R_PreDrawPlanes(void)
{
if (R_TryLockPln())
{
R_PreDrawPlanes();
R_UnlockPln();
}
}
#endif
//
// Render all visplanes
//
void R_DrawPlanes(void)
{
#ifdef MARS
if (R_TryLockPln())
{
R_PreDrawPlanes();
R_UnlockPln();
}
else
{
// the secondary CPU is already on it, take our hands off the bus
Mars_R_SecWait();
}
Mars_R_BeginDrawPlanes();
R_DrawPlanes2();
Mars_R_EndDrawPlanes();
#else
R_DrawPlanes2();
#endif
}
// EOF