03c8943de198611ff43af1f1d25f05a5c2c3f327
[mesa.git] / src / mesa / tnl / t_vb_render.c
1 /* $Id: t_vb_render.c,v 1.15 2001/03/12 00:48:44 gareth Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
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.BuildProjectedVertices()
39 *
40 * context->Driver.PointsFunc()
41 * context->Driver.LineFunc()
42 * context->Driver.TriangleFunc()
43 * context->Driver.QuadFunc()
44 *
45 * context->Driver.RenderTabVerts[]
46 * context->Driver.RenderTabElts[]
47 *
48 * None of these may be null.
49 */
50
51
52 #include "glheader.h"
53 #include "context.h"
54 #include "macros.h"
55 #include "mem.h"
56 #include "mtypes.h"
57 #include "mmath.h"
58
59 #include "math/m_matrix.h"
60 #include "math/m_xform.h"
61
62 #include "t_pipeline.h"
63
64
65
66 /**********************************************************************/
67 /* Clip single primitives */
68 /**********************************************************************/
69
70
71 #if defined(USE_IEEE)
72 #define NEGATIVE(x) ((*(GLuint *)&x) & (1<<31))
73 #define DIFFERENT_SIGNS(x,y) (((*(GLuint *)&x)^(*(GLuint *)&y)) & (1<<31))
74 #else
75 #define NEGATIVE(x) (x < 0)
76 #define DIFFERENT_SIGNS(x,y) (x * y <= 0 && x - y != 0)
77 /* Could just use (x*y<0) except for the flatshading requirements.
78 * Maybe there's a better way?
79 */
80 #endif
81
82 #define LINTERP_SZ( t, vec, to, a, b, sz ) \
83 do { \
84 switch (sz) { \
85 case 2: vec[to][2] = 0.0; \
86 case 3: vec[to][3] = 1.0; \
87 } \
88 switch (sz) { \
89 case 4: vec[to][3] = LINTERP( t, vec[a][3], vec[b][3] ); \
90 case 3: vec[to][2] = LINTERP( t, vec[a][2], vec[b][2] ); \
91 case 2: vec[to][1] = LINTERP( t, vec[a][1], vec[b][1] ); \
92 vec[to][0] = LINTERP( t, vec[a][0], vec[b][0] ); \
93 } \
94 } while(0)
95
96 #define LINTERP_4F( t, vec, to, a, b, sz ) \
97 do { \
98 vec[to][3] = LINTERP( t, vec[a][3], vec[b][3] ); \
99 vec[to][2] = LINTERP( t, vec[a][2], vec[b][2] ); \
100 vec[to][1] = LINTERP( t, vec[a][1], vec[b][1] ); \
101 vec[to][0] = LINTERP( t, vec[a][0], vec[b][0] ); \
102 } while (0)
103
104 #define W(i) coord[i][3]
105 #define Z(i) coord[i][2]
106 #define Y(i) coord[i][1]
107 #define X(i) coord[i][0]
108 #define SIZE 4
109 #define TAG(x) x##_4
110 #include "t_vb_cliptmp.h"
111
112
113
114 /**********************************************************************/
115 /* Clip and render whole begin/end objects */
116 /**********************************************************************/
117
118 #define NEED_EDGEFLAG_SETUP (ctx->_TriangleCaps & DD_TRI_UNFILLED)
119 #define EDGEFLAG_GET(idx) VB->EdgeFlag[idx]
120 #define EDGEFLAG_SET(idx, val) VB->EdgeFlag[idx] = val
121
122
123 /* Vertices, with the possibility of clipping.
124 */
125 #define RENDER_POINTS( start, count ) \
126 ctx->Driver.PointsFunc( ctx, start, count )
127
128 #define RENDER_LINE( v1, v2 ) \
129 do { \
130 GLubyte c1 = mask[v1], c2 = mask[v2]; \
131 GLubyte ormask = c1|c2; \
132 if (!ormask) \
133 LineFunc( ctx, v1, v2 ); \
134 else if (!(c1 & c2 & 0x3f)) \
135 clip_line_4( ctx, v1, v2, ormask ); \
136 } while (0)
137
138 #define RENDER_TRI( v1, v2, v3 ) \
139 do { \
140 GLubyte c1 = mask[v1], c2 = mask[v2], c3 = mask[v3]; \
141 GLubyte ormask = c1|c2|c3; \
142 if (!ormask) \
143 TriangleFunc( ctx, v1, v2, v3 ); \
144 else if (!(c1 & c2 & c3 & 0x3f)) \
145 clip_tri_4( ctx, v1, v2, v3, ormask ); \
146 } while (0)
147
148 #define RENDER_QUAD( v1, v2, v3, v4 ) \
149 do { \
150 GLubyte c1 = mask[v1], c2 = mask[v2]; \
151 GLubyte c3 = mask[v3], c4 = mask[v4]; \
152 GLubyte ormask = c1|c2|c3|c4; \
153 if (!ormask) \
154 QuadFunc( ctx, v1, v2, v3, v4 ); \
155 else if (!(c1 & c2 & c3 & c4 & 0x3f)) \
156 clip_quad_4( ctx, v1, v2, v3, v4, ormask ); \
157 } while (0)
158
159
160 #define LOCAL_VARS \
161 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb; \
162 const GLuint * const elt = VB->Elts; \
163 const GLubyte *mask = VB->ClipMask; \
164 const GLuint sz = VB->ClipPtr->size; \
165 const line_func LineFunc = ctx->Driver.LineFunc; \
166 const triangle_func TriangleFunc = ctx->Driver.TriangleFunc; \
167 const quad_func QuadFunc = ctx->Driver.QuadFunc; \
168 const GLboolean stipple = ctx->Line.StippleFlag; \
169 (void) (LineFunc && TriangleFunc && QuadFunc); \
170 (void) elt; (void) mask; (void) sz; (void) stipple;
171
172 #define TAG(x) clip_##x##_verts
173 #define INIT(x) ctx->Driver.RenderPrimitive( ctx, x )
174 #define RESET_STIPPLE if (stipple) ctx->Driver.ResetLineStipple( ctx )
175 #define RESET_OCCLUSION ctx->OcclusionResult = GL_TRUE
176 #define PRESERVE_VB_DEFS
177 #include "t_vb_rendertmp.h"
178
179
180
181 /* Elts, with the possibility of clipping.
182 */
183 #undef ELT
184 #undef TAG
185 #define ELT(x) elt[x]
186 #define TAG(x) clip_##x##_elts
187 #include "t_vb_rendertmp.h"
188
189 /* TODO: do this for all primitives, verts and elts:
190 */
191 static void clip_elt_triangles( GLcontext *ctx,
192 GLuint start,
193 GLuint count,
194 GLuint flags )
195 {
196 GLuint j;
197 GLuint last = count-2;
198 render_func render_tris = ctx->Driver.RenderTabElts[GL_TRIANGLES];
199 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
200 const GLuint * const elt = VB->Elts;
201 GLubyte *mask = VB->ClipMask;
202 (void) flags;
203
204 ctx->Driver.RenderPrimitive( ctx, GL_TRIANGLES );
205
206 for (j=start; j < last; j+=3 ) {
207 GLubyte c1 = mask[elt[j]];
208 GLubyte c2 = mask[elt[j+1]];
209 GLubyte c3 = mask[elt[j+2]];
210 GLubyte ormask = c1|c2|c3;
211 if (ormask) {
212 if (start < j)
213 render_tris( ctx, start, j, 0 );
214 if (!(c1&c2&c3&0x3f))
215 clip_tri_4( ctx, elt[j], elt[j+1], elt[j+2], ormask );
216 start = j+3;
217 }
218 }
219
220 if (start < j)
221 render_tris( ctx, start, j, 0 );
222 }
223
224 /**********************************************************************/
225 /* Render whole begin/end objects */
226 /**********************************************************************/
227
228 #define NEED_EDGEFLAG_SETUP (ctx->_TriangleCaps & DD_TRI_UNFILLED)
229 #define EDGEFLAG_GET(idx) VB->EdgeFlag[idx]
230 #define EDGEFLAG_SET(idx, val) VB->EdgeFlag[idx] = val
231
232
233 /* Vertices, no clipping.
234 */
235 #define RENDER_POINTS( start, count ) \
236 ctx->Driver.PointsFunc( ctx, start, count )
237
238 #define RENDER_LINE( v1, v2 ) \
239 LineFunc( ctx, v1, v2 )
240
241 #define RENDER_TRI( v1, v2, v3 ) \
242 TriangleFunc( ctx, v1, v2, v3 )
243
244 #define RENDER_QUAD( v1, v2, v3, v4 ) \
245 QuadFunc( ctx, v1, v2, v3, v4 )
246
247 #define TAG(x) _tnl_##x##_verts
248
249 #define LOCAL_VARS \
250 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb; \
251 const GLuint * const elt = VB->Elts; \
252 const line_func LineFunc = ctx->Driver.LineFunc; \
253 const triangle_func TriangleFunc = ctx->Driver.TriangleFunc; \
254 const quad_func QuadFunc = ctx->Driver.QuadFunc; \
255 (void) (LineFunc && TriangleFunc && QuadFunc); \
256 (void) elt;
257
258 #define RESET_STIPPLE ctx->Driver.ResetLineStipple( ctx )
259 #define RESET_OCCLUSION ctx->OcclusionResult = GL_TRUE
260 #define INIT(x) ctx->Driver.RenderPrimitive( ctx, x )
261 #define RENDER_TAB_QUALIFIER
262 #define PRESERVE_VB_DEFS
263 #include "t_vb_rendertmp.h"
264
265
266 /* Elts, no clipping.
267 */
268 #undef ELT
269 #define TAG(x) _tnl_##x##_elts
270 #define ELT(x) elt[x]
271 #include "t_vb_rendertmp.h"
272
273
274
275
276 /**********************************************************************/
277 /* Clip and render whole vertex buffers */
278 /**********************************************************************/
279
280
281 static GLboolean run_render( GLcontext *ctx,
282 struct gl_pipeline_stage *stage )
283 {
284 TNLcontext *tnl = TNL_CONTEXT(ctx);
285 struct vertex_buffer *VB = &tnl->vb;
286 GLuint new_inputs = stage->changed_inputs;
287 render_func *tab;
288 GLint pass = 0;
289
290
291 /* Allow the drivers to lock before projected verts are built so
292 * that window coordinates are guarenteed not to change before
293 * rendering.
294 */
295 ctx->Driver.RenderStart( ctx );
296
297 ASSERT(ctx->Driver.BuildProjectedVertices);
298 ASSERT(ctx->Driver.RenderPrimitive);
299 ASSERT(ctx->Driver.PointsFunc);
300 ASSERT(ctx->Driver.LineFunc);
301 ASSERT(ctx->Driver.TriangleFunc);
302 ASSERT(ctx->Driver.QuadFunc);
303 ASSERT(ctx->Driver.ResetLineStipple);
304 ASSERT(ctx->Driver.RenderInterp);
305 ASSERT(ctx->Driver.RenderCopyPV);
306 ASSERT(ctx->Driver.RenderClippedLine);
307 ASSERT(ctx->Driver.RenderClippedPolygon);
308
309 ctx->Driver.BuildProjectedVertices( ctx, 0, VB->Count, new_inputs );
310
311 if (VB->ClipOrMask) {
312 tab = VB->Elts ? clip_render_tab_elts : clip_render_tab_verts;
313 clip_render_tab_elts[GL_TRIANGLES] = clip_elt_triangles;
314 }
315 else {
316 tab = VB->Elts ? ctx->Driver.RenderTabElts : ctx->Driver.RenderTabVerts;
317 }
318
319 do
320 {
321 GLuint i, length, flags = 0;
322 for (i = 0 ; !(flags & PRIM_LAST) ; i += length)
323 {
324 flags = VB->Primitive[i];
325 length= VB->PrimitiveLength[i];
326 ASSERT(length || (flags & PRIM_LAST));
327 ASSERT((flags & PRIM_MODE_MASK) <= GL_POLYGON+1);
328 if (length)
329 tab[flags & PRIM_MODE_MASK]( ctx, i, i + length, flags );
330 }
331 } while (ctx->Driver.MultipassFunc &&
332 ctx->Driver.MultipassFunc( ctx, ++pass ));
333
334
335 ctx->Driver.RenderFinish( ctx );
336 return GL_FALSE; /* finished the pipe */
337 }
338
339
340 /**********************************************************************/
341 /* Render pipeline stage */
342 /**********************************************************************/
343
344
345
346 /* Quite a bit of work involved in finding out the inputs for the
347 * render stage.
348 */
349 static void check_render( GLcontext *ctx, struct gl_pipeline_stage *stage )
350 {
351 GLuint inputs = VERT_CLIP;
352 GLuint i;
353
354 if (ctx->Visual.rgbMode) {
355 inputs |= VERT_RGBA;
356
357 if (ctx->_TriangleCaps & DD_SEPERATE_SPECULAR)
358 inputs |= VERT_SPEC_RGB;
359
360 if (ctx->Texture._ReallyEnabled) {
361 for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++) {
362 if (ctx->Texture.Unit[i]._ReallyEnabled)
363 inputs |= VERT_TEX(i);
364 }
365 }
366 }
367 else {
368 inputs |= VERT_INDEX;
369 }
370
371 if (ctx->Point._Attenuated)
372 inputs |= VERT_POINT_SIZE;
373
374 /* How do drivers turn this off?
375 */
376 if (ctx->Fog.Enabled)
377 inputs |= VERT_FOG_COORD;
378
379 if (ctx->_TriangleCaps & DD_TRI_UNFILLED)
380 inputs |= VERT_EDGE;
381
382 if (ctx->RenderMode==GL_FEEDBACK)
383 inputs |= VERT_TEX_ANY;
384
385 stage->inputs = inputs;
386 }
387
388
389
390
391 static void dtr( struct gl_pipeline_stage *stage )
392 {
393 }
394
395
396 const struct gl_pipeline_stage _tnl_render_stage =
397 {
398 "render",
399 (_NEW_BUFFERS |
400 _DD_NEW_SEPERATE_SPECULAR |
401 _DD_NEW_FLATSHADE |
402 _NEW_TEXTURE|
403 _NEW_LIGHT|
404 _NEW_POINT|
405 _NEW_FOG|
406 _DD_NEW_TRI_UNFILLED |
407 _NEW_RENDERMODE), /* re-check (new inputs, interp function) */
408 0, /* re-run (always runs) */
409 GL_TRUE, /* active */
410 0, 0, /* inputs (set in check_render), outputs */
411 0, 0, /* changed_inputs, private */
412 dtr, /* destructor */
413 check_render, /* check */
414 run_render /* run */
415 };