edge_walk.c
8.66 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
#include "graphic.h"
#define EW_X_SUBPIX_ROUND (EW_X_FRAC_MASK>>SU_XY_FRAC_SHFT)
/* minor edge state transitions */
#define EW_MINOR_BEG 1 /* in upper minor edge */
#define EW_MINOR_MID 2 /* exit upper minor edge */
#define EW_MINOR_LOW 3 /* enter lower minor edge */
#define EW_MINOR_END 4 /* in lower minor edge */
/*------------------------------------------*/
do_edge_walk()
{
struct EW_major_edge *ewh;
struct EW_minor_edge *ewm, *ewl;
struct EW_span_edge *ews;
struct MS_span_data *ms;
struct MS_subpixel *msh, *msm;
struct CZ_step *czs;
struct TC_step *tcs;
int minor_state; /* status of minor edge */
int hx_index, mx_index, lx_index; /* subpixel to bound span */
int hx_carry; /* attribute dx step control */
int sp_mask; /* subpixel enable mask */
int hu_mask, mu_mask; /* update subpixel mask */
int hy_mask, my_mask; /* valid subpixel mask */
int beg_mask, end_mask, mid_mask, low_mask; /* y edge masks */
int xl, xr; /* span x bounds */
int sy; /* y subpixel values */
int i, j;
/* set edge pointers */
ewh = &ew_high[0];
ewm = &ew_mid[0];
ewl = &ew_low[0];
ews = &ew_span[0];
/* initialize mem_span and sort outputs */
ms = &mem_span_data[0];
ms->left = ewh->left;
if (ewh->left) {
msh = &ms->s_left;
msm = &ms->s_right;
} else {
msh = &ms->s_right;
msm = &ms->s_left;
}
/* initialize cz_step and tc_step */
tcs = &tc_step[0];
czs = &cz_step[0];
if (ewh->left) {
czs->del.r = ews->del.r;
czs->del.g = ews->del.g;
czs->del.b = ews->del.b;
czs->del.a = ews->del.a;
czs->del.z = ews->del.z;
tcs->del.s = ews->del.s;
tcs->del.t = ews->del.t;
tcs->del.w = ews->del.w;
tcs->del.l = ews->del.l;
} else {
czs->del.r = -ews->del.r;
czs->del.g = -ews->del.g;
czs->del.b = -ews->del.b;
czs->del.a = -ews->del.a;
czs->del.z = -ews->del.z;
tcs->del.s = -ews->del.s;
tcs->del.t = -ews->del.t;
tcs->del.w = -ews->del.w;
tcs->del.l = -ews->del.l;
}
/* convert attribute dx to dy+dx on x carry */
ews->del.r += ewh->del.r;
ews->del.g += ewh->del.g;
ews->del.b += ewh->del.b;
ews->del.a += ewh->del.a;
ews->del.z += ewh->del.z;
ews->del.s += ewh->del.s;
ews->del.t += ewh->del.t;
ews->del.w += ewh->del.w;
ews->del.l += ewh->del.l;
/* if not antialiasing, draw only if integer scanline covered */
/* XXXX this an only optimization to skip spans */
if (en_antialias) sp_mask = AA_SP_MASK;
else sp_mask = 1 << (AA_SP_COUNT-1);
/* select x subpixel to use */
hx_index = 0;
mx_index = 0;
lx_index = 0;
/* if left dx negative, use last x subpixel */
/* if right dx positive, use last x subpixel */
if ((ewh->left && ewh->xy.xdi < 0)||(!ewh->left && ewh->xy.xdi >= 0))
hx_index = (AA_SP_COUNT-1);
if ((ewh->left && ewm->xy.xdi >= 0)||(!ewh->left && ewm->xy.xdi < 0))
mx_index = (AA_SP_COUNT-1);
if ((ewh->left && ewl->xy.xdi >= 0)||(!ewh->left && ewl->xy.xdi < 0))
lx_index = (AA_SP_COUNT-1);
/* generate y edge masks */
sy = (ewh->xy.ypf) >> (EW_X_FRAC_SHFT - AA_SP_BITS);
beg_mask = sp_decode_b[ sy];
sy = (ewl->xy.ypf) >> (EW_X_FRAC_SHFT - AA_SP_BITS);
end_mask = sp_decode_e[ sy];
sy = (ewm->xy.ypf) >> (EW_X_FRAC_SHFT - AA_SP_BITS);
mid_mask = sp_decode_e[ sy];
low_mask = ~mid_mask & AA_SP_MASK; /* complement mid mask */
/* on first span, using begin mask on both edges, update all subpixels */
hy_mask = beg_mask;
my_mask = beg_mask;
hu_mask = AA_SP_MASK;
mu_mask = AA_SP_MASK;
/* EDGE_LOOP */
minor_state = EW_MINOR_BEG;
while (ewh->xy.yp <= ewl->xy.yp) { /* until major edge expires */
/* last line of first minor edge */
if (ewh->xy.yp == ewm->xy.yp && minor_state == EW_MINOR_BEG) {
my_mask &= mid_mask; /* don't & mu_mask, update all subpixels */
/* if second minor edge is not zero, skip drawing span and advance edge */
if ((ewm->xy.yp != ewl->xy.yp)||(ewm->xy.ypf != ewl->xy.ypf))
minor_state = EW_MINOR_MID;
/* XXXX note, unfortunately need to special case zero height lower
edges in this way because the xl/xr subpixel selection gets bogus
values (because of infinite slope). maybe could simplify that by
not updating the edge */
else minor_state = EW_MINOR_END; /* finish, no lower minor edge */
}
/* first line of next minor edge */
if (minor_state == EW_MINOR_LOW) {
minor_state = EW_MINOR_END;
mx_index = lx_index;
mu_mask = low_mask; /* update following subpixels */
my_mask |= low_mask; /* enable following subpixels */
ewm = ewl; /* new edge */
}
/* last line of major edge */
if (ewh->xy.yp == ewl->xy.yp) {
my_mask &= end_mask; /* enable preceeding subpixels */
hy_mask &= end_mask;
}
/* set subpixel valid bits */
msh->xv = hy_mask;
msm->xv = my_mask;
/* load x subpixels */
j = 1 << (AA_SP_COUNT-1);
for (i=0; i<AA_SP_COUNT; i++) {
if (hu_mask & j) {
msh->xi[i] = ewh->xy.xpi[i];
/*
msh->xf[i] = ewh->xy.xpf[i] & EW_X_SUBPIX_MASK;
*/
msh->xf[i] = ewh->xy.xpf[i] + EW_X_SUBPIX_ROUND;
if (msh->xf[i] & ~EW_X_FRAC_MASK) msh->xi[i]++;
msh->xf[i] &= EW_X_SUBPIX_MASK;
}
if (mu_mask & j) {
msm->xi[i] = ewm->xy.xpi[i];
/*
msm->xf[i] = ewm->xy.xpf[i] & EW_X_SUBPIX_MASK;
*/
msm->xf[i] = ewm->xy.xpf[i] + EW_X_SUBPIX_ROUND;
if (msm->xf[i] & ~EW_X_FRAC_MASK) msm->xi[i]++;
msm->xf[i] &= EW_X_SUBPIX_MASK;
}
j = j >> 1;
}
/* scissor subpixel X */
{
int sl = sc_xmin << (EW_X_FRAC_SHFT-SU_XY_FRAC_SHFT);
int sr = sc_xmax << (EW_X_FRAC_SHFT-SU_XY_FRAC_SHFT);
for (i=0; i<AA_SP_COUNT; i++) {
if (ewh->left) {
if (((msh->xi[i]<<EW_X_FRAC_SHFT)|msh->xf[i]) < sl) {
msh->xi[i] = sl >> EW_X_FRAC_SHFT;
msh->xf[i] = sl & EW_X_FRAC_MASK;
}
if (((msm->xi[i]<<EW_X_FRAC_SHFT)|msm->xf[i]) > sr) {
msm->xi[i] = sr >> EW_X_FRAC_SHFT;
msm->xf[i] = sr & EW_X_FRAC_MASK;
}
} else {
if (((msm->xi[i]<<EW_X_FRAC_SHFT)|msm->xf[i]) < sl) {
msm->xi[i] = sl >> EW_X_FRAC_SHFT;
msm->xf[i] = sl & EW_X_FRAC_MASK;
}
if (((msh->xi[i]<<EW_X_FRAC_SHFT)|msh->xf[i]) > sr) {
msh->xi[i] = sr >> EW_X_FRAC_SHFT;
msh->xf[i] = sr & EW_X_FRAC_MASK;
}
}
}
}
/* if last span of upper minor edge, skip draw until lower next edge */
if (minor_state == EW_MINOR_MID) {
minor_state = EW_MINOR_LOW;
continue;
}
/* DRAW_SPAN */
if (hy_mask & my_mask & sp_mask) {/* draw if something valid */
/* get left and right x coordinates of span */
/* XXXX note that index works only for convex edge chains.
if concave, a sort on subpixel x coordinates is required */
if (ewh->left) {
xl = ewh->xy.xpi[hx_index];
xr = ewm->xy.xpi[mx_index];
} else {
xr = ewh->xy.xpi[hx_index];
xl = ewm->xy.xpi[mx_index];
}
/* scissor y */
if (ewh->xy.yp > (sc_ymax>>SU_XY_FRAC_SHFT)) return;
if (ewh->xy.yp < (sc_ymin>>SU_XY_FRAC_SHFT)) goto UPDATE_EDGES;
/* scissor x */
{
int sl, sr;
sl = sc_xmin >> SU_XY_FRAC_SHFT;
sr = sc_xmax >> SU_XY_FRAC_SHFT;
if (xr < sl) goto UPDATE_EDGES;
if (xl > sr) goto UPDATE_EDGES;
if (ewh->left && xr > sr) xr = sr;
if (!ewh->left && xl < sl) xl = sl;
}
/*
if multiplier, attributes += (min_x - hx) * dx_attributes;
else mem_span needs to disable pixels < min_x
*/
/* write to cz_step */
czs->pnt.r = ewh->pnt.r;
czs->pnt.g = ewh->pnt.g;
czs->pnt.b = ewh->pnt.b;
czs->pnt.a = ewh->pnt.a;
czs->pnt.z = ewh->pnt.z;
/* write to tc_step */
tcs->first = TRUE;
tcs->pnt.s = ewh->pnt.s;
tcs->pnt.t = ewh->pnt.t;
tcs->pnt.w = ewh->pnt.w;
tcs->pnt.l = ewh->pnt.l;
/* write to mem_span */
/* XXX should use a scanline address not y, and update depth.base */
ms->color.base = ewh->xy.yp;
ms->x_left = xl;
ms->x_right = xr;
do_mem_span();
UPDATE_EDGES: ;
} /* end DRAW_SPAN */
/* step edges edges in y */
ewh->xy.yp++;
/* step left edge x */
hx_carry = FALSE;
for (i=0; i<AA_SP_COUNT; i++) {
ewh->xy.xpi[i] += ewh->xy.xdi;
ewh->xy.xpf[i] += ewh->xy.xdf;
/* if fraction carries, bump x and remember the carry */
if (ewh->xy.xpf[i] & ~EW_X_FRAC_MASK) {
ewh->xy.xpf[i] &= EW_X_FRAC_MASK;
ewh->xy.xpi[i]++;
/* remember only the indexed x carry */
if (i == hx_index) hx_carry = TRUE;
}
}
/* step right edge x */
for (i=0; i<AA_SP_COUNT; i++) {
ewm->xy.xpi[i] += ewm->xy.xdi;
ewm->xy.xpf[i] += ewm->xy.xdf;
if (ewm->xy.xpf[i] & ~EW_X_FRAC_MASK) {
ewm->xy.xpf[i] &= EW_X_FRAC_MASK;
ewm->xy.xpi[i]++;
}
}
/* step left edge attributes */
if (!hx_carry) {
ewh->pnt.r += ewh->del.r;
ewh->pnt.g += ewh->del.g;
ewh->pnt.b += ewh->del.b;
ewh->pnt.a += ewh->del.a;
ewh->pnt.z += ewh->del.z;
ewh->pnt.s += ewh->del.s;
ewh->pnt.t += ewh->del.t;
ewh->pnt.w += ewh->del.w;
ewh->pnt.l += ewh->del.l;
/* step left attributes by dx */
} else {
ewh->pnt.r += ews->del.r;
ewh->pnt.g += ews->del.g;
ewh->pnt.b += ews->del.b;
ewh->pnt.a += ews->del.a;
ewh->pnt.z += ews->del.z;
ewh->pnt.s += ews->del.s;
ewh->pnt.t += ews->del.t;
ewh->pnt.w += ews->del.w;
ewh->pnt.l += ews->del.l;
}
/* initialize masks for next span */
hy_mask = hu_mask = AA_SP_MASK;
my_mask = mu_mask = AA_SP_MASK;
} /* end EDGE_LOOP */
}