Merge branch 'mesa_7_6_branch' of git+ssh://agd5f@git.freedesktop.org/git/mesa/mesa
[mesa.git] / src / mesa / tnl / t_vb_vertex.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2006 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 "main/glheader.h"
30 #include "main/colormac.h"
31 #include "main/context.h"
32 #include "main/macros.h"
33 #include "main/imports.h"
34 #include "main/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 GLfloat *clipdistance[MAX_CLIP_PLANES];
48 GLubyte *clipmask;
49 GLubyte ormask;
50 GLubyte andmask;
51 };
52
53 #define VERTEX_STAGE_DATA(stage) ((struct vertex_stage_data *)stage->privatePtr)
54
55
56
57
58 /* This function implements cliptesting for user-defined clip planes.
59 * The clipping of primitives to these planes is implemented in
60 * t_vp_cliptmp.h.
61 */
62 #define USER_CLIPTEST(NAME, SZ) \
63 static void NAME( GLcontext *ctx, \
64 GLvector4f *clip, \
65 GLfloat *clipdistances[MAX_CLIP_PLANES], \
66 GLubyte *clipmask, \
67 GLubyte *clipormask, \
68 GLubyte *clipandmask ) \
69 { \
70 GLuint p; \
71 \
72 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) \
73 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) { \
74 GLuint nr, i; \
75 const GLfloat a = ctx->Transform._ClipUserPlane[p][0]; \
76 const GLfloat b = ctx->Transform._ClipUserPlane[p][1]; \
77 const GLfloat c = ctx->Transform._ClipUserPlane[p][2]; \
78 const GLfloat d = ctx->Transform._ClipUserPlane[p][3]; \
79 GLfloat *coord = (GLfloat *)clip->data; \
80 GLuint stride = clip->stride; \
81 GLuint count = clip->count; \
82 \
83 for (nr = 0, i = 0 ; i < count ; i++) { \
84 GLfloat dp = coord[0] * a + coord[1] * b; \
85 if (SZ > 2) dp += coord[2] * c; \
86 if (SZ > 3) dp += coord[3] * d; else dp += d; \
87 \
88 if (dp < 0) { \
89 nr++; \
90 clipmask[i] |= CLIP_USER_BIT; \
91 } \
92 \
93 clipdistances[p][i] = dp; \
94 \
95 STRIDE_F(coord, stride); \
96 } \
97 \
98 if (nr > 0) { \
99 *clipormask |= CLIP_USER_BIT; \
100 if (nr == count) { \
101 *clipandmask |= CLIP_USER_BIT; \
102 return; \
103 } \
104 } \
105 } \
106 }
107
108
109 USER_CLIPTEST(userclip2, 2)
110 USER_CLIPTEST(userclip3, 3)
111 USER_CLIPTEST(userclip4, 4)
112
113 static void (*(usercliptab[5]))( GLcontext *,
114 GLvector4f *,
115 GLfloat *[MAX_CLIP_PLANES],
116 GLubyte *, GLubyte *, GLubyte * ) =
117 {
118 NULL,
119 NULL,
120 userclip2,
121 userclip3,
122 userclip4
123 };
124
125
126 void
127 tnl_clip_prepare(GLcontext *ctx)
128 {
129 /* Neither the x86 nor sparc asm cliptest functions have been updated
130 * for ARB_depth_clamp, so force the C paths.
131 */
132 if (ctx->Transform.DepthClamp) {
133 static GLboolean c_funcs_installed = GL_FALSE;
134 if (!c_funcs_installed) {
135 init_c_cliptest();
136 c_funcs_installed = GL_TRUE;
137 }
138 }
139 }
140
141
142
143 static GLboolean run_vertex_stage( GLcontext *ctx,
144 struct tnl_pipeline_stage *stage )
145 {
146 struct vertex_stage_data *store = (struct vertex_stage_data *)stage->privatePtr;
147 TNLcontext *tnl = TNL_CONTEXT(ctx);
148 struct vertex_buffer *VB = &tnl->vb;
149
150 if (ctx->VertexProgram._Current)
151 return GL_TRUE;
152
153 tnl_clip_prepare(ctx);
154
155 if (ctx->_NeedEyeCoords) {
156 /* Separate modelview transformation:
157 * Use combined ModelProject to avoid some depth artifacts
158 */
159 if (ctx->ModelviewMatrixStack.Top->type == MATRIX_IDENTITY)
160 VB->EyePtr = VB->ObjPtr;
161 else
162 VB->EyePtr = TransformRaw( &store->eye,
163 ctx->ModelviewMatrixStack.Top,
164 VB->ObjPtr);
165 }
166
167 VB->ClipPtr = TransformRaw( &store->clip,
168 &ctx->_ModelProjectMatrix,
169 VB->ObjPtr );
170
171 /* Drivers expect this to be clean to element 4...
172 */
173 switch (VB->ClipPtr->size) {
174 case 1:
175 /* impossible */
176 case 2:
177 _mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 2 );
178 /* fall-through */
179 case 3:
180 _mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 3 );
181 /* fall-through */
182 case 4:
183 break;
184 }
185
186
187 /* Cliptest and perspective divide. Clip functions must clear
188 * the clipmask.
189 */
190 store->ormask = 0;
191 store->andmask = CLIP_FRUSTUM_BITS;
192
193 if (tnl->NeedNdcCoords) {
194 VB->NdcPtr =
195 _mesa_clip_tab[VB->ClipPtr->size]( VB->ClipPtr,
196 &store->proj,
197 store->clipmask,
198 &store->ormask,
199 &store->andmask,
200 !ctx->Transform.DepthClamp );
201 }
202 else {
203 VB->NdcPtr = NULL;
204 _mesa_clip_np_tab[VB->ClipPtr->size]( VB->ClipPtr,
205 NULL,
206 store->clipmask,
207 &store->ormask,
208 &store->andmask,
209 !ctx->Transform.DepthClamp );
210 }
211
212 if (store->andmask)
213 return GL_FALSE;
214
215
216 /* Test userclip planes. This contributes to VB->ClipMask, so
217 * is essentially required to be in this stage.
218 */
219 if (ctx->Transform.ClipPlanesEnabled) {
220 usercliptab[VB->ClipPtr->size]( ctx,
221 VB->ClipPtr,
222 store->clipdistance,
223 store->clipmask,
224 &store->ormask,
225 &store->andmask );
226
227 if (store->andmask)
228 return GL_FALSE;
229
230 memcpy(VB->ClipDistancePtr, store->clipdistance,
231 sizeof(store->clipdistance));
232 }
233
234 VB->ClipAndMask = store->andmask;
235 VB->ClipOrMask = store->ormask;
236 VB->ClipMask = store->clipmask;
237
238 return GL_TRUE;
239 }
240
241
242 static GLboolean init_vertex_stage( GLcontext *ctx,
243 struct tnl_pipeline_stage *stage )
244 {
245 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
246 struct vertex_stage_data *store;
247 GLuint size = VB->Size;
248 unsigned i;
249
250 stage->privatePtr = CALLOC(sizeof(*store));
251 store = VERTEX_STAGE_DATA(stage);
252 if (!store)
253 return GL_FALSE;
254
255 _mesa_vector4f_alloc( &store->eye, 0, size, 32 );
256 _mesa_vector4f_alloc( &store->clip, 0, size, 32 );
257 _mesa_vector4f_alloc( &store->proj, 0, size, 32 );
258
259 store->clipmask = (GLubyte *) ALIGN_MALLOC(sizeof(GLubyte)*size, 32 );
260 for (i = 0; i < MAX_CLIP_PLANES; i++)
261 store->clipdistance[i] =
262 (GLfloat *) ALIGN_MALLOC(sizeof(GLfloat) * size, 32);
263
264 if (!store->clipmask ||
265 !store->clipdistance[0] ||
266 !store->clipdistance[1] ||
267 !store->clipdistance[2] ||
268 !store->clipdistance[3] ||
269 !store->clipdistance[4] ||
270 !store->clipdistance[5] ||
271 !store->eye.data ||
272 !store->clip.data ||
273 !store->proj.data)
274 return GL_FALSE;
275
276 return GL_TRUE;
277 }
278
279 static void dtr( struct tnl_pipeline_stage *stage )
280 {
281 struct vertex_stage_data *store = VERTEX_STAGE_DATA(stage);
282
283 if (store) {
284 unsigned i;
285
286 _mesa_vector4f_free( &store->eye );
287 _mesa_vector4f_free( &store->clip );
288 _mesa_vector4f_free( &store->proj );
289 ALIGN_FREE( store->clipmask );
290
291 for (i = 0; i < MAX_CLIP_PLANES; i++)
292 ALIGN_FREE(store->clipdistance[i]);
293
294 FREE(store);
295 stage->privatePtr = NULL;
296 stage->run = init_vertex_stage;
297 }
298 }
299
300
301 const struct tnl_pipeline_stage _tnl_vertex_transform_stage =
302 {
303 "modelview/project/cliptest/divide",
304 NULL, /* private data */
305 init_vertex_stage,
306 dtr, /* destructor */
307 NULL,
308 run_vertex_stage /* run -- initially set to init */
309 };