Update Visual Studio Project file for src tree updates.
[mesa.git] / src / mesa / tnl / t_vb_vertex.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.1
4 *
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Keith Whitwell <keith@tungstengraphics.com>
26 */
27
28
29 #include "glheader.h"
30 #include "colormac.h"
31 #include "context.h"
32 #include "macros.h"
33 #include "imports.h"
34 #include "mtypes.h"
35
36 #include "math/m_xform.h"
37
38 #include "t_context.h"
39 #include "t_pipeline.h"
40
41
42
43 struct vertex_stage_data {
44 GLvector4f eye;
45 GLvector4f clip;
46 GLvector4f proj;
47 GLubyte *clipmask;
48 GLubyte ormask;
49 GLubyte andmask;
50
51
52 /* Need these because it's difficult to replay the sideeffects
53 * analytically.
54 */
55 GLvector4f *save_eyeptr;
56 GLvector4f *save_clipptr;
57 GLvector4f *save_ndcptr;
58 };
59
60 #define VERTEX_STAGE_DATA(stage) ((struct vertex_stage_data *)stage->privatePtr)
61
62
63
64
65 /* This function implements cliptesting for user-defined clip planes.
66 * The clipping of primitives to these planes is implemented in
67 * t_render_clip.h.
68 */
69 #define USER_CLIPTEST(NAME, SZ) \
70 static void NAME( GLcontext *ctx, \
71 GLvector4f *clip, \
72 GLubyte *clipmask, \
73 GLubyte *clipormask, \
74 GLubyte *clipandmask ) \
75 { \
76 GLuint p; \
77 \
78 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) \
79 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) { \
80 GLuint nr, i; \
81 const GLfloat a = ctx->Transform._ClipUserPlane[p][0]; \
82 const GLfloat b = ctx->Transform._ClipUserPlane[p][1]; \
83 const GLfloat c = ctx->Transform._ClipUserPlane[p][2]; \
84 const GLfloat d = ctx->Transform._ClipUserPlane[p][3]; \
85 GLfloat *coord = (GLfloat *)clip->data; \
86 GLuint stride = clip->stride; \
87 GLuint count = clip->count; \
88 \
89 for (nr = 0, i = 0 ; i < count ; i++) { \
90 GLfloat dp = coord[0] * a + coord[1] * b; \
91 if (SZ > 2) dp += coord[2] * c; \
92 if (SZ > 3) dp += coord[3] * d; else dp += d; \
93 \
94 if (dp < 0) { \
95 nr++; \
96 clipmask[i] |= CLIP_USER_BIT; \
97 } \
98 \
99 STRIDE_F(coord, stride); \
100 } \
101 \
102 if (nr > 0) { \
103 *clipormask |= CLIP_USER_BIT; \
104 if (nr == count) { \
105 *clipandmask |= CLIP_USER_BIT; \
106 return; \
107 } \
108 } \
109 } \
110 }
111
112
113 USER_CLIPTEST(userclip2, 2)
114 USER_CLIPTEST(userclip3, 3)
115 USER_CLIPTEST(userclip4, 4)
116
117 static void (*(usercliptab[5]))( GLcontext *,
118 GLvector4f *, GLubyte *,
119 GLubyte *, GLubyte * ) =
120 {
121 0,
122 0,
123 userclip2,
124 userclip3,
125 userclip4
126 };
127
128
129
130 static GLboolean run_vertex_stage( GLcontext *ctx,
131 struct tnl_pipeline_stage *stage )
132 {
133 struct vertex_stage_data *store = (struct vertex_stage_data *)stage->privatePtr;
134 TNLcontext *tnl = TNL_CONTEXT(ctx);
135 struct vertex_buffer *VB = &tnl->vb;
136
137 ASSERT(!ctx->VertexProgram._Enabled);
138
139 if (stage->changed_inputs) {
140
141 if (ctx->_NeedEyeCoords) {
142 /* Separate modelview transformation:
143 * Use combined ModelProject to avoid some depth artifacts
144 */
145 if (ctx->ModelviewMatrixStack.Top->type == MATRIX_IDENTITY)
146 VB->EyePtr = VB->ObjPtr;
147 else
148 VB->EyePtr = TransformRaw( &store->eye,
149 ctx->ModelviewMatrixStack.Top,
150 VB->ObjPtr);
151 #if 0
152 /* examine some eye coordinates */
153 {
154 GLuint i;
155 GLfloat *v = VB->EyePtr->start;
156 for (i = 0; i < 4; i++) {
157 _mesa_printf("eye[%d] = %g, %g, %g, %g\n",
158 i, v[0], v[1], v[2], v[3]);
159 v += 4;
160 }
161 }
162 #endif
163 }
164
165 VB->ClipPtr = TransformRaw( &store->clip,
166 &ctx->_ModelProjectMatrix,
167 VB->ObjPtr );
168
169 /* Drivers expect this to be clean to element 4...
170 */
171 switch (VB->ClipPtr->size) {
172 case 1:
173 /* impossible */
174 case 2:
175 _mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 2 );
176 /* fall-through */
177 case 3:
178 _mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 3 );
179 /* fall-through */
180 case 4:
181 break;
182 }
183
184 #if 0
185 /* examine some clip coordinates */
186 {
187 GLuint i;
188 GLfloat *v = VB->ClipPtr->start;
189 for (i = 0; i < 4; i++) {
190 _mesa_printf("clip[%d] = %g, %g, %g, %g\n",
191 i, v[0], v[1], v[2], v[3]);
192 v += 4;
193 }
194 }
195 #endif
196
197 /* Cliptest and perspective divide. Clip functions must clear
198 * the clipmask.
199 */
200 store->ormask = 0;
201 store->andmask = CLIP_ALL_BITS;
202
203 if (tnl->NeedNdcCoords) {
204 VB->NdcPtr =
205 _mesa_clip_tab[VB->ClipPtr->size]( VB->ClipPtr,
206 &store->proj,
207 store->clipmask,
208 &store->ormask,
209 &store->andmask );
210 }
211 else {
212 VB->NdcPtr = 0;
213 _mesa_clip_np_tab[VB->ClipPtr->size]( VB->ClipPtr,
214 0,
215 store->clipmask,
216 &store->ormask,
217 &store->andmask );
218 }
219
220 if (store->andmask)
221 return GL_FALSE;
222
223
224 /* Test userclip planes. This contributes to VB->ClipMask, so
225 * is essentially required to be in this stage.
226 */
227 if (ctx->Transform.ClipPlanesEnabled) {
228 usercliptab[VB->ClipPtr->size]( ctx,
229 VB->ClipPtr,
230 store->clipmask,
231 &store->ormask,
232 &store->andmask );
233
234 if (store->andmask)
235 return GL_FALSE;
236 }
237
238 VB->ClipAndMask = store->andmask;
239 VB->ClipOrMask = store->ormask;
240 VB->ClipMask = store->clipmask;
241
242 store->save_eyeptr = VB->EyePtr;
243 store->save_clipptr = VB->ClipPtr;
244 store->save_ndcptr = VB->NdcPtr;
245 }
246 else {
247 /* Replay the sideeffects.
248 */
249 VB->EyePtr = store->save_eyeptr;
250 VB->ClipPtr = store->save_clipptr;
251 VB->NdcPtr = store->save_ndcptr;
252 VB->ClipMask = store->clipmask;
253 VB->ClipAndMask = store->andmask;
254 VB->ClipOrMask = store->ormask;
255 if (store->andmask)
256 return GL_FALSE;
257 }
258
259 return GL_TRUE;
260 }
261
262
263 static void check_vertex( GLcontext *ctx, struct tnl_pipeline_stage *stage )
264 {
265 stage->active = !ctx->VertexProgram._Enabled;
266 }
267
268 static GLboolean init_vertex_stage( GLcontext *ctx,
269 struct tnl_pipeline_stage *stage )
270 {
271 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
272 struct vertex_stage_data *store;
273 GLuint size = VB->Size;
274
275 stage->privatePtr = CALLOC(sizeof(*store));
276 store = VERTEX_STAGE_DATA(stage);
277 if (!store)
278 return GL_FALSE;
279
280 _mesa_vector4f_alloc( &store->eye, 0, size, 32 );
281 _mesa_vector4f_alloc( &store->clip, 0, size, 32 );
282 _mesa_vector4f_alloc( &store->proj, 0, size, 32 );
283
284 store->clipmask = (GLubyte *) ALIGN_MALLOC(sizeof(GLubyte)*size, 32 );
285
286 if (!store->clipmask ||
287 !store->eye.data ||
288 !store->clip.data ||
289 !store->proj.data)
290 return GL_FALSE;
291
292 /* Now run the stage.
293 */
294 stage->run = run_vertex_stage;
295 return stage->run( ctx, stage );
296 }
297
298 static void dtr( struct tnl_pipeline_stage *stage )
299 {
300 struct vertex_stage_data *store = VERTEX_STAGE_DATA(stage);
301
302 if (store) {
303 _mesa_vector4f_free( &store->eye );
304 _mesa_vector4f_free( &store->clip );
305 _mesa_vector4f_free( &store->proj );
306 ALIGN_FREE( store->clipmask );
307 FREE(store);
308 stage->privatePtr = NULL;
309 stage->run = init_vertex_stage;
310 }
311 }
312
313
314 const struct tnl_pipeline_stage _tnl_vertex_transform_stage =
315 {
316 "modelview/project/cliptest/divide",
317 _NEW_PROGRAM, /* check_state: only care about vertex prog */
318 _MESA_NEW_NEED_EYE_COORDS | /* run_state: when to invalidate / re-run */
319 _NEW_MODELVIEW|
320 _NEW_PROJECTION|
321 _NEW_PROGRAM|
322 _NEW_TRANSFORM,
323 GL_TRUE, /* active */
324 _TNL_BIT_POS, /* inputs */
325 _TNL_BIT_POS, /* outputs */
326 0, /* changed_inputs */
327 NULL, /* private data */
328 dtr, /* destructor */
329 check_vertex, /* check */
330 init_vertex_stage /* run -- initially set to init */
331 };