2 * Copyright 2000-2001 VA Linux Systems, Inc.
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:
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
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.
25 * Keith Whitwell <keith@tungstengraphics.com>
27 /* $XFree86: xc/lib/GL/mesa/src/drv/mga/mgavb.c,v 1.15 2003/03/26 20:43:49 tsi Exp $ */
29 #include "mgacontext.h"
33 #include "mga_xmesa.h"
41 #include "tnl/t_context.h"
42 #include "swrast_setup/swrast_setup.h"
43 #include "swrast/swrast.h"
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
56 void (*emit
)( GLcontext
*, GLuint
, GLuint
, void *, GLuint
);
59 GLboolean (*check_tex_sizes
)( GLcontext
*ctx
);
61 GLuint vertex_stride_shift
;
63 } setup_tab
[MGA_MAX_SETUP
];
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
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)
83 #define DO_PTEX (IND & MGA_PTEX_BIT)
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
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
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
114 #define UNVIEWPORT_X(x) x + dx;
115 #define UNVIEWPORT_Y(y) - y + dy;
116 #define UNVIEWPORT_Z(z) z * sz;
118 #define PTEX_FALLBACK() FALLBACK(ctx, MGA_FALLBACK_TEXTURE, 1)
121 #define IMPORT_FLOAT_COLORS mga_import_float_colors
122 #define IMPORT_FLOAT_SPEC_COLORS mga_import_float_spec_colors
124 #define INTERP_VERTEX setup_tab[MGA_CONTEXT(ctx)->SetupIndex].interp
125 #define COPY_PV_VERTEX setup_tab[MGA_CONTEXT(ctx)->SetupIndex].copy_pv
128 /***********************************************************************
129 * Generate pv-copying and translation functions *
130 ***********************************************************************/
132 #define TAG(x) mga_##x
133 #include "tnl_dd/t_dd_vb.c"
135 /***********************************************************************
136 * Generate vertex emit and interp functions *
137 ***********************************************************************/
140 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT)
141 #define TAG(x) x##_wg
142 #include "tnl_dd/t_dd_vbtmp.h"
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"
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"
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"
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"
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"
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"
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"
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"
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"
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"
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"
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"
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"
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"
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"
204 #define IND (MGA_TEX0_BIT)
205 #define TAG(x) x##_t0
206 #include "tnl_dd/t_dd_vbtmp.h"
208 #define IND (MGA_TEX0_BIT|MGA_TEX1_BIT)
209 #define TAG(x) x##_t0t1
210 #include "tnl_dd/t_dd_vbtmp.h"
212 #define IND (MGA_FOG_BIT)
214 #include "tnl_dd/t_dd_vbtmp.h"
216 #define IND (MGA_FOG_BIT|MGA_TEX0_BIT)
217 #define TAG(x) x##_ft0
218 #include "tnl_dd/t_dd_vbtmp.h"
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"
224 #define IND (MGA_RGBA_BIT)
226 #include "tnl_dd/t_dd_vbtmp.h"
228 #define IND (MGA_RGBA_BIT|MGA_SPEC_BIT)
229 #define TAG(x) x##_gs
230 #include "tnl_dd/t_dd_vbtmp.h"
232 #define IND (MGA_RGBA_BIT|MGA_TEX0_BIT)
233 #define TAG(x) x##_gt0
234 #include "tnl_dd/t_dd_vbtmp.h"
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"
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"
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"
248 #define IND (MGA_RGBA_BIT|MGA_FOG_BIT)
249 #define TAG(x) x##_gf
250 #include "tnl_dd/t_dd_vbtmp.h"
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"
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"
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"
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"
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"
273 static void init_setup_tab( void )
313 void mgaPrintSetupFlags(char *msg
, GLuint flags
)
315 fprintf(stderr
, "%s: %d %s%s%s%s%s%s\n",
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," : "");
327 void mgaCheckTexSizes( GLcontext
*ctx
)
329 mgaContextPtr mmesa
= MGA_CONTEXT( ctx
);
330 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
332 /*fprintf(stderr, "%s\n", __FUNCTION__);*/
334 if (!setup_tab
[mmesa
->SetupIndex
].check_tex_sizes(ctx
)) {
335 mmesa
->SetupIndex
|= MGA_PTEX_BIT
;
336 mmesa
->SetupNewInputs
= ~0;
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
;
343 if (mmesa
->Fallback
) {
344 tnl
->Driver
.Render
.Start(ctx
);
350 void mgaBuildVertices( GLcontext
*ctx
,
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
;
359 newinputs
|= mmesa
->SetupNewInputs
;
360 mmesa
->SetupNewInputs
= 0;
365 if (newinputs
& VERT_BIT_POS
) {
366 setup_tab
[mmesa
->SetupIndex
].emit( ctx
, start
, count
, v
, stride
);
370 if (newinputs
& VERT_BIT_COLOR0
)
373 if (newinputs
& VERT_BIT_COLOR1
)
376 if (newinputs
& VERT_BIT_TEX0
)
379 if (newinputs
& VERT_BIT_TEX1
)
380 ind
|= MGA_TEX0_BIT
|MGA_TEX1_BIT
;
382 if (newinputs
& VERT_BIT_FOG
)
385 if (mmesa
->SetupIndex
& MGA_PTEX_BIT
)
388 ind
&= mmesa
->SetupIndex
;
391 setup_tab
[ind
].emit( ctx
, start
, count
, v
, stride
);
397 void mgaChooseVertexState( GLcontext
*ctx
)
399 mgaContextPtr mmesa
= MGA_CONTEXT( ctx
);
400 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
401 GLuint ind
= MGA_XYZW_BIT
|MGA_RGBA_BIT
;
403 if (ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
)
406 if (ctx
->Fog
.Enabled
)
409 if (ctx
->Texture
._EnabledUnits
& 0x2) {
411 if (ctx
->Texture
._EnabledUnits
& 0x1) {
413 ind
|= MGA_TEX1_BIT
|MGA_TEX0_BIT
;
419 else if (ctx
->Texture
._EnabledUnits
& 0x1) {
424 mmesa
->SetupIndex
= ind
;
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
;
430 tnl
->Driver
.Render
.Interp
= setup_tab
[ind
].interp
;
431 tnl
->Driver
.Render
.CopyPV
= setup_tab
[ind
].copy_pv
;
434 if (setup_tab
[ind
].vertex_format
!= mmesa
->vertex_format
) {
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
;
445 void mga_emit_contiguous_verts( GLcontext
*ctx
,
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
);
457 void mgaInitVB( GLcontext
*ctx
)
459 mgaContextPtr mmesa
= MGA_CONTEXT(ctx
);
460 GLuint size
= TNL_CONTEXT(ctx
)->vb
.Size
;
462 mmesa
->verts
= (GLubyte
*)ALIGN_MALLOC(size
* sizeof(mgaVertex
), 32);
465 static int firsttime
= 1;
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
;
479 void mgaFreeVB( GLcontext
*ctx
)
481 mgaContextPtr mmesa
= MGA_CONTEXT(ctx
);
483 ALIGN_FREE(mmesa
->verts
);
487 if (mmesa
->UbyteSecondaryColor
.Ptr
) {
488 ALIGN_FREE(mmesa
->UbyteSecondaryColor
.Ptr
);
489 mmesa
->UbyteSecondaryColor
.Ptr
= 0;
492 if (mmesa
->UbyteColor
.Ptr
) {
493 ALIGN_FREE(mmesa
->UbyteColor
.Ptr
);
494 mmesa
->UbyteColor
.Ptr
= 0;