3 * Mesa 3-D graphics library
6 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * Keith Whitwell <keith@tungstengraphics.com>
31 * Render whole vertex buffers, including projection of vertices from
32 * clip space and clipping of primitives.
34 * This file makes calls to project vertices and to the point, line
35 * and triangle rasterizers via the function pointers:
37 * context->Driver.Render.*
48 #include "nvfragprog.h"
49 #include "math/m_matrix.h"
50 #include "math/m_xform.h"
52 #include "t_pipeline.h"
56 /**********************************************************************/
57 /* Clip single primitives */
58 /**********************************************************************/
61 #define W(i) coord[i][3]
62 #define Z(i) coord[i][2]
63 #define Y(i) coord[i][1]
64 #define X(i) coord[i][0]
67 #include "t_vb_cliptmp.h"
71 /**********************************************************************/
72 /* Clip and render whole begin/end objects */
73 /**********************************************************************/
75 #define NEED_EDGEFLAG_SETUP (ctx->Polygon.FrontMode != GL_FILL || ctx->Polygon.BackMode != GL_FILL)
76 #define EDGEFLAG_GET(idx) VB->EdgeFlag[idx]
77 #define EDGEFLAG_SET(idx, val) VB->EdgeFlag[idx] = val
80 /* Vertices, with the possibility of clipping.
82 #define RENDER_POINTS( start, count ) \
83 tnl->Driver.Render.Points( ctx, start, count )
85 #define RENDER_LINE( v1, v2 ) \
87 GLubyte c1 = mask[v1], c2 = mask[v2]; \
88 GLubyte ormask = c1|c2; \
90 LineFunc( ctx, v1, v2 ); \
91 else if (!(c1 & c2 & 0x3f)) \
92 clip_line_4( ctx, v1, v2, ormask ); \
95 #define RENDER_TRI( v1, v2, v3 ) \
97 GLubyte c1 = mask[v1], c2 = mask[v2], c3 = mask[v3]; \
98 GLubyte ormask = c1|c2|c3; \
100 TriangleFunc( ctx, v1, v2, v3 ); \
101 else if (!(c1 & c2 & c3 & 0x3f)) \
102 clip_tri_4( ctx, v1, v2, v3, ormask ); \
105 #define RENDER_QUAD( v1, v2, v3, v4 ) \
107 GLubyte c1 = mask[v1], c2 = mask[v2]; \
108 GLubyte c3 = mask[v3], c4 = mask[v4]; \
109 GLubyte ormask = c1|c2|c3|c4; \
111 QuadFunc( ctx, v1, v2, v3, v4 ); \
112 else if (!(c1 & c2 & c3 & c4 & 0x3f)) \
113 clip_quad_4( ctx, v1, v2, v3, v4, ormask ); \
118 TNLcontext *tnl = TNL_CONTEXT(ctx); \
119 struct vertex_buffer *VB = &tnl->vb; \
120 const GLuint * const elt = VB->Elts; \
121 const GLubyte *mask = VB->ClipMask; \
122 const GLuint sz = VB->ClipPtr->size; \
123 const line_func LineFunc = tnl->Driver.Render.Line; \
124 const triangle_func TriangleFunc = tnl->Driver.Render.Triangle; \
125 const quad_func QuadFunc = tnl->Driver.Render.Quad; \
126 const GLboolean stipple = ctx->Line.StippleFlag; \
127 (void) (LineFunc && TriangleFunc && QuadFunc); \
128 (void) elt; (void) mask; (void) sz; (void) stipple;
130 #define TAG(x) clip_##x##_verts
131 #define INIT(x) tnl->Driver.Render.PrimitiveNotify( ctx, x )
132 #define RESET_STIPPLE if (stipple) tnl->Driver.Render.ResetLineStipple( ctx )
133 #define RESET_OCCLUSION ctx->OcclusionResult = GL_TRUE
134 #define PRESERVE_VB_DEFS
135 #include "t_vb_rendertmp.h"
139 /* Elts, with the possibility of clipping.
143 #define ELT(x) elt[x]
144 #define TAG(x) clip_##x##_elts
145 #include "t_vb_rendertmp.h"
147 /* TODO: do this for all primitives, verts and elts:
149 static void clip_elt_triangles( GLcontext
*ctx
,
154 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
155 render_func render_tris
= tnl
->Driver
.Render
.PrimTabElts
[GL_TRIANGLES
];
156 struct vertex_buffer
*VB
= &tnl
->vb
;
157 const GLuint
* const elt
= VB
->Elts
;
158 GLubyte
*mask
= VB
->ClipMask
;
159 GLuint last
= count
-2;
163 tnl
->Driver
.Render
.PrimitiveNotify( ctx
, GL_TRIANGLES
);
165 for (j
=start
; j
< last
; j
+=3 ) {
166 GLubyte c1
= mask
[elt
[j
]];
167 GLubyte c2
= mask
[elt
[j
+1]];
168 GLubyte c3
= mask
[elt
[j
+2]];
169 GLubyte ormask
= c1
|c2
|c3
;
172 render_tris( ctx
, start
, j
, 0 );
173 if (!(c1
&c2
&c3
&0x3f))
174 clip_tri_4( ctx
, elt
[j
], elt
[j
+1], elt
[j
+2], ormask
);
180 render_tris( ctx
, start
, j
, 0 );
183 /**********************************************************************/
184 /* Render whole begin/end objects */
185 /**********************************************************************/
187 #define NEED_EDGEFLAG_SETUP (ctx->Polygon.FrontMode != GL_FILL || ctx->Polygon.BackMode != GL_FILL)
188 #define EDGEFLAG_GET(idx) VB->EdgeFlag[idx]
189 #define EDGEFLAG_SET(idx, val) VB->EdgeFlag[idx] = val
192 /* Vertices, no clipping.
194 #define RENDER_POINTS( start, count ) \
195 tnl->Driver.Render.Points( ctx, start, count )
197 #define RENDER_LINE( v1, v2 ) \
198 LineFunc( ctx, v1, v2 )
200 #define RENDER_TRI( v1, v2, v3 ) \
201 TriangleFunc( ctx, v1, v2, v3 )
203 #define RENDER_QUAD( v1, v2, v3, v4 ) \
204 QuadFunc( ctx, v1, v2, v3, v4 )
206 #define TAG(x) _tnl_##x##_verts
209 TNLcontext *tnl = TNL_CONTEXT(ctx); \
210 struct vertex_buffer *VB = &tnl->vb; \
211 const GLuint * const elt = VB->Elts; \
212 const line_func LineFunc = tnl->Driver.Render.Line; \
213 const triangle_func TriangleFunc = tnl->Driver.Render.Triangle; \
214 const quad_func QuadFunc = tnl->Driver.Render.Quad; \
215 const GLboolean stipple = ctx->Line.StippleFlag; \
216 (void) (LineFunc && TriangleFunc && QuadFunc); \
217 (void) elt; (void) stipple
219 #define RESET_STIPPLE if (stipple) tnl->Driver.Render.ResetLineStipple( ctx )
220 #define RESET_OCCLUSION ctx->OcclusionResult = GL_TRUE
221 #define INIT(x) tnl->Driver.Render.PrimitiveNotify( ctx, x )
222 #define RENDER_TAB_QUALIFIER
223 #define PRESERVE_VB_DEFS
224 #include "t_vb_rendertmp.h"
227 /* Elts, no clipping.
230 #define TAG(x) _tnl_##x##_elts
231 #define ELT(x) elt[x]
232 #include "t_vb_rendertmp.h"
235 /**********************************************************************/
236 /* Helper functions for drivers */
237 /**********************************************************************/
239 void _tnl_RenderClippedPolygon( GLcontext
*ctx
, const GLuint
*elts
, GLuint n
)
241 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
242 struct vertex_buffer
*VB
= &tnl
->vb
;
243 GLuint
*tmp
= VB
->Elts
;
245 VB
->Elts
= (GLuint
*)elts
;
246 tnl
->Driver
.Render
.PrimTabElts
[GL_POLYGON
]( ctx
, 0, n
, PRIM_BEGIN
|PRIM_END
);
250 void _tnl_RenderClippedLine( GLcontext
*ctx
, GLuint ii
, GLuint jj
)
252 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
253 tnl
->Driver
.Render
.Line( ctx
, ii
, jj
);
258 /**********************************************************************/
259 /* Clip and render whole vertex buffers */
260 /**********************************************************************/
263 static GLboolean
run_render( GLcontext
*ctx
,
264 struct tnl_pipeline_stage
*stage
)
266 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
267 struct vertex_buffer
*VB
= &tnl
->vb
;
268 GLuint new_inputs
= stage
->changed_inputs
;
272 /* Allow the drivers to lock before projected verts are built so
273 * that window coordinates are guarenteed not to change before
276 ASSERT(tnl
->Driver
.Render
.Start
);
278 tnl
->Driver
.Render
.Start( ctx
);
280 ASSERT(tnl
->Driver
.Render
.BuildVertices
);
281 ASSERT(tnl
->Driver
.Render
.PrimitiveNotify
);
282 ASSERT(tnl
->Driver
.Render
.Points
);
283 ASSERT(tnl
->Driver
.Render
.Line
);
284 ASSERT(tnl
->Driver
.Render
.Triangle
);
285 ASSERT(tnl
->Driver
.Render
.Quad
);
286 ASSERT(tnl
->Driver
.Render
.ResetLineStipple
);
287 ASSERT(tnl
->Driver
.Render
.Interp
);
288 ASSERT(tnl
->Driver
.Render
.CopyPV
);
289 ASSERT(tnl
->Driver
.Render
.ClippedLine
);
290 ASSERT(tnl
->Driver
.Render
.ClippedPolygon
);
291 ASSERT(tnl
->Driver
.Render
.Finish
);
293 tnl
->Driver
.Render
.BuildVertices( ctx
, 0, VB
->Count
, new_inputs
);
295 if (VB
->ClipOrMask
) {
296 tab
= VB
->Elts
? clip_render_tab_elts
: clip_render_tab_verts
;
297 clip_render_tab_elts
[GL_TRIANGLES
] = clip_elt_triangles
;
301 tnl
->Driver
.Render
.PrimTabElts
:
302 tnl
->Driver
.Render
.PrimTabVerts
);
309 for (i
= 0 ; i
< VB
->PrimitiveCount
; i
++)
311 GLuint prim
= VB
->Primitive
[i
].mode
;
312 GLuint start
= VB
->Primitive
[i
].start
;
313 GLuint length
= VB
->Primitive
[i
].count
;
315 assert((prim
& PRIM_MODE_MASK
) < GL_POLYGON
+1);
317 if (MESA_VERBOSE
& VERBOSE_PRIMS
)
318 _mesa_debug(NULL
, "MESA prim %s %d..%d\n",
319 _mesa_lookup_enum_by_nr(prim
& PRIM_MODE_MASK
),
320 start
, start
+length
);
323 tab
[prim
& PRIM_MODE_MASK
]( ctx
, start
, start
+ length
, prim
);
325 } while (tnl
->Driver
.Render
.Multipass
&&
326 tnl
->Driver
.Render
.Multipass( ctx
, ++pass
));
328 tnl
->Driver
.Render
.Finish( ctx
);
330 return GL_FALSE
; /* finished the pipe */
334 /**********************************************************************/
335 /* Render pipeline stage */
336 /**********************************************************************/
340 /* Quite a bit of work involved in finding out the inputs for the
343 static void check_render( GLcontext
*ctx
, struct tnl_pipeline_stage
*stage
)
345 stage
->inputs
= TNL_CONTEXT(ctx
)->render_inputs
;
351 static void dtr( struct tnl_pipeline_stage
*stage
)
356 const struct tnl_pipeline_stage _tnl_render_stage
=
360 _DD_NEW_SEPARATE_SPECULAR
|
366 _DD_NEW_TRI_UNFILLED
|
367 _NEW_RENDERMODE
), /* re-check (new inputs, interp function) */
368 0, /* re-run (always runs) */
369 GL_TRUE
, /* active? */
370 0, /* inputs (set in check_render) */
372 0, /* changed_inputs */
373 NULL
, /* private data */
374 dtr
, /* destructor */
375 check_render
, /* check */