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