63de3563c4565b7acb36a9abf0392978cdd51bd7
[mesa.git] / src / mesa / drivers / dri / mga / mgavb.c
1 /*
2 * Copyright 2000-2001 VA Linux Systems, Inc.
3 * All Rights Reserved.
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Keith Whitwell <keith@tungstengraphics.com>
26 */
27 /* $XFree86: xc/lib/GL/mesa/src/drv/mga/mgavb.c,v 1.15 2003/03/26 20:43:49 tsi Exp $ */
28
29 #include "mgacontext.h"
30 #include "mgavb.h"
31 #include "mgatris.h"
32 #include "mgaioctl.h"
33 #include "mga_xmesa.h"
34
35 #include "glheader.h"
36 #include "mtypes.h"
37 #include "imports.h"
38 #include "macros.h"
39 #include "colormac.h"
40
41 #include "tnl/t_context.h"
42 #include "swrast_setup/swrast_setup.h"
43 #include "swrast/swrast.h"
44
45
46 #define MGA_TEX1_BIT 0x1
47 #define MGA_TEX0_BIT 0x2
48 #define MGA_RGBA_BIT 0x4
49 #define MGA_SPEC_BIT 0x8
50 #define MGA_FOG_BIT 0x10
51 #define MGA_XYZW_BIT 0x20
52 #define MGA_PTEX_BIT 0x40
53 #define MGA_MAX_SETUP 0x80
54
55 static struct {
56 void (*emit)( GLcontext *, GLuint, GLuint, void *, GLuint );
57 interp_func interp;
58 copy_pv_func copy_pv;
59 GLboolean (*check_tex_sizes)( GLcontext *ctx );
60 GLuint vertex_size;
61 GLuint vertex_stride_shift;
62 GLuint vertex_format;
63 } setup_tab[MGA_MAX_SETUP];
64
65
66 #define TINY_VERTEX_FORMAT 0
67 #define NOTEX_VERTEX_FORMAT 0
68 #define TEX0_VERTEX_FORMAT (MGA_A|MGA_S|MGA_F)
69 #define TEX1_VERTEX_FORMAT (MGA_A|MGA_S|MGA_F|MGA_T2)
70 #define PROJ_TEX1_VERTEX_FORMAT 0
71 #define TEX2_VERTEX_FORMAT 0
72 #define TEX3_VERTEX_FORMAT 0
73 #define PROJ_TEX3_VERTEX_FORMAT 0
74
75 #define DO_XYZW (IND & MGA_XYZW_BIT)
76 #define DO_RGBA (IND & MGA_RGBA_BIT)
77 #define DO_SPEC (IND & MGA_SPEC_BIT)
78 #define DO_FOG (IND & MGA_FOG_BIT)
79 #define DO_TEX0 (IND & MGA_TEX0_BIT)
80 #define DO_TEX1 (IND & MGA_TEX1_BIT)
81 #define DO_TEX2 0
82 #define DO_TEX3 0
83 #define DO_PTEX (IND & MGA_PTEX_BIT)
84
85
86 #define VERTEX mgaVertex
87 #define VERTEX_COLOR mga_color_t
88 #define LOCALVARS mgaContextPtr mmesa = MGA_CONTEXT(ctx);
89 #define GET_VIEWPORT_MAT() mmesa->hw_viewport
90 #define GET_TEXSOURCE(n) mmesa->tmu_source[n]
91 #define GET_VERTEX_FORMAT() mmesa->vertex_format
92 #define GET_VERTEX_STORE() mmesa->verts
93 #define GET_VERTEX_STRIDE_SHIFT() mmesa->vertex_stride_shift
94 #define GET_UBYTE_COLOR_STORE() &mmesa->UbyteColor
95 #define GET_UBYTE_SPEC_COLOR_STORE() &mmesa->UbyteSecondaryColor
96
97 #define HAVE_HW_VIEWPORT 0
98 #define HAVE_HW_DIVIDE 0
99 #define HAVE_RGBA_COLOR 0
100 #define HAVE_TINY_VERTICES 0
101 #define HAVE_NOTEX_VERTICES 0
102 #define HAVE_TEX0_VERTICES 1
103 #define HAVE_TEX1_VERTICES 1
104 #define HAVE_TEX2_VERTICES 0
105 #define HAVE_TEX3_VERTICES 0
106 #define HAVE_PTEX_VERTICES 0
107
108 #define UNVIEWPORT_VARS \
109 const GLfloat dx = - mmesa->drawX - SUBPIXEL_X; \
110 const GLfloat dy = (mmesa->driDrawable->h + \
111 mmesa->drawY + SUBPIXEL_Y); \
112 const GLfloat sz = 1.0 / mmesa->depth_scale
113
114 #define UNVIEWPORT_X(x) x + dx;
115 #define UNVIEWPORT_Y(y) - y + dy;
116 #define UNVIEWPORT_Z(z) z * sz;
117
118 #define PTEX_FALLBACK() FALLBACK(ctx, MGA_FALLBACK_TEXTURE, 1)
119
120
121 #define IMPORT_FLOAT_COLORS mga_import_float_colors
122 #define IMPORT_FLOAT_SPEC_COLORS mga_import_float_spec_colors
123
124 #define INTERP_VERTEX setup_tab[MGA_CONTEXT(ctx)->SetupIndex].interp
125 #define COPY_PV_VERTEX setup_tab[MGA_CONTEXT(ctx)->SetupIndex].copy_pv
126
127
128 /***********************************************************************
129 * Generate pv-copying and translation functions *
130 ***********************************************************************/
131
132 #define TAG(x) mga_##x
133 #include "tnl_dd/t_dd_vb.c"
134
135 /***********************************************************************
136 * Generate vertex emit and interp functions *
137 ***********************************************************************/
138
139
140 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT)
141 #define TAG(x) x##_wg
142 #include "tnl_dd/t_dd_vbtmp.h"
143
144 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_SPEC_BIT)
145 #define TAG(x) x##_wgs
146 #include "tnl_dd/t_dd_vbtmp.h"
147
148 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_TEX0_BIT)
149 #define TAG(x) x##_wgt0
150 #include "tnl_dd/t_dd_vbtmp.h"
151
152 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
153 #define TAG(x) x##_wgt0t1
154 #include "tnl_dd/t_dd_vbtmp.h"
155
156 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_TEX0_BIT|MGA_PTEX_BIT)
157 #define TAG(x) x##_wgpt0
158 #include "tnl_dd/t_dd_vbtmp.h"
159
160 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT)
161 #define TAG(x) x##_wgst0
162 #include "tnl_dd/t_dd_vbtmp.h"
163
164 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
165 #define TAG(x) x##_wgst0t1
166 #include "tnl_dd/t_dd_vbtmp.h"
167
168 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT|MGA_PTEX_BIT)
169 #define TAG(x) x##_wgspt0
170 #include "tnl_dd/t_dd_vbtmp.h"
171
172 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_FOG_BIT)
173 #define TAG(x) x##_wgf
174 #include "tnl_dd/t_dd_vbtmp.h"
175
176 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_FOG_BIT|MGA_SPEC_BIT)
177 #define TAG(x) x##_wgfs
178 #include "tnl_dd/t_dd_vbtmp.h"
179
180 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_FOG_BIT|MGA_TEX0_BIT)
181 #define TAG(x) x##_wgft0
182 #include "tnl_dd/t_dd_vbtmp.h"
183
184 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_FOG_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
185 #define TAG(x) x##_wgft0t1
186 #include "tnl_dd/t_dd_vbtmp.h"
187
188 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_FOG_BIT|MGA_TEX0_BIT|MGA_PTEX_BIT)
189 #define TAG(x) x##_wgfpt0
190 #include "tnl_dd/t_dd_vbtmp.h"
191
192 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_FOG_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT)
193 #define TAG(x) x##_wgfst0
194 #include "tnl_dd/t_dd_vbtmp.h"
195
196 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_FOG_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
197 #define TAG(x) x##_wgfst0t1
198 #include "tnl_dd/t_dd_vbtmp.h"
199
200 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_FOG_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT|MGA_PTEX_BIT)
201 #define TAG(x) x##_wgfspt0
202 #include "tnl_dd/t_dd_vbtmp.h"
203
204 #define IND (MGA_TEX0_BIT)
205 #define TAG(x) x##_t0
206 #include "tnl_dd/t_dd_vbtmp.h"
207
208 #define IND (MGA_TEX0_BIT|MGA_TEX1_BIT)
209 #define TAG(x) x##_t0t1
210 #include "tnl_dd/t_dd_vbtmp.h"
211
212 #define IND (MGA_FOG_BIT)
213 #define TAG(x) x##_f
214 #include "tnl_dd/t_dd_vbtmp.h"
215
216 #define IND (MGA_FOG_BIT|MGA_TEX0_BIT)
217 #define TAG(x) x##_ft0
218 #include "tnl_dd/t_dd_vbtmp.h"
219
220 #define IND (MGA_FOG_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
221 #define TAG(x) x##_ft0t1
222 #include "tnl_dd/t_dd_vbtmp.h"
223
224 #define IND (MGA_RGBA_BIT)
225 #define TAG(x) x##_g
226 #include "tnl_dd/t_dd_vbtmp.h"
227
228 #define IND (MGA_RGBA_BIT|MGA_SPEC_BIT)
229 #define TAG(x) x##_gs
230 #include "tnl_dd/t_dd_vbtmp.h"
231
232 #define IND (MGA_RGBA_BIT|MGA_TEX0_BIT)
233 #define TAG(x) x##_gt0
234 #include "tnl_dd/t_dd_vbtmp.h"
235
236 #define IND (MGA_RGBA_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
237 #define TAG(x) x##_gt0t1
238 #include "tnl_dd/t_dd_vbtmp.h"
239
240 #define IND (MGA_RGBA_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT)
241 #define TAG(x) x##_gst0
242 #include "tnl_dd/t_dd_vbtmp.h"
243
244 #define IND (MGA_RGBA_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
245 #define TAG(x) x##_gst0t1
246 #include "tnl_dd/t_dd_vbtmp.h"
247
248 #define IND (MGA_RGBA_BIT|MGA_FOG_BIT)
249 #define TAG(x) x##_gf
250 #include "tnl_dd/t_dd_vbtmp.h"
251
252 #define IND (MGA_RGBA_BIT|MGA_FOG_BIT|MGA_SPEC_BIT)
253 #define TAG(x) x##_gfs
254 #include "tnl_dd/t_dd_vbtmp.h"
255
256 #define IND (MGA_RGBA_BIT|MGA_FOG_BIT|MGA_TEX0_BIT)
257 #define TAG(x) x##_gft0
258 #include "tnl_dd/t_dd_vbtmp.h"
259
260 #define IND (MGA_RGBA_BIT|MGA_FOG_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
261 #define TAG(x) x##_gft0t1
262 #include "tnl_dd/t_dd_vbtmp.h"
263
264 #define IND (MGA_RGBA_BIT|MGA_FOG_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT)
265 #define TAG(x) x##_gfst0
266 #include "tnl_dd/t_dd_vbtmp.h"
267
268 #define IND (MGA_RGBA_BIT|MGA_FOG_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
269 #define TAG(x) x##_gfst0t1
270 #include "tnl_dd/t_dd_vbtmp.h"
271
272
273 static void init_setup_tab( void )
274 {
275 init_wg();
276 init_wgs();
277 init_wgt0();
278 init_wgt0t1();
279 init_wgpt0();
280 init_wgst0();
281 init_wgst0t1();
282 init_wgspt0();
283 init_wgf();
284 init_wgfs();
285 init_wgft0();
286 init_wgft0t1();
287 init_wgfpt0();
288 init_wgfst0();
289 init_wgfst0t1();
290 init_wgfspt0();
291 init_t0();
292 init_t0t1();
293 init_f();
294 init_ft0();
295 init_ft0t1();
296 init_g();
297 init_gs();
298 init_gt0();
299 init_gt0t1();
300 init_gst0();
301 init_gst0t1();
302 init_gf();
303 init_gfs();
304 init_gft0();
305 init_gft0t1();
306 init_gfst0();
307 init_gfst0t1();
308 }
309
310
311
312
313 void mgaPrintSetupFlags(char *msg, GLuint flags )
314 {
315 fprintf(stderr, "%s: %d %s%s%s%s%s%s\n",
316 msg,
317 (int)flags,
318 (flags & MGA_XYZW_BIT) ? " xyzw," : "",
319 (flags & MGA_RGBA_BIT) ? " rgba," : "",
320 (flags & MGA_SPEC_BIT) ? " spec," : "",
321 (flags & MGA_FOG_BIT) ? " fog," : "",
322 (flags & MGA_TEX0_BIT) ? " tex-0," : "",
323 (flags & MGA_TEX1_BIT) ? " tex-1," : "");
324 }
325
326
327 void mgaCheckTexSizes( GLcontext *ctx )
328 {
329 mgaContextPtr mmesa = MGA_CONTEXT( ctx );
330 TNLcontext *tnl = TNL_CONTEXT(ctx);
331
332 /*fprintf(stderr, "%s\n", __FUNCTION__);*/
333
334 if (!setup_tab[mmesa->SetupIndex].check_tex_sizes(ctx)) {
335 mmesa->SetupIndex |= MGA_PTEX_BIT;
336 mmesa->SetupNewInputs = ~0;
337
338 if (!mmesa->Fallback &&
339 !(ctx->_TriangleCaps & (DD_TRI_LIGHT_TWOSIDE|DD_TRI_UNFILLED))) {
340 tnl->Driver.Render.Interp = setup_tab[mmesa->SetupIndex].interp;
341 tnl->Driver.Render.CopyPV = setup_tab[mmesa->SetupIndex].copy_pv;
342 }
343 if (mmesa->Fallback) {
344 tnl->Driver.Render.Start(ctx);
345 }
346 }
347 }
348
349
350 void mgaBuildVertices( GLcontext *ctx,
351 GLuint start,
352 GLuint count,
353 GLuint newinputs )
354 {
355 mgaContextPtr mmesa = MGA_CONTEXT( ctx );
356 GLubyte *v = ((GLubyte *)mmesa->verts + (start<<mmesa->vertex_stride_shift));
357 GLuint stride = 1<<mmesa->vertex_stride_shift;
358
359 newinputs |= mmesa->SetupNewInputs;
360 mmesa->SetupNewInputs = 0;
361
362 if (!newinputs)
363 return;
364
365 if (newinputs & VERT_BIT_POS) {
366 setup_tab[mmesa->SetupIndex].emit( ctx, start, count, v, stride );
367 } else {
368 GLuint ind = 0;
369
370 if (newinputs & VERT_BIT_COLOR0)
371 ind |= MGA_RGBA_BIT;
372
373 if (newinputs & VERT_BIT_COLOR1)
374 ind |= MGA_SPEC_BIT;
375
376 if (newinputs & VERT_BIT_TEX0)
377 ind |= MGA_TEX0_BIT;
378
379 if (newinputs & VERT_BIT_TEX1)
380 ind |= MGA_TEX0_BIT|MGA_TEX1_BIT;
381
382 if (newinputs & VERT_BIT_FOG)
383 ind |= MGA_FOG_BIT;
384
385 if (mmesa->SetupIndex & MGA_PTEX_BIT)
386 ind = ~0;
387
388 ind &= mmesa->SetupIndex;
389
390 if (ind) {
391 setup_tab[ind].emit( ctx, start, count, v, stride );
392 }
393 }
394 }
395
396
397 void mgaChooseVertexState( GLcontext *ctx )
398 {
399 mgaContextPtr mmesa = MGA_CONTEXT( ctx );
400 TNLcontext *tnl = TNL_CONTEXT(ctx);
401 GLuint ind = MGA_XYZW_BIT|MGA_RGBA_BIT;
402
403 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR)
404 ind |= MGA_SPEC_BIT;
405
406 if (ctx->Fog.Enabled)
407 ind |= MGA_FOG_BIT;
408
409 if (ctx->Texture._EnabledUnits & 0x2) {
410 /* unit 1 enabled */
411 if (ctx->Texture._EnabledUnits & 0x1) {
412 /* unit 0 enabled */
413 ind |= MGA_TEX1_BIT|MGA_TEX0_BIT;
414 }
415 else {
416 ind |= MGA_TEX0_BIT;
417 }
418 }
419 else if (ctx->Texture._EnabledUnits & 0x1) {
420 /* unit 0 enabled */
421 ind |= MGA_TEX0_BIT;
422 }
423
424 mmesa->SetupIndex = ind;
425
426 if (ctx->_TriangleCaps & (DD_TRI_LIGHT_TWOSIDE|DD_TRI_UNFILLED)) {
427 tnl->Driver.Render.Interp = mga_interp_extras;
428 tnl->Driver.Render.CopyPV = mga_copy_pv_extras;
429 } else {
430 tnl->Driver.Render.Interp = setup_tab[ind].interp;
431 tnl->Driver.Render.CopyPV = setup_tab[ind].copy_pv;
432 }
433
434 if (setup_tab[ind].vertex_format != mmesa->vertex_format) {
435 FLUSH_BATCH(mmesa);
436 mmesa->dirty |= MGA_UPLOAD_PIPE;
437 mmesa->vertex_format = setup_tab[ind].vertex_format;
438 mmesa->vertex_size = setup_tab[ind].vertex_size;
439 mmesa->vertex_stride_shift = setup_tab[ind].vertex_stride_shift;
440 }
441 }
442
443
444
445 void mga_emit_contiguous_verts( GLcontext *ctx,
446 GLuint start,
447 GLuint count )
448 {
449 mgaContextPtr mmesa = MGA_CONTEXT(ctx);
450 GLuint vertex_size = mmesa->vertex_size * 4;
451 GLuint *dest = mgaAllocDmaLow( mmesa, (count-start) * vertex_size);
452 setup_tab[mmesa->SetupIndex].emit( ctx, start, count, dest, vertex_size );
453 }
454
455
456
457 void mgaInitVB( GLcontext *ctx )
458 {
459 mgaContextPtr mmesa = MGA_CONTEXT(ctx);
460 GLuint size = TNL_CONTEXT(ctx)->vb.Size;
461
462 mmesa->verts = (GLubyte *)ALIGN_MALLOC(size * sizeof(mgaVertex), 32);
463
464 {
465 static int firsttime = 1;
466 if (firsttime) {
467 init_setup_tab();
468 firsttime = 0;
469 }
470 }
471
472 mmesa->dirty |= MGA_UPLOAD_PIPE;
473 mmesa->vertex_format = setup_tab[0].vertex_format;
474 mmesa->vertex_size = setup_tab[0].vertex_size;
475 mmesa->vertex_stride_shift = setup_tab[0].vertex_stride_shift;
476 }
477
478
479 void mgaFreeVB( GLcontext *ctx )
480 {
481 mgaContextPtr mmesa = MGA_CONTEXT(ctx);
482 if (mmesa->verts) {
483 ALIGN_FREE(mmesa->verts);
484 mmesa->verts = 0;
485 }
486
487 if (mmesa->UbyteSecondaryColor.Ptr) {
488 ALIGN_FREE(mmesa->UbyteSecondaryColor.Ptr);
489 mmesa->UbyteSecondaryColor.Ptr = 0;
490 }
491
492 if (mmesa->UbyteColor.Ptr) {
493 ALIGN_FREE(mmesa->UbyteColor.Ptr);
494 mmesa->UbyteColor.Ptr = 0;
495 }
496 }
497