1 /* $XFree86: xc/lib/GL/mesa/src/drv/r200/r200_tcl.c,v 1.2 2002/12/16 16:18:55 dawes Exp $ */
3 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
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.
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:
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.
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.
29 **************************************************************************/
33 * Keith Whitwell <keith@tungstengraphics.com>
43 #include "array_cache/acache.h"
45 #include "tnl/t_pipeline.h"
47 #include "r200_context.h"
48 #include "r200_state.h"
49 #include "r200_ioctl.h"
52 #include "r200_swtcl.h"
53 #include "r200_maos.h"
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 0 /* hw quad verts in wrong order??? */
66 #define HAVE_QUAD_STRIPS 1
67 #define HAVE_POLYGONS 1
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
84 static GLboolean discrete_prim
[0x10] = {
92 0, /* 7 tri_w_flags */
93 1, /* 8 rect list (unused) */
94 1, /* 9 3vert point */
96 0, /* b point sprite */
104 #define LOCAL_VARS r200ContextPtr rmesa = R200_CONTEXT(ctx); (void)rmesa
105 #define ELT_TYPE GLushort
107 #define ELT_INIT(prim, hw_prim) \
108 r200TclPrimitive( ctx, prim, hw_prim | R200_VF_PRIM_WALK_IND )
110 #define GET_MESA_ELTS() rmesa->tcl.Elts
113 /* Don't really know how many elts will fit in what's left of cmdbuf,
114 * as there is state to emit, etc:
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.
120 #define GET_MAX_HW_ELTS() 300
122 #define RESET_STIPPLE() do { \
123 R200_STATECHANGE( rmesa, lin ); \
124 r200EmitState( rmesa ); \
127 #define AUTO_STIPPLE( mode ) do { \
128 R200_STATECHANGE( rmesa, lin ); \
130 rmesa->hw.lin.cmd[LIN_RE_LINE_PATTERN] |= \
131 R200_LINE_PATTERN_AUTO_RESET; \
133 rmesa->hw.lin.cmd[LIN_RE_LINE_PATTERN] &= \
134 ~R200_LINE_PATTERN_AUTO_RESET; \
135 r200EmitState( rmesa ); \
139 #define ALLOC_ELTS(nr) r200AllocElts( rmesa, nr )
141 static GLushort
*r200AllocElts( r200ContextPtr rmesa
, GLuint nr
)
143 if (rmesa
->dma
.flush
== r200FlushElts
&&
144 rmesa
->store
.cmd_used
+ nr
*2 < R200_CMD_BUF_SZ
) {
146 GLushort
*dest
= (GLushort
*)(rmesa
->store
.cmd_buf
+
147 rmesa
->store
.cmd_used
);
149 rmesa
->store
.cmd_used
+= nr
*2;
154 if (rmesa
->dma
.flush
)
155 rmesa
->dma
.flush( rmesa
);
157 r200EnsureCmdBufSpace( rmesa
, AOS_BUFSZ(rmesa
->tcl
.nr_aos_components
) +
158 rmesa
->hw
.max_state_size
+ ELTS_BUFSZ(nr
) );
161 rmesa
->tcl
.aos_components
,
162 rmesa
->tcl
.nr_aos_components
, 0 );
164 return r200AllocEltsOpenEnded( rmesa
, rmesa
->tcl
.hw_primitive
, nr
);
169 #define CLOSE_ELTS() \
171 if (0) R200_NEWPRIM( rmesa ); \
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)
180 static void EMIT_PRIM( GLcontext
*ctx
,
186 r200ContextPtr rmesa
= R200_CONTEXT( ctx
);
187 r200TclPrimitive( ctx
, prim
, hwprim
);
189 r200EnsureCmdBufSpace( rmesa
, AOS_BUFSZ(rmesa
->tcl
.nr_aos_components
) +
190 rmesa
->hw
.max_state_size
+ VBUF_BUFSZ
);
193 rmesa
->tcl
.aos_components
,
194 rmesa
->tcl
.nr_aos_components
,
197 /* Why couldn't this packet have taken an offset param?
199 r200EmitVbufPrim( rmesa
,
200 rmesa
->tcl
.hw_primitive
,
206 /* Try & join small primitives
209 #define PREFER_DISCRETE_ELT_PRIM( NR, PRIM ) 0
211 #define PREFER_DISCRETE_ELT_PRIM( NR, PRIM ) \
214 rmesa->tcl.hw_primitive == (PRIM| \
215 R200_VF_TCL_OUTPUT_VTX_ENABLE| \
216 R200_VF_PRIM_WALK_IND)))
219 #ifdef MESA_BIG_ENDIAN
220 /* We could do without (most of) this ugliness if dest was always 32 bit word aligned... */
221 #define EMIT_ELT(dest, offset, x) do { \
222 int off = offset + ( ( (GLuint)dest & 0x2 ) >> 1 ); \
223 GLushort *des = (GLushort *)( (GLuint)dest & ~0x2 ); \
224 (des)[ off + 1 - 2 * ( off & 1 ) ] = (GLushort)(x); } while (0)
226 #define EMIT_ELT(dest, offset, x) (dest)[offset] = (GLushort) (x)
229 #define EMIT_TWO_ELTS(dest, offset, x, y) *(GLuint *)((dest)+offset) = ((y)<<16)|(x);
233 #define TAG(x) tcl_##x
234 #include "tnl_dd/t_dd_dmatmp2.h"
236 /**********************************************************************/
237 /* External entrypoints */
238 /**********************************************************************/
240 void r200EmitPrimitive( GLcontext
*ctx
,
245 tcl_render_tab_verts
[flags
&PRIM_MODE_MASK
]( ctx
, first
, last
, flags
);
248 void r200EmitEltPrimitive( GLcontext
*ctx
,
253 tcl_render_tab_elts
[flags
&PRIM_MODE_MASK
]( ctx
, first
, last
, flags
);
256 void r200TclPrimitive( GLcontext
*ctx
,
260 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
261 GLuint newprim
= hw_prim
| R200_VF_TCL_OUTPUT_VTX_ENABLE
;
263 if (newprim
!= rmesa
->tcl
.hw_primitive
||
264 !discrete_prim
[hw_prim
&0xf]) {
265 R200_NEWPRIM( rmesa
);
266 rmesa
->tcl
.hw_primitive
= newprim
;
271 /**********************************************************************/
272 /* Fog blend factor computation for hw tcl */
273 /* same calculation used as in t_vb_fog.c */
274 /**********************************************************************/
276 #define FOG_EXP_TABLE_SIZE 256
277 #define FOG_MAX (10.0)
278 #define EXP_FOG_MAX .0006595
279 #define FOG_INCR (FOG_MAX/FOG_EXP_TABLE_SIZE)
280 static GLfloat exp_table
[FOG_EXP_TABLE_SIZE
];
283 #define NEG_EXP( result, narg ) \
285 GLfloat f = (GLfloat) (narg * (1.0/FOG_INCR)); \
286 GLint k = (GLint) f; \
287 if (k > FOG_EXP_TABLE_SIZE-2) \
288 result = (GLfloat) EXP_FOG_MAX; \
290 result = exp_table[k] + (f-k)*(exp_table[k+1]-exp_table[k]); \
293 #define NEG_EXP( result, narg ) \
295 result = exp(-narg); \
301 * Initialize the exp_table[] lookup table for approximating exp().
304 r200InitStaticFogData( void )
308 for ( ; i
< FOG_EXP_TABLE_SIZE
; i
++, f
+= FOG_INCR
) {
309 exp_table
[i
] = (GLfloat
) exp(-f
);
315 * Compute per-vertex fog blend factors from fog coordinates by
316 * evaluating the GL_LINEAR, GL_EXP or GL_EXP2 fog function.
317 * Fog coordinates are distances from the eye (typically between the
318 * near and far clip plane distances).
319 * Note the fog (eye Z) coords may be negative so we use ABS(z) below.
320 * Fog blend factors are in the range [0,1].
323 r200ComputeFogBlendFactor( GLcontext
*ctx
, GLfloat fogcoord
)
325 GLfloat end
= ctx
->Fog
.End
;
327 const GLfloat z
= FABSF(fogcoord
);
329 switch (ctx
->Fog
.Mode
) {
331 if (ctx
->Fog
.Start
== ctx
->Fog
.End
)
334 d
= 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
335 temp
= (end
- z
) * d
;
336 return CLAMP(temp
, 0.0F
, 1.0F
);
339 d
= ctx
->Fog
.Density
;
340 NEG_EXP( temp
, d
* z
);
344 d
= ctx
->Fog
.Density
*ctx
->Fog
.Density
;
345 NEG_EXP( temp
, d
* z
* z
);
349 _mesa_problem(ctx
, "Bad fog mode in make_fog_coord");
355 /**********************************************************************/
356 /* Render pipeline stage */
357 /**********************************************************************/
362 static GLboolean
r200_run_tcl_render( GLcontext
*ctx
,
363 struct tnl_pipeline_stage
*stage
)
365 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
366 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
367 struct vertex_buffer
*VB
= &tnl
->vb
;
370 /* TODO: separate this from the swtnl pipeline
372 if (rmesa
->TclFallback
)
373 return GL_TRUE
; /* fallback to software t&l */
375 if (R200_DEBUG
& DEBUG_PRIMS
)
376 fprintf(stderr
, "%s\n", __FUNCTION__
);
381 r200ReleaseArrays( ctx
, ~0 /* stage->changed_inputs */ );
382 r200EmitArrays( ctx
, stage
->inputs
);
384 rmesa
->tcl
.Elts
= VB
->Elts
;
386 for (i
= 0 ; i
< VB
->PrimitiveCount
; i
++)
388 GLuint prim
= VB
->Primitive
[i
].mode
;
389 GLuint start
= VB
->Primitive
[i
].start
;
390 GLuint length
= VB
->Primitive
[i
].count
;
396 r200EmitEltPrimitive( ctx
, start
, start
+length
, prim
);
398 r200EmitPrimitive( ctx
, start
, start
+length
, prim
);
401 return GL_FALSE
; /* finished the pipe */
406 static void r200_check_tcl_render( GLcontext
*ctx
,
407 struct tnl_pipeline_stage
*stage
)
409 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
410 GLuint inputs
= VERT_BIT_POS
;
415 if (rmesa
->NewGLState
)
416 r200ValidateState( ctx
);
418 if (ctx
->RenderMode
== GL_RENDER
) {
419 /* Make all this event-driven:
421 if (ctx
->Light
.Enabled
) {
422 inputs
|= VERT_BIT_NORMAL
;
424 if (1 || ctx
->Light
.ColorMaterialEnabled
) {
425 inputs
|= VERT_BIT_COLOR0
;
429 inputs
|= VERT_BIT_COLOR0
;
431 if (ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
) {
432 inputs
|= VERT_BIT_COLOR1
;
436 if ( ctx
->Fog
.FogCoordinateSource
== GL_FOG_COORD
) {
437 inputs
|= VERT_BIT_FOG
;
440 for (unit
= 0 ; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
441 if (ctx
->Texture
.Unit
[unit
]._ReallyEnabled
) {
442 if (rmesa
->TexGenNeedNormals
[unit
]) {
443 inputs
|= VERT_BIT_NORMAL
;
445 inputs
|= VERT_BIT_TEX(unit
);
449 stage
->inputs
= inputs
;
456 static void r200_init_tcl_render( GLcontext
*ctx
,
457 struct tnl_pipeline_stage
*stage
)
459 stage
->check
= r200_check_tcl_render
;
460 stage
->check( ctx
, stage
);
463 static void dtr( struct tnl_pipeline_stage
*stage
)
469 /* Initial state for tcl stage.
471 const struct tnl_pipeline_stage _r200_tcl_stage
=
474 (_DD_NEW_SEPARATE_SPECULAR
|
478 _NEW_RENDERMODE
), /* re-check (new inputs) */
479 0, /* re-run (always runs) */
480 GL_TRUE
, /* active */
481 0, 0, /* inputs (set in check_render), outputs */
482 0, 0, /* changed_inputs, private */
483 dtr
, /* destructor */
484 r200_init_tcl_render
, /* check - initially set to alloc data */
485 r200_run_tcl_render
/* run */
490 /**********************************************************************/
491 /* Validate state at pipeline start */
492 /**********************************************************************/
495 /*-----------------------------------------------------------------------
496 * Manage TCL fallbacks
500 static void transition_to_swtnl( GLcontext
*ctx
)
502 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
503 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
505 R200_NEWPRIM( rmesa
);
507 r200ChooseVertexState( ctx
);
508 r200ChooseRenderState( ctx
);
510 _mesa_validate_all_lighting_tables( ctx
);
512 tnl
->Driver
.NotifyMaterialChange
=
513 _mesa_validate_all_lighting_tables
;
515 r200ReleaseArrays( ctx
, ~0 );
517 /* Still using the D3D based hardware-rasterizer from the radeon;
518 * need to put the card into D3D mode to make it work:
520 R200_STATECHANGE( rmesa
, vap
);
521 rmesa
->hw
.vap
.cmd
[VAP_SE_VAP_CNTL
] &= ~R200_VAP_TCL_ENABLE
;
524 static void transition_to_hwtnl( GLcontext
*ctx
)
526 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
527 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
529 _tnl_need_projected_coords( ctx
, GL_FALSE
);
531 r200UpdateMaterial( ctx
);
533 tnl
->Driver
.NotifyMaterialChange
= r200UpdateMaterial
;
535 if ( rmesa
->dma
.flush
)
536 rmesa
->dma
.flush( rmesa
);
538 rmesa
->dma
.flush
= 0;
540 if (rmesa
->swtcl
.indexed_verts
.buf
)
541 r200ReleaseDmaRegion( rmesa
, &rmesa
->swtcl
.indexed_verts
,
544 R200_STATECHANGE( rmesa
, vap
);
545 rmesa
->hw
.vap
.cmd
[VAP_SE_VAP_CNTL
] |= R200_VAP_TCL_ENABLE
;
546 rmesa
->hw
.vap
.cmd
[VAP_SE_VAP_CNTL
] &= ~R200_VAP_FORCE_W_TO_ONE
;
548 if ( ((rmesa
->hw
.ctx
.cmd
[CTX_PP_FOG_COLOR
] & R200_FOG_USE_MASK
)
549 == R200_FOG_USE_SPEC_ALPHA
) &&
550 (ctx
->Fog
.FogCoordinateSource
== GL_FOG_COORD
)) {
551 R200_STATECHANGE( rmesa
, ctx
);
552 rmesa
->hw
.ctx
.cmd
[CTX_PP_FOG_COLOR
] &= ~R200_FOG_USE_MASK
;
553 rmesa
->hw
.ctx
.cmd
[CTX_PP_FOG_COLOR
] |= R200_FOG_USE_VTX_FOG
;
556 R200_STATECHANGE( rmesa
, vte
);
557 rmesa
->hw
.vte
.cmd
[VTE_SE_VTE_CNTL
] &= ~(R200_VTX_XY_FMT
|R200_VTX_Z_FMT
);
559 if (R200_DEBUG
& DEBUG_FALLBACKS
)
560 fprintf(stderr
, "R200 end tcl fallback\n");
564 static char *fallbackStrings
[] = {
565 "Rasterization fallback",
566 "Unfilled triangles",
567 "Twosided lighting, differing materials",
568 "Materials in VB (maybe between begin/end)",
580 static char *getFallbackString(GLuint bit
)
587 return fallbackStrings
[i
];
592 void r200TclFallback( GLcontext
*ctx
, GLuint bit
, GLboolean mode
)
594 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
595 GLuint oldfallback
= rmesa
->TclFallback
;
598 rmesa
->TclFallback
|= bit
;
599 if (oldfallback
== 0) {
600 if (R200_DEBUG
& DEBUG_FALLBACKS
)
601 fprintf(stderr
, "R200 begin tcl fallback %s\n",
602 getFallbackString( bit
));
603 transition_to_swtnl( ctx
);
607 rmesa
->TclFallback
&= ~bit
;
608 if (oldfallback
== bit
) {
609 if (R200_DEBUG
& DEBUG_FALLBACKS
)
610 fprintf(stderr
, "R200 end tcl fallback %s\n",
611 getFallbackString( bit
));
612 transition_to_hwtnl( ctx
);