Added ctx->Texture._EnabledCoordUnits bitfield.
[mesa.git] / src / mesa / tnl / t_vb_render.c
1 /* $Id: t_vb_render.c,v 1.35 2003/04/08 02:27:20 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 5.1
6 *
7 * Copyright (C) 1999-2003 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 * Authors:
27 * Keith Whitwell <keith@tungstengraphics.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.Render.*
39 *
40 */
41
42
43 #include "glheader.h"
44 #include "context.h"
45 #include "enums.h"
46 #include "macros.h"
47 #include "imports.h"
48 #include "mtypes.h"
49
50 #include "math/m_matrix.h"
51 #include "math/m_xform.h"
52
53 #include "t_pipeline.h"
54
55
56
57 /**********************************************************************/
58 /* Clip single primitives */
59 /**********************************************************************/
60
61
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]
66 #define SIZE 4
67 #define TAG(x) x##_4
68 #include "t_vb_cliptmp.h"
69
70
71
72 /**********************************************************************/
73 /* Clip and render whole begin/end objects */
74 /**********************************************************************/
75
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
79
80
81 /* Vertices, with the possibility of clipping.
82 */
83 #define RENDER_POINTS( start, count ) \
84 tnl->Driver.Render.Points( ctx, start, count )
85
86 #define RENDER_LINE( v1, v2 ) \
87 do { \
88 GLubyte c1 = mask[v1], c2 = mask[v2]; \
89 GLubyte ormask = c1|c2; \
90 if (!ormask) \
91 LineFunc( ctx, v1, v2 ); \
92 else if (!(c1 & c2 & 0x3f)) \
93 clip_line_4( ctx, v1, v2, ormask ); \
94 } while (0)
95
96 #define RENDER_TRI( v1, v2, v3 ) \
97 do { \
98 GLubyte c1 = mask[v1], c2 = mask[v2], c3 = mask[v3]; \
99 GLubyte ormask = c1|c2|c3; \
100 if (!ormask) \
101 TriangleFunc( ctx, v1, v2, v3 ); \
102 else if (!(c1 & c2 & c3 & 0x3f)) \
103 clip_tri_4( ctx, v1, v2, v3, ormask ); \
104 } while (0)
105
106 #define RENDER_QUAD( v1, v2, v3, v4 ) \
107 do { \
108 GLubyte c1 = mask[v1], c2 = mask[v2]; \
109 GLubyte c3 = mask[v3], c4 = mask[v4]; \
110 GLubyte ormask = c1|c2|c3|c4; \
111 if (!ormask) \
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 ); \
115 } while (0)
116
117
118 #define LOCAL_VARS \
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;
130
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"
137
138
139
140 /* Elts, with the possibility of clipping.
141 */
142 #undef ELT
143 #undef TAG
144 #define ELT(x) elt[x]
145 #define TAG(x) clip_##x##_elts
146 #include "t_vb_rendertmp.h"
147
148 /* TODO: do this for all primitives, verts and elts:
149 */
150 static void clip_elt_triangles( GLcontext *ctx,
151 GLuint start,
152 GLuint count,
153 GLuint flags )
154 {
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;
161 GLuint j;
162 (void) flags;
163
164 tnl->Driver.Render.PrimitiveNotify( ctx, GL_TRIANGLES );
165
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;
171 if (ormask) {
172 if (start < j)
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 );
176 start = j+3;
177 }
178 }
179
180 if (start < j)
181 render_tris( ctx, start, j, 0 );
182 }
183
184 /**********************************************************************/
185 /* Render whole begin/end objects */
186 /**********************************************************************/
187
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
191
192
193 /* Vertices, no clipping.
194 */
195 #define RENDER_POINTS( start, count ) \
196 tnl->Driver.Render.Points( ctx, start, count )
197
198 #define RENDER_LINE( v1, v2 ) \
199 LineFunc( ctx, v1, v2 )
200
201 #define RENDER_TRI( v1, v2, v3 ) \
202 TriangleFunc( ctx, v1, v2, v3 )
203
204 #define RENDER_QUAD( v1, v2, v3, v4 ) \
205 QuadFunc( ctx, v1, v2, v3, v4 )
206
207 #define TAG(x) _tnl_##x##_verts
208
209 #define LOCAL_VARS \
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
219
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"
226
227
228 /* Elts, no clipping.
229 */
230 #undef ELT
231 #define TAG(x) _tnl_##x##_elts
232 #define ELT(x) elt[x]
233 #include "t_vb_rendertmp.h"
234
235
236 /**********************************************************************/
237 /* Helper functions for drivers */
238 /**********************************************************************/
239
240 void _tnl_RenderClippedPolygon( GLcontext *ctx, const GLuint *elts, GLuint n )
241 {
242 TNLcontext *tnl = TNL_CONTEXT(ctx);
243 struct vertex_buffer *VB = &tnl->vb;
244 GLuint *tmp = VB->Elts;
245
246 VB->Elts = (GLuint *)elts;
247 tnl->Driver.Render.PrimTabElts[GL_POLYGON]( ctx, 0, n, PRIM_BEGIN|PRIM_END );
248 VB->Elts = tmp;
249 }
250
251 void _tnl_RenderClippedLine( GLcontext *ctx, GLuint ii, GLuint jj )
252 {
253 TNLcontext *tnl = TNL_CONTEXT(ctx);
254 tnl->Driver.Render.Line( ctx, ii, jj );
255 }
256
257
258
259 /**********************************************************************/
260 /* Clip and render whole vertex buffers */
261 /**********************************************************************/
262
263
264 static GLboolean run_render( GLcontext *ctx,
265 struct gl_pipeline_stage *stage )
266 {
267 TNLcontext *tnl = TNL_CONTEXT(ctx);
268 struct vertex_buffer *VB = &tnl->vb;
269 GLuint new_inputs = stage->changed_inputs;
270 render_func *tab;
271 GLint pass = 0;
272
273 /* Allow the drivers to lock before projected verts are built so
274 * that window coordinates are guarenteed not to change before
275 * rendering.
276 */
277 ASSERT(tnl->Driver.Render.Start);
278
279 tnl->Driver.Render.Start( ctx );
280
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);
293
294 tnl->Driver.Render.BuildVertices( ctx, 0, VB->Count, new_inputs );
295
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;
299 }
300 else {
301 tab = (VB->Elts ?
302 tnl->Driver.Render.PrimTabElts :
303 tnl->Driver.Render.PrimTabVerts);
304 }
305
306 do
307 {
308 GLuint i, length, flags = 0;
309 for (i = VB->FirstPrimitive ; !(flags & PRIM_LAST) ; i += length)
310 {
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);
315
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),
319 i, i+length);
320
321 if (length)
322 tab[flags & PRIM_MODE_MASK]( ctx, i, i + length, flags );
323 }
324 } while (tnl->Driver.Render.Multipass &&
325 tnl->Driver.Render.Multipass( ctx, ++pass ));
326
327
328 tnl->Driver.Render.Finish( ctx );
329 /* _swrast_flush(ctx); */
330 /* usleep(1000000); */
331 return GL_FALSE; /* finished the pipe */
332 }
333
334
335 /**********************************************************************/
336 /* Render pipeline stage */
337 /**********************************************************************/
338
339
340
341 /* Quite a bit of work involved in finding out the inputs for the
342 * render stage.
343 */
344 static void check_render( GLcontext *ctx, struct gl_pipeline_stage *stage )
345 {
346 GLuint inputs = VERT_BIT_CLIP;
347 GLuint i;
348
349 if (ctx->Visual.rgbMode) {
350 inputs |= VERT_BIT_COLOR0;
351
352 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR)
353 inputs |= VERT_BIT_COLOR1;
354
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);
359 }
360 }
361 }
362 else {
363 inputs |= VERT_BIT_INDEX;
364 }
365
366 if (ctx->Point._Attenuated)
367 inputs |= VERT_BIT_POINT_SIZE;
368
369 /* How do drivers turn this off?
370 */
371 if (ctx->Fog.Enabled)
372 inputs |= VERT_BIT_FOG;
373
374 if (ctx->_TriangleCaps & DD_TRI_UNFILLED)
375 inputs |= VERT_BIT_EDGEFLAG;
376
377 if (ctx->RenderMode==GL_FEEDBACK)
378 inputs |= VERT_BITS_TEX_ANY;
379
380 stage->inputs = inputs;
381 }
382
383
384
385
386 static void dtr( struct gl_pipeline_stage *stage )
387 {
388 }
389
390
391 const struct gl_pipeline_stage _tnl_render_stage =
392 {
393 "render", /* name */
394 (_NEW_BUFFERS |
395 _DD_NEW_SEPARATE_SPECULAR |
396 _DD_NEW_FLATSHADE |
397 _NEW_TEXTURE|
398 _NEW_LIGHT|
399 _NEW_POINT|
400 _NEW_FOG|
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) */
406 0, /* outputs */
407 0, /* changed_inputs */
408 NULL, /* private data */
409 dtr, /* destructor */
410 check_render, /* check */
411 run_render /* run */
412 };