1 /* $Id: t_vb_vertex.c,v 1.15 2002/10/24 23:57:25 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
27 * Keith Whitwell <keithw@valinux.com>
39 #include "math/m_xform.h"
41 #include "t_context.h"
42 #include "t_pipeline.h"
46 struct vertex_stage_data
{
55 /* Need these because it's difficult to replay the sideeffects
58 GLvector4f
*save_eyeptr
;
59 GLvector4f
*save_clipptr
;
60 GLvector4f
*save_ndcptr
;
63 #define VERTEX_STAGE_DATA(stage) ((struct vertex_stage_data *)stage->privatePtr)
68 /* This function implements cliptesting for user-defined clip planes.
69 * The clipping of primitives to these planes is implemented in
72 #define USER_CLIPTEST(NAME, SZ) \
73 static void NAME( GLcontext *ctx, \
76 GLubyte *clipormask, \
77 GLubyte *clipandmask ) \
81 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) \
82 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) { \
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; \
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; \
99 clipmask[i] |= CLIP_USER_BIT; \
102 STRIDE_F(coord, stride); \
106 *clipormask |= CLIP_USER_BIT; \
108 *clipandmask |= CLIP_USER_BIT; \
116 USER_CLIPTEST(userclip2
, 2)
117 USER_CLIPTEST(userclip3
, 3)
118 USER_CLIPTEST(userclip4
, 4)
120 static void (*(usercliptab
[5]))( GLcontext
*,
121 GLvector4f
*, GLubyte
*,
122 GLubyte
*, GLubyte
* ) =
133 static GLboolean
run_vertex_stage( GLcontext
*ctx
,
134 struct gl_pipeline_stage
*stage
)
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
;
140 if (stage
->changed_inputs
) {
142 if (ctx
->_NeedEyeCoords
) {
143 /* Separate modelview transformation:
144 * Use combined ModelProject to avoid some depth artifacts
146 if (ctx
->ModelviewMatrixStack
.Top
->type
== MATRIX_IDENTITY
)
147 VB
->EyePtr
= VB
->ObjPtr
;
149 VB
->EyePtr
= TransformRaw( &store
->eye
,
150 ctx
->ModelviewMatrixStack
.Top
,
153 if (ctx
->ProjectionMatrixStack
.Top
->type
== MATRIX_IDENTITY
)
154 VB
->ClipPtr
= VB
->EyePtr
;
156 VB
->ClipPtr
= TransformRaw( &store
->clip
,
157 &ctx
->_ModelProjectMatrix
,
161 /* Combined modelviewproject transform:
163 if (ctx
->_ModelProjectMatrix
.type
== MATRIX_IDENTITY
)
164 VB
->ClipPtr
= VB
->ObjPtr
;
166 VB
->ClipPtr
= TransformRaw( &store
->clip
,
167 &ctx
->_ModelProjectMatrix
,
171 /* Drivers expect this to be clean to element 4...
173 if (VB
->ClipPtr
->size
< 4) {
174 if (VB
->ClipPtr
->flags
& VEC_NOT_WRITEABLE
) {
175 ASSERT(VB
->ClipPtr
== VB
->ObjPtr
);
176 VB
->import_data( ctx
, VERT_BIT_POS
, VEC_NOT_WRITEABLE
);
177 VB
->ClipPtr
= VB
->ObjPtr
;
179 if (VB
->ClipPtr
->size
== 2)
180 _mesa_vector4f_clean_elem( VB
->ClipPtr
, VB
->Count
, 2 );
181 _mesa_vector4f_clean_elem( VB
->ClipPtr
, VB
->Count
, 3 );
184 /* Cliptest and perspective divide. Clip functions must clear
188 store
->andmask
= CLIP_ALL_BITS
;
190 if (tnl
->NeedNdcCoords
) {
192 _mesa_clip_tab
[VB
->ClipPtr
->size
]( VB
->ClipPtr
,
201 _mesa_clip_np_tab
[VB
->ClipPtr
->size
]( VB
->ClipPtr
,
212 /* Test userclip planes. This contributes to VB->ClipMask, so
213 * is essentially required to be in this stage.
215 if (ctx
->Transform
.ClipPlanesEnabled
) {
216 usercliptab
[VB
->ClipPtr
->size
]( ctx
,
226 VB
->ClipOrMask
= store
->ormask
;
227 VB
->ClipMask
= store
->clipmask
;
229 if (VB
->ClipPtr
== VB
->ObjPtr
&& (VB
->importable_data
& VERT_BIT_POS
))
230 VB
->importable_data
|= VERT_BIT_CLIP
;
232 store
->save_eyeptr
= VB
->EyePtr
;
233 store
->save_clipptr
= VB
->ClipPtr
;
234 store
->save_ndcptr
= VB
->NdcPtr
;
237 /* Replay the sideeffects.
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
;
254 static void check_vertex( GLcontext
*ctx
, struct gl_pipeline_stage
*stage
)
256 stage
->active
= !ctx
->VertexProgram
.Enabled
;
259 static GLboolean
init_vertex_stage( GLcontext
*ctx
,
260 struct gl_pipeline_stage
*stage
)
262 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
263 struct vertex_stage_data
*store
;
264 GLuint size
= VB
->Size
;
266 stage
->privatePtr
= CALLOC(sizeof(*store
));
267 store
= VERTEX_STAGE_DATA(stage
);
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 );
275 store
->clipmask
= (GLubyte
*) ALIGN_MALLOC(sizeof(GLubyte
)*size
, 32 );
277 if (!store
->clipmask
||
283 /* Now run the stage.
285 stage
->run
= run_vertex_stage
;
286 return stage
->run( ctx
, stage
);
289 static void dtr( struct gl_pipeline_stage
*stage
)
291 struct vertex_stage_data
*store
= VERTEX_STAGE_DATA(stage
);
294 _mesa_vector4f_free( &store
->eye
);
295 _mesa_vector4f_free( &store
->clip
);
296 _mesa_vector4f_free( &store
->proj
);
297 ALIGN_FREE( store
->clipmask
);
299 stage
->privatePtr
= NULL
;
300 stage
->run
= init_vertex_stage
;
305 const struct gl_pipeline_stage _tnl_vertex_transform_stage
=
307 "modelview/project/cliptest/divide",
308 0, /* re-check -- always on */
309 _MESA_NEW_NEED_EYE_COORDS
|
312 _NEW_TRANSFORM
, /* re-run */
313 GL_TRUE
, /* active */
314 VERT_BIT_POS
, /* inputs */
315 VERT_BIT_EYE
|VERT_BIT_CLIP
, /* outputs */
316 0, /* changed_inputs */
317 NULL
, /* private data */
318 dtr
, /* destructor */
319 check_vertex
, /* check */
320 init_vertex_stage
/* run -- initially set to init */