2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
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:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
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.
35 #include "swrast/swrast.h"
36 #include "swrast_setup/swrast_setup.h"
37 #include "tnl/t_context.h"
38 #include "tnl/t_pipeline.h"
40 #include "via_context.h"
42 #include "via_state.h"
44 #include "via_ioctl.h"
46 /***********************************************************************
47 * Emit primitives as inline vertices *
48 ***********************************************************************/
51 #define COPY_DWORDS(vb, vertsize, v) \
55 __asm__ __volatile__("rep ; movsl" \
56 : "=%c" (j), "=D" (vb), "=S" (__tmp) \
62 #define COPY_DWORDS(vb, vertsize, v) \
65 for (j = 0; j < vertsize; j++) \
66 vb[j] = ((GLuint *)v)[j]; \
71 static void via_draw_triangle(viaContextPtr vmesa
,
76 GLuint vertsize
= vmesa
->vertexSize
;
77 GLuint
*vb
= viaExtendPrimitive(vmesa
, 3 * 4 * vertsize
);
79 COPY_DWORDS(vb
, vertsize
, v0
);
80 COPY_DWORDS(vb
, vertsize
, v1
);
81 COPY_DWORDS(vb
, vertsize
, v2
);
85 static void via_draw_quad(viaContextPtr vmesa
,
91 GLuint vertsize
= vmesa
->vertexSize
;
92 GLuint
*vb
= viaExtendPrimitive(vmesa
, 6 * 4 * vertsize
);
94 COPY_DWORDS(vb
, vertsize
, v0
);
95 COPY_DWORDS(vb
, vertsize
, v1
);
96 COPY_DWORDS(vb
, vertsize
, v3
);
97 COPY_DWORDS(vb
, vertsize
, v1
);
98 COPY_DWORDS(vb
, vertsize
, v2
);
99 COPY_DWORDS(vb
, vertsize
, v3
);
102 static void via_draw_line(viaContextPtr vmesa
,
106 GLuint vertsize
= vmesa
->vertexSize
;
107 GLuint
*vb
= viaExtendPrimitive(vmesa
, 2 * 4 * vertsize
);
108 COPY_DWORDS(vb
, vertsize
, v0
);
109 COPY_DWORDS(vb
, vertsize
, v1
);
113 static void via_draw_point(viaContextPtr vmesa
,
116 GLuint vertsize
= vmesa
->vertexSize
;
117 GLuint
*vb
= viaExtendPrimitive(vmesa
, 4 * vertsize
);
118 COPY_DWORDS(vb
, vertsize
, v0
);
122 /* Fallback drawing functions for the ptex hack.
124 #define PTEX_VERTEX( tmp, vertex_size, v) \
127 GLfloat rhw = 1.0 / v->f[vertex_size]; \
128 for ( j = 0 ; j < vertex_size ; j++ ) \
129 tmp.f[j] = v->f[j]; \
130 tmp.f[3] *= v->f[vertex_size]; \
131 tmp.f[vertex_size-2] *= rhw; \
132 tmp.f[vertex_size-1] *= rhw; \
135 static void via_ptex_tri (viaContextPtr vmesa
,
140 GLuint vertsize
= vmesa
->hwVertexSize
;
141 GLuint
*vb
= viaExtendPrimitive(vmesa
, 3*4*vertsize
);
144 PTEX_VERTEX(tmp
, vertsize
, v0
); COPY_DWORDS(vb
, vertsize
, &tmp
);
145 PTEX_VERTEX(tmp
, vertsize
, v1
); COPY_DWORDS(vb
, vertsize
, &tmp
);
146 PTEX_VERTEX(tmp
, vertsize
, v2
); COPY_DWORDS(vb
, vertsize
, &tmp
);
149 static void via_ptex_line (viaContextPtr vmesa
,
153 GLuint vertsize
= vmesa
->hwVertexSize
;
154 GLuint
*vb
= viaExtendPrimitive(vmesa
, 2*4*vertsize
);
157 PTEX_VERTEX(tmp
, vertsize
, v0
); COPY_DWORDS(vb
, vertsize
, &tmp
);
158 PTEX_VERTEX(tmp
, vertsize
, v1
); COPY_DWORDS(vb
, vertsize
, &tmp
);
161 static void via_ptex_point (viaContextPtr vmesa
,
164 GLuint vertsize
= vmesa
->hwVertexSize
;
165 GLuint
*vb
= viaExtendPrimitive(vmesa
, 1*4*vertsize
);
168 PTEX_VERTEX(tmp
, vertsize
, v0
); COPY_DWORDS(vb
, vertsize
, &tmp
);
175 /***********************************************************************
176 * Macros for via_dd_tritmp.h to draw basic primitives *
177 ***********************************************************************/
179 #define TRI(a, b, c) \
182 vmesa->drawTri(vmesa, a, b, c); \
184 via_draw_triangle(vmesa, a, b, c); \
187 #define QUAD(a, b, c, d) \
190 vmesa->drawTri(vmesa, a, b, d); \
191 vmesa->drawTri(vmesa, b, c, d); \
194 via_draw_quad(vmesa, a, b, c, d); \
197 #define LINE(v0, v1) \
200 vmesa->drawLine(vmesa, v0, v1); \
202 via_draw_line(vmesa, v0, v1); \
208 vmesa->drawPoint(vmesa, v0); \
210 via_draw_point(vmesa, v0); \
214 /***********************************************************************
215 * Build render functions from dd templates *
216 ***********************************************************************/
218 #define VIA_OFFSET_BIT 0x01
219 #define VIA_TWOSIDE_BIT 0x02
220 #define VIA_UNFILLED_BIT 0x04
221 #define VIA_FALLBACK_BIT 0x08
222 #define VIA_MAX_TRIFUNC 0x10
226 tnl_points_func points
;
228 tnl_triangle_func triangle
;
230 } rast_tab
[VIA_MAX_TRIFUNC
];
233 #define DO_FALLBACK (IND & VIA_FALLBACK_BIT)
234 #define DO_OFFSET (IND & VIA_OFFSET_BIT)
235 #define DO_UNFILLED (IND & VIA_UNFILLED_BIT)
236 #define DO_TWOSIDE (IND & VIA_TWOSIDE_BIT)
242 #define DO_FULL_QUAD 1
246 #define HAVE_BACK_COLORS 0
247 #define HAVE_HW_FLATSHADE 1
248 #define VERTEX viaVertex
251 /* Only used to pull back colors into vertices (ie, we know color is
254 #define VIA_COLOR(dst, src) \
262 #define VIA_SPEC(dst, src) \
270 #define DEPTH_SCALE vmesa->polygon_offset_scale
271 #define UNFILLED_TRI unfilled_tri
272 #define UNFILLED_QUAD unfilled_quad
273 #define VERT_X(_v) _v->v.x
274 #define VERT_Y(_v) _v->v.y
275 #define VERT_Z(_v) _v->v.z
276 #define AREA_IS_CCW(a) (a > 0)
277 #define GET_VERTEX(e) (vmesa->verts + (e * vmesa->vertexSize * sizeof(int)))
279 #define VERT_SET_RGBA( v, c ) \
281 via_color_t *color = (via_color_t *)&((v)->ui[coloroffset]); \
282 UNCLAMPED_FLOAT_TO_UBYTE(color->red, (c)[0]); \
283 UNCLAMPED_FLOAT_TO_UBYTE(color->green, (c)[1]); \
284 UNCLAMPED_FLOAT_TO_UBYTE(color->blue, (c)[2]); \
285 UNCLAMPED_FLOAT_TO_UBYTE(color->alpha, (c)[3]); \
288 #define VERT_COPY_RGBA( v0, v1 ) v0->ui[coloroffset] = v1->ui[coloroffset]
290 #define VERT_SET_SPEC( v0, c ) \
293 UNCLAMPED_FLOAT_TO_UBYTE(v0->v.specular.red, (c)[0]); \
294 UNCLAMPED_FLOAT_TO_UBYTE(v0->v.specular.green, (c)[1]); \
295 UNCLAMPED_FLOAT_TO_UBYTE(v0->v.specular.blue, (c)[2]); \
298 #define VERT_COPY_SPEC( v0, v1 ) \
301 v0->v.specular.red = v1->v.specular.red; \
302 v0->v.specular.green = v1->v.specular.green; \
303 v0->v.specular.blue = v1->v.specular.blue; \
308 #define VERT_SAVE_RGBA( idx ) color[idx] = v[idx]->ui[coloroffset]
309 #define VERT_RESTORE_RGBA( idx ) v[idx]->ui[coloroffset] = color[idx]
310 #define VERT_SAVE_SPEC( idx ) if (specoffset) spec[idx] = v[idx]->ui[specoffset]
311 #define VERT_RESTORE_SPEC( idx ) if (specoffset) v[idx]->ui[specoffset] = spec[idx]
314 #define LOCAL_VARS(n) \
315 viaContextPtr vmesa = VIA_CONTEXT(ctx); \
316 GLuint color[n], spec[n]; \
317 GLuint coloroffset = vmesa->coloroffset; \
318 GLuint specoffset = vmesa->specoffset; \
319 (void)color; (void)spec; (void)coloroffset; (void)specoffset;
322 /***********************************************************************
323 * Helpers for rendering unfilled primitives *
324 ***********************************************************************/
326 static const GLenum hwPrim
[GL_POLYGON
+ 1] = {
340 #define RASTERIZE(x) viaRasterPrimitive( ctx, x, hwPrim[x] )
341 #define RENDER_PRIMITIVE vmesa->renderPrimitive
343 #define IND VIA_FALLBACK_BIT
344 #include "tnl_dd/t_dd_unfilled.h"
348 /***********************************************************************
349 * Generate GL render functions *
350 ***********************************************************************/
355 #include "tnl_dd/t_dd_tritmp.h"
357 #define IND (VIA_OFFSET_BIT)
358 #define TAG(x) x##_offset
359 #include "tnl_dd/t_dd_tritmp.h"
361 #define IND (VIA_TWOSIDE_BIT)
362 #define TAG(x) x##_twoside
363 #include "tnl_dd/t_dd_tritmp.h"
365 #define IND (VIA_TWOSIDE_BIT|VIA_OFFSET_BIT)
366 #define TAG(x) x##_twoside_offset
367 #include "tnl_dd/t_dd_tritmp.h"
369 #define IND (VIA_UNFILLED_BIT)
370 #define TAG(x) x##_unfilled
371 #include "tnl_dd/t_dd_tritmp.h"
373 #define IND (VIA_OFFSET_BIT|VIA_UNFILLED_BIT)
374 #define TAG(x) x##_offset_unfilled
375 #include "tnl_dd/t_dd_tritmp.h"
377 #define IND (VIA_TWOSIDE_BIT|VIA_UNFILLED_BIT)
378 #define TAG(x) x##_twoside_unfilled
379 #include "tnl_dd/t_dd_tritmp.h"
381 #define IND (VIA_TWOSIDE_BIT|VIA_OFFSET_BIT|VIA_UNFILLED_BIT)
382 #define TAG(x) x##_twoside_offset_unfilled
383 #include "tnl_dd/t_dd_tritmp.h"
385 #define IND (VIA_FALLBACK_BIT)
386 #define TAG(x) x##_fallback
387 #include "tnl_dd/t_dd_tritmp.h"
389 #define IND (VIA_OFFSET_BIT|VIA_FALLBACK_BIT)
390 #define TAG(x) x##_offset_fallback
391 #include "tnl_dd/t_dd_tritmp.h"
393 #define IND (VIA_TWOSIDE_BIT|VIA_FALLBACK_BIT)
394 #define TAG(x) x##_twoside_fallback
395 #include "tnl_dd/t_dd_tritmp.h"
397 #define IND (VIA_TWOSIDE_BIT|VIA_OFFSET_BIT|VIA_FALLBACK_BIT)
398 #define TAG(x) x##_twoside_offset_fallback
399 #include "tnl_dd/t_dd_tritmp.h"
401 #define IND (VIA_UNFILLED_BIT|VIA_FALLBACK_BIT)
402 #define TAG(x) x##_unfilled_fallback
403 #include "tnl_dd/t_dd_tritmp.h"
405 #define IND (VIA_OFFSET_BIT|VIA_UNFILLED_BIT|VIA_FALLBACK_BIT)
406 #define TAG(x) x##_offset_unfilled_fallback
407 #include "tnl_dd/t_dd_tritmp.h"
409 #define IND (VIA_TWOSIDE_BIT|VIA_UNFILLED_BIT|VIA_FALLBACK_BIT)
410 #define TAG(x) x##_twoside_unfilled_fallback
411 #include "tnl_dd/t_dd_tritmp.h"
413 #define IND (VIA_TWOSIDE_BIT|VIA_OFFSET_BIT|VIA_UNFILLED_BIT| \
415 #define TAG(x) x##_twoside_offset_unfilled_fallback
416 #include "tnl_dd/t_dd_tritmp.h"
419 static void init_rast_tab(void)
424 init_twoside_offset();
426 init_offset_unfilled();
427 init_twoside_unfilled();
428 init_twoside_offset_unfilled();
430 init_offset_fallback();
431 init_twoside_fallback();
432 init_twoside_offset_fallback();
433 init_unfilled_fallback();
434 init_offset_unfilled_fallback();
435 init_twoside_unfilled_fallback();
436 init_twoside_offset_unfilled_fallback();
440 /***********************************************************************
441 * Rasterization fallback helpers *
442 ***********************************************************************/
445 /* This code is hit only when a mix of accelerated and unaccelerated
446 * primitives are being drawn, and only for the unaccelerated
450 via_fallback_tri(viaContextPtr vmesa
,
455 GLcontext
*ctx
= vmesa
->glCtx
;
457 _swsetup_Translate(ctx
, v0
, &v
[0]);
458 _swsetup_Translate(ctx
, v1
, &v
[1]);
459 _swsetup_Translate(ctx
, v2
, &v
[2]);
460 viaSpanRenderStart( ctx
);
461 _swrast_Triangle(ctx
, &v
[0], &v
[1], &v
[2]);
462 viaSpanRenderFinish( ctx
);
467 via_fallback_line(viaContextPtr vmesa
,
471 GLcontext
*ctx
= vmesa
->glCtx
;
473 _swsetup_Translate(ctx
, v0
, &v
[0]);
474 _swsetup_Translate(ctx
, v1
, &v
[1]);
475 viaSpanRenderStart( ctx
);
476 _swrast_Line(ctx
, &v
[0], &v
[1]);
477 viaSpanRenderFinish( ctx
);
482 via_fallback_point(viaContextPtr vmesa
,
485 GLcontext
*ctx
= vmesa
->glCtx
;
487 _swsetup_Translate(ctx
, v0
, &v
[0]);
488 viaSpanRenderStart( ctx
);
489 _swrast_Point(ctx
, &v
[0]);
490 viaSpanRenderFinish( ctx
);
493 static void viaResetLineStipple( GLcontext
*ctx
)
495 viaContextPtr vmesa
= VIA_CONTEXT(ctx
);
496 vmesa
->regCmdB
|= HC_HLPrst_MASK
;
499 /**********************************************************************/
500 /* Render unclipped begin/end objects */
501 /**********************************************************************/
503 #define V(x) (viaVertex *)(vertptr + ((x) * vertsize * sizeof(int)))
504 #define RENDER_POINTS(start, count) \
505 for (; start < count; start++) POINT(V(ELT(start)));
506 #define RENDER_LINE(v0, v1) LINE(V(v0), V(v1))
507 #define RENDER_TRI( v0, v1, v2) TRI( V(v0), V(v1), V(v2))
508 #define RENDER_QUAD(v0, v1, v2, v3) QUAD(V(v0), V(v1), V(v2), V(v3))
509 #define INIT(x) viaRasterPrimitive(ctx, x, hwPrim[x])
512 viaContextPtr vmesa = VIA_CONTEXT(ctx); \
513 GLubyte *vertptr = (GLubyte *)vmesa->verts; \
514 const GLuint vertsize = vmesa->vertexSize; \
515 const GLuint * const elt = TNL_CONTEXT(ctx)->vb.Elts; \
516 const GLboolean stipple = ctx->Line.StippleFlag; \
517 (void) elt; (void) stipple;
518 #define RESET_STIPPLE if ( stipple ) { printf("RESET\n"); viaResetLineStipple( ctx ); }
519 #define RESET_OCCLUSION
520 #define PRESERVE_VB_DEFS
522 #define TAG(x) via_##x##_verts
523 #include "tnl/t_vb_rendertmp.h"
526 #define TAG(x) via_##x##_elts
527 #define ELT(x) elt[x]
528 #include "tnl/t_vb_rendertmp.h"
531 #undef NEED_EDGEFLAG_SETUP
534 #undef RESET_OCCLUSION
537 /**********************************************************************/
538 /* Render clipped primitives */
539 /**********************************************************************/
543 static void viaRenderClippedPoly(GLcontext
*ctx
, const GLuint
*elts
,
546 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
547 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
548 GLuint prim
= VIA_CONTEXT(ctx
)->renderPrimitive
;
550 /* Render the new vertices as an unclipped polygon.
553 GLuint
*tmp
= VB
->Elts
;
554 VB
->Elts
= (GLuint
*)elts
;
555 tnl
->Driver
.Render
.PrimTabElts
[GL_POLYGON
](ctx
, 0, n
,
556 PRIM_BEGIN
|PRIM_END
);
560 /* Restore the render primitive
562 if (prim
!= GL_POLYGON
)
563 tnl
->Driver
.Render
.PrimitiveNotify( ctx
, prim
);
566 static void viaRenderClippedLine(GLcontext
*ctx
, GLuint ii
, GLuint jj
)
568 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
569 tnl
->Driver
.Render
.Line(ctx
, ii
, jj
);
572 static void viaFastRenderClippedPoly(GLcontext
*ctx
, const GLuint
*elts
,
575 viaContextPtr vmesa
= VIA_CONTEXT(ctx
);
576 GLuint vertsize
= vmesa
->vertexSize
;
577 GLuint
*vb
= viaExtendPrimitive(vmesa
, (n
- 2) * 3 * 4 * vertsize
);
578 GLubyte
*vertptr
= (GLubyte
*)vmesa
->verts
;
579 const GLuint
*start
= (const GLuint
*)V(elts
[0]);
582 for (i
= 2; i
< n
; i
++) {
583 COPY_DWORDS(vb
, vertsize
, V(elts
[i
- 1]));
584 COPY_DWORDS(vb
, vertsize
, V(elts
[i
]));
585 COPY_DWORDS(vb
, vertsize
, start
);
589 /**********************************************************************/
590 /* Choose render functions */
591 /**********************************************************************/
596 #define _VIA_NEW_VERTEX (_NEW_TEXTURE | \
597 _DD_NEW_SEPARATE_SPECULAR | \
598 _DD_NEW_TRI_UNFILLED | \
599 _DD_NEW_TRI_LIGHT_TWOSIDE | \
602 #define _VIA_NEW_RENDERSTATE (_DD_NEW_LINE_STIPPLE | \
603 _DD_NEW_TRI_UNFILLED | \
604 _DD_NEW_TRI_LIGHT_TWOSIDE | \
605 _DD_NEW_TRI_OFFSET | \
606 _DD_NEW_TRI_STIPPLE | \
609 #define LINE_FALLBACK (0)
610 #define POINT_FALLBACK (0)
611 #define TRI_FALLBACK (0)
612 #define ANY_FALLBACK_FLAGS (POINT_FALLBACK|LINE_FALLBACK|TRI_FALLBACK)
613 #define ANY_RASTER_FLAGS (DD_TRI_LIGHT_TWOSIDE|DD_TRI_OFFSET|DD_TRI_UNFILLED)
615 static void viaChooseRenderState(GLcontext
*ctx
)
617 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
618 viaContextPtr vmesa
= VIA_CONTEXT(ctx
);
619 GLuint flags
= ctx
->_TriangleCaps
;
622 if (vmesa
->ptexHack
) {
623 vmesa
->drawPoint
= via_ptex_point
;
624 vmesa
->drawLine
= via_ptex_line
;
625 vmesa
->drawTri
= via_ptex_tri
;
626 index
|= VIA_FALLBACK_BIT
;
629 vmesa
->drawPoint
= via_draw_point
;
630 vmesa
->drawLine
= via_draw_line
;
631 vmesa
->drawTri
= via_draw_triangle
;
634 if (flags
& (ANY_FALLBACK_FLAGS
|ANY_RASTER_FLAGS
)) {
635 if (flags
& DD_TRI_LIGHT_TWOSIDE
) index
|= VIA_TWOSIDE_BIT
;
636 if (flags
& DD_TRI_OFFSET
) index
|= VIA_OFFSET_BIT
;
637 if (flags
& DD_TRI_UNFILLED
) index
|= VIA_UNFILLED_BIT
;
638 if (flags
& ANY_FALLBACK_FLAGS
) index
|= VIA_FALLBACK_BIT
;
640 /* Hook in fallbacks for specific primitives.
642 if (flags
& POINT_FALLBACK
)
643 vmesa
->drawPoint
= via_fallback_point
;
645 if (flags
& LINE_FALLBACK
)
646 vmesa
->drawLine
= via_fallback_line
;
648 if (flags
& TRI_FALLBACK
)
649 vmesa
->drawTri
= via_fallback_tri
;
652 if (vmesa
->renderIndex
!= index
) {
653 vmesa
->renderIndex
= index
;
655 tnl
->Driver
.Render
.Points
= rast_tab
[index
].points
;
656 tnl
->Driver
.Render
.Line
= rast_tab
[index
].line
;
657 tnl
->Driver
.Render
.Triangle
= rast_tab
[index
].triangle
;
658 tnl
->Driver
.Render
.Quad
= rast_tab
[index
].quad
;
661 tnl
->Driver
.Render
.PrimTabVerts
= via_render_tab_verts
;
662 tnl
->Driver
.Render
.PrimTabElts
= via_render_tab_elts
;
663 tnl
->Driver
.Render
.ClippedLine
= line
; /* from tritmp.h */
664 tnl
->Driver
.Render
.ClippedPolygon
= viaFastRenderClippedPoly
;
667 tnl
->Driver
.Render
.PrimTabVerts
= _tnl_render_tab_verts
;
668 tnl
->Driver
.Render
.PrimTabElts
= _tnl_render_tab_elts
;
669 tnl
->Driver
.Render
.ClippedLine
= viaRenderClippedLine
;
670 tnl
->Driver
.Render
.ClippedPolygon
= viaRenderClippedPoly
;
676 #define VIA_EMIT_TEX1 0x01
677 #define VIA_EMIT_TEX0 0x02
678 #define VIA_EMIT_PTEX0 0x04
679 #define VIA_EMIT_RGBA 0x08
680 #define VIA_EMIT_SPEC 0x10
681 #define VIA_EMIT_FOG 0x20
682 #define VIA_EMIT_W 0x40
684 #define EMIT_ATTR( ATTR, STYLE, INDEX, REGB ) \
686 vmesa->vertex_attrs[vmesa->vertex_attr_count].attrib = (ATTR); \
687 vmesa->vertex_attrs[vmesa->vertex_attr_count].format = (STYLE); \
688 vmesa->vertex_attr_count++; \
689 setupIndex |= (INDEX); \
693 #define EMIT_PAD( N ) \
695 vmesa->vertex_attrs[vmesa->vertex_attr_count].attrib = 0; \
696 vmesa->vertex_attrs[vmesa->vertex_attr_count].format = EMIT_PAD; \
697 vmesa->vertex_attrs[vmesa->vertex_attr_count].offset = (N); \
698 vmesa->vertex_attr_count++; \
703 static void viaChooseVertexState( GLcontext
*ctx
)
705 viaContextPtr vmesa
= VIA_CONTEXT(ctx
);
706 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
707 GLuint index
= tnl
->render_inputs
;
708 GLuint regCmdB
= HC_HVPMSK_X
| HC_HVPMSK_Y
| HC_HVPMSK_Z
;
709 GLuint setupIndex
= 0;
711 vmesa
->vertex_attr_count
= 0;
713 /* EMIT_ATTR's must be in order as they tell t_vertex.c how to
714 * build up a hardware vertex.
716 if (index
& (_TNL_BITS_TEX_ANY
|_TNL_BIT_FOG
)) {
717 EMIT_ATTR( _TNL_ATTRIB_POS
, EMIT_4F_VIEWPORT
, VIA_EMIT_W
, HC_HVPMSK_W
);
718 vmesa
->coloroffset
= 4;
721 EMIT_ATTR( _TNL_ATTRIB_POS
, EMIT_3F_VIEWPORT
, 0, 0 );
722 vmesa
->coloroffset
= 3;
725 /* t_context.c always includes a diffuse color */
726 EMIT_ATTR( _TNL_ATTRIB_COLOR0
, EMIT_4UB_4F_BGRA
, VIA_EMIT_RGBA
, HC_HVPMSK_Cd
);
728 vmesa
->specoffset
= 0;
729 if (index
& (_TNL_BIT_COLOR1
|_TNL_BIT_FOG
)) {
730 if ((index
& _TNL_BIT_COLOR1
)) {
731 vmesa
->specoffset
= vmesa
->coloroffset
+ 1;
732 EMIT_ATTR( _TNL_ATTRIB_COLOR1
, EMIT_3UB_3F_BGR
, VIA_EMIT_SPEC
, HC_HVPMSK_Cs
);
737 if ((index
& _TNL_BIT_FOG
))
738 EMIT_ATTR( _TNL_ATTRIB_FOG
, EMIT_1UB_1F
, VIA_EMIT_FOG
, HC_HVPMSK_Cs
);
743 if (index
& _TNL_BIT_TEX(0)) {
745 EMIT_ATTR( _TNL_ATTRIB_TEX0
, EMIT_3F_XYW
, VIA_EMIT_PTEX0
, (HC_HVPMSK_S
| HC_HVPMSK_T
) );
747 EMIT_ATTR( _TNL_ATTRIB_TEX0
, EMIT_2F
, VIA_EMIT_TEX0
, (HC_HVPMSK_S
| HC_HVPMSK_T
) );
750 if (index
& _TNL_BIT_TEX(1)) {
751 EMIT_ATTR( _TNL_ATTRIB_TEX1
, EMIT_2F
, VIA_EMIT_TEX1
, (HC_HVPMSK_S
| HC_HVPMSK_T
) );
754 if (setupIndex
!= vmesa
->setupIndex
) {
755 vmesa
->vertexSize
= _tnl_install_attrs( ctx
,
757 vmesa
->vertex_attr_count
,
758 vmesa
->ViewportMatrix
.m
, 0 );
759 vmesa
->vertexSize
>>= 2;
760 vmesa
->setupIndex
= setupIndex
;
761 vmesa
->regCmdB
&= ~HC_HVPMSK_MASK
;
762 vmesa
->regCmdB
|= regCmdB
;
765 vmesa
->hwVertexSize
= vmesa
->vertexSize
- 1;
767 vmesa
->hwVertexSize
= vmesa
->vertexSize
;
774 /* Check if projective texture coordinates are used and if we can fake
775 * them. Fallback to swrast if we can't. Returns GL_TRUE if projective
776 * texture coordinates must be faked, GL_FALSE otherwise.
778 static GLboolean
viaCheckPTexHack( GLcontext
*ctx
)
780 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
781 struct vertex_buffer
*VB
= &tnl
->vb
;
782 GLuint index
= tnl
->render_inputs
;
783 GLboolean fallback
= GL_FALSE
;
784 GLboolean ptexHack
= GL_FALSE
;
786 if (index
& _TNL_BIT_TEX(0) && VB
->TexCoordPtr
[0]->size
== 4) {
787 if ((index
& _TNL_BITS_TEX_ANY
) == _TNL_BIT_TEX(0))
792 if ((index
& _TNL_BIT_TEX(1)) && VB
->TexCoordPtr
[1]->size
== 4)
795 FALLBACK(VIA_CONTEXT(ctx
), VIA_FALLBACK_PROJ_TEXTURE
, fallback
);
802 /**********************************************************************/
803 /* High level hooks for t_vb_render.c */
804 /**********************************************************************/
807 static void viaRenderStart(GLcontext
*ctx
)
809 viaContextPtr vmesa
= VIA_CONTEXT(ctx
);
810 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
811 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
814 GLboolean ptexHack
= viaCheckPTexHack( ctx
);
815 if (ptexHack
!= vmesa
->ptexHack
) {
816 vmesa
->ptexHack
= ptexHack
;
817 vmesa
->newRenderState
|= _VIA_NEW_RENDERSTATE
;
821 if (vmesa
->newState
) {
822 vmesa
->newRenderState
|= vmesa
->newState
;
823 viaValidateState( ctx
);
826 if (vmesa
->Fallback
) {
827 tnl
->Driver
.Render
.Start(ctx
);
831 if (vmesa
->newRenderState
) {
832 viaChooseVertexState(ctx
);
833 viaChooseRenderState(ctx
);
834 vmesa
->newRenderState
= 0;
839 VB
->AttribPtr
[VERT_ATTRIB_POS
] = VB
->NdcPtr
;
842 static void viaRenderFinish(GLcontext
*ctx
)
844 VIA_FINISH_PRIM(VIA_CONTEXT(ctx
));
848 /* System to flush dma and emit state changes based on the rasterized
851 void viaRasterPrimitive(GLcontext
*ctx
,
855 viaContextPtr vmesa
= VIA_CONTEXT(ctx
);
860 fprintf(stderr
, "%s: %s/%s\n", __FUNCTION__
, _mesa_lookup_enum_by_nr(glprim
),
861 _mesa_lookup_enum_by_nr(hwprim
));
863 vmesa
->renderPrimitive
= glprim
;
865 if (hwprim
!= vmesa
->hwPrimitive
) {
866 VIA_FINISH_PRIM(vmesa
);
868 viaCheckDma( vmesa
, 1024 ); /* Ensure no wrapping inside this function */
870 if (vmesa
->newEmitState
) {
874 vmesa
->regCmdA_End
= HC_ACMD_HCmdA
;
876 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
877 vmesa
->regCmdA_End
|= HC_HShading_Gouraud
;
880 regCmdB
= vmesa
->regCmdB
;
884 vmesa
->regCmdA_End
|= HC_HPMType_Point
| HC_HVCycle_Full
;
885 vmesa
->regCmdA_End
|= HC_HShading_Gouraud
; /* always Gouraud shade points?!? */
888 vmesa
->regCmdA_End
|= HC_HPMType_Line
| HC_HVCycle_Full
;
889 if (ctx
->Light
.ShadeModel
== GL_FLAT
)
890 vmesa
->regCmdA_End
|= HC_HShading_FlatB
;
894 vmesa
->regCmdA_End
|= HC_HPMType_Line
| HC_HVCycle_AFP
|
895 HC_HVCycle_AB
| HC_HVCycle_NewB
;
896 regCmdB
|= HC_HVCycle_AB
| HC_HVCycle_NewB
| HC_HLPrst_MASK
;
897 if (ctx
->Light
.ShadeModel
== GL_FLAT
)
898 vmesa
->regCmdA_End
|= HC_HShading_FlatB
;
901 vmesa
->regCmdA_End
|= HC_HPMType_Tri
| HC_HVCycle_Full
;
902 if (ctx
->Light
.ShadeModel
== GL_FLAT
)
903 vmesa
->regCmdA_End
|= HC_HShading_FlatC
;
905 case GL_TRIANGLE_STRIP
:
906 vmesa
->regCmdA_End
|= HC_HPMType_Tri
| HC_HVCycle_AFP
|
907 HC_HVCycle_AC
| HC_HVCycle_BB
| HC_HVCycle_NewC
;
908 regCmdB
|= HC_HVCycle_AA
| HC_HVCycle_BC
| HC_HVCycle_NewC
;
909 if (ctx
->Light
.ShadeModel
== GL_FLAT
)
910 vmesa
->regCmdA_End
|= HC_HShading_FlatC
;
912 case GL_TRIANGLE_FAN
:
913 vmesa
->regCmdA_End
|= HC_HPMType_Tri
| HC_HVCycle_AFP
|
914 HC_HVCycle_AA
| HC_HVCycle_BC
| HC_HVCycle_NewC
;
915 regCmdB
|= HC_HVCycle_AA
| HC_HVCycle_BC
| HC_HVCycle_NewC
;
916 if (ctx
->Light
.ShadeModel
== GL_FLAT
)
917 vmesa
->regCmdA_End
|= HC_HShading_FlatC
;
926 vmesa
->regCmdA_End
|= HC_HPMType_Tri
| HC_HVCycle_AFP
|
927 HC_HVCycle_AA
| HC_HVCycle_BC
| HC_HVCycle_NewC
;
928 regCmdB
|= HC_HVCycle_AA
| HC_HVCycle_BC
| HC_HVCycle_NewC
;
929 if (ctx
->Light
.ShadeModel
== GL_FLAT
)
930 vmesa
->regCmdA_End
|= HC_HShading_FlatC
;
937 /* assert((vmesa->dmaLow & 0x4) == 0); */
939 if (vmesa
->dmaCliprectAddr
== ~0) {
940 if (VIA_DEBUG
) fprintf(stderr
, "reserve cliprect space at %x\n", vmesa
->dmaLow
);
941 vmesa
->dmaCliprectAddr
= vmesa
->dmaLow
;
943 OUT_RING( HC_HEADER2
);
944 OUT_RING( (HC_ParaType_NotTex
<< 16) );
945 OUT_RING( 0xCCCCCCCC );
946 OUT_RING( 0xCCCCCCCC );
947 OUT_RING( 0xCCCCCCCC );
948 OUT_RING( 0xCCCCCCCC );
949 OUT_RING( 0xCCCCCCCC );
950 OUT_RING( 0xCCCCCCCC );
954 assert(vmesa
->dmaLastPrim
== 0);
957 OUT_RING( HC_HEADER2
);
958 OUT_RING( (HC_ParaType_NotTex
<< 16) );
959 OUT_RING( 0xCCCCCCCC );
960 OUT_RING( 0xDDDDDDDD );
962 OUT_RING( HC_HEADER2
);
963 OUT_RING( (HC_ParaType_CmdVdata
<< 16) );
965 OUT_RING( vmesa
->regCmdA_End
);
968 vmesa
->hwPrimitive
= hwprim
;
969 vmesa
->dmaLastPrim
= vmesa
->dmaLow
;
972 assert(!vmesa
->newEmitState
);
976 /* Callback for mesa:
978 static void viaRenderPrimitive( GLcontext
*ctx
, GLuint prim
)
980 viaRasterPrimitive( ctx
, prim
, hwPrim
[prim
] );
984 void viaFinishPrimitive(viaContextPtr vmesa
)
987 fprintf(stderr
, "%s\n", __FUNCTION__
);
989 if (!vmesa
->dmaLastPrim
|| vmesa
->dmaCliprectAddr
== ~0) {
992 else if (vmesa
->dmaLow
!= vmesa
->dmaLastPrim
) {
993 GLuint cmdA
= vmesa
->regCmdA_End
| HC_HPLEND_MASK
| HC_HPMValidN_MASK
| HC_HE3Fire_MASK
;
996 vmesa
->dmaLastPrim
= 0;
998 /* KW: modified 0x1 to 0x4 below:
1000 if ((vmesa
->dmaLow
& 0x4) || !vmesa
->useAgp
) {
1001 BEGIN_RING_NOCHECK( 1 );
1006 BEGIN_RING_NOCHECK( 2 );
1012 if (vmesa
->dmaLow
> VIA_DMA_HIGHWATER
)
1013 viaFlushDma( vmesa
);
1017 fprintf(stderr
, "remove empty primitive\n");
1019 /* Remove the primitive header:
1021 vmesa
->dmaLastPrim
= 0;
1022 vmesa
->dmaLow
-= 8 * sizeof(GLuint
);
1024 /* Maybe remove the cliprect as well:
1026 if (vmesa
->dmaCliprectAddr
== vmesa
->dmaLow
- 8 * sizeof(GLuint
)) {
1027 vmesa
->dmaLow
-= 8 * sizeof(GLuint
);
1028 vmesa
->dmaCliprectAddr
= ~0;
1032 vmesa
->renderPrimitive
= GL_POLYGON
+ 1;
1033 vmesa
->hwPrimitive
= GL_POLYGON
+ 1;
1034 vmesa
->dmaLastPrim
= 0;
1038 /**********************************************************************/
1039 /* Transition to/from hardware rasterization. */
1040 /**********************************************************************/
1043 void viaFallback(viaContextPtr vmesa
, GLuint bit
, GLboolean mode
)
1045 GLcontext
*ctx
= vmesa
->glCtx
;
1046 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1047 GLuint oldfallback
= vmesa
->Fallback
;
1048 if (VIA_DEBUG
) fprintf(stderr
, "%s old %x bit %x mode %d\n", __FUNCTION__
,
1049 vmesa
->Fallback
, bit
, mode
);
1052 vmesa
->Fallback
|= bit
;
1053 if (oldfallback
== 0) {
1054 VIA_FLUSH_DMA(vmesa
);
1055 _swsetup_Wakeup(ctx
);
1056 vmesa
->renderIndex
= ~0;
1060 vmesa
->Fallback
&= ~bit
;
1061 if (oldfallback
== bit
) {
1062 _swrast_flush( ctx
);
1064 tnl
->Driver
.Render
.Start
= viaRenderStart
;
1065 tnl
->Driver
.Render
.PrimitiveNotify
= viaRenderPrimitive
;
1066 tnl
->Driver
.Render
.Finish
= viaRenderFinish
;
1068 tnl
->Driver
.Render
.BuildVertices
= _tnl_build_vertices
;
1069 tnl
->Driver
.Render
.CopyPV
= _tnl_copy_pv
;
1070 tnl
->Driver
.Render
.Interp
= _tnl_interp
;
1071 tnl
->Driver
.Render
.ResetLineStipple
= viaResetLineStipple
;
1073 _tnl_invalidate_vertex_state( ctx
, ~0 );
1074 _tnl_invalidate_vertices( ctx
, ~0 );
1075 _tnl_install_attrs( ctx
,
1076 vmesa
->vertex_attrs
,
1077 vmesa
->vertex_attr_count
,
1078 vmesa
->ViewportMatrix
.m
, 0 );
1080 vmesa
->newState
|= (_VIA_NEW_RENDERSTATE
|_VIA_NEW_VERTEX
);
1086 /**********************************************************************/
1087 /* Initialization. */
1088 /**********************************************************************/
1091 void viaInitTriFuncs(GLcontext
*ctx
)
1093 viaContextPtr vmesa
= VIA_CONTEXT(ctx
);
1094 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1095 static int firsttime
= 1;
1102 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
1103 tnl
->Driver
.Render
.Start
= viaRenderStart
;
1104 tnl
->Driver
.Render
.Finish
= viaRenderFinish
;
1105 tnl
->Driver
.Render
.PrimitiveNotify
= viaRenderPrimitive
;
1106 tnl
->Driver
.Render
.ResetLineStipple
= viaResetLineStipple
;
1107 tnl
->Driver
.Render
.BuildVertices
= _tnl_build_vertices
;
1108 tnl
->Driver
.Render
.CopyPV
= _tnl_copy_pv
;
1109 tnl
->Driver
.Render
.Interp
= _tnl_interp
;
1111 _tnl_init_vertices( ctx
, ctx
->Const
.MaxArrayLockSize
+ 12,
1112 (6 + 2*ctx
->Const
.MaxTextureUnits
) * sizeof(GLfloat
) );
1114 vmesa
->verts
= (GLubyte
*)tnl
->clipspace
.vertex_buf
;