add tdfx DRI driver
[mesa.git] / src / mesa / drivers / dri / tdfx / tdfx_vb.c
1 /*
2 * GLX Hardware Device Driver for Intel i810
3 * Copyright (C) 1999 Keith Whitwell
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * KEITH WHITWELL, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
21 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 *
24 */
25 /* $XFree86: xc/lib/GL/mesa/src/drv/tdfx/tdfx_vb.c,v 1.3 2002/10/30 12:52:01 alanh Exp $ */
26
27 #include "glheader.h"
28 #include "mtypes.h"
29 #include "imports.h"
30 #include "macros.h"
31 #include "colormac.h"
32
33 #include "math/m_translate.h"
34 #include "swrast_setup/swrast_setup.h"
35
36 #include "tdfx_context.h"
37 #include "tdfx_vb.h"
38 #include "tdfx_tris.h"
39 #include "tdfx_state.h"
40 #include "tdfx_render.h"
41
42 static void copy_pv_rgba4( GLcontext *ctx, GLuint edst, GLuint esrc )
43 {
44 tdfxContextPtr fxMesa = TDFX_CONTEXT( ctx );
45 GLubyte *tdfxverts = (GLubyte *)fxMesa->verts;
46 GLuint shift = fxMesa->vertex_stride_shift;
47 tdfxVertex *dst = (tdfxVertex *)(tdfxverts + (edst << shift));
48 tdfxVertex *src = (tdfxVertex *)(tdfxverts + (esrc << shift));
49 dst->ui[4] = src->ui[4];
50 }
51
52 static void copy_pv_rgba3( GLcontext *ctx, GLuint edst, GLuint esrc )
53 {
54 tdfxContextPtr fxMesa = TDFX_CONTEXT( ctx );
55 GLubyte *tdfxverts = (GLubyte *)fxMesa->verts;
56 GLuint shift = fxMesa->vertex_stride_shift;
57 tdfxVertex *dst = (tdfxVertex *)(tdfxverts + (edst << shift));
58 tdfxVertex *src = (tdfxVertex *)(tdfxverts + (esrc << shift));
59 dst->ui[3] = src->ui[3];
60 }
61
62 typedef void (*emit_func)( GLcontext *, GLuint, GLuint, void *, GLuint );
63
64 static struct {
65 emit_func emit;
66 interp_func interp;
67 copy_pv_func copy_pv;
68 GLboolean (*check_tex_sizes)( GLcontext *ctx );
69 GLuint vertex_size;
70 GLuint vertex_stride_shift;
71 GLuint vertex_format;
72 } setup_tab[TDFX_MAX_SETUP];
73
74
75 static void import_float_colors( GLcontext *ctx )
76 {
77 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
78 struct gl_client_array *from = VB->ColorPtr[0];
79 struct gl_client_array *to = &TDFX_CONTEXT(ctx)->UbyteColor;
80 GLuint count = VB->Count;
81
82 if (!to->Ptr) {
83 to->Ptr = ALIGN_MALLOC( VB->Size * 4 * sizeof(GLubyte), 32 );
84 to->Type = GL_UNSIGNED_BYTE;
85 }
86
87 /* No need to transform the same value 3000 times.
88 */
89 if (!from->StrideB) {
90 to->StrideB = 0;
91 count = 1;
92 }
93 else
94 to->StrideB = 4 * sizeof(GLubyte);
95
96 _math_trans_4ub( (GLubyte (*)[4]) to->Ptr,
97 from->Ptr,
98 from->StrideB,
99 from->Type,
100 from->Size,
101 0,
102 count);
103
104 VB->ColorPtr[0] = to;
105 }
106
107
108 #define GET_COLOR(ptr, idx) (((GLchan (*)[4])((ptr)->Ptr))[idx])
109
110
111 static void interp_extras( GLcontext *ctx,
112 GLfloat t,
113 GLuint dst, GLuint out, GLuint in,
114 GLboolean force_boundary )
115 {
116 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
117
118 /*fprintf(stderr, "%s\n", __FUNCTION__);*/
119
120 if (VB->ColorPtr[1]) {
121 INTERP_4CHAN( t,
122 GET_COLOR(VB->ColorPtr[1], dst),
123 GET_COLOR(VB->ColorPtr[1], out),
124 GET_COLOR(VB->ColorPtr[1], in) );
125 }
126
127 if (VB->EdgeFlag) {
128 VB->EdgeFlag[dst] = VB->EdgeFlag[out] || force_boundary;
129 }
130
131 setup_tab[TDFX_CONTEXT(ctx)->SetupIndex].interp(ctx, t, dst, out, in,
132 force_boundary);
133 }
134
135 static void copy_pv_extras( GLcontext *ctx, GLuint dst, GLuint src )
136 {
137 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
138
139 if (VB->ColorPtr[1]) {
140 COPY_CHAN4( GET_COLOR(VB->ColorPtr[1], dst),
141 GET_COLOR(VB->ColorPtr[1], src) );
142 }
143
144 setup_tab[TDFX_CONTEXT(ctx)->SetupIndex].copy_pv(ctx, dst, src);
145 }
146
147
148
149 #define IND (TDFX_XYZ_BIT|TDFX_RGBA_BIT)
150 #define TAG(x) x##_wg
151 #include "tdfx_vbtmp.h"
152
153 /* Special for tdfx: fog requires w
154 */
155 #define IND (TDFX_XYZ_BIT|TDFX_RGBA_BIT|TDFX_W_BIT)
156 #define TAG(x) x##_wg_fog
157 #include "tdfx_vbtmp.h"
158
159 #define IND (TDFX_XYZ_BIT|TDFX_RGBA_BIT|TDFX_W_BIT|TDFX_TEX0_BIT)
160 #define TAG(x) x##_wgt0
161 #include "tdfx_vbtmp.h"
162
163 #define IND (TDFX_XYZ_BIT|TDFX_RGBA_BIT|TDFX_W_BIT|TDFX_TEX0_BIT|TDFX_TEX1_BIT)
164 #define TAG(x) x##_wgt0t1
165 #include "tdfx_vbtmp.h"
166
167 #define IND (TDFX_XYZ_BIT|TDFX_RGBA_BIT|TDFX_W_BIT|TDFX_TEX0_BIT|TDFX_PTEX_BIT)
168 #define TAG(x) x##_wgpt0
169 #include "tdfx_vbtmp.h"
170
171 #define IND (TDFX_XYZ_BIT|TDFX_RGBA_BIT|TDFX_W_BIT|TDFX_TEX0_BIT|TDFX_TEX1_BIT|\
172 TDFX_PTEX_BIT)
173 #define TAG(x) x##_wgpt0t1
174 #include "tdfx_vbtmp.h"
175
176 #define IND (TDFX_RGBA_BIT)
177 #define TAG(x) x##_g
178 #include "tdfx_vbtmp.h"
179
180 #define IND (TDFX_TEX0_BIT)
181 #define TAG(x) x##_t0
182 #include "tdfx_vbtmp.h"
183
184 #define IND (TDFX_TEX0_BIT|TDFX_TEX1_BIT)
185 #define TAG(x) x##_t0t1
186 #include "tdfx_vbtmp.h"
187
188 #define IND (TDFX_RGBA_BIT|TDFX_TEX0_BIT)
189 #define TAG(x) x##_gt0
190 #include "tdfx_vbtmp.h"
191
192 #define IND (TDFX_RGBA_BIT|TDFX_TEX0_BIT|TDFX_TEX1_BIT)
193 #define TAG(x) x##_gt0t1
194 #include "tdfx_vbtmp.h"
195
196
197
198 static void init_setup_tab( void )
199 {
200 init_wg();
201 init_wg_fog();
202 init_wgt0();
203 init_wgt0t1();
204 init_wgpt0();
205 init_wgpt0t1();
206
207 init_g();
208 init_t0();
209 init_t0t1();
210 init_gt0();
211 init_gt0t1();
212 }
213
214
215 void tdfxPrintSetupFlags(char *msg, GLuint flags )
216 {
217 fprintf(stderr, "%s(%x): %s%s%s%s%s\n",
218 msg,
219 (int)flags,
220 (flags & TDFX_XYZ_BIT) ? " xyz," : "",
221 (flags & TDFX_W_BIT) ? " w," : "",
222 (flags & TDFX_RGBA_BIT) ? " rgba," : "",
223 (flags & TDFX_TEX0_BIT) ? " tex-0," : "",
224 (flags & TDFX_TEX1_BIT) ? " tex-1," : "");
225 }
226
227
228
229 void tdfxCheckTexSizes( GLcontext *ctx )
230 {
231 TNLcontext *tnl = TNL_CONTEXT(ctx);
232 tdfxContextPtr fxMesa = TDFX_CONTEXT( ctx );
233
234 if (!setup_tab[fxMesa->SetupIndex].check_tex_sizes(ctx)) {
235 GLuint ind = fxMesa->SetupIndex |= (TDFX_PTEX_BIT|TDFX_RGBA_BIT);
236
237 /* Tdfx handles projective textures nicely; just have to change
238 * up to the new vertex format.
239 */
240 if (setup_tab[ind].vertex_format != fxMesa->vertexFormat) {
241 FLUSH_BATCH(fxMesa);
242 fxMesa->dirty |= TDFX_UPLOAD_VERTEX_LAYOUT;
243 fxMesa->vertexFormat = setup_tab[ind].vertex_format;
244 fxMesa->vertex_stride_shift = setup_tab[ind].vertex_stride_shift;
245
246 /* This is required as we have just changed the vertex
247 * format, so the interp and copy routines must also change.
248 * In the unfilled and twosided cases we are using the
249 * swrast_setup ones anyway, so leave them in place.
250 */
251 if (!(ctx->_TriangleCaps & (DD_TRI_LIGHT_TWOSIDE|DD_TRI_UNFILLED))) {
252 tnl->Driver.Render.Interp = setup_tab[fxMesa->SetupIndex].interp;
253 tnl->Driver.Render.CopyPV = setup_tab[fxMesa->SetupIndex].copy_pv;
254 }
255 }
256 }
257 }
258
259
260 void tdfxBuildVertices( GLcontext *ctx, GLuint start, GLuint count,
261 GLuint newinputs )
262 {
263 tdfxContextPtr fxMesa = TDFX_CONTEXT( ctx );
264 GLubyte *v = (fxMesa->verts + (start<<fxMesa->vertex_stride_shift));
265 GLuint stride = 1<<fxMesa->vertex_stride_shift;
266
267 newinputs |= fxMesa->SetupNewInputs;
268 fxMesa->SetupNewInputs = 0;
269
270 if (!newinputs)
271 return;
272
273 if (newinputs & VERT_BIT_CLIP) {
274 setup_tab[fxMesa->SetupIndex].emit( ctx, start, count, v, stride );
275 } else {
276 GLuint ind = 0;
277
278 if (newinputs & VERT_BIT_COLOR0)
279 ind |= TDFX_RGBA_BIT;
280
281 if (newinputs & VERT_BIT_TEX0)
282 ind |= TDFX_TEX0_BIT;
283
284 if (newinputs & VERT_BIT_TEX1)
285 ind |= TDFX_TEX0_BIT|TDFX_TEX1_BIT;
286
287 if (fxMesa->SetupIndex & TDFX_PTEX_BIT)
288 ind = ~0;
289
290 ind &= fxMesa->SetupIndex;
291
292 if (ind) {
293 setup_tab[ind].emit( ctx, start, count, v, stride );
294 }
295 }
296 }
297
298
299 void tdfxChooseVertexState( GLcontext *ctx )
300 {
301 TNLcontext *tnl = TNL_CONTEXT(ctx);
302 tdfxContextPtr fxMesa = TDFX_CONTEXT( ctx );
303 GLuint ind = TDFX_XYZ_BIT|TDFX_RGBA_BIT;
304
305 if (ctx->Texture._EnabledUnits & 0x2)
306 /* unit 1 enabled */
307 ind |= TDFX_W_BIT|TDFX_TEX1_BIT|TDFX_TEX0_BIT;
308 else if (ctx->Texture._EnabledUnits & 0x1)
309 /* unit 0 enabled */
310 ind |= TDFX_W_BIT|TDFX_TEX0_BIT;
311 else if (ctx->Fog.Enabled)
312 ind |= TDFX_W_BIT;
313
314 fxMesa->SetupIndex = ind;
315
316 if (ctx->_TriangleCaps & (DD_TRI_LIGHT_TWOSIDE|DD_TRI_UNFILLED)) {
317 tnl->Driver.Render.Interp = interp_extras;
318 tnl->Driver.Render.CopyPV = copy_pv_extras;
319 } else {
320 tnl->Driver.Render.Interp = setup_tab[ind].interp;
321 tnl->Driver.Render.CopyPV = setup_tab[ind].copy_pv;
322 }
323
324 if (setup_tab[ind].vertex_format != fxMesa->vertexFormat) {
325 FLUSH_BATCH(fxMesa);
326 fxMesa->dirty |= TDFX_UPLOAD_VERTEX_LAYOUT;
327 fxMesa->vertexFormat = setup_tab[ind].vertex_format;
328 fxMesa->vertex_stride_shift = setup_tab[ind].vertex_stride_shift;
329 }
330 }
331
332
333
334 void tdfxInitVB( GLcontext *ctx )
335 {
336 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx);
337 GLuint size = TNL_CONTEXT(ctx)->vb.Size;
338 static int firsttime = 1;
339 if (firsttime) {
340 init_setup_tab();
341 firsttime = 0;
342 }
343
344 fxMesa->verts = (GLubyte *)ALIGN_MALLOC(size * sizeof(tdfxVertex), 32);
345 fxMesa->vertexFormat = setup_tab[TDFX_XYZ_BIT|TDFX_RGBA_BIT].vertex_format;
346 fxMesa->vertex_stride_shift = setup_tab[(TDFX_XYZ_BIT|
347 TDFX_RGBA_BIT)].vertex_stride_shift;
348 fxMesa->SetupIndex = TDFX_XYZ_BIT|TDFX_RGBA_BIT;
349 }
350
351
352 void tdfxFreeVB( GLcontext *ctx )
353 {
354 tdfxContextPtr fxMesa = TDFX_CONTEXT(ctx);
355 if (fxMesa->verts) {
356 ALIGN_FREE(fxMesa->verts);
357 fxMesa->verts = 0;
358 }
359
360 if (fxMesa->UbyteColor.Ptr) {
361 ALIGN_FREE(fxMesa->UbyteColor.Ptr);
362 fxMesa->UbyteColor.Ptr = 0;
363 }
364
365 }