1 /* $Id: t_vb_render.c,v 1.23 2001/11/19 10:22:41 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.Render.*
50 #include "math/m_matrix.h"
51 #include "math/m_xform.h"
53 #include "t_pipeline.h"
57 /**********************************************************************/
58 /* Clip single primitives */
59 /**********************************************************************/
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))
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?
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]
80 #include "t_vb_cliptmp.h"
84 /**********************************************************************/
85 /* Clip and render whole begin/end objects */
86 /**********************************************************************/
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
93 /* Vertices, with the possibility of clipping.
95 #define RENDER_POINTS( start, count ) \
96 tnl->Driver.Render.Points( ctx, start, count )
98 #define RENDER_LINE( v1, v2 ) \
100 GLubyte c1 = mask[v1], c2 = mask[v2]; \
101 GLubyte ormask = c1|c2; \
103 LineFunc( ctx, v1, v2 ); \
104 else if (!(c1 & c2 & 0x3f)) \
105 clip_line_4( ctx, v1, v2, ormask ); \
108 #define RENDER_TRI( v1, v2, v3 ) \
110 GLubyte c1 = mask[v1], c2 = mask[v2], c3 = mask[v3]; \
111 GLubyte ormask = c1|c2|c3; \
113 TriangleFunc( ctx, v1, v2, v3 ); \
114 else if (!(c1 & c2 & c3 & 0x3f)) \
115 clip_tri_4( ctx, v1, v2, v3, ormask ); \
118 #define RENDER_QUAD( v1, v2, v3, v4 ) \
120 GLubyte c1 = mask[v1], c2 = mask[v2]; \
121 GLubyte c3 = mask[v3], c4 = mask[v4]; \
122 GLubyte ormask = c1|c2|c3|c4; \
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 ); \
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;
143 #define TAG(x) clip_##x##_verts
144 #define INIT(x) tnl->Driver.Render.PrimitiveNotify( ctx, x )
145 #define RESET_STIPPLE if (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"
152 /* Elts, with the possibility of clipping.
156 #define ELT(x) elt[x]
157 #define TAG(x) clip_##x##_elts
158 #include "t_vb_rendertmp.h"
160 /* TODO: do this for all primitives, verts and elts:
162 static void clip_elt_triangles( GLcontext
*ctx
,
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;
176 tnl
->Driver
.Render
.PrimitiveNotify( ctx
, GL_TRIANGLES
);
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
;
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
);
193 render_tris( ctx
, start
, j
, 0 );
196 /**********************************************************************/
197 /* Render whole begin/end objects */
198 /**********************************************************************/
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
205 /* Vertices, no clipping.
207 #define RENDER_POINTS( start, count ) \
208 tnl->Driver.Render.Points( ctx, start, count )
210 #define RENDER_LINE( v1, v2 ) \
211 LineFunc( ctx, v1, v2 )
213 #define RENDER_TRI( v1, v2, v3 ) \
214 TriangleFunc( ctx, v1, v2, v3 )
216 #define RENDER_QUAD( v1, v2, v3, v4 ) \
217 QuadFunc( ctx, v1, v2, v3, v4 )
219 #define TAG(x) _tnl_##x##_verts
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 (void) (LineFunc && TriangleFunc && QuadFunc); \
231 #define RESET_STIPPLE tnl->Driver.Render.ResetLineStipple( ctx )
232 #define RESET_OCCLUSION ctx->OcclusionResult = GL_TRUE
233 #define INIT(x) tnl->Driver.Render.PrimitiveNotify( ctx, x )
234 #define RENDER_TAB_QUALIFIER
235 #define PRESERVE_VB_DEFS
236 #include "t_vb_rendertmp.h"
239 /* Elts, no clipping.
242 #define TAG(x) _tnl_##x##_elts
243 #define ELT(x) elt[x]
244 #include "t_vb_rendertmp.h"
247 /**********************************************************************/
248 /* Helper functions for drivers */
249 /**********************************************************************/
251 void _tnl_RenderClippedPolygon( GLcontext
*ctx
, const GLuint
*elts
, GLuint n
)
253 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
254 struct vertex_buffer
*VB
= &tnl
->vb
;
255 GLuint
*tmp
= VB
->Elts
;
257 VB
->Elts
= (GLuint
*)elts
;
258 tnl
->Driver
.Render
.PrimTabElts
[GL_POLYGON
]( ctx
, 0, n
, PRIM_BEGIN
|PRIM_END
);
262 void _tnl_RenderClippedLine( GLcontext
*ctx
, GLuint ii
, GLuint jj
)
264 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
265 tnl
->Driver
.Render
.Line( ctx
, ii
, jj
);
270 /**********************************************************************/
271 /* Clip and render whole vertex buffers */
272 /**********************************************************************/
275 static GLboolean
run_render( GLcontext
*ctx
,
276 struct gl_pipeline_stage
*stage
)
278 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
279 struct vertex_buffer
*VB
= &tnl
->vb
;
280 GLuint new_inputs
= stage
->changed_inputs
;
285 /* Allow the drivers to lock before projected verts are built so
286 * that window coordinates are guarenteed not to change before
289 ASSERT(tnl
->Driver
.Render
.Start
);
291 tnl
->Driver
.Render
.Start( ctx
);
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
);
306 tnl
->Driver
.Render
.BuildVertices( ctx
, 0, VB
->Count
, new_inputs
);
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
;
314 tnl
->Driver
.Render
.PrimTabElts
:
315 tnl
->Driver
.Render
.PrimTabVerts
);
320 GLuint i
, length
, flags
= 0;
321 for (i
= VB
->FirstPrimitive
; !(flags
& PRIM_LAST
) ; i
+= length
)
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);
328 tab
[flags
& PRIM_MODE_MASK
]( ctx
, i
, i
+ length
, flags
);
330 } while (tnl
->Driver
.Render
.Multipass
&&
331 tnl
->Driver
.Render
.Multipass( ctx
, ++pass
));
334 tnl
->Driver
.Render
.Finish( ctx
);
335 /* _swrast_flush(ctx); */
336 /* usleep(1000000); */
337 return GL_FALSE
; /* finished the pipe */
341 /**********************************************************************/
342 /* Render pipeline stage */
343 /**********************************************************************/
347 /* Quite a bit of work involved in finding out the inputs for the
350 static void check_render( GLcontext
*ctx
, struct gl_pipeline_stage
*stage
)
352 GLuint inputs
= VERT_CLIP
;
355 if (ctx
->Visual
.rgbMode
) {
358 if (ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
)
359 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
);
369 inputs
|= VERT_INDEX
;
372 if (ctx
->Point
._Attenuated
)
373 inputs
|= VERT_POINT_SIZE
;
375 /* How do drivers turn this off?
377 if (ctx
->Fog
.Enabled
)
378 inputs
|= VERT_FOG_COORD
;
380 if (ctx
->_TriangleCaps
& DD_TRI_UNFILLED
)
383 if (ctx
->RenderMode
==GL_FEEDBACK
)
384 inputs
|= VERT_TEX_ANY
;
386 stage
->inputs
= inputs
;
392 static void dtr( struct gl_pipeline_stage
*stage
)
397 const struct gl_pipeline_stage _tnl_render_stage
=
401 _DD_NEW_SEPARATE_SPECULAR
|
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, 0, /* inputs (set in check_render), outputs */
412 0, 0, /* changed_inputs, private */
413 dtr
, /* destructor */
414 check_render
, /* check */