Quake II RTX doxygen  1.0 dev
scan.c File Reference
#include "sw.h"

Go to the source code of this file.

Functions

void D_WarpScreen (void)
 
void D_DrawTurbulent16 (espan_t *pspan, int *warptable)
 
void D_DrawSpans16 (espan_t *pspan)
 
void D_DrawZSpans (espan_t *pspan)
 

Function Documentation

◆ D_DrawSpans16()

void D_DrawSpans16 ( espan_t pspan)

Definition at line 222 of file scan.c.

223 {
224  int count, spancount;
225  byte *pbase, *pdest, *ptex;
226  fixed16_t s, t, snext, tnext, sstep, tstep;
227  float sdivz, tdivz, zi, z, du, dv, spancountminus1;
228  float sdivz16stepu, tdivz16stepu, zi16stepu;
229 
230  sstep = 0; // keep compiler happy
231  tstep = 0; // ditto
232 
233  pbase = (byte *)cacheblock;
234 
235  sdivz16stepu = d_sdivzstepu * 16;
236  tdivz16stepu = d_tdivzstepu * 16;
237  zi16stepu = d_zistepu * 16;
238 
239  do {
240  pdest = d_spantable[pspan->v] + pspan->u * VID_BYTES;
241 
242  count = pspan->count;
243 
244  // calculate the initial s/z, t/z, 1/z, s, and t and clamp
245  du = (float)pspan->u;
246  dv = (float)pspan->v;
247 
248  sdivz = d_sdivzorigin + dv * d_sdivzstepv + du * d_sdivzstepu;
249  tdivz = d_tdivzorigin + dv * d_tdivzstepv + du * d_tdivzstepu;
250  zi = d_ziorigin + dv * d_zistepv + du * d_zistepu;
251  z = (float)0x10000 / zi; // prescale to 16.16 fixed-point
252 
253  s = (int)(sdivz * z) + sadjust;
254  if (s > bbextents)
255  s = bbextents;
256  else if (s < 0)
257  s = 0;
258 
259  t = (int)(tdivz * z) + tadjust;
260  if (t > bbextentt)
261  t = bbextentt;
262  else if (t < 0)
263  t = 0;
264 
265  do {
266  // calculate s and t at the far end of the span
267  if (count >= 16)
268  spancount = 16;
269  else
270  spancount = count;
271 
272  count -= spancount;
273 
274  if (q_likely(count)) {
275  // calculate s/z, t/z, zi->fixed s and t at far end of span,
276  // calculate s and t steps across span by shifting
277  sdivz += sdivz16stepu;
278  tdivz += tdivz16stepu;
279  zi += zi16stepu;
280  z = (float)0x10000 / zi; // prescale to 16.16 fixed-point
281 
282  snext = (int)(sdivz * z) + sadjust;
283  if (snext > bbextents)
284  snext = bbextents;
285  else if (snext < 16)
286  snext = 16; // prevent round-off error on <0 steps from
287  // from causing overstepping & running off the
288  // edge of the texture
289 
290  tnext = (int)(tdivz * z) + tadjust;
291  if (tnext > bbextentt)
292  tnext = bbextentt;
293  else if (tnext < 16)
294  tnext = 16; // guard against round-off error on <0 steps
295 
296  sstep = (snext - s) >> 4;
297  tstep = (tnext - t) >> 4;
298  } else {
299  // calculate s/z, t/z, zi->fixed s and t at last pixel in span (so
300  // can't step off polygon), clamp, calculate s and t steps across
301  // span by division, biasing steps low so we don't run off the
302  // texture
303  spancountminus1 = (float)(spancount - 1);
304  sdivz += d_sdivzstepu * spancountminus1;
305  tdivz += d_tdivzstepu * spancountminus1;
306  zi += d_zistepu * spancountminus1;
307  z = (float)0x10000 / zi; // prescale to 16.16 fixed-point
308 
309  snext = (int)(sdivz * z) + sadjust;
310  if (snext > bbextents)
311  snext = bbextents;
312  else if (snext < 16)
313  snext = 16; // prevent round-off error on <0 steps from
314  // from causing overstepping & running off the
315  // edge of the texture
316 
317  tnext = (int)(tdivz * z) + tadjust;
318  if (tnext > bbextentt)
319  tnext = bbextentt;
320  else if (tnext < 16)
321  tnext = 16; // guard against round-off error on <0 steps
322 
323  if (spancount > 1) {
324  sstep = (snext - s) / (spancount - 1);
325  tstep = (tnext - t) / (spancount - 1);
326  }
327  }
328 
329  do {
330  ptex = pbase + (s >> 16) * TEX_BYTES + (t >> 16) * cachewidth;
331  pdest[0] = ptex[2];
332  pdest[1] = ptex[1];
333  pdest[2] = ptex[0];
334  pdest += VID_BYTES;
335  s += sstep;
336  t += tstep;
337  } while (--spancount > 0);
338 
339  s = snext;
340  t = tnext;
341 
342  } while (count > 0);
343 
344  } while ((pspan = pspan->pnext) != NULL);
345 }

