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 <keithw@vmware.com>
29 #include "main/glheader.h"
30 #include "main/macros.h"
31 #include "main/imports.h"
32 #include "main/mtypes.h"
34 #include "math/m_xform.h"
36 #include "t_context.h"
37 #include "t_pipeline.h"
41 struct vertex_stage_data
{
50 #define VERTEX_STAGE_DATA(stage) ((struct vertex_stage_data *)stage->privatePtr)
55 /* This function implements cliptesting for user-defined clip planes.
56 * The clipping of primitives to these planes is implemented in
59 #define USER_CLIPTEST(NAME, SZ) \
60 static void NAME( struct gl_context *ctx, \
63 GLubyte *clipormask, \
64 GLubyte *clipandmask ) \
68 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) \
69 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) { \
71 const GLfloat a = ctx->Transform._ClipUserPlane[p][0]; \
72 const GLfloat b = ctx->Transform._ClipUserPlane[p][1]; \
73 const GLfloat c = ctx->Transform._ClipUserPlane[p][2]; \
74 const GLfloat d = ctx->Transform._ClipUserPlane[p][3]; \
75 GLfloat *coord = (GLfloat *)clip->data; \
76 GLuint stride = clip->stride; \
77 GLuint count = clip->count; \
79 for (nr = 0, i = 0 ; i < count ; i++) { \
80 GLfloat dp = coord[0] * a + coord[1] * b; \
81 if (SZ > 2) dp += coord[2] * c; \
82 if (SZ > 3) dp += coord[3] * d; else dp += d; \
86 clipmask[i] |= CLIP_USER_BIT; \
89 STRIDE_F(coord, stride); \
93 *clipormask |= CLIP_USER_BIT; \
95 *clipandmask |= CLIP_USER_BIT; \
103 USER_CLIPTEST(userclip2
, 2)
104 USER_CLIPTEST(userclip3
, 3)
105 USER_CLIPTEST(userclip4
, 4)
107 static void (*(usercliptab
[5]))( struct gl_context
*,
108 GLvector4f
*, GLubyte
*,
109 GLubyte
*, GLubyte
* ) =
120 tnl_clip_prepare(struct gl_context
*ctx
)
122 /* Neither the x86 nor sparc asm cliptest functions have been updated
123 * for ARB_depth_clamp, so force the C paths.
125 if (ctx
->Transform
.DepthClamp
) {
126 static GLboolean c_funcs_installed
= GL_FALSE
;
127 if (!c_funcs_installed
) {
129 c_funcs_installed
= GL_TRUE
;
136 static GLboolean
run_vertex_stage( struct gl_context
*ctx
,
137 struct tnl_pipeline_stage
*stage
)
139 struct vertex_stage_data
*store
= (struct vertex_stage_data
*)stage
->privatePtr
;
140 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
141 struct vertex_buffer
*VB
= &tnl
->vb
;
143 if (ctx
->VertexProgram
._Current
)
146 tnl_clip_prepare(ctx
);
148 if (ctx
->_NeedEyeCoords
) {
149 /* Separate modelview transformation:
150 * Use combined ModelProject to avoid some depth artifacts
152 if (ctx
->ModelviewMatrixStack
.Top
->type
== MATRIX_IDENTITY
)
153 VB
->EyePtr
= VB
->AttribPtr
[_TNL_ATTRIB_POS
];
155 VB
->EyePtr
= TransformRaw( &store
->eye
,
156 ctx
->ModelviewMatrixStack
.Top
,
157 VB
->AttribPtr
[_TNL_ATTRIB_POS
]);
160 VB
->ClipPtr
= TransformRaw( &store
->clip
,
161 &ctx
->_ModelProjectMatrix
,
162 VB
->AttribPtr
[_TNL_ATTRIB_POS
] );
164 /* Drivers expect this to be clean to element 4...
166 switch (VB
->ClipPtr
->size
) {
170 _mesa_vector4f_clean_elem( VB
->ClipPtr
, VB
->Count
, 2 );
173 _mesa_vector4f_clean_elem( VB
->ClipPtr
, VB
->Count
, 3 );
180 /* Cliptest and perspective divide. Clip functions must clear
184 store
->andmask
= CLIP_FRUSTUM_BITS
;
186 if (tnl
->NeedNdcCoords
) {
188 _mesa_clip_tab
[VB
->ClipPtr
->size
]( VB
->ClipPtr
,
193 !ctx
->Transform
.DepthClamp
);
197 _mesa_clip_np_tab
[VB
->ClipPtr
->size
]( VB
->ClipPtr
,
202 !ctx
->Transform
.DepthClamp
);
209 /* Test userclip planes. This contributes to VB->ClipMask, so
210 * is essentially required to be in this stage.
212 if (ctx
->Transform
.ClipPlanesEnabled
) {
213 usercliptab
[VB
->ClipPtr
->size
]( ctx
,
223 VB
->ClipAndMask
= store
->andmask
;
224 VB
->ClipOrMask
= store
->ormask
;
225 VB
->ClipMask
= store
->clipmask
;
231 static GLboolean
init_vertex_stage( struct gl_context
*ctx
,
232 struct tnl_pipeline_stage
*stage
)
234 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
235 struct vertex_stage_data
*store
;
236 GLuint size
= VB
->Size
;
238 stage
->privatePtr
= calloc(1, sizeof(*store
));
239 store
= VERTEX_STAGE_DATA(stage
);
243 _mesa_vector4f_alloc( &store
->eye
, 0, size
, 32 );
244 _mesa_vector4f_alloc( &store
->clip
, 0, size
, 32 );
245 _mesa_vector4f_alloc( &store
->proj
, 0, size
, 32 );
247 store
->clipmask
= _mesa_align_malloc(sizeof(GLubyte
)*size
, 32 );
249 if (!store
->clipmask
||
258 static void dtr( struct tnl_pipeline_stage
*stage
)
260 struct vertex_stage_data
*store
= VERTEX_STAGE_DATA(stage
);
263 _mesa_vector4f_free( &store
->eye
);
264 _mesa_vector4f_free( &store
->clip
);
265 _mesa_vector4f_free( &store
->proj
);
266 _mesa_align_free( store
->clipmask
);
268 stage
->privatePtr
= NULL
;
269 stage
->run
= init_vertex_stage
;
274 const struct tnl_pipeline_stage _tnl_vertex_transform_stage
=
276 "modelview/project/cliptest/divide",
277 NULL
, /* private data */
279 dtr
, /* destructor */
281 run_vertex_stage
/* run -- initially set to init */