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