2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
25 * Keith Whitwell <keith@tungstengraphics.com>
29 #include "main/glheader.h"
30 #include "main/colormac.h"
31 #include "main/macros.h"
32 #include "main/imports.h"
33 #include "main/mtypes.h"
35 #include "math/m_xform.h"
37 #include "t_context.h"
38 #include "t_pipeline.h"
42 struct vertex_stage_data {
51 #define VERTEX_STAGE_DATA(stage) ((struct vertex_stage_data *)stage->privatePtr)
56 /* This function implements cliptesting for user-defined clip planes.
57 * The clipping of primitives to these planes is implemented in
60 #define USER_CLIPTEST(NAME, SZ) \
61 static void NAME( struct gl_context *ctx, \
64 GLubyte *clipormask, \
65 GLubyte *clipandmask ) \
69 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) \
70 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) { \
72 const GLfloat a = ctx->Transform._ClipUserPlane[p][0]; \
73 const GLfloat b = ctx->Transform._ClipUserPlane[p][1]; \
74 const GLfloat c = ctx->Transform._ClipUserPlane[p][2]; \
75 const GLfloat d = ctx->Transform._ClipUserPlane[p][3]; \
76 GLfloat *coord = (GLfloat *)clip->data; \
77 GLuint stride = clip->stride; \
78 GLuint count = clip->count; \
80 for (nr = 0, i = 0 ; i < count ; i++) { \
81 GLfloat dp = coord[0] * a + coord[1] * b; \
82 if (SZ > 2) dp += coord[2] * c; \
83 if (SZ > 3) dp += coord[3] * d; else dp += d; \
87 clipmask[i] |= CLIP_USER_BIT; \
90 STRIDE_F(coord, stride); \
94 *clipormask |= CLIP_USER_BIT; \
96 *clipandmask |= CLIP_USER_BIT; \
104 USER_CLIPTEST(userclip2, 2)
105 USER_CLIPTEST(userclip3, 3)
106 USER_CLIPTEST(userclip4, 4)
108 static void (*(usercliptab[5]))( struct gl_context *,
109 GLvector4f *, GLubyte *,
110 GLubyte *, GLubyte * ) =
121 tnl_clip_prepare(struct gl_context *ctx)
123 /* Neither the x86 nor sparc asm cliptest functions have been updated
124 * for ARB_depth_clamp, so force the C paths.
126 if (ctx->Transform.DepthClamp) {
127 static GLboolean c_funcs_installed = GL_FALSE;
128 if (!c_funcs_installed) {
130 c_funcs_installed = GL_TRUE;
137 static GLboolean run_vertex_stage( struct gl_context *ctx,
138 struct tnl_pipeline_stage *stage )
140 struct vertex_stage_data *store = (struct vertex_stage_data *)stage->privatePtr;
141 TNLcontext *tnl = TNL_CONTEXT(ctx);
142 struct vertex_buffer *VB = &tnl->vb;
144 if (ctx->VertexProgram._Current)
147 tnl_clip_prepare(ctx);
149 if (ctx->_NeedEyeCoords) {
150 /* Separate modelview transformation:
151 * Use combined ModelProject to avoid some depth artifacts
153 if (ctx->ModelviewMatrixStack.Top->type == MATRIX_IDENTITY)
154 VB->EyePtr = VB->AttribPtr[_TNL_ATTRIB_POS];
156 VB->EyePtr = TransformRaw( &store->eye,
157 ctx->ModelviewMatrixStack.Top,
158 VB->AttribPtr[_TNL_ATTRIB_POS]);
161 VB->ClipPtr = TransformRaw( &store->clip,
162 &ctx->_ModelProjectMatrix,
163 VB->AttribPtr[_TNL_ATTRIB_POS] );
165 /* Drivers expect this to be clean to element 4...
167 switch (VB->ClipPtr->size) {
171 _mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 2 );
174 _mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 3 );
181 /* Cliptest and perspective divide. Clip functions must clear
185 store->andmask = CLIP_FRUSTUM_BITS;
187 if (tnl->NeedNdcCoords) {
189 _mesa_clip_tab[VB->ClipPtr->size]( VB->ClipPtr,
194 !ctx->Transform.DepthClamp );
198 _mesa_clip_np_tab[VB->ClipPtr->size]( VB->ClipPtr,
203 !ctx->Transform.DepthClamp );
210 /* Test userclip planes. This contributes to VB->ClipMask, so
211 * is essentially required to be in this stage.
213 if (ctx->Transform.ClipPlanesEnabled) {
214 usercliptab[VB->ClipPtr->size]( ctx,
224 VB->ClipAndMask = store->andmask;
225 VB->ClipOrMask = store->ormask;
226 VB->ClipMask = store->clipmask;
232 static GLboolean init_vertex_stage( struct gl_context *ctx,
233 struct tnl_pipeline_stage *stage )
235 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
236 struct vertex_stage_data *store;
237 GLuint size = VB->Size;
239 stage->privatePtr = calloc(1, sizeof(*store));
240 store = VERTEX_STAGE_DATA(stage);
244 _mesa_vector4f_alloc( &store->eye, 0, size, 32 );
245 _mesa_vector4f_alloc( &store->clip, 0, size, 32 );
246 _mesa_vector4f_alloc( &store->proj, 0, size, 32 );
248 store->clipmask = _mesa_align_malloc(sizeof(GLubyte)*size, 32 );
250 if (!store->clipmask ||
259 static void dtr( struct tnl_pipeline_stage *stage )
261 struct vertex_stage_data *store = VERTEX_STAGE_DATA(stage);
264 _mesa_vector4f_free( &store->eye );
265 _mesa_vector4f_free( &store->clip );
266 _mesa_vector4f_free( &store->proj );
267 _mesa_align_free( store->clipmask );
269 stage->privatePtr = NULL;
270 stage->run = init_vertex_stage;
275 const struct tnl_pipeline_stage _tnl_vertex_transform_stage =
277 "modelview/project/cliptest/divide",
278 NULL, /* private data */
280 dtr, /* destructor */
282 run_vertex_stage /* run -- initially set to init */