Large update
[mesa.git] / src / mesa / drivers / dri / unichrome / via_tris.c
1 /*
2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <stdio.h>
26 #include <math.h>
27
28 #include "glheader.h"
29 #include "context.h"
30 #include "mtypes.h"
31 #include "macros.h"
32 #include "colormac.h"
33 #include "enums.h"
34
35 #include "swrast/swrast.h"
36 #include "swrast_setup/swrast_setup.h"
37 #include "tnl/t_context.h"
38 #include "tnl/t_pipeline.h"
39
40 #include "via_context.h"
41 #include "via_tris.h"
42 #include "via_state.h"
43 #include "via_vb.h"
44 #include "via_ioctl.h"
45
46 /***********************************************************************
47 * Emit primitives as inline vertices *
48 ***********************************************************************/
49
50 #if 0
51 #define COPY_DWORDS(vb, vertsize, v) \
52 do { \
53 int j; \
54 int __tmp; \
55 __asm__ __volatile__("rep ; movsl" \
56 : "=%c" (j), "=D" (vb), "=S" (__tmp) \
57 : "0" (vertsize), \
58 "D" ((long)vb), \
59 "S" ((long)v)); \
60 } while (0)
61 #else
62 #define COPY_DWORDS(vb, vertsize, v) \
63 do { \
64 int j; \
65 for (j = 0; j < vertsize; j++) \
66 vb[j] = ((GLuint *)v)[j]; \
67 vb += vertsize; \
68 } while (0)
69 #endif
70
71 static void __inline__ via_draw_triangle(viaContextPtr vmesa,
72 viaVertexPtr v0,
73 viaVertexPtr v1,
74 viaVertexPtr v2)
75 {
76 GLuint vertsize = vmesa->vertexSize;
77 GLuint *vb = viaAllocDma(vmesa, 3 * 4 * vertsize);
78 /* fprintf(stderr, "%s: %p %p %p\n", __FUNCTION__, v0, v1, v2); */
79 COPY_DWORDS(vb, vertsize, v0);
80 COPY_DWORDS(vb, vertsize, v1);
81 COPY_DWORDS(vb, vertsize, v2);
82 }
83
84
85 static void __inline__ via_draw_quad(viaContextPtr vmesa,
86 viaVertexPtr v0,
87 viaVertexPtr v1,
88 viaVertexPtr v2,
89 viaVertexPtr v3)
90 {
91 GLuint vertsize = vmesa->vertexSize;
92 GLuint *vb = viaAllocDma(vmesa, 6 * 4 * vertsize);
93
94 /* fprintf(stderr, "%s: %p %p %p %p\n", __FUNCTION__, v0, v1, v2, v3); */
95 COPY_DWORDS(vb, vertsize, v0);
96 COPY_DWORDS(vb, vertsize, v1);
97 COPY_DWORDS(vb, vertsize, v3);
98 COPY_DWORDS(vb, vertsize, v1);
99 COPY_DWORDS(vb, vertsize, v2);
100 COPY_DWORDS(vb, vertsize, v3);
101 }
102
103 static __inline__ void via_draw_line(viaContextPtr vmesa,
104 viaVertexPtr v0,
105 viaVertexPtr v1)
106 {
107 GLuint vertsize = vmesa->vertexSize;
108 GLuint *vb = viaAllocDma(vmesa, 2 * 4 * vertsize);
109 COPY_DWORDS(vb, vertsize, v0);
110 COPY_DWORDS(vb, vertsize, v1);
111 }
112
113
114 static __inline__ void via_draw_point(viaContextPtr vmesa,
115 viaVertexPtr v0)
116 {
117 GLuint vertsize = vmesa->vertexSize;
118 GLuint *vb = viaAllocDma(vmesa, 4 * vertsize);
119 COPY_DWORDS(vb, vertsize, v0);
120 }
121
122
123
124
125 /***********************************************************************
126 * Macros for via_dd_tritmp.h to draw basic primitives *
127 ***********************************************************************/
128
129 #define TRI(a, b, c) \
130 do { \
131 if (VIA_DEBUG) fprintf(stderr, "hw TRI\n"); \
132 if (DO_FALLBACK) \
133 vmesa->drawTri(vmesa, a, b, c); \
134 else \
135 via_draw_triangle(vmesa, a, b, c); \
136 } while (0)
137
138 #define QUAD(a, b, c, d) \
139 do { \
140 if (VIA_DEBUG) fprintf(stderr, "hw QUAD\n");\
141 if (DO_FALLBACK) { \
142 vmesa->drawTri(vmesa, a, b, d); \
143 vmesa->drawTri(vmesa, b, c, d); \
144 } \
145 else \
146 via_draw_quad(vmesa, a, b, c, d); \
147 } while (0)
148
149 #define LINE(v0, v1) \
150 do { \
151 if(VIA_DEBUG) fprintf(stderr, "hw LINE\n");\
152 if (DO_FALLBACK) \
153 vmesa->drawLine(vmesa, v0, v1); \
154 else \
155 via_draw_line(vmesa, v0, v1); \
156 } while (0)
157
158 #define POINT(v0) \
159 do { \
160 if (VIA_DEBUG) fprintf(stderr, "hw POINT\n");\
161 if (DO_FALLBACK) \
162 vmesa->drawPoint(vmesa, v0); \
163 else \
164 via_draw_point(vmesa, v0); \
165 } while (0)
166
167
168 /***********************************************************************
169 * Build render functions from dd templates *
170 ***********************************************************************/
171
172 #define VIA_OFFSET_BIT 0x01
173 #define VIA_TWOSIDE_BIT 0x02
174 #define VIA_UNFILLED_BIT 0x04
175 #define VIA_FALLBACK_BIT 0x08
176 #define VIA_MAX_TRIFUNC 0x10
177
178
179 static struct {
180 tnl_points_func points;
181 tnl_line_func line;
182 tnl_triangle_func triangle;
183 tnl_quad_func quad;
184 } rast_tab[VIA_MAX_TRIFUNC];
185
186
187 #define DO_FALLBACK (IND & VIA_FALLBACK_BIT)
188 #define DO_OFFSET (IND & VIA_OFFSET_BIT)
189 #define DO_UNFILLED (IND & VIA_UNFILLED_BIT)
190 #define DO_TWOSIDE (IND & VIA_TWOSIDE_BIT)
191 #define DO_FLAT 0
192 #define DO_TRI 1
193 #define DO_QUAD 1
194 #define DO_LINE 1
195 #define DO_POINTS 1
196 #define DO_FULL_QUAD 1
197
198 #define HAVE_RGBA 1
199 #define HAVE_SPEC 1
200 #define HAVE_BACK_COLORS 0
201 #define HAVE_HW_FLATSHADE 1
202 #define VERTEX viaVertex
203 #define TAB rast_tab
204
205 /* Only used to pull back colors into vertices (ie, we know color is
206 * floating point).
207 */
208 #define VIA_COLOR(dst, src) \
209 do { \
210 dst[0] = src[2]; \
211 dst[1] = src[1]; \
212 dst[2] = src[0]; \
213 dst[3] = src[3]; \
214 } while (0)
215
216 #define VIA_SPEC(dst, src) \
217 do { \
218 dst[0] = src[2]; \
219 dst[1] = src[1]; \
220 dst[2] = src[0]; \
221 } while (0)
222
223
224 #define DEPTH_SCALE (1.0 / 0xffff)
225 #define UNFILLED_TRI unfilled_tri
226 #define UNFILLED_QUAD unfilled_quad
227 #define VERT_X(_v) _v->v.x
228 #define VERT_Y(_v) _v->v.y
229 #define VERT_Z(_v) _v->v.z
230 #define AREA_IS_CCW(a) (a > 0)
231 #define GET_VERTEX(e) (vmesa->verts + (e * vmesa->vertexSize * sizeof(int)))
232
233 #define VERT_SET_RGBA(v, c) VIA_COLOR(v->ub4[coloroffset], c)
234 #define VERT_COPY_RGBA(v0, v1) v0->ui[coloroffset] = v1->ui[coloroffset]
235 #define VERT_SAVE_RGBA(idx) color[idx] = v[idx]->ui[coloroffset]
236 #define VERT_RESTORE_RGBA(idx) v[idx]->ui[coloroffset] = color[idx]
237 #define VERT_SET_SPEC(v, c) if (havespec) VIA_SPEC(v->ub4[5], c)
238 #define VERT_COPY_SPEC(v0, v1) if (havespec) COPY_3V(v0->ub4[5], v1->ub4[5])
239 #define VERT_SAVE_SPEC(idx) if (havespec) spec[idx] = v[idx]->ui[5]
240 #define VERT_RESTORE_SPEC(idx) if (havespec) v[idx]->ui[5] = spec[idx]
241
242
243 #define LOCAL_VARS(n) \
244 viaContextPtr vmesa = VIA_CONTEXT(ctx); \
245 GLuint color[n], spec[n]; \
246 GLuint coloroffset = (vmesa->vertexSize == 4 ? 3 : 4); \
247 GLboolean havespec = (vmesa->vertexSize > 4); \
248 (void)color; (void)spec; (void)coloroffset; (void)havespec;
249
250
251 /***********************************************************************
252 * Helpers for rendering unfilled primitives *
253 ***********************************************************************/
254
255 static const GLenum hwPrim[GL_POLYGON + 1] = {
256 GL_POINTS,
257 GL_LINES,
258 GL_LINES,
259 GL_LINES,
260 GL_TRIANGLES,
261 GL_TRIANGLES,
262 GL_TRIANGLES,
263 GL_TRIANGLES,
264 GL_TRIANGLES,
265 GL_TRIANGLES
266 };
267
268
269 #define RASTERIZE(x) viaRasterPrimitive( ctx, x, hwPrim[x] )
270 #define RENDER_PRIMITIVE vmesa->renderPrimitive
271 #define TAG(x) x
272 #define IND VIA_FALLBACK_BIT
273 #include "tnl_dd/t_dd_unfilled.h"
274 #undef IND
275 #undef RASTERIZE
276
277 /***********************************************************************
278 * Generate GL render functions *
279 ***********************************************************************/
280 #define RASTERIZE(x)
281
282 #define IND (0)
283 #define TAG(x) x
284 #include "tnl_dd/t_dd_tritmp.h"
285
286 #define IND (VIA_OFFSET_BIT)
287 #define TAG(x) x##_offset
288 #include "tnl_dd/t_dd_tritmp.h"
289
290 #define IND (VIA_TWOSIDE_BIT)
291 #define TAG(x) x##_twoside
292 #include "tnl_dd/t_dd_tritmp.h"
293
294 #define IND (VIA_TWOSIDE_BIT|VIA_OFFSET_BIT)
295 #define TAG(x) x##_twoside_offset
296 #include "tnl_dd/t_dd_tritmp.h"
297
298 #define IND (VIA_UNFILLED_BIT)
299 #define TAG(x) x##_unfilled
300 #include "tnl_dd/t_dd_tritmp.h"
301
302 #define IND (VIA_OFFSET_BIT|VIA_UNFILLED_BIT)
303 #define TAG(x) x##_offset_unfilled
304 #include "tnl_dd/t_dd_tritmp.h"
305
306 #define IND (VIA_TWOSIDE_BIT|VIA_UNFILLED_BIT)
307 #define TAG(x) x##_twoside_unfilled
308 #include "tnl_dd/t_dd_tritmp.h"
309
310 #define IND (VIA_TWOSIDE_BIT|VIA_OFFSET_BIT|VIA_UNFILLED_BIT)
311 #define TAG(x) x##_twoside_offset_unfilled
312 #include "tnl_dd/t_dd_tritmp.h"
313
314 #define IND (VIA_FALLBACK_BIT)
315 #define TAG(x) x##_fallback
316 #include "tnl_dd/t_dd_tritmp.h"
317
318 #define IND (VIA_OFFSET_BIT|VIA_FALLBACK_BIT)
319 #define TAG(x) x##_offset_fallback
320 #include "tnl_dd/t_dd_tritmp.h"
321
322 #define IND (VIA_TWOSIDE_BIT|VIA_FALLBACK_BIT)
323 #define TAG(x) x##_twoside_fallback
324 #include "tnl_dd/t_dd_tritmp.h"
325
326 #define IND (VIA_TWOSIDE_BIT|VIA_OFFSET_BIT|VIA_FALLBACK_BIT)
327 #define TAG(x) x##_twoside_offset_fallback
328 #include "tnl_dd/t_dd_tritmp.h"
329
330 #define IND (VIA_UNFILLED_BIT|VIA_FALLBACK_BIT)
331 #define TAG(x) x##_unfilled_fallback
332 #include "tnl_dd/t_dd_tritmp.h"
333
334 #define IND (VIA_OFFSET_BIT|VIA_UNFILLED_BIT|VIA_FALLBACK_BIT)
335 #define TAG(x) x##_offset_unfilled_fallback
336 #include "tnl_dd/t_dd_tritmp.h"
337
338 #define IND (VIA_TWOSIDE_BIT|VIA_UNFILLED_BIT|VIA_FALLBACK_BIT)
339 #define TAG(x) x##_twoside_unfilled_fallback
340 #include "tnl_dd/t_dd_tritmp.h"
341
342 #define IND (VIA_TWOSIDE_BIT|VIA_OFFSET_BIT|VIA_UNFILLED_BIT| \
343 VIA_FALLBACK_BIT)
344 #define TAG(x) x##_twoside_offset_unfilled_fallback
345 #include "tnl_dd/t_dd_tritmp.h"
346
347
348 static void init_rast_tab(void)
349 {
350 init();
351 init_offset();
352 init_twoside();
353 init_twoside_offset();
354 init_unfilled();
355 init_offset_unfilled();
356 init_twoside_unfilled();
357 init_twoside_offset_unfilled();
358 init_fallback();
359 init_offset_fallback();
360 init_twoside_fallback();
361 init_twoside_offset_fallback();
362 init_unfilled_fallback();
363 init_offset_unfilled_fallback();
364 init_twoside_unfilled_fallback();
365 init_twoside_offset_unfilled_fallback();
366 }
367
368
369 /***********************************************************************
370 * Rasterization fallback helpers *
371 ***********************************************************************/
372
373
374 /* This code is hit only when a mix of accelerated and unaccelerated
375 * primitives are being drawn, and only for the unaccelerated
376 * primitives.
377 */
378 static void
379 via_fallback_tri(viaContextPtr vmesa,
380 viaVertex *v0,
381 viaVertex *v1,
382 viaVertex *v2)
383 {
384 GLcontext *ctx = vmesa->glCtx;
385 SWvertex v[3];
386 via_translate_vertex(ctx, v0, &v[0]);
387 via_translate_vertex(ctx, v1, &v[1]);
388 via_translate_vertex(ctx, v2, &v[2]);
389 _swrast_Triangle(ctx, &v[0], &v[1], &v[2]);
390 }
391
392
393 static void
394 via_fallback_line(viaContextPtr vmesa,
395 viaVertex *v0,
396 viaVertex *v1)
397 {
398 GLcontext *ctx = vmesa->glCtx;
399 SWvertex v[2];
400 via_translate_vertex(ctx, v0, &v[0]);
401 via_translate_vertex(ctx, v1, &v[1]);
402 _swrast_Line(ctx, &v[0], &v[1]);
403 }
404
405
406 static void
407 via_fallback_point(viaContextPtr vmesa,
408 viaVertex *v0)
409 {
410 GLcontext *ctx = vmesa->glCtx;
411 SWvertex v[1];
412 via_translate_vertex(ctx, v0, &v[0]);
413 _swrast_Point(ctx, &v[0]);
414 }
415
416 /**********************************************************************/
417 /* Render unclipped begin/end objects */
418 /**********************************************************************/
419 #define IND 0
420 #define V(x) (viaVertex *)(vertptr + ((x) * vertsize * sizeof(int)))
421 #define RENDER_POINTS(start, count) \
422 for (; start < count; start++) POINT(V(ELT(start)));
423 #define RENDER_LINE(v0, v1) LINE(V(v0), V(v1))
424 #define RENDER_TRI( v0, v1, v2) TRI( V(v0), V(v1), V(v2))
425 #define RENDER_QUAD(v0, v1, v2, v3) QUAD(V(v0), V(v1), V(v2), V(v3))
426 #define INIT(x) viaRasterPrimitive(ctx, x, hwPrim[x])
427 #undef LOCAL_VARS
428 #define LOCAL_VARS \
429 viaContextPtr vmesa = VIA_CONTEXT(ctx); \
430 GLubyte *vertptr = (GLubyte *)vmesa->verts; \
431 const GLuint vertsize = vmesa->vertexSize; \
432 const GLuint * const elt = TNL_CONTEXT(ctx)->vb.Elts; \
433 (void)elt;
434 #define RESET_STIPPLE
435 #define RESET_OCCLUSION
436 #define PRESERVE_VB_DEFS
437 #define ELT(x) x
438 #define TAG(x) via_fast##x##_verts
439 #include "tnl/t_vb_rendertmp.h"
440 #undef ELT
441 #undef TAG
442 #define TAG(x) via_fast##x##_elts
443 #define ELT(x) elt[x]
444 #include "tnl/t_vb_rendertmp.h"
445 #undef ELT
446 #undef TAG
447 #undef NEED_EDGEFLAG_SETUP
448 #undef EDGEFLAG_GET
449 #undef EDGEFLAG_SET
450 #undef RESET_OCCLUSION
451
452
453 /**********************************************************************/
454 /* Render clipped primitives */
455 /**********************************************************************/
456
457
458
459 static void viaRenderClippedPoly(GLcontext *ctx, const GLuint *elts,
460 GLuint n)
461 {
462 TNLcontext *tnl = TNL_CONTEXT(ctx);
463 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
464 GLuint prim = VIA_CONTEXT(ctx)->renderPrimitive;
465
466 /* Render the new vertices as an unclipped polygon.
467 */
468 {
469 GLuint *tmp = VB->Elts;
470 VB->Elts = (GLuint *)elts;
471 tnl->Driver.Render.PrimTabElts[GL_POLYGON](ctx, 0, n,
472 PRIM_BEGIN|PRIM_END);
473 VB->Elts = tmp;
474 }
475
476 /* Restore the render primitive
477 */
478 if (prim != GL_POLYGON)
479 tnl->Driver.Render.PrimitiveNotify( ctx, prim );
480 }
481
482 static void viaRenderClippedLine(GLcontext *ctx, GLuint ii, GLuint jj)
483 {
484 TNLcontext *tnl = TNL_CONTEXT(ctx);
485 tnl->Driver.Render.Line(ctx, ii, jj);
486 }
487
488 static void viaFastRenderClippedPoly(GLcontext *ctx, const GLuint *elts,
489 GLuint n)
490 {
491 viaContextPtr vmesa = VIA_CONTEXT(ctx);
492 GLuint vertsize = vmesa->vertexSize;
493 GLuint *vb = viaAllocDma(vmesa, (n - 2) * 3 * 4 * vertsize);
494 GLubyte *vertptr = (GLubyte *)vmesa->verts;
495 const GLuint *start = (const GLuint *)V(elts[0]);
496 int i;
497
498 for (i = 2; i < n; i++) {
499 COPY_DWORDS(vb, vertsize, V(elts[i - 1]));
500 COPY_DWORDS(vb, vertsize, V(elts[i]));
501 COPY_DWORDS(vb, vertsize, start);
502 }
503 }
504
505 /**********************************************************************/
506 /* Choose render functions */
507 /**********************************************************************/
508
509
510
511 #define _VIA_NEW_RENDERSTATE (_DD_NEW_LINE_STIPPLE | \
512 _DD_NEW_TRI_UNFILLED | \
513 _DD_NEW_TRI_LIGHT_TWOSIDE | \
514 _DD_NEW_TRI_OFFSET | \
515 _DD_NEW_TRI_STIPPLE | \
516 _NEW_POLYGONSTIPPLE)
517
518 #define POINT_FALLBACK (0)
519 /*#define LINE_FALLBACK (DD_LINE_STIPPLE)
520 */
521 #define LINE_FALLBACK (0)
522 #define TRI_FALLBACK (0)
523 #define ANY_FALLBACK_FLAGS (POINT_FALLBACK|LINE_FALLBACK|TRI_FALLBACK)
524 #define ANY_RASTER_FLAGS (DD_TRI_LIGHT_TWOSIDE|DD_TRI_OFFSET|DD_TRI_UNFILLED)
525
526 static void viaChooseRenderState(GLcontext *ctx)
527 {
528 TNLcontext *tnl = TNL_CONTEXT(ctx);
529 viaContextPtr vmesa = VIA_CONTEXT(ctx);
530 GLuint flags = ctx->_TriangleCaps;
531 GLuint index = 0;
532 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
533
534 if (VIA_DEBUG) fprintf(stderr, "_TriangleCaps = %x\n", flags);
535 if (flags & (ANY_FALLBACK_FLAGS|ANY_RASTER_FLAGS)) {
536 if (flags & ANY_RASTER_FLAGS) {
537 if (flags & DD_TRI_LIGHT_TWOSIDE) index |= VIA_TWOSIDE_BIT;
538 if (flags & DD_TRI_OFFSET) index |= VIA_OFFSET_BIT;
539 if (flags & DD_TRI_UNFILLED) index |= VIA_UNFILLED_BIT;
540 }
541
542 vmesa->drawPoint = via_draw_point;
543 vmesa->drawLine = via_draw_line;
544 vmesa->drawTri = via_draw_triangle;
545
546 /* Hook in fallbacks for specific primitives.
547 */
548 if (flags & ANY_FALLBACK_FLAGS) {
549 if (flags & POINT_FALLBACK)
550 vmesa->drawPoint = via_fallback_point;
551
552 if (flags & LINE_FALLBACK)
553 vmesa->drawLine = via_fallback_line;
554
555 if (flags & TRI_FALLBACK)
556 vmesa->drawTri = via_fallback_tri;
557
558 index |= VIA_FALLBACK_BIT;
559 }
560 }
561 if (VIA_DEBUG) {
562 fprintf(stderr, "index = %x\n", index);
563 fprintf(stderr, "renderIndex = %x\n", vmesa->renderIndex);
564 }
565 if (vmesa->renderIndex != index) {
566 vmesa->renderIndex = index;
567
568 tnl->Driver.Render.Points = rast_tab[index].points;
569 tnl->Driver.Render.Line = rast_tab[index].line;
570 tnl->Driver.Render.Triangle = rast_tab[index].triangle;
571
572 tnl->Driver.Render.Quad = rast_tab[index].quad;
573
574 if (index == 0) {
575 tnl->Driver.Render.PrimTabVerts = via_fastrender_tab_verts;
576 tnl->Driver.Render.PrimTabElts = via_fastrender_tab_elts;
577 tnl->Driver.Render.ClippedLine = line; /* from tritmp.h */
578 tnl->Driver.Render.ClippedPolygon = viaFastRenderClippedPoly;
579 }
580 else {
581 tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
582 tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
583 tnl->Driver.Render.ClippedLine = viaRenderClippedLine;
584 tnl->Driver.Render.ClippedPolygon = viaRenderClippedPoly;
585 }
586 }
587 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
588 }
589
590
591
592
593
594 /**********************************************************************/
595 /* High level hooks for t_vb_render.c */
596 /**********************************************************************/
597
598 static void viaRunPipeline(GLcontext *ctx)
599 {
600 viaContextPtr vmesa = VIA_CONTEXT(ctx);
601
602 if (vmesa->newState) {
603 viaValidateState( ctx );
604
605 if (!vmesa->Fallback) {
606 viaChooseVertexState(ctx);
607 viaChooseRenderState(ctx);
608 }
609 }
610
611 _tnl_run_pipeline(ctx);
612 }
613
614 static void viaRenderStart(GLcontext *ctx)
615 {
616 /* Check for projective texturing. Make sure all texcoord
617 * pointers point to something. (fix in mesa?)
618 */
619 viaCheckTexSizes(ctx);
620 }
621
622 static void viaRenderFinish(GLcontext *ctx)
623 {
624 if (VIA_CONTEXT(ctx)->renderIndex & VIA_FALLBACK_BIT)
625 _swrast_flush(ctx);
626 else
627 VIA_FINISH_PRIM(VIA_CONTEXT(ctx));
628 }
629
630
631
632 /* System to flush dma and emit state changes based on the rasterized
633 * primitive.
634 */
635 void viaRasterPrimitive(GLcontext *ctx,
636 GLenum glprim,
637 GLenum hwprim)
638 {
639 viaContextPtr vmesa = VIA_CONTEXT(ctx);
640 GLuint regCmdB;
641 RING_VARS;
642
643 if (VIA_DEBUG)
644 fprintf(stderr, "%s: %s/%s\n", __FUNCTION__, _mesa_lookup_enum_by_nr(glprim),
645 _mesa_lookup_enum_by_nr(hwprim));
646
647 VIA_FINISH_PRIM(vmesa);
648
649 vmesa->renderPrimitive = glprim;
650
651 regCmdB = vmesa->regCmdB;
652
653 switch (hwprim) {
654 case GL_POINTS:
655 vmesa->regCmdA_End = vmesa->regCmdA | HC_HPMType_Point | HC_HVCycle_Full;
656 if (ctx->Light.ShadeModel == GL_FLAT)
657 vmesa->regCmdA_End |= HC_HShading_FlatA;
658 break;
659 case GL_LINES:
660 vmesa->regCmdA_End = vmesa->regCmdA | HC_HPMType_Line | HC_HVCycle_Full;
661 if (ctx->Light.ShadeModel == GL_FLAT)
662 vmesa->regCmdA_End |= HC_HShading_FlatB;
663 break;
664 case GL_LINE_LOOP:
665 case GL_LINE_STRIP:
666 vmesa->regCmdA_End = vmesa->regCmdA | HC_HPMType_Line | HC_HVCycle_AFP |
667 HC_HVCycle_AB | HC_HVCycle_NewB;
668 regCmdB |= HC_HVCycle_AB | HC_HVCycle_NewB | HC_HLPrst_MASK;
669 if (ctx->Light.ShadeModel == GL_FLAT)
670 vmesa->regCmdA_End |= HC_HShading_FlatB;
671 break;
672 case GL_TRIANGLES:
673 vmesa->regCmdA_End = vmesa->regCmdA | HC_HPMType_Tri | HC_HVCycle_Full;
674 if (ctx->Light.ShadeModel == GL_FLAT)
675 vmesa->regCmdA_End |= HC_HShading_FlatC;
676 break;
677 case GL_TRIANGLE_STRIP:
678 vmesa->regCmdA_End = vmesa->regCmdA | HC_HPMType_Tri | HC_HVCycle_AFP |
679 HC_HVCycle_AC | HC_HVCycle_BB | HC_HVCycle_NewC;
680 regCmdB |= HC_HVCycle_AA | HC_HVCycle_BC | HC_HVCycle_NewC;
681 if (ctx->Light.ShadeModel == GL_FLAT)
682 vmesa->regCmdA_End |= HC_HShading_FlatB;
683 break;
684 case GL_TRIANGLE_FAN:
685 vmesa->regCmdA_End = vmesa->regCmdA | HC_HPMType_Tri | HC_HVCycle_AFP |
686 HC_HVCycle_AA | HC_HVCycle_BC | HC_HVCycle_NewC;
687 regCmdB |= HC_HVCycle_AA | HC_HVCycle_BC | HC_HVCycle_NewC;
688 if (ctx->Light.ShadeModel == GL_FLAT)
689 vmesa->regCmdA_End |= HC_HShading_FlatC;
690 break;
691 case GL_QUADS:
692 abort();
693 return;
694 case GL_QUAD_STRIP:
695 abort();
696 return;
697 case GL_POLYGON:
698 vmesa->regCmdA_End = vmesa->regCmdA | HC_HPMType_Tri | HC_HVCycle_AFP |
699 HC_HVCycle_AA | HC_HVCycle_BC | HC_HVCycle_NewC;
700 regCmdB |= HC_HVCycle_AA | HC_HVCycle_BC | HC_HVCycle_NewC;
701 if (ctx->Light.ShadeModel == GL_FLAT)
702 vmesa->regCmdA_End |= HC_HShading_FlatC;
703 break;
704 default:
705 abort();
706 return;
707 }
708
709 assert((vmesa->dmaLow & 0x4) == 0);
710
711 BEGIN_RING(8);
712 OUT_RING( HC_HEADER2 );
713 OUT_RING( (HC_ParaType_NotTex << 16) );
714 OUT_RING( 0xCCCCCCCC );
715 OUT_RING( 0xDDDDDDDD );
716
717 OUT_RING( HC_HEADER2 );
718 OUT_RING( (HC_ParaType_CmdVdata << 16) );
719 OUT_RING( regCmdB );
720 OUT_RING( vmesa->regCmdA_End );
721 ADVANCE_RING();
722
723 vmesa->dmaLastPrim = vmesa->dmaLow;
724 vmesa->hwPrimitive = hwprim;
725 }
726
727 /* Callback for mesa:
728 */
729 static void viaRenderPrimitive( GLcontext *ctx, GLuint prim )
730 {
731 viaRasterPrimitive( ctx, prim, hwPrim[prim] );
732 }
733
734
735 void viaFinishPrimitive(viaContextPtr vmesa)
736 {
737 if (!vmesa->dmaLastPrim) {
738 return;
739 }
740 else if (vmesa->dmaLow != vmesa->dmaLastPrim) {
741 GLuint cmdA = vmesa->regCmdA_End | HC_HPLEND_MASK | HC_HPMValidN_MASK | HC_HE3Fire_MASK;
742 RING_VARS;
743
744 /* KW: modified 0x1 to 0x4 below:
745 */
746 if ((vmesa->dmaLow & 0x1) || !vmesa->useAgp) {
747 BEGIN_RING_NOCHECK( 1 );
748 OUT_RING( cmdA );
749 ADVANCE_RING();
750 }
751 else {
752 BEGIN_RING_NOCHECK( 2 );
753 OUT_RING( cmdA );
754 OUT_RING( cmdA );
755 ADVANCE_RING();
756 }
757 vmesa->dmaLastPrim = 0;
758
759 if (1 || vmesa->dmaLow > VIA_DMA_HIGHWATER)
760 viaFlushPrims( vmesa );
761 }
762 else {
763 assert(vmesa->dmaLow >= (32 + DMA_OFFSET));
764 vmesa->dmaLow -= 32;
765 vmesa->dmaLastPrim = 0;
766 }
767 }
768
769
770 /**********************************************************************/
771 /* Transition to/from hardware rasterization. */
772 /**********************************************************************/
773
774
775 void viaFallback(viaContextPtr vmesa, GLuint bit, GLboolean mode)
776 {
777 GLcontext *ctx = vmesa->glCtx;
778 TNLcontext *tnl = TNL_CONTEXT(ctx);
779 GLuint oldfallback = vmesa->Fallback;
780 if (VIA_DEBUG) fprintf(stderr, "%s old %x bit %x mode %d\n", __FUNCTION__,
781 vmesa->Fallback, bit, mode);
782
783 if (mode) {
784 vmesa->Fallback |= bit;
785 if (oldfallback == 0) {
786 if (VIA_DEBUG) fprintf(stderr, "ENTER FALLBACK\n");
787 VIA_FLUSH_DMA(vmesa);
788 _swsetup_Wakeup(ctx);
789 vmesa->renderIndex = ~0;
790 }
791 }
792 else {
793 vmesa->Fallback &= ~bit;
794 if (oldfallback == bit) {
795 if (VIA_DEBUG) fprintf(stderr, "LEAVE FALLBACK\n");
796 tnl->Driver.Render.Start = viaRenderStart;
797 tnl->Driver.Render.PrimitiveNotify = viaRenderPrimitive;
798 tnl->Driver.Render.Finish = viaRenderFinish;
799 tnl->Driver.Render.BuildVertices = viaBuildVertices;
800 vmesa->newState |= (_VIA_NEW_RENDERSTATE|_VIA_NEW_VERTEX);
801 }
802 }
803
804 }
805
806
807 /**********************************************************************/
808 /* Initialization. */
809 /**********************************************************************/
810
811
812 void viaInitTriFuncs(GLcontext *ctx)
813 {
814 TNLcontext *tnl = TNL_CONTEXT(ctx);
815 static int firsttime = 1;
816
817 if (firsttime) {
818 init_rast_tab();
819 firsttime = 0;
820 }
821
822 tnl->Driver.RunPipeline = viaRunPipeline;
823 tnl->Driver.Render.Start = viaRenderStart;
824 tnl->Driver.Render.Finish = viaRenderFinish;
825 tnl->Driver.Render.PrimitiveNotify = viaRenderPrimitive;
826 tnl->Driver.Render.ResetLineStipple = _swrast_ResetLineStipple;
827 tnl->Driver.Render.BuildVertices = viaBuildVertices;
828 }