-
Notifications
You must be signed in to change notification settings - Fork 12
/
r_phase6.c
559 lines (474 loc) · 15.1 KB
/
r_phase6.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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
CALICO
Renderer phase 6 - Seg Loop
*/
#include "r_local.h"
#include "mars.h"
#define MIPSCALE 0x20000
#define LIGHTZSHIFT 10
typedef struct
{
#ifdef MARS
inpixel_t *data;
#else
pixel_t *data;
#endif
VINT height;
drawcol_t drawcol;
// decals stuff
VINT numdecals;
VINT lastcol;
texdecal_t *decals;
uint8_t *columncache;
} drawmip_t;
typedef struct drawtex_s
{
drawmip_t mip[MIPLEVELS];
VINT maxmip;
VINT widthmask;
fixed_t texturemid;
fixed_t topheight;
fixed_t bottomheight;
uint8_t *columncache;
} drawtex_t;
typedef struct
{
viswall_t* segl;
drawtex_t tex[2];
drawtex_t *first, *last;
int lightmin, lightmax, lightsub, lightcoef;
#if MIPLEVELS > 1
unsigned minmip, maxmip;
#endif
} seglocal_t;
static char seg_lock = 0;
static void R_DrawTexture(int x, unsigned iscale, int colnum, fixed_t scale2, int floorclipx, int ceilingclipx, unsigned light, drawtex_t *tex, int miplevel) ATTR_DATA_CACHE_ALIGN;
static void R_DrawSeg(seglocal_t* lseg, unsigned short *clipbounds) ATTR_DATA_CACHE_ALIGN __attribute__((noinline));
static void R_SetupDrawTexture(drawtex_t *drawtex, texture_t *tex, fixed_t texturemid, fixed_t topheight, fixed_t bottomheight) ATTR_DATA_CACHE_ALIGN;
static void R_LockSeg(void) ATTR_DATA_CACHE_ALIGN;
static void R_UnlockSeg(void) ATTR_DATA_CACHE_ALIGN;
void R_SegCommands(void) ATTR_DATA_CACHE_ALIGN __attribute__((noinline));
//
// Render a wall texture as columns
//
static void R_DrawTexture(int x, unsigned iscale, int colnum, fixed_t scale2, int floorclipx, int ceilingclipx, unsigned light, drawtex_t *tex, int miplevel)
{
fixed_t top, bottom;
top = FixedMul(scale2, tex->topheight)>>FRACBITS;
bottom = FixedMul(scale2, tex->bottomheight)>>FRACBITS;
top = centerY - top;
if (top < ceilingclipx)
top = ceilingclipx;
bottom = centerY - bottom;
if (bottom > floorclipx)
bottom = floorclipx;
#if MIPLEVELS <= 1
miplevel = 0;
#endif
// column has no length?
if (top < bottom)
{
int mipcolnum;
drawmip_t *mip;
fixed_t frac;
#ifdef MARS
inpixel_t* src;
#else
pixel_t* src;
#endif
colnum &= tex->widthmask;
mipcolnum = colnum;
frac = tex->texturemid - (centerY - top) * iscale;
#if MIPLEVELS > 1
if (miplevel > tex->maxmip)
miplevel = tex->maxmip;
if (miplevel > 0) {
unsigned m = miplevel;
do {
frac >>= 1;
mipcolnum >>= 1;
iscale >>= 1;
} while (--m);
}
#endif
// CALICO: Jaguar-specific GPU blitter input calculation starts here.
// We invoke a software column drawer instead.
mip = &tex->mip[miplevel];
src = mip->data + mipcolnum * mip->height;
if (mip->numdecals > 0)
{
// decals/composite textures
if (mip->lastcol == colnum)
{
src = mip->columncache;
}
else
{
boolean decaled;
decaled = R_CompositeColumn(colnum, mip->numdecals, mip->decals,
src, mip->columncache, mip->height, miplevel);
if (decaled)
{
src = mip->columncache;
mip->lastcol = colnum;
}
}
}
mip->drawcol(x, top, bottom-1, light, frac, iscale, src, mip->height);
}
}
//
// Main seg drawing loop
//
static void R_DrawSeg(seglocal_t* lseg, unsigned short *clipbounds)
{
viswall_t* segl = lseg->segl;
drawcol_t drawsky = (segl->actionbits & AC_ADDSKY) != 0 ? drawcol : NULL;
fixed_t scalefrac = segl->scalefrac;
const fixed_t scalestep = segl->scalestep;
const unsigned centerangle = segl->centerangle;
unsigned offset = segl->offset;
fixed_t distance = segl->distance;
const int ceilingheight = segl->ceilingheight;
int texturelight = lseg->lightmax;
int lightmax = lseg->lightmax, lightmin = lseg->lightmin,
lightcoef = lseg->lightcoef, lightsub = lseg->lightsub;
const int start = segl->start;
const int stop = segl->stop;
int x;
unsigned miplevel = 0;
drawtex_t *tex;
uint16_t *segcolmask = (segl->actionbits & AC_MIDTEXTURE) ? segl->clipbounds + (stop - start + 1) : NULL;
for (x = start; x <= stop; x++)
{
fixed_t r;
int floorclipx, ceilingclipx;
fixed_t scale2;
unsigned colnum, iscale;
#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 %0, @(16, r0) /* set high bits of the 64-bit dividend */ \n\t"
"mov.l %1, @(0, r0) /* set 32-bit divisor */ \n\t"
"mov #-1, %0\n\t"
"mov.l %0, @(20, r0) /* set low bits of the 64-bit dividend, start divide */\n\t"
: "=&r" (t) : "r" (scalefrac) : "r0");
#else
fixed_t scale = scalefrac;
#endif
scale2 = scalefrac;
scalefrac += scalestep;
//
// get ceilingclipx and floorclipx from clipbounds
//
ceilingclipx = clipbounds[x];
floorclipx = ceilingclipx & 0x00ff;
ceilingclipx = (unsigned)ceilingclipx >> 8;
//
// sky mapping
//
if (drawsky)
{
int top, bottom;
top = ceilingclipx;
bottom = FixedMul(scale2, ceilingheight)>>FRACBITS;
bottom = centerY - bottom;
if (bottom > floorclipx)
bottom = floorclipx;
if (top < bottom)
{
// CALICO: draw sky column
int colnum = ((vd.viewangle + (xtoviewangle[x]<<FRACBITS)) >> ANGLETOSKYSHIFT) & 0xff;
#ifdef MARS
inpixel_t* data = skytexturep->data[0] + colnum * skytexturep->height;
#else
pixel_t* data = skytexturep->data[0] + colnum * skytexturep->height;
#endif
drawsky(x, top, --bottom, 0, (top * 18204) << 2, FRACUNIT + 7281, data, 128);
}
}
//
// texture only stuff
//
if (lightcoef != 0)
{
// calc light level
texturelight = FixedMul(scale2, lightcoef) - lightsub;
if (texturelight < lightmin)
texturelight = lightmin;
else if (texturelight > lightmax)
texturelight = lightmax;
// convert to a hardware value
texturelight = HWLIGHT((unsigned)texturelight>>FRACBITS);
}
// calculate texture offset
r = finetangent((centerangle + (xtoviewangle[x]<<FRACBITS)) >> ANGLETOFINESHIFT);
r = FixedMul(distance, r);
colnum = (offset - r) >> FRACBITS;
if (segcolmask)
segcolmask[x] = texturelight | (colnum & 0xff);
#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" (iscale) : : "r0");
#else
iscale = 0xffffffffu / scale;
#endif
#if MIPLEVELS > 1
// other texture drawing info
miplevel = iscale / MIPSCALE;
if (miplevel > lseg->maxmip)
lseg->maxmip = miplevel;
if (miplevel < lseg->minmip)
lseg->minmip = miplevel;
#endif
for (tex = lseg->first; tex < lseg->last; tex++)
R_DrawTexture(x, iscale, colnum, scale2, floorclipx, ceilingclipx, texturelight, tex, miplevel);
}
}
static void R_LockSeg(void)
{
int res;
do {
__asm volatile (\
"tas.b %1\n\t" \
"movt %0\n\t" \
: "=&r" (res) \
: "m" (seg_lock) \
);
} while (res == 0);
}
static void R_UnlockSeg(void)
{
seg_lock = 0;
}
static void R_SetupDrawTexture(drawtex_t *drawtex, texture_t *tex,
fixed_t texturemid, fixed_t topheight, fixed_t bottomheight)
{
int j;
int width = tex->width, height = tex->height;
uint8_t *columncache = drawtex->columncache;
int mipwidth, mipheight;
drawtex->widthmask = width-1;
drawtex->topheight = topheight;
drawtex->bottomheight = bottomheight;
drawtex->texturemid = texturemid;
#if MIPLEVELS > 1
drawtex->maxmip = !texmips ? 0 : tex->mipcount-1;
#else
drawtex->maxmip = 0;
#endif
mipwidth = width;
mipheight = height;
for (j = 0; j <= drawtex->maxmip; j++)
{
drawmip_t *mip = &drawtex->mip[j];
mip->height = mipheight;
mip->data = tex->data[j];
mip->drawcol = (mipheight & (mipheight - 1)) ? drawcolnpo2 : drawcol;
mipwidth >>= 1, mipheight >>= 1;
if (mipwidth < 1)
mipwidth = 1;
if (mipheight < 1)
mipheight = 1;
// decals stuff
mip->lastcol = -1;
mip->columncache = columncache;
columncache += mipheight;
mip->numdecals = tex->decals & 0x3;
if (mip->numdecals && R_InTexCache(&r_texcache, mip->data)) {
mip->numdecals = 0;
continue;
}
mip->decals = &decals[tex->decals >> 2];
}
}
void R_SegCommands(void)
{
int i, segcount;
seglocal_t lseg;
drawtex_t* toptex, * bottomtex;
int extralight;
uint32_t clipbounds_[SCREENWIDTH / 2 + 1];
uint16_t *clipbounds = (uint16_t *)clipbounds_;
#ifdef MARS
volatile int8_t *addedsegs = (volatile int8_t *)&MARS_SYS_COMM6;
volatile uint8_t *readysegs = (volatile uint8_t *)addedsegs + 1;
#endif
// initialize the clipbounds array
R_InitClipBounds(clipbounds_);
D_memset(&lseg, 0, sizeof(lseg));
toptex = &lseg.tex[0];
bottomtex = &lseg.tex[1];
D_memset(toptex, 0, sizeof(*toptex));
D_memset(bottomtex, 0, sizeof(*bottomtex));
I_SetThreadLocalVar(DOOMTLS_COLORMAP, dc_colormaps);
I_GetThreadLocalVar(DOOMTLS_COLUMNCACHE, toptex->columncache);
bottomtex->columncache = toptex->columncache + COLUMN_CACHE_SIZE;
extralight = vd.extralight;
segcount = vd.lastwallcmd - vd.viswalls;
for (i = 0; i < segcount; i++)
{
int seglight;
unsigned actionbits;
viswall_t* segl = vd.viswalls + i;
#ifdef MARS
if (*addedsegs == -1)
return; // the other cpu is done drawing segments, so should we
while (*readysegs <= i)
continue;
#endif
if (segl->start > segl->stop)
continue;
#ifdef MARS
R_LockSeg();
actionbits = *(volatile short *)&segl->actionbits;
if (actionbits & AC_DRAWN || !(actionbits & (AC_TOPTEXTURE | AC_BOTTOMTEXTURE | AC_MIDTEXTURE | AC_ADDSKY))) {
R_UnlockSeg();
goto post_draw;
} else {
segl->actionbits = actionbits|AC_DRAWN;
R_UnlockSeg();
}
#endif
lseg.segl = segl;
lseg.first = lseg.tex + 1;
lseg.last = lseg.tex + 1;
#if MIPLEVELS > 1
lseg.minmip = MIPLEVELS;
lseg.maxmip = 0;
#endif
if (vd.fixedcolormap)
{
lseg.lightmin = lseg.lightmax = vd.fixedcolormap;
lseg.lightcoef = 0;
}
else
{
seglight = segl->seglightlevel & 0xff;
seglight += (int8_t)((unsigned)segl->seglightlevel >> 8);
if (seglight < 0)
seglight = 0;
#ifdef MARS
lseg.lightmax = seglight;
lseg.lightmax += extralight;
#endif
if (lseg.lightmax > 255)
lseg.lightmax = 255;
#ifdef MARS
seglight = seglight - (255 - seglight - seglight/2) * 2;
#else
seglight = seglight - (255 - seglight) * 2;
#endif
seglight += extralight;
if (seglight < MINLIGHT)
seglight = MINLIGHT;
if (seglight > lseg.lightmax)
seglight = lseg.lightmax;
lseg.lightmin = seglight;
lseg.lightcoef = 0;
if (lseg.lightmin != lseg.lightmax)
lseg.lightcoef = ((unsigned)(lseg.lightmax - lseg.lightmin) << FRACBITS) / (800 - 160);
if (lseg.lightcoef != 0)
{
lseg.lightsub = 160 * lseg.lightcoef;
lseg.lightcoef <<= LIGHTZSHIFT;
lseg.lightmin <<= FRACBITS;
lseg.lightmax <<= FRACBITS;
}
else
{
lseg.lightcoef = 0;
lseg.lightmin = lseg.lightmax = HWLIGHT((unsigned)lseg.lightmax);
}
}
if (actionbits & AC_TOPTEXTURE)
{
R_SetupDrawTexture(toptex, &textures[segl->t_texturenum],
segl->t_texturemid, segl->t_topheight, segl->t_bottomheight);
lseg.first--;
}
if (actionbits & AC_BOTTOMTEXTURE)
{
R_SetupDrawTexture(bottomtex, &textures[segl->b_texturenum],
segl->b_texturemid, segl->b_topheight, segl->b_bottomheight);
lseg.last++;
}
R_DrawSeg(&lseg, clipbounds);
#if MIPLEVELS > 1
if (1)
{
if (lseg.maxmip >= MIPLEVELS)
lseg.maxmip = MIPLEVELS-1;
segl->miplevels[0] = lseg.minmip;
segl->miplevels[1] = lseg.maxmip;
}
else
#endif
{
segl->miplevels[0] = 0;
segl->miplevels[1] = 0;
}
post_draw:
if(actionbits & (AC_NEWFLOOR|AC_NEWCEILING))
{
unsigned w = segl->stop - segl->start + 1;
unsigned short *src = segl->clipbounds + segl->start, *dst = clipbounds + segl->start;
if ((actionbits & (AC_NEWFLOOR|AC_NEWCEILING)) == (AC_NEWFLOOR|AC_NEWCEILING)) {
--src;
do {
*dst++ = *++src;
} while (--w > 0);
}
else {
int8_t *psrc = (int8_t *)src;
int8_t *pdst = (int8_t *)dst;
if (actionbits & AC_NEWFLOOR) {
psrc++, pdst++;
}
do {
*pdst = *psrc, psrc += 2, pdst += 2;
} while (--w > 0);
}
}
}
#ifdef MARS
// mark all segments as rendered
*addedsegs = -1;
#endif
}
#ifdef MARS
void Mars_Sec_R_SegCommands(void)
{
viswall_t *segl;
for (segl = vd.viswalls; segl < vd.lastwallcmd; segl++)
{
if (segl->actionbits & AC_TOPTEXTURE)
{
texture_t* tex = &textures[segl->t_texturenum];
Mars_ClearCacheLines(tex->data, (sizeof(tex->data)+31)/16);
}
if (segl->actionbits & AC_BOTTOMTEXTURE)
{
texture_t* tex = &textures[segl->b_texturenum];
Mars_ClearCacheLines(tex->data, (sizeof(tex->data)+31)/16);
}
if (segl->actionbits & AC_ADDFLOOR)
{
flattex_t *flat = &flatpixels[segl->floorpicnum];
Mars_ClearCacheLines(flat->data, (sizeof(flat->data)+31)/16);
}
if (segl->actionbits & AC_ADDCEILING)
{
flattex_t *flat = &flatpixels[segl->ceilingpicnum];
Mars_ClearCacheLines(flat->data, (sizeof(flat->data)+31)/16);
}
}
R_SegCommands();
}
#endif