b02a7cc559ba44cf5e8bcba015f230efe29f0fae
[mesa.git] / src / mesa / drivers / dri / r200 / r200_tcl.c
1 /* $XFree86: xc/lib/GL/mesa/src/drv/r200/r200_tcl.c,v 1.2 2002/12/16 16:18:55 dawes 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 "imports.h"
38 #include "mtypes.h"
39 #include "enums.h"
40 #include "colormac.h"
41 #include "light.h"
42
43 #include "array_cache/acache.h"
44 #include "tnl/tnl.h"
45 #include "tnl/t_pipeline.h"
46
47 #include "r200_context.h"
48 #include "r200_state.h"
49 #include "r200_ioctl.h"
50 #include "r200_tex.h"
51 #include "r200_tcl.h"
52 #include "r200_swtcl.h"
53 #include "r200_maos.h"
54
55
56
57 #define HAVE_POINTS 1
58 #define HAVE_LINES 1
59 #define HAVE_LINE_LOOP 0
60 #define HAVE_LINE_STRIPS 1
61 #define HAVE_TRIANGLES 1
62 #define HAVE_TRI_STRIPS 1
63 #define HAVE_TRI_STRIP_1 0
64 #define HAVE_TRI_FANS 1
65 #define HAVE_QUADS 0 /* hw quad verts in wrong order??? */
66 #define HAVE_QUAD_STRIPS 1
67 #define HAVE_POLYGONS 1
68 #define HAVE_ELTS 1
69
70
71 #define HW_POINTS R200_VF_PRIM_POINTS
72 #define HW_LINES R200_VF_PRIM_LINES
73 #define HW_LINE_LOOP 0
74 #define HW_LINE_STRIP R200_VF_PRIM_LINE_STRIP
75 #define HW_TRIANGLES R200_VF_PRIM_TRIANGLES
76 #define HW_TRIANGLE_STRIP_0 R200_VF_PRIM_TRIANGLE_STRIP
77 #define HW_TRIANGLE_STRIP_1 0
78 #define HW_TRIANGLE_FAN R200_VF_PRIM_TRIANGLE_FAN
79 #define HW_QUADS R200_VF_PRIM_QUADS
80 #define HW_QUAD_STRIP R200_VF_PRIM_QUAD_STRIP
81 #define HW_POLYGON R200_VF_PRIM_POLYGON
82
83
84 static GLboolean discrete_prim[0x10] = {
85 0, /* 0 none */
86 1, /* 1 points */
87 1, /* 2 lines */
88 0, /* 3 line_strip */
89 1, /* 4 tri_list */
90 0, /* 5 tri_fan */
91 0, /* 6 tri_strip */
92 0, /* 7 tri_w_flags */
93 1, /* 8 rect list (unused) */
94 1, /* 9 3vert point */
95 1, /* a 3vert line */
96 0, /* b point sprite */
97 0, /* c line loop */
98 1, /* d quads */
99 0, /* e quad strip */
100 0, /* f polygon */
101 };
102
103
104 #define LOCAL_VARS r200ContextPtr rmesa = R200_CONTEXT(ctx)
105 #define ELT_TYPE GLushort
106
107 #define ELT_INIT(prim, hw_prim) \
108 r200TclPrimitive( ctx, prim, hw_prim | R200_VF_PRIM_WALK_IND )
109
110 #define GET_MESA_ELTS() rmesa->tcl.Elts
111
112
113 /* Don't really know how many elts will fit in what's left of cmdbuf,
114 * as there is state to emit, etc:
115 */
116
117 /* Testing on isosurf shows a maximum around here. Don't know if it's
118 * the card or driver or kernel module that is causing the behaviour.
119 */
120 #define GET_MAX_HW_ELTS() 300
121
122 #define RESET_STIPPLE() do { \
123 R200_STATECHANGE( rmesa, lin ); \
124 r200EmitState( rmesa ); \
125 } while (0)
126
127 #define AUTO_STIPPLE( mode ) do { \
128 R200_STATECHANGE( rmesa, lin ); \
129 if (mode) \
130 rmesa->hw.lin.cmd[LIN_RE_LINE_PATTERN] |= \
131 R200_LINE_PATTERN_AUTO_RESET; \
132 else \
133 rmesa->hw.lin.cmd[LIN_RE_LINE_PATTERN] &= \
134 ~R200_LINE_PATTERN_AUTO_RESET; \
135 r200EmitState( rmesa ); \
136 } while (0)
137
138
139 #define ALLOC_ELTS(nr) r200AllocElts( rmesa, nr )
140
141 static GLushort *r200AllocElts( r200ContextPtr rmesa, GLuint nr )
142 {
143 if (rmesa->dma.flush)
144 rmesa->dma.flush( rmesa );
145
146 r200EmitAOS( rmesa,
147 rmesa->tcl.aos_components,
148 rmesa->tcl.nr_aos_components, 0 );
149
150 return r200AllocEltsOpenEnded( rmesa, rmesa->tcl.hw_primitive, nr );
151 }
152
153
154 #define CLOSE_ELTS() R200_NEWPRIM( rmesa )
155
156
157 /* TODO: Try to extend existing primitive if both are identical,
158 * discrete and there are no intervening state changes. (Somewhat
159 * duplicates changes to DrawArrays code)
160 */
161 static void EMIT_PRIM( GLcontext *ctx,
162 GLenum prim,
163 GLuint hwprim,
164 GLuint start,
165 GLuint count)
166 {
167 r200ContextPtr rmesa = R200_CONTEXT( ctx );
168 r200TclPrimitive( ctx, prim, hwprim );
169
170 r200EmitAOS( rmesa,
171 rmesa->tcl.aos_components,
172 rmesa->tcl.nr_aos_components,
173 start );
174
175 /* Why couldn't this packet have taken an offset param?
176 */
177 r200EmitVbufPrim( rmesa,
178 rmesa->tcl.hw_primitive,
179 count - start );
180 }
181
182
183
184 /* Try & join small primitives
185 */
186 #if 0
187 #define PREFER_DISCRETE_ELT_PRIM( NR, PRIM ) 0
188 #else
189 #define PREFER_DISCRETE_ELT_PRIM( NR, PRIM ) \
190 ((NR) < 20 || \
191 ((NR) < 40 && \
192 rmesa->tcl.hw_primitive == (PRIM| \
193 R200_VF_TCL_OUTPUT_VTX_ENABLE| \
194 R200_VF_PRIM_WALK_IND)))
195 #endif
196
197 #ifdef MESA_BIG_ENDIAN
198 /* We could do without (most of) this ugliness if dest was always 32 bit word aligned... */
199 #define EMIT_ELT(dest, offset, x) do { \
200 int off = offset + ( ( (GLuint)dest & 0x2 ) >> 1 ); \
201 GLushort *des = (GLushort *)( (GLuint)dest & ~0x2 ); \
202 (des)[ off + 1 - 2 * ( off & 1 ) ] = (GLushort)(x); } while (0)
203 #else
204 #define EMIT_ELT(dest, offset, x) (dest)[offset] = (GLushort) (x)
205 #endif
206
207 #define EMIT_TWO_ELTS(dest, offset, x, y) *(GLuint *)((dest)+offset) = ((y)<<16)|(x);
208
209
210
211 #define TAG(x) tcl_##x
212 #include "tnl_dd/t_dd_dmatmp2.h"
213
214 /**********************************************************************/
215 /* External entrypoints */
216 /**********************************************************************/
217
218 void r200EmitPrimitive( GLcontext *ctx,
219 GLuint first,
220 GLuint last,
221 GLuint flags )
222 {
223 tcl_render_tab_verts[flags&PRIM_MODE_MASK]( ctx, first, last, flags );
224 }
225
226 void r200EmitEltPrimitive( GLcontext *ctx,
227 GLuint first,
228 GLuint last,
229 GLuint flags )
230 {
231 tcl_render_tab_elts[flags&PRIM_MODE_MASK]( ctx, first, last, flags );
232 }
233
234 void r200TclPrimitive( GLcontext *ctx,
235 GLenum prim,
236 int hw_prim )
237 {
238 r200ContextPtr rmesa = R200_CONTEXT(ctx);
239 GLuint newprim = hw_prim | R200_VF_TCL_OUTPUT_VTX_ENABLE;
240
241 if (newprim != rmesa->tcl.hw_primitive ||
242 !discrete_prim[hw_prim&0xf]) {
243 R200_NEWPRIM( rmesa );
244 rmesa->tcl.hw_primitive = newprim;
245 }
246 }
247
248
249 /**********************************************************************/
250 /* Render pipeline stage */
251 /**********************************************************************/
252
253
254 /* TCL render.
255 */
256 static GLboolean r200_run_tcl_render( GLcontext *ctx,
257 struct gl_pipeline_stage *stage )
258 {
259 r200ContextPtr rmesa = R200_CONTEXT(ctx);
260 TNLcontext *tnl = TNL_CONTEXT(ctx);
261 struct vertex_buffer *VB = &tnl->vb;
262 GLuint i,flags = 0,length;
263
264 /* TODO: separate this from the swtnl pipeline
265 */
266 if (rmesa->TclFallback)
267 return GL_TRUE; /* fallback to software t&l */
268
269 if (R200_DEBUG & DEBUG_PRIMS)
270 fprintf(stderr, "%s\n", __FUNCTION__);
271
272 if (VB->Count == 0)
273 return GL_FALSE;
274
275 r200ReleaseArrays( ctx, ~0 /* stage->changed_inputs */ );
276 r200EmitArrays( ctx, stage->inputs );
277
278 rmesa->tcl.Elts = VB->Elts;
279
280 for (i = VB->FirstPrimitive ; !(flags & PRIM_LAST) ; i += length)
281 {
282 flags = VB->Primitive[i];
283 length = VB->PrimitiveLength[i];
284
285 if (R200_DEBUG & DEBUG_PRIMS)
286 fprintf(stderr, "%s: prim %s %d..%d\n",
287 __FUNCTION__,
288 _mesa_lookup_enum_by_nr(flags & PRIM_MODE_MASK),
289 i, i+length);
290
291 if (!length)
292 continue;
293
294 if (rmesa->tcl.Elts)
295 r200EmitEltPrimitive( ctx, i, i+length, flags );
296 else
297 r200EmitPrimitive( ctx, i, i+length, flags );
298 }
299
300 return GL_FALSE; /* finished the pipe */
301 }
302
303
304
305 static void r200_check_tcl_render( GLcontext *ctx,
306 struct gl_pipeline_stage *stage )
307 {
308 r200ContextPtr rmesa = R200_CONTEXT(ctx);
309 GLuint inputs = VERT_BIT_POS;
310
311 /* Validate state:
312 */
313 if (rmesa->NewGLState)
314 r200ValidateState( ctx );
315
316 if (ctx->RenderMode == GL_RENDER) {
317 /* Make all this event-driven:
318 */
319 if (ctx->Light.Enabled) {
320 inputs |= VERT_BIT_NORMAL;
321
322 if (1 || ctx->Light.ColorMaterialEnabled) {
323 inputs |= VERT_BIT_COLOR0;
324 }
325 }
326 else {
327 inputs |= VERT_BIT_COLOR0;
328
329 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) {
330 inputs |= VERT_BIT_COLOR1;
331 }
332 }
333
334 if (ctx->Texture.Unit[0]._ReallyEnabled) {
335 if (ctx->Texture.Unit[0].TexGenEnabled) {
336 if (rmesa->TexGenNeedNormals[0]) {
337 inputs |= VERT_BIT_NORMAL;
338 }
339 } else {
340 inputs |= VERT_BIT_TEX0;
341 }
342 }
343
344 if (ctx->Texture.Unit[1]._ReallyEnabled) {
345 if (ctx->Texture.Unit[1].TexGenEnabled) {
346 if (rmesa->TexGenNeedNormals[1]) {
347 inputs |= VERT_BIT_NORMAL;
348 }
349 } else {
350 inputs |= VERT_BIT_TEX1;
351 }
352 }
353
354 stage->inputs = inputs;
355 stage->active = 1;
356 }
357 else
358 stage->active = 0;
359 }
360
361 static void r200_init_tcl_render( GLcontext *ctx,
362 struct gl_pipeline_stage *stage )
363 {
364 stage->check = r200_check_tcl_render;
365 stage->check( ctx, stage );
366 }
367
368 static void dtr( struct gl_pipeline_stage *stage )
369 {
370 (void)stage;
371 }
372
373
374 /* Initial state for tcl stage.
375 */
376 const struct gl_pipeline_stage _r200_tcl_stage =
377 {
378 "r200 render",
379 (_DD_NEW_SEPARATE_SPECULAR |
380 _NEW_LIGHT|
381 _NEW_TEXTURE|
382 _NEW_FOG|
383 _NEW_RENDERMODE), /* re-check (new inputs) */
384 0, /* re-run (always runs) */
385 GL_TRUE, /* active */
386 0, 0, /* inputs (set in check_render), outputs */
387 0, 0, /* changed_inputs, private */
388 dtr, /* destructor */
389 r200_init_tcl_render, /* check - initially set to alloc data */
390 r200_run_tcl_render /* run */
391 };
392
393
394
395 /**********************************************************************/
396 /* Validate state at pipeline start */
397 /**********************************************************************/
398
399
400 /*-----------------------------------------------------------------------
401 * Manage TCL fallbacks
402 */
403
404
405 static void transition_to_swtnl( GLcontext *ctx )
406 {
407 r200ContextPtr rmesa = R200_CONTEXT(ctx);
408 TNLcontext *tnl = TNL_CONTEXT(ctx);
409
410 R200_NEWPRIM( rmesa );
411 rmesa->swtcl.vertex_format = 0;
412
413 r200ChooseVertexState( ctx );
414 r200ChooseRenderState( ctx );
415
416 _mesa_validate_all_lighting_tables( ctx );
417
418 tnl->Driver.NotifyMaterialChange =
419 _mesa_validate_all_lighting_tables;
420
421 r200ReleaseArrays( ctx, ~0 );
422
423 /* Still using the D3D based hardware-rasterizer from the radeon;
424 * need to put the card into D3D mode to make it work:
425 */
426 R200_STATECHANGE( rmesa, vap );
427 rmesa->hw.vap.cmd[VAP_SE_VAP_CNTL] &= ~R200_VAP_TCL_ENABLE;
428 rmesa->hw.vap.cmd[VAP_SE_VAP_CNTL] |= R200_VAP_D3D_TEX_DEFAULT;
429
430 R200_STATECHANGE( rmesa, vte );
431 rmesa->hw.vte.cmd[VTE_SE_VTE_CNTL] &= ~R200_VTX_W0_FMT;
432
433 R200_STATECHANGE( rmesa, set );
434 rmesa->hw.set.cmd[SET_RE_CNTL] |= (R200_VTX_STQ0_D3D |
435 R200_VTX_STQ1_D3D);
436 }
437
438
439 static void transition_to_hwtnl( GLcontext *ctx )
440 {
441 r200ContextPtr rmesa = R200_CONTEXT(ctx);
442 TNLcontext *tnl = TNL_CONTEXT(ctx);
443
444 _tnl_need_projected_coords( ctx, GL_FALSE );
445
446 r200UpdateMaterial( ctx );
447
448 tnl->Driver.NotifyMaterialChange = r200UpdateMaterial;
449
450 if ( rmesa->dma.flush )
451 rmesa->dma.flush( rmesa );
452
453 rmesa->dma.flush = 0;
454 rmesa->swtcl.vertex_format = 0;
455
456 if (rmesa->swtcl.indexed_verts.buf)
457 r200ReleaseDmaRegion( rmesa, &rmesa->swtcl.indexed_verts,
458 __FUNCTION__ );
459
460 R200_STATECHANGE( rmesa, vap );
461 rmesa->hw.vap.cmd[VAP_SE_VAP_CNTL] |= R200_VAP_TCL_ENABLE;
462 rmesa->hw.vap.cmd[VAP_SE_VAP_CNTL] &= ~(R200_VAP_FORCE_W_TO_ONE |
463 R200_VAP_D3D_TEX_DEFAULT);
464
465 R200_STATECHANGE( rmesa, vte );
466 rmesa->hw.vte.cmd[VTE_SE_VTE_CNTL] &= ~(R200_VTX_XY_FMT|R200_VTX_Z_FMT);
467 rmesa->hw.vte.cmd[VTE_SE_VTE_CNTL] |= R200_VTX_W0_FMT;
468
469 R200_STATECHANGE( rmesa, set );
470 rmesa->hw.set.cmd[SET_RE_CNTL] &= ~(R200_VTX_STQ0_D3D |
471 R200_VTX_STQ1_D3D);
472
473
474 if (R200_DEBUG & DEBUG_FALLBACKS)
475 fprintf(stderr, "R200 end tcl fallback\n");
476 }
477
478
479 static char *fallbackStrings[] = {
480 "Rasterization fallback",
481 "Unfilled triangles",
482 "Twosided lighting, differing materials",
483 "Materials in VB (maybe between begin/end)",
484 "Texgen unit 0",
485 "Texgen unit 1",
486 "Texgen unit 2",
487 "User disable"
488 };
489
490
491 static char *getFallbackString(GLuint bit)
492 {
493 int i = 0;
494 while (bit > 1) {
495 i++;
496 bit >>= 1;
497 }
498 return fallbackStrings[i];
499 }
500
501
502
503 void r200TclFallback( GLcontext *ctx, GLuint bit, GLboolean mode )
504 {
505 r200ContextPtr rmesa = R200_CONTEXT(ctx);
506 GLuint oldfallback = rmesa->TclFallback;
507
508 if (mode) {
509 rmesa->TclFallback |= bit;
510 if (oldfallback == 0) {
511 if (R200_DEBUG & DEBUG_FALLBACKS)
512 fprintf(stderr, "R200 begin tcl fallback %s\n",
513 getFallbackString( bit ));
514 transition_to_swtnl( ctx );
515 }
516 }
517 else {
518 rmesa->TclFallback &= ~bit;
519 if (oldfallback == bit) {
520 if (R200_DEBUG & DEBUG_FALLBACKS)
521 fprintf(stderr, "R200 end tcl fallback %s\n",
522 getFallbackString( bit ));
523 transition_to_hwtnl( ctx );
524 }
525 }
526 }