uint*t -> u_int*t changes
[mesa.git] / src / mesa / drivers / dri / r200 / r200_tcl.c
1 /* $XFree86: xc/lib/GL/mesa/src/drv/r200/r200_tcl.c,v 1.2 2002/12/16 16:18:55 dawes Exp $ */
2 /*
3 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
4
5 The Weather Channel (TM) funded Tungsten Graphics to develop the
6 initial release of the Radeon 8500 driver under the XFree86 license.
7 This notice must be preserved.
8
9 Permission is hereby granted, free of charge, to any person obtaining
10 a copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sublicense, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
16
17 The above copyright notice and this permission notice (including the
18 next paragraph) shall be included in all copies or substantial
19 portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
25 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
29 **************************************************************************/
30
31 /*
32 * Authors:
33 * Keith Whitwell <keith@tungstengraphics.com>
34 */
35
36 #include "glheader.h"
37 #include "imports.h"
38 #include "mtypes.h"
39 #include "enums.h"
40 #include "colormac.h"
41 #include "light.h"
42
43 #include "array_cache/acache.h"
44 #include "tnl/tnl.h"
45 #include "tnl/t_pipeline.h"
46
47 #include "r200_context.h"
48 #include "r200_state.h"
49 #include "r200_ioctl.h"
50 #include "r200_tex.h"
51 #include "r200_tcl.h"
52 #include "r200_swtcl.h"
53 #include "r200_maos.h"
54
55
56
57 #define HAVE_POINTS 1
58 #define HAVE_LINES 1
59 #define HAVE_LINE_LOOP 0
60 #define HAVE_LINE_STRIPS 1
61 #define HAVE_TRIANGLES 1
62 #define HAVE_TRI_STRIPS 1
63 #define HAVE_TRI_STRIP_1 0
64 #define HAVE_TRI_FANS 1
65 #define HAVE_QUADS 1
66 #define HAVE_QUAD_STRIPS 1
67 #define HAVE_POLYGONS 1
68 #define HAVE_ELTS 1
69
70
71 #define HW_POINTS R200_VF_PRIM_POINTS
72 #define HW_LINES R200_VF_PRIM_LINES
73 #define HW_LINE_LOOP 0
74 #define HW_LINE_STRIP R200_VF_PRIM_LINE_STRIP
75 #define HW_TRIANGLES R200_VF_PRIM_TRIANGLES
76 #define HW_TRIANGLE_STRIP_0 R200_VF_PRIM_TRIANGLE_STRIP
77 #define HW_TRIANGLE_STRIP_1 0
78 #define HW_TRIANGLE_FAN R200_VF_PRIM_TRIANGLE_FAN
79 #define HW_QUADS R200_VF_PRIM_QUADS
80 #define HW_QUAD_STRIP R200_VF_PRIM_QUAD_STRIP
81 #define HW_POLYGON R200_VF_PRIM_POLYGON
82
83
84 static GLboolean discrete_prim[0x10] = {
85 0, /* 0 none */
86 1, /* 1 points */
87 1, /* 2 lines */
88 0, /* 3 line_strip */
89 1, /* 4 tri_list */
90 0, /* 5 tri_fan */
91 0, /* 6 tri_strip */
92 0, /* 7 tri_w_flags */
93 1, /* 8 rect list (unused) */
94 1, /* 9 3vert point */
95 1, /* a 3vert line */
96 0, /* b point sprite */
97 0, /* c line loop */
98 1, /* d quads */
99 0, /* e quad strip */
100 0, /* f polygon */
101 };
102
103
104 #define LOCAL_VARS r200ContextPtr rmesa = R200_CONTEXT(ctx)
105 #define ELT_TYPE GLushort
106
107 #define ELT_INIT(prim, hw_prim) \
108 r200TclPrimitive( ctx, prim, hw_prim | R200_VF_PRIM_WALK_IND )
109
110 #define GET_MESA_ELTS() rmesa->tcl.Elts
111
112
113 /* Don't really know how many elts will fit in what's left of cmdbuf,
114 * as there is state to emit, etc:
115 */
116
117 /* Testing on isosurf shows a maximum around here. Don't know if it's
118 * the card or driver or kernel module that is causing the behaviour.
119 */
120 #define GET_MAX_HW_ELTS() 300
121
122 #define RESET_STIPPLE() do { \
123 R200_STATECHANGE( rmesa, lin ); \
124 r200EmitState( rmesa ); \
125 } while (0)
126
127 #define AUTO_STIPPLE( mode ) do { \
128 R200_STATECHANGE( rmesa, lin ); \
129 if (mode) \
130 rmesa->hw.lin.cmd[LIN_RE_LINE_PATTERN] |= \
131 R200_LINE_PATTERN_AUTO_RESET; \
132 else \
133 rmesa->hw.lin.cmd[LIN_RE_LINE_PATTERN] &= \
134 ~R200_LINE_PATTERN_AUTO_RESET; \
135 r200EmitState( rmesa ); \
136 } while (0)
137
138
139 #define ALLOC_ELTS(nr) r200AllocElts( rmesa, nr )
140
141 static GLushort *r200AllocElts( r200ContextPtr rmesa, GLuint nr )
142 {
143 if (rmesa->dma.flush == r200FlushElts &&
144 rmesa->store.cmd_used + nr*2 < R200_CMD_BUF_SZ) {
145
146 GLushort *dest = (GLushort *)(rmesa->store.cmd_buf +
147 rmesa->store.cmd_used);
148
149 rmesa->store.cmd_used += nr*2;
150
151 return dest;
152 }
153 else {
154 if (rmesa->dma.flush)
155 rmesa->dma.flush( rmesa );
156
157 r200EnsureCmdBufSpace( rmesa, AOS_BUFSZ(rmesa->tcl.nr_aos_components) +
158 rmesa->hw.max_state_size + ELTS_BUFSZ(nr) );
159
160 r200EmitAOS( rmesa,
161 rmesa->tcl.aos_components,
162 rmesa->tcl.nr_aos_components, 0 );
163
164 return r200AllocEltsOpenEnded( rmesa, rmesa->tcl.hw_primitive, nr );
165 }
166 }
167
168
169 #define CLOSE_ELTS() \
170 do { \
171 if (0) R200_NEWPRIM( rmesa ); \
172 } \
173 while (0)
174
175
176 /* TODO: Try to extend existing primitive if both are identical,
177 * discrete and there are no intervening state changes. (Somewhat
178 * duplicates changes to DrawArrays code)
179 */
180 static void r200EmitPrim( GLcontext *ctx,
181 GLenum prim,
182 GLuint hwprim,
183 GLuint start,
184 GLuint count)
185 {
186 r200ContextPtr rmesa = R200_CONTEXT( ctx );
187 r200TclPrimitive( ctx, prim, hwprim );
188
189 r200EnsureCmdBufSpace( rmesa, AOS_BUFSZ(rmesa->tcl.nr_aos_components) +
190 rmesa->hw.max_state_size + VBUF_BUFSZ );
191
192 r200EmitAOS( rmesa,
193 rmesa->tcl.aos_components,
194 rmesa->tcl.nr_aos_components,
195 start );
196
197 /* Why couldn't this packet have taken an offset param?
198 */
199 r200EmitVbufPrim( rmesa,
200 rmesa->tcl.hw_primitive,
201 count - start );
202 }
203
204 #define EMIT_PRIM(ctx, prim, hwprim, start, count) do { \
205 r200EmitPrim( ctx, prim, hwprim, start, count ); \
206 (void) rmesa; } while (0)
207
208 /* Try & join small primitives
209 */
210 #if 0
211 #define PREFER_DISCRETE_ELT_PRIM( NR, PRIM ) 0
212 #else
213 #define PREFER_DISCRETE_ELT_PRIM( NR, PRIM ) \
214 ((NR) < 20 || \
215 ((NR) < 40 && \
216 rmesa->tcl.hw_primitive == (PRIM| \
217 R200_VF_TCL_OUTPUT_VTX_ENABLE| \
218 R200_VF_PRIM_WALK_IND)))
219 #endif
220
221 #ifdef MESA_BIG_ENDIAN
222 /* We could do without (most of) this ugliness if dest was always 32 bit word aligned... */
223 #define EMIT_ELT(dest, offset, x) do { \
224 int off = offset + ( ( (GLuint)dest & 0x2 ) >> 1 ); \
225 GLushort *des = (GLushort *)( (GLuint)dest & ~0x2 ); \
226 (des)[ off + 1 - 2 * ( off & 1 ) ] = (GLushort)(x); \
227 (void)rmesa; } while (0)
228 #else
229 #define EMIT_ELT(dest, offset, x) do { \
230 (dest)[offset] = (GLushort) (x); \
231 (void)rmesa; } while (0)
232 #endif
233
234 #define EMIT_TWO_ELTS(dest, offset, x, y) *(GLuint *)((dest)+offset) = ((y)<<16)|(x);
235
236
237
238 #define TAG(x) tcl_##x
239 #include "tnl_dd/t_dd_dmatmp2.h"
240
241 /**********************************************************************/
242 /* External entrypoints */
243 /**********************************************************************/
244
245 void r200EmitPrimitive( GLcontext *ctx,
246 GLuint first,
247 GLuint last,
248 GLuint flags )
249 {
250 tcl_render_tab_verts[flags&PRIM_MODE_MASK]( ctx, first, last, flags );
251 }
252
253 void r200EmitEltPrimitive( GLcontext *ctx,
254 GLuint first,
255 GLuint last,
256 GLuint flags )
257 {
258 tcl_render_tab_elts[flags&PRIM_MODE_MASK]( ctx, first, last, flags );
259 }
260
261 void r200TclPrimitive( GLcontext *ctx,
262 GLenum prim,
263 int hw_prim )
264 {
265 r200ContextPtr rmesa = R200_CONTEXT(ctx);
266 GLuint newprim = hw_prim | R200_VF_TCL_OUTPUT_VTX_ENABLE;
267
268 if (newprim != rmesa->tcl.hw_primitive ||
269 !discrete_prim[hw_prim&0xf]) {
270 R200_NEWPRIM( rmesa );
271 rmesa->tcl.hw_primitive = newprim;
272 }
273 }
274
275
276 /**********************************************************************/
277 /* Fog blend factor computation for hw tcl */
278 /* same calculation used as in t_vb_fog.c */
279 /**********************************************************************/
280
281 #define FOG_EXP_TABLE_SIZE 256
282 #define FOG_MAX (10.0)
283 #define EXP_FOG_MAX .0006595
284 #define FOG_INCR (FOG_MAX/FOG_EXP_TABLE_SIZE)
285 static GLfloat exp_table[FOG_EXP_TABLE_SIZE];
286
287 #if 1
288 #define NEG_EXP( result, narg ) \
289 do { \
290 GLfloat f = (GLfloat) (narg * (1.0/FOG_INCR)); \
291 GLint k = (GLint) f; \
292 if (k > FOG_EXP_TABLE_SIZE-2) \
293 result = (GLfloat) EXP_FOG_MAX; \
294 else \
295 result = exp_table[k] + (f-k)*(exp_table[k+1]-exp_table[k]); \
296 } while (0)
297 #else
298 #define NEG_EXP( result, narg ) \
299 do { \
300 result = exp(-narg); \
301 } while (0)
302 #endif
303
304
305 /**
306 * Initialize the exp_table[] lookup table for approximating exp().
307 */
308 void
309 r200InitStaticFogData( void )
310 {
311 GLfloat f = 0.0F;
312 GLint i = 0;
313 for ( ; i < FOG_EXP_TABLE_SIZE ; i++, f += FOG_INCR) {
314 exp_table[i] = (GLfloat) exp(-f);
315 }
316 }
317
318
319 /**
320 * Compute per-vertex fog blend factors from fog coordinates by
321 * evaluating the GL_LINEAR, GL_EXP or GL_EXP2 fog function.
322 * Fog coordinates are distances from the eye (typically between the
323 * near and far clip plane distances).
324 * Note the fog (eye Z) coords may be negative so we use ABS(z) below.
325 * Fog blend factors are in the range [0,1].
326 */
327 float
328 r200ComputeFogBlendFactor( GLcontext *ctx, GLfloat fogcoord )
329 {
330 GLfloat end = ctx->Fog.End;
331 GLfloat d, temp;
332 const GLfloat z = FABSF(fogcoord);
333
334 switch (ctx->Fog.Mode) {
335 case GL_LINEAR:
336 if (ctx->Fog.Start == ctx->Fog.End)
337 d = 1.0F;
338 else
339 d = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
340 temp = (end - z) * d;
341 return CLAMP(temp, 0.0F, 1.0F);
342 break;
343 case GL_EXP:
344 d = ctx->Fog.Density;
345 NEG_EXP( temp, d * z );
346 return temp;
347 break;
348 case GL_EXP2:
349 d = ctx->Fog.Density*ctx->Fog.Density;
350 NEG_EXP( temp, d * z * z );
351 return temp;
352 break;
353 default:
354 _mesa_problem(ctx, "Bad fog mode in make_fog_coord");
355 return 0;
356 }
357 }
358
359
360 /**********************************************************************/
361 /* Render pipeline stage */
362 /**********************************************************************/
363
364
365 /* TCL render.
366 */
367 static GLboolean r200_run_tcl_render( GLcontext *ctx,
368 struct tnl_pipeline_stage *stage )
369 {
370 r200ContextPtr rmesa = R200_CONTEXT(ctx);
371 TNLcontext *tnl = TNL_CONTEXT(ctx);
372 struct vertex_buffer *VB = &tnl->vb;
373 GLuint i;
374
375 /* TODO: separate this from the swtnl pipeline
376 */
377 if (rmesa->TclFallback)
378 return GL_TRUE; /* fallback to software t&l */
379
380 if (R200_DEBUG & DEBUG_PRIMS)
381 fprintf(stderr, "%s\n", __FUNCTION__);
382
383 if (VB->Count == 0)
384 return GL_FALSE;
385
386 r200ReleaseArrays( ctx, ~0 /* stage->changed_inputs */ );
387 r200EmitArrays( ctx, stage->inputs );
388
389 rmesa->tcl.Elts = VB->Elts;
390
391 for (i = 0 ; i < VB->PrimitiveCount ; i++)
392 {
393 GLuint prim = VB->Primitive[i].mode;
394 GLuint start = VB->Primitive[i].start;
395 GLuint length = VB->Primitive[i].count;
396
397 if (!length)
398 continue;
399
400 if (rmesa->tcl.Elts)
401 r200EmitEltPrimitive( ctx, start, start+length, prim );
402 else
403 r200EmitPrimitive( ctx, start, start+length, prim );
404 }
405
406 return GL_FALSE; /* finished the pipe */
407 }
408
409
410
411 static void r200_check_tcl_render( GLcontext *ctx,
412 struct tnl_pipeline_stage *stage )
413 {
414 r200ContextPtr rmesa = R200_CONTEXT(ctx);
415 GLuint inputs = VERT_BIT_POS;
416 GLuint unit;
417
418 /* Validate state:
419 */
420 if (rmesa->NewGLState)
421 r200ValidateState( ctx );
422
423 if (ctx->RenderMode == GL_RENDER) {
424 /* Make all this event-driven:
425 */
426 if (ctx->Light.Enabled) {
427 inputs |= VERT_BIT_NORMAL;
428
429 if (1 || ctx->Light.ColorMaterialEnabled) {
430 inputs |= VERT_BIT_COLOR0;
431 }
432 }
433 else {
434 inputs |= VERT_BIT_COLOR0;
435
436 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) {
437 inputs |= VERT_BIT_COLOR1;
438 }
439 }
440
441 if ( ctx->Fog.FogCoordinateSource == GL_FOG_COORD ) {
442 inputs |= VERT_BIT_FOG;
443 }
444
445 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++) {
446 if (ctx->Texture.Unit[unit]._ReallyEnabled) {
447 if (rmesa->TexGenNeedNormals[unit]) {
448 inputs |= VERT_BIT_NORMAL;
449 }
450 inputs |= VERT_BIT_TEX(unit);
451 }
452 }
453
454 stage->inputs = inputs;
455 stage->active = 1;
456 }
457 else
458 stage->active = 0;
459 }
460
461 static void r200_init_tcl_render( GLcontext *ctx,
462 struct tnl_pipeline_stage *stage )
463 {
464 stage->check = r200_check_tcl_render;
465 stage->check( ctx, stage );
466 }
467
468 static void dtr( struct tnl_pipeline_stage *stage )
469 {
470 (void)stage;
471 }
472
473
474 /* Initial state for tcl stage.
475 */
476 const struct tnl_pipeline_stage _r200_tcl_stage =
477 {
478 "r200 render",
479 (_DD_NEW_SEPARATE_SPECULAR |
480 _NEW_LIGHT|
481 _NEW_TEXTURE|
482 _NEW_FOG|
483 _NEW_RENDERMODE), /* re-check (new inputs) */
484 0, /* re-run (always runs) */
485 GL_TRUE, /* active */
486 0, 0, /* inputs (set in check_render), outputs */
487 0, 0, /* changed_inputs, private */
488 dtr, /* destructor */
489 r200_init_tcl_render, /* check - initially set to alloc data */
490 r200_run_tcl_render /* run */
491 };
492
493
494
495 /**********************************************************************/
496 /* Validate state at pipeline start */
497 /**********************************************************************/
498
499
500 /*-----------------------------------------------------------------------
501 * Manage TCL fallbacks
502 */
503
504
505 static void transition_to_swtnl( GLcontext *ctx )
506 {
507 r200ContextPtr rmesa = R200_CONTEXT(ctx);
508 TNLcontext *tnl = TNL_CONTEXT(ctx);
509
510 R200_NEWPRIM( rmesa );
511
512 r200ChooseVertexState( ctx );
513 r200ChooseRenderState( ctx );
514
515 _mesa_validate_all_lighting_tables( ctx );
516
517 tnl->Driver.NotifyMaterialChange =
518 _mesa_validate_all_lighting_tables;
519
520 r200ReleaseArrays( ctx, ~0 );
521
522 /* Still using the D3D based hardware-rasterizer from the radeon;
523 * need to put the card into D3D mode to make it work:
524 */
525 R200_STATECHANGE( rmesa, vap );
526 rmesa->hw.vap.cmd[VAP_SE_VAP_CNTL] &= ~R200_VAP_TCL_ENABLE;
527 }
528
529 static void transition_to_hwtnl( GLcontext *ctx )
530 {
531 r200ContextPtr rmesa = R200_CONTEXT(ctx);
532 TNLcontext *tnl = TNL_CONTEXT(ctx);
533
534 _tnl_need_projected_coords( ctx, GL_FALSE );
535
536 r200UpdateMaterial( ctx );
537
538 tnl->Driver.NotifyMaterialChange = r200UpdateMaterial;
539
540 if ( rmesa->dma.flush )
541 rmesa->dma.flush( rmesa );
542
543 rmesa->dma.flush = 0;
544
545 if (rmesa->swtcl.indexed_verts.buf)
546 r200ReleaseDmaRegion( rmesa, &rmesa->swtcl.indexed_verts,
547 __FUNCTION__ );
548
549 R200_STATECHANGE( rmesa, vap );
550 rmesa->hw.vap.cmd[VAP_SE_VAP_CNTL] |= R200_VAP_TCL_ENABLE;
551 rmesa->hw.vap.cmd[VAP_SE_VAP_CNTL] &= ~R200_VAP_FORCE_W_TO_ONE;
552
553 if ( ((rmesa->hw.ctx.cmd[CTX_PP_FOG_COLOR] & R200_FOG_USE_MASK)
554 == R200_FOG_USE_SPEC_ALPHA) &&
555 (ctx->Fog.FogCoordinateSource == GL_FOG_COORD )) {
556 R200_STATECHANGE( rmesa, ctx );
557 rmesa->hw.ctx.cmd[CTX_PP_FOG_COLOR] &= ~R200_FOG_USE_MASK;
558 rmesa->hw.ctx.cmd[CTX_PP_FOG_COLOR] |= R200_FOG_USE_VTX_FOG;
559 }
560
561 R200_STATECHANGE( rmesa, vte );
562 rmesa->hw.vte.cmd[VTE_SE_VTE_CNTL] &= ~(R200_VTX_XY_FMT|R200_VTX_Z_FMT);
563
564 if (R200_DEBUG & DEBUG_FALLBACKS)
565 fprintf(stderr, "R200 end tcl fallback\n");
566 }
567
568
569 static char *fallbackStrings[] = {
570 "Rasterization fallback",
571 "Unfilled triangles",
572 "Twosided lighting, differing materials",
573 "Materials in VB (maybe between begin/end)",
574 "Texgen unit 0",
575 "Texgen unit 1",
576 "Texgen unit 2",
577 "Texgen unit 3",
578 "Texgen unit 4",
579 "Texgen unit 5",
580 "User disable",
581 "Bitmap as points"
582 };
583
584
585 static char *getFallbackString(GLuint bit)
586 {
587 int i = 0;
588 while (bit > 1) {
589 i++;
590 bit >>= 1;
591 }
592 return fallbackStrings[i];
593 }
594
595
596
597 void r200TclFallback( GLcontext *ctx, GLuint bit, GLboolean mode )
598 {
599 r200ContextPtr rmesa = R200_CONTEXT(ctx);
600 GLuint oldfallback = rmesa->TclFallback;
601
602 if (mode) {
603 rmesa->TclFallback |= bit;
604 if (oldfallback == 0) {
605 if (R200_DEBUG & DEBUG_FALLBACKS)
606 fprintf(stderr, "R200 begin tcl fallback %s\n",
607 getFallbackString( bit ));
608 transition_to_swtnl( ctx );
609 }
610 }
611 else {
612 rmesa->TclFallback &= ~bit;
613 if (oldfallback == bit) {
614 if (R200_DEBUG & DEBUG_FALLBACKS)
615 fprintf(stderr, "R200 end tcl fallback %s\n",
616 getFallbackString( bit ));
617 transition_to_hwtnl( ctx );
618 }
619 }
620 }