2 /**************************************************************************
4 Copyright 2000, 2001 ATI Technologies Inc., Ontario, Canada, and
5 Tungsten Graphics Inc., Austin, Texas.
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>
42 #include "array_cache/acache.h"
44 #include "tnl/t_pipeline.h"
46 #include "radeon_context.h"
47 #include "radeon_state.h"
48 #include "radeon_ioctl.h"
49 #include "radeon_tex.h"
50 #include "radeon_tcl.h"
51 #include "radeon_swtcl.h"
52 #include "radeon_maos.h"
57 * Render unclipped vertex buffers by emitting vertices directly to
58 * dma buffers. Use strip/fan hardware primitives where possible.
59 * Try to simulate missing primitives with indexed vertices.
63 #define HAVE_LINE_LOOP 0
64 #define HAVE_LINE_STRIPS 1
65 #define HAVE_TRIANGLES 1
66 #define HAVE_TRI_STRIPS 1
67 #define HAVE_TRI_STRIP_1 0
68 #define HAVE_TRI_FANS 1
70 #define HAVE_QUAD_STRIPS 0
71 #define HAVE_POLYGONS 1
75 #define HW_POINTS RADEON_CP_VC_CNTL_PRIM_TYPE_POINT
76 #define HW_LINES RADEON_CP_VC_CNTL_PRIM_TYPE_LINE
77 #define HW_LINE_LOOP 0
78 #define HW_LINE_STRIP RADEON_CP_VC_CNTL_PRIM_TYPE_LINE_STRIP
79 #define HW_TRIANGLES RADEON_CP_VC_CNTL_PRIM_TYPE_TRI_LIST
80 #define HW_TRIANGLE_STRIP_0 RADEON_CP_VC_CNTL_PRIM_TYPE_TRI_STRIP
81 #define HW_TRIANGLE_STRIP_1 0
82 #define HW_TRIANGLE_FAN RADEON_CP_VC_CNTL_PRIM_TYPE_TRI_FAN
84 #define HW_QUAD_STRIP 0
85 #define HW_POLYGON RADEON_CP_VC_CNTL_PRIM_TYPE_TRI_FAN
88 static GLboolean discrete_prim
[0x10] = {
96 1, /* 7 rect list (unused) */
97 1, /* 8 3vert point */
108 #define LOCAL_VARS radeonContextPtr rmesa = RADEON_CONTEXT(ctx); (void)rmesa
109 #define ELT_TYPE GLushort
111 #define ELT_INIT(prim, hw_prim) \
112 radeonTclPrimitive( ctx, prim, hw_prim | RADEON_CP_VC_CNTL_PRIM_WALK_IND )
114 #define GET_MESA_ELTS() rmesa->tcl.Elts
117 /* Don't really know how many elts will fit in what's left of cmdbuf,
118 * as there is state to emit, etc:
121 /* Testing on isosurf shows a maximum around here. Don't know if it's
122 * the card or driver or kernel module that is causing the behaviour.
124 #define GET_MAX_HW_ELTS() 300
127 #define RESET_STIPPLE() do { \
128 RADEON_STATECHANGE( rmesa, lin ); \
129 radeonEmitState( rmesa ); \
132 #define AUTO_STIPPLE( mode ) do { \
133 RADEON_STATECHANGE( rmesa, lin ); \
135 rmesa->hw.lin.cmd[LIN_RE_LINE_PATTERN] |= \
136 RADEON_LINE_PATTERN_AUTO_RESET; \
138 rmesa->hw.lin.cmd[LIN_RE_LINE_PATTERN] &= \
139 ~RADEON_LINE_PATTERN_AUTO_RESET; \
140 radeonEmitState( rmesa ); \
145 #define ALLOC_ELTS(nr) radeonAllocElts( rmesa, nr )
147 static GLushort
*radeonAllocElts( radeonContextPtr rmesa
, GLuint nr
)
149 if (rmesa
->dma
.flush
)
150 rmesa
->dma
.flush( rmesa
);
152 radeonEnsureCmdBufSpace(rmesa
, AOS_BUFSZ(rmesa
->tcl
.nr_aos_components
) +
153 rmesa
->hw
.max_state_size
+ ELTS_BUFSZ(nr
));
155 radeonEmitAOS( rmesa
,
156 rmesa
->tcl
.aos_components
,
157 rmesa
->tcl
.nr_aos_components
, 0 );
159 return radeonAllocEltsOpenEnded( rmesa
,
160 rmesa
->tcl
.vertex_format
,
161 rmesa
->tcl
.hw_primitive
, nr
);
164 #define CLOSE_ELTS() RADEON_NEWPRIM( rmesa )
168 /* TODO: Try to extend existing primitive if both are identical,
169 * discrete and there are no intervening state changes. (Somewhat
170 * duplicates changes to DrawArrays code)
172 static void radeonEmitPrim( GLcontext
*ctx
,
178 radeonContextPtr rmesa
= RADEON_CONTEXT( ctx
);
179 radeonTclPrimitive( ctx
, prim
, hwprim
);
181 radeonEnsureCmdBufSpace( rmesa
, AOS_BUFSZ(rmesa
->tcl
.nr_aos_components
) +
182 rmesa
->hw
.max_state_size
+ VBUF_BUFSZ
);
184 radeonEmitAOS( rmesa
,
185 rmesa
->tcl
.aos_components
,
186 rmesa
->tcl
.nr_aos_components
,
189 /* Why couldn't this packet have taken an offset param?
191 radeonEmitVbufPrim( rmesa
,
192 rmesa
->tcl
.vertex_format
,
193 rmesa
->tcl
.hw_primitive
,
197 #define EMIT_PRIM( ctx, prim, hwprim, start, count ) do { \
198 radeonEmitPrim( ctx, prim, hwprim, start, count ); \
199 (void) rmesa; } while (0)
201 /* Try & join small primitives
204 #define PREFER_DISCRETE_ELT_PRIM( NR, PRIM ) 0
206 #define PREFER_DISCRETE_ELT_PRIM( NR, PRIM ) \
209 rmesa->tcl.hw_primitive == (PRIM| \
210 RADEON_CP_VC_CNTL_PRIM_WALK_IND| \
211 RADEON_CP_VC_CNTL_TCL_ENABLE)))
214 #ifdef MESA_BIG_ENDIAN
215 /* We could do without (most of) this ugliness if dest was always 32 bit word aligned... */
216 #define EMIT_ELT(dest, offset, x) do { \
217 int off = offset + ( ( (GLuint)dest & 0x2 ) >> 1 ); \
218 GLushort *des = (GLushort *)( (GLuint)dest & ~0x2 ); \
219 (des)[ off + 1 - 2 * ( off & 1 ) ] = (GLushort)(x); \
220 (void)rmesa; } while (0)
222 #define EMIT_ELT(dest, offset, x) do { \
223 (dest)[offset] = (GLushort) (x); \
224 (void)rmesa; } while (0)
227 #define EMIT_TWO_ELTS(dest, offset, x, y) *(GLuint *)(dest+offset) = ((y)<<16)|(x);
231 #define TAG(x) tcl_##x
232 #include "tnl_dd/t_dd_dmatmp2.h"
234 /**********************************************************************/
235 /* External entrypoints */
236 /**********************************************************************/
238 void radeonEmitPrimitive( GLcontext
*ctx
,
243 tcl_render_tab_verts
[flags
&PRIM_MODE_MASK
]( ctx
, first
, last
, flags
);
246 void radeonEmitEltPrimitive( GLcontext
*ctx
,
251 tcl_render_tab_elts
[flags
&PRIM_MODE_MASK
]( ctx
, first
, last
, flags
);
254 void radeonTclPrimitive( GLcontext
*ctx
,
258 radeonContextPtr rmesa
= RADEON_CONTEXT(ctx
);
260 GLuint newprim
= hw_prim
| RADEON_CP_VC_CNTL_TCL_ENABLE
;
262 if (newprim
!= rmesa
->tcl
.hw_primitive
||
263 !discrete_prim
[hw_prim
&0xf]) {
264 RADEON_NEWPRIM( rmesa
);
265 rmesa
->tcl
.hw_primitive
= newprim
;
268 se_cntl
= rmesa
->hw
.set
.cmd
[SET_SE_CNTL
];
269 se_cntl
&= ~RADEON_FLAT_SHADE_VTX_LAST
;
271 if (prim
== GL_POLYGON
&& (ctx
->_TriangleCaps
& DD_FLATSHADE
))
272 se_cntl
|= RADEON_FLAT_SHADE_VTX_0
;
274 se_cntl
|= RADEON_FLAT_SHADE_VTX_LAST
;
276 if (se_cntl
!= rmesa
->hw
.set
.cmd
[SET_SE_CNTL
]) {
277 RADEON_STATECHANGE( rmesa
, set
);
278 rmesa
->hw
.set
.cmd
[SET_SE_CNTL
] = se_cntl
;
283 /**********************************************************************/
284 /* Render pipeline stage */
285 /**********************************************************************/
290 static GLboolean
radeon_run_tcl_render( GLcontext
*ctx
,
291 struct tnl_pipeline_stage
*stage
)
293 radeonContextPtr rmesa
= RADEON_CONTEXT(ctx
);
294 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
295 struct vertex_buffer
*VB
= &tnl
->vb
;
298 /* TODO: separate this from the swtnl pipeline
300 if (rmesa
->TclFallback
)
301 return GL_TRUE
; /* fallback to software t&l */
306 radeonReleaseArrays( ctx
, stage
->changed_inputs
);
307 radeonEmitArrays( ctx
, stage
->inputs
);
309 rmesa
->tcl
.Elts
= VB
->Elts
;
311 for (i
= 0 ; i
< VB
->PrimitiveCount
; i
++)
313 GLuint prim
= VB
->Primitive
[i
].mode
;
314 GLuint start
= VB
->Primitive
[i
].start
;
315 GLuint length
= VB
->Primitive
[i
].count
;
321 radeonEmitEltPrimitive( ctx
, start
, start
+length
, prim
);
323 radeonEmitPrimitive( ctx
, start
, start
+length
, prim
);
326 return GL_FALSE
; /* finished the pipe */
331 static void radeon_check_tcl_render( GLcontext
*ctx
,
332 struct tnl_pipeline_stage
*stage
)
334 radeonContextPtr rmesa
= RADEON_CONTEXT(ctx
);
335 GLuint inputs
= VERT_BIT_POS
;
337 if (ctx
->RenderMode
== GL_RENDER
) {
338 /* Make all this event-driven:
340 if (ctx
->Light
.Enabled
) {
341 inputs
|= VERT_BIT_NORMAL
;
343 if (1 || ctx
->Light
.ColorMaterialEnabled
) {
344 inputs
|= VERT_BIT_COLOR0
;
348 inputs
|= VERT_BIT_COLOR0
;
350 if (ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
) {
351 inputs
|= VERT_BIT_COLOR1
;
355 if (ctx
->Texture
.Unit
[0]._ReallyEnabled
) {
356 if (ctx
->Texture
.Unit
[0].TexGenEnabled
) {
357 if (rmesa
->TexGenNeedNormals
[0]) {
358 inputs
|= VERT_BIT_NORMAL
;
361 inputs
|= VERT_BIT_TEX0
;
365 if (ctx
->Texture
.Unit
[1]._ReallyEnabled
) {
366 if (ctx
->Texture
.Unit
[1].TexGenEnabled
) {
367 if (rmesa
->TexGenNeedNormals
[1]) {
368 inputs
|= VERT_BIT_NORMAL
;
371 inputs
|= VERT_BIT_TEX1
;
375 stage
->inputs
= inputs
;
382 static void radeon_init_tcl_render( GLcontext
*ctx
,
383 struct tnl_pipeline_stage
*stage
)
385 stage
->check
= radeon_check_tcl_render
;
386 stage
->check( ctx
, stage
);
389 static void dtr( struct tnl_pipeline_stage
*stage
)
395 /* Initial state for tcl stage.
397 const struct tnl_pipeline_stage _radeon_tcl_stage
=
400 (_DD_NEW_SEPARATE_SPECULAR
|
404 _NEW_RENDERMODE
), /* re-check (new inputs) */
405 0, /* re-run (always runs) */
406 GL_TRUE
, /* active */
407 0, 0, /* inputs (set in check_render), outputs */
408 0, 0, /* changed_inputs, private */
409 dtr
, /* destructor */
410 radeon_init_tcl_render
, /* check - initially set to alloc data */
411 radeon_run_tcl_render
/* run */
416 /**********************************************************************/
417 /* Validate state at pipeline start */
418 /**********************************************************************/
421 /*-----------------------------------------------------------------------
422 * Manage TCL fallbacks
426 static void transition_to_swtnl( GLcontext
*ctx
)
428 radeonContextPtr rmesa
= RADEON_CONTEXT(ctx
);
429 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
432 RADEON_NEWPRIM( rmesa
);
433 rmesa
->swtcl
.vertex_format
= 0;
435 radeonChooseVertexState( ctx
);
436 radeonChooseRenderState( ctx
);
438 _mesa_validate_all_lighting_tables( ctx
);
440 tnl
->Driver
.NotifyMaterialChange
=
441 _mesa_validate_all_lighting_tables
;
443 radeonReleaseArrays( ctx
, ~0 );
445 se_cntl
= rmesa
->hw
.set
.cmd
[SET_SE_CNTL
];
446 se_cntl
|= RADEON_FLAT_SHADE_VTX_LAST
;
448 if (se_cntl
!= rmesa
->hw
.set
.cmd
[SET_SE_CNTL
]) {
449 RADEON_STATECHANGE( rmesa
, set
);
450 rmesa
->hw
.set
.cmd
[SET_SE_CNTL
] = se_cntl
;
455 static void transition_to_hwtnl( GLcontext
*ctx
)
457 radeonContextPtr rmesa
= RADEON_CONTEXT(ctx
);
458 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
459 GLuint se_coord_fmt
= (RADEON_VTX_W0_IS_NOT_1_OVER_W0
|
460 RADEON_TEX1_W_ROUTING_USE_Q1
);
462 if ( se_coord_fmt
!= rmesa
->hw
.set
.cmd
[SET_SE_COORDFMT
] ) {
463 RADEON_STATECHANGE( rmesa
, set
);
464 rmesa
->hw
.set
.cmd
[SET_SE_COORDFMT
] = se_coord_fmt
;
465 _tnl_need_projected_coords( ctx
, GL_FALSE
);
468 radeonUpdateMaterial( ctx
);
470 tnl
->Driver
.NotifyMaterialChange
= radeonUpdateMaterial
;
472 if ( rmesa
->dma
.flush
)
473 rmesa
->dma
.flush( rmesa
);
475 rmesa
->dma
.flush
= 0;
476 rmesa
->swtcl
.vertex_format
= 0;
478 if (rmesa
->swtcl
.indexed_verts
.buf
)
479 radeonReleaseDmaRegion( rmesa
, &rmesa
->swtcl
.indexed_verts
,
482 if (RADEON_DEBUG
& DEBUG_FALLBACKS
)
483 fprintf(stderr
, "Radeon end tcl fallback\n");
486 static char *fallbackStrings
[] = {
487 "Rasterization fallback",
488 "Unfilled triangles",
489 "Twosided lighting, differing materials",
490 "Materials in VB (maybe between begin/end)",
498 static char *getFallbackString(GLuint bit
)
505 return fallbackStrings
[i
];
510 void radeonTclFallback( GLcontext
*ctx
, GLuint bit
, GLboolean mode
)
512 radeonContextPtr rmesa
= RADEON_CONTEXT(ctx
);
513 GLuint oldfallback
= rmesa
->TclFallback
;
516 rmesa
->TclFallback
|= bit
;
517 if (oldfallback
== 0) {
518 if (RADEON_DEBUG
& DEBUG_FALLBACKS
)
519 fprintf(stderr
, "Radeon begin tcl fallback %s\n",
520 getFallbackString( bit
));
521 transition_to_swtnl( ctx
);
525 rmesa
->TclFallback
&= ~bit
;
526 if (oldfallback
== bit
) {
527 if (RADEON_DEBUG
& DEBUG_FALLBACKS
)
528 fprintf(stderr
, "Radeon end tcl fallback %s\n",
529 getFallbackString( bit
));
530 transition_to_hwtnl( ctx
);