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