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.*
42 #include "main/glheader.h"
43 #include "main/context.h"
44 #include "main/enums.h"
45 #include "main/macros.h"
46 #include "main/imports.h"
47 #include "main/mtypes.h"
48 #include "math/m_xform.h"
50 #include "t_pipeline.h"
54 /**********************************************************************/
55 /* Clip single primitives */
56 /**********************************************************************/
59 #define W(i) coord[i][3]
60 #define Z(i) coord[i][2]
61 #define Y(i) coord[i][1]
62 #define X(i) coord[i][0]
65 #include "t_vb_cliptmp.h"
69 /**********************************************************************/
70 /* Clip and render whole begin/end objects */
71 /**********************************************************************/
73 #define NEED_EDGEFLAG_SETUP (ctx->Polygon.FrontMode != GL_FILL || ctx->Polygon.BackMode != GL_FILL)
74 #define EDGEFLAG_GET(idx) VB->EdgeFlag[idx]
75 #define EDGEFLAG_SET(idx, val) VB->EdgeFlag[idx] = val
78 /* This does NOT include the CLIP_USER_BIT! */
79 #define CLIPMASK (CLIP_FRUSTUM_BITS | CLIP_CULL_BIT)
82 /* Vertices, with the possibility of clipping.
84 #define RENDER_POINTS( start, count ) \
85 tnl->Driver.Render.Points( ctx, start, count )
87 #define RENDER_LINE( v1, v2 ) \
89 GLubyte c1 = mask[v1], c2 = mask[v2]; \
90 GLubyte ormask = c1|c2; \
92 LineFunc( ctx, v1, v2 ); \
93 else if (!(c1 & c2 & CLIPMASK)) \
94 clip_line_4( ctx, v1, v2, ormask ); \
97 #define RENDER_TRI( v1, v2, v3 ) \
99 GLubyte c1 = mask[v1], c2 = mask[v2], c3 = mask[v3]; \
100 GLubyte ormask = c1|c2|c3; \
102 TriangleFunc( ctx, v1, v2, v3 ); \
103 else if (!(c1 & c2 & c3 & CLIPMASK)) \
104 clip_tri_4( ctx, v1, v2, v3, ormask ); \
107 #define RENDER_QUAD( v1, v2, v3, v4 ) \
109 GLubyte c1 = mask[v1], c2 = mask[v2]; \
110 GLubyte c3 = mask[v3], c4 = mask[v4]; \
111 GLubyte ormask = c1|c2|c3|c4; \
113 QuadFunc( ctx, v1, v2, v3, v4 ); \
114 else if (!(c1 & c2 & c3 & c4 & CLIPMASK)) \
115 clip_quad_4( ctx, v1, v2, v3, v4, ormask ); \
120 TNLcontext *tnl = TNL_CONTEXT(ctx); \
121 struct vertex_buffer *VB = &tnl->vb; \
122 const GLuint * const elt = VB->Elts; \
123 const GLubyte *mask = VB->ClipMask; \
124 const GLuint sz = VB->ClipPtr->size; \
125 const tnl_line_func LineFunc = tnl->Driver.Render.Line; \
126 const tnl_triangle_func TriangleFunc = tnl->Driver.Render.Triangle; \
127 const tnl_quad_func QuadFunc = tnl->Driver.Render.Quad; \
128 const GLboolean stipple = ctx->Line.StippleFlag; \
129 (void) (LineFunc && TriangleFunc && QuadFunc); \
130 (void) elt; (void) mask; (void) sz; (void) stipple;
132 #define TAG(x) clip_##x##_verts
133 #define INIT(x) tnl->Driver.Render.PrimitiveNotify( ctx, x )
134 #define RESET_STIPPLE if (stipple) tnl->Driver.Render.ResetLineStipple( ctx )
135 #define PRESERVE_VB_DEFS
136 #include "t_vb_rendertmp.h"
140 /* Elts, with the possibility of clipping.
144 #define ELT(x) elt[x]
145 #define TAG(x) clip_##x##_elts
146 #include "t_vb_rendertmp.h"
148 /* TODO: do this for all primitives, verts and elts:
150 static void clip_elt_triangles( struct gl_context
*ctx
,
155 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
156 tnl_render_func render_tris
= tnl
->Driver
.Render
.PrimTabElts
[GL_TRIANGLES
];
157 struct vertex_buffer
*VB
= &tnl
->vb
;
158 const GLuint
* const elt
= VB
->Elts
;
159 GLubyte
*mask
= VB
->ClipMask
;
160 GLuint last
= count
-2;
164 tnl
->Driver
.Render
.PrimitiveNotify( ctx
, GL_TRIANGLES
);
166 for (j
=start
; j
< last
; j
+=3 ) {
167 GLubyte c1
= mask
[elt
[j
]];
168 GLubyte c2
= mask
[elt
[j
+1]];
169 GLubyte c3
= mask
[elt
[j
+2]];
170 GLubyte ormask
= c1
|c2
|c3
;
173 render_tris( ctx
, start
, j
, 0 );
174 if (!(c1
&c2
&c3
&CLIPMASK
))
175 clip_tri_4( ctx
, elt
[j
], elt
[j
+1], elt
[j
+2], ormask
);
181 render_tris( ctx
, start
, j
, 0 );
184 /**********************************************************************/
185 /* Render whole begin/end objects */
186 /**********************************************************************/
188 #define NEED_EDGEFLAG_SETUP (ctx->Polygon.FrontMode != GL_FILL || ctx->Polygon.BackMode != GL_FILL)
189 #define EDGEFLAG_GET(idx) VB->EdgeFlag[idx]
190 #define EDGEFLAG_SET(idx, val) VB->EdgeFlag[idx] = val
193 /* Vertices, no clipping.
195 #define RENDER_POINTS( start, count ) \
196 tnl->Driver.Render.Points( ctx, start, count )
198 #define RENDER_LINE( v1, v2 ) \
199 LineFunc( ctx, v1, v2 )
201 #define RENDER_TRI( v1, v2, v3 ) \
202 TriangleFunc( ctx, v1, v2, v3 )
204 #define RENDER_QUAD( v1, v2, v3, v4 ) \
205 QuadFunc( ctx, v1, v2, v3, v4 )
207 #define TAG(x) _tnl_##x##_verts
210 TNLcontext *tnl = TNL_CONTEXT(ctx); \
211 struct vertex_buffer *VB = &tnl->vb; \
212 const GLuint * const elt = VB->Elts; \
213 const tnl_line_func LineFunc = tnl->Driver.Render.Line; \
214 const tnl_triangle_func TriangleFunc = tnl->Driver.Render.Triangle; \
215 const tnl_quad_func QuadFunc = tnl->Driver.Render.Quad; \
216 const GLboolean stipple = ctx->Line.StippleFlag; \
217 (void) (LineFunc && TriangleFunc && QuadFunc); \
218 (void) elt; (void) stipple
220 #define RESET_STIPPLE if (stipple) tnl->Driver.Render.ResetLineStipple( ctx )
221 #define INIT(x) tnl->Driver.Render.PrimitiveNotify( ctx, x )
222 #define RENDER_TAB_QUALIFIER
223 #define PRESERVE_VB_DEFS
224 #include "t_vb_rendertmp.h"
227 /* Elts, no clipping.
230 #define TAG(x) _tnl_##x##_elts
231 #define ELT(x) elt[x]
232 #include "t_vb_rendertmp.h"
235 /**********************************************************************/
236 /* Helper functions for drivers */
237 /**********************************************************************/
239 void _tnl_RenderClippedPolygon( struct gl_context
*ctx
, const GLuint
*elts
, GLuint n
)
241 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
242 struct vertex_buffer
*VB
= &tnl
->vb
;
243 GLuint
*tmp
= VB
->Elts
;
245 VB
->Elts
= (GLuint
*)elts
;
246 tnl
->Driver
.Render
.PrimTabElts
[GL_POLYGON
]( ctx
, 0, n
, PRIM_BEGIN
|PRIM_END
);
250 void _tnl_RenderClippedLine( struct gl_context
*ctx
, GLuint ii
, GLuint jj
)
252 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
253 tnl
->Driver
.Render
.Line( ctx
, ii
, jj
);
258 /**********************************************************************/
259 /* Clip and render whole vertex buffers */
260 /**********************************************************************/
263 static GLboolean
run_render( struct gl_context
*ctx
,
264 struct tnl_pipeline_stage
*stage
)
266 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
267 struct vertex_buffer
*VB
= &tnl
->vb
;
268 tnl_render_func
*tab
;
271 /* Allow the drivers to lock before projected verts are built so
272 * that window coordinates are guarenteed not to change before
275 assert(tnl
->Driver
.Render
.Start
);
277 tnl
->Driver
.Render
.Start( ctx
);
279 assert(tnl
->Driver
.Render
.BuildVertices
);
280 assert(tnl
->Driver
.Render
.PrimitiveNotify
);
281 assert(tnl
->Driver
.Render
.Points
);
282 assert(tnl
->Driver
.Render
.Line
);
283 assert(tnl
->Driver
.Render
.Triangle
);
284 assert(tnl
->Driver
.Render
.Quad
);
285 assert(tnl
->Driver
.Render
.ResetLineStipple
);
286 assert(tnl
->Driver
.Render
.Interp
);
287 assert(tnl
->Driver
.Render
.CopyPV
);
288 assert(tnl
->Driver
.Render
.ClippedLine
);
289 assert(tnl
->Driver
.Render
.ClippedPolygon
);
290 assert(tnl
->Driver
.Render
.Finish
);
292 tnl
->Driver
.Render
.BuildVertices( ctx
, 0, VB
->Count
, ~0 );
294 if (VB
->ClipOrMask
) {
295 tab
= VB
->Elts
? clip_render_tab_elts
: clip_render_tab_verts
;
296 clip_render_tab_elts
[GL_TRIANGLES
] = clip_elt_triangles
;
300 tnl
->Driver
.Render
.PrimTabElts
:
301 tnl
->Driver
.Render
.PrimTabVerts
);
308 for (i
= 0 ; i
< VB
->PrimitiveCount
; i
++)
310 GLuint prim
= _tnl_translate_prim(&VB
->Primitive
[i
]);
311 GLuint start
= VB
->Primitive
[i
].start
;
312 GLuint length
= VB
->Primitive
[i
].count
;
314 assert((prim
& PRIM_MODE_MASK
) <= GL_POLYGON
);
316 if (MESA_VERBOSE
& VERBOSE_PRIMS
)
317 _mesa_debug(NULL
, "MESA prim %s %d..%d\n",
318 _mesa_enum_to_string(prim
& PRIM_MODE_MASK
),
319 start
, start
+length
);
322 tab
[prim
& PRIM_MODE_MASK
]( ctx
, start
, start
+ length
, prim
);
324 } while (tnl
->Driver
.Render
.Multipass
&&
325 tnl
->Driver
.Render
.Multipass( ctx
, ++pass
));
327 tnl
->Driver
.Render
.Finish( ctx
);
329 return GL_FALSE
; /* finished the pipe */
333 /**********************************************************************/
334 /* Render pipeline stage */
335 /**********************************************************************/
341 const struct tnl_pipeline_stage _tnl_render_stage
=
344 NULL
, /* private data */
346 NULL
, /* destructor */