Referenced by D_SkySurf(), and D_SolidSurf().

◆ D_DrawTurbulent16()

void D_DrawTurbulent16 ( espan_t pspan,
int warptable 
)

Definition at line 83 of file scan.c.

84 {
85  int count, spancount;
86  byte *pbase, *pdest, *ptex;
87  fixed16_t s, t, snext, tnext, sstep, tstep;
88  float sdivz, tdivz, zi, z, du, dv, spancountminus1;
89  float sdivz16stepu, tdivz16stepu, zi16stepu;
90  int *turb;
91  int turb_s, turb_t;
92 
93  turb = warptable + ((int)(r_newrefdef.time * SPEED) & (CYCLE - 1));
94 
95  sstep = 0; // keep compiler happy
96  tstep = 0; // ditto
97 
98  pbase = (byte *)cacheblock;
99 
100  sdivz16stepu = d_sdivzstepu * 16;
101  tdivz16stepu = d_tdivzstepu * 16;
102  zi16stepu = d_zistepu * 16;
103 
104  do {
105  pdest = d_spantable[pspan->v] + pspan->u * VID_BYTES;
106 
107  count = pspan->count;
108 
109  // calculate the initial s/z, t/z, 1/z, s, and t and clamp
110  du = (float)pspan->u;
111  dv = (float)pspan->v;
112 
113  sdivz = d_sdivzorigin + dv * d_sdivzstepv + du * d_sdivzstepu;
114  tdivz = d_tdivzorigin + dv * d_tdivzstepv + du * d_tdivzstepu;
115  zi = d_ziorigin + dv * d_zistepv + du * d_zistepu;
116  z = (float)0x10000 / zi; // prescale to 16.16 fixed-point
117 
118  s = (int)(sdivz * z) + sadjust;
119  if (s > bbextents)
120  s = bbextents;
121  else if (s < 0)
122  s = 0;
123 
124  t = (int)(tdivz * z) + tadjust;
125  if (t > bbextentt)
126  t = bbextentt;
127  else if (t < 0)
128  t = 0;
129 
130  do {
131  // calculate s and t at the far end of the span
132  if (count >= 16)
133  spancount = 16;
134  else
135  spancount = count;
136 
137  count -= spancount;
138 
139  if (q_likely(count)) {
140  // calculate s/z, t/z, zi->fixed s and t at far end of span,
141  // calculate s and t steps across span by shifting
142  sdivz += sdivz16stepu;
143  tdivz += tdivz16stepu;
144  zi += zi16stepu;
145  z = (float)0x10000 / zi; // prescale to 16.16 fixed-point
146 
147  snext = (int)(sdivz * z) + sadjust;
148  if (snext > bbextents)
149  snext = bbextents;
150  else if (snext < 16)
151  snext = 16; // prevent round-off error on <0 steps from
152  // from causing overstepping & running off the
153  // edge of the texture
154 
155  tnext = (int)(tdivz * z) + tadjust;
156  if (tnext > bbextentt)
157  tnext = bbextentt;
158  else if (tnext < 16)
159  tnext = 16; // guard against round-off error on <0 steps
160 
161  sstep = (snext - s) >> 4;
162  tstep = (tnext - t) >> 4;
163  } else {
164  // calculate s/z, t/z, zi->fixed s and t at last pixel in span (so
165  // can't step off polygon), clamp, calculate s and t steps across
166  // span by division, biasing steps low so we don't run off the
167  // texture
168  spancountminus1 = (float)(spancount - 1);
169  sdivz += d_sdivzstepu * spancountminus1;
170  tdivz += d_tdivzstepu * spancountminus1;
171  zi += d_zistepu * spancountminus1;
172  z = (float)0x10000 / zi; // prescale to 16.16 fixed-point
173 
174  snext = (int)(sdivz * z) + sadjust;
175  if (snext > bbextents)
176  snext = bbextents;
177  else if (snext < 16)
178  snext = 16; // prevent round-off error on <0 steps from
179  // from causing overstepping & running off the
180  // edge of the texture
181 
182  tnext = (int)(tdivz * z) + tadjust;
183  if (tnext > bbextentt)
184  tnext = bbextentt;
185  else if (tnext < 16)
186  tnext = 16; // guard against round-off error on <0 steps
187 
188  if (spancount > 1) {
189  sstep = (snext - s) / (spancount - 1);
190  tstep = (tnext - t) / (spancount - 1);
191  }
192  }
193 
194  s = s & ((CYCLE << 16) - 1);
195  t = t & ((CYCLE << 16) - 1);
196 
197  do {
198  turb_s = ((s + turb[(t >> 16) & (CYCLE - 1)]) >> 16) & TURB_MASK;
199  turb_t = ((t + turb[(s >> 16) & (CYCLE - 1)]) >> 16) & TURB_MASK;
200  ptex = pbase + (turb_t * TURB_SIZE * TEX_BYTES) + turb_s * TEX_BYTES;
201  pdest[0] = ptex[2];
202  pdest[1] = ptex[1];
203  pdest[2] = ptex[0];
204  pdest += VID_BYTES;
205  s += sstep;
206  t += tstep;
207  } while (--spancount > 0);
208 
209  s = snext;
210  t = tnext;
211 
212  } while (count > 0);
213 
214  } while ((pspan = pspan->pnext) != NULL);
215 }

