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