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)
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 radeonEmitAOS( rmesa
,
153 rmesa
->tcl
.aos_components
,
154 rmesa
->tcl
.nr_aos_components
, 0 );
156 return radeonAllocEltsOpenEnded( rmesa
,
157 rmesa
->tcl
.vertex_format
,
158 rmesa
->tcl
.hw_primitive
, nr
);
161 #define CLOSE_ELTS() RADEON_NEWPRIM( rmesa )
165 /* TODO: Try to extend existing primitive if both are identical,
166 * discrete and there are no intervening state changes. (Somewhat
167 * duplicates changes to DrawArrays code)
169 static void EMIT_PRIM( GLcontext
*ctx
,
175 radeonContextPtr rmesa
= RADEON_CONTEXT( ctx
);
176 radeonTclPrimitive( ctx
, prim
, hwprim
);
178 radeonEmitAOS( rmesa
,
179 rmesa
->tcl
.aos_components
,
180 rmesa
->tcl
.nr_aos_components
,
183 /* Why couldn't this packet have taken an offset param?
185 radeonEmitVbufPrim( rmesa
,
186 rmesa
->tcl
.vertex_format
,
187 rmesa
->tcl
.hw_primitive
,
193 /* Try & join small primitives
196 #define PREFER_DISCRETE_ELT_PRIM( NR, PRIM ) 0
198 #define PREFER_DISCRETE_ELT_PRIM( NR, PRIM ) \
201 rmesa->tcl.hw_primitive == (PRIM| \
202 RADEON_CP_VC_CNTL_PRIM_WALK_IND| \
203 RADEON_CP_VC_CNTL_TCL_ENABLE)))
206 #ifdef MESA_BIG_ENDIAN
207 /* We could do without (most of) this ugliness if dest was always 32 bit word aligned... */
208 #define EMIT_ELT(dest, offset, x) do { \
209 int off = offset + ( ( (GLuint)dest & 0x2 ) >> 1 ); \
210 GLushort *des = (GLushort *)( (GLuint)dest & ~0x2 ); \
211 (des)[ off + 1 - 2 * ( off & 1 ) ] = (GLushort)(x); } while (0)
213 #define EMIT_ELT(dest, offset, x) (dest)[offset] = (GLushort) (x)
216 #define EMIT_TWO_ELTS(dest, offset, x, y) *(GLuint *)(dest+offset) = ((y)<<16)|(x);
220 #define TAG(x) tcl_##x
221 #include "tnl_dd/t_dd_dmatmp2.h"
223 /**********************************************************************/
224 /* External entrypoints */
225 /**********************************************************************/
227 void radeonEmitPrimitive( GLcontext
*ctx
,
232 tcl_render_tab_verts
[flags
&PRIM_MODE_MASK
]( ctx
, first
, last
, flags
);
235 void radeonEmitEltPrimitive( GLcontext
*ctx
,
240 tcl_render_tab_elts
[flags
&PRIM_MODE_MASK
]( ctx
, first
, last
, flags
);
243 void radeonTclPrimitive( GLcontext
*ctx
,
247 radeonContextPtr rmesa
= RADEON_CONTEXT(ctx
);
249 GLuint newprim
= hw_prim
| RADEON_CP_VC_CNTL_TCL_ENABLE
;
251 if (newprim
!= rmesa
->tcl
.hw_primitive
||
252 !discrete_prim
[hw_prim
&0xf]) {
253 RADEON_NEWPRIM( rmesa
);
254 rmesa
->tcl
.hw_primitive
= newprim
;
257 se_cntl
= rmesa
->hw
.set
.cmd
[SET_SE_CNTL
];
258 se_cntl
&= ~RADEON_FLAT_SHADE_VTX_LAST
;
260 if (prim
== GL_POLYGON
&& (ctx
->_TriangleCaps
& DD_FLATSHADE
))
261 se_cntl
|= RADEON_FLAT_SHADE_VTX_0
;
263 se_cntl
|= RADEON_FLAT_SHADE_VTX_LAST
;
265 if (se_cntl
!= rmesa
->hw
.set
.cmd
[SET_SE_CNTL
]) {
266 RADEON_STATECHANGE( rmesa
, set
);
267 rmesa
->hw
.set
.cmd
[SET_SE_CNTL
] = se_cntl
;
272 /**********************************************************************/
273 /* Render pipeline stage */
274 /**********************************************************************/
279 static GLboolean
radeon_run_tcl_render( GLcontext
*ctx
,
280 struct tnl_pipeline_stage
*stage
)
282 radeonContextPtr rmesa
= RADEON_CONTEXT(ctx
);
283 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
284 struct vertex_buffer
*VB
= &tnl
->vb
;
285 GLuint i
,flags
= 0,length
;
287 /* TODO: separate this from the swtnl pipeline
289 if (rmesa
->TclFallback
)
290 return GL_TRUE
; /* fallback to software t&l */
295 radeonReleaseArrays( ctx
, stage
->changed_inputs
);
296 radeonEmitArrays( ctx
, stage
->inputs
);
298 rmesa
->tcl
.Elts
= VB
->Elts
;
300 for (i
= 0 ; i
< VB
->PrimitiveCount
; i
++)
302 GLuint prim
= VB
->Primitive
[i
].mode
;
303 GLuint start
= VB
->Primitive
[i
].start
;
304 GLuint length
= VB
->Primitive
[i
].count
;
310 radeonEmitEltPrimitive( ctx
, start
, start
+length
, prim
);
312 radeonEmitPrimitive( ctx
, start
, start
+length
, prim
);
315 return GL_FALSE
; /* finished the pipe */
320 static void radeon_check_tcl_render( GLcontext
*ctx
,
321 struct tnl_pipeline_stage
*stage
)
323 radeonContextPtr rmesa
= RADEON_CONTEXT(ctx
);
324 GLuint inputs
= VERT_BIT_POS
;
326 if (ctx
->RenderMode
== GL_RENDER
) {
327 /* Make all this event-driven:
329 if (ctx
->Light
.Enabled
) {
330 inputs
|= VERT_BIT_NORMAL
;
332 if (1 || ctx
->Light
.ColorMaterialEnabled
) {
333 inputs
|= VERT_BIT_COLOR0
;
337 inputs
|= VERT_BIT_COLOR0
;
339 if (ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
) {
340 inputs
|= VERT_BIT_COLOR1
;
344 if (ctx
->Texture
.Unit
[0]._ReallyEnabled
) {
345 if (ctx
->Texture
.Unit
[0].TexGenEnabled
) {
346 if (rmesa
->TexGenNeedNormals
[0]) {
347 inputs
|= VERT_BIT_NORMAL
;
350 inputs
|= VERT_BIT_TEX0
;
354 if (ctx
->Texture
.Unit
[1]._ReallyEnabled
) {
355 if (ctx
->Texture
.Unit
[1].TexGenEnabled
) {
356 if (rmesa
->TexGenNeedNormals
[1]) {
357 inputs
|= VERT_BIT_NORMAL
;
360 inputs
|= VERT_BIT_TEX1
;
364 stage
->inputs
= inputs
;
371 static void radeon_init_tcl_render( GLcontext
*ctx
,
372 struct tnl_pipeline_stage
*stage
)
374 stage
->check
= radeon_check_tcl_render
;
375 stage
->check( ctx
, stage
);
378 static void dtr( struct tnl_pipeline_stage
*stage
)
384 /* Initial state for tcl stage.
386 const struct tnl_pipeline_stage _radeon_tcl_stage
=
389 (_DD_NEW_SEPARATE_SPECULAR
|
393 _NEW_RENDERMODE
), /* re-check (new inputs) */
394 0, /* re-run (always runs) */
395 GL_TRUE
, /* active */
396 0, 0, /* inputs (set in check_render), outputs */
397 0, 0, /* changed_inputs, private */
398 dtr
, /* destructor */
399 radeon_init_tcl_render
, /* check - initially set to alloc data */
400 radeon_run_tcl_render
/* run */
405 /**********************************************************************/
406 /* Validate state at pipeline start */
407 /**********************************************************************/
410 /*-----------------------------------------------------------------------
411 * Manage TCL fallbacks
415 static void transition_to_swtnl( GLcontext
*ctx
)
417 radeonContextPtr rmesa
= RADEON_CONTEXT(ctx
);
418 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
421 RADEON_NEWPRIM( rmesa
);
422 rmesa
->swtcl
.vertex_format
= 0;
424 radeonChooseVertexState( ctx
);
425 radeonChooseRenderState( ctx
);
427 _mesa_validate_all_lighting_tables( ctx
);
429 tnl
->Driver
.NotifyMaterialChange
=
430 _mesa_validate_all_lighting_tables
;
432 radeonReleaseArrays( ctx
, ~0 );
434 se_cntl
= rmesa
->hw
.set
.cmd
[SET_SE_CNTL
];
435 se_cntl
|= RADEON_FLAT_SHADE_VTX_LAST
;
437 if (se_cntl
!= rmesa
->hw
.set
.cmd
[SET_SE_CNTL
]) {
438 RADEON_STATECHANGE( rmesa
, set
);
439 rmesa
->hw
.set
.cmd
[SET_SE_CNTL
] = se_cntl
;
444 static void transition_to_hwtnl( GLcontext
*ctx
)
446 radeonContextPtr rmesa
= RADEON_CONTEXT(ctx
);
447 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
448 GLuint se_coord_fmt
= (RADEON_VTX_W0_IS_NOT_1_OVER_W0
|
449 RADEON_TEX1_W_ROUTING_USE_Q1
);
451 if ( se_coord_fmt
!= rmesa
->hw
.set
.cmd
[SET_SE_COORDFMT
] ) {
452 RADEON_STATECHANGE( rmesa
, set
);
453 rmesa
->hw
.set
.cmd
[SET_SE_COORDFMT
] = se_coord_fmt
;
454 _tnl_need_projected_coords( ctx
, GL_FALSE
);
457 radeonUpdateMaterial( ctx
);
459 tnl
->Driver
.NotifyMaterialChange
= radeonUpdateMaterial
;
461 if ( rmesa
->dma
.flush
)
462 rmesa
->dma
.flush( rmesa
);
464 rmesa
->dma
.flush
= 0;
465 rmesa
->swtcl
.vertex_format
= 0;
467 if (rmesa
->swtcl
.indexed_verts
.buf
)
468 radeonReleaseDmaRegion( rmesa
, &rmesa
->swtcl
.indexed_verts
,
471 if (RADEON_DEBUG
& DEBUG_FALLBACKS
)
472 fprintf(stderr
, "Radeon end tcl fallback\n");
475 static char *fallbackStrings
[] = {
476 "Rasterization fallback",
477 "Unfilled triangles",
478 "Twosided lighting, differing materials",
479 "Materials in VB (maybe between begin/end)",
487 static char *getFallbackString(GLuint bit
)
494 return fallbackStrings
[i
];
499 void radeonTclFallback( GLcontext
*ctx
, GLuint bit
, GLboolean mode
)
501 radeonContextPtr rmesa
= RADEON_CONTEXT(ctx
);
502 GLuint oldfallback
= rmesa
->TclFallback
;
505 rmesa
->TclFallback
|= bit
;
506 if (oldfallback
== 0) {
507 if (RADEON_DEBUG
& DEBUG_FALLBACKS
)
508 fprintf(stderr
, "Radeon begin tcl fallback %s\n",
509 getFallbackString( bit
));
510 transition_to_swtnl( ctx
);
514 rmesa
->TclFallback
&= ~bit
;
515 if (oldfallback
== bit
) {
516 if (RADEON_DEBUG
& DEBUG_FALLBACKS
)
517 fprintf(stderr
, "Radeon end tcl fallback %s\n",
518 getFallbackString( bit
));
519 transition_to_hwtnl( ctx
);