-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathr_phase2.c
More file actions
427 lines (343 loc) · 11.3 KB
/
r_phase2.c
File metadata and controls
427 lines (343 loc) · 11.3 KB
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
/*
CALICO
Renderer phase 2 - Wall prep
*/
#include "doomdef.h"
#include "r_local.h"
#include "mars.h"
static fixed_t R_PointToDist(fixed_t x, fixed_t y) ATTR_DATA_CACHE_ALIGN;
static fixed_t R_ScaleFromGlobalAngle(fixed_t rw_distance, angle_t visangle, angle_t normalangle) ATTR_DATA_CACHE_ALIGN;
static uint16_t P_SegOffset(seg_t *seg) ATTR_DATA_CACHE_ALIGN;
static void R_SetupCalc(viswall_t* wc, fixed_t hyp, angle_t normalangle, int angle1) ATTR_DATA_CACHE_ALIGN;
void R_WallLatePrep(viswall_t* wc, mapvertex_t *verts) ATTR_DATA_CACHE_ALIGN;
// the volatiles help gcc produce less silly SH2 assembler code
static void R_SegLoop(viswall_t* segl, unsigned short *restrict clipbounds) ATTR_DATA_CACHE_ALIGN __attribute__((noinline));
void R_WallPrep(void) ATTR_DATA_CACHE_ALIGN __attribute__((noinline));
//
// Get distance to point in 3D projection
//
static fixed_t R_PointToDist(fixed_t x, fixed_t y)
{
angle_t angle;
fixed_t dx, dy, temp;
dx = D_abs(x - vd->viewx);
dy = D_abs(y - vd->viewy);
if (dy > dx)
{
temp = dx;
dx = dy;
dy = temp;
}
angle = (tantoangle[(unsigned)FixedDiv(dy, dx) >> DBITS] + ANG90) >> ANGLETOFINESHIFT;
// use as cosine
return FixedDiv(dx, finesine(angle));
}
//
// Convert angle and distance within view frustum to texture scale factor.
//
static fixed_t R_ScaleFromGlobalAngle(fixed_t rw_distance, angle_t visangle, angle_t normalangle)
{
angle_t anglea, angleb;
fixed_t num, den;
int sinea, sineb;
visangle += ANG90;
anglea = visangle - vd->viewangle;
sinea = finesine(anglea >> ANGLETOFINESHIFT);
angleb = visangle - normalangle;
sineb = finesine(angleb >> ANGLETOFINESHIFT);
num = FixedMul(stretchX, sineb);
den = FixedMul(rw_distance, sinea);
return FixedDiv(num, den);
}
//
// Setup texture calculations for lines with upper and lower textures
//
static void R_SetupCalc(viswall_t* wc, fixed_t hyp, angle_t normalangle, int angle1)
{
fixed_t sineval, rw_offset;
angle_t offsetangle;
offsetangle = normalangle - angle1;
if (offsetangle > ANG180)
offsetangle = 0 - offsetangle;
if (offsetangle > ANG90)
offsetangle = ANG90;
sineval = finesine(offsetangle >> ANGLETOFINESHIFT);
rw_offset = FixedMul(hyp, sineval);
if (normalangle - angle1 < ANG180)
rw_offset = -rw_offset;
wc->offset += rw_offset;
wc->centerangle = ANG90 + vd->viewangle - normalangle;
}
static uint16_t P_SegOffset(seg_t *seg)
{
int side = SEG_UNPACK_SIDE(seg);
mapvertex_t *sv = &vertexes[lines[SEG_UNPACK_LINEDEF(seg)].v1];
mapvertex_t *ev = &vertexes[lines[SEG_UNPACK_LINEDEF(seg)].v2];
mapvertex_t *v1 = &vertexes[SEG_UNPACK_V1(seg)];
uint16_t dx, dy;
uint16_t g, c;
unsigned n;
if (side)
{
dx = D_abs(ev->x - v1->x);
dy = D_abs(ev->y - v1->y);
}
else
{
dx = D_abs(sv->x - v1->x);
dy = D_abs(sv->y - v1->y);
}
if (dx == 0)
return dy;
if (dy == 0)
return dx;
// integer square root
n = dx*dx + dy*dy;
g = c = 0x8000;
for ( ; ; ) {
if (g*g > n) g ^= c;
c >>= 1;
if (c == 0) return g;
g |= c;
}
return g;
}
void R_WallLatePrep(viswall_t* wc, mapvertex_t *verts)
{
angle_t distangle, offsetangle, normalangle;
seg_t *seg = wc->seg;
angle_t angle1 = wc->scalestep;
fixed_t sineval, rw_distance;
fixed_t scalefrac, scale2;
fixed_t hyp;
fixed_t x1, y1, x2, y2;
int nv1 = SEG_UNPACK_V1(seg);
int nv2 = SEG_UNPACK_V2(seg);
int offset = P_SegOffset(seg);
// this is essentially R_StoreWallRange
// calculate rw_distance for scale calculation
x1 = verts[nv1].x << FRACBITS;
y1 = verts[nv1].y << FRACBITS;
x2 = verts[nv2].x << FRACBITS;
y2 = verts[nv2].y << FRACBITS;
hyp = R_PointToDist(x1, y1);
normalangle = R_PointToAngle(x1, y1, x2, y2);
normalangle += ANG90;
offsetangle = normalangle - angle1;
if ((int)offsetangle < 0)
offsetangle = 0 - offsetangle;
if (offsetangle > ANG90)
offsetangle = ANG90;
distangle = ANG90 - offsetangle;
sineval = finesine(distangle >> ANGLETOFINESHIFT);
rw_distance = FixedMul(hyp, sineval);
wc->distance = rw_distance;
wc->offset = ((fixed_t)wc->offset + offset) << FRACBITS;
scalefrac = scale2 = wc->scalefrac =
R_ScaleFromGlobalAngle(rw_distance, vd->viewangle + (xtoviewangle[wc->start]<<FRACBITS), normalangle);
if (wc->stop > wc->start)
{
scale2 = R_ScaleFromGlobalAngle(rw_distance, vd->viewangle + (xtoviewangle[wc->stop]<<FRACBITS), normalangle);
#ifdef MARS
SH2_DIVU_DVSR = wc->stop - wc->start; // set 32-bit divisor
SH2_DIVU_DVDNT = scale2 - scalefrac; // set 32-bit dividend, start divide
#else
wc->scalestep = IDiv(scale2 - scalefrac, wc->stop - wc->start);
#endif
}
wc->scale2 = scale2;
// does line have top or bottom textures?
if (wc->actionbits & (AC_TOPTEXTURE | AC_BOTTOMTEXTURE | AC_MIDTEXTURE))
{
R_SetupCalc(wc, hyp, normalangle, angle1);// do calc setup
}
wc->clipbounds = NULL;
const int start = wc->start;
const int stop = wc->stop;
const int width = stop - start + 1;
if (wc->actionbits & (AC_NEWFLOOR | AC_NEWCEILING | AC_TOPSIL | AC_BOTTOMSIL | AC_MIDTEXTURE))
{
wc->clipbounds = vd->lastsegclip - start;
vd->lastsegclip += width;
}
if (wc->actionbits & AC_MIDTEXTURE)
{
// lighting + column
//D_memset(vd->lastsegclip, 255, sizeof(*vd->lastsegclip)*width);
vd->lastsegclip += width;
}
#ifdef MARS
if (wc->stop > wc->start)
{
wc->scalestep = SH2_DIVU_DVDNT; // get 32-bit quotient
}
#endif
}
//
// Main seg clipping loop
//
static void R_SegLoop(viswall_t* segl, unsigned short * restrict clipbounds)
{
const volatile int actionbits = segl->actionbits;
fixed_t scalefrac = segl->scalefrac;
const fixed_t scalestep = segl->scalestep;
int x, start = segl->start;
const int stop = segl->stop;
const volatile fixed_t ceilingheight = segl->ceilingheight << 12;
const int floorandlight = ((segl->seglightlevel & 0xff) << 16) | segl->floorpicnum;
const int ceilandlight = ((segl->seglightlevel & 0xff) << 16) | segl->ceilingpicnum;
unsigned short * restrict flooropen = (actionbits & AC_ADDFLOOR) ? vd->visplanes[0].open : NULL;
unsigned short * restrict ceilopen = (actionbits & AC_ADDCEILING) ? vd->visplanes[0].open : NULL;
volatile fixed_t floorheight = segl->floorheight << 12;
volatile fixed_t floornewheight = segl->floornewheight << 12;
volatile fixed_t ceilingnewheight = segl->ceilingnewheight << 12;
unsigned short * restrict newclipbounds = segl->clipbounds;
const int cy = centerY;
const int vh = viewportHeight;
if (!(actionbits & (AC_ADDFLOOR|AC_ADDCEILING)) && !newclipbounds)
return;
x = start;
do {
fixed_t floorclipx, ceilingclipx;
fixed_t low, high, top, bottom;
fixed_t scale2;
scale2 = scalefrac;
scalefrac += scalestep;
//
// get ceilingclipx and floorclipx from clipbounds
//
ceilingclipx = clipbounds[x];
floorclipx = ceilingclipx & 0x00ff;
ceilingclipx = ((unsigned)ceilingclipx & 0xff00) >> 8;
//
// calc high and low
//
low = FixedMul(scale2, floornewheight)>>FRACBITS;
low = cy - low;
if (low < 0)
low = 0;
else if (low > floorclipx)
low = floorclipx;
high = FixedMul(scale2, ceilingnewheight)>>FRACBITS;
high = cy - high;
if (high > vh)
high = vh;
else if (high < ceilingclipx)
high = ceilingclipx;
if (newclipbounds)
{
const int newclip = actionbits & (AC_NEWFLOOR|AC_NEWCEILING);
newclipbounds[x] = (high << 8) | low;
if (!(newclip & AC_NEWFLOOR))
low = floorclipx;
if (!(newclip & AC_NEWCEILING))
high = ceilingclipx;
// rewrite clipbounds
clipbounds[x] = (high << 8) | low;
}
//
// floor
//
if (flooropen)
{
top = FixedMul(scale2, floorheight)>>FRACBITS;
top = cy - top;
if (top < ceilingclipx)
top = ceilingclipx;
bottom = floorclipx;
if (top < bottom)
{
if (!MARKEDOPEN(flooropen[x]))
{
visplane_t *floor = R_FindPlane(floorheight, floorandlight, x, stop);
flooropen = floor->open;
}
flooropen[x] = (top << 8) + (bottom-1);
}
}
//
// ceiling
//
if (ceilopen)
{
top = ceilingclipx;
bottom = FixedMul(scale2, ceilingheight)>>FRACBITS;
bottom = cy - bottom;
if (bottom > floorclipx)
bottom = floorclipx;
if (top < bottom)
{
if (!MARKEDOPEN(ceilopen[x]))
{
visplane_t *ceiling = R_FindPlane(ceilingheight, ceilandlight, x, stop);
ceilopen = ceiling->open;
}
ceilopen[x] = (top << 8) + (bottom-1);
}
}
} while (++x <= stop);
}
#ifdef MARS
void Mars_Sec_R_WallPrep(void)
{
int i;
uint16_t *vp0open;
viswall_t *segl;
viswall_t *first, *last, *verylast;
uint32_t clipbounds_[SCREENWIDTH/2+1];
__attribute__((aligned(16)))
visplane_t *visplanes_hash_[NUM_VISPLANES_BUCKETS];
uint16_t *clipbounds = (uint16_t *)clipbounds_;
mapvertex_t *verts;
volatile uint8_t *addedsegs = (volatile uint8_t *)&MARS_SYS_COMM6;
volatile uint8_t *readysegs = addedsegs + 1;
// clear visplane 0
vp0open = vd->visplanes[0].open - 1;
for (i = 0; i < SCREENWIDTH+2; i++)
vp0open[i] = 0;
vd->visplanes_hash = visplanes_hash_;
for (i = 0; i < NUM_VISPLANES_BUCKETS; i++)
vd->visplanes_hash[i] = NULL;
R_InitClipBounds(clipbounds_);
first = last = vd->viswalls;
verylast = NULL;
verts = vertexes;
I_SetBankPage(segspage);
for (segl = first; segl != verylast; )
{
int8_t nextsegs = *addedsegs;
// check if master CPU finished exec'ing R_BSP()
if (nextsegs == -2)
{
Mars_ClearCacheLine(&vd->lastwallcmd);
verylast = vd->lastwallcmd;
last = verylast;
}
else
{
last = first + (uint8_t)nextsegs;
}
for (; segl < last; segl++)
{
R_WallLatePrep(segl, verts);
R_SegLoop(segl, clipbounds);
*readysegs = *readysegs + 1;
}
}
}
#else
void R_WallPrep(void)
{
viswall_t* segl;
unsigned clipbounds_[SCREENWIDTH/2+1];
unsigned short *clipbounds = (unsigned short *)clipbounds_;
R_InitClipBounds(clipbounds_);
for (segl = vd->viswalls; segl != vd->lastwallcmd; segl++)
{
fixed_t floornewheight = 0, ceilingnewheight = 0;
R_WallLatePrep(segl, vertexes);
R_SegLoop(segl, clipbounds, floornewheight, ceilingnewheight);
}
}
#endif
// EOF