Referenced by D_TurbulentSurf().

◆ D_DrawZSpans()

void D_DrawZSpans ( espan_t pspan)

Definition at line 352 of file scan.c.

353 {
354  int count, doublecount, izistep;
355  int izi;
356  short *pdest;
357  uint32_t ltemp;
358  float zi;
359  float du, dv;
360 
361 // FIXME: check for clamping/range problems
362 // we count on FP exceptions being turned off to avoid range problems
363  izistep = (int)(d_zistepu * 0x8000 * 0x10000);
364 
365  do {
366  pdest = d_zspantable[pspan->v] + pspan->u;
367 
368  count = pspan->count;
369 
370  // calculate the initial 1/z
371  du = (float)pspan->u;
372  dv = (float)pspan->v;
373 
374  zi = d_ziorigin + dv * d_zistepv + du * d_zistepu;
375  // we count on FP exceptions being turned off to avoid range problems
376  izi = (int)(zi * 0x8000 * 0x10000);
377 
378  if ((uintptr_t)pdest & 0x02) {
379  *pdest++ = (short)(izi >> 16);
380  izi += izistep;
381  count--;
382  }
383 
384  if ((doublecount = count >> 1) > 0) {
385  do {
386  ltemp = izi >> 16;
387  izi += izistep;
388  ltemp |= izi & 0xFFFF0000;
389  izi += izistep;
390  *(uint32_t *)pdest = ltemp;
391  pdest += 2;
392  } while (--doublecount > 0);
393  }
394 
395  if (count & 1)
396  *pdest = (short)(izi >> 16);
397 
398  } while ((pspan = pspan->pnext) != NULL);
399 }

Referenced by D_BackgroundSurf(), D_DrawflatSurfaces(), D_DrawZSurfaces(), D_SkySurf(), D_SolidSurf(), and D_TurbulentSurf().

◆ D_WarpScreen()

void D_WarpScreen ( void  )

Definition at line 32 of file scan.c.

