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>
35 #include "main/glheader.h"
36 #include "main/imports.h"
37 #include "main/mtypes.h"
38 #include "main/enums.h"
39 #include "main/colormac.h"
40 #include "main/light.h"
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 radeonEmitState(&rmesa->radeon); \
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 radeonEmitState(&rmesa->radeon); \
141 #define ALLOC_ELTS(nr) r200AllocElts( rmesa, nr )
143 static GLushort
*r200AllocElts( r200ContextPtr rmesa
, GLuint nr
)
145 if (rmesa
->radeon
.dma
.flush
== r200FlushElts
&&
146 rmesa
->tcl
.elt_used
+ nr
*2 < R200_ELT_BUF_SZ
) {
148 GLushort
*dest
= (GLushort
*)(rmesa
->radeon
.tcl
.elt_dma_bo
->ptr
+
149 rmesa
->tcl
.elt_used
);
151 rmesa
->tcl
.elt_used
+= nr
*2;
156 if (rmesa
->radeon
.dma
.flush
)
157 rmesa
->radeon
.dma
.flush( rmesa
->radeon
.glCtx
);
159 rcommonEnsureCmdBufSpace(&rmesa
->radeon
, AOS_BUFSZ(rmesa
->radeon
.tcl
.aos_count
), __FUNCTION__
);
162 rmesa
->radeon
.tcl
.aos_count
, 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 r200EmitPrim( GLcontext
*ctx
,
186 r200ContextPtr rmesa
= R200_CONTEXT( ctx
);
187 r200TclPrimitive( ctx
, prim
, hwprim
);
189 // fprintf(stderr,"Emit prim %d\n", rmesa->radeon.tcl.aos_count);
190 rcommonEnsureCmdBufSpace( &rmesa
->radeon
,
191 AOS_BUFSZ(rmesa
->radeon
.tcl
.aos_count
) +
192 rmesa
->radeon
.hw
.max_state_size
+ VBUF_BUFSZ
, __FUNCTION__
);
195 rmesa
->radeon
.tcl
.aos_count
,
198 /* Why couldn't this packet have taken an offset param?
200 r200EmitVbufPrim( rmesa
,
201 rmesa
->tcl
.hw_primitive
,
205 #define EMIT_PRIM(ctx, prim, hwprim, start, count) do { \
206 r200EmitPrim( ctx, prim, hwprim, start, count ); \
207 (void) rmesa; } while (0)
209 /* Try & join small primitives
212 #define PREFER_DISCRETE_ELT_PRIM( NR, PRIM ) 0
214 #define PREFER_DISCRETE_ELT_PRIM( NR, PRIM ) \
217 rmesa->tcl.hw_primitive == (PRIM| \
218 R200_VF_TCL_OUTPUT_VTX_ENABLE| \
219 R200_VF_PRIM_WALK_IND)))
222 #ifdef MESA_BIG_ENDIAN
223 /* We could do without (most of) this ugliness if dest was always 32 bit word aligned... */
224 #define EMIT_ELT(dest, offset, x) do { \
225 int off = offset + ( ( (GLuint)dest & 0x2 ) >> 1 ); \
226 GLushort *des = (GLushort *)( (GLuint)dest & ~0x2 ); \
227 (des)[ off + 1 - 2 * ( off & 1 ) ] = (GLushort)(x); \
228 (void)rmesa; } while (0)
230 #define EMIT_ELT(dest, offset, x) do { \
231 (dest)[offset] = (GLushort) (x); \
232 (void)rmesa; } while (0)
235 #define EMIT_TWO_ELTS(dest, offset, x, y) *(GLuint *)((dest)+offset) = ((y)<<16)|(x);
239 #define TAG(x) tcl_##x
240 #include "tnl_dd/t_dd_dmatmp2.h"
242 /**********************************************************************/
243 /* External entrypoints */
244 /**********************************************************************/
246 void r200EmitPrimitive( GLcontext
*ctx
,
251 tcl_render_tab_verts
[flags
&PRIM_MODE_MASK
]( ctx
, first
, last
, flags
);
254 void r200EmitEltPrimitive( GLcontext
*ctx
,
259 tcl_render_tab_elts
[flags
&PRIM_MODE_MASK
]( ctx
, first
, last
, flags
);
262 void r200TclPrimitive( GLcontext
*ctx
,
266 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
267 GLuint newprim
= hw_prim
| R200_VF_TCL_OUTPUT_VTX_ENABLE
;
269 if (newprim
!= rmesa
->tcl
.hw_primitive
||
270 !discrete_prim
[hw_prim
&0xf]) {
271 /* need to disable perspective-correct texturing for point sprites */
272 if ((prim
& PRIM_MODE_MASK
) == GL_POINTS
&& ctx
->Point
.PointSprite
) {
273 if (rmesa
->hw
.set
.cmd
[SET_RE_CNTL
] & R200_PERSPECTIVE_ENABLE
) {
274 R200_STATECHANGE( rmesa
, set
);
275 rmesa
->hw
.set
.cmd
[SET_RE_CNTL
] &= ~R200_PERSPECTIVE_ENABLE
;
278 else if (!(rmesa
->hw
.set
.cmd
[SET_RE_CNTL
] & R200_PERSPECTIVE_ENABLE
)) {
279 R200_STATECHANGE( rmesa
, set
);
280 rmesa
->hw
.set
.cmd
[SET_RE_CNTL
] |= R200_PERSPECTIVE_ENABLE
;
282 R200_NEWPRIM( rmesa
);
283 rmesa
->tcl
.hw_primitive
= newprim
;
288 /**********************************************************************/
289 /* Fog blend factor computation for hw tcl */
290 /* same calculation used as in t_vb_fog.c */
291 /**********************************************************************/
293 #define FOG_EXP_TABLE_SIZE 256
294 #define FOG_MAX (10.0)
295 #define EXP_FOG_MAX .0006595
296 #define FOG_INCR (FOG_MAX/FOG_EXP_TABLE_SIZE)
297 static GLfloat exp_table
[FOG_EXP_TABLE_SIZE
];
300 #define NEG_EXP( result, narg ) \
302 GLfloat f = (GLfloat) (narg * (1.0/FOG_INCR)); \
303 GLint k = (GLint) f; \
304 if (k > FOG_EXP_TABLE_SIZE-2) \
305 result = (GLfloat) EXP_FOG_MAX; \
307 result = exp_table[k] + (f-k)*(exp_table[k+1]-exp_table[k]); \
310 #define NEG_EXP( result, narg ) \
312 result = exp(-narg); \
318 * Initialize the exp_table[] lookup table for approximating exp().
321 r200InitStaticFogData( void )
325 for ( ; i
< FOG_EXP_TABLE_SIZE
; i
++, f
+= FOG_INCR
) {
326 exp_table
[i
] = (GLfloat
) exp(-f
);
332 * Compute per-vertex fog blend factors from fog coordinates by
333 * evaluating the GL_LINEAR, GL_EXP or GL_EXP2 fog function.
334 * Fog coordinates are distances from the eye (typically between the
335 * near and far clip plane distances).
336 * Note the fog (eye Z) coords may be negative so we use ABS(z) below.
337 * Fog blend factors are in the range [0,1].
340 r200ComputeFogBlendFactor( GLcontext
*ctx
, GLfloat fogcoord
)
342 GLfloat end
= ctx
->Fog
.End
;
344 const GLfloat z
= FABSF(fogcoord
);
346 switch (ctx
->Fog
.Mode
) {
348 if (ctx
->Fog
.Start
== ctx
->Fog
.End
)
351 d
= 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
352 temp
= (end
- z
) * d
;
353 return CLAMP(temp
, 0.0F
, 1.0F
);
356 d
= ctx
->Fog
.Density
;
357 NEG_EXP( temp
, d
* z
);
361 d
= ctx
->Fog
.Density
*ctx
->Fog
.Density
;
362 NEG_EXP( temp
, d
* z
* z
);
366 _mesa_problem(ctx
, "Bad fog mode in make_fog_coord");
372 /**********************************************************************/
373 /* Render pipeline stage */
374 /**********************************************************************/
379 static GLboolean
r200_run_tcl_render( GLcontext
*ctx
,
380 struct tnl_pipeline_stage
*stage
)
382 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
383 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
384 struct vertex_buffer
*VB
= &tnl
->vb
;
387 /* use hw fixed order for simplicity, pos 0, weight 1, normal 2, fog 3,
388 color0 - color3 4-7, texcoord0 - texcoord5 8-13, pos 1 14. Must not use
389 more than 12 of those at the same time. */
390 GLubyte map_rev_fixed
[15] = {255, 255, 255, 255, 255, 255, 255, 255,
391 255, 255, 255, 255, 255, 255, 255};
394 /* TODO: separate this from the swtnl pipeline
396 if (rmesa
->radeon
.TclFallback
)
397 return GL_TRUE
; /* fallback to software t&l */
399 if (R200_DEBUG
& DEBUG_PRIMS
)
400 fprintf(stderr
, "%s\n", __FUNCTION__
);
407 if (rmesa
->radeon
.NewGLState
)
408 if (!r200ValidateState( ctx
))
409 return GL_TRUE
; /* fallback to sw t&l */
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 radeonReleaseArrays( 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
= _tnl_translate_prim(&VB
->Primitive
[i
]);
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 radeonReleaseArrays( 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
->radeon
.dma
.flush
)
569 rmesa
->radeon
.dma
.flush( rmesa
->radeon
.glCtx
);
571 rmesa
->radeon
.dma
.flush
= NULL
;
573 R200_STATECHANGE( rmesa
, vap
);
574 rmesa
->hw
.vap
.cmd
[VAP_SE_VAP_CNTL
] |= R200_VAP_TCL_ENABLE
;
575 rmesa
->hw
.vap
.cmd
[VAP_SE_VAP_CNTL
] &= ~R200_VAP_FORCE_W_TO_ONE
;
577 if (ctx
->VertexProgram
._Enabled
) {
578 rmesa
->hw
.vap
.cmd
[VAP_SE_VAP_CNTL
] |= R200_VAP_PROG_VTX_SHADER_ENABLE
;
581 if ( ((rmesa
->hw
.ctx
.cmd
[CTX_PP_FOG_COLOR
] & R200_FOG_USE_MASK
)
582 == R200_FOG_USE_SPEC_ALPHA
) &&
583 (ctx
->Fog
.FogCoordinateSource
== GL_FOG_COORD
)) {
584 R200_STATECHANGE( rmesa
, ctx
);
585 rmesa
->hw
.ctx
.cmd
[CTX_PP_FOG_COLOR
] &= ~R200_FOG_USE_MASK
;
586 rmesa
->hw
.ctx
.cmd
[CTX_PP_FOG_COLOR
] |= R200_FOG_USE_VTX_FOG
;
589 R200_STATECHANGE( rmesa
, vte
);
590 rmesa
->hw
.vte
.cmd
[VTE_SE_VTE_CNTL
] &= ~(R200_VTX_XY_FMT
|R200_VTX_Z_FMT
);
591 rmesa
->hw
.vte
.cmd
[VTE_SE_VTE_CNTL
] |= R200_VTX_W0_FMT
;
593 if (R200_DEBUG
& DEBUG_FALLBACKS
)
594 fprintf(stderr
, "R200 end tcl fallback\n");
598 static char *fallbackStrings
[] = {
599 "Rasterization fallback",
600 "Unfilled triangles",
601 "Twosided lighting, differing materials",
602 "Materials in VB (maybe between begin/end)",
615 static char *getFallbackString(GLuint bit
)
622 return fallbackStrings
[i
];
627 void r200TclFallback( GLcontext
*ctx
, GLuint bit
, GLboolean mode
)
629 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
630 GLuint oldfallback
= rmesa
->radeon
.TclFallback
;
633 rmesa
->radeon
.TclFallback
|= bit
;
634 if (oldfallback
== 0) {
635 if (R200_DEBUG
& DEBUG_FALLBACKS
)
636 fprintf(stderr
, "R200 begin tcl fallback %s\n",
637 getFallbackString( bit
));
638 transition_to_swtnl( ctx
);
642 rmesa
->radeon
.TclFallback
&= ~bit
;
643 if (oldfallback
== bit
) {
644 if (R200_DEBUG
& DEBUG_FALLBACKS
)
645 fprintf(stderr
, "R200 end tcl fallback %s\n",
646 getFallbackString( bit
));
647 transition_to_hwtnl( ctx
);