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