1 /* $Id: t_vb_render.c,v 1.35 2003/04/08 02:27:20 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2003 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 <keith@tungstengraphics.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.Render.*
50 #include "math/m_matrix.h"
51 #include "math/m_xform.h"
53 #include "t_pipeline.h"
57 /**********************************************************************/
58 /* Clip single primitives */
59 /**********************************************************************/
62 #define W(i) coord[i][3]
63 #define Z(i) coord[i][2]
64 #define Y(i) coord[i][1]
65 #define X(i) coord[i][0]
68 #include "t_vb_cliptmp.h"
72 /**********************************************************************/
73 /* Clip and render whole begin/end objects */
74 /**********************************************************************/
76 #define NEED_EDGEFLAG_SETUP (ctx->_TriangleCaps & DD_TRI_UNFILLED)
77 #define EDGEFLAG_GET(idx) VB->EdgeFlag[idx]
78 #define EDGEFLAG_SET(idx, val) VB->EdgeFlag[idx] = val
81 /* Vertices, with the possibility of clipping.
83 #define RENDER_POINTS( start, count ) \
84 tnl->Driver.Render.Points( ctx, start, count )
86 #define RENDER_LINE( v1, v2 ) \
88 GLubyte c1 = mask[v1], c2 = mask[v2]; \
89 GLubyte ormask = c1|c2; \
91 LineFunc( ctx, v1, v2 ); \
92 else if (!(c1 & c2 & 0x3f)) \
93 clip_line_4( ctx, v1, v2, ormask ); \
96 #define RENDER_TRI( v1, v2, v3 ) \
98 GLubyte c1 = mask[v1], c2 = mask[v2], c3 = mask[v3]; \
99 GLubyte ormask = c1|c2|c3; \
101 TriangleFunc( ctx, v1, v2, v3 ); \
102 else if (!(c1 & c2 & c3 & 0x3f)) \
103 clip_tri_4( ctx, v1, v2, v3, ormask ); \
106 #define RENDER_QUAD( v1, v2, v3, v4 ) \
108 GLubyte c1 = mask[v1], c2 = mask[v2]; \
109 GLubyte c3 = mask[v3], c4 = mask[v4]; \
110 GLubyte ormask = c1|c2|c3|c4; \
112 QuadFunc( ctx, v1, v2, v3, v4 ); \
113 else if (!(c1 & c2 & c3 & c4 & 0x3f)) \
114 clip_quad_4( ctx, v1, v2, v3, v4, ormask ); \
119 TNLcontext *tnl = TNL_CONTEXT(ctx); \
120 struct vertex_buffer *VB = &tnl->vb; \
121 const GLuint * const elt = VB->Elts; \
122 const GLubyte *mask = VB->ClipMask; \
123 const GLuint sz = VB->ClipPtr->size; \
124 const line_func LineFunc = tnl->Driver.Render.Line; \
125 const triangle_func TriangleFunc = tnl->Driver.Render.Triangle; \
126 const quad_func QuadFunc = tnl->Driver.Render.Quad; \
127 const GLboolean stipple = ctx->Line.StippleFlag; \
128 (void) (LineFunc && TriangleFunc && QuadFunc); \
129 (void) elt; (void) mask; (void) sz; (void) stipple;
131 #define TAG(x) clip_##x##_verts
132 #define INIT(x) tnl->Driver.Render.PrimitiveNotify( ctx, x )
133 #define RESET_STIPPLE if (stipple) tnl->Driver.Render.ResetLineStipple( ctx )
134 #define RESET_OCCLUSION ctx->OcclusionResult = GL_TRUE
135 #define PRESERVE_VB_DEFS
136 #include "t_vb_rendertmp.h"
140 /* Elts, with the possibility of clipping.
144 #define ELT(x) elt[x]
145 #define TAG(x) clip_##x##_elts
146 #include "t_vb_rendertmp.h"
148 /* TODO: do this for all primitives, verts and elts:
150 static void clip_elt_triangles( GLcontext
*ctx
,
155 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
156 render_func render_tris
= tnl
->Driver
.Render
.PrimTabElts
[GL_TRIANGLES
];
157 struct vertex_buffer
*VB
= &tnl
->vb
;
158 const GLuint
* const elt
= VB
->Elts
;
159 GLubyte
*mask
= VB
->ClipMask
;
160 GLuint last
= count
-2;
164 tnl
->Driver
.Render
.PrimitiveNotify( ctx
, GL_TRIANGLES
);
166 for (j
=start
; j
< last
; j
+=3 ) {
167 GLubyte c1
= mask
[elt
[j
]];
168 GLubyte c2
= mask
[elt
[j
+1]];
169 GLubyte c3
= mask
[elt
[j
+2]];
170 GLubyte ormask
= c1
|c2
|c3
;
173 render_tris( ctx
, start
, j
, 0 );
174 if (!(c1
&c2
&c3
&0x3f))
175 clip_tri_4( ctx
, elt
[j
], elt
[j
+1], elt
[j
+2], ormask
);
181 render_tris( ctx
, start
, j
, 0 );
184 /**********************************************************************/
185 /* Render whole begin/end objects */
186 /**********************************************************************/
188 #define NEED_EDGEFLAG_SETUP (ctx->_TriangleCaps & DD_TRI_UNFILLED)
189 #define EDGEFLAG_GET(idx) VB->EdgeFlag[idx]
190 #define EDGEFLAG_SET(idx, val) VB->EdgeFlag[idx] = val
193 /* Vertices, no clipping.
195 #define RENDER_POINTS( start, count ) \
196 tnl->Driver.Render.Points( ctx, start, count )
198 #define RENDER_LINE( v1, v2 ) \
199 LineFunc( ctx, v1, v2 )
201 #define RENDER_TRI( v1, v2, v3 ) \
202 TriangleFunc( ctx, v1, v2, v3 )
204 #define RENDER_QUAD( v1, v2, v3, v4 ) \
205 QuadFunc( ctx, v1, v2, v3, v4 )
207 #define TAG(x) _tnl_##x##_verts
210 TNLcontext *tnl = TNL_CONTEXT(ctx); \
211 struct vertex_buffer *VB = &tnl->vb; \
212 const GLuint * const elt = VB->Elts; \
213 const line_func LineFunc = tnl->Driver.Render.Line; \
214 const triangle_func TriangleFunc = tnl->Driver.Render.Triangle; \
215 const quad_func QuadFunc = tnl->Driver.Render.Quad; \
216 const GLboolean stipple = ctx->Line.StippleFlag; \
217 (void) (LineFunc && TriangleFunc && QuadFunc); \
218 (void) elt; (void) stipple
220 #define RESET_STIPPLE if (stipple) tnl->Driver.Render.ResetLineStipple( ctx )
221 #define RESET_OCCLUSION ctx->OcclusionResult = GL_TRUE
222 #define INIT(x) tnl->Driver.Render.PrimitiveNotify( ctx, x )
223 #define RENDER_TAB_QUALIFIER
224 #define PRESERVE_VB_DEFS
225 #include "t_vb_rendertmp.h"
228 /* Elts, no clipping.
231 #define TAG(x) _tnl_##x##_elts
232 #define ELT(x) elt[x]
233 #include "t_vb_rendertmp.h"
236 /**********************************************************************/
237 /* Helper functions for drivers */
238 /**********************************************************************/
240 void _tnl_RenderClippedPolygon( GLcontext
*ctx
, const GLuint
*elts
, GLuint n
)
242 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
243 struct vertex_buffer
*VB
= &tnl
->vb
;
244 GLuint
*tmp
= VB
->Elts
;
246 VB
->Elts
= (GLuint
*)elts
;
247 tnl
->Driver
.Render
.PrimTabElts
[GL_POLYGON
]( ctx
, 0, n
, PRIM_BEGIN
|PRIM_END
);
251 void _tnl_RenderClippedLine( GLcontext
*ctx
, GLuint ii
, GLuint jj
)
253 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
254 tnl
->Driver
.Render
.Line( ctx
, ii
, jj
);
259 /**********************************************************************/
260 /* Clip and render whole vertex buffers */
261 /**********************************************************************/
264 static GLboolean
run_render( GLcontext
*ctx
,
265 struct gl_pipeline_stage
*stage
)
267 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
268 struct vertex_buffer
*VB
= &tnl
->vb
;
269 GLuint new_inputs
= stage
->changed_inputs
;
273 /* Allow the drivers to lock before projected verts are built so
274 * that window coordinates are guarenteed not to change before
277 ASSERT(tnl
->Driver
.Render
.Start
);
279 tnl
->Driver
.Render
.Start( ctx
);
281 ASSERT(tnl
->Driver
.Render
.BuildVertices
);
282 ASSERT(tnl
->Driver
.Render
.PrimitiveNotify
);
283 ASSERT(tnl
->Driver
.Render
.Points
);
284 ASSERT(tnl
->Driver
.Render
.Line
);
285 ASSERT(tnl
->Driver
.Render
.Triangle
);
286 ASSERT(tnl
->Driver
.Render
.Quad
);
287 ASSERT(tnl
->Driver
.Render
.ResetLineStipple
);
288 ASSERT(tnl
->Driver
.Render
.Interp
);
289 ASSERT(tnl
->Driver
.Render
.CopyPV
);
290 ASSERT(tnl
->Driver
.Render
.ClippedLine
);
291 ASSERT(tnl
->Driver
.Render
.ClippedPolygon
);
292 ASSERT(tnl
->Driver
.Render
.Finish
);
294 tnl
->Driver
.Render
.BuildVertices( ctx
, 0, VB
->Count
, new_inputs
);
296 if (VB
->ClipOrMask
) {
297 tab
= VB
->Elts
? clip_render_tab_elts
: clip_render_tab_verts
;
298 clip_render_tab_elts
[GL_TRIANGLES
] = clip_elt_triangles
;
302 tnl
->Driver
.Render
.PrimTabElts
:
303 tnl
->Driver
.Render
.PrimTabVerts
);
308 GLuint i
, length
, flags
= 0;
309 for (i
= VB
->FirstPrimitive
; !(flags
& PRIM_LAST
) ; i
+= length
)
311 flags
= VB
->Primitive
[i
];
312 length
= VB
->PrimitiveLength
[i
];
313 ASSERT(length
|| (flags
& PRIM_LAST
));
314 ASSERT((flags
& PRIM_MODE_MASK
) <= GL_POLYGON
+1);
316 if (MESA_VERBOSE
& VERBOSE_PRIMS
)
317 _mesa_debug(NULL
, "MESA prim %s %d..%d\n",
318 _mesa_lookup_enum_by_nr(flags
& PRIM_MODE_MASK
),
322 tab
[flags
& PRIM_MODE_MASK
]( ctx
, i
, i
+ length
, flags
);
324 } while (tnl
->Driver
.Render
.Multipass
&&
325 tnl
->Driver
.Render
.Multipass( ctx
, ++pass
));
328 tnl
->Driver
.Render
.Finish( ctx
);
329 /* _swrast_flush(ctx); */
330 /* usleep(1000000); */
331 return GL_FALSE
; /* finished the pipe */
335 /**********************************************************************/
336 /* Render pipeline stage */
337 /**********************************************************************/
341 /* Quite a bit of work involved in finding out the inputs for the
344 static void check_render( GLcontext
*ctx
, struct gl_pipeline_stage
*stage
)
346 GLuint inputs
= VERT_BIT_CLIP
;
349 if (ctx
->Visual
.rgbMode
) {
350 inputs
|= VERT_BIT_COLOR0
;
352 if (ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
)
353 inputs
|= VERT_BIT_COLOR1
;
355 if (ctx
->Texture
._EnabledCoordUnits
) {
356 for (i
= 0 ; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
357 if (ctx
->Texture
._EnabledCoordUnits
& (1 << i
))
358 inputs
|= VERT_BIT_TEX(i
);
363 inputs
|= VERT_BIT_INDEX
;
366 if (ctx
->Point
._Attenuated
)
367 inputs
|= VERT_BIT_POINT_SIZE
;
369 /* How do drivers turn this off?
371 if (ctx
->Fog
.Enabled
)
372 inputs
|= VERT_BIT_FOG
;
374 if (ctx
->_TriangleCaps
& DD_TRI_UNFILLED
)
375 inputs
|= VERT_BIT_EDGEFLAG
;
377 if (ctx
->RenderMode
==GL_FEEDBACK
)
378 inputs
|= VERT_BITS_TEX_ANY
;
380 stage
->inputs
= inputs
;
386 static void dtr( struct gl_pipeline_stage
*stage
)
391 const struct gl_pipeline_stage _tnl_render_stage
=
395 _DD_NEW_SEPARATE_SPECULAR
|
401 _DD_NEW_TRI_UNFILLED
|
402 _NEW_RENDERMODE
), /* re-check (new inputs, interp function) */
403 0, /* re-run (always runs) */
404 GL_TRUE
, /* active? */
405 0, /* inputs (set in check_render) */
407 0, /* changed_inputs */
408 NULL
, /* private data */
409 dtr
, /* destructor */
410 check_render
, /* check */