rerun vertex stage on _MESA_NEW_NEED_EYE_COORDS
[mesa.git] / src / mesa / tnl / t_vb_vertex.c
1 /* $Id: t_vb_vertex.c,v 1.9 2001/05/30 10:01:41 keithw Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 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 <keithw@valinux.com>
28 */
29
30
31 #include "glheader.h"
32 #include "colormac.h"
33 #include "context.h"
34 #include "macros.h"
35 #include "mem.h"
36 #include "mmath.h"
37 #include "mtypes.h"
38
39 #include "math/m_xform.h"
40
41 #include "t_context.h"
42 #include "t_pipeline.h"
43
44
45
46 struct vertex_stage_data {
47 GLvector4f eye;
48 GLvector4f clip;
49 GLvector4f proj;
50 GLubyte *clipmask;
51 GLubyte ormask;
52 GLubyte andmask;
53
54
55 /* Need these because it's difficult to replay the sideeffects
56 * analytically.
57 */
58 GLvector4f *save_eyeptr;
59 GLvector4f *save_clipptr;
60 GLvector4f *save_projptr;
61 };
62
63 #define VERTEX_STAGE_DATA(stage) ((struct vertex_stage_data *)stage->privatePtr)
64
65
66
67
68 /* This function implements cliptesting for user-defined clip planes.
69 * The clipping of primitives to these planes is implemented in
70 * t_render_clip.h.
71 */
72 #define USER_CLIPTEST(NAME, SZ) \
73 static void NAME( GLcontext *ctx, \
74 GLvector4f *clip, \
75 GLubyte *clipmask, \
76 GLubyte *clipormask, \
77 GLubyte *clipandmask ) \
78 { \
79 GLuint p; \
80 \
81 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) \
82 if (ctx->Transform.ClipEnabled[p]) { \
83 GLuint nr, i; \
84 const GLfloat a = ctx->Transform._ClipUserPlane[p][0]; \
85 const GLfloat b = ctx->Transform._ClipUserPlane[p][1]; \
86 const GLfloat c = ctx->Transform._ClipUserPlane[p][2]; \
87 const GLfloat d = ctx->Transform._ClipUserPlane[p][3]; \
88 GLfloat *coord = (GLfloat *)clip->data; \
89 GLuint stride = clip->stride; \
90 GLuint count = clip->count; \
91 \
92 for (nr = 0, i = 0 ; i < count ; i++) { \
93 GLfloat dp = coord[0] * a + coord[1] * b; \
94 if (SZ > 2) dp += coord[2] * c; \
95 if (SZ > 3) dp += coord[3] * d; else dp += d; \
96 \
97 if (dp < 0) { \
98 nr++; \
99 clipmask[i] |= CLIP_USER_BIT; \
100 } \
101 \
102 STRIDE_F(coord, stride); \
103 } \
104 \
105 if (nr > 0) { \
106 *clipormask |= CLIP_USER_BIT; \
107 if (nr == count) { \
108 *clipandmask |= CLIP_USER_BIT; \
109 return; \
110 } \
111 } \
112 } \
113 }
114
115
116 USER_CLIPTEST(userclip2, 2)
117 USER_CLIPTEST(userclip3, 3)
118 USER_CLIPTEST(userclip4, 4)
119
120 static void (*(usercliptab[5]))( GLcontext *,
121 GLvector4f *, GLubyte *,
122 GLubyte *, GLubyte * ) =
123 {
124 0,
125 0,
126 userclip2,
127 userclip3,
128 userclip4
129 };
130
131
132
133 static GLboolean run_vertex_stage( GLcontext *ctx,
134 struct gl_pipeline_stage *stage )
135 {
136 struct vertex_stage_data *store = (struct vertex_stage_data *)stage->privatePtr;
137 TNLcontext *tnl = TNL_CONTEXT(ctx);
138 struct vertex_buffer *VB = &tnl->vb;
139
140 if (stage->changed_inputs) {
141
142 if (ctx->_NeedEyeCoords) {
143 /* Separate modelview and project transformations:
144 */
145 if (ctx->ModelView.type == MATRIX_IDENTITY)
146 VB->EyePtr = VB->ObjPtr;
147 else
148 VB->EyePtr = TransformRaw( &store->eye, &ctx->ModelView,
149 VB->ObjPtr);
150
151 if (ctx->ProjectionMatrix.type == MATRIX_IDENTITY)
152 VB->ClipPtr = VB->EyePtr;
153 else
154 VB->ClipPtr = TransformRaw( &store->clip, &ctx->ProjectionMatrix,
155 VB->EyePtr );
156 }
157 else {
158 /* Combined modelviewproject transform:
159 */
160 if (ctx->_ModelProjectMatrix.type == MATRIX_IDENTITY)
161 VB->ClipPtr = VB->ObjPtr;
162 else
163 VB->ClipPtr = TransformRaw( &store->clip,
164 &ctx->_ModelProjectMatrix,
165 VB->ObjPtr );
166 }
167
168 /* Drivers expect this to be clean to element 4...
169 */
170 if (VB->ClipPtr->size < 4) {
171 if (VB->ClipPtr->flags & VEC_NOT_WRITEABLE) {
172 ASSERT(VB->ClipPtr == VB->ObjPtr);
173 VB->import_data( ctx, VERT_OBJ, VEC_NOT_WRITEABLE );
174 VB->ClipPtr = VB->ObjPtr;
175 }
176 if (VB->ClipPtr->size == 2)
177 _mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 2 );
178 _mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 3 );
179 }
180
181 /* Cliptest and perspective divide. Clip functions must clear
182 * the clipmask.
183 */
184 store->ormask = 0;
185 store->andmask = CLIP_ALL_BITS;
186
187 if (tnl->NeedProjCoords) {
188 VB->ProjectedClipPtr =
189 _mesa_clip_tab[VB->ClipPtr->size]( VB->ClipPtr,
190 &store->proj,
191 store->clipmask,
192 &store->ormask,
193 &store->andmask );
194
195 } else {
196 VB->ProjectedClipPtr = 0;
197 _mesa_clip_np_tab[VB->ClipPtr->size]( VB->ClipPtr,
198 0,
199 store->clipmask,
200 &store->ormask,
201 &store->andmask );
202 }
203
204 if (store->andmask)
205 return GL_FALSE;
206
207
208 /* Test userclip planes. This contributes to VB->ClipMask, so
209 * is essentially required to be in this stage.
210 */
211 if (ctx->Transform._AnyClip) {
212 usercliptab[VB->ClipPtr->size]( ctx,
213 VB->ClipPtr,
214 store->clipmask,
215 &store->ormask,
216 &store->andmask );
217
218 if (store->andmask)
219 return GL_FALSE;
220 }
221
222 VB->ClipOrMask = store->ormask;
223 VB->ClipMask = store->clipmask;
224
225 if (VB->ClipPtr == VB->ObjPtr && (VB->importable_data & VERT_OBJ))
226 VB->importable_data |= VERT_CLIP;
227
228 store->save_eyeptr = VB->EyePtr;
229 store->save_clipptr = VB->ClipPtr;
230 store->save_projptr = VB->ProjectedClipPtr;
231 }
232 else {
233 /* Replay the sideeffects.
234 */
235 VB->EyePtr = store->save_eyeptr;
236 VB->ClipPtr = store->save_clipptr;
237 VB->ProjectedClipPtr = store->save_projptr;
238 VB->ClipMask = store->clipmask;
239 VB->ClipOrMask = store->ormask;
240 if (VB->ClipPtr == VB->ObjPtr && (VB->importable_data & VERT_OBJ))
241 VB->importable_data |= VERT_CLIP;
242 if (store->andmask)
243 return GL_FALSE;
244 }
245
246 return GL_TRUE;
247 }
248
249
250 static void check_vertex( GLcontext *ctx, struct gl_pipeline_stage *stage )
251 {
252 (void) ctx;
253 (void) stage;
254 }
255
256 static GLboolean init_vertex_stage( GLcontext *ctx,
257 struct gl_pipeline_stage *stage )
258 {
259 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
260 struct vertex_stage_data *store;
261 GLuint size = VB->Size;
262
263 stage->privatePtr = CALLOC(sizeof(*store));
264 store = VERTEX_STAGE_DATA(stage);
265 if (!store)
266 return GL_FALSE;
267
268 _mesa_vector4f_alloc( &store->eye, 0, size, 32 );
269 _mesa_vector4f_alloc( &store->clip, 0, size, 32 );
270 _mesa_vector4f_alloc( &store->proj, 0, size, 32 );
271
272 store->clipmask = (GLubyte *) ALIGN_MALLOC(sizeof(GLubyte)*size, 32 );
273
274 if (!store->clipmask ||
275 !store->eye.data ||
276 !store->clip.data ||
277 !store->proj.data)
278 return GL_FALSE;
279
280 /* Now run the stage.
281 */
282 stage->run = run_vertex_stage;
283 return stage->run( ctx, stage );
284 }
285
286 static void dtr( struct gl_pipeline_stage *stage )
287 {
288 struct vertex_stage_data *store = VERTEX_STAGE_DATA(stage);
289
290 if (store) {
291 _mesa_vector4f_free( &store->eye );
292 _mesa_vector4f_free( &store->clip );
293 _mesa_vector4f_free( &store->proj );
294 ALIGN_FREE( store->clipmask );
295 FREE(store);
296 stage->privatePtr = NULL;
297 stage->run = init_vertex_stage;
298 }
299 }
300
301
302 const struct gl_pipeline_stage _tnl_vertex_transform_stage =
303 {
304 "modelview/project/cliptest/divide",
305 0, /* re-check -- always on */
306 _MESA_NEW_NEED_EYE_COORDS |
307 _NEW_MODELVIEW|
308 _NEW_PROJECTION|
309 _NEW_TRANSFORM, /* re-run */
310 GL_TRUE, /* active */
311 VERT_OBJ, /* inputs */
312 VERT_EYE|VERT_CLIP, /* outputs */
313 0, 0, /* changed_inputs, private */
314 dtr, /* destructor */
315 check_vertex, /* check */
316 init_vertex_stage /* run -- initially set to init */
317 };