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 #define CLIPMASK (CLIP_ALL_BITS|CLIP_CULL_BIT)
79 /* Vertices, with the possibility of clipping.
81 #define RENDER_POINTS( start, count ) \
82 tnl->Driver.Render.Points( ctx, start, count )
84 #define RENDER_LINE( v1, v2 ) \
86 GLubyte c1 = mask[v1], c2 = mask[v2]; \
87 GLubyte ormask = c1|c2; \
89 LineFunc( ctx, v1, v2 ); \
90 else if (!(c1 & c2 & CLIPMASK)) \
91 clip_line_4( ctx, v1, v2, ormask ); \
94 #define RENDER_TRI( v1, v2, v3 ) \
96 GLubyte c1 = mask[v1], c2 = mask[v2], c3 = mask[v3]; \
97 GLubyte ormask = c1|c2|c3; \
99 TriangleFunc( ctx, v1, v2, v3 ); \
100 else if (!(c1 & c2 & c3 & CLIPMASK)) \
101 clip_tri_4( ctx, v1, v2, v3, ormask ); \
104 #define RENDER_QUAD( v1, v2, v3, v4 ) \
106 GLubyte c1 = mask[v1], c2 = mask[v2]; \
107 GLubyte c3 = mask[v3], c4 = mask[v4]; \
108 GLubyte ormask = c1|c2|c3|c4; \
110 QuadFunc( ctx, v1, v2, v3, v4 ); \
111 else if (!(c1 & c2 & c3 & c4 & CLIPMASK)) \
112 clip_quad_4( ctx, v1, v2, v3, v4, ormask ); \
117 TNLcontext *tnl = TNL_CONTEXT(ctx); \
118 struct vertex_buffer *VB = &tnl->vb; \
119 const GLuint * const elt = VB->Elts; \
120 const GLubyte *mask = VB->ClipMask; \
121 const GLuint sz = VB->ClipPtr->size; \
122 const tnl_line_func LineFunc = tnl->Driver.Render.Line; \
123 const tnl_triangle_func TriangleFunc = tnl->Driver.Render.Triangle; \
124 const tnl_quad_func QuadFunc = tnl->Driver.Render.Quad; \
125 const GLboolean stipple = ctx->Line.StippleFlag; \
126 (void) (LineFunc && TriangleFunc && QuadFunc); \
127 (void) elt; (void) mask; (void) sz; (void) stipple;
129 #define TAG(x) clip_##x##_verts
130 #define INIT(x) tnl->Driver.Render.PrimitiveNotify( ctx, x )
131 #define RESET_STIPPLE if (stipple) tnl->Driver.Render.ResetLineStipple( ctx )
132 #define PRESERVE_VB_DEFS
133 #include "t_vb_rendertmp.h"
137 /* Elts, with the possibility of clipping.
141 #define ELT(x) elt[x]
142 #define TAG(x) clip_##x##_elts
143 #include "t_vb_rendertmp.h"
145 /* TODO: do this for all primitives, verts and elts:
147 static void clip_elt_triangles( GLcontext
*ctx
,
152 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
153 tnl_render_func render_tris
= tnl
->Driver
.Render
.PrimTabElts
[GL_TRIANGLES
];
154 struct vertex_buffer
*VB
= &tnl
->vb
;
155 const GLuint
* const elt
= VB
->Elts
;
156 GLubyte
*mask
= VB
->ClipMask
;
157 GLuint last
= count
-2;
161 tnl
->Driver
.Render
.PrimitiveNotify( ctx
, GL_TRIANGLES
);
163 for (j
=start
; j
< last
; j
+=3 ) {
164 GLubyte c1
= mask
[elt
[j
]];
165 GLubyte c2
= mask
[elt
[j
+1]];
166 GLubyte c3
= mask
[elt
[j
+2]];
167 GLubyte ormask
= c1
|c2
|c3
;
170 render_tris( ctx
, start
, j
, 0 );
171 if (!(c1
&c2
&c3
&CLIPMASK
))
172 clip_tri_4( ctx
, elt
[j
], elt
[j
+1], elt
[j
+2], ormask
);
178 render_tris( ctx
, start
, j
, 0 );
181 /**********************************************************************/
182 /* Render whole begin/end objects */
183 /**********************************************************************/
185 #define NEED_EDGEFLAG_SETUP (ctx->Polygon.FrontMode != GL_FILL || ctx->Polygon.BackMode != GL_FILL)
186 #define EDGEFLAG_GET(idx) VB->EdgeFlag[idx]
187 #define EDGEFLAG_SET(idx, val) VB->EdgeFlag[idx] = val
190 /* Vertices, no clipping.
192 #define RENDER_POINTS( start, count ) \
193 tnl->Driver.Render.Points( ctx, start, count )
195 #define RENDER_LINE( v1, v2 ) \
196 LineFunc( ctx, v1, v2 )
198 #define RENDER_TRI( v1, v2, v3 ) \
199 TriangleFunc( ctx, v1, v2, v3 )
201 #define RENDER_QUAD( v1, v2, v3, v4 ) \
202 QuadFunc( ctx, v1, v2, v3, v4 )
204 #define TAG(x) _tnl_##x##_verts
207 TNLcontext *tnl = TNL_CONTEXT(ctx); \
208 struct vertex_buffer *VB = &tnl->vb; \
209 const GLuint * const elt = VB->Elts; \
210 const tnl_line_func LineFunc = tnl->Driver.Render.Line; \
211 const tnl_triangle_func TriangleFunc = tnl->Driver.Render.Triangle; \
212 const tnl_quad_func QuadFunc = tnl->Driver.Render.Quad; \
213 const GLboolean stipple = ctx->Line.StippleFlag; \
214 (void) (LineFunc && TriangleFunc && QuadFunc); \
215 (void) elt; (void) stipple
217 #define RESET_STIPPLE if (stipple) tnl->Driver.Render.ResetLineStipple( ctx )
218 #define INIT(x) tnl->Driver.Render.PrimitiveNotify( ctx, x )
219 #define RENDER_TAB_QUALIFIER
220 #define PRESERVE_VB_DEFS
221 #include "t_vb_rendertmp.h"
224 /* Elts, no clipping.
227 #define TAG(x) _tnl_##x##_elts
228 #define ELT(x) elt[x]
229 #include "t_vb_rendertmp.h"
232 /**********************************************************************/
233 /* Helper functions for drivers */
234 /**********************************************************************/
236 void _tnl_RenderClippedPolygon( GLcontext
*ctx
, const GLuint
*elts
, GLuint n
)
238 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
239 struct vertex_buffer
*VB
= &tnl
->vb
;
240 GLuint
*tmp
= VB
->Elts
;
242 VB
->Elts
= (GLuint
*)elts
;
243 tnl
->Driver
.Render
.PrimTabElts
[GL_POLYGON
]( ctx
, 0, n
, PRIM_BEGIN
|PRIM_END
);
247 void _tnl_RenderClippedLine( GLcontext
*ctx
, GLuint ii
, GLuint jj
)
249 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
250 tnl
->Driver
.Render
.Line( ctx
, ii
, jj
);
255 /**********************************************************************/
256 /* Clip and render whole vertex buffers */
257 /**********************************************************************/
260 static GLboolean
run_render( GLcontext
*ctx
,
261 struct tnl_pipeline_stage
*stage
)
263 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
264 struct vertex_buffer
*VB
= &tnl
->vb
;
265 tnl_render_func
*tab
;
268 /* Allow the drivers to lock before projected verts are built so
269 * that window coordinates are guarenteed not to change before
272 ASSERT(tnl
->Driver
.Render
.Start
);
274 tnl
->Driver
.Render
.Start( ctx
);
276 ASSERT(tnl
->Driver
.Render
.BuildVertices
);
277 ASSERT(tnl
->Driver
.Render
.PrimitiveNotify
);
278 ASSERT(tnl
->Driver
.Render
.Points
);
279 ASSERT(tnl
->Driver
.Render
.Line
);
280 ASSERT(tnl
->Driver
.Render
.Triangle
);
281 ASSERT(tnl
->Driver
.Render
.Quad
);
282 ASSERT(tnl
->Driver
.Render
.ResetLineStipple
);
283 ASSERT(tnl
->Driver
.Render
.Interp
);
284 ASSERT(tnl
->Driver
.Render
.CopyPV
);
285 ASSERT(tnl
->Driver
.Render
.ClippedLine
);
286 ASSERT(tnl
->Driver
.Render
.ClippedPolygon
);
287 ASSERT(tnl
->Driver
.Render
.Finish
);
289 tnl
->Driver
.Render
.BuildVertices( ctx
, 0, VB
->Count
, ~0 );
291 if (VB
->ClipOrMask
) {
292 tab
= VB
->Elts
? clip_render_tab_elts
: clip_render_tab_verts
;
293 clip_render_tab_elts
[GL_TRIANGLES
] = clip_elt_triangles
;
297 tnl
->Driver
.Render
.PrimTabElts
:
298 tnl
->Driver
.Render
.PrimTabVerts
);
305 for (i
= 0 ; i
< VB
->PrimitiveCount
; i
++)
307 GLuint prim
= VB
->Primitive
[i
].mode
;
308 GLuint start
= VB
->Primitive
[i
].start
;
309 GLuint length
= VB
->Primitive
[i
].count
;
311 assert((prim
& PRIM_MODE_MASK
) <= GL_POLYGON
);
313 if (MESA_VERBOSE
& VERBOSE_PRIMS
)
314 _mesa_debug(NULL
, "MESA prim %s %d..%d\n",
315 _mesa_lookup_enum_by_nr(prim
& PRIM_MODE_MASK
),
316 start
, start
+length
);
319 tab
[prim
& PRIM_MODE_MASK
]( ctx
, start
, start
+ length
, prim
);
321 } while (tnl
->Driver
.Render
.Multipass
&&
322 tnl
->Driver
.Render
.Multipass( ctx
, ++pass
));
324 tnl
->Driver
.Render
.Finish( ctx
);
326 return GL_FALSE
; /* finished the pipe */
330 /**********************************************************************/
331 /* Render pipeline stage */
332 /**********************************************************************/
338 const struct tnl_pipeline_stage _tnl_render_stage
=
341 NULL
, /* private data */
343 NULL
, /* destructor */