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