3d4e70115578425a39169659009a01dea12d2f27
[mesa.git] / src / mesa / drivers / dri / r200 / r200_swtcl.c
1 /*
2 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
3
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.
7
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:
15
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.
19
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.
27
28 **************************************************************************/
29
30 /*
31 * Authors:
32 * Keith Whitwell <keith@tungstengraphics.com>
33 */
34
35 #include "main/glheader.h"
36 #include "main/mtypes.h"
37 #include "main/colormac.h"
38 #include "main/enums.h"
39 #include "main/image.h"
40 #include "main/imports.h"
41 #include "main/macros.h"
42 #include "main/simple_list.h"
43
44 #include "swrast/s_context.h"
45 #include "swrast/s_fog.h"
46 #include "swrast_setup/swrast_setup.h"
47 #include "math/m_translate.h"
48 #include "tnl/tnl.h"
49 #include "tnl/t_context.h"
50 #include "tnl/t_pipeline.h"
51
52 #include "r200_context.h"
53 #include "r200_ioctl.h"
54 #include "r200_state.h"
55 #include "r200_swtcl.h"
56 #include "r200_tcl.h"
57
58
59 /***********************************************************************
60 * Initialization
61 ***********************************************************************/
62
63 #define EMIT_ATTR( ATTR, STYLE, F0 ) \
64 do { \
65 rmesa->radeon.swtcl.vertex_attrs[rmesa->radeon.swtcl.vertex_attr_count].attrib = (ATTR); \
66 rmesa->radeon.swtcl.vertex_attrs[rmesa->radeon.swtcl.vertex_attr_count].format = (STYLE); \
67 rmesa->radeon.swtcl.vertex_attr_count++; \
68 fmt_0 |= F0; \
69 } while (0)
70
71 #define EMIT_PAD( N ) \
72 do { \
73 rmesa->radeon.swtcl.vertex_attrs[rmesa->radeon.swtcl.vertex_attr_count].attrib = 0; \
74 rmesa->radeon.swtcl.vertex_attrs[rmesa->radeon.swtcl.vertex_attr_count].format = EMIT_PAD; \
75 rmesa->radeon.swtcl.vertex_attrs[rmesa->radeon.swtcl.vertex_attr_count].offset = (N); \
76 rmesa->radeon.swtcl.vertex_attr_count++; \
77 } while (0)
78
79 static void r200SetVertexFormat( GLcontext *ctx )
80 {
81 r200ContextPtr rmesa = R200_CONTEXT( ctx );
82 TNLcontext *tnl = TNL_CONTEXT(ctx);
83 struct vertex_buffer *VB = &tnl->vb;
84 DECLARE_RENDERINPUTS(index_bitset);
85 int fmt_0 = 0;
86 int fmt_1 = 0;
87 int offset = 0;
88
89 RENDERINPUTS_COPY( index_bitset, tnl->render_inputs_bitset );
90
91 /* Important:
92 */
93 if ( VB->NdcPtr != NULL ) {
94 VB->AttribPtr[VERT_ATTRIB_POS] = VB->NdcPtr;
95 }
96 else {
97 VB->AttribPtr[VERT_ATTRIB_POS] = VB->ClipPtr;
98 }
99
100 assert( VB->AttribPtr[VERT_ATTRIB_POS] != NULL );
101 rmesa->radeon.swtcl.vertex_attr_count = 0;
102
103 /* EMIT_ATTR's must be in order as they tell t_vertex.c how to
104 * build up a hardware vertex.
105 */
106 if ( !rmesa->swtcl.needproj ||
107 RENDERINPUTS_TEST_RANGE( index_bitset, _TNL_FIRST_TEX, _TNL_LAST_TEX )) { /* need w coord for projected textures */
108 EMIT_ATTR( _TNL_ATTRIB_POS, EMIT_4F, R200_VTX_XY | R200_VTX_Z0 | R200_VTX_W0 );
109 offset = 4;
110 }
111 else {
112 EMIT_ATTR( _TNL_ATTRIB_POS, EMIT_3F, R200_VTX_XY | R200_VTX_Z0 );
113 offset = 3;
114 }
115
116 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_POINTSIZE )) {
117 EMIT_ATTR( _TNL_ATTRIB_POINTSIZE, EMIT_1F, R200_VTX_POINT_SIZE );
118 offset += 1;
119 }
120
121 rmesa->swtcl.coloroffset = offset;
122 #if MESA_LITTLE_ENDIAN
123 EMIT_ATTR( _TNL_ATTRIB_COLOR0, EMIT_4UB_4F_RGBA, (R200_VTX_PK_RGBA << R200_VTX_COLOR_0_SHIFT) );
124 #else
125 EMIT_ATTR( _TNL_ATTRIB_COLOR0, EMIT_4UB_4F_ABGR, (R200_VTX_PK_RGBA << R200_VTX_COLOR_0_SHIFT) );
126 #endif
127 offset += 1;
128
129 rmesa->swtcl.specoffset = 0;
130 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_COLOR1 ) ||
131 RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_FOG )) {
132
133 #if MESA_LITTLE_ENDIAN
134 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_COLOR1 )) {
135 rmesa->swtcl.specoffset = offset;
136 EMIT_ATTR( _TNL_ATTRIB_COLOR1, EMIT_3UB_3F_RGB, (R200_VTX_PK_RGBA << R200_VTX_COLOR_1_SHIFT) );
137 }
138 else {
139 EMIT_PAD( 3 );
140 }
141
142 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_FOG )) {
143 EMIT_ATTR( _TNL_ATTRIB_FOG, EMIT_1UB_1F, (R200_VTX_PK_RGBA << R200_VTX_COLOR_1_SHIFT) );
144 }
145 else {
146 EMIT_PAD( 1 );
147 }
148 #else
149 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_FOG )) {
150 EMIT_ATTR( _TNL_ATTRIB_FOG, EMIT_1UB_1F, (R200_VTX_PK_RGBA << R200_VTX_COLOR_1_SHIFT) );
151 }
152 else {
153 EMIT_PAD( 1 );
154 }
155
156 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_COLOR1 )) {
157 rmesa->swtcl.specoffset = offset;
158 EMIT_ATTR( _TNL_ATTRIB_COLOR1, EMIT_3UB_3F_BGR, (R200_VTX_PK_RGBA << R200_VTX_COLOR_1_SHIFT) );
159 }
160 else {
161 EMIT_PAD( 3 );
162 }
163 #endif
164 }
165
166 if (RENDERINPUTS_TEST_RANGE( index_bitset, _TNL_FIRST_TEX, _TNL_LAST_TEX )) {
167 int i;
168
169 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
170 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_TEX(i) )) {
171 GLuint sz = VB->TexCoordPtr[i]->size;
172
173 fmt_1 |= sz << (3 * i);
174 EMIT_ATTR( _TNL_ATTRIB_TEX0+i, EMIT_1F + sz - 1, 0 );
175 }
176 }
177 }
178
179 if ( (rmesa->hw.ctx.cmd[CTX_PP_FOG_COLOR] & R200_FOG_USE_MASK)
180 != R200_FOG_USE_SPEC_ALPHA ) {
181 R200_STATECHANGE( rmesa, ctx );
182 rmesa->hw.ctx.cmd[CTX_PP_FOG_COLOR] &= ~R200_FOG_USE_MASK;
183 rmesa->hw.ctx.cmd[CTX_PP_FOG_COLOR] |= R200_FOG_USE_SPEC_ALPHA;
184 }
185
186 if (!RENDERINPUTS_EQUAL( rmesa->radeon.tnl_index_bitset, index_bitset ) ||
187 (rmesa->hw.vtx.cmd[VTX_VTXFMT_0] != fmt_0) ||
188 (rmesa->hw.vtx.cmd[VTX_VTXFMT_1] != fmt_1) ) {
189 R200_NEWPRIM(rmesa);
190 R200_STATECHANGE( rmesa, vtx );
191 rmesa->hw.vtx.cmd[VTX_VTXFMT_0] = fmt_0;
192 rmesa->hw.vtx.cmd[VTX_VTXFMT_1] = fmt_1;
193
194 rmesa->radeon.swtcl.vertex_size =
195 _tnl_install_attrs( ctx,
196 rmesa->radeon.swtcl.vertex_attrs,
197 rmesa->radeon.swtcl.vertex_attr_count,
198 NULL, 0 );
199 rmesa->radeon.swtcl.vertex_size /= 4;
200 RENDERINPUTS_COPY( rmesa->radeon.tnl_index_bitset, index_bitset );
201 }
202 }
203
204 static void r200_predict_emit_size( GLcontext *ctx )
205 {
206 r200ContextPtr rmesa = R200_CONTEXT( ctx );
207 const int vertex_array_size = 7;
208 const int prim_size = 3;
209 if (!rmesa->radeon.swtcl.emit_prediction) {
210 const int state_size = radeonCountStateEmitSize(&rmesa->radeon);
211 if (rcommonEnsureCmdBufSpace(&rmesa->radeon,
212 state_size +
213 vertex_array_size + prim_size,
214 __FUNCTION__))
215 rmesa->radeon.swtcl.emit_prediction = radeonCountStateEmitSize(&rmesa->radeon);
216 else
217 rmesa->radeon.swtcl.emit_prediction = state_size;
218 rmesa->radeon.swtcl.emit_prediction += vertex_array_size + prim_size
219 + rmesa->radeon.cmdbuf.cs->cdw;
220 }
221 }
222
223
224 static void r200RenderStart( GLcontext *ctx )
225 {
226 r200SetVertexFormat( ctx );
227 if (RADEON_DEBUG & DEBUG_VERTS)
228 fprintf(stderr, "%s\n", __func__);
229 r200_predict_emit_size( ctx );
230 }
231
232
233 /**
234 * Set vertex state for SW TCL. The primary purpose of this function is to
235 * determine in advance whether or not the hardware can / should do the
236 * projection divide or Mesa should do it.
237 */
238 void r200ChooseVertexState( GLcontext *ctx )
239 {
240 r200ContextPtr rmesa = R200_CONTEXT( ctx );
241 TNLcontext *tnl = TNL_CONTEXT(ctx);
242 GLuint vte;
243 GLuint vap;
244
245 /* We must ensure that we don't do _tnl_need_projected_coords while in a
246 * rasterization fallback. As this function will be called again when we
247 * leave a rasterization fallback, we can just skip it for now.
248 */
249 if (rmesa->radeon.Fallback != 0)
250 return;
251
252 vte = rmesa->hw.vte.cmd[VTE_SE_VTE_CNTL];
253 vap = rmesa->hw.vap.cmd[VAP_SE_VAP_CNTL];
254
255 /* HW perspective divide is a win, but tiny vertex formats are a
256 * bigger one.
257 */
258 if (!RENDERINPUTS_TEST_RANGE( tnl->render_inputs_bitset, _TNL_FIRST_TEX, _TNL_LAST_TEX )
259 || (ctx->_TriangleCaps & (DD_TRI_LIGHT_TWOSIDE|DD_TRI_UNFILLED))) {
260 rmesa->swtcl.needproj = GL_TRUE;
261 vte |= R200_VTX_XY_FMT | R200_VTX_Z_FMT;
262 vte &= ~R200_VTX_W0_FMT;
263 if (RENDERINPUTS_TEST_RANGE( tnl->render_inputs_bitset, _TNL_FIRST_TEX, _TNL_LAST_TEX )) {
264 vap &= ~R200_VAP_FORCE_W_TO_ONE;
265 }
266 else {
267 vap |= R200_VAP_FORCE_W_TO_ONE;
268 }
269 }
270 else {
271 rmesa->swtcl.needproj = GL_FALSE;
272 vte &= ~(R200_VTX_XY_FMT | R200_VTX_Z_FMT);
273 vte |= R200_VTX_W0_FMT;
274 vap &= ~R200_VAP_FORCE_W_TO_ONE;
275 }
276
277 _tnl_need_projected_coords( ctx, rmesa->swtcl.needproj );
278
279 if (vte != rmesa->hw.vte.cmd[VTE_SE_VTE_CNTL]) {
280 R200_STATECHANGE( rmesa, vte );
281 rmesa->hw.vte.cmd[VTE_SE_VTE_CNTL] = vte;
282 }
283
284 if (vap != rmesa->hw.vap.cmd[VAP_SE_VAP_CNTL]) {
285 R200_STATECHANGE( rmesa, vap );
286 rmesa->hw.vap.cmd[VAP_SE_VAP_CNTL] = vap;
287 }
288 }
289
290 void r200_swtcl_flush(GLcontext *ctx, uint32_t current_offset)
291 {
292 r200ContextPtr rmesa = R200_CONTEXT(ctx);
293 if (RADEON_DEBUG & DEBUG_VERTS)
294 fprintf(stderr, "%s\n", __func__);
295
296
297 radeonEmitState(&rmesa->radeon);
298 r200EmitVertexAOS( rmesa,
299 rmesa->radeon.swtcl.vertex_size,
300 first_elem(&rmesa->radeon.dma.reserved)->bo,
301 current_offset);
302
303
304 r200EmitVbufPrim( rmesa,
305 rmesa->radeon.swtcl.hw_primitive,
306 rmesa->radeon.swtcl.numverts);
307 if ( rmesa->radeon.swtcl.emit_prediction < rmesa->radeon.cmdbuf.cs->cdw )
308 WARN_ONCE("Rendering was %d commands larger than predicted size."
309 " We might overflow command buffer.\n",
310 rmesa->radeon.cmdbuf.cs->cdw - rmesa->radeon.swtcl.emit_prediction );
311
312 rmesa->radeon.swtcl.emit_prediction = 0;
313 r200_predict_emit_size( ctx );
314
315 }
316
317 /**************************************************************************/
318
319
320 static INLINE GLuint reduced_hw_prim( GLcontext *ctx, GLuint prim)
321 {
322 switch (prim) {
323 case GL_POINTS:
324 return (ctx->Point.PointSprite ||
325 ((ctx->_TriangleCaps & (DD_POINT_SIZE | DD_POINT_ATTEN)) &&
326 !(ctx->_TriangleCaps & (DD_POINT_SMOOTH)))) ?
327 R200_VF_PRIM_POINT_SPRITES : R200_VF_PRIM_POINTS;
328 case GL_LINES:
329 /* fallthrough */
330 case GL_LINE_LOOP:
331 /* fallthrough */
332 case GL_LINE_STRIP:
333 return R200_VF_PRIM_LINES;
334 default:
335 /* all others reduced to triangles */
336 return R200_VF_PRIM_TRIANGLES;
337 }
338 }
339
340
341 static void r200RasterPrimitive( GLcontext *ctx, GLuint hwprim );
342 static void r200RenderPrimitive( GLcontext *ctx, GLenum prim );
343 static void r200ResetLineStipple( GLcontext *ctx );
344
345 /***********************************************************************
346 * Emit primitives as inline vertices *
347 ***********************************************************************/
348
349 #define HAVE_POINTS 1
350 #define HAVE_LINES 1
351 #define HAVE_LINE_STRIPS 1
352 #define HAVE_TRIANGLES 1
353 #define HAVE_TRI_STRIPS 1
354 #define HAVE_TRI_STRIP_1 0
355 #define HAVE_TRI_FANS 1
356 #define HAVE_QUADS 0
357 #define HAVE_QUAD_STRIPS 0
358 #define HAVE_POLYGONS 1
359 #define HAVE_ELTS 0
360
361 #undef LOCAL_VARS
362 #undef ALLOC_VERTS
363 #define CTX_ARG r200ContextPtr rmesa
364 #define GET_VERTEX_DWORDS() rmesa->radeon.swtcl.vertex_size
365 #define ALLOC_VERTS( n, size ) rcommonAllocDmaLowVerts( &rmesa->radeon, n, size * 4 )
366 #define LOCAL_VARS \
367 r200ContextPtr rmesa = R200_CONTEXT(ctx); \
368 const char *r200verts = (char *)rmesa->radeon.swtcl.verts;
369 #define VERT(x) (radeonVertex *)(r200verts + ((x) * vertsize * sizeof(int)))
370 #define VERTEX radeonVertex
371 #define DO_DEBUG_VERTS (1 && (R200_DEBUG & DEBUG_VERTS))
372
373 #undef TAG
374 #define TAG(x) r200_##x
375 #include "tnl_dd/t_dd_triemit.h"
376
377
378 /***********************************************************************
379 * Macros for t_dd_tritmp.h to draw basic primitives *
380 ***********************************************************************/
381
382 #define QUAD( a, b, c, d ) r200_quad( rmesa, a, b, c, d )
383 #define TRI( a, b, c ) r200_triangle( rmesa, a, b, c )
384 #define LINE( a, b ) r200_line( rmesa, a, b )
385 #define POINT( a ) r200_point( rmesa, a )
386
387 /***********************************************************************
388 * Build render functions from dd templates *
389 ***********************************************************************/
390
391 #define R200_TWOSIDE_BIT 0x01
392 #define R200_UNFILLED_BIT 0x02
393 #define R200_MAX_TRIFUNC 0x04
394
395
396 static struct {
397 tnl_points_func points;
398 tnl_line_func line;
399 tnl_triangle_func triangle;
400 tnl_quad_func quad;
401 } rast_tab[R200_MAX_TRIFUNC];
402
403
404 #define DO_FALLBACK 0
405 #define DO_UNFILLED (IND & R200_UNFILLED_BIT)
406 #define DO_TWOSIDE (IND & R200_TWOSIDE_BIT)
407 #define DO_FLAT 0
408 #define DO_OFFSET 0
409 #define DO_TRI 1
410 #define DO_QUAD 1
411 #define DO_LINE 1
412 #define DO_POINTS 1
413 #define DO_FULL_QUAD 1
414
415 #define HAVE_RGBA 1
416 #define HAVE_SPEC 1
417 #define HAVE_BACK_COLORS 0
418 #define HAVE_HW_FLATSHADE 1
419 #define TAB rast_tab
420
421 #define DEPTH_SCALE 1.0
422 #define UNFILLED_TRI unfilled_tri
423 #define UNFILLED_QUAD unfilled_quad
424 #define VERT_X(_v) _v->v.x
425 #define VERT_Y(_v) _v->v.y
426 #define VERT_Z(_v) _v->v.z
427 #define AREA_IS_CCW( a ) (a < 0)
428 #define GET_VERTEX(e) (rmesa->radeon.swtcl.verts + (e*rmesa->radeon.swtcl.vertex_size*sizeof(int)))
429
430 #define VERT_SET_RGBA( v, c ) \
431 do { \
432 radeon_color_t *color = (radeon_color_t *)&((v)->ui[coloroffset]); \
433 UNCLAMPED_FLOAT_TO_UBYTE(color->red, (c)[0]); \
434 UNCLAMPED_FLOAT_TO_UBYTE(color->green, (c)[1]); \
435 UNCLAMPED_FLOAT_TO_UBYTE(color->blue, (c)[2]); \
436 UNCLAMPED_FLOAT_TO_UBYTE(color->alpha, (c)[3]); \
437 } while (0)
438
439 #define VERT_COPY_RGBA( v0, v1 ) v0->ui[coloroffset] = v1->ui[coloroffset]
440
441 #define VERT_SET_SPEC( v, c ) \
442 do { \
443 if (specoffset) { \
444 radeon_color_t *spec = (radeon_color_t *)&((v)->ui[specoffset]); \
445 UNCLAMPED_FLOAT_TO_UBYTE(spec->red, (c)[0]); \
446 UNCLAMPED_FLOAT_TO_UBYTE(spec->green, (c)[1]); \
447 UNCLAMPED_FLOAT_TO_UBYTE(spec->blue, (c)[2]); \
448 } \
449 } while (0)
450 #define VERT_COPY_SPEC( v0, v1 ) \
451 do { \
452 if (specoffset) { \
453 radeon_color_t *spec0 = (radeon_color_t *)&((v0)->ui[specoffset]); \
454 radeon_color_t *spec1 = (radeon_color_t *)&((v1)->ui[specoffset]); \
455 spec0->red = spec1->red; \
456 spec0->green = spec1->green; \
457 spec0->blue = spec1->blue; \
458 } \
459 } while (0)
460
461 /* These don't need LE32_TO_CPU() as they used to save and restore
462 * colors which are already in the correct format.
463 */
464 #define VERT_SAVE_RGBA( idx ) color[idx] = v[idx]->ui[coloroffset]
465 #define VERT_RESTORE_RGBA( idx ) v[idx]->ui[coloroffset] = color[idx]
466 #define VERT_SAVE_SPEC( idx ) if (specoffset) spec[idx] = v[idx]->ui[specoffset]
467 #define VERT_RESTORE_SPEC( idx ) if (specoffset) v[idx]->ui[specoffset] = spec[idx]
468
469 #undef LOCAL_VARS
470 #undef TAG
471 #undef INIT
472
473 #define LOCAL_VARS(n) \
474 r200ContextPtr rmesa = R200_CONTEXT(ctx); \
475 GLuint color[n] = {0}, spec[n] = {0}; \
476 GLuint coloroffset = rmesa->swtcl.coloroffset; \
477 GLuint specoffset = rmesa->swtcl.specoffset; \
478 (void) color; (void) spec; (void) coloroffset; (void) specoffset;
479
480 /***********************************************************************
481 * Helpers for rendering unfilled primitives *
482 ***********************************************************************/
483
484 #define RASTERIZE(x) r200RasterPrimitive( ctx, reduced_hw_prim(ctx, x) )
485 #define RENDER_PRIMITIVE rmesa->radeon.swtcl.render_primitive
486 #undef TAG
487 #define TAG(x) x
488 #include "tnl_dd/t_dd_unfilled.h"
489 #undef IND
490
491
492 /***********************************************************************
493 * Generate GL render functions *
494 ***********************************************************************/
495
496
497 #define IND (0)
498 #define TAG(x) x
499 #include "tnl_dd/t_dd_tritmp.h"
500
501 #define IND (R200_TWOSIDE_BIT)
502 #define TAG(x) x##_twoside
503 #include "tnl_dd/t_dd_tritmp.h"
504
505 #define IND (R200_UNFILLED_BIT)
506 #define TAG(x) x##_unfilled
507 #include "tnl_dd/t_dd_tritmp.h"
508
509 #define IND (R200_TWOSIDE_BIT|R200_UNFILLED_BIT)
510 #define TAG(x) x##_twoside_unfilled
511 #include "tnl_dd/t_dd_tritmp.h"
512
513
514 static void init_rast_tab( void )
515 {
516 init();
517 init_twoside();
518 init_unfilled();
519 init_twoside_unfilled();
520 }
521
522 /**********************************************************************/
523 /* Render unclipped begin/end objects */
524 /**********************************************************************/
525
526 #define RENDER_POINTS( start, count ) \
527 for ( ; start < count ; start++) \
528 r200_point( rmesa, VERT(start) )
529 #define RENDER_LINE( v0, v1 ) \
530 r200_line( rmesa, VERT(v0), VERT(v1) )
531 #define RENDER_TRI( v0, v1, v2 ) \
532 r200_triangle( rmesa, VERT(v0), VERT(v1), VERT(v2) )
533 #define RENDER_QUAD( v0, v1, v2, v3 ) \
534 r200_quad( rmesa, VERT(v0), VERT(v1), VERT(v2), VERT(v3) )
535 #define INIT(x) do { \
536 r200RenderPrimitive( ctx, x ); \
537 } while (0)
538 #undef LOCAL_VARS
539 #define LOCAL_VARS \
540 r200ContextPtr rmesa = R200_CONTEXT(ctx); \
541 const GLuint vertsize = rmesa->radeon.swtcl.vertex_size; \
542 const char *r200verts = (char *)rmesa->radeon.swtcl.verts; \
543 const GLuint * const elt = TNL_CONTEXT(ctx)->vb.Elts; \
544 const GLboolean stipple = ctx->Line.StippleFlag; \
545 (void) elt; (void) stipple;
546 #define RESET_STIPPLE if ( stipple ) r200ResetLineStipple( ctx );
547 #define RESET_OCCLUSION
548 #define PRESERVE_VB_DEFS
549 #define ELT(x) (x)
550 #define TAG(x) r200_##x##_verts
551 #include "tnl/t_vb_rendertmp.h"
552 #undef ELT
553 #undef TAG
554 #define TAG(x) r200_##x##_elts
555 #define ELT(x) elt[x]
556 #include "tnl/t_vb_rendertmp.h"
557
558
559
560 /**********************************************************************/
561 /* Choose render functions */
562 /**********************************************************************/
563
564 void r200ChooseRenderState( GLcontext *ctx )
565 {
566 TNLcontext *tnl = TNL_CONTEXT(ctx);
567 r200ContextPtr rmesa = R200_CONTEXT(ctx);
568 GLuint index = 0;
569 GLuint flags = ctx->_TriangleCaps;
570
571 if (!rmesa->radeon.TclFallback || rmesa->radeon.Fallback)
572 return;
573
574 if (flags & DD_TRI_LIGHT_TWOSIDE) index |= R200_TWOSIDE_BIT;
575 if (flags & DD_TRI_UNFILLED) index |= R200_UNFILLED_BIT;
576
577 if (index != rmesa->radeon.swtcl.RenderIndex) {
578 tnl->Driver.Render.Points = rast_tab[index].points;
579 tnl->Driver.Render.Line = rast_tab[index].line;
580 tnl->Driver.Render.ClippedLine = rast_tab[index].line;
581 tnl->Driver.Render.Triangle = rast_tab[index].triangle;
582 tnl->Driver.Render.Quad = rast_tab[index].quad;
583
584 if (index == 0) {
585 tnl->Driver.Render.PrimTabVerts = r200_render_tab_verts;
586 tnl->Driver.Render.PrimTabElts = r200_render_tab_elts;
587 tnl->Driver.Render.ClippedPolygon = r200_fast_clipped_poly;
588 } else {
589 tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
590 tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
591 tnl->Driver.Render.ClippedPolygon = _tnl_RenderClippedPolygon;
592 }
593
594 rmesa->radeon.swtcl.RenderIndex = index;
595 }
596 }
597
598
599 /**********************************************************************/
600 /* High level hooks for t_vb_render.c */
601 /**********************************************************************/
602
603
604 static void r200RasterPrimitive( GLcontext *ctx, GLuint hwprim )
605 {
606 r200ContextPtr rmesa = R200_CONTEXT(ctx);
607
608 if (rmesa->radeon.swtcl.hw_primitive != hwprim) {
609 /* need to disable perspective-correct texturing for point sprites */
610 if ((hwprim & 0xf) == R200_VF_PRIM_POINT_SPRITES && ctx->Point.PointSprite) {
611 if (rmesa->hw.set.cmd[SET_RE_CNTL] & R200_PERSPECTIVE_ENABLE) {
612 R200_STATECHANGE( rmesa, set );
613 rmesa->hw.set.cmd[SET_RE_CNTL] &= ~R200_PERSPECTIVE_ENABLE;
614 }
615 }
616 else if (!(rmesa->hw.set.cmd[SET_RE_CNTL] & R200_PERSPECTIVE_ENABLE)) {
617 R200_STATECHANGE( rmesa, set );
618 rmesa->hw.set.cmd[SET_RE_CNTL] |= R200_PERSPECTIVE_ENABLE;
619 }
620 R200_NEWPRIM( rmesa );
621 rmesa->radeon.swtcl.hw_primitive = hwprim;
622 }
623 }
624
625 static void r200RenderPrimitive( GLcontext *ctx, GLenum prim )
626 {
627 r200ContextPtr rmesa = R200_CONTEXT(ctx);
628 rmesa->radeon.swtcl.render_primitive = prim;
629 if (prim < GL_TRIANGLES || !(ctx->_TriangleCaps & DD_TRI_UNFILLED))
630 r200RasterPrimitive( ctx, reduced_hw_prim(ctx, prim) );
631 }
632
633 static void r200RenderFinish( GLcontext *ctx )
634 {
635 }
636
637 static void r200ResetLineStipple( GLcontext *ctx )
638 {
639 r200ContextPtr rmesa = R200_CONTEXT(ctx);
640 R200_STATECHANGE( rmesa, lin );
641 }
642
643
644 /**********************************************************************/
645 /* Transition to/from hardware rasterization. */
646 /**********************************************************************/
647
648 static const char * const fallbackStrings[] = {
649 "Texture mode",
650 "glDrawBuffer(GL_FRONT_AND_BACK)",
651 "glEnable(GL_STENCIL) without hw stencil buffer",
652 "glRenderMode(selection or feedback)",
653 "R200_NO_RAST",
654 "Mixing GL_CLAMP_TO_BORDER and GL_CLAMP (or GL_MIRROR_CLAMP_ATI)"
655 };
656
657
658 static const char *getFallbackString(GLuint bit)
659 {
660 int i = 0;
661 while (bit > 1) {
662 i++;
663 bit >>= 1;
664 }
665 return fallbackStrings[i];
666 }
667
668
669 void r200Fallback( GLcontext *ctx, GLuint bit, GLboolean mode )
670 {
671 r200ContextPtr rmesa = R200_CONTEXT(ctx);
672 TNLcontext *tnl = TNL_CONTEXT(ctx);
673 GLuint oldfallback = rmesa->radeon.Fallback;
674
675 if (mode) {
676 rmesa->radeon.Fallback |= bit;
677 if (oldfallback == 0) {
678 radeon_firevertices(&rmesa->radeon);
679 TCL_FALLBACK( ctx, R200_TCL_FALLBACK_RASTER, GL_TRUE );
680 _swsetup_Wakeup( ctx );
681 rmesa->radeon.swtcl.RenderIndex = ~0;
682 if (R200_DEBUG & DEBUG_FALLBACKS) {
683 fprintf(stderr, "R200 begin rasterization fallback: 0x%x %s\n",
684 bit, getFallbackString(bit));
685 }
686 }
687 }
688 else {
689 rmesa->radeon.Fallback &= ~bit;
690 if (oldfallback == bit) {
691
692 _swrast_flush( ctx );
693 tnl->Driver.Render.Start = r200RenderStart;
694 tnl->Driver.Render.PrimitiveNotify = r200RenderPrimitive;
695 tnl->Driver.Render.Finish = r200RenderFinish;
696
697 tnl->Driver.Render.BuildVertices = _tnl_build_vertices;
698 tnl->Driver.Render.CopyPV = _tnl_copy_pv;
699 tnl->Driver.Render.Interp = _tnl_interp;
700
701 tnl->Driver.Render.ResetLineStipple = r200ResetLineStipple;
702 TCL_FALLBACK( ctx, R200_TCL_FALLBACK_RASTER, GL_FALSE );
703 if (rmesa->radeon.TclFallback) {
704 /* These are already done if rmesa->radeon.TclFallback goes to
705 * zero above. But not if it doesn't (R200_NO_TCL for
706 * example?)
707 */
708 _tnl_invalidate_vertex_state( ctx, ~0 );
709 _tnl_invalidate_vertices( ctx, ~0 );
710 RENDERINPUTS_ZERO( rmesa->radeon.tnl_index_bitset );
711 r200ChooseVertexState( ctx );
712 r200ChooseRenderState( ctx );
713 }
714 if (R200_DEBUG & DEBUG_FALLBACKS) {
715 fprintf(stderr, "R200 end rasterization fallback: 0x%x %s\n",
716 bit, getFallbackString(bit));
717 }
718 }
719 }
720 }
721
722
723
724
725 /**
726 * Cope with depth operations by drawing individual pixels as points.
727 *
728 * \todo
729 * The way the vertex state is set in this routine is hokey. It seems to
730 * work, but it's very hackish. This whole routine is pretty hackish. If
731 * the bitmap is small enough, it seems like it would be faster to copy it
732 * to AGP memory and use it as a non-power-of-two texture (i.e.,
733 * NV_texture_rectangle).
734 */
735 void
736 r200PointsBitmap( GLcontext *ctx, GLint px, GLint py,
737 GLsizei width, GLsizei height,
738 const struct gl_pixelstore_attrib *unpack,
739 const GLubyte *bitmap )
740 {
741 r200ContextPtr rmesa = R200_CONTEXT(ctx);
742 const GLfloat *rc = ctx->Current.RasterColor;
743 GLint row, col;
744 radeonVertex vert;
745 GLuint orig_vte;
746 GLuint h;
747
748
749 /* Turn off tcl.
750 */
751 TCL_FALLBACK( ctx, R200_TCL_FALLBACK_BITMAP, 1 );
752
753 /* Choose tiny vertex format
754 */
755 {
756 const GLuint fmt_0 = R200_VTX_XY | R200_VTX_Z0 | R200_VTX_W0
757 | (R200_VTX_PK_RGBA << R200_VTX_COLOR_0_SHIFT);
758 const GLuint fmt_1 = 0;
759 GLuint vte = rmesa->hw.vte.cmd[VTE_SE_VTE_CNTL];
760 GLuint vap = rmesa->hw.vap.cmd[VAP_SE_VAP_CNTL];
761
762 vte &= ~(R200_VTX_XY_FMT | R200_VTX_Z_FMT);
763 vte |= R200_VTX_W0_FMT;
764 vap &= ~R200_VAP_FORCE_W_TO_ONE;
765
766 rmesa->radeon.swtcl.vertex_size = 5;
767
768 if ( (rmesa->hw.vtx.cmd[VTX_VTXFMT_0] != fmt_0)
769 || (rmesa->hw.vtx.cmd[VTX_VTXFMT_1] != fmt_1) ) {
770 R200_NEWPRIM(rmesa);
771 R200_STATECHANGE( rmesa, vtx );
772 rmesa->hw.vtx.cmd[VTX_VTXFMT_0] = fmt_0;
773 rmesa->hw.vtx.cmd[VTX_VTXFMT_1] = fmt_1;
774 }
775
776 if (vte != rmesa->hw.vte.cmd[VTE_SE_VTE_CNTL]) {
777 R200_STATECHANGE( rmesa, vte );
778 rmesa->hw.vte.cmd[VTE_SE_VTE_CNTL] = vte;
779 }
780
781 if (vap != rmesa->hw.vap.cmd[VAP_SE_VAP_CNTL]) {
782 R200_STATECHANGE( rmesa, vap );
783 rmesa->hw.vap.cmd[VAP_SE_VAP_CNTL] = vap;
784 }
785 }
786
787 /* Ready for point primitives:
788 */
789 r200RenderPrimitive( ctx, GL_POINTS );
790
791 /* Turn off the hw viewport transformation:
792 */
793 R200_STATECHANGE( rmesa, vte );
794 orig_vte = rmesa->hw.vte.cmd[VTE_SE_VTE_CNTL];
795 rmesa->hw.vte.cmd[VTE_SE_VTE_CNTL] &= ~(R200_VPORT_X_SCALE_ENA |
796 R200_VPORT_Y_SCALE_ENA |
797 R200_VPORT_Z_SCALE_ENA |
798 R200_VPORT_X_OFFSET_ENA |
799 R200_VPORT_Y_OFFSET_ENA |
800 R200_VPORT_Z_OFFSET_ENA);
801
802 /* Turn off other stuff: Stipple?, texture?, blending?, etc.
803 */
804
805
806 /* Populate the vertex
807 *
808 * Incorporate FOG into RGBA
809 */
810 if (ctx->Fog.Enabled) {
811 const GLfloat *fc = ctx->Fog.Color;
812 GLfloat color[4];
813 GLfloat f;
814
815 if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT)
816 f = _swrast_z_to_fogfactor(ctx, ctx->Current.Attrib[VERT_ATTRIB_FOG][0]);
817 else
818 f = _swrast_z_to_fogfactor(ctx, ctx->Current.RasterDistance);
819
820 color[0] = f * rc[0] + (1.F - f) * fc[0];
821 color[1] = f * rc[1] + (1.F - f) * fc[1];
822 color[2] = f * rc[2] + (1.F - f) * fc[2];
823 color[3] = rc[3];
824
825 UNCLAMPED_FLOAT_TO_CHAN(vert.tv.color.red, color[0]);
826 UNCLAMPED_FLOAT_TO_CHAN(vert.tv.color.green, color[1]);
827 UNCLAMPED_FLOAT_TO_CHAN(vert.tv.color.blue, color[2]);
828 UNCLAMPED_FLOAT_TO_CHAN(vert.tv.color.alpha, color[3]);
829 }
830 else {
831 UNCLAMPED_FLOAT_TO_CHAN(vert.tv.color.red, rc[0]);
832 UNCLAMPED_FLOAT_TO_CHAN(vert.tv.color.green, rc[1]);
833 UNCLAMPED_FLOAT_TO_CHAN(vert.tv.color.blue, rc[2]);
834 UNCLAMPED_FLOAT_TO_CHAN(vert.tv.color.alpha, rc[3]);
835 }
836
837
838 vert.tv.z = ctx->Current.RasterPos[2];
839
840
841 /* Update window height
842 */
843 LOCK_HARDWARE( &rmesa->radeon );
844 UNLOCK_HARDWARE( &rmesa->radeon );
845 h = radeon_get_drawable(&rmesa->radeon)->h + radeon_get_drawable(&rmesa->radeon)->y;
846 px += radeon_get_drawable(&rmesa->radeon)->x;
847
848 /* Clipping handled by existing mechansims in r200_ioctl.c?
849 */
850 for (row=0; row<height; row++) {
851 const GLubyte *src = (const GLubyte *)
852 _mesa_image_address2d(unpack, bitmap, width, height,
853 GL_COLOR_INDEX, GL_BITMAP, row, 0 );
854
855 if (unpack->LsbFirst) {
856 /* Lsb first */
857 GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
858 for (col=0; col<width; col++) {
859 if (*src & mask) {
860 vert.tv.x = px+col;
861 vert.tv.y = h - (py+row) - 1;
862 r200_point( rmesa, &vert );
863 }
864 src += (mask >> 7);
865 mask = ((mask << 1) & 0xff) | (mask >> 7);
866 }
867
868 /* get ready for next row */
869 if (mask != 1)
870 src++;
871 }
872 else {
873 /* Msb first */
874 GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
875 for (col=0; col<width; col++) {
876 if (*src & mask) {
877 vert.tv.x = px+col;
878 vert.tv.y = h - (py+row) - 1;
879 r200_point( rmesa, &vert );
880 }
881 src += mask & 1;
882 mask = ((mask << 7) & 0xff) | (mask >> 1);
883 }
884 /* get ready for next row */
885 if (mask != 128)
886 src++;
887 }
888 }
889
890 /* Fire outstanding vertices, restore state
891 */
892 R200_STATECHANGE( rmesa, vte );
893 rmesa->hw.vte.cmd[VTE_SE_VTE_CNTL] = orig_vte;
894
895 /* Unfallback
896 */
897 TCL_FALLBACK( ctx, R200_TCL_FALLBACK_BITMAP, 0 );
898
899 /* Need to restore vertexformat?
900 */
901 if (rmesa->radeon.TclFallback)
902 r200ChooseVertexState( ctx );
903 }
904
905
906
907 /**********************************************************************/
908 /* Initialization. */
909 /**********************************************************************/
910
911 void r200InitSwtcl( GLcontext *ctx )
912 {
913 TNLcontext *tnl = TNL_CONTEXT(ctx);
914 r200ContextPtr rmesa = R200_CONTEXT(ctx);
915 static int firsttime = 1;
916
917 if (firsttime) {
918 init_rast_tab();
919 firsttime = 0;
920 }
921 rmesa->radeon.swtcl.emit_prediction = 0;
922
923 tnl->Driver.Render.Start = r200RenderStart;
924 tnl->Driver.Render.Finish = r200RenderFinish;
925 tnl->Driver.Render.PrimitiveNotify = r200RenderPrimitive;
926 tnl->Driver.Render.ResetLineStipple = r200ResetLineStipple;
927 tnl->Driver.Render.BuildVertices = _tnl_build_vertices;
928 tnl->Driver.Render.CopyPV = _tnl_copy_pv;
929 tnl->Driver.Render.Interp = _tnl_interp;
930
931 /* FIXME: what are these numbers? */
932 _tnl_init_vertices( ctx, ctx->Const.MaxArrayLockSize + 12,
933 36 * sizeof(GLfloat) );
934
935 rmesa->radeon.swtcl.verts = (GLubyte *)tnl->clipspace.vertex_buf;
936 rmesa->radeon.swtcl.RenderIndex = ~0;
937 rmesa->radeon.swtcl.render_primitive = GL_TRIANGLES;
938 rmesa->radeon.swtcl.hw_primitive = 0;
939 }
940