2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
25 * Keith Whitwell <keith@tungstengraphics.com>
36 #include "math/m_xform.h"
38 #include "t_context.h"
39 #include "t_pipeline.h"
43 struct vertex_stage_data
{
52 /* Need these because it's difficult to replay the sideeffects
55 GLvector4f
*save_eyeptr
;
56 GLvector4f
*save_clipptr
;
57 GLvector4f
*save_ndcptr
;
60 #define VERTEX_STAGE_DATA(stage) ((struct vertex_stage_data *)stage->privatePtr)
65 /* This function implements cliptesting for user-defined clip planes.
66 * The clipping of primitives to these planes is implemented in
69 #define USER_CLIPTEST(NAME, SZ) \
70 static void NAME( GLcontext *ctx, \
73 GLubyte *clipormask, \
74 GLubyte *clipandmask ) \
78 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) \
79 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) { \
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; \
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; \
96 clipmask[i] |= CLIP_USER_BIT; \
99 STRIDE_F(coord, stride); \
103 *clipormask |= CLIP_USER_BIT; \
105 *clipandmask |= CLIP_USER_BIT; \
113 USER_CLIPTEST(userclip2
, 2)
114 USER_CLIPTEST(userclip3
, 3)
115 USER_CLIPTEST(userclip4
, 4)
117 static void (*(usercliptab
[5]))( GLcontext
*,
118 GLvector4f
*, GLubyte
*,
119 GLubyte
*, GLubyte
* ) =
130 static GLboolean
run_vertex_stage( GLcontext
*ctx
,
131 struct tnl_pipeline_stage
*stage
)
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
;
137 ASSERT(!ctx
->VertexProgram
._Enabled
);
139 if (stage
->changed_inputs
) {
141 if (ctx
->_NeedEyeCoords
) {
142 /* Separate modelview transformation:
143 * Use combined ModelProject to avoid some depth artifacts
145 if (ctx
->ModelviewMatrixStack
.Top
->type
== MATRIX_IDENTITY
)
146 VB
->EyePtr
= VB
->ObjPtr
;
148 VB
->EyePtr
= TransformRaw( &store
->eye
,
149 ctx
->ModelviewMatrixStack
.Top
,
153 VB
->ClipPtr
= TransformRaw( &store
->clip
,
154 &ctx
->_ModelProjectMatrix
,
157 /* Drivers expect this to be clean to element 4...
159 switch (VB
->ClipPtr
->size
) {
163 _mesa_vector4f_clean_elem( VB
->ClipPtr
, VB
->Count
, 2 );
165 _mesa_vector4f_clean_elem( VB
->ClipPtr
, VB
->Count
, 3 );
170 /* Cliptest and perspective divide. Clip functions must clear
174 store
->andmask
= CLIP_ALL_BITS
;
176 if (tnl
->NeedNdcCoords
) {
178 _mesa_clip_tab
[VB
->ClipPtr
->size
]( VB
->ClipPtr
,
186 _mesa_clip_np_tab
[VB
->ClipPtr
->size
]( VB
->ClipPtr
,
197 /* Test userclip planes. This contributes to VB->ClipMask, so
198 * is essentially required to be in this stage.
200 if (ctx
->Transform
.ClipPlanesEnabled
) {
201 usercliptab
[VB
->ClipPtr
->size
]( ctx
,
211 VB
->ClipAndMask
= store
->andmask
;
212 VB
->ClipOrMask
= store
->ormask
;
213 VB
->ClipMask
= store
->clipmask
;
215 store
->save_eyeptr
= VB
->EyePtr
;
216 store
->save_clipptr
= VB
->ClipPtr
;
217 store
->save_ndcptr
= VB
->NdcPtr
;
220 /* Replay the sideeffects.
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
->ClipAndMask
= store
->andmask
;
227 VB
->ClipOrMask
= store
->ormask
;
236 static void check_vertex( GLcontext
*ctx
, struct tnl_pipeline_stage
*stage
)
238 stage
->active
= !ctx
->VertexProgram
._Enabled
;
241 static GLboolean
init_vertex_stage( GLcontext
*ctx
,
242 struct tnl_pipeline_stage
*stage
)
244 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
245 struct vertex_stage_data
*store
;
246 GLuint size
= VB
->Size
;
248 stage
->privatePtr
= CALLOC(sizeof(*store
));
249 store
= VERTEX_STAGE_DATA(stage
);
253 _mesa_vector4f_alloc( &store
->eye
, 0, size
, 32 );
254 _mesa_vector4f_alloc( &store
->clip
, 0, size
, 32 );
255 _mesa_vector4f_alloc( &store
->proj
, 0, size
, 32 );
257 store
->clipmask
= (GLubyte
*) ALIGN_MALLOC(sizeof(GLubyte
)*size
, 32 );
259 if (!store
->clipmask
||
265 /* Now run the stage.
267 stage
->run
= run_vertex_stage
;
268 return stage
->run( ctx
, stage
);
271 static void dtr( struct tnl_pipeline_stage
*stage
)
273 struct vertex_stage_data
*store
= VERTEX_STAGE_DATA(stage
);
276 _mesa_vector4f_free( &store
->eye
);
277 _mesa_vector4f_free( &store
->clip
);
278 _mesa_vector4f_free( &store
->proj
);
279 ALIGN_FREE( store
->clipmask
);
281 stage
->privatePtr
= NULL
;
282 stage
->run
= init_vertex_stage
;
287 const struct tnl_pipeline_stage _tnl_vertex_transform_stage
=
289 "modelview/project/cliptest/divide",
290 _NEW_PROGRAM
, /* check_state: only care about vertex prog */
291 _MESA_NEW_NEED_EYE_COORDS
| /* run_state: when to invalidate / re-run */
296 GL_TRUE
, /* active */
297 _TNL_BIT_POS
, /* inputs */
298 _TNL_BIT_POS
, /* outputs */
299 0, /* changed_inputs */
300 NULL
, /* private data */
301 dtr
, /* destructor */
302 check_vertex
, /* check */
303 init_vertex_stage
/* run -- initially set to init */