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