Move away from using the ctx->_TriangleCaps bitfield.
[mesa.git] / src / mesa / tnl / t_vb_render.c
1
2 /*
3 * Mesa 3-D graphics library
4 * Version: 5.1
5 *
6 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Keith Whitwell <keith@tungstengraphics.com>
27 */
28
29
30 /*
31 * Render whole vertex buffers, including projection of vertices from
32 * clip space and clipping of primitives.
33 *
34 * This file makes calls to project vertices and to the point, line
35 * and triangle rasterizers via the function pointers:
36 *
37 * context->Driver.Render.*
38 *
39 */
40
41
42 #include "glheader.h"
43 #include "context.h"
44 #include "enums.h"
45 #include "macros.h"
46 #include "imports.h"
47 #include "mtypes.h"
48
49 #include "math/m_matrix.h"
50 #include "math/m_xform.h"
51
52 #include "t_pipeline.h"
53
54
55
56 /**********************************************************************/
57 /* Clip single primitives */
58 /**********************************************************************/
59
60
61 #define W(i) coord[i][3]
62 #define Z(i) coord[i][2]
63 #define Y(i) coord[i][1]
64 #define X(i) coord[i][0]
65 #define SIZE 4
66 #define TAG(x) x##_4
67 #include "t_vb_cliptmp.h"
68
69
70
71 /**********************************************************************/
72 /* Clip and render whole begin/end objects */
73 /**********************************************************************/
74
75 #define NEED_EDGEFLAG_SETUP (ctx->Polygon.FrontMode != GL_FILL || ctx->Polygon.BackMode != GL_FILL)
76 #define EDGEFLAG_GET(idx) VB->EdgeFlag[idx]
77 #define EDGEFLAG_SET(idx, val) VB->EdgeFlag[idx] = val
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 & 0x3f)) \
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 & 0x3f)) \
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 & 0x3f)) \
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 line_func LineFunc = tnl->Driver.Render.Line; \
124 const triangle_func TriangleFunc = tnl->Driver.Render.Triangle; \
125 const 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 RESET_OCCLUSION ctx->OcclusionResult = GL_TRUE
134 #define PRESERVE_VB_DEFS
135 #include "t_vb_rendertmp.h"
136
137
138
139 /* Elts, with the possibility of clipping.
140 */
141 #undef ELT
142 #undef TAG
143 #define ELT(x) elt[x]
144 #define TAG(x) clip_##x##_elts
145 #include "t_vb_rendertmp.h"
146
147 /* TODO: do this for all primitives, verts and elts:
148 */
149 static void clip_elt_triangles( GLcontext *ctx,
150 GLuint start,
151 GLuint count,
152 GLuint flags )
153 {
154 TNLcontext *tnl = TNL_CONTEXT(ctx);
155 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;
160 GLuint j;
161 (void) flags;
162
163 tnl->Driver.Render.PrimitiveNotify( ctx, GL_TRIANGLES );
164
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;
170 if (ormask) {
171 if (start < j)
172 render_tris( ctx, start, j, 0 );
173 if (!(c1&c2&c3&0x3f))
174 clip_tri_4( ctx, elt[j], elt[j+1], elt[j+2], ormask );
175 start = j+3;
176 }
177 }
178
179 if (start < j)
180 render_tris( ctx, start, j, 0 );
181 }
182
183 /**********************************************************************/
184 /* Render whole begin/end objects */
185 /**********************************************************************/
186
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
190
191
192 /* Vertices, no clipping.
193 */
194 #define RENDER_POINTS( start, count ) \
195 tnl->Driver.Render.Points( ctx, start, count )
196
197 #define RENDER_LINE( v1, v2 ) \
198 LineFunc( ctx, v1, v2 )
199
200 #define RENDER_TRI( v1, v2, v3 ) \
201 TriangleFunc( ctx, v1, v2, v3 )
202
203 #define RENDER_QUAD( v1, v2, v3, v4 ) \
204 QuadFunc( ctx, v1, v2, v3, v4 )
205
206 #define TAG(x) _tnl_##x##_verts
207
208 #define LOCAL_VARS \
209 TNLcontext *tnl = TNL_CONTEXT(ctx); \
210 struct vertex_buffer *VB = &tnl->vb; \
211 const GLuint * const elt = VB->Elts; \
212 const line_func LineFunc = tnl->Driver.Render.Line; \
213 const triangle_func TriangleFunc = tnl->Driver.Render.Triangle; \
214 const quad_func QuadFunc = tnl->Driver.Render.Quad; \
215 const GLboolean stipple = ctx->Line.StippleFlag; \
216 (void) (LineFunc && TriangleFunc && QuadFunc); \
217 (void) elt; (void) stipple
218
219 #define RESET_STIPPLE if (stipple) tnl->Driver.Render.ResetLineStipple( ctx )
220 #define RESET_OCCLUSION ctx->OcclusionResult = GL_TRUE
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"
225
226
227 /* Elts, no clipping.
228 */
229 #undef ELT
230 #define TAG(x) _tnl_##x##_elts
231 #define ELT(x) elt[x]
232 #include "t_vb_rendertmp.h"
233
234
235 /**********************************************************************/
236 /* Helper functions for drivers */
237 /**********************************************************************/
238
239 void _tnl_RenderClippedPolygon( GLcontext *ctx, const GLuint *elts, GLuint n )
240 {
241 TNLcontext *tnl = TNL_CONTEXT(ctx);
242 struct vertex_buffer *VB = &tnl->vb;
243 GLuint *tmp = VB->Elts;
244
245 VB->Elts = (GLuint *)elts;
246 tnl->Driver.Render.PrimTabElts[GL_POLYGON]( ctx, 0, n, PRIM_BEGIN|PRIM_END );
247 VB->Elts = tmp;
248 }
249
250 void _tnl_RenderClippedLine( GLcontext *ctx, GLuint ii, GLuint jj )
251 {
252 TNLcontext *tnl = TNL_CONTEXT(ctx);
253 tnl->Driver.Render.Line( ctx, ii, jj );
254 }
255
256
257
258 /**********************************************************************/
259 /* Clip and render whole vertex buffers */
260 /**********************************************************************/
261
262
263 static GLboolean run_render( GLcontext *ctx,
264 struct gl_pipeline_stage *stage )
265 {
266 TNLcontext *tnl = TNL_CONTEXT(ctx);
267 struct vertex_buffer *VB = &tnl->vb;
268 GLuint new_inputs = stage->changed_inputs;
269 render_func *tab;
270 GLint pass = 0;
271
272 /* Allow the drivers to lock before projected verts are built so
273 * that window coordinates are guarenteed not to change before
274 * rendering.
275 */
276 ASSERT(tnl->Driver.Render.Start);
277
278 tnl->Driver.Render.Start( ctx );
279
280 ASSERT(tnl->Driver.Render.BuildVertices);
281 ASSERT(tnl->Driver.Render.PrimitiveNotify);
282 ASSERT(tnl->Driver.Render.Points);
283 ASSERT(tnl->Driver.Render.Line);
284 ASSERT(tnl->Driver.Render.Triangle);
285 ASSERT(tnl->Driver.Render.Quad);
286 ASSERT(tnl->Driver.Render.ResetLineStipple);
287 ASSERT(tnl->Driver.Render.Interp);
288 ASSERT(tnl->Driver.Render.CopyPV);
289 ASSERT(tnl->Driver.Render.ClippedLine);
290 ASSERT(tnl->Driver.Render.ClippedPolygon);
291 ASSERT(tnl->Driver.Render.Finish);
292
293 tnl->Driver.Render.BuildVertices( ctx, 0, VB->Count, new_inputs );
294
295 if (VB->ClipOrMask) {
296 tab = VB->Elts ? clip_render_tab_elts : clip_render_tab_verts;
297 clip_render_tab_elts[GL_TRIANGLES] = clip_elt_triangles;
298 }
299 else {
300 tab = (VB->Elts ?
301 tnl->Driver.Render.PrimTabElts :
302 tnl->Driver.Render.PrimTabVerts);
303 }
304
305 do
306 {
307 GLuint i, length, flags = 0;
308 for (i = VB->FirstPrimitive ; !(flags & PRIM_LAST) ; i += length)
309 {
310 flags = VB->Primitive[i];
311 length= VB->PrimitiveLength[i];
312 ASSERT(length || (flags & PRIM_LAST));
313 ASSERT((flags & PRIM_MODE_MASK) <= GL_POLYGON+1);
314
315 if (MESA_VERBOSE & VERBOSE_PRIMS)
316 _mesa_debug(NULL, "MESA prim %s %d..%d\n",
317 _mesa_lookup_enum_by_nr(flags & PRIM_MODE_MASK),
318 i, i+length);
319
320 if (length)
321 tab[flags & PRIM_MODE_MASK]( ctx, i, i + length, flags );
322 }
323 } while (tnl->Driver.Render.Multipass &&
324 tnl->Driver.Render.Multipass( ctx, ++pass ));
325
326
327 tnl->Driver.Render.Finish( ctx );
328 /* _swrast_flush(ctx); */
329 /* usleep(1000000); */
330 return GL_FALSE; /* finished the pipe */
331 }
332
333
334 /**********************************************************************/
335 /* Render pipeline stage */
336 /**********************************************************************/
337
338
339
340 /* Quite a bit of work involved in finding out the inputs for the
341 * render stage.
342 */
343 static void check_render( GLcontext *ctx, struct gl_pipeline_stage *stage )
344 {
345 GLuint inputs = VERT_BIT_CLIP;
346 GLuint i;
347
348 if (ctx->Visual.rgbMode) {
349 inputs |= VERT_BIT_COLOR0;
350
351 if (NEED_SECONDARY_COLOR(ctx))
352 inputs |= VERT_BIT_COLOR1;
353
354 if (ctx->Texture._EnabledCoordUnits) {
355 for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++) {
356 if (ctx->Texture._EnabledCoordUnits & (1 << i))
357 inputs |= VERT_BIT_TEX(i);
358 }
359 }
360 }
361 else {
362 inputs |= VERT_BIT_INDEX;
363 }
364
365 if (ctx->Point._Attenuated)
366 inputs |= VERT_BIT_POINT_SIZE;
367
368 /* How do drivers turn this off?
369 */
370 if (ctx->Fog.Enabled)
371 inputs |= VERT_BIT_FOG;
372
373 if (ctx->Polygon.FrontMode != GL_FILL || ctx->Polygon.BackMode != GL_FILL)
374 inputs |= VERT_BIT_EDGEFLAG;
375
376 if (ctx->RenderMode==GL_FEEDBACK)
377 inputs |= VERT_BITS_TEX_ANY;
378
379 stage->inputs = inputs;
380 }
381
382
383
384
385 static void dtr( struct gl_pipeline_stage *stage )
386 {
387 }
388
389
390 const struct gl_pipeline_stage _tnl_render_stage =
391 {
392 "render", /* name */
393 (_NEW_BUFFERS |
394 _DD_NEW_SEPARATE_SPECULAR |
395 _DD_NEW_FLATSHADE |
396 _NEW_TEXTURE|
397 _NEW_LIGHT|
398 _NEW_POINT|
399 _NEW_FOG|
400 _DD_NEW_TRI_UNFILLED |
401 _NEW_RENDERMODE), /* re-check (new inputs, interp function) */
402 0, /* re-run (always runs) */
403 GL_TRUE, /* active? */
404 0, /* inputs (set in check_render) */
405 0, /* outputs */
406 0, /* changed_inputs */
407 NULL, /* private data */
408 dtr, /* destructor */
409 check_render, /* check */
410 run_render /* run */
411 };