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