2 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
4 The Weather Channel (TM) funded Tungsten Graphics to develop the
5 initial release of the Radeon 8500 driver under the XFree86 license.
6 This notice must be preserved.
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
16 The above copyright notice and this permission notice (including the
17 next paragraph) shall be included in all copies or substantial
18 portions of the Software.
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
24 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 **************************************************************************/
32 * Keith Whitwell <keith@tungstengraphics.com>
44 #include "tnl/t_pipeline.h"
46 #include "r200_context.h"
47 #include "r200_state.h"
48 #include "r200_ioctl.h"
51 #include "r200_swtcl.h"
52 #include "r200_maos.h"
58 #define HAVE_LINE_LOOP 0
59 #define HAVE_LINE_STRIPS 1
60 #define HAVE_TRIANGLES 1
61 #define HAVE_TRI_STRIPS 1
62 #define HAVE_TRI_STRIP_1 0
63 #define HAVE_TRI_FANS 1
65 #define HAVE_QUAD_STRIPS 1
66 #define HAVE_POLYGONS 1
70 #define HW_POINTS ((ctx->Point.PointSprite || \
71 ((ctx->_TriangleCaps & (DD_POINT_SIZE | DD_POINT_ATTEN)) && \
72 !(ctx->_TriangleCaps & (DD_POINT_SMOOTH)))) ? \
73 R200_VF_PRIM_POINT_SPRITES : R200_VF_PRIM_POINTS)
74 #define HW_LINES R200_VF_PRIM_LINES
75 #define HW_LINE_LOOP 0
76 #define HW_LINE_STRIP R200_VF_PRIM_LINE_STRIP
77 #define HW_TRIANGLES R200_VF_PRIM_TRIANGLES
78 #define HW_TRIANGLE_STRIP_0 R200_VF_PRIM_TRIANGLE_STRIP
79 #define HW_TRIANGLE_STRIP_1 0
80 #define HW_TRIANGLE_FAN R200_VF_PRIM_TRIANGLE_FAN
81 #define HW_QUADS R200_VF_PRIM_QUADS
82 #define HW_QUAD_STRIP R200_VF_PRIM_QUAD_STRIP
83 #define HW_POLYGON R200_VF_PRIM_POLYGON
86 static GLboolean discrete_prim
[0x10] = {
94 0, /* 7 tri_w_flags */
95 1, /* 8 rect list (unused) */
96 1, /* 9 3vert point */
98 0, /* b point sprite */
101 0, /* e quad strip */
106 #define LOCAL_VARS r200ContextPtr rmesa = R200_CONTEXT(ctx)
107 #define ELT_TYPE GLushort
109 #define ELT_INIT(prim, hw_prim) \
110 r200TclPrimitive( ctx, prim, hw_prim | R200_VF_PRIM_WALK_IND )
112 #define GET_MESA_ELTS() rmesa->tcl.Elts
115 /* Don't really know how many elts will fit in what's left of cmdbuf,
116 * as there is state to emit, etc:
119 /* Testing on isosurf shows a maximum around here. Don't know if it's
120 * the card or driver or kernel module that is causing the behaviour.
122 #define GET_MAX_HW_ELTS() 300
124 #define RESET_STIPPLE() do { \
125 R200_STATECHANGE( rmesa, lin ); \
126 r200EmitState( rmesa ); \
129 #define AUTO_STIPPLE( mode ) do { \
130 R200_STATECHANGE( rmesa, lin ); \
132 rmesa->hw.lin.cmd[LIN_RE_LINE_PATTERN] |= \
133 R200_LINE_PATTERN_AUTO_RESET; \
135 rmesa->hw.lin.cmd[LIN_RE_LINE_PATTERN] &= \
136 ~R200_LINE_PATTERN_AUTO_RESET; \
137 r200EmitState( rmesa ); \
141 #define ALLOC_ELTS(nr) r200AllocElts( rmesa, nr )
143 static GLushort
*r200AllocElts( r200ContextPtr rmesa
, GLuint nr
)
145 if (rmesa
->dma
.flush
== r200FlushElts
&&
146 rmesa
->store
.cmd_used
+ nr
*2 < R200_CMD_BUF_SZ
) {
148 GLushort
*dest
= (GLushort
*)(rmesa
->store
.cmd_buf
+
149 rmesa
->store
.cmd_used
);
151 rmesa
->store
.cmd_used
+= nr
*2;
156 if (rmesa
->dma
.flush
)
157 rmesa
->dma
.flush( rmesa
);
159 r200EnsureCmdBufSpace( rmesa
, AOS_BUFSZ(rmesa
->tcl
.nr_aos_components
) +
160 rmesa
->hw
.max_state_size
+ ELTS_BUFSZ(nr
) );
163 rmesa
->tcl
.aos_components
,
164 rmesa
->tcl
.nr_aos_components
, 0 );
166 return r200AllocEltsOpenEnded( rmesa
, rmesa
->tcl
.hw_primitive
, nr
);
171 #define CLOSE_ELTS() \
173 if (0) R200_NEWPRIM( rmesa ); \
178 /* TODO: Try to extend existing primitive if both are identical,
179 * discrete and there are no intervening state changes. (Somewhat
180 * duplicates changes to DrawArrays code)
182 static void r200EmitPrim( GLcontext
*ctx
,
188 r200ContextPtr rmesa
= R200_CONTEXT( ctx
);
189 r200TclPrimitive( ctx
, prim
, hwprim
);
191 r200EnsureCmdBufSpace( rmesa
, AOS_BUFSZ(rmesa
->tcl
.nr_aos_components
) +
192 rmesa
->hw
.max_state_size
+ VBUF_BUFSZ
);
195 rmesa
->tcl
.aos_components
,
196 rmesa
->tcl
.nr_aos_components
,
199 /* Why couldn't this packet have taken an offset param?
201 r200EmitVbufPrim( rmesa
,
202 rmesa
->tcl
.hw_primitive
,
206 #define EMIT_PRIM(ctx, prim, hwprim, start, count) do { \
207 r200EmitPrim( ctx, prim, hwprim, start, count ); \
208 (void) rmesa; } while (0)
210 /* Try & join small primitives
213 #define PREFER_DISCRETE_ELT_PRIM( NR, PRIM ) 0
215 #define PREFER_DISCRETE_ELT_PRIM( NR, PRIM ) \
218 rmesa->tcl.hw_primitive == (PRIM| \
219 R200_VF_TCL_OUTPUT_VTX_ENABLE| \
220 R200_VF_PRIM_WALK_IND)))
223 #ifdef MESA_BIG_ENDIAN
224 /* We could do without (most of) this ugliness if dest was always 32 bit word aligned... */
225 #define EMIT_ELT(dest, offset, x) do { \
226 int off = offset + ( ( (GLuint)dest & 0x2 ) >> 1 ); \
227 GLushort *des = (GLushort *)( (GLuint)dest & ~0x2 ); \
228 (des)[ off + 1 - 2 * ( off & 1 ) ] = (GLushort)(x); \
229 (void)rmesa; } while (0)
231 #define EMIT_ELT(dest, offset, x) do { \
232 (dest)[offset] = (GLushort) (x); \
233 (void)rmesa; } while (0)
236 #define EMIT_TWO_ELTS(dest, offset, x, y) *(GLuint *)((dest)+offset) = ((y)<<16)|(x);
240 #define TAG(x) tcl_##x
241 #include "tnl_dd/t_dd_dmatmp2.h"
243 /**********************************************************************/
244 /* External entrypoints */
245 /**********************************************************************/
247 void r200EmitPrimitive( GLcontext
*ctx
,
252 tcl_render_tab_verts
[flags
&PRIM_MODE_MASK
]( ctx
, first
, last
, flags
);
255 void r200EmitEltPrimitive( GLcontext
*ctx
,
260 tcl_render_tab_elts
[flags
&PRIM_MODE_MASK
]( ctx
, first
, last
, flags
);
263 void r200TclPrimitive( GLcontext
*ctx
,
267 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
268 GLuint newprim
= hw_prim
| R200_VF_TCL_OUTPUT_VTX_ENABLE
;
270 if (newprim
!= rmesa
->tcl
.hw_primitive
||
271 !discrete_prim
[hw_prim
&0xf]) {
272 /* need to disable perspective-correct texturing for point sprites */
273 if ((prim
& PRIM_MODE_MASK
) == GL_POINTS
&& ctx
->Point
.PointSprite
) {
274 if (rmesa
->hw
.set
.cmd
[SET_RE_CNTL
] & R200_PERSPECTIVE_ENABLE
) {
275 R200_STATECHANGE( rmesa
, set
);
276 rmesa
->hw
.set
.cmd
[SET_RE_CNTL
] &= ~R200_PERSPECTIVE_ENABLE
;
279 else if (!(rmesa
->hw
.set
.cmd
[SET_RE_CNTL
] & R200_PERSPECTIVE_ENABLE
)) {
280 R200_STATECHANGE( rmesa
, set
);
281 rmesa
->hw
.set
.cmd
[SET_RE_CNTL
] |= R200_PERSPECTIVE_ENABLE
;
283 R200_NEWPRIM( rmesa
);
284 rmesa
->tcl
.hw_primitive
= newprim
;
289 /**********************************************************************/
290 /* Fog blend factor computation for hw tcl */
291 /* same calculation used as in t_vb_fog.c */
292 /**********************************************************************/
294 #define FOG_EXP_TABLE_SIZE 256
295 #define FOG_MAX (10.0)
296 #define EXP_FOG_MAX .0006595
297 #define FOG_INCR (FOG_MAX/FOG_EXP_TABLE_SIZE)
298 static GLfloat exp_table
[FOG_EXP_TABLE_SIZE
];
301 #define NEG_EXP( result, narg ) \
303 GLfloat f = (GLfloat) (narg * (1.0/FOG_INCR)); \
304 GLint k = (GLint) f; \
305 if (k > FOG_EXP_TABLE_SIZE-2) \
306 result = (GLfloat) EXP_FOG_MAX; \
308 result = exp_table[k] + (f-k)*(exp_table[k+1]-exp_table[k]); \
311 #define NEG_EXP( result, narg ) \
313 result = exp(-narg); \
319 * Initialize the exp_table[] lookup table for approximating exp().
322 r200InitStaticFogData( void )
326 for ( ; i
< FOG_EXP_TABLE_SIZE
; i
++, f
+= FOG_INCR
) {
327 exp_table
[i
] = (GLfloat
) exp(-f
);
333 * Compute per-vertex fog blend factors from fog coordinates by
334 * evaluating the GL_LINEAR, GL_EXP or GL_EXP2 fog function.
335 * Fog coordinates are distances from the eye (typically between the
336 * near and far clip plane distances).
337 * Note the fog (eye Z) coords may be negative so we use ABS(z) below.
338 * Fog blend factors are in the range [0,1].
341 r200ComputeFogBlendFactor( GLcontext
*ctx
, GLfloat fogcoord
)
343 GLfloat end
= ctx
->Fog
.End
;
345 const GLfloat z
= FABSF(fogcoord
);
347 switch (ctx
->Fog
.Mode
) {
349 if (ctx
->Fog
.Start
== ctx
->Fog
.End
)
352 d
= 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
353 temp
= (end
- z
) * d
;
354 return CLAMP(temp
, 0.0F
, 1.0F
);
357 d
= ctx
->Fog
.Density
;
358 NEG_EXP( temp
, d
* z
);
362 d
= ctx
->Fog
.Density
*ctx
->Fog
.Density
;
363 NEG_EXP( temp
, d
* z
* z
);
367 _mesa_problem(ctx
, "Bad fog mode in make_fog_coord");
373 /**********************************************************************/
374 /* Render pipeline stage */
375 /**********************************************************************/
380 static GLboolean
r200_run_tcl_render( GLcontext
*ctx
,
381 struct tnl_pipeline_stage
*stage
)
383 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
384 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
385 struct vertex_buffer
*VB
= &tnl
->vb
;
388 /* use hw fixed order for simplicity, pos 0, weight 1, normal 2, fog 3,
389 color0 - color3 4-7, texcoord0 - texcoord5 8-13, pos 1 14. Must not use
390 more than 12 of those at the same time. */
391 GLubyte map_rev_fixed
[15] = {255, 255, 255, 255, 255, 255, 255, 255,
392 255, 255, 255, 255, 255, 255, 255};
395 /* TODO: separate this from the swtnl pipeline
397 if (rmesa
->TclFallback
)
398 return GL_TRUE
; /* fallback to software t&l */
400 if (R200_DEBUG
& DEBUG_PRIMS
)
401 fprintf(stderr
, "%s\n", __FUNCTION__
);
408 if (rmesa
->NewGLState
)
409 r200ValidateState( ctx
);
411 if (!ctx
->VertexProgram
._Enabled
) {
412 /* NOTE: inputs != tnl->render_inputs - these are the untransformed
415 map_rev_fixed
[0] = VERT_ATTRIB_POS
;
416 /* technically there is no reason we always need VA_COLOR0. In theory
417 could disable it depending on lighting, color materials, texturing... */
418 map_rev_fixed
[4] = VERT_ATTRIB_COLOR0
;
420 if (ctx
->Light
.Enabled
) {
421 map_rev_fixed
[2] = VERT_ATTRIB_NORMAL
;
424 /* this also enables VA_COLOR1 when using separate specular
425 lighting model, which is unnecessary.
426 FIXME: OTOH, we're missing the case where a ATI_fragment_shader accesses
427 the secondary color (if lighting is disabled). The chip seems
428 misconfigured for that though elsewhere (tcl output, might lock up) */
429 if (ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
) {
430 map_rev_fixed
[5] = VERT_ATTRIB_COLOR1
;
433 if ( (ctx
->Fog
.FogCoordinateSource
== GL_FOG_COORD
) && ctx
->Fog
.Enabled
) {
434 map_rev_fixed
[3] = VERT_ATTRIB_FOG
;
437 for (i
= 0 ; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
438 if (ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
439 if (rmesa
->TexGenNeedNormals
[i
]) {
440 map_rev_fixed
[2] = VERT_ATTRIB_NORMAL
;
442 map_rev_fixed
[8 + i
] = VERT_ATTRIB_TEX0
+ i
;
445 vimap_rev
= &map_rev_fixed
[0];
448 /* vtx_tcl_output_vtxfmt_0/1 need to match configuration of "fragment
449 part", since using some vertex interpolator later which is not in
450 out_vtxfmt0/1 will lock up. It seems to be ok to write in vertex
451 prog to a not enabled output however, so just don't mess with it.
452 We only need to change compsel. */
453 GLuint out_compsel
= 0;
454 GLuint vp_out
= rmesa
->curr_vp_hw
->mesa_program
.Base
.OutputsWritten
;
456 vimap_rev
= &rmesa
->curr_vp_hw
->inputmap_rev
[0];
457 assert(vp_out
& (1 << VERT_RESULT_HPOS
));
458 out_compsel
= R200_OUTPUT_XYZW
;
459 if (vp_out
& (1 << VERT_RESULT_COL0
)) {
460 out_compsel
|= R200_OUTPUT_COLOR_0
;
462 if (vp_out
& (1 << VERT_RESULT_COL1
)) {
463 out_compsel
|= R200_OUTPUT_COLOR_1
;
465 if (vp_out
& (1 << VERT_RESULT_FOGC
)) {
466 out_compsel
|= R200_OUTPUT_DISCRETE_FOG
;
468 if (vp_out
& (1 << VERT_RESULT_PSIZ
)) {
469 out_compsel
|= R200_OUTPUT_PT_SIZE
;
471 for (i
= VERT_RESULT_TEX0
; i
< VERT_RESULT_TEX6
; i
++) {
472 if (vp_out
& (1 << i
)) {
473 out_compsel
|= R200_OUTPUT_TEX_0
<< (i
- VERT_RESULT_TEX0
);
476 if (rmesa
->hw
.vtx
.cmd
[VTX_TCL_OUTPUT_COMPSEL
] != out_compsel
) {
477 R200_STATECHANGE( rmesa
, vtx
);
478 rmesa
->hw
.vtx
.cmd
[VTX_TCL_OUTPUT_COMPSEL
] = out_compsel
;
482 /* Do the actual work:
484 r200ReleaseArrays( ctx
, ~0 /* stage->changed_inputs */ );
485 r200EmitArrays( ctx
, vimap_rev
);
487 rmesa
->tcl
.Elts
= VB
->Elts
;
489 for (i
= 0 ; i
< VB
->PrimitiveCount
; i
++)
491 GLuint prim
= VB
->Primitive
[i
].mode
;
492 GLuint start
= VB
->Primitive
[i
].start
;
493 GLuint length
= VB
->Primitive
[i
].count
;
499 r200EmitEltPrimitive( ctx
, start
, start
+length
, prim
);
501 r200EmitPrimitive( ctx
, start
, start
+length
, prim
);
504 return GL_FALSE
; /* finished the pipe */
509 /* Initial state for tcl stage.
511 const struct tnl_pipeline_stage _r200_tcl_stage
=
518 r200_run_tcl_render
/* run */
523 /**********************************************************************/
524 /* Validate state at pipeline start */
525 /**********************************************************************/
528 /*-----------------------------------------------------------------------
529 * Manage TCL fallbacks
533 static void transition_to_swtnl( GLcontext
*ctx
)
535 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
536 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
538 R200_NEWPRIM( rmesa
);
540 r200ChooseVertexState( ctx
);
541 r200ChooseRenderState( ctx
);
543 _mesa_validate_all_lighting_tables( ctx
);
545 tnl
->Driver
.NotifyMaterialChange
=
546 _mesa_validate_all_lighting_tables
;
548 r200ReleaseArrays( ctx
, ~0 );
550 /* Still using the D3D based hardware-rasterizer from the radeon;
551 * need to put the card into D3D mode to make it work:
553 R200_STATECHANGE( rmesa
, vap
);
554 rmesa
->hw
.vap
.cmd
[VAP_SE_VAP_CNTL
] &= ~(R200_VAP_TCL_ENABLE
|R200_VAP_PROG_VTX_SHADER_ENABLE
);
557 static void transition_to_hwtnl( GLcontext
*ctx
)
559 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
560 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
562 _tnl_need_projected_coords( ctx
, GL_FALSE
);
564 r200UpdateMaterial( ctx
);
566 tnl
->Driver
.NotifyMaterialChange
= r200UpdateMaterial
;
568 if ( rmesa
->dma
.flush
)
569 rmesa
->dma
.flush( rmesa
);
571 rmesa
->dma
.flush
= NULL
;
573 if (rmesa
->swtcl
.indexed_verts
.buf
)
574 r200ReleaseDmaRegion( rmesa
, &rmesa
->swtcl
.indexed_verts
,
577 R200_STATECHANGE( rmesa
, vap
);
578 rmesa
->hw
.vap
.cmd
[VAP_SE_VAP_CNTL
] |= R200_VAP_TCL_ENABLE
;
579 rmesa
->hw
.vap
.cmd
[VAP_SE_VAP_CNTL
] &= ~R200_VAP_FORCE_W_TO_ONE
;
581 if (ctx
->VertexProgram
._Enabled
) {
582 rmesa
->hw
.vap
.cmd
[VAP_SE_VAP_CNTL
] |= R200_VAP_PROG_VTX_SHADER_ENABLE
;
585 if ( ((rmesa
->hw
.ctx
.cmd
[CTX_PP_FOG_COLOR
] & R200_FOG_USE_MASK
)
586 == R200_FOG_USE_SPEC_ALPHA
) &&
587 (ctx
->Fog
.FogCoordinateSource
== GL_FOG_COORD
)) {
588 R200_STATECHANGE( rmesa
, ctx
);
589 rmesa
->hw
.ctx
.cmd
[CTX_PP_FOG_COLOR
] &= ~R200_FOG_USE_MASK
;
590 rmesa
->hw
.ctx
.cmd
[CTX_PP_FOG_COLOR
] |= R200_FOG_USE_VTX_FOG
;
593 R200_STATECHANGE( rmesa
, vte
);
594 rmesa
->hw
.vte
.cmd
[VTE_SE_VTE_CNTL
] &= ~(R200_VTX_XY_FMT
|R200_VTX_Z_FMT
);
595 rmesa
->hw
.vte
.cmd
[VTE_SE_VTE_CNTL
] |= R200_VTX_W0_FMT
;
597 if (R200_DEBUG
& DEBUG_FALLBACKS
)
598 fprintf(stderr
, "R200 end tcl fallback\n");
602 static char *fallbackStrings
[] = {
603 "Rasterization fallback",
604 "Unfilled triangles",
605 "Twosided lighting, differing materials",
606 "Materials in VB (maybe between begin/end)",
619 static char *getFallbackString(GLuint bit
)
626 return fallbackStrings
[i
];
631 void r200TclFallback( GLcontext
*ctx
, GLuint bit
, GLboolean mode
)
633 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
634 GLuint oldfallback
= rmesa
->TclFallback
;
637 rmesa
->TclFallback
|= bit
;
638 if (oldfallback
== 0) {
639 if (R200_DEBUG
& DEBUG_FALLBACKS
)
640 fprintf(stderr
, "R200 begin tcl fallback %s\n",
641 getFallbackString( bit
));
642 transition_to_swtnl( ctx
);
646 rmesa
->TclFallback
&= ~bit
;
647 if (oldfallback
== bit
) {
648 if (R200_DEBUG
& DEBUG_FALLBACKS
)
649 fprintf(stderr
, "R200 end tcl fallback %s\n",
650 getFallbackString( bit
));
651 transition_to_hwtnl( ctx
);