]> git.wh0rd.org - ICEs.git/blame - 394889/s_triangle.i.9
more
[ICEs.git] / 394889 / s_triangle.i.9
CommitLineData
bd3239d2
MF
1typedef union {
2 float f;
3 int i;
4} fi_type;
5static __inline__ int IS_INF_OR_NAN(float x)
6{
7 fi_type tmp;
8 tmp.f = x;
9 return !(int)((unsigned int)((tmp.i & 0x7fffffff) - 0x7f800000) >> 31);
10}
11
12typedef enum {
13 FRAG_ATTRIB_WPOS = 0, FRAG_ATTRIB_COL0 = 1, FRAG_ATTRIB_COL1 =
14 2, FRAG_ATTRIB_FOGC = 3, FRAG_ATTRIB_TEX0 = 4, FRAG_ATTRIB_TEX1 =
15 5, FRAG_ATTRIB_TEX2 = 6, FRAG_ATTRIB_TEX3 = 7, FRAG_ATTRIB_TEX4 =
16 8, FRAG_ATTRIB_TEX5 = 9, FRAG_ATTRIB_TEX6 = 10, FRAG_ATTRIB_TEX7 =
17 11, FRAG_ATTRIB_FACE = 12, FRAG_ATTRIB_PNTC = 13, FRAG_ATTRIB_VAR0 =
18 14, FRAG_ATTRIB_MAX = (FRAG_ATTRIB_VAR0 + 16)
19} gl_frag_result;
20typedef enum {
21 TEXTURE_BUFFER_INDEX, TEXTURE_2D_ARRAY_INDEX, TEXTURE_1D_ARRAY_INDEX,
22 TEXTURE_CUBE_INDEX, TEXTURE_3D_INDEX, TEXTURE_RECT_INDEX,
23 TEXTURE_2D_INDEX, TEXTURE_1D_INDEX, NUM_TEXTURE_TARGETS
24} gl_texture_index;
25struct gl_texture_image {
26 unsigned int Width;
27 unsigned int Height;
28 unsigned int WidthLog2;
29 void *Data;
30};
31typedef enum {
32 MAX_FACES = 6
33} gl_face_index;
34struct gl_texture_object {
35 int BaseLevel;
36 struct gl_texture_image *Image[MAX_FACES][15];
37};
38struct gl_texture_unit {
39 struct gl_texture_object *CurrentTex[NUM_TEXTURE_TARGETS];
40};
41struct gl_texture_attrib {
42 struct gl_texture_unit Unit[(16 + 16)];
43};
44struct gl_renderbuffer {
45 void (*PutRowRGB) (struct gl_context * ctx, struct gl_renderbuffer * rb,
46 unsigned int count, int x, int y,
47 const void *values);
48};
49struct gl_framebuffer {
50 struct gl_renderbuffer *_ColorDrawBuffers[8];
51};
52struct gl_context {
53 struct gl_framebuffer *DrawBuffer;
54 struct gl_texture_attrib Texture;
55};
56typedef struct {
57 float attrib[FRAG_ATTRIB_MAX][4];
58} SWvertex;
59typedef struct sw_span {
60 int x, y;
61 unsigned int end;
62 float attrStepX[FRAG_ATTRIB_MAX][4];
63 int intTex[2], intTexStep[2];
64} SWspan;
65typedef void (*swrast_tri_func) (struct gl_context * ctx, const SWvertex *,
66 const SWvertex *, const SWvertex *);
67typedef struct {
68 swrast_tri_func Triangle;
69} SWcontext;
70static void simple_textured_triangle(struct gl_context *ctx,
71 const SWvertex * v0, const SWvertex * v1,
72 const SWvertex * v2)
73{
74 typedef struct {
75 float dx;
76 float dy;
77 int fsx;
78 int fx0;
79 int lines;
80 } EdgeT;
81 EdgeT eMaj;
82 EdgeT eTop;
83 EdgeT eBot;
84 float oneOverArea;
85 const SWvertex *vMin;
86 const SWvertex *vMid;
87 const SWvertex *vMax;
88 const int snapMask = ~(((1 << 11) / (1 << 4)) - 1);
89 int vMin_fx;
90 int vMin_fy;
91 int vMid_fx;
92 int vMid_fy;
93 int vMax_fx;
94 int vMax_fy;
95 SWspan span;
96 const int fy0 =
97 (((int)
98 ((((v0->attrib[FRAG_ATTRIB_WPOS][1] -
99 0.5F) * ((float)(1 << 11))) >=
100 0.0F) ? (((v0->attrib[FRAG_ATTRIB_WPOS][1] - 0.5F) *
101 ((float)(1 << 11))) +
102 0.5F) : (((v0->attrib[FRAG_ATTRIB_WPOS][1] - 0.5F) *
103 ((float)(1 << 11))) - 0.5F)))) & snapMask;
104 const int fy1 =
105 (((int)
106 ((((v1->attrib[FRAG_ATTRIB_WPOS][1] -
107 0.5F) * ((float)(1 << 11))) >=
108 0.0F) ? (((v1->attrib[FRAG_ATTRIB_WPOS][1] - 0.5F) *
109 ((float)(1 << 11))) +
110 0.5F) : (((v1->attrib[FRAG_ATTRIB_WPOS][1] - 0.5F) *
111 ((float)(1 << 11))) - 0.5F)))) & snapMask;
112 const int fy2 =
113 (((int)
114 ((((v2->attrib[FRAG_ATTRIB_WPOS][1] -
115 0.5F) * ((float)(1 << 11))) >=
116 0.0F) ? (((v2->attrib[FRAG_ATTRIB_WPOS][1] - 0.5F) *
117 ((float)(1 << 11))) +
118 0.5F) : (((v2->attrib[FRAG_ATTRIB_WPOS][1] - 0.5F) *
119 ((float)(1 << 11))) - 0.5F)))) & snapMask;
120 if (fy0 <= fy1) {
121 if (fy1 <= fy2) {
122 vMin = v0;
123 vMax = v1;
124 vMin_fy = fy0;
125 }
126 }
127 vMin_fx =
128 (((int)
129 ((((vMin->attrib[FRAG_ATTRIB_WPOS][0] +
130 0.5F) * ((float)(1 << 11))) >=
131 0.0F) ? (((vMin->attrib[FRAG_ATTRIB_WPOS][0] + 0.5F) *
132 ((float)(1 << 11))) +
133 0.5F) : (((vMin->attrib[FRAG_ATTRIB_WPOS][0] + 0.5F) *
134 ((float)(1 << 11))) - 0.5F)))) & snapMask;
135 vMid_fx =
136 (((int)
137 ((((vMid->attrib[FRAG_ATTRIB_WPOS][0] +
138 0.5F) * ((float)(1 << 11))) >=
139 0.0F) ? (((vMid->attrib[FRAG_ATTRIB_WPOS][0] + 0.5F) *
140 ((float)(1 << 11))) +
141 0.5F) : (((vMid->attrib[FRAG_ATTRIB_WPOS][0] + 0.5F) *
142 ((float)(1 << 11))) - 0.5F)))) & snapMask;
143 eMaj.dx = ((vMax_fx - vMin_fx) * (1.0F / ((float)(1 << 11))));
144 eMaj.dy = ((vMax_fy - vMin_fy) * (1.0F / ((float)(1 << 11))));
145 eTop.dx = ((vMax_fx - vMid_fx) * (1.0F / ((float)(1 << 11))));
146 eBot.dy = ((vMid_fy - vMin_fy) * (1.0F / ((float)(1 << 11))));
147 const float area = eMaj.dx * eBot.dy - eBot.dx * eMaj.dy;
148 if (IS_INF_OR_NAN(area) || area == 0.0F)
149 return;
150 oneOverArea = 1.0F / area;
151 if (eBot.lines > 0) {
152 eBot.fx0 = vMin_fx;
153 }
154 int scan_from_left_to_right;
155 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0];
156 const struct gl_texture_object *obj =
157 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX];
158 const struct gl_texture_image *texImg = obj->Image[0][obj->BaseLevel];
159 const float twidth = (float)texImg->Width;
160 const int twidth_log2 = texImg->WidthLog2;
161 const unsigned char *texture = (const unsigned char *)texImg->Data;
162 const int smask = texImg->Width - 1;
163 const int tmask = texImg->Height - 1;
164 if (!rb || !texture) {
165 return;
166 }
167 float eMaj_ds =
168 (vMax->attrib[FRAG_ATTRIB_TEX0][0] -
169 vMin->attrib[FRAG_ATTRIB_TEX0][0]) * twidth;
170 float eBot_ds =
171 (vMid->attrib[FRAG_ATTRIB_TEX0][0] -
172 vMin->attrib[FRAG_ATTRIB_TEX0][0]) * twidth;
173 span.attrStepX[FRAG_ATTRIB_TEX0][0] =
174 oneOverArea * (eMaj_ds * eBot.dy - eMaj.dy * eBot_ds);
175 span.intTexStep[0] =
176 (((int)
177 ((((span.attrStepX[FRAG_ATTRIB_TEX0][0]) * ((float)(1 << 11))) >=
178 0.0F) ? (((span.attrStepX[FRAG_ATTRIB_TEX0][0]) *
179 ((float)(1 << 11))) +
180 0.5F) : (((span.attrStepX[FRAG_ATTRIB_TEX0][0]) *
181 ((float)(1 << 11))) - 0.5F))));
182 int subTriangle;
183 int fxLeftEdge = 0;
184 int fxRightEdge = 0;
185 int sLeft = 0;
186 for (subTriangle = 0; subTriangle <= 1; subTriangle++) {
187 EdgeT *eRight;
188 int lines;
189 if (subTriangle == 0) {
190 if (scan_from_left_to_right) {
191 eRight = &eBot;
192 lines = eRight->lines;
193 } else {
194 eRight = &eMaj;
195 }
196 } else {
197 if (scan_from_left_to_right) {
198 eRight = &eTop;
199 }
200 fxRightEdge = eRight->fsx - 1;
201 }
202 while (lines > 0) {
203 const int right = fxRightEdge >> 11;
204 span.x = fxLeftEdge >> 11;
205 if (right <= span.x)
206 span.end = 0;
207 else
208 span.end = right - span.x;
209 span.intTex[0] = sLeft;
210 if (span.end > 0 && span.y >= 0) {
211 unsigned int i;
212 unsigned char rgb[16384][3];
213 for (i = 0; i < span.end; i++) {
214 int s =
215 ((span.intTex[0]) >> 11) & smask;
216 int t =
217 ((span.intTex[1]) >> 11) & tmask;
218 int pos = (t << twidth_log2) + s;
219 rgb[i][0] = texture[pos + 2];
220 span.intTex[0] += span.intTexStep[0];
221 }
222 rb->PutRowRGB(ctx, rb, span.end, span.x, span.y,
223 rgb);
224 }
225 }
226 }
227}
228
229void _swrast_choose_triangle(struct gl_context *ctx)
230{
231 SWcontext *swrast = SWRAST_CONTEXT(ctx);
232 swrast->Triangle = simple_textured_triangle;
233}