-
Notifications
You must be signed in to change notification settings - Fork 14
/
r_phase2.c
365 lines (295 loc) · 9.8 KB
/
r_phase2.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
/*
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 void R_SetupCalc(viswall_t* wc, fixed_t hyp, angle_t normalangle, int angle1) ATTR_DATA_CACHE_ALIGN;
void R_WallLatePrep(viswall_t* wc, vertex_t *verts) ATTR_DATA_CACHE_ALIGN;
static void R_SegLoop(viswall_t* segl, unsigned short* clipbounds, fixed_t floorheight,
fixed_t floornewheight, fixed_t ceilingnewheight) 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)
{
int 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[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;
}
void R_WallLatePrep(viswall_t* wc, vertex_t *verts)
{
angle_t distangle, offsetangle, normalangle;
seg_t* seg = wc->seg;
int angle1 = wc->scalestep;
fixed_t sineval, rw_distance;
fixed_t scalefrac, scale2;
fixed_t hyp;
// this is essentially R_StoreWallRange
// calculate rw_distance for scale calculation
normalangle = R_PointToAngle2(verts[seg->v1].x, verts[seg->v1].y,
verts[seg->v2].x, verts[seg->v2].y);
normalangle += ANG90;
offsetangle = normalangle - angle1;
if ((int)offsetangle < 0)
offsetangle = 0 - offsetangle;
if (offsetangle > ANG90)
offsetangle = ANG90;
distangle = ANG90 - offsetangle;
hyp = R_PointToDist(verts[seg->v1].x, verts[seg->v1].y);
sineval = finesine(distangle >> ANGLETOFINESHIFT);
rw_distance = FixedMul(hyp, sineval);
wc->distance = rw_distance;
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
}
#ifdef MARS
if (wc->stop > wc->start)
{
wc->scalestep = SH2_DIVU_DVDNT; // get 32-bit quotient
}
#endif
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;
}
}
//
// Main seg clipping loop
//
static void R_SegLoop(viswall_t* segl, unsigned short* clipbounds,
fixed_t floorheight, fixed_t floornewheight, fixed_t ceilingnewheight)
{
const volatile int actionbits = segl->actionbits;
fixed_t scalefrac = segl->scalefrac;
volatile const fixed_t scalestep = segl->scalestep;
int x, start = segl->start;
const int stop = segl->stop;
const fixed_t ceilingheight = segl->ceilingheight;
const int floorandlight = ((segl->seglightlevel & 0xff) << 16) | segl->floorpicnum;
const int ceilandlight = ((segl->seglightlevel & 0xff) << 16) | segl->ceilingpicnum;
unsigned short *flooropen = (actionbits & AC_ADDFLOOR) ? vd.visplanes[0].open : NULL;
unsigned short *ceilopen = (actionbits & AC_ADDCEILING) ? vd.visplanes[0].open : NULL;
unsigned short *newclipbounds = segl->clipbounds;
const int cyvh = (centerY << 16) | viewportHeight;
for (x = start; x <= stop; x++)
{
int floorclipx, ceilingclipx;
int low, high, top, bottom;
int cy, vh;
fixed_t scale2;
scale2 = scalefrac;
scalefrac += scalestep;
//
// get ceilingclipx and floorclipx from clipbounds
//
ceilingclipx = clipbounds[x];
floorclipx = ceilingclipx & 0x00ff;
ceilingclipx = ((unsigned)ceilingclipx & 0xff00) >> 8;
cy = cyvh >> 16;
vh = cyvh & 0xffff;
//
// 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)
{
newclipbounds[x] = (high << 8) | low;
}
int newclip = actionbits & (AC_NEWFLOOR|AC_NEWCEILING);
if (newclip)
{
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);
}
}
}
}
#ifdef MARS
void Mars_Sec_R_WallPrep(void)
{
viswall_t *segl;
viswallextra_t *seglex;
viswall_t *first, *last, *verylast;
uint32_t clipbounds_[SCREENWIDTH/2+1];
uint16_t *clipbounds = (uint16_t *)clipbounds_;
vertex_t *verts;
volatile uint8_t *addedsegs = (volatile uint8_t *)&MARS_SYS_COMM6;
volatile uint8_t *readysegs = addedsegs + 1;
R_InitClipBounds(clipbounds_);
first = last = vd.viswalls;
verylast = NULL;
seglex = vd.viswallextras;
verts = W_GetLumpData(gamemaplump+ML_VERTEXES);
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++)
{
#ifdef MARS
Mars_ClearCacheLine(seglex);
#endif
R_WallLatePrep(segl, verts);
R_SegLoop(segl, clipbounds, seglex->floorheight, seglex->floornewheight, seglex->ceilnewheight);
seglex++;
*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