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