33 {
34  int w, h;
35  int u, v, u2, v2;
36  byte *dest;
37  int *turb;
38  int *col;
39  byte **row;
40 
41  static int cached_width, cached_height;
42  static byte *rowptr[MAXHEIGHT * 2 + AMP2 * 2];
43  static int column[MAXWIDTH * 2 + AMP2 * 2];
44 
45  //
46  // these are constant over resolutions, and can be saved
47  //
48  w = r_newrefdef.width;
49  h = r_newrefdef.height;
50  if (w != cached_width || h != cached_height) {
51  cached_width = w;
52  cached_height = h;
53  for (v = 0; v < h + AMP2 * 2; v++) {
54  v2 = (int)((float)v / (h + AMP2 * 2) * r_refdef.vrect.height);
55  rowptr[v] = r_warpbuffer + (WARP_WIDTH * v2) * VID_BYTES;
56  }
57 
58  for (u = 0; u < w + AMP2 * 2; u++) {
59  u2 = (int)((float)u / (w + AMP2 * 2) * r_refdef.vrect.width);
60  column[u] = u2 * VID_BYTES;
61  }
62  }
63 
64  turb = intsintable + ((int)(r_newrefdef.time * SPEED) & (CYCLE - 1));
66 
67  for (v = 0; v < h; v++, dest += vid.rowbytes) {
68  col = &column[turb[v & (CYCLE - 1)]];
69  row = &rowptr[v];
70  for (u = 0; u < w; u++) {
71  dest[u * VID_BYTES + 0] = row[turb[u & (CYCLE - 1)]][col[u] + 0];
72  dest[u * VID_BYTES + 1] = row[turb[u & (CYCLE - 1)]][col[u] + 1];
73  dest[u * VID_BYTES + 2] = row[turb[u & (CYCLE - 1)]][col[u] + 2];
74  }
75  }
76 }

Referenced by R_RenderFrame().

MAXWIDTH
#define MAXWIDTH
Definition: sw.h:58
SPEED
#define SPEED
Definition: sw.h:110
r_newrefdef
refdef_t r_newrefdef
Definition: main.c:28
viddef_t::buffer
pixel_t * buffer
Definition: sw.h:124
d_sdivzstepv
float d_sdivzstepv
Definition: main.c:117
TURB_MASK
#define TURB_MASK
Definition: sw.h:72
cacheblock
pixel_t * cacheblock
Definition: main.c:122
TEX_BYTES
#define TEX_BYTES
Definition: sw.h:50
espan_s::pnext
struct espan_s * pnext
Definition: sw.h:222
CYCLE
#define CYCLE
Definition: sw.h:74
r_refdef
oldrefdef_t r_refdef
Definition: main.c:57
d_zistepu
float d_zistepu
Definition: main.c:116
AMP2
#define AMP2
Definition: sw.h:109
espan_s::count
int count
Definition: sw.h:221
cachewidth
int cachewidth
Definition: main.c:123
bbextentt
fixed16_t bbextentt
Definition: main.c:120
WARP_WIDTH
#define WARP_WIDTH
Definition: sw.h:64
viddef_t::rowbytes
int rowbytes
Definition: sw.h:125
espan_s::v
int v
Definition: sw.h:221
d_tdivzorigin
float d_tdivzorigin
Definition: main.c:118
intsintable
int intsintable[CYCLE *2]
Definition: main.c:134
d_spantable
byte * d_spantable[MAXHEIGHT]
Definition: main.c:130
espan_s::u
int u
Definition: sw.h:221
d_zspantable
short * d_zspantable[MAXHEIGHT]
Definition: main.c:131
d_tdivzstepv
float d_tdivzstepv
Definition: main.c:117
TURB_SIZE
#define TURB_SIZE
Definition: sw.h:71
VID_BYTES
#define VID_BYTES
Definition: sw.h:49
r_warpbuffer
byte r_warpbuffer[WARP_WIDTH *WARP_HEIGHT *VID_BYTES]
Definition: main.c:33
d_tdivzstepu
float d_tdivzstepu
Definition: main.c:116
oldrefdef_t::vrect
vrect_t vrect
Definition: sw.h:132
d_zistepv
float d_zistepv
Definition: main.c:117
sadjust
fixed16_t sadjust
Definition: main.c:120
d_sdivzstepu
float d_sdivzstepu
Definition: main.c:116
d_sdivzorigin
float d_sdivzorigin
Definition: main.c:118
tadjust
fixed16_t tadjust
Definition: main.c:120
vid
viddef_t vid
Definition: main.c:22
int
CONST PIXELFORMATDESCRIPTOR int
Definition: wgl.c:26
MAXHEIGHT
#define MAXHEIGHT
Definition: sw.h:57
bbextents
fixed16_t bbextents
Definition: main.c:120
d_ziorigin
float d_ziorigin
Definition: main.c:118