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