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