mesa: prefix a bunch of #include lines with "main/".
[mesa.git] / src / mesa / tnl / t_vb_render.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 *
24 * Authors:
25 * Keith Whitwell <keith@tungstengraphics.com>
26 */
27
28
29 /*
30 * Render whole vertex buffers, including projection of vertices from
31 * clip space and clipping of primitives.
32 *
33 * This file makes calls to project vertices and to the point, line
34 * and triangle rasterizers via the function pointers:
35 *
36 * context->Driver.Render.*
37 *
38 */
39
40
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
48 #include "t_pipeline.h"
49
50
51
52 /**********************************************************************/
53 /* Clip single primitives */
54 /**********************************************************************/
55
56
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]
61 #define SIZE 4
62 #define TAG(x) x##_4
63 #include "t_vb_cliptmp.h"
64
65
66
67 /**********************************************************************/
68 /* Clip and render whole begin/end objects */
69 /**********************************************************************/
70
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
74
75
76 /* This does NOT include the CLIP_USER_BIT! */
77 #define CLIPMASK (CLIP_FRUSTUM_BITS | CLIP_CULL_BIT)
78
79
80 /* Vertices, with the possibility of clipping.
81 */
82 #define RENDER_POINTS( start, count ) \
83 tnl->Driver.Render.Points( ctx, start, count )
84
85 #define RENDER_LINE( v1, v2 ) \
86 do { \
87 GLubyte c1 = mask[v1], c2 = mask[v2]; \
88 GLubyte ormask = c1|c2; \
89 if (!ormask) \
90 LineFunc( ctx, v1, v2 ); \
91 else if (!(c1 & c2 & CLIPMASK)) \
92 clip_line_4( ctx, v1, v2, ormask ); \
93 } while (0)
94
95 #define RENDER_TRI( v1, v2, v3 ) \
96 do { \
97 GLubyte c1 = mask[v1], c2 = mask[v2], c3 = mask[v3]; \
98 GLubyte ormask = c1|c2|c3; \
99 if (!ormask) \
100 TriangleFunc( ctx, v1, v2, v3 ); \
101 else if (!(c1 & c2 & c3 & CLIPMASK)) \
102 clip_tri_4( ctx, v1, v2, v3, ormask ); \
103 } while (0)
104
105 #define RENDER_QUAD( v1, v2, v3, v4 ) \
106 do { \
107 GLubyte c1 = mask[v1], c2 = mask[v2]; \
108 GLubyte c3 = mask[v3], c4 = mask[v4]; \
109 GLubyte ormask = c1|c2|c3|c4; \
110 if (!ormask) \
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 ); \
114 } while (0)
115
116
117 #define LOCAL_VARS \
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;
129
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"
135
136
137
138 /* Elts, with the possibility of clipping.
139 */
140 #undef ELT
141 #undef TAG
142 #define ELT(x) elt[x]
143 #define TAG(x) clip_##x##_elts
144 #include "t_vb_rendertmp.h"
145
146 /* TODO: do this for all primitives, verts and elts:
147 */
148 static void clip_elt_triangles( GLcontext *ctx,
149 GLuint start,
150 GLuint count,
151 GLuint flags )
152 {
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;
159 GLuint j;
160 (void) flags;
161
162 tnl->Driver.Render.PrimitiveNotify( ctx, GL_TRIANGLES );
163
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;
169 if (ormask) {
170 if (start < j)
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 );
174 start = j+3;
175 }
176 }
177
178 if (start < j)
179 render_tris( ctx, start, j, 0 );
180 }
181
182 /**********************************************************************/
183 /* Render whole begin/end objects */
184 /**********************************************************************/
185
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
189
190
191 /* Vertices, no clipping.
192 */
193 #define RENDER_POINTS( start, count ) \
194 tnl->Driver.Render.Points( ctx, start, count )
195
196 #define RENDER_LINE( v1, v2 ) \
197 LineFunc( ctx, v1, v2 )
198
199 #define RENDER_TRI( v1, v2, v3 ) \
200 TriangleFunc( ctx, v1, v2, v3 )
201
202 #define RENDER_QUAD( v1, v2, v3, v4 ) \
203 QuadFunc( ctx, v1, v2, v3, v4 )
204
205 #define TAG(x) _tnl_##x##_verts
206
207 #define LOCAL_VARS \
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
217
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"
223
224
225 /* Elts, no clipping.
226 */
227 #undef ELT
228 #define TAG(x) _tnl_##x##_elts
229 #define ELT(x) elt[x]
230 #include "t_vb_rendertmp.h"
231
232
233 /**********************************************************************/
234 /* Helper functions for drivers */
235 /**********************************************************************/
236
237 void _tnl_RenderClippedPolygon( GLcontext *ctx, const GLuint *elts, GLuint n )
238 {
239 TNLcontext *tnl = TNL_CONTEXT(ctx);
240 struct vertex_buffer *VB = &tnl->vb;
241 GLuint *tmp = VB->Elts;
242
243 VB->Elts = (GLuint *)elts;
244 tnl->Driver.Render.PrimTabElts[GL_POLYGON]( ctx, 0, n, PRIM_BEGIN|PRIM_END );
245 VB->Elts = tmp;
246 }
247
248 void _tnl_RenderClippedLine( GLcontext *ctx, GLuint ii, GLuint jj )
249 {
250 TNLcontext *tnl = TNL_CONTEXT(ctx);
251 tnl->Driver.Render.Line( ctx, ii, jj );
252 }
253
254
255
256 /**********************************************************************/
257 /* Clip and render whole vertex buffers */
258 /**********************************************************************/
259
260
261 static GLboolean run_render( GLcontext *ctx,
262 struct tnl_pipeline_stage *stage )
263 {
264 TNLcontext *tnl = TNL_CONTEXT(ctx);
265 struct vertex_buffer *VB = &tnl->vb;
266 tnl_render_func *tab;
267 GLint pass = 0;
268
269 /* Allow the drivers to lock before projected verts are built so
270 * that window coordinates are guarenteed not to change before
271 * rendering.
272 */
273 ASSERT(tnl->Driver.Render.Start);
274
275 tnl->Driver.Render.Start( ctx );
276
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);
289
290 tnl->Driver.Render.BuildVertices( ctx, 0, VB->Count, ~0 );
291
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;
295 }
296 else {
297 tab = (VB->Elts ?
298 tnl->Driver.Render.PrimTabElts :
299 tnl->Driver.Render.PrimTabVerts);
300 }
301
302 do
303 {
304 GLuint i;
305
306 for (i = 0 ; i < VB->PrimitiveCount ; i++)
307 {
308 GLuint prim = _tnl_translate_prim(&VB->Primitive[i]);
309 GLuint start = VB->Primitive[i].start;
310 GLuint length = VB->Primitive[i].count;
311
312 assert((prim & PRIM_MODE_MASK) <= GL_POLYGON);
313
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);
318
319 if (length)
320 tab[prim & PRIM_MODE_MASK]( ctx, start, start + length, prim );
321 }
322 } while (tnl->Driver.Render.Multipass &&
323 tnl->Driver.Render.Multipass( ctx, ++pass ));
324
325 tnl->Driver.Render.Finish( ctx );
326
327 return GL_FALSE; /* finished the pipe */
328 }
329
330
331 /**********************************************************************/
332 /* Render pipeline stage */
333 /**********************************************************************/
334
335
336
337
338
339 const struct tnl_pipeline_stage _tnl_render_stage =
340 {
341 "render", /* name */
342 NULL, /* private data */
343 NULL, /* creator */
344 NULL, /* destructor */
345 NULL, /* validate */
346 run_render /* run */
347 };