1 /**************************************************************************
3 Copyright 2000, 2001 ATI Technologies Inc., Ontario, Canada, and
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 <keithw@vmware.com>
35 #include "main/glheader.h"
36 #include "util/imports.h"
37 #include "main/mtypes.h"
38 #include "main/light.h"
39 #include "main/enums.h"
40 #include "main/state.h"
42 #include "util/macros.h"
46 #include "tnl/t_pipeline.h"
48 #include "radeon_common.h"
49 #include "radeon_context.h"
50 #include "radeon_state.h"
51 #include "radeon_ioctl.h"
52 #include "radeon_tcl.h"
53 #include "radeon_swtcl.h"
54 #include "radeon_maos.h"
55 #include "radeon_common_context.h"
60 * Render unclipped vertex buffers by emitting vertices directly to
61 * dma buffers. Use strip/fan hardware primitives where possible.
62 * Try to simulate missing primitives with indexed vertices.
66 #define HAVE_LINE_LOOP 0
67 #define HAVE_LINE_STRIPS 1
68 #define HAVE_TRIANGLES 1
69 #define HAVE_TRI_STRIPS 1
70 #define HAVE_TRI_FANS 1
72 #define HAVE_QUAD_STRIPS 0
73 #define HAVE_POLYGONS 1
77 #define HW_POINTS RADEON_CP_VC_CNTL_PRIM_TYPE_POINT
78 #define HW_LINES RADEON_CP_VC_CNTL_PRIM_TYPE_LINE
79 #define HW_LINE_LOOP 0
80 #define HW_LINE_STRIP RADEON_CP_VC_CNTL_PRIM_TYPE_LINE_STRIP
81 #define HW_TRIANGLES RADEON_CP_VC_CNTL_PRIM_TYPE_TRI_LIST
82 #define HW_TRIANGLE_STRIP_0 RADEON_CP_VC_CNTL_PRIM_TYPE_TRI_STRIP
83 #define HW_TRIANGLE_STRIP_1 0
84 #define HW_TRIANGLE_FAN RADEON_CP_VC_CNTL_PRIM_TYPE_TRI_FAN
86 #define HW_QUAD_STRIP 0
87 #define HW_POLYGON RADEON_CP_VC_CNTL_PRIM_TYPE_TRI_FAN
90 static GLboolean discrete_prim
[0x10] = {
98 1, /* 7 rect list (unused) */
99 1, /* 8 3vert point */
100 1, /* 9 3vert line */
110 #define LOCAL_VARS r100ContextPtr rmesa = R100_CONTEXT(ctx)
111 #define ELT_TYPE GLushort
113 #define ELT_INIT(prim, hw_prim) \
114 radeonTclPrimitive( ctx, prim, hw_prim | RADEON_CP_VC_CNTL_PRIM_WALK_IND )
116 #define GET_MESA_ELTS() rmesa->tcl.Elts
119 /* Don't really know how many elts will fit in what's left of cmdbuf,
120 * as there is state to emit, etc:
123 /* Testing on isosurf shows a maximum around here. Don't know if it's
124 * the card or driver or kernel module that is causing the behaviour.
126 #define GET_MAX_HW_ELTS() 300
129 #define RESET_STIPPLE() do { \
130 RADEON_STATECHANGE( rmesa, lin ); \
131 radeonEmitState(&rmesa->radeon); \
134 #define AUTO_STIPPLE( mode ) do { \
135 RADEON_STATECHANGE( rmesa, lin ); \
137 rmesa->hw.lin.cmd[LIN_RE_LINE_PATTERN] |= \
138 RADEON_LINE_PATTERN_AUTO_RESET; \
140 rmesa->hw.lin.cmd[LIN_RE_LINE_PATTERN] &= \
141 ~RADEON_LINE_PATTERN_AUTO_RESET; \
142 radeonEmitState(&rmesa->radeon); \
147 #define ALLOC_ELTS(nr) radeonAllocElts( rmesa, nr )
149 static GLushort
*radeonAllocElts( r100ContextPtr rmesa
, GLuint nr
)
151 if (rmesa
->radeon
.dma
.flush
)
152 rmesa
->radeon
.dma
.flush( &rmesa
->radeon
.glCtx
);
154 radeonEmitAOS( rmesa
,
155 rmesa
->radeon
.tcl
.aos_count
, 0 );
157 return radeonAllocEltsOpenEnded( rmesa
, rmesa
->tcl
.vertex_format
,
158 rmesa
->tcl
.hw_primitive
, nr
);
161 #define CLOSE_ELTS() if (0) 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 radeonEmitPrim( struct gl_context
*ctx
,
175 r100ContextPtr rmesa
= R100_CONTEXT( ctx
);
176 radeonTclPrimitive( ctx
, prim
, hwprim
);
178 radeonEmitAOS( rmesa
,
179 rmesa
->radeon
.tcl
.aos_count
,
182 /* Why couldn't this packet have taken an offset param?
184 radeonEmitVbufPrim( rmesa
,
185 rmesa
->tcl
.vertex_format
,
186 rmesa
->tcl
.hw_primitive
,
190 #define EMIT_PRIM( ctx, prim, hwprim, start, count ) do { \
191 radeonEmitPrim( ctx, prim, hwprim, start, count ); \
192 (void) rmesa; } while (0)
194 #define MAX_CONVERSION_SIZE 40
196 /* Try & join small primitives
199 #define PREFER_DISCRETE_ELT_PRIM( NR, PRIM ) 0
201 #define PREFER_DISCRETE_ELT_PRIM( NR, PRIM ) \
204 rmesa->tcl.hw_primitive == (PRIM| \
205 RADEON_CP_VC_CNTL_PRIM_WALK_IND| \
206 RADEON_CP_VC_CNTL_TCL_ENABLE)))
209 #ifdef MESA_BIG_ENDIAN
210 /* We could do without (most of) this ugliness if dest was always 32 bit word aligned... */
211 #define EMIT_ELT(dest, offset, x) do { \
212 int off = offset + ( ( (uintptr_t)dest & 0x2 ) >> 1 ); \
213 GLushort *des = (GLushort *)( (uintptr_t)dest & ~0x2 ); \
214 (des)[ off + 1 - 2 * ( off & 1 ) ] = (GLushort)(x); \
215 (void)rmesa; } while (0)
217 #define EMIT_ELT(dest, offset, x) do { \
218 (dest)[offset] = (GLushort) (x); \
219 (void)rmesa; } while (0)
222 #define EMIT_TWO_ELTS(dest, offset, x, y) *(GLuint *)(dest+offset) = ((y)<<16)|(x);
226 #define TAG(x) tcl_##x
227 #include "tnl_dd/t_dd_dmatmp2.h"
229 /**********************************************************************/
230 /* External entrypoints */
231 /**********************************************************************/
233 void radeonEmitPrimitive( struct gl_context
*ctx
,
238 tcl_render_tab_verts
[flags
&PRIM_MODE_MASK
]( ctx
, first
, last
, flags
);
241 void radeonEmitEltPrimitive( struct gl_context
*ctx
,
246 tcl_render_tab_elts
[flags
&PRIM_MODE_MASK
]( ctx
, first
, last
, flags
);
249 void radeonTclPrimitive( struct gl_context
*ctx
,
253 r100ContextPtr rmesa
= R100_CONTEXT(ctx
);
255 GLuint newprim
= hw_prim
| RADEON_CP_VC_CNTL_TCL_ENABLE
;
257 radeon_prepare_render(&rmesa
->radeon
);
258 if (rmesa
->radeon
.NewGLState
)
259 radeonValidateState( ctx
);
261 if (newprim
!= rmesa
->tcl
.hw_primitive
||
262 !discrete_prim
[hw_prim
&0xf]) {
263 RADEON_NEWPRIM( rmesa
);
264 rmesa
->tcl
.hw_primitive
= newprim
;
267 se_cntl
= rmesa
->hw
.set
.cmd
[SET_SE_CNTL
];
268 se_cntl
&= ~RADEON_FLAT_SHADE_VTX_LAST
;
270 if (prim
== GL_POLYGON
&& ctx
->Light
.ShadeModel
== GL_FLAT
)
271 se_cntl
|= RADEON_FLAT_SHADE_VTX_0
;
273 se_cntl
|= RADEON_FLAT_SHADE_VTX_LAST
;
275 if (se_cntl
!= rmesa
->hw
.set
.cmd
[SET_SE_CNTL
]) {
276 RADEON_STATECHANGE( rmesa
, set
);
277 rmesa
->hw
.set
.cmd
[SET_SE_CNTL
] = se_cntl
;
282 * Predict total emit size for next rendering operation so there is no flush in middle of rendering
283 * Prediction has to aim towards the best possible value that is worse than worst case scenario
285 static GLuint
radeonEnsureEmitSize( struct gl_context
* ctx
, GLuint inputs
)
287 r100ContextPtr rmesa
= R100_CONTEXT(ctx
);
288 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
289 struct vertex_buffer
*VB
= &tnl
->vb
;
290 GLuint space_required
;
292 GLuint nr_aos
= 1; /* radeonEmitArrays does always emit one */
294 /* list of flags that are allocating aos object */
295 const GLuint flags_to_check
[] = {
301 /* predict number of aos to emit */
302 for (i
=0; i
< ARRAY_SIZE(flags_to_check
); ++i
)
304 if (inputs
& flags_to_check
[i
])
307 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; ++i
)
309 if (inputs
& VERT_BIT_TEX(i
))
314 /* count the prediction for state size */
316 state_size
= radeonCountStateEmitSize( &rmesa
->radeon
);
317 /* tcl may be changed in radeonEmitArrays so account for it if not dirty */
318 if (!rmesa
->hw
.tcl
.dirty
)
319 state_size
+= rmesa
->hw
.tcl
.check( &rmesa
->radeon
.glCtx
, &rmesa
->hw
.tcl
);
320 /* predict size for elements */
321 for (i
= 0; i
< VB
->PrimitiveCount
; ++i
)
323 /* If primitive.count is less than MAX_CONVERSION_SIZE
324 rendering code may decide convert to elts.
325 In that case we have to make pessimistic prediction.
326 and use larger of 2 paths. */
327 const GLuint elts
= ELTS_BUFSZ(nr_aos
);
328 const GLuint index
= INDEX_BUFSZ
;
329 const GLuint vbuf
= VBUF_BUFSZ
;
330 if (!VB
->Primitive
[i
].count
)
332 if ( (!VB
->Elts
&& VB
->Primitive
[i
].count
>= MAX_CONVERSION_SIZE
)
333 || vbuf
> index
+ elts
)
334 space_required
+= vbuf
;
336 space_required
+= index
+ elts
;
337 space_required
+= VB
->Primitive
[i
].count
* 3;
338 space_required
+= AOS_BUFSZ(nr_aos
);
340 space_required
+= SCISSOR_BUFSZ
;
342 /* flush the buffer in case we need more than is left. */
343 if (rcommonEnsureCmdBufSpace(&rmesa
->radeon
, space_required
, __func__
))
344 return space_required
+ radeonCountStateEmitSize( &rmesa
->radeon
);
346 return space_required
+ state_size
;
349 /**********************************************************************/
350 /* Render pipeline stage */
351 /**********************************************************************/
356 static GLboolean
radeon_run_tcl_render( struct gl_context
*ctx
,
357 struct tnl_pipeline_stage
*stage
)
359 r100ContextPtr rmesa
= R100_CONTEXT(ctx
);
360 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
361 struct vertex_buffer
*VB
= &tnl
->vb
;
362 GLuint inputs
= VERT_BIT_POS
| VERT_BIT_COLOR0
;
366 /* TODO: separate this from the swtnl pipeline
368 if (rmesa
->radeon
.TclFallback
)
369 return GL_TRUE
; /* fallback to software t&l */
374 /* NOTE: inputs != tnl->render_inputs - these are the untransformed
377 if (ctx
->Light
.Enabled
) {
378 inputs
|= VERT_BIT_NORMAL
;
381 if (_mesa_need_secondary_color(ctx
)) {
382 inputs
|= VERT_BIT_COLOR1
;
385 if ( (ctx
->Fog
.FogCoordinateSource
== GL_FOG_COORD
) && ctx
->Fog
.Enabled
) {
386 inputs
|= VERT_BIT_FOG
;
389 for (i
= 0 ; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
390 if (ctx
->Texture
.Unit
[i
]._Current
) {
391 /* TODO: probably should not emit texture coords when texgen is enabled */
392 if (rmesa
->TexGenNeedNormals
[i
]) {
393 inputs
|= VERT_BIT_NORMAL
;
395 inputs
|= VERT_BIT_TEX(i
);
399 radeonReleaseArrays( ctx
, ~0 );
400 emit_end
= radeonEnsureEmitSize( ctx
, inputs
)
401 + rmesa
->radeon
.cmdbuf
.cs
->cdw
;
402 radeonEmitArrays( ctx
, inputs
);
404 rmesa
->tcl
.Elts
= VB
->Elts
;
406 for (i
= 0 ; i
< VB
->PrimitiveCount
; i
++)
408 GLuint prim
= _tnl_translate_prim(&VB
->Primitive
[i
]);
409 GLuint start
= VB
->Primitive
[i
].start
;
410 GLuint length
= VB
->Primitive
[i
].count
;
416 radeonEmitEltPrimitive( ctx
, start
, start
+length
, prim
);
418 radeonEmitPrimitive( ctx
, start
, start
+length
, prim
);
421 if (emit_end
< rmesa
->radeon
.cmdbuf
.cs
->cdw
)
422 WARN_ONCE("Rendering was %d commands larger than predicted size."
423 " We might overflow command buffer.\n", rmesa
->radeon
.cmdbuf
.cs
->cdw
- emit_end
);
425 return GL_FALSE
; /* finished the pipe */
430 /* Initial state for tcl stage.
432 const struct tnl_pipeline_stage _radeon_tcl_stage
=
439 radeon_run_tcl_render
/* run */
444 /**********************************************************************/
445 /* Validate state at pipeline start */
446 /**********************************************************************/
449 /*-----------------------------------------------------------------------
450 * Manage TCL fallbacks
454 static void transition_to_swtnl( struct gl_context
*ctx
)
456 r100ContextPtr rmesa
= R100_CONTEXT(ctx
);
457 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
460 RADEON_NEWPRIM( rmesa
);
461 rmesa
->swtcl
.vertex_format
= 0;
463 radeonChooseVertexState( ctx
);
464 radeonChooseRenderState( ctx
);
466 _tnl_validate_shine_tables( ctx
);
468 tnl
->Driver
.NotifyMaterialChange
=
469 _tnl_validate_shine_tables
;
471 radeonReleaseArrays( ctx
, ~0 );
473 se_cntl
= rmesa
->hw
.set
.cmd
[SET_SE_CNTL
];
474 se_cntl
|= RADEON_FLAT_SHADE_VTX_LAST
;
476 if (se_cntl
!= rmesa
->hw
.set
.cmd
[SET_SE_CNTL
]) {
477 RADEON_STATECHANGE( rmesa
, set
);
478 rmesa
->hw
.set
.cmd
[SET_SE_CNTL
] = se_cntl
;
483 static void transition_to_hwtnl( struct gl_context
*ctx
)
485 r100ContextPtr rmesa
= R100_CONTEXT(ctx
);
486 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
487 GLuint se_coord_fmt
= rmesa
->hw
.set
.cmd
[SET_SE_COORDFMT
];
489 se_coord_fmt
&= ~(RADEON_VTX_XY_PRE_MULT_1_OVER_W0
|
490 RADEON_VTX_Z_PRE_MULT_1_OVER_W0
|
491 RADEON_VTX_W0_IS_NOT_1_OVER_W0
);
492 se_coord_fmt
|= RADEON_VTX_W0_IS_NOT_1_OVER_W0
;
494 if ( se_coord_fmt
!= rmesa
->hw
.set
.cmd
[SET_SE_COORDFMT
] ) {
495 RADEON_STATECHANGE( rmesa
, set
);
496 rmesa
->hw
.set
.cmd
[SET_SE_COORDFMT
] = se_coord_fmt
;
497 _tnl_need_projected_coords( ctx
, GL_FALSE
);
500 radeonUpdateMaterial( ctx
);
502 tnl
->Driver
.NotifyMaterialChange
= radeonUpdateMaterial
;
504 if ( rmesa
->radeon
.dma
.flush
)
505 rmesa
->radeon
.dma
.flush( &rmesa
->radeon
.glCtx
);
507 rmesa
->radeon
.dma
.flush
= NULL
;
508 rmesa
->swtcl
.vertex_format
= 0;
510 // if (rmesa->swtcl.indexed_verts.buf)
511 // radeonReleaseDmaRegion( rmesa, &rmesa->swtcl.indexed_verts,
514 if (RADEON_DEBUG
& RADEON_FALLBACKS
)
515 fprintf(stderr
, "Radeon end tcl fallback\n");
518 static char *fallbackStrings
[] = {
519 "Rasterization fallback",
520 "Unfilled triangles",
521 "Twosided lighting, differing materials",
522 "Materials in VB (maybe between begin/end)",
527 "Fogcoord with separate specular lighting"
531 static char *getFallbackString(GLuint bit
)
538 return fallbackStrings
[i
];
543 void radeonTclFallback( struct gl_context
*ctx
, GLuint bit
, GLboolean mode
)
545 r100ContextPtr rmesa
= R100_CONTEXT(ctx
);
546 GLuint oldfallback
= rmesa
->radeon
.TclFallback
;
549 rmesa
->radeon
.TclFallback
|= bit
;
550 if (oldfallback
== 0) {
551 if (RADEON_DEBUG
& RADEON_FALLBACKS
)
552 fprintf(stderr
, "Radeon begin tcl fallback %s\n",
553 getFallbackString( bit
));
554 transition_to_swtnl( ctx
);
558 rmesa
->radeon
.TclFallback
&= ~bit
;
559 if (oldfallback
== bit
) {
560 if (RADEON_DEBUG
& RADEON_FALLBACKS
)
561 fprintf(stderr
, "Radeon end tcl fallback %s\n",
562 getFallbackString( bit
));
563 transition_to_hwtnl( ctx
);