2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS 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
22 * OTHER DEALINGS IN THE SOFTWARE.
25 * Keith Whitwell <keithw@vmware.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.*
41 #include "main/glheader.h"
42 #include "main/context.h"
43 #include "main/enums.h"
44 #include "main/macros.h"
45 #include "main/imports.h"
46 #include "main/mtypes.h"
47 #include "math/m_xform.h"
49 #include "t_pipeline.h"
53 /**********************************************************************/
54 /* Clip single primitives */
55 /**********************************************************************/
58 #define W(i) coord[i][3]
59 #define Z(i) coord[i][2]
60 #define Y(i) coord[i][1]
61 #define X(i) coord[i][0]
64 #include "t_vb_cliptmp.h"
68 /**********************************************************************/
69 /* Clip and render whole begin/end objects */
70 /**********************************************************************/
72 #define NEED_EDGEFLAG_SETUP (ctx->Polygon.FrontMode != GL_FILL || ctx->Polygon.BackMode != GL_FILL)
73 #define EDGEFLAG_GET(idx) VB->EdgeFlag[idx]
74 #define EDGEFLAG_SET(idx, val) VB->EdgeFlag[idx] = val
77 /* This does NOT include the CLIP_USER_BIT! */
78 #define CLIPMASK (CLIP_FRUSTUM_BITS | CLIP_CULL_BIT)
81 /* Vertices, with the possibility of clipping.
83 #define RENDER_POINTS( start, count ) \
84 tnl->Driver.Render.Points( ctx, start, count )
86 #define RENDER_LINE( v1, v2 ) \
88 GLubyte c1 = mask[v1], c2 = mask[v2]; \
89 GLubyte ormask = c1|c2; \
91 LineFunc( ctx, v1, v2 ); \
92 else if (!(c1 & c2 & CLIPMASK)) \
93 clip_line_4( ctx, v1, v2, ormask ); \
96 #define RENDER_TRI( v1, v2, v3 ) \
98 GLubyte c1 = mask[v1], c2 = mask[v2], c3 = mask[v3]; \
99 GLubyte ormask = c1|c2|c3; \
101 TriangleFunc( ctx, v1, v2, v3 ); \
102 else if (!(c1 & c2 & c3 & CLIPMASK)) \
103 clip_tri_4( ctx, v1, v2, v3, ormask ); \
106 #define RENDER_QUAD( v1, v2, v3, v4 ) \
108 GLubyte c1 = mask[v1], c2 = mask[v2]; \
109 GLubyte c3 = mask[v3], c4 = mask[v4]; \
110 GLubyte ormask = c1|c2|c3|c4; \
112 QuadFunc( ctx, v1, v2, v3, v4 ); \
113 else if (!(c1 & c2 & c3 & c4 & CLIPMASK)) \
114 clip_quad_4( ctx, v1, v2, v3, v4, ormask ); \
119 TNLcontext *tnl = TNL_CONTEXT(ctx); \
120 struct vertex_buffer *VB = &tnl->vb; \
121 const GLuint * const elt = VB->Elts; \
122 const GLubyte *mask = VB->ClipMask; \
123 const GLuint sz = VB->ClipPtr->size; \
124 const tnl_line_func LineFunc = tnl->Driver.Render.Line; \
125 const tnl_triangle_func TriangleFunc = tnl->Driver.Render.Triangle; \
126 const tnl_quad_func QuadFunc = tnl->Driver.Render.Quad; \
127 const GLboolean stipple = ctx->Line.StippleFlag; \
128 (void) (LineFunc && TriangleFunc && QuadFunc); \
129 (void) elt; (void) mask; (void) sz; (void) stipple;
131 #define TAG(x) clip_##x##_verts
132 #define INIT(x) tnl->Driver.Render.PrimitiveNotify( ctx, x )
133 #define RESET_STIPPLE if (stipple) tnl->Driver.Render.ResetLineStipple( ctx )
134 #define PRESERVE_VB_DEFS
135 #include "t_vb_rendertmp.h"
139 /* Elts, with the possibility of clipping.
143 #define ELT(x) elt[x]
144 #define TAG(x) clip_##x##_elts
145 #include "t_vb_rendertmp.h"
147 /* TODO: do this for all primitives, verts and elts:
149 static void clip_elt_triangles( struct gl_context
*ctx
,
154 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
155 tnl_render_func render_tris
= tnl
->Driver
.Render
.PrimTabElts
[GL_TRIANGLES
];
156 struct vertex_buffer
*VB
= &tnl
->vb
;
157 const GLuint
* const elt
= VB
->Elts
;
158 GLubyte
*mask
= VB
->ClipMask
;
159 GLuint last
= count
-2;
163 tnl
->Driver
.Render
.PrimitiveNotify( ctx
, GL_TRIANGLES
);
165 for (j
=start
; j
< last
; j
+=3 ) {
166 GLubyte c1
= mask
[elt
[j
]];
167 GLubyte c2
= mask
[elt
[j
+1]];
168 GLubyte c3
= mask
[elt
[j
+2]];
169 GLubyte ormask
= c1
|c2
|c3
;
172 render_tris( ctx
, start
, j
, 0 );
173 if (!(c1
&c2
&c3
&CLIPMASK
))
174 clip_tri_4( ctx
, elt
[j
], elt
[j
+1], elt
[j
+2], ormask
);
180 render_tris( ctx
, start
, j
, 0 );
183 /**********************************************************************/
184 /* Render whole begin/end objects */
185 /**********************************************************************/
187 #define NEED_EDGEFLAG_SETUP (ctx->Polygon.FrontMode != GL_FILL || ctx->Polygon.BackMode != GL_FILL)
188 #define EDGEFLAG_GET(idx) VB->EdgeFlag[idx]
189 #define EDGEFLAG_SET(idx, val) VB->EdgeFlag[idx] = val
192 /* Vertices, no clipping.
194 #define RENDER_POINTS( start, count ) \
195 tnl->Driver.Render.Points( ctx, start, count )
197 #define RENDER_LINE( v1, v2 ) \
198 LineFunc( ctx, v1, v2 )
200 #define RENDER_TRI( v1, v2, v3 ) \
201 TriangleFunc( ctx, v1, v2, v3 )
203 #define RENDER_QUAD( v1, v2, v3, v4 ) \
204 QuadFunc( ctx, v1, v2, v3, v4 )
206 #define TAG(x) _tnl_##x##_verts
209 TNLcontext *tnl = TNL_CONTEXT(ctx); \
210 struct vertex_buffer *VB = &tnl->vb; \
211 const GLuint * const elt = VB->Elts; \
212 const tnl_line_func LineFunc = tnl->Driver.Render.Line; \
213 const tnl_triangle_func TriangleFunc = tnl->Driver.Render.Triangle; \
214 const tnl_quad_func QuadFunc = tnl->Driver.Render.Quad; \
215 const GLboolean stipple = ctx->Line.StippleFlag; \
216 (void) (LineFunc && TriangleFunc && QuadFunc); \
217 (void) elt; (void) stipple
219 #define RESET_STIPPLE if (stipple) tnl->Driver.Render.ResetLineStipple( ctx )
220 #define INIT(x) tnl->Driver.Render.PrimitiveNotify( ctx, x )
221 #define RENDER_TAB_QUALIFIER
222 #define PRESERVE_VB_DEFS
223 #include "t_vb_rendertmp.h"
226 /* Elts, no clipping.
229 #define TAG(x) _tnl_##x##_elts
230 #define ELT(x) elt[x]
231 #include "t_vb_rendertmp.h"
234 /**********************************************************************/
235 /* Helper functions for drivers */
236 /**********************************************************************/
238 void _tnl_RenderClippedPolygon( struct gl_context
*ctx
, const GLuint
*elts
, GLuint n
)
240 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
241 struct vertex_buffer
*VB
= &tnl
->vb
;
242 GLuint
*tmp
= VB
->Elts
;
244 VB
->Elts
= (GLuint
*)elts
;
245 tnl
->Driver
.Render
.PrimTabElts
[GL_POLYGON
]( ctx
, 0, n
, PRIM_BEGIN
|PRIM_END
);
249 void _tnl_RenderClippedLine( struct gl_context
*ctx
, GLuint ii
, GLuint jj
)
251 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
252 tnl
->Driver
.Render
.Line( ctx
, ii
, jj
);
257 /**********************************************************************/
258 /* Clip and render whole vertex buffers */
259 /**********************************************************************/
262 static GLboolean
run_render( struct gl_context
*ctx
,
263 struct tnl_pipeline_stage
*stage
)
265 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
266 struct vertex_buffer
*VB
= &tnl
->vb
;
267 tnl_render_func
*tab
;
270 /* Allow the drivers to lock before projected verts are built so
271 * that window coordinates are guarenteed not to change before
274 assert(tnl
->Driver
.Render
.Start
);
276 tnl
->Driver
.Render
.Start( ctx
);
278 assert(tnl
->Driver
.Render
.BuildVertices
);
279 assert(tnl
->Driver
.Render
.PrimitiveNotify
);
280 assert(tnl
->Driver
.Render
.Points
);
281 assert(tnl
->Driver
.Render
.Line
);
282 assert(tnl
->Driver
.Render
.Triangle
);
283 assert(tnl
->Driver
.Render
.Quad
);
284 assert(tnl
->Driver
.Render
.ResetLineStipple
);
285 assert(tnl
->Driver
.Render
.Interp
);
286 assert(tnl
->Driver
.Render
.CopyPV
);
287 assert(tnl
->Driver
.Render
.ClippedLine
);
288 assert(tnl
->Driver
.Render
.ClippedPolygon
);
289 assert(tnl
->Driver
.Render
.Finish
);
291 tnl
->Driver
.Render
.BuildVertices( ctx
, 0, VB
->Count
, ~0 );
293 if (VB
->ClipOrMask
) {
294 tab
= VB
->Elts
? clip_render_tab_elts
: clip_render_tab_verts
;
295 clip_render_tab_elts
[GL_TRIANGLES
] = clip_elt_triangles
;
299 tnl
->Driver
.Render
.PrimTabElts
:
300 tnl
->Driver
.Render
.PrimTabVerts
);
307 for (i
= 0 ; i
< VB
->PrimitiveCount
; i
++)
309 GLuint prim
= _tnl_translate_prim(&VB
->Primitive
[i
]);
310 GLuint start
= VB
->Primitive
[i
].start
;
311 GLuint length
= VB
->Primitive
[i
].count
;
313 assert((prim
& PRIM_MODE_MASK
) <= GL_POLYGON
);
315 if (MESA_VERBOSE
& VERBOSE_PRIMS
)
316 _mesa_debug(NULL
, "MESA prim %s %d..%d\n",
317 _mesa_lookup_enum_by_nr(prim
& PRIM_MODE_MASK
),
318 start
, start
+length
);
321 tab
[prim
& PRIM_MODE_MASK
]( ctx
, start
, start
+ length
, prim
);
323 } while (tnl
->Driver
.Render
.Multipass
&&
324 tnl
->Driver
.Render
.Multipass( ctx
, ++pass
));
326 tnl
->Driver
.Render
.Finish( ctx
);
328 return GL_FALSE
; /* finished the pipe */
332 /**********************************************************************/
333 /* Render pipeline stage */
334 /**********************************************************************/
340 const struct tnl_pipeline_stage _tnl_render_stage
=
343 NULL
, /* private data */
345 NULL
, /* destructor */