Merge branch 'nouveau-import'
[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 <stdlib.h>
30 #include "mgacontext.h"
31 #include "mgavb.h"
32 #include "mgatris.h"
33 #include "mgaioctl.h"
34 #include "mga_xmesa.h"
35
36 #include "glheader.h"
37 #include "mtypes.h"
38 #include "imports.h"
39 #include "macros.h"
40 #include "colormac.h"
41
42 #include "tnl/t_context.h"
43 #include "swrast_setup/swrast_setup.h"
44 #include "swrast/swrast.h"
45
46
47 #define MGA_TEX1_BIT 0x1
48 #define MGA_TEX0_BIT 0x2
49 #define MGA_RGBA_BIT 0x4
50 #define MGA_SPEC_BIT 0x8
51 #define MGA_FOG_BIT 0x10
52 #define MGA_XYZW_BIT 0x20
53 #define MGA_PTEX_BIT 0x40
54 #define MGA_MAX_SETUP 0x80
55
56 static struct {
57 void (*emit)( GLcontext *, GLuint, GLuint, void *, GLuint );
58 tnl_interp_func interp;
59 tnl_copy_pv_func copy_pv;
60 GLboolean (*check_tex_sizes)( GLcontext *ctx );
61 GLuint vertex_size;
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_SIZE() mmesa->vertex_size * sizeof(GLuint)
94
95 #define HAVE_HW_VIEWPORT 0
96 #define HAVE_HW_DIVIDE 0
97 #define HAVE_RGBA_COLOR 0
98 #define HAVE_TINY_VERTICES 0
99 #define HAVE_NOTEX_VERTICES 0
100 #define HAVE_TEX0_VERTICES 1
101 #define HAVE_TEX1_VERTICES 1
102 #define HAVE_TEX2_VERTICES 0
103 #define HAVE_TEX3_VERTICES 0
104 #define HAVE_PTEX_VERTICES 0
105
106 #define UNVIEWPORT_VARS \
107 const GLfloat dx = - mmesa->drawX - SUBPIXEL_X; \
108 const GLfloat dy = (mmesa->driDrawable->h + \
109 mmesa->drawY + SUBPIXEL_Y); \
110 const GLfloat sz = 1.0 / mmesa->depth_scale
111
112 #define UNVIEWPORT_X(x) x + dx;
113 #define UNVIEWPORT_Y(y) - y + dy;
114 #define UNVIEWPORT_Z(z) z * sz;
115
116 #define PTEX_FALLBACK() FALLBACK(ctx, MGA_FALLBACK_TEXTURE, 1)
117
118 #define INTERP_VERTEX setup_tab[mmesa->SetupIndex].interp
119 #define COPY_PV_VERTEX setup_tab[mmesa->SetupIndex].copy_pv
120
121
122 /***********************************************************************
123 * Generate pv-copying and translation functions *
124 ***********************************************************************/
125
126 #define TAG(x) mga_##x
127 #include "tnl_dd/t_dd_vb.c"
128
129 /***********************************************************************
130 * Generate vertex emit and interp functions *
131 ***********************************************************************/
132
133
134 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT)
135 #define TAG(x) x##_wg
136 #include "tnl_dd/t_dd_vbtmp.h"
137
138 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_SPEC_BIT)
139 #define TAG(x) x##_wgs
140 #include "tnl_dd/t_dd_vbtmp.h"
141
142 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_TEX0_BIT)
143 #define TAG(x) x##_wgt0
144 #include "tnl_dd/t_dd_vbtmp.h"
145
146 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
147 #define TAG(x) x##_wgt0t1
148 #include "tnl_dd/t_dd_vbtmp.h"
149
150 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_TEX0_BIT|MGA_PTEX_BIT)
151 #define TAG(x) x##_wgpt0
152 #include "tnl_dd/t_dd_vbtmp.h"
153
154 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT)
155 #define TAG(x) x##_wgst0
156 #include "tnl_dd/t_dd_vbtmp.h"
157
158 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
159 #define TAG(x) x##_wgst0t1
160 #include "tnl_dd/t_dd_vbtmp.h"
161
162 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT|MGA_PTEX_BIT)
163 #define TAG(x) x##_wgspt0
164 #include "tnl_dd/t_dd_vbtmp.h"
165
166 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_FOG_BIT)
167 #define TAG(x) x##_wgf
168 #include "tnl_dd/t_dd_vbtmp.h"
169
170 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_FOG_BIT|MGA_SPEC_BIT)
171 #define TAG(x) x##_wgfs
172 #include "tnl_dd/t_dd_vbtmp.h"
173
174 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_FOG_BIT|MGA_TEX0_BIT)
175 #define TAG(x) x##_wgft0
176 #include "tnl_dd/t_dd_vbtmp.h"
177
178 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_FOG_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
179 #define TAG(x) x##_wgft0t1
180 #include "tnl_dd/t_dd_vbtmp.h"
181
182 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_FOG_BIT|MGA_TEX0_BIT|MGA_PTEX_BIT)
183 #define TAG(x) x##_wgfpt0
184 #include "tnl_dd/t_dd_vbtmp.h"
185
186 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_FOG_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT)
187 #define TAG(x) x##_wgfst0
188 #include "tnl_dd/t_dd_vbtmp.h"
189
190 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_FOG_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
191 #define TAG(x) x##_wgfst0t1
192 #include "tnl_dd/t_dd_vbtmp.h"
193
194 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_FOG_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT|MGA_PTEX_BIT)
195 #define TAG(x) x##_wgfspt0
196 #include "tnl_dd/t_dd_vbtmp.h"
197
198 #define IND (MGA_TEX0_BIT)
199 #define TAG(x) x##_t0
200 #include "tnl_dd/t_dd_vbtmp.h"
201
202 #define IND (MGA_TEX0_BIT|MGA_TEX1_BIT)
203 #define TAG(x) x##_t0t1
204 #include "tnl_dd/t_dd_vbtmp.h"
205
206 #define IND (MGA_FOG_BIT)
207 #define TAG(x) x##_f
208 #include "tnl_dd/t_dd_vbtmp.h"
209
210 #define IND (MGA_FOG_BIT|MGA_TEX0_BIT)
211 #define TAG(x) x##_ft0
212 #include "tnl_dd/t_dd_vbtmp.h"
213
214 #define IND (MGA_FOG_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
215 #define TAG(x) x##_ft0t1
216 #include "tnl_dd/t_dd_vbtmp.h"
217
218 #define IND (MGA_RGBA_BIT)
219 #define TAG(x) x##_g
220 #include "tnl_dd/t_dd_vbtmp.h"
221
222 #define IND (MGA_RGBA_BIT|MGA_SPEC_BIT)
223 #define TAG(x) x##_gs
224 #include "tnl_dd/t_dd_vbtmp.h"
225
226 #define IND (MGA_RGBA_BIT|MGA_TEX0_BIT)
227 #define TAG(x) x##_gt0
228 #include "tnl_dd/t_dd_vbtmp.h"
229
230 #define IND (MGA_RGBA_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
231 #define TAG(x) x##_gt0t1
232 #include "tnl_dd/t_dd_vbtmp.h"
233
234 #define IND (MGA_RGBA_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT)
235 #define TAG(x) x##_gst0
236 #include "tnl_dd/t_dd_vbtmp.h"
237
238 #define IND (MGA_RGBA_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
239 #define TAG(x) x##_gst0t1
240 #include "tnl_dd/t_dd_vbtmp.h"
241
242 #define IND (MGA_RGBA_BIT|MGA_FOG_BIT)
243 #define TAG(x) x##_gf
244 #include "tnl_dd/t_dd_vbtmp.h"
245
246 #define IND (MGA_RGBA_BIT|MGA_FOG_BIT|MGA_SPEC_BIT)
247 #define TAG(x) x##_gfs
248 #include "tnl_dd/t_dd_vbtmp.h"
249
250 #define IND (MGA_RGBA_BIT|MGA_FOG_BIT|MGA_TEX0_BIT)
251 #define TAG(x) x##_gft0
252 #include "tnl_dd/t_dd_vbtmp.h"
253
254 #define IND (MGA_RGBA_BIT|MGA_FOG_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
255 #define TAG(x) x##_gft0t1
256 #include "tnl_dd/t_dd_vbtmp.h"
257
258 #define IND (MGA_RGBA_BIT|MGA_FOG_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT)
259 #define TAG(x) x##_gfst0
260 #include "tnl_dd/t_dd_vbtmp.h"
261
262 #define IND (MGA_RGBA_BIT|MGA_FOG_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
263 #define TAG(x) x##_gfst0t1
264 #include "tnl_dd/t_dd_vbtmp.h"
265
266
267 static void init_setup_tab( void )
268 {
269 init_wg();
270 init_wgs();
271 init_wgt0();
272 init_wgt0t1();
273 init_wgpt0();
274 init_wgst0();
275 init_wgst0t1();
276 init_wgspt0();
277 init_wgf();
278 init_wgfs();
279 init_wgft0();
280 init_wgft0t1();
281 init_wgfpt0();
282 init_wgfst0();
283 init_wgfst0t1();
284 init_wgfspt0();
285 init_t0();
286 init_t0t1();
287 init_f();
288 init_ft0();
289 init_ft0t1();
290 init_g();
291 init_gs();
292 init_gt0();
293 init_gt0t1();
294 init_gst0();
295 init_gst0t1();
296 init_gf();
297 init_gfs();
298 init_gft0();
299 init_gft0t1();
300 init_gfst0();
301 init_gfst0t1();
302 }
303
304
305
306
307 void mgaPrintSetupFlags(char *msg, GLuint flags )
308 {
309 fprintf(stderr, "%s: %d %s%s%s%s%s%s\n",
310 msg,
311 (int)flags,
312 (flags & MGA_XYZW_BIT) ? " xyzw," : "",
313 (flags & MGA_RGBA_BIT) ? " rgba," : "",
314 (flags & MGA_SPEC_BIT) ? " spec," : "",
315 (flags & MGA_FOG_BIT) ? " fog," : "",
316 (flags & MGA_TEX0_BIT) ? " tex-0," : "",
317 (flags & MGA_TEX1_BIT) ? " tex-1," : "");
318 }
319
320
321 void mgaCheckTexSizes( GLcontext *ctx )
322 {
323 mgaContextPtr mmesa = MGA_CONTEXT( ctx );
324 TNLcontext *tnl = TNL_CONTEXT(ctx);
325
326 /*fprintf(stderr, "%s\n", __FUNCTION__);*/
327
328 if (!setup_tab[mmesa->SetupIndex].check_tex_sizes(ctx)) {
329 mmesa->SetupIndex |= MGA_PTEX_BIT;
330 mmesa->SetupNewInputs = ~0;
331
332 if (!mmesa->Fallback &&
333 !(ctx->_TriangleCaps & (DD_TRI_LIGHT_TWOSIDE|DD_TRI_UNFILLED))) {
334 tnl->Driver.Render.Interp = setup_tab[mmesa->SetupIndex].interp;
335 tnl->Driver.Render.CopyPV = setup_tab[mmesa->SetupIndex].copy_pv;
336 }
337 if (mmesa->Fallback) {
338 tnl->Driver.Render.Start(ctx);
339 }
340 }
341 }
342
343
344 void mgaBuildVertices( GLcontext *ctx,
345 GLuint start,
346 GLuint count,
347 GLuint newinputs )
348 {
349 mgaContextPtr mmesa = MGA_CONTEXT( ctx );
350 GLuint stride = mmesa->vertex_size * sizeof(int);
351 GLubyte *v = ((GLubyte *)mmesa->verts + (start * stride));
352
353 newinputs |= mmesa->SetupNewInputs;
354 mmesa->SetupNewInputs = 0;
355
356 if (!newinputs)
357 return;
358
359 if (newinputs & VERT_BIT_POS) {
360 setup_tab[mmesa->SetupIndex].emit( ctx, start, count, v, stride );
361 } else {
362 GLuint ind = 0;
363
364 if (newinputs & VERT_BIT_COLOR0)
365 ind |= MGA_RGBA_BIT;
366
367 if (newinputs & VERT_BIT_COLOR1)
368 ind |= MGA_SPEC_BIT;
369
370 if (newinputs & VERT_BIT_TEX0)
371 ind |= MGA_TEX0_BIT;
372
373 if (newinputs & VERT_BIT_TEX1)
374 ind |= MGA_TEX0_BIT|MGA_TEX1_BIT;
375
376 if (newinputs & VERT_BIT_FOG)
377 ind |= MGA_FOG_BIT;
378
379 if (mmesa->SetupIndex & MGA_PTEX_BIT)
380 ind = ~0;
381
382 ind &= mmesa->SetupIndex;
383
384 if (ind) {
385 setup_tab[ind].emit( ctx, start, count, v, stride );
386 }
387 }
388 }
389
390
391 void mgaChooseVertexState( GLcontext *ctx )
392 {
393 mgaContextPtr mmesa = MGA_CONTEXT( ctx );
394 TNLcontext *tnl = TNL_CONTEXT(ctx);
395 GLuint ind = MGA_XYZW_BIT|MGA_RGBA_BIT;
396
397 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR)
398 ind |= MGA_SPEC_BIT;
399
400 if (ctx->Fog.Enabled)
401 ind |= MGA_FOG_BIT;
402
403 if (ctx->Texture._EnabledUnits & 0x2) {
404 /* unit 1 enabled */
405 if (ctx->Texture._EnabledUnits & 0x1) {
406 /* unit 0 enabled */
407 ind |= MGA_TEX1_BIT|MGA_TEX0_BIT;
408 }
409 else {
410 ind |= MGA_TEX0_BIT;
411 }
412 }
413 else if (ctx->Texture._EnabledUnits & 0x1) {
414 /* unit 0 enabled */
415 ind |= MGA_TEX0_BIT;
416 }
417
418 mmesa->SetupIndex = ind;
419
420 if (ctx->_TriangleCaps & (DD_TRI_LIGHT_TWOSIDE|DD_TRI_UNFILLED)) {
421 tnl->Driver.Render.Interp = mga_interp_extras;
422 tnl->Driver.Render.CopyPV = mga_copy_pv_extras;
423 } else {
424 tnl->Driver.Render.Interp = setup_tab[ind].interp;
425 tnl->Driver.Render.CopyPV = setup_tab[ind].copy_pv;
426 }
427
428 if (setup_tab[ind].vertex_format != mmesa->vertex_format) {
429 FLUSH_BATCH(mmesa);
430 mmesa->dirty |= MGA_UPLOAD_PIPE;
431 mmesa->vertex_format = setup_tab[ind].vertex_format;
432 mmesa->vertex_size = setup_tab[ind].vertex_size;
433 }
434 }
435
436
437
438 void *mga_emit_contiguous_verts( GLcontext *ctx,
439 GLuint start,
440 GLuint count,
441 void *dest)
442 {
443 mgaContextPtr mmesa = MGA_CONTEXT(ctx);
444 GLuint stride = mmesa->vertex_size * 4;
445 setup_tab[mmesa->SetupIndex].emit( ctx, start, count, dest, stride );
446 return (void *)((char *)dest + stride * (count - start));
447 }
448
449
450
451 void mgaInitVB( GLcontext *ctx )
452 {
453 mgaContextPtr mmesa = MGA_CONTEXT(ctx);
454 GLuint size = TNL_CONTEXT(ctx)->vb.Size;
455
456 mmesa->verts = (GLubyte *)ALIGN_MALLOC(size * sizeof(mgaVertex), 32);
457
458 {
459 static int firsttime = 1;
460 if (firsttime) {
461 init_setup_tab();
462 firsttime = 0;
463 }
464 }
465
466 mmesa->dirty |= MGA_UPLOAD_PIPE;
467 mmesa->vertex_format = setup_tab[0].vertex_format;
468 mmesa->vertex_size = setup_tab[0].vertex_size;
469 }
470
471
472 void mgaFreeVB( GLcontext *ctx )
473 {
474 mgaContextPtr mmesa = MGA_CONTEXT(ctx);
475 if (mmesa->verts) {
476 ALIGN_FREE(mmesa->verts);
477 mmesa->verts = 0;
478 }
479 }
480