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.
28 #include "main/glheader.h"
29 #include "main/context.h"
30 #include "main/mtypes.h"
31 #include "main/macros.h"
32 #include "main/colormac.h"
33 #include "main/enums.h"
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"
45 #include "via_3d_reg.h"
48 /***********************************************************************
49 * Emit primitives as inline vertices *
50 ***********************************************************************/
51 #define LINE_FALLBACK (0)
52 #define POINT_FALLBACK (0)
53 #define TRI_FALLBACK (0)
54 #define ANY_FALLBACK_FLAGS (POINT_FALLBACK|LINE_FALLBACK|TRI_FALLBACK)
55 #define ANY_RASTER_FLAGS (DD_TRI_LIGHT_TWOSIDE|DD_TRI_OFFSET|DD_TRI_UNFILLED)
59 #define COPY_DWORDS(vb, vertsize, v) \
61 via_sse_memcpy(vb, v, vertsize * 4); \
65 #if defined( USE_X86_ASM )
66 #define COPY_DWORDS(vb, vertsize, v) \
70 __asm__ __volatile__("rep ; movsl" \
71 : "=%c" (j), "=D" (vb), "=S" (__tmp) \
77 #define COPY_DWORDS(vb, vertsize, v) \
80 for (j = 0; j < vertsize; j++) \
81 vb[j] = ((GLuint *)v)[j]; \
87 static void via_draw_triangle(struct via_context
*vmesa
,
92 GLuint vertsize
= vmesa
->vertexSize
;
93 GLuint
*vb
= viaExtendPrimitive(vmesa
, 3 * 4 * vertsize
);
95 COPY_DWORDS(vb
, vertsize
, v0
);
96 COPY_DWORDS(vb
, vertsize
, v1
);
97 COPY_DWORDS(vb
, vertsize
, v2
);
101 static void via_draw_quad(struct via_context
*vmesa
,
107 GLuint vertsize
= vmesa
->vertexSize
;
108 GLuint
*vb
= viaExtendPrimitive(vmesa
, 6 * 4 * vertsize
);
110 COPY_DWORDS(vb
, vertsize
, v0
);
111 COPY_DWORDS(vb
, vertsize
, v1
);
112 COPY_DWORDS(vb
, vertsize
, v3
);
113 COPY_DWORDS(vb
, vertsize
, v1
);
114 COPY_DWORDS(vb
, vertsize
, v2
);
115 COPY_DWORDS(vb
, vertsize
, v3
);
118 static void via_draw_line(struct via_context
*vmesa
,
122 GLuint vertsize
= vmesa
->vertexSize
;
123 GLuint
*vb
= viaExtendPrimitive(vmesa
, 2 * 4 * vertsize
);
124 COPY_DWORDS(vb
, vertsize
, v0
);
125 COPY_DWORDS(vb
, vertsize
, v1
);
129 static void via_draw_point(struct via_context
*vmesa
,
132 GLuint vertsize
= vmesa
->vertexSize
;
133 GLuint
*vb
= viaExtendPrimitive(vmesa
, 4 * vertsize
);
134 COPY_DWORDS(vb
, vertsize
, v0
);
138 /* Fallback drawing functions for the ptex hack.
140 #define PTEX_VERTEX( tmp, vertex_size, v) \
143 GLfloat rhw = 1.0 / v->f[vertex_size]; \
144 for ( j = 0 ; j < vertex_size ; j++ ) \
145 tmp.f[j] = v->f[j]; \
146 tmp.f[3] *= v->f[vertex_size]; \
147 tmp.f[vertex_size-2] *= rhw; \
148 tmp.f[vertex_size-1] *= rhw; \
151 static void via_ptex_tri (struct via_context
*vmesa
,
156 GLuint vertsize
= vmesa
->hwVertexSize
;
157 GLuint
*vb
= viaExtendPrimitive(vmesa
, 3*4*vertsize
);
160 PTEX_VERTEX(tmp
, vertsize
, v0
); COPY_DWORDS(vb
, vertsize
, &tmp
);
161 PTEX_VERTEX(tmp
, vertsize
, v1
); COPY_DWORDS(vb
, vertsize
, &tmp
);
162 PTEX_VERTEX(tmp
, vertsize
, v2
); COPY_DWORDS(vb
, vertsize
, &tmp
);
165 static void via_ptex_line (struct via_context
*vmesa
,
169 GLuint vertsize
= vmesa
->hwVertexSize
;
170 GLuint
*vb
= viaExtendPrimitive(vmesa
, 2*4*vertsize
);
173 PTEX_VERTEX(tmp
, vertsize
, v0
); COPY_DWORDS(vb
, vertsize
, &tmp
);
174 PTEX_VERTEX(tmp
, vertsize
, v1
); COPY_DWORDS(vb
, vertsize
, &tmp
);
177 static void via_ptex_point (struct via_context
*vmesa
,
180 GLuint vertsize
= vmesa
->hwVertexSize
;
181 GLuint
*vb
= viaExtendPrimitive(vmesa
, 1*4*vertsize
);
184 PTEX_VERTEX(tmp
, vertsize
, v0
); COPY_DWORDS(vb
, vertsize
, &tmp
);
191 /***********************************************************************
192 * Macros for via_dd_tritmp.h to draw basic primitives *
193 ***********************************************************************/
195 #define TRI(a, b, c) \
198 vmesa->drawTri(vmesa, a, b, c); \
200 via_draw_triangle(vmesa, a, b, c); \
203 #define QUAD(a, b, c, d) \
206 vmesa->drawTri(vmesa, a, b, d); \
207 vmesa->drawTri(vmesa, b, c, d); \
210 via_draw_quad(vmesa, a, b, c, d); \
213 #define LINE(v0, v1) \
216 vmesa->drawLine(vmesa, v0, v1); \
218 via_draw_line(vmesa, v0, v1); \
224 vmesa->drawPoint(vmesa, v0); \
226 via_draw_point(vmesa, v0); \
230 /***********************************************************************
231 * Build render functions from dd templates *
232 ***********************************************************************/
234 #define VIA_OFFSET_BIT 0x01
235 #define VIA_TWOSIDE_BIT 0x02
236 #define VIA_UNFILLED_BIT 0x04
237 #define VIA_FALLBACK_BIT 0x08
238 #define VIA_MAX_TRIFUNC 0x10
242 tnl_points_func points
;
244 tnl_triangle_func triangle
;
246 } rast_tab
[VIA_MAX_TRIFUNC
+ 1];
249 #define DO_FALLBACK (IND & VIA_FALLBACK_BIT)
250 #define DO_OFFSET (IND & VIA_OFFSET_BIT)
251 #define DO_UNFILLED (IND & VIA_UNFILLED_BIT)
252 #define DO_TWOSIDE (IND & VIA_TWOSIDE_BIT)
258 #define DO_FULL_QUAD 1
262 #define HAVE_BACK_COLORS 0
263 #define HAVE_HW_FLATSHADE 1
264 #define VERTEX viaVertex
267 /* Only used to pull back colors into vertices (ie, we know color is
270 #define VIA_COLOR(dst, src) \
278 #define VIA_SPEC(dst, src) \
286 #define DEPTH_SCALE vmesa->polygon_offset_scale
287 #define UNFILLED_TRI unfilled_tri
288 #define UNFILLED_QUAD unfilled_quad
289 #define VERT_X(_v) _v->v.x
290 #define VERT_Y(_v) _v->v.y
291 #define VERT_Z(_v) _v->v.z
292 #define AREA_IS_CCW(a) (a > 0)
293 #define GET_VERTEX(e) (vmesa->verts + (e * vmesa->vertexSize * sizeof(int)))
295 #define VERT_SET_RGBA( v, c ) \
297 via_color_t *color = (via_color_t *)&((v)->ui[coloroffset]); \
298 UNCLAMPED_FLOAT_TO_UBYTE(color->red, (c)[0]); \
299 UNCLAMPED_FLOAT_TO_UBYTE(color->green, (c)[1]); \
300 UNCLAMPED_FLOAT_TO_UBYTE(color->blue, (c)[2]); \
301 UNCLAMPED_FLOAT_TO_UBYTE(color->alpha, (c)[3]); \
304 #define VERT_COPY_RGBA( v0, v1 ) v0->ui[coloroffset] = v1->ui[coloroffset]
306 #define VERT_SET_SPEC( v, c ) \
309 via_color_t *color = (via_color_t *)&((v)->ui[specoffset]); \
310 UNCLAMPED_FLOAT_TO_UBYTE(color->red, (c)[0]); \
311 UNCLAMPED_FLOAT_TO_UBYTE(color->green, (c)[1]); \
312 UNCLAMPED_FLOAT_TO_UBYTE(color->blue, (c)[2]); \
315 #define VERT_COPY_SPEC( v0, v1 ) \
318 v0->ub4[specoffset][0] = v1->ub4[specoffset][0]; \
319 v0->ub4[specoffset][1] = v1->ub4[specoffset][1]; \
320 v0->ub4[specoffset][2] = v1->ub4[specoffset][2]; \
325 #define VERT_SAVE_RGBA( idx ) color[idx] = v[idx]->ui[coloroffset]
326 #define VERT_RESTORE_RGBA( idx ) v[idx]->ui[coloroffset] = color[idx]
327 #define VERT_SAVE_SPEC( idx ) if (specoffset) spec[idx] = v[idx]->ui[specoffset]
328 #define VERT_RESTORE_SPEC( idx ) if (specoffset) v[idx]->ui[specoffset] = spec[idx]
331 #define LOCAL_VARS(n) \
332 struct via_context *vmesa = VIA_CONTEXT(ctx); \
333 GLuint color[n] = { 0 }; \
334 GLuint spec[n] = { 0 }; \
335 GLuint coloroffset = vmesa->coloroffset; \
336 GLuint specoffset = vmesa->specoffset; \
337 (void)color; (void)spec; (void)coloroffset; (void)specoffset;
340 /***********************************************************************
341 * Helpers for rendering unfilled primitives *
342 ***********************************************************************/
344 static const GLenum hwPrim
[GL_POLYGON
+ 2] = {
359 #define RASTERIZE(x) viaRasterPrimitive( ctx, x, hwPrim[x] )
360 #define RENDER_PRIMITIVE vmesa->renderPrimitive
362 #define IND VIA_FALLBACK_BIT
363 #include "tnl_dd/t_dd_unfilled.h"
367 /***********************************************************************
368 * Generate GL render functions *
369 ***********************************************************************/
374 #include "tnl_dd/t_dd_tritmp.h"
376 #define IND (VIA_OFFSET_BIT)
377 #define TAG(x) x##_offset
378 #include "tnl_dd/t_dd_tritmp.h"
380 #define IND (VIA_TWOSIDE_BIT)
381 #define TAG(x) x##_twoside
382 #include "tnl_dd/t_dd_tritmp.h"
384 #define IND (VIA_TWOSIDE_BIT|VIA_OFFSET_BIT)
385 #define TAG(x) x##_twoside_offset
386 #include "tnl_dd/t_dd_tritmp.h"
388 #define IND (VIA_UNFILLED_BIT)
389 #define TAG(x) x##_unfilled
390 #include "tnl_dd/t_dd_tritmp.h"
392 #define IND (VIA_OFFSET_BIT|VIA_UNFILLED_BIT)
393 #define TAG(x) x##_offset_unfilled
394 #include "tnl_dd/t_dd_tritmp.h"
396 #define IND (VIA_TWOSIDE_BIT|VIA_UNFILLED_BIT)
397 #define TAG(x) x##_twoside_unfilled
398 #include "tnl_dd/t_dd_tritmp.h"
400 #define IND (VIA_TWOSIDE_BIT|VIA_OFFSET_BIT|VIA_UNFILLED_BIT)
401 #define TAG(x) x##_twoside_offset_unfilled
402 #include "tnl_dd/t_dd_tritmp.h"
404 #define IND (VIA_FALLBACK_BIT)
405 #define TAG(x) x##_fallback
406 #include "tnl_dd/t_dd_tritmp.h"
408 #define IND (VIA_OFFSET_BIT|VIA_FALLBACK_BIT)
409 #define TAG(x) x##_offset_fallback
410 #include "tnl_dd/t_dd_tritmp.h"
412 #define IND (VIA_TWOSIDE_BIT|VIA_FALLBACK_BIT)
413 #define TAG(x) x##_twoside_fallback
414 #include "tnl_dd/t_dd_tritmp.h"
416 #define IND (VIA_TWOSIDE_BIT|VIA_OFFSET_BIT|VIA_FALLBACK_BIT)
417 #define TAG(x) x##_twoside_offset_fallback
418 #include "tnl_dd/t_dd_tritmp.h"
420 #define IND (VIA_UNFILLED_BIT|VIA_FALLBACK_BIT)
421 #define TAG(x) x##_unfilled_fallback
422 #include "tnl_dd/t_dd_tritmp.h"
424 #define IND (VIA_OFFSET_BIT|VIA_UNFILLED_BIT|VIA_FALLBACK_BIT)
425 #define TAG(x) x##_offset_unfilled_fallback
426 #include "tnl_dd/t_dd_tritmp.h"
428 #define IND (VIA_TWOSIDE_BIT|VIA_UNFILLED_BIT|VIA_FALLBACK_BIT)
429 #define TAG(x) x##_twoside_unfilled_fallback
430 #include "tnl_dd/t_dd_tritmp.h"
432 #define IND (VIA_TWOSIDE_BIT|VIA_OFFSET_BIT|VIA_UNFILLED_BIT| \
434 #define TAG(x) x##_twoside_offset_unfilled_fallback
435 #include "tnl_dd/t_dd_tritmp.h"
438 /* Catchall case for flat, separate specular triangles (via has flat
439 * diffuse shading, but always does specular color with gouraud).
446 #define DO_FALLBACK (0)
447 #define DO_OFFSET (ctx->_TriangleCaps & DD_TRI_OFFSET)
448 #define DO_UNFILLED (ctx->_TriangleCaps & DD_TRI_UNFILLED)
449 #define DO_TWOSIDE (ctx->_TriangleCaps & DD_TRI_LIGHT_TWOSIDE)
451 #define TAG(x) x##_flat_specular
452 #define IND VIA_MAX_TRIFUNC
453 #include "tnl_dd/t_dd_tritmp.h"
456 static void init_rast_tab(void)
461 init_twoside_offset();
463 init_offset_unfilled();
464 init_twoside_unfilled();
465 init_twoside_offset_unfilled();
467 init_offset_fallback();
468 init_twoside_fallback();
469 init_twoside_offset_fallback();
470 init_unfilled_fallback();
471 init_offset_unfilled_fallback();
472 init_twoside_unfilled_fallback();
473 init_twoside_offset_unfilled_fallback();
475 init_flat_specular(); /* special! */
479 /***********************************************************************
480 * Rasterization fallback helpers *
481 ***********************************************************************/
484 /* This code is hit only when a mix of accelerated and unaccelerated
485 * primitives are being drawn, and only for the unaccelerated
489 via_fallback_tri(struct via_context
*vmesa
,
494 GLcontext
*ctx
= vmesa
->glCtx
;
496 _swsetup_Translate(ctx
, v0
, &v
[0]);
497 _swsetup_Translate(ctx
, v1
, &v
[1]);
498 _swsetup_Translate(ctx
, v2
, &v
[2]);
499 viaSpanRenderStart( ctx
);
500 _swrast_Triangle(ctx
, &v
[0], &v
[1], &v
[2]);
501 viaSpanRenderFinish( ctx
);
506 via_fallback_line(struct via_context
*vmesa
,
510 GLcontext
*ctx
= vmesa
->glCtx
;
512 _swsetup_Translate(ctx
, v0
, &v
[0]);
513 _swsetup_Translate(ctx
, v1
, &v
[1]);
514 viaSpanRenderStart( ctx
);
515 _swrast_Line(ctx
, &v
[0], &v
[1]);
516 viaSpanRenderFinish( ctx
);
521 via_fallback_point(struct via_context
*vmesa
,
524 GLcontext
*ctx
= vmesa
->glCtx
;
526 _swsetup_Translate(ctx
, v0
, &v
[0]);
527 viaSpanRenderStart( ctx
);
528 _swrast_Point(ctx
, &v
[0]);
529 viaSpanRenderFinish( ctx
);
532 static void viaResetLineStipple( GLcontext
*ctx
)
534 struct via_context
*vmesa
= VIA_CONTEXT(ctx
);
535 vmesa
->regCmdB
|= HC_HLPrst_MASK
;
538 /**********************************************************************/
539 /* Render unclipped begin/end objects */
540 /**********************************************************************/
542 #define V(x) (viaVertex *)(vertptr + ((x) * vertsize * sizeof(int)))
543 #define RENDER_POINTS(start, count) \
544 for (; start < count; start++) POINT(V(ELT(start)));
545 #define RENDER_LINE(v0, v1) LINE(V(v0), V(v1))
546 #define RENDER_TRI( v0, v1, v2) TRI( V(v0), V(v1), V(v2))
547 #define RENDER_QUAD(v0, v1, v2, v3) QUAD(V(v0), V(v1), V(v2), V(v3))
548 #define INIT(x) viaRasterPrimitive(ctx, x, hwPrim[x])
551 struct via_context *vmesa = VIA_CONTEXT(ctx); \
552 GLubyte *vertptr = (GLubyte *)vmesa->verts; \
553 const GLuint vertsize = vmesa->vertexSize; \
554 const GLuint * const elt = TNL_CONTEXT(ctx)->vb.Elts; \
555 const GLboolean stipple = ctx->Line.StippleFlag; \
556 (void) elt; (void) stipple;
557 #define RESET_STIPPLE if ( stipple ) viaResetLineStipple( ctx );
558 #define RESET_OCCLUSION
559 #define PRESERVE_VB_DEFS
561 #define TAG(x) via_##x##_verts
562 #include "tnl/t_vb_rendertmp.h"
565 #define TAG(x) via_##x##_elts
566 #define ELT(x) elt[x]
567 #include "tnl/t_vb_rendertmp.h"
570 #undef NEED_EDGEFLAG_SETUP
573 #undef RESET_OCCLUSION
576 /**********************************************************************/
577 /* Render clipped primitives */
578 /**********************************************************************/
582 static void viaRenderClippedPoly(GLcontext
*ctx
, const GLuint
*elts
,
585 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
586 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
587 GLuint prim
= VIA_CONTEXT(ctx
)->renderPrimitive
;
589 /* Render the new vertices as an unclipped polygon.
592 GLuint
*tmp
= VB
->Elts
;
593 VB
->Elts
= (GLuint
*)elts
;
594 tnl
->Driver
.Render
.PrimTabElts
[GL_POLYGON
](ctx
, 0, n
,
595 PRIM_BEGIN
|PRIM_END
);
599 /* Restore the render primitive
601 if (prim
!= GL_POLYGON
&&
602 prim
!= GL_POLYGON
+ 1)
603 tnl
->Driver
.Render
.PrimitiveNotify( ctx
, prim
);
606 static void viaRenderClippedLine(GLcontext
*ctx
, GLuint ii
, GLuint jj
)
608 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
609 tnl
->Driver
.Render
.Line(ctx
, ii
, jj
);
612 static void viaFastRenderClippedPoly(GLcontext
*ctx
, const GLuint
*elts
,
615 struct via_context
*vmesa
= VIA_CONTEXT(ctx
);
616 GLuint vertsize
= vmesa
->vertexSize
;
617 GLuint
*vb
= viaExtendPrimitive(vmesa
, (n
- 2) * 3 * 4 * vertsize
);
618 GLubyte
*vertptr
= (GLubyte
*)vmesa
->verts
;
619 const GLuint
*start
= (const GLuint
*)V(elts
[0]);
622 for (i
= 2; i
< n
; i
++) {
623 COPY_DWORDS(vb
, vertsize
, V(elts
[i
- 1]));
624 COPY_DWORDS(vb
, vertsize
, V(elts
[i
]));
625 COPY_DWORDS(vb
, vertsize
, start
);
630 /**********************************************************************/
631 /* Choose render functions */
632 /**********************************************************************/
635 #define _VIA_NEW_VERTEX (_NEW_TEXTURE | \
636 _DD_NEW_SEPARATE_SPECULAR | \
637 _DD_NEW_TRI_UNFILLED | \
638 _DD_NEW_TRI_LIGHT_TWOSIDE | \
641 #define _VIA_NEW_RENDERSTATE (_DD_NEW_LINE_STIPPLE | \
642 _DD_NEW_TRI_UNFILLED | \
643 _DD_NEW_TRI_LIGHT_TWOSIDE | \
644 _DD_NEW_TRI_OFFSET | \
645 _DD_NEW_TRI_STIPPLE | \
649 static void viaChooseRenderState(GLcontext
*ctx
)
651 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
652 struct via_context
*vmesa
= VIA_CONTEXT(ctx
);
653 GLuint flags
= ctx
->_TriangleCaps
;
656 if (vmesa
->ptexHack
) {
657 vmesa
->drawPoint
= via_ptex_point
;
658 vmesa
->drawLine
= via_ptex_line
;
659 vmesa
->drawTri
= via_ptex_tri
;
660 index
|= VIA_FALLBACK_BIT
;
663 vmesa
->drawPoint
= via_draw_point
;
664 vmesa
->drawLine
= via_draw_line
;
665 vmesa
->drawTri
= via_draw_triangle
;
668 if (flags
& (ANY_FALLBACK_FLAGS
| ANY_RASTER_FLAGS
)) {
669 if (ctx
->Light
.Enabled
&& ctx
->Light
.Model
.TwoSide
)
670 index
|= VIA_TWOSIDE_BIT
;
671 if (ctx
->Polygon
.FrontMode
!= GL_FILL
|| ctx
->Polygon
.BackMode
!= GL_FILL
)
672 index
|= VIA_UNFILLED_BIT
;
673 if (flags
& DD_TRI_OFFSET
)
674 index
|= VIA_OFFSET_BIT
;
675 if (flags
& ANY_FALLBACK_FLAGS
)
676 index
|= VIA_FALLBACK_BIT
;
678 /* Hook in fallbacks for specific primitives. */
679 if (flags
& POINT_FALLBACK
)
680 vmesa
->drawPoint
= via_fallback_point
;
682 if (flags
& LINE_FALLBACK
)
683 vmesa
->drawLine
= via_fallback_line
;
685 if (flags
& TRI_FALLBACK
)
686 vmesa
->drawTri
= via_fallback_tri
;
689 if ((flags
& DD_SEPARATE_SPECULAR
) && ctx
->Light
.ShadeModel
== GL_FLAT
)
690 index
= VIA_MAX_TRIFUNC
; /* flat specular */
692 if (vmesa
->renderIndex
!= index
) {
693 vmesa
->renderIndex
= index
;
695 tnl
->Driver
.Render
.Points
= rast_tab
[index
].points
;
696 tnl
->Driver
.Render
.Line
= rast_tab
[index
].line
;
697 tnl
->Driver
.Render
.Triangle
= rast_tab
[index
].triangle
;
698 tnl
->Driver
.Render
.Quad
= rast_tab
[index
].quad
;
701 tnl
->Driver
.Render
.PrimTabVerts
= via_render_tab_verts
;
702 tnl
->Driver
.Render
.PrimTabElts
= via_render_tab_elts
;
703 tnl
->Driver
.Render
.ClippedLine
= line
; /* from tritmp.h */
704 tnl
->Driver
.Render
.ClippedPolygon
= viaFastRenderClippedPoly
;
707 tnl
->Driver
.Render
.PrimTabVerts
= _tnl_render_tab_verts
;
708 tnl
->Driver
.Render
.PrimTabElts
= _tnl_render_tab_elts
;
709 tnl
->Driver
.Render
.ClippedLine
= viaRenderClippedLine
;
710 tnl
->Driver
.Render
.ClippedPolygon
= viaRenderClippedPoly
;
716 #define VIA_EMIT_TEX1 0x01
717 #define VIA_EMIT_TEX0 0x02
718 #define VIA_EMIT_PTEX0 0x04
719 #define VIA_EMIT_RGBA 0x08
720 #define VIA_EMIT_SPEC 0x10
721 #define VIA_EMIT_FOG 0x20
722 #define VIA_EMIT_W 0x40
724 #define EMIT_ATTR( ATTR, STYLE, INDEX, REGB ) \
726 vmesa->vertex_attrs[vmesa->vertex_attr_count].attrib = (ATTR); \
727 vmesa->vertex_attrs[vmesa->vertex_attr_count].format = (STYLE); \
728 vmesa->vertex_attr_count++; \
729 setupIndex |= (INDEX); \
733 #define EMIT_PAD( N ) \
735 vmesa->vertex_attrs[vmesa->vertex_attr_count].attrib = 0; \
736 vmesa->vertex_attrs[vmesa->vertex_attr_count].format = EMIT_PAD; \
737 vmesa->vertex_attrs[vmesa->vertex_attr_count].offset = (N); \
738 vmesa->vertex_attr_count++; \
743 static void viaChooseVertexState( GLcontext
*ctx
)
745 struct via_context
*vmesa
= VIA_CONTEXT(ctx
);
746 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
747 DECLARE_RENDERINPUTS(index_bitset
);
748 GLuint regCmdB
= HC_HVPMSK_X
| HC_HVPMSK_Y
| HC_HVPMSK_Z
;
749 GLuint setupIndex
= 0;
751 RENDERINPUTS_COPY( index_bitset
, tnl
->render_inputs_bitset
);
752 vmesa
->vertex_attr_count
= 0;
754 /* EMIT_ATTR's must be in order as they tell t_vertex.c how to
755 * build up a hardware vertex.
757 if (RENDERINPUTS_TEST_RANGE( index_bitset
, _TNL_FIRST_TEX
, _TNL_LAST_TEX
) ||
758 RENDERINPUTS_TEST( index_bitset
, _TNL_ATTRIB_FOG
)) {
759 EMIT_ATTR( _TNL_ATTRIB_POS
, EMIT_4F_VIEWPORT
, VIA_EMIT_W
, HC_HVPMSK_W
);
760 vmesa
->coloroffset
= 4;
763 EMIT_ATTR( _TNL_ATTRIB_POS
, EMIT_3F_VIEWPORT
, 0, 0 );
764 vmesa
->coloroffset
= 3;
767 /* t_context.c always includes a diffuse color */
768 EMIT_ATTR( _TNL_ATTRIB_COLOR0
, EMIT_4UB_4F_BGRA
, VIA_EMIT_RGBA
,
771 vmesa
->specoffset
= 0;
772 if (RENDERINPUTS_TEST( index_bitset
, _TNL_ATTRIB_COLOR1
) ||
773 RENDERINPUTS_TEST( index_bitset
, _TNL_ATTRIB_FOG
)) {
774 if (RENDERINPUTS_TEST( index_bitset
, _TNL_ATTRIB_COLOR1
)) {
775 vmesa
->specoffset
= vmesa
->coloroffset
+ 1;
776 EMIT_ATTR( _TNL_ATTRIB_COLOR1
, EMIT_3UB_3F_BGR
, VIA_EMIT_SPEC
,
782 if (RENDERINPUTS_TEST( index_bitset
, _TNL_ATTRIB_FOG
))
783 EMIT_ATTR( _TNL_ATTRIB_FOG
, EMIT_1UB_1F
, VIA_EMIT_FOG
, HC_HVPMSK_Cs
);
788 if (RENDERINPUTS_TEST( index_bitset
, _TNL_ATTRIB_TEX0
)) {
790 EMIT_ATTR( _TNL_ATTRIB_TEX0
, EMIT_3F_XYW
, VIA_EMIT_PTEX0
,
791 (HC_HVPMSK_S
| HC_HVPMSK_T
) );
793 EMIT_ATTR( _TNL_ATTRIB_TEX0
, EMIT_2F
, VIA_EMIT_TEX0
,
794 (HC_HVPMSK_S
| HC_HVPMSK_T
) );
797 if (RENDERINPUTS_TEST( index_bitset
, _TNL_ATTRIB_TEX1
)) {
798 EMIT_ATTR( _TNL_ATTRIB_TEX1
, EMIT_2F
, VIA_EMIT_TEX1
,
799 (HC_HVPMSK_S
| HC_HVPMSK_T
) );
802 if (setupIndex
!= vmesa
->setupIndex
) {
803 vmesa
->vertexSize
= _tnl_install_attrs( ctx
,
805 vmesa
->vertex_attr_count
,
806 vmesa
->ViewportMatrix
.m
, 0 );
807 vmesa
->vertexSize
>>= 2;
808 vmesa
->setupIndex
= setupIndex
;
809 vmesa
->regCmdB
&= ~HC_HVPMSK_MASK
;
810 vmesa
->regCmdB
|= regCmdB
;
813 vmesa
->hwVertexSize
= vmesa
->vertexSize
- 1;
815 vmesa
->hwVertexSize
= vmesa
->vertexSize
;
822 /* Check if projective texture coordinates are used and if we can fake
823 * them. Fallback to swrast if we can't. Returns GL_TRUE if projective
824 * texture coordinates must be faked, GL_FALSE otherwise.
826 static GLboolean
viaCheckPTexHack( GLcontext
*ctx
)
828 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
829 struct vertex_buffer
*VB
= &tnl
->vb
;
830 DECLARE_RENDERINPUTS(index_bitset
);
831 GLboolean fallback
= GL_FALSE
;
832 GLboolean ptexHack
= GL_FALSE
;
834 RENDERINPUTS_COPY( index_bitset
, tnl
->render_inputs_bitset
);
836 if (RENDERINPUTS_TEST( index_bitset
, _TNL_ATTRIB_TEX0
) && VB
->AttribPtr
[_TNL_ATTRIB_TEX0
]->size
== 4) {
837 if (!RENDERINPUTS_TEST_RANGE( index_bitset
, _TNL_ATTRIB_TEX1
, _TNL_LAST_TEX
))
842 if (RENDERINPUTS_TEST( index_bitset
, _TNL_ATTRIB_TEX1
) && VB
->AttribPtr
[_TNL_ATTRIB_TEX1
]->size
== 4)
845 FALLBACK(VIA_CONTEXT(ctx
), VIA_FALLBACK_PROJ_TEXTURE
, fallback
);
852 /**********************************************************************/
853 /* High level hooks for t_vb_render.c */
854 /**********************************************************************/
857 static void viaRenderStart(GLcontext
*ctx
)
859 struct via_context
*vmesa
= VIA_CONTEXT(ctx
);
860 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
861 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
864 GLboolean ptexHack
= viaCheckPTexHack( ctx
);
865 if (ptexHack
!= vmesa
->ptexHack
) {
866 vmesa
->ptexHack
= ptexHack
;
867 vmesa
->newRenderState
|= _VIA_NEW_RENDERSTATE
;
871 if (vmesa
->newState
) {
872 vmesa
->newRenderState
|= vmesa
->newState
;
873 viaValidateState( ctx
);
876 if (vmesa
->Fallback
) {
877 tnl
->Driver
.Render
.Start(ctx
);
881 if (vmesa
->newRenderState
) {
882 viaChooseVertexState(ctx
);
883 viaChooseRenderState(ctx
);
884 vmesa
->newRenderState
= 0;
889 VB
->AttribPtr
[VERT_ATTRIB_POS
] = VB
->NdcPtr
;
892 static void viaRenderFinish(GLcontext
*ctx
)
894 VIA_FINISH_PRIM(VIA_CONTEXT(ctx
));
898 /* System to flush dma and emit state changes based on the rasterized
901 void viaRasterPrimitive(GLcontext
*ctx
,
905 struct via_context
*vmesa
= VIA_CONTEXT(ctx
);
909 if (VIA_DEBUG
& DEBUG_PRIMS
)
910 fprintf(stderr
, "%s: %s/%s/%s\n",
911 __FUNCTION__
, _mesa_lookup_enum_by_nr(glprim
),
912 _mesa_lookup_enum_by_nr(hwprim
),
913 _mesa_lookup_enum_by_nr(ctx
->Light
.ShadeModel
));
915 assert (!vmesa
->newState
);
917 vmesa
->renderPrimitive
= glprim
;
919 if (hwprim
!= vmesa
->hwPrimitive
||
920 ctx
->Light
.ShadeModel
!= vmesa
->hwShadeModel
) {
922 VIA_FINISH_PRIM(vmesa
);
924 /* Ensure no wrapping inside this function */
925 viaCheckDma( vmesa
, 1024 );
927 if (vmesa
->newEmitState
) {
931 vmesa
->regCmdA_End
= HC_ACMD_HCmdA
;
933 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
934 vmesa
->regCmdA_End
|= HC_HShading_Gouraud
;
937 vmesa
->hwShadeModel
= ctx
->Light
.ShadeModel
;
938 regCmdB
= vmesa
->regCmdB
;
942 vmesa
->regCmdA_End
|= HC_HPMType_Point
| HC_HVCycle_Full
;
943 vmesa
->regCmdA_End
|= HC_HShading_Gouraud
; /* always Gouraud
947 vmesa
->regCmdA_End
|= HC_HPMType_Line
| HC_HVCycle_Full
;
948 regCmdB
|= HC_HLPrst_MASK
;
949 if (ctx
->Light
.ShadeModel
== GL_FLAT
)
950 vmesa
->regCmdA_End
|= HC_HShading_FlatB
;
954 vmesa
->regCmdA_End
|= HC_HPMType_Line
| HC_HVCycle_AFP
|
955 HC_HVCycle_AB
| HC_HVCycle_NewB
;
956 regCmdB
|= HC_HVCycle_AB
| HC_HVCycle_NewB
| HC_HLPrst_MASK
;
957 if (ctx
->Light
.ShadeModel
== GL_FLAT
)
958 vmesa
->regCmdA_End
|= HC_HShading_FlatB
;
961 vmesa
->regCmdA_End
|= HC_HPMType_Tri
| HC_HVCycle_Full
;
962 if (ctx
->Light
.ShadeModel
== GL_FLAT
)
963 vmesa
->regCmdA_End
|= HC_HShading_FlatC
;
965 case GL_TRIANGLE_STRIP
:
966 vmesa
->regCmdA_End
|= HC_HPMType_Tri
| HC_HVCycle_AFP
|
967 HC_HVCycle_AC
| HC_HVCycle_BB
| HC_HVCycle_NewC
;
968 regCmdB
|= HC_HVCycle_AA
| HC_HVCycle_BC
| HC_HVCycle_NewC
;
969 if (ctx
->Light
.ShadeModel
== GL_FLAT
)
970 vmesa
->regCmdA_End
|= HC_HShading_FlatC
;
972 case GL_TRIANGLE_FAN
:
973 vmesa
->regCmdA_End
|= HC_HPMType_Tri
| HC_HVCycle_AFP
|
974 HC_HVCycle_AA
| HC_HVCycle_BC
| HC_HVCycle_NewC
;
975 regCmdB
|= HC_HVCycle_AA
| HC_HVCycle_BC
| HC_HVCycle_NewC
;
976 if (ctx
->Light
.ShadeModel
== GL_FLAT
)
977 vmesa
->regCmdA_End
|= HC_HShading_FlatC
;
986 vmesa
->regCmdA_End
|= HC_HPMType_Tri
| HC_HVCycle_AFP
|
987 HC_HVCycle_AA
| HC_HVCycle_BC
| HC_HVCycle_NewC
;
988 regCmdB
|= HC_HVCycle_AA
| HC_HVCycle_BC
| HC_HVCycle_NewC
;
989 if (ctx
->Light
.ShadeModel
== GL_FLAT
)
990 vmesa
->regCmdA_End
|= HC_HShading_FlatC
;
997 /* assert((vmesa->dmaLow & 0x4) == 0); */
999 if (vmesa
->dmaCliprectAddr
== ~0) {
1000 if (VIA_DEBUG
& DEBUG_DMA
)
1001 fprintf(stderr
, "reserve cliprect space at %x\n", vmesa
->dmaLow
);
1002 vmesa
->dmaCliprectAddr
= vmesa
->dmaLow
;
1004 OUT_RING( HC_HEADER2
);
1005 OUT_RING( (HC_ParaType_NotTex
<< 16) );
1006 OUT_RING( 0xCCCCCCCC );
1007 OUT_RING( 0xCCCCCCCC );
1008 OUT_RING( 0xCCCCCCCC );
1009 OUT_RING( 0xCCCCCCCC );
1010 OUT_RING( 0xCCCCCCCC );
1011 OUT_RING( 0xCCCCCCCC );
1015 assert(vmesa
->dmaLastPrim
== 0);
1018 OUT_RING( HC_HEADER2
);
1019 OUT_RING( (HC_ParaType_NotTex
<< 16) );
1020 OUT_RING( 0xCCCCCCCC );
1021 OUT_RING( 0xDDDDDDDD );
1023 OUT_RING( HC_HEADER2
);
1024 OUT_RING( (HC_ParaType_CmdVdata
<< 16) );
1025 OUT_RING( regCmdB
);
1026 OUT_RING( vmesa
->regCmdA_End
);
1029 vmesa
->hwPrimitive
= hwprim
;
1030 vmesa
->dmaLastPrim
= vmesa
->dmaLow
;
1033 assert(!vmesa
->newEmitState
);
1037 /* Callback for mesa:
1039 static void viaRenderPrimitive( GLcontext
*ctx
, GLuint prim
)
1041 viaRasterPrimitive( ctx
, prim
, hwPrim
[prim
] );
1045 void viaFinishPrimitive(struct via_context
*vmesa
)
1047 if (VIA_DEBUG
& (DEBUG_DMA
|DEBUG_PRIMS
))
1048 fprintf(stderr
, "%s\n", __FUNCTION__
);
1050 if (!vmesa
->dmaLastPrim
|| vmesa
->dmaCliprectAddr
== ~0) {
1053 else if (vmesa
->dmaLow
!= vmesa
->dmaLastPrim
) {
1054 GLuint cmdA
= (vmesa
->regCmdA_End
| HC_HPLEND_MASK
|
1055 HC_HPMValidN_MASK
| HC_HE3Fire_MASK
);
1058 vmesa
->dmaLastPrim
= 0;
1060 /* KW: modified 0x1 to 0x4 below:
1062 if ((vmesa
->dmaLow
& 0x4) || !vmesa
->useAgp
) {
1063 BEGIN_RING_NOCHECK( 1 );
1068 BEGIN_RING_NOCHECK( 2 );
1074 if (vmesa
->dmaLow
> VIA_DMA_HIGHWATER
)
1075 viaFlushDma( vmesa
);
1078 if (VIA_DEBUG
& (DEBUG_DMA
|DEBUG_PRIMS
))
1079 fprintf(stderr
, "remove empty primitive\n");
1081 /* Remove the primitive header:
1083 vmesa
->dmaLastPrim
= 0;
1084 vmesa
->dmaLow
-= 8 * sizeof(GLuint
);
1086 /* Maybe remove the cliprect as well:
1088 if (vmesa
->dmaCliprectAddr
== vmesa
->dmaLow
- 8 * sizeof(GLuint
)) {
1089 vmesa
->dmaLow
-= 8 * sizeof(GLuint
);
1090 vmesa
->dmaCliprectAddr
= ~0;
1094 vmesa
->renderPrimitive
= GL_POLYGON
+ 1;
1095 vmesa
->hwPrimitive
= GL_POLYGON
+ 1;
1096 vmesa
->dmaLastPrim
= 0;
1100 /**********************************************************************/
1101 /* Transition to/from hardware rasterization. */
1102 /**********************************************************************/
1105 void viaFallback(struct via_context
*vmesa
, GLuint bit
, GLboolean mode
)
1107 GLcontext
*ctx
= vmesa
->glCtx
;
1108 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1109 GLuint oldfallback
= vmesa
->Fallback
;
1112 vmesa
->Fallback
|= bit
;
1113 if (oldfallback
== 0) {
1114 VIA_FLUSH_DMA(vmesa
);
1116 if (VIA_DEBUG
& DEBUG_FALLBACKS
)
1117 fprintf(stderr
, "ENTER FALLBACK %x\n", bit
);
1119 _swsetup_Wakeup(ctx
);
1120 vmesa
->renderIndex
= ~0;
1124 vmesa
->Fallback
&= ~bit
;
1125 if (oldfallback
== bit
) {
1126 _swrast_flush( ctx
);
1128 if (VIA_DEBUG
& DEBUG_FALLBACKS
)
1129 fprintf(stderr
, "LEAVE FALLBACK %x\n", bit
);
1131 tnl
->Driver
.Render
.Start
= viaRenderStart
;
1132 tnl
->Driver
.Render
.PrimitiveNotify
= viaRenderPrimitive
;
1133 tnl
->Driver
.Render
.Finish
= viaRenderFinish
;
1135 tnl
->Driver
.Render
.BuildVertices
= _tnl_build_vertices
;
1136 tnl
->Driver
.Render
.CopyPV
= _tnl_copy_pv
;
1137 tnl
->Driver
.Render
.Interp
= _tnl_interp
;
1138 tnl
->Driver
.Render
.ResetLineStipple
= viaResetLineStipple
;
1140 _tnl_invalidate_vertex_state( ctx
, ~0 );
1141 _tnl_invalidate_vertices( ctx
, ~0 );
1142 _tnl_install_attrs( ctx
,
1143 vmesa
->vertex_attrs
,
1144 vmesa
->vertex_attr_count
,
1145 vmesa
->ViewportMatrix
.m
, 0 );
1147 vmesa
->newState
|= (_VIA_NEW_RENDERSTATE
|_VIA_NEW_VERTEX
);
1152 static void viaRunPipeline( GLcontext
*ctx
)
1154 struct via_context
*vmesa
= VIA_CONTEXT(ctx
);
1156 if (vmesa
->newState
) {
1157 vmesa
->newRenderState
|= vmesa
->newState
;
1158 viaValidateState( ctx
);
1161 _tnl_run_pipeline( ctx
);
1165 /**********************************************************************/
1166 /* Initialization. */
1167 /**********************************************************************/
1170 void viaInitTriFuncs(GLcontext
*ctx
)
1172 struct via_context
*vmesa
= VIA_CONTEXT(ctx
);
1173 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1174 static int firsttime
= 1;
1181 tnl
->Driver
.RunPipeline
= viaRunPipeline
;
1182 tnl
->Driver
.Render
.Start
= viaRenderStart
;
1183 tnl
->Driver
.Render
.Finish
= viaRenderFinish
;
1184 tnl
->Driver
.Render
.PrimitiveNotify
= viaRenderPrimitive
;
1185 tnl
->Driver
.Render
.ResetLineStipple
= viaResetLineStipple
;
1186 tnl
->Driver
.Render
.BuildVertices
= _tnl_build_vertices
;
1187 tnl
->Driver
.Render
.CopyPV
= _tnl_copy_pv
;
1188 tnl
->Driver
.Render
.Interp
= _tnl_interp
;
1190 _tnl_init_vertices( ctx
, ctx
->Const
.MaxArrayLockSize
+ 12,
1191 (6 + 2*ctx
->Const
.MaxTextureUnits
) * sizeof(GLfloat
) );
1193 vmesa
->verts
= (GLubyte
*)tnl
->clipspace
.vertex_buf
;