1 /* $Id: t_vb_render.c,v 1.12 2001/01/29 20:47:39 keithw Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
27 * Keith Whitwell <keithw@valinux.com>
32 * Render whole vertex buffers, including projection of vertices from
33 * clip space and clipping of primitives.
35 * This file makes calls to project vertices and to the point, line
36 * and triangle rasterizers via the function pointers:
38 * context->Driver.BuildProjectedVertices()
40 * context->Driver.PointsFunc()
41 * context->Driver.LineFunc()
42 * context->Driver.TriangleFunc()
43 * context->Driver.QuadFunc()
45 * context->Driver.RenderTabVerts[]
46 * context->Driver.RenderTabElts[]
48 * None of these may be null.
59 #include "math/m_matrix.h"
60 #include "math/m_xform.h"
62 #include "t_pipeline.h"
66 typedef void (*clip_line_func
)( GLcontext
*ctx
,
70 typedef void (*clip_poly_func
)( GLcontext
*ctx
,
71 GLuint n
, GLuint vlist
[],
78 /**********************************************************************/
79 /* Clip single primitives */
80 /**********************************************************************/
84 #define NEGATIVE(x) ((*(GLuint *)&x) & (1<<31))
85 #define DIFFERENT_SIGNS(x,y) (((*(GLuint *)&x)^(*(GLuint *)&y)) & (1<<31))
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?
94 #define LINTERP_SZ( t, vec, to, a, b, sz ) \
97 case 2: vec[to][2] = 0.0; \
98 case 3: vec[to][3] = 1.0; \
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] ); \
108 #define LINTERP_4F( t, vec, to, a, b, sz ) \
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] ); \
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]
122 #include "t_vb_cliptmp.h"
126 /**********************************************************************/
127 /* Clip and render whole begin/end objects */
128 /**********************************************************************/
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
135 /* Vertices, with the possibility of clipping.
137 #define RENDER_POINTS( start, count ) \
138 ctx->Driver.PointsFunc( ctx, start, count )
140 #define RENDER_LINE( v1, v2 ) \
142 GLubyte c1 = mask[v1], c2 = mask[v2]; \
143 GLubyte ormask = c1|c2; \
145 LineFunc( ctx, v1, v2 ); \
146 else if (!(c1 & c2 & 0x3f)) \
147 clip_line_4( ctx, v1, v2, ormask ); \
150 #define RENDER_TRI( v1, v2, v3 ) \
152 GLubyte c1 = mask[v1], c2 = mask[v2], c3 = mask[v3]; \
153 GLubyte ormask = c1|c2|c3; \
155 TriangleFunc( ctx, v1, v2, v3 ); \
156 else if (!(c1 & c2 & c3 & 0x3f)) \
157 clip_tri_4( ctx, v1, v2, v3, ormask ); \
160 #define RENDER_QUAD( v1, v2, v3, v4 ) \
162 GLubyte c1 = mask[v1], c2 = mask[v2]; \
163 GLubyte c3 = mask[v3], c4 = mask[v4]; \
164 GLubyte ormask = c1|c2|c3|c4; \
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 ); \
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;
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"
193 /* Elts, with the possibility of clipping.
197 #define ELT(x) elt[x]
198 #define TAG(x) clip_##x##_elts
199 #include "t_vb_rendertmp.h"
201 /* TODO: do this for all primitives, verts and elts:
203 static void clip_elt_triangles( GLcontext
*ctx
,
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
;
216 ctx
->Driver
.RenderPrimitive( ctx
, GL_TRIANGLES
);
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
;
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
);
233 render_tris( ctx
, start
, j
, 0 );
236 /**********************************************************************/
237 /* Render whole begin/end objects */
238 /**********************************************************************/
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
245 /* Vertices, no clipping.
247 #define RENDER_POINTS( start, count ) \
248 ctx->Driver.PointsFunc( ctx, start, count )
250 #define RENDER_LINE( v1, v2 ) \
251 LineFunc( ctx, v1, v2 )
253 #define RENDER_TRI( v1, v2, v3 ) \
254 TriangleFunc( ctx, v1, v2, v3 )
256 #define RENDER_QUAD( v1, v2, v3, v4 ) \
257 QuadFunc( ctx, v1, v2, v3, v4 )
259 #define TAG(x) _tnl_##x##_verts
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); \
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"
278 /* Elts, no clipping.
281 #define TAG(x) _tnl_##x##_elts
282 #define ELT(x) elt[x]
283 #include "t_vb_rendertmp.h"
288 /**********************************************************************/
289 /* Clip and render whole vertex buffers */
290 /**********************************************************************/
293 static GLboolean
run_render( GLcontext
*ctx
,
294 struct gl_pipeline_stage
*stage
)
296 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
297 struct vertex_buffer
*VB
= &tnl
->vb
;
298 GLuint new_inputs
= stage
->changed_inputs
;
302 /* Allow the drivers to lock before projected verts are built so
303 * that window coordinates are guarenteed not to change before
306 ctx
->Driver
.RenderStart( ctx
);
307 ctx
->Driver
.BuildProjectedVertices( ctx
, 0, VB
->Count
, new_inputs
);
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
;
314 tab
= VB
->Elts
? ctx
->Driver
.RenderTabElts
: ctx
->Driver
.RenderTabVerts
;
319 GLuint i
, length
, flags
= 0;
320 for (i
= 0 ; !(flags
& PRIM_LAST
) ; i
+= length
)
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); */
329 tab
[flags
& PRIM_MODE_MASK
]( ctx
, i
, i
+ length
, flags
);
331 } while (ctx
->Driver
.MultipassFunc
&&
332 ctx
->Driver
.MultipassFunc( ctx
, ++pass
));
335 ctx
->Driver
.RenderFinish( ctx
);
336 return GL_FALSE
; /* finished the pipe */
340 /**********************************************************************/
341 /* Render pipeline stage */
342 /**********************************************************************/
346 /* Quite a bit of work involved in finding out the inputs for the
349 static void check_render( GLcontext
*ctx
, struct gl_pipeline_stage
*stage
)
351 GLuint inputs
= VERT_CLIP
;
354 if (ctx
->Visual
.rgbMode
) {
357 if (ctx
->_TriangleCaps
& DD_SEPERATE_SPECULAR
) {
358 inputs
|= VERT_SPEC_RGB
;
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
);
370 inputs
|= VERT_INDEX
;
373 if (ctx
->Point
._Attenuated
)
374 inputs
|= VERT_POINT_SIZE
;
376 /* How do drivers turn this off?
378 if (ctx
->Fog
.Enabled
) {
379 inputs
|= VERT_FOG_COORD
;
382 if (ctx
->_TriangleCaps
& DD_TRI_UNFILLED
) {
386 if (ctx
->RenderMode
==GL_FEEDBACK
) {
387 inputs
|= VERT_TEX_ANY
;
390 stage
->inputs
= inputs
;
396 static void dtr( struct gl_pipeline_stage
*stage
)
401 const struct gl_pipeline_stage _tnl_render_stage
=
405 _DD_NEW_SEPERATE_SPECULAR
|
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 */