2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 * Keith Whitwell <keith@tungstengraphics.com>
30 * Render whole vertex buffers, including projection of vertices from
31 * clip space and clipping of primitives.
33 * This file makes calls to project vertices and to the point, line
34 * and triangle rasterizers via the function pointers:
36 * context->Driver.Render.*
48 #include "t_pipeline.h"
52 /**********************************************************************/
53 /* Clip single primitives */
54 /**********************************************************************/
57 #define W(i) coord[i][3]
58 #define Z(i) coord[i][2]
59 #define Y(i) coord[i][1]
60 #define X(i) coord[i][0]
63 #include "t_vb_cliptmp.h"
67 /**********************************************************************/
68 /* Clip and render whole begin/end objects */
69 /**********************************************************************/
71 #define NEED_EDGEFLAG_SETUP (ctx->Polygon.FrontMode != GL_FILL || ctx->Polygon.BackMode != GL_FILL)
72 #define EDGEFLAG_GET(idx) VB->EdgeFlag[idx]
73 #define EDGEFLAG_SET(idx, val) VB->EdgeFlag[idx] = val
76 /* This does NOT include the CLIP_USER_BIT! */
77 #define CLIPMASK (CLIP_FRUSTUM_BITS | CLIP_CULL_BIT)
80 /* Vertices, with the possibility of clipping.
82 #define RENDER_POINTS( start, count ) \
83 tnl->Driver.Render.Points( ctx, start, count )
85 #define RENDER_LINE( v1, v2 ) \
87 GLubyte c1 = mask[v1], c2 = mask[v2]; \
88 GLubyte ormask = c1|c2; \
90 LineFunc( ctx, v1, v2 ); \
91 else if (!(c1 & c2 & CLIPMASK)) \
92 clip_line_4( ctx, v1, v2, ormask ); \
95 #define RENDER_TRI( v1, v2, v3 ) \
97 GLubyte c1 = mask[v1], c2 = mask[v2], c3 = mask[v3]; \
98 GLubyte ormask = c1|c2|c3; \
100 TriangleFunc( ctx, v1, v2, v3 ); \
101 else if (!(c1 & c2 & c3 & CLIPMASK)) \
102 clip_tri_4( ctx, v1, v2, v3, ormask ); \
105 #define RENDER_QUAD( v1, v2, v3, v4 ) \
107 GLubyte c1 = mask[v1], c2 = mask[v2]; \
108 GLubyte c3 = mask[v3], c4 = mask[v4]; \
109 GLubyte ormask = c1|c2|c3|c4; \
111 QuadFunc( ctx, v1, v2, v3, v4 ); \
112 else if (!(c1 & c2 & c3 & c4 & CLIPMASK)) \
113 clip_quad_4( ctx, v1, v2, v3, v4, ormask ); \
118 TNLcontext *tnl = TNL_CONTEXT(ctx); \
119 struct vertex_buffer *VB = &tnl->vb; \
120 const GLuint * const elt = VB->Elts; \
121 const GLubyte *mask = VB->ClipMask; \
122 const GLuint sz = VB->ClipPtr->size; \
123 const tnl_line_func LineFunc = tnl->Driver.Render.Line; \
124 const tnl_triangle_func TriangleFunc = tnl->Driver.Render.Triangle; \
125 const tnl_quad_func QuadFunc = tnl->Driver.Render.Quad; \
126 const GLboolean stipple = ctx->Line.StippleFlag; \
127 (void) (LineFunc && TriangleFunc && QuadFunc); \
128 (void) elt; (void) mask; (void) sz; (void) stipple;
130 #define TAG(x) clip_##x##_verts
131 #define INIT(x) tnl->Driver.Render.PrimitiveNotify( ctx, x )
132 #define RESET_STIPPLE if (stipple) tnl->Driver.Render.ResetLineStipple( ctx )
133 #define PRESERVE_VB_DEFS
134 #include "t_vb_rendertmp.h"
138 /* Elts, with the possibility of clipping.
142 #define ELT(x) elt[x]
143 #define TAG(x) clip_##x##_elts
144 #include "t_vb_rendertmp.h"
146 /* TODO: do this for all primitives, verts and elts:
148 static void clip_elt_triangles( GLcontext
*ctx
,
153 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
154 tnl_render_func render_tris
= tnl
->Driver
.Render
.PrimTabElts
[GL_TRIANGLES
];
155 struct vertex_buffer
*VB
= &tnl
->vb
;
156 const GLuint
* const elt
= VB
->Elts
;
157 GLubyte
*mask
= VB
->ClipMask
;
158 GLuint last
= count
-2;
162 tnl
->Driver
.Render
.PrimitiveNotify( ctx
, GL_TRIANGLES
);
164 for (j
=start
; j
< last
; j
+=3 ) {
165 GLubyte c1
= mask
[elt
[j
]];
166 GLubyte c2
= mask
[elt
[j
+1]];
167 GLubyte c3
= mask
[elt
[j
+2]];
168 GLubyte ormask
= c1
|c2
|c3
;
171 render_tris( ctx
, start
, j
, 0 );
172 if (!(c1
&c2
&c3
&CLIPMASK
))
173 clip_tri_4( ctx
, elt
[j
], elt
[j
+1], elt
[j
+2], ormask
);
179 render_tris( ctx
, start
, j
, 0 );
182 /**********************************************************************/
183 /* Render whole begin/end objects */
184 /**********************************************************************/
186 #define NEED_EDGEFLAG_SETUP (ctx->Polygon.FrontMode != GL_FILL || ctx->Polygon.BackMode != GL_FILL)
187 #define EDGEFLAG_GET(idx) VB->EdgeFlag[idx]
188 #define EDGEFLAG_SET(idx, val) VB->EdgeFlag[idx] = val
191 /* Vertices, no clipping.
193 #define RENDER_POINTS( start, count ) \
194 tnl->Driver.Render.Points( ctx, start, count )
196 #define RENDER_LINE( v1, v2 ) \
197 LineFunc( ctx, v1, v2 )
199 #define RENDER_TRI( v1, v2, v3 ) \
200 TriangleFunc( ctx, v1, v2, v3 )
202 #define RENDER_QUAD( v1, v2, v3, v4 ) \
203 QuadFunc( ctx, v1, v2, v3, v4 )
205 #define TAG(x) _tnl_##x##_verts
208 TNLcontext *tnl = TNL_CONTEXT(ctx); \
209 struct vertex_buffer *VB = &tnl->vb; \
210 const GLuint * const elt = VB->Elts; \
211 const tnl_line_func LineFunc = tnl->Driver.Render.Line; \
212 const tnl_triangle_func TriangleFunc = tnl->Driver.Render.Triangle; \
213 const tnl_quad_func QuadFunc = tnl->Driver.Render.Quad; \
214 const GLboolean stipple = ctx->Line.StippleFlag; \
215 (void) (LineFunc && TriangleFunc && QuadFunc); \
216 (void) elt; (void) stipple
218 #define RESET_STIPPLE if (stipple) tnl->Driver.Render.ResetLineStipple( ctx )
219 #define INIT(x) tnl->Driver.Render.PrimitiveNotify( ctx, x )
220 #define RENDER_TAB_QUALIFIER
221 #define PRESERVE_VB_DEFS
222 #include "t_vb_rendertmp.h"
225 /* Elts, no clipping.
228 #define TAG(x) _tnl_##x##_elts
229 #define ELT(x) elt[x]
230 #include "t_vb_rendertmp.h"
233 /**********************************************************************/
234 /* Helper functions for drivers */
235 /**********************************************************************/
237 void _tnl_RenderClippedPolygon( GLcontext
*ctx
, const GLuint
*elts
, GLuint n
)
239 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
240 struct vertex_buffer
*VB
= &tnl
->vb
;
241 GLuint
*tmp
= VB
->Elts
;
243 VB
->Elts
= (GLuint
*)elts
;
244 tnl
->Driver
.Render
.PrimTabElts
[GL_POLYGON
]( ctx
, 0, n
, PRIM_BEGIN
|PRIM_END
);
248 void _tnl_RenderClippedLine( GLcontext
*ctx
, GLuint ii
, GLuint jj
)
250 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
251 tnl
->Driver
.Render
.Line( ctx
, ii
, jj
);
256 /**********************************************************************/
257 /* Clip and render whole vertex buffers */
258 /**********************************************************************/
261 static GLboolean
run_render( GLcontext
*ctx
,
262 struct tnl_pipeline_stage
*stage
)
264 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
265 struct vertex_buffer
*VB
= &tnl
->vb
;
266 tnl_render_func
*tab
;
269 /* Allow the drivers to lock before projected verts are built so
270 * that window coordinates are guarenteed not to change before
273 ASSERT(tnl
->Driver
.Render
.Start
);
275 tnl
->Driver
.Render
.Start( ctx
);
277 ASSERT(tnl
->Driver
.Render
.BuildVertices
);
278 ASSERT(tnl
->Driver
.Render
.PrimitiveNotify
);
279 ASSERT(tnl
->Driver
.Render
.Points
);
280 ASSERT(tnl
->Driver
.Render
.Line
);
281 ASSERT(tnl
->Driver
.Render
.Triangle
);
282 ASSERT(tnl
->Driver
.Render
.Quad
);
283 ASSERT(tnl
->Driver
.Render
.ResetLineStipple
);
284 ASSERT(tnl
->Driver
.Render
.Interp
);
285 ASSERT(tnl
->Driver
.Render
.CopyPV
);
286 ASSERT(tnl
->Driver
.Render
.ClippedLine
);
287 ASSERT(tnl
->Driver
.Render
.ClippedPolygon
);
288 ASSERT(tnl
->Driver
.Render
.Finish
);
290 tnl
->Driver
.Render
.BuildVertices( ctx
, 0, VB
->Count
, ~0 );
292 if (VB
->ClipOrMask
) {
293 tab
= VB
->Elts
? clip_render_tab_elts
: clip_render_tab_verts
;
294 clip_render_tab_elts
[GL_TRIANGLES
] = clip_elt_triangles
;
298 tnl
->Driver
.Render
.PrimTabElts
:
299 tnl
->Driver
.Render
.PrimTabVerts
);
306 for (i
= 0 ; i
< VB
->PrimitiveCount
; i
++)
308 GLuint prim
= _tnl_translate_prim(&VB
->Primitive
[i
]);
309 GLuint start
= VB
->Primitive
[i
].start
;
310 GLuint length
= VB
->Primitive
[i
].count
;
312 assert((prim
& PRIM_MODE_MASK
) <= GL_POLYGON
);
314 if (MESA_VERBOSE
& VERBOSE_PRIMS
)
315 _mesa_debug(NULL
, "MESA prim %s %d..%d\n",
316 _mesa_lookup_enum_by_nr(prim
& PRIM_MODE_MASK
),
317 start
, start
+length
);
320 tab
[prim
& PRIM_MODE_MASK
]( ctx
, start
, start
+ length
, prim
);
322 } while (tnl
->Driver
.Render
.Multipass
&&
323 tnl
->Driver
.Render
.Multipass( ctx
, ++pass
));
325 tnl
->Driver
.Render
.Finish( ctx
);
327 return GL_FALSE
; /* finished the pipe */
331 /**********************************************************************/
332 /* Render pipeline stage */
333 /**********************************************************************/
339 const struct tnl_pipeline_stage _tnl_render_stage
=
342 NULL
, /* private data */
344 NULL
, /* destructor */