2 /**************************************************************************
4 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
6 The Weather Channel (TM) funded Tungsten Graphics to develop the
7 initial release of the Radeon 8500 driver under the XFree86 license.
8 This notice must be preserved.
10 Permission is hereby granted, free of charge, to any person obtaining
11 a copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sublicense, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial
20 portions of the Software.
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
26 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 **************************************************************************/
34 * Keith Whitwell <keith@tungstengraphics.com>
44 #include "array_cache/acache.h"
46 #include "tnl/t_pipeline.h"
48 #include "r200_context.h"
49 #include "r200_state.h"
50 #include "r200_ioctl.h"
53 #include "r200_swtcl.h"
54 #include "r200_maos.h"
60 #define HAVE_LINE_LOOP 0
61 #define HAVE_LINE_STRIPS 1
62 #define HAVE_TRIANGLES 1
63 #define HAVE_TRI_STRIPS 1
64 #define HAVE_TRI_STRIP_1 0
65 #define HAVE_TRI_FANS 1
66 #define HAVE_QUADS 0 /* hw quad verts in wrong order??? */
67 #define HAVE_QUAD_STRIPS 1
68 #define HAVE_POLYGONS 1
72 #define HW_POINTS R200_VF_PRIM_POINTS
73 #define HW_LINES R200_VF_PRIM_LINES
74 #define HW_LINE_LOOP 0
75 #define HW_LINE_STRIP R200_VF_PRIM_LINE_STRIP
76 #define HW_TRIANGLES R200_VF_PRIM_TRIANGLES
77 #define HW_TRIANGLE_STRIP_0 R200_VF_PRIM_TRIANGLE_STRIP
78 #define HW_TRIANGLE_STRIP_1 0
79 #define HW_TRIANGLE_FAN R200_VF_PRIM_TRIANGLE_FAN
80 #define HW_QUADS R200_VF_PRIM_QUADS
81 #define HW_QUAD_STRIP R200_VF_PRIM_QUAD_STRIP
82 #define HW_POLYGON R200_VF_PRIM_POLYGON
85 static GLboolean discrete_prim
[0x10] = {
93 0, /* 7 tri_w_flags */
94 1, /* 8 rect list (unused) */
95 1, /* 9 3vert point */
97 0, /* b point sprite */
100 0, /* e quad strip */
105 #define LOCAL_VARS r200ContextPtr rmesa = R200_CONTEXT(ctx);rmesa = rmesa
106 #define ELTS_VARS GLushort *dest
108 #define ELT_INIT(prim, hw_prim) \
109 r200TclPrimitive( ctx, prim, hw_prim | R200_VF_PRIM_WALK_IND )
111 #define GET_ELTS() rmesa->tcl.Elts
114 #define NEW_PRIMITIVE() R200_NEWPRIM( rmesa )
115 #define NEW_BUFFER() r200RefillCurrentDmaRegion( rmesa )
117 /* Don't really know how many elts will fit in what's left of cmdbuf,
118 * as there is state to emit, etc:
122 #define GET_CURRENT_VB_MAX_ELTS() \
123 ((R200_CMD_BUF_SZ - (rmesa->store.cmd_used + 16)) / 2)
124 #define GET_SUBSEQUENT_VB_MAX_ELTS() ((R200_CMD_BUF_SZ - 16) / 2)
126 /* Testing on isosurf shows a maximum around here. Don't know if it's
127 * the card or driver or kernel module that is causing the behaviour.
129 #define GET_CURRENT_VB_MAX_ELTS() 300
130 #define GET_SUBSEQUENT_VB_MAX_ELTS() 300
133 #define RESET_STIPPLE() do { \
134 R200_STATECHANGE( rmesa, lin ); \
135 r200EmitState( rmesa ); \
138 #define AUTO_STIPPLE( mode ) do { \
139 R200_STATECHANGE( rmesa, lin ); \
141 rmesa->hw.lin.cmd[LIN_RE_LINE_PATTERN] |= \
142 R200_LINE_PATTERN_AUTO_RESET; \
144 rmesa->hw.lin.cmd[LIN_RE_LINE_PATTERN] &= \
145 ~R200_LINE_PATTERN_AUTO_RESET; \
146 r200EmitState( rmesa ); \
150 /* How do you extend an existing primitive?
152 #define ALLOC_ELTS(nr) \
154 if (rmesa->dma.flush == r200FlushElts && \
155 rmesa->store.cmd_used + nr*2 < R200_CMD_BUF_SZ) { \
157 dest = (GLushort *)(rmesa->store.cmd_buf + \
158 rmesa->store.cmd_used); \
159 rmesa->store.cmd_used += nr*2; \
162 if (rmesa->dma.flush) \
163 rmesa->dma.flush( rmesa ); \
165 r200EmitAOS( rmesa, \
166 rmesa->tcl.aos_components, \
167 rmesa->tcl.nr_aos_components, \
170 dest = r200AllocEltsOpenEnded( rmesa, \
171 rmesa->tcl.hw_primitive, \
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 EMIT_PRIM( GLcontext
*ctx
,
188 r200ContextPtr rmesa
= R200_CONTEXT( ctx
);
189 r200TclPrimitive( ctx
, prim
, hwprim
);
192 rmesa
->tcl
.aos_components
,
193 rmesa
->tcl
.nr_aos_components
,
196 /* Why couldn't this packet have taken an offset param?
198 r200EmitVbufPrim( rmesa
,
199 rmesa
->tcl
.hw_primitive
,
205 /* Try & join small primitives
208 #define PREFER_DISCRETE_ELT_PRIM( NR, PRIM ) 0
210 #define PREFER_DISCRETE_ELT_PRIM( NR, PRIM ) \
213 rmesa->tcl.hw_primitive == (PRIM| \
214 R200_VF_TCL_OUTPUT_VTX_ENABLE| \
215 R200_VF_PRIM_WALK_IND)))
218 #ifdef MESA_BIG_ENDIAN
219 /* We could do without (most of) this ugliness if dest was always 32 bit word aligned... */
220 #define EMIT_ELT(offset, x) do { \
221 int off = offset + ( ( (GLuint)dest & 0x2 ) >> 1 ); \
222 GLushort *des = (GLushort *)( (GLuint)dest & ~0x2 ); \
223 (des)[ off + 1 - 2 * ( off & 1 ) ] = (GLushort)(x); } while (0)
225 #define EMIT_ELT(offset, x) (dest)[offset] = (GLushort) (x)
227 #define EMIT_TWO_ELTS(offset, x, y) *(GLuint *)(dest+offset) = ((y)<<16)|(x);
228 #define INCR_ELTS( nr ) dest += nr
229 #define RELEASE_ELT_VERTS() \
230 r200ReleaseArrays( ctx, ~0 )
234 #define TAG(x) tcl_##x
235 #include "tnl_dd/t_dd_dmatmp2.h"
237 /**********************************************************************/
238 /* External entrypoints */
239 /**********************************************************************/
241 void r200EmitPrimitive( GLcontext
*ctx
,
246 tcl_render_tab_verts
[flags
&PRIM_MODE_MASK
]( ctx
, first
, last
, flags
);
249 void r200EmitEltPrimitive( GLcontext
*ctx
,
254 tcl_render_tab_elts
[flags
&PRIM_MODE_MASK
]( ctx
, first
, last
, flags
);
257 void r200TclPrimitive( GLcontext
*ctx
,
261 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
262 GLuint newprim
= hw_prim
| R200_VF_TCL_OUTPUT_VTX_ENABLE
;
264 if (newprim
!= rmesa
->tcl
.hw_primitive
||
265 !discrete_prim
[hw_prim
&0xf]) {
266 R200_NEWPRIM( rmesa
);
267 rmesa
->tcl
.hw_primitive
= newprim
;
272 /**********************************************************************/
273 /* Render pipeline stage */
274 /**********************************************************************/
279 static GLboolean
r200_run_tcl_render( GLcontext
*ctx
,
280 struct gl_pipeline_stage
*stage
)
282 r200ContextPtr rmesa
= R200_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 */
292 if (R200_DEBUG
& DEBUG_PRIMS
)
293 fprintf(stderr
, "%s\n", __FUNCTION__
);
298 r200ReleaseArrays( ctx
, ~0 /* stage->changed_inputs */ );
299 r200EmitArrays( ctx
, stage
->inputs
);
301 rmesa
->tcl
.Elts
= VB
->Elts
;
303 for (i
= VB
->FirstPrimitive
; !(flags
& PRIM_LAST
) ; i
+= length
)
305 flags
= VB
->Primitive
[i
];
306 length
= VB
->PrimitiveLength
[i
];
308 if (R200_DEBUG
& DEBUG_PRIMS
)
309 fprintf(stderr
, "%s: prim %s %d..%d\n",
311 _mesa_lookup_enum_by_nr(flags
& PRIM_MODE_MASK
),
318 r200EmitEltPrimitive( ctx
, i
, i
+length
, flags
);
320 r200EmitPrimitive( ctx
, i
, i
+length
, flags
);
323 return GL_FALSE
; /* finished the pipe */
328 static void r200_check_tcl_render( GLcontext
*ctx
,
329 struct gl_pipeline_stage
*stage
)
331 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
332 GLuint inputs
= VERT_BIT_POS
;
336 if (rmesa
->NewGLState
)
337 r200ValidateState( ctx
);
339 if (ctx
->RenderMode
== GL_RENDER
) {
340 /* Make all this event-driven:
342 if (ctx
->Light
.Enabled
) {
343 inputs
|= VERT_BIT_NORMAL
;
345 if (1 || ctx
->Light
.ColorMaterialEnabled
) {
346 inputs
|= VERT_BIT_COLOR0
;
350 inputs
|= VERT_BIT_COLOR0
;
352 if (ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
) {
353 inputs
|= VERT_BIT_COLOR1
;
357 if (ctx
->Texture
.Unit
[0]._ReallyEnabled
) {
358 if (ctx
->Texture
.Unit
[0].TexGenEnabled
) {
359 if (rmesa
->TexGenNeedNormals
[0]) {
360 inputs
|= VERT_BIT_NORMAL
;
363 inputs
|= VERT_BIT_TEX0
;
367 if (ctx
->Texture
.Unit
[1]._ReallyEnabled
) {
368 if (ctx
->Texture
.Unit
[1].TexGenEnabled
) {
369 if (rmesa
->TexGenNeedNormals
[1]) {
370 inputs
|= VERT_BIT_NORMAL
;
373 inputs
|= VERT_BIT_TEX1
;
377 stage
->inputs
= inputs
;
384 static void r200_init_tcl_render( GLcontext
*ctx
,
385 struct gl_pipeline_stage
*stage
)
387 stage
->check
= r200_check_tcl_render
;
388 stage
->check( ctx
, stage
);
391 static void dtr( struct gl_pipeline_stage
*stage
)
397 /* Initial state for tcl stage.
399 const struct gl_pipeline_stage _r200_tcl_stage
=
402 (_DD_NEW_SEPARATE_SPECULAR
|
406 _NEW_RENDERMODE
), /* re-check (new inputs) */
407 0, /* re-run (always runs) */
408 GL_TRUE
, /* active */
409 0, 0, /* inputs (set in check_render), outputs */
410 0, 0, /* changed_inputs, private */
411 dtr
, /* destructor */
412 r200_init_tcl_render
, /* check - initially set to alloc data */
413 r200_run_tcl_render
/* run */
418 /**********************************************************************/
419 /* Validate state at pipeline start */
420 /**********************************************************************/
423 /*-----------------------------------------------------------------------
424 * Manage TCL fallbacks
428 static void transition_to_swtnl( GLcontext
*ctx
)
430 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
431 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
433 R200_NEWPRIM( rmesa
);
434 rmesa
->swtcl
.vertex_format
= 0;
436 r200ChooseVertexState( ctx
);
437 r200ChooseRenderState( ctx
);
439 _mesa_validate_all_lighting_tables( ctx
);
441 tnl
->Driver
.NotifyMaterialChange
=
442 _mesa_validate_all_lighting_tables
;
444 r200ReleaseArrays( ctx
, ~0 );
446 /* Still using the D3D based hardware-rasterizer from the radeon;
447 * need to put the card into D3D mode to make it work:
449 R200_STATECHANGE( rmesa
, vap
);
450 rmesa
->hw
.vap
.cmd
[VAP_SE_VAP_CNTL
] &= ~R200_VAP_TCL_ENABLE
;
451 rmesa
->hw
.vap
.cmd
[VAP_SE_VAP_CNTL
] |= R200_VAP_D3D_TEX_DEFAULT
;
453 R200_STATECHANGE( rmesa
, vte
);
454 rmesa
->hw
.vte
.cmd
[VTE_SE_VTE_CNTL
] &= ~R200_VTX_W0_FMT
;
456 R200_STATECHANGE( rmesa
, set
);
457 rmesa
->hw
.set
.cmd
[SET_RE_CNTL
] |= (R200_VTX_STQ0_D3D
|
462 static void transition_to_hwtnl( GLcontext
*ctx
)
464 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
465 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
467 _tnl_need_projected_coords( ctx
, GL_FALSE
);
469 r200UpdateMaterial( ctx
);
471 tnl
->Driver
.NotifyMaterialChange
= r200UpdateMaterial
;
473 if ( rmesa
->dma
.flush
)
474 rmesa
->dma
.flush( rmesa
);
476 rmesa
->dma
.flush
= 0;
477 rmesa
->swtcl
.vertex_format
= 0;
479 if (rmesa
->swtcl
.indexed_verts
.buf
)
480 r200ReleaseDmaRegion( rmesa
, &rmesa
->swtcl
.indexed_verts
,
483 R200_STATECHANGE( rmesa
, vap
);
484 rmesa
->hw
.vap
.cmd
[VAP_SE_VAP_CNTL
] |= R200_VAP_TCL_ENABLE
;
485 rmesa
->hw
.vap
.cmd
[VAP_SE_VAP_CNTL
] &= ~(R200_VAP_FORCE_W_TO_ONE
|
486 R200_VAP_D3D_TEX_DEFAULT
);
488 R200_STATECHANGE( rmesa
, vte
);
489 rmesa
->hw
.vte
.cmd
[VTE_SE_VTE_CNTL
] &= ~(R200_VTX_XY_FMT
|R200_VTX_Z_FMT
);
490 rmesa
->hw
.vte
.cmd
[VTE_SE_VTE_CNTL
] |= R200_VTX_W0_FMT
;
492 R200_STATECHANGE( rmesa
, set
);
493 rmesa
->hw
.set
.cmd
[SET_RE_CNTL
] &= ~(R200_VTX_STQ0_D3D
|
497 if (R200_DEBUG
& DEBUG_FALLBACKS
)
498 fprintf(stderr
, "R200 end tcl fallback\n");
502 static char *fallbackStrings
[] = {
503 "Rasterization fallback",
504 "Unfilled triangles",
505 "Twosided lighting, differing materials",
506 "Materials in VB (maybe between begin/end)",
514 static char *getFallbackString(GLuint bit
)
521 return fallbackStrings
[i
];
526 void r200TclFallback( GLcontext
*ctx
, GLuint bit
, GLboolean mode
)
528 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
529 GLuint oldfallback
= rmesa
->TclFallback
;
532 rmesa
->TclFallback
|= bit
;
533 if (oldfallback
== 0) {
534 if (R200_DEBUG
& DEBUG_FALLBACKS
)
535 fprintf(stderr
, "R200 begin tcl fallback %s\n",
536 getFallbackString( bit
));
537 transition_to_swtnl( ctx
);
541 rmesa
->TclFallback
&= ~bit
;
542 if (oldfallback
== bit
) {
543 if (R200_DEBUG
& DEBUG_FALLBACKS
)
544 fprintf(stderr
, "R200 end tcl fallback %s\n",
545 getFallbackString( bit
));
546 transition_to_hwtnl( ctx
);