setup.c
13.9 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
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
#include "graphic.h"
int pleq_coef();
int debug_overflow = FALSE;
do_setup( vh, vm, vl)
struct Vertex *vh, *vm, *vl;
{
struct Vertex *vt; /* sort temp */
int hpy, mpy, lpy; /* y vertex */
int hpyf, mpyf, lpyf; /* y vertex fraction */
int hdy, mdy, ldy; /* delta y */
int hpx, mpx, lpx; /* x vertex */
int hpxf, mpxf, lpxf; /* x vertex fraction */
int hdx, mdx, ldx; /* delta x */
int hdxf, mdxf, ldxf; /* delta x fraction */
int hxo; /* high x offset to pixel center */
int left = FALSE; /* high-to-low edge is on left */
int ax, ay, bx, by; /* plane equation coefficients */
int r; /* dot product = orientation */
int hpr, hpg, hpb, hpa, hps, hpt, hpw, hpz, hpl; /* high vertex */
int mpr, mpg, mpb, mpa, mps, mpt, mpw, mpz, mpl; /* middle vertex */
int lpr, lpg, lpb, lpa, lps, lpt, lpw, lpz, lpl; /* low vertex */
int dxr, dxg, dxb, dxa, dxs, dxt, dxw, dxz, dxl; /* span edge */
int dyr, dyg, dyb, dya, dys, dyt, dyw, dyz, dyl; /* major edge */
int i;
/* sort vertices by low y */
if (vl->y < vm->y) {
vt = vm; vm = vl; vl = vt;
}
if (vm->y < vh->y) {
vt = vh; vh = vm; vm = vt;
if (vl->y < vm->y) {
vt = vm; vm = vl; vl = vt;
}
}
/* load vertex x and y */
hpy = vh->y;
mpy = vm->y;
lpy = vl->y;
hpx = vh->x;
mpx = vm->x;
lpx = vl->x;
/* subtract for delta x and y */
/* note that vertex x and y have subpixel fractional coordinates, but
that the edge X/Y divides cancel those out */
hdy = lpy - hpy;
mdy = mpy - hpy;
ldy = lpy - mpy;
hdx = lpx - hpx;
mdx = mpx - hpx;
ldx = lpx - mpx;
/* compute plane equation coefficients */
/* note that vertex x and y have subpixel fractional coordinates, but
that the plane equation X/(X*Y) divisor needs to be scaled down
by the subpixel fractional bits */
r = pleq_coef( hdx, hdy, mdx, mdy, &ax, &ay, &bx, &by);
/* divide dxdy */
xy_divide( hdy, &hdx, &hdxf);
xy_divide( mdy, &mdx, &mdxf);
xy_divide( ldy, &ldx, &ldxf);
/* determine orientation of trianlge, long edge left or right :
* if already y sorted and dot product computed, then just use the sign of
* dot product
*/
/*
if (r < 0) left = TRUE;
*/
/* or just sort in x for high edge left or right */
if (hdx < mdx) left = TRUE;
else if (hdx == mdx && hdxf < mdxf) left = TRUE;
/* scale x and y points to integer and fractional parts */
xy_scale( hpy, &hpy, &hpyf);
xy_scale( mpy, &mpy, &mpyf);
xy_scale( lpy, &lpy, &lpyf);
xy_scale( hpx, &hpx, &hpxf);
xy_scale( mpx, &mpx, &mpxf);
/* get attribute offset to pixel center */
/* hxo has the vertex x coordinate. push attributes to the
pixel center (integer x coordinate) of the farthest
subpixel coordinate, which on the left edge is px+dx if dx
is negative, or px if dx is positive, and vv on the right edge */
{
/* should use edge walk data here from x subpixel */
int xi[AA_SP_COUNT], xf[AA_SP_COUNT];
x_subpixel( hpx, hpxf, hdx, hdxf, xi, xf, hpyf);
i = 0;
if ((left && hdx < 0) || (!left && hdx >= 0)) i = AA_SP_COUNT-1;
hxo = (xi[i]<<EW_X_FRAC_SHFT) - ((hpx<<EW_X_FRAC_SHFT)|hpxf);
}
/* attributes */
/* load vertices */
hpr = vh->r; hpg = vh->g; hpb = vh->b; hpa = vh->a;
mpr = vm->r; mpg = vm->g; mpb = vm->b; mpa = vm->a;
lpr = vl->r; lpg = vl->g; lpb = vl->b; lpa = vl->a;
hpz = vh->z; hps = vh->s; hpt = vh->t; hpw = vh->w;
mpz = vm->z; mps = vm->s; mpt = vm->t; mpw = vm->w;
lpz = vl->z; lps = vl->s; lpt = vl->t; lpw = vl->w;
/* compute attribute dx and dy */
dxr = lpr - hpr;
dyr = mpr - hpr;
dxg = lpg - hpg;
dyg = mpg - hpg;
dxb = lpb - hpb;
dyb = mpb - hpb;
dxa = lpa - hpa;
dya = mpa - hpa;
dxs = lps - hps;
dys = mps - hps;
dxt = lpt - hpt;
dyt = mpt - hpt;
dxw = lpw - hpw;
dyw = mpw - hpw;
dxz = lpz - hpz;
dyz = mpz - hpz;
pleq_dxdy( ax, ay, bx, by, dxr, dyr, &dxr, &dyr);
pleq_dxdy( ax, ay, bx, by, dxg, dyg, &dxg, &dyg);
pleq_dxdy( ax, ay, bx, by, dxb, dyb, &dxb, &dyb);
pleq_dxdy( ax, ay, bx, by, dxa, dya, &dxa, &dya);
pleq_dxdy( ax, ay, bx, by, dxs, dys, &dxs, &dys);
pleq_dxdy( ax, ay, bx, by, dxt, dyt, &dxt, &dyt);
pleq_dxdy( ax, ay, bx, by, dxw, dyw, &dxw, &dyw);
pleq_dxdy( ax, ay, bx, by, dxz, dyz, &dxz, &dyz);
/*
printf("Dxy %.5f %.5f, %.5f %.5f\n",
float_ewa( dxs),float_ewa( dxt),
float_ewa( dys),float_ewa( dyt));
*/
/* compute texture coordinate length at vertices for LOD */
tc_length( hps, hpt, hpw, dxs, dxt, dxw, dys, dyt, dyw, &hpl);
tc_length( mps, mpt, mpw, dxs, dxt, dxw, dys, dyt, dyw, &mpl);
tc_length( lps, lpt, lpw, dxs, dxt, dxw, dys, dyt, dyw, &lpl);
dxl = lpl - hpl;
dyl = mpl - hpl;
pleq_dxdy( ax, ay, bx, by, dxl, dyl, &dxl, &dyl);
/* adjust vertex attributes to first pixel center on high edge */
/* Note this adjustment assumes unit dy before conversion to edge slope */
attr_adjust( hpr, dyr, -hpyf, dxr, hxo, &hpr);
attr_adjust( hpg, dyg, -hpyf, dxg, hxo, &hpg);
attr_adjust( hpb, dyb, -hpyf, dxb, hxo, &hpb);
attr_adjust( hpa, dya, -hpyf, dxa, hxo, &hpa);
attr_adjust( hps, dys, -hpyf, dxs, hxo, &hps);
attr_adjust( hpt, dyt, -hpyf, dxt, hxo, &hpt);
attr_adjust( hpw, dyw, -hpyf, dxw, hxo, &hpw);
attr_adjust( hpl, dyl, -hpyf, dxl, hxo, &hpl);
attr_adjust( hpz, dyz, -hpyf, dxz, hxo, &hpz);
/* save for sub-pixel adjust in mem_span (before edge sloped) */
drdx = int_ewa(dxr);
drdy = int_ewa(dyr);
dgdx = int_ewa(dxg);
dgdy = int_ewa(dyg);
dbdx = int_ewa(dxb);
dbdy = int_ewa(dyb);
dadx = int_ewa(dxa);
dady = int_ewa(dya);
/* save for fuzzy depth compare in mem_span (before edge sloped) */
dzdx = /*int_ewa*/(dxz);
dzdy = /*int_ewa*/(dyz);
/* convert attribute dx and dy to edge slope */
attr_slope( dxr, dyr, hdx, &dyr);
attr_slope( dxg, dyg, hdx, &dyg);
attr_slope( dxb, dyb, hdx, &dyb);
attr_slope( dxa, dya, hdx, &dya);
attr_slope( dxs, dys, hdx, &dys);
attr_slope( dxt, dyt, hdx, &dyt);
attr_slope( dxw, dyw, hdx, &dyw);
attr_slope( dxl, dyl, hdx, &dyl);
attr_slope( dxz, dyz, hdx, &dyz);
/*------------------------------------------- write to edge walker */
{
struct EW_xy_edge *ewh, *ewm, *ewl;
struct EW_at_edge *ews, *ewd, *ewp;
/* set orientation */
ew_high[0].left = left;
/* set attribute edge pointers */
ewp = &ew_high[0].pnt;
ewd = &ew_high[0].del;
ews = &ew_span[0].del;
/* xy edge pointers for high, medium, low */
ewh = &ew_high[0].xy;
ewm = &ew_mid[0].xy;
ewl = &ew_low[0].xy;
/* load span edge delta attributes */
ews->r = dxr;
ews->g = dxg;
ews->b = dxb;
ews->a = dxa;
ews->s = dxs;
ews->t = dxt;
ews->w = dxw;
ews->l = dxl;
ews->z = dxz;
/* load major edge attribute point and delta */
ewp->r = hpr;
ewp->g = hpg;
ewp->b = hpb;
ewp->a = hpa;
ewp->s = hps;
ewp->t = hpt;
ewp->w = hpw;
ewp->l = hpl;
ewp->z = hpz;
ewd->r = dyr;
ewd->g = dyg;
ewd->b = dyb;
ewd->a = dya;
ewd->s = dys;
ewd->t = dyt;
ewd->w = dyw;
ewd->l = dyl;
ewd->z = dyz;
/* load x, y for three edges */
/* high edge */
ewh->yp = hpy;
ewh->ypf = hpyf;
x_subpixel( hpx, hpxf, hdx, hdxf, ewh->xpi, ewh->xpf, hpyf);
ewh->xdi = hdx;
ewh->xdf = hdxf;
/* mid edge */
ewm->yp = mpy;
ewm->ypf = mpyf;
x_subpixel( hpx, hpxf, mdx, mdxf, ewm->xpi, ewm->xpf, hpyf);
ewm->xdi = mdx;
ewm->xdf = mdxf;
/* lower edge */
ewl->yp = lpy;
ewl->ypf = lpyf;
x_subpixel( mpx, mpxf, ldx, ldxf, ewl->xpi, ewl->xpf, mpyf);
ewl->xdi = ldx;
ewl->xdf = ldxf;
/* draw the trapezoid */
do_edge_walk();
} /* end load edges */
}
/*-----------------------------------------------------*/
attr_slope( dx, dy, di, dd)
int dx, dy, di, *dd;
{
float temp;
temp = dx * (float)di;
if (temp > (float)0x7FFFFFFF) {
temp = (float)0x7FFFFFFF;
if (debug_overflow)
printf("attr_slope overflow.\n");
}
if (temp < -(float)0x7FFFFFFF) {
temp = -(float)0x7FFFFFFF;
if (debug_overflow)
printf("attr_slope overflow.\n");
}
*dd = (int)temp + dy;
}
/*-----------------------------------------------------*/
int pleq_coef( axi, ayi, bxi, byi, axo, ayo, bxo, byo)
int axi, ayi, bxi, byi;
int *axo, *ayo, *bxo, *byo;
{
float ax, ay, bx, by;
float r;
float s;
float faxo, fayo, fbxo, fbyo;
int sign;
int d, e, f;
ax = (float)axi;
ay = (float)ayi;
bx = (float)bxi;
by = (float)byi;
r = (ax * by - bx * ay);
/* scale r by subpixel so xy/r is unit length */
if (en_su_subpix) r = r * ((float)1/SU_XY_FRAC_ONE);
/* compute reciprocal of r */
d = (int)r;
divide_seed( d, &e);
divide_newt( d, e, &f);
r = (float)f *((float)SU_P_FRAC_ONE/DIV_RECP_FRAC);
/*
if (r != 0) r = (float)SU_P_FRAC_ONE/r;
*/
if (r < 0) sign = -1; else sign = 0;
faxo = ax * r;
fayo = ay * r;
fbxo = bx * r;
fbyo = by * r;
if (faxo > (float)0x7FFFFFFF) {
faxo = (float)0x7FFFFFFF;
if (debug_overflow)
printf("pleq_coef overflow.\n");
}
if (faxo < -(float)0x7FFFFFFF) {
faxo = -(float)0x7FFFFFFF;
if (debug_overflow)
printf("pleq_coef overflow.\n");
}
if (fayo > (float)0x7FFFFFFF) {
fayo = (float)0x7FFFFFFF;
if (debug_overflow)
printf("pleq_coef overflow.\n");
}
if (fayo < -(float)0x7FFFFFFF) {
fayo = -(float)0x7FFFFFFF;
if (debug_overflow)
printf("pleq_coef overflow.\n");
}
if (fbxo > (float)0x7FFFFFFF) {
fbxo = (float)0x7FFFFFFF;
if (debug_overflow)
printf("pleq_coef overflow.\n");
}
if (fbxo < -(float)0x7FFFFFFF) {
fbxo = -(float)0x7FFFFFFF;
if (debug_overflow)
printf("pleq_coef overflow.\n");
}
if (fbyo > (float)0x7FFFFFFF) {
fbyo = (float)0x7FFFFFFF;
if (debug_overflow)
printf("pleq_coef overflow.\n");
}
if (fbyo < -(float)0x7FFFFFFF) {
fbyo = -(float)0x7FFFFFFF;
if (debug_overflow)
printf("pleq_coef overflow.\n");
}
*axo = (int)faxo;
*ayo = (int)fayo;
*bxo = (int)fbxo;
*byo = (int)fbyo;
return (sign);
}
/*-----------------------------------------------------*/
pleq_dxdy( ax, ay, bx, by, ac, bc, cx, cy)
int ax, ay, bx, by;
int ac, bc, *cx, *cy;
{
float fcx, fcy;
fcx = ((float)by * (float)ac - (float)ay * (float)bc)
* (float)EW_A_FRAC_ONE/SU_P_FRAC_ONE;
fcy = ((float)ax * (float)bc - (float)bx * (float)ac)
* (float)EW_A_FRAC_ONE/SU_P_FRAC_ONE;
if (fcx > (float)0x7FFFFFFF) {
fcx = (float)0x7FFFFFFF;
if (debug_overflow)
printf("pleq_dxdy overflow.\n");
}
if (fcx < -(float)0x7FFFFFFF) {
fcx = -(float)0x7FFFFFFF;
if (debug_overflow)
printf("pleq_dxdy overflow.\n");
}
if (fcy > (float)0x7FFFFFFF) {
fcy = (float)0x7FFFFFFF;
if (debug_overflow)
printf("pleq_dxdy overflow.\n");
}
if (fcy < -(float)0x7FFFFFFF) {
fcy = -(float)0x7FFFFFFF;
if (debug_overflow)
printf("pleq_dxdy overflow.\n");
}
*cx = (int)fcx;
*cy = (int)fcy;
}
/*-----------------------------------------------------*/
attr_adjust( pi, dy, ay, dx, ax, po)
int pi, dy, ay, dx, ax, *po;
{
float fpx, fpy;
fpx = (float)dy * (float)ay/EW_X_FRAC_ONE;
fpy = (float)dx * (float)ax/EW_X_FRAC_ONE;
if (fpx > (float)0x7FFFFFFF) {
fpx = (float)0x7FFFFFFF;
if (debug_overflow)
printf("attr_adjust overflow.\n");
}
if (fpx < -(float)0x7FFFFFFF) {
fpx = -(float)0x7FFFFFFF;
if (debug_overflow)
printf("attr_adjust overflow.\n");
}
if (fpy > (float)0x7FFFFFFF) {
fpy = (float)0x7FFFFFFF;
if (debug_overflow)
printf("attr_adjust overflow.\n");
}
if (fpy < -(float)0x7FFFFFFF) {
fpy = -(float)0x7FFFFFFF;
if (debug_overflow)
printf("attr_adjust overflow.\n");
}
*po = (pi << EW_A_FRAC_SHFT) + (int)fpx + (int)fpy;
}
/*-----------------------------------------------------*/
xy_divide( d, n, nf)
int d, *n, *nf;
{
int r;
int i;
divide_seed( d, &r);
i = (int)((float)*n * (float)r
* ((float)EW_X_FRAC_ONE/(float)DIV_RECP_FRAC) );
*nf = i & EW_X_FRAC_MASK;
*n = i >> EW_X_FRAC_SHFT;
}
/*-----------------------------------------------------*/
xy_scale( d, n, nf)
int d, *n, *nf;
{
int i;
if (en_su_subpix) {
i = (int)((float)d * (float)EW_X_FRAC_ONE/SU_XY_FRAC_ONE);
*nf = i & EW_X_FRAC_MASK;
*n = i >> EW_X_FRAC_SHFT;
}
else {
*nf = 0;
*n = d;
}
}
/*-----------------------------------------------------*/
xy_adjust( a, p, pf, d, df, o, of)
int a, p, pf, d, df, *o, *of;
{
int i, j;
i = (p << EW_X_FRAC_SHFT) | pf;
j = (d << EW_X_FRAC_SHFT) | df;
i += (int)((float)j * (float)a/EW_X_FRAC_ONE);
*of = i & EW_X_FRAC_MASK;
*o = i >> EW_X_FRAC_SHFT;
}
/*-----------------------------------------------------*/
x_subpixel( px, pxf, dx, dxf, xpi, xpf, yf)
int px, pxf, dx, dxf, *xpi, *xpf, yf;
/* compute edge x points at subpixel resolution */
{
int ex, edx, nx;
int i;
yf = -yf;
/* cheat and make x values 32 bits for now */
ex = (px << EW_X_FRAC_SHFT) | pxf;
edx = (dx << EW_X_FRAC_SHFT) | dxf;
for (i=0; i< AA_SP_COUNT; i++) {
nx = (int)((float)ex +
(float)edx*(float)yf*(float)1/EW_X_FRAC_ONE);
xpi[i] = nx >> EW_X_FRAC_SHFT;
xpf[i] = nx & EW_X_FRAC_MASK;
yf += 1<<(EW_X_FRAC_SHFT-SU_XY_FRAC_SHFT);
}
}
/*-----------------------------------------------------*/
tc_length( ps, pt, pw, dxs, dxt, dxw, dys, dyt, dyw, pl)
int ps, pt, pw, dxs, dxt, dxw, dys, dyt, dyw, *pl;
{
int os, ot, ow;
int qs, qt, qw;
float so, sq;
/* compute texture coordinate at one pixel diagonal */
os = ps + int_ewa(dxs);
ot = pt + int_ewa(dxt);
ow = pw + int_ewa(dxw);
qs = ps + int_ewa(dys);
qt = pt + int_ewa(dyt);
qw = pw + int_ewa(dyw);
/* perspective correct texture coords */
/* XXXX should use vertex input coords to avoid divide */
ps = (int)((float)TC_PERSP_ONE*(float)ps/(float)pw);
pt = (int)((float)TC_PERSP_ONE*(float)pt/(float)pw);
os = (int)((float)TC_PERSP_ONE*(float)os/(float)ow);
ot = (int)((float)TC_PERSP_ONE*(float)ot/(float)ow);
qs = (int)((float)TC_PERSP_ONE*(float)qs/(float)qw);
qt = (int)((float)TC_PERSP_ONE*(float)qt/(float)qw);
/* compute vector length */
os = abs( os - ps);
ot = abs( ot - pt);
qs = abs( qs - ps);
qt = abs( qt - pt);
so = (float)os*(float)os+(float)ot*(float)ot;
so = sqrt( so);
sq = (float)qs*(float)qs+(float)qt*(float)qt;
sq = sqrt( sq);
if (so > sq) sq = so;
*pl = (int)sq;
}
/*-----------------------------------------------------*/