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