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