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