[MESA]
[reactos.git] / reactos / dll / opengl / mesa / 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/macros.h"
32 #include "main/imports.h"
33 #include "main/mtypes.h"
34
35 #include "math/m_xform.h"
36
37 #include "t_context.h"
38 #include "t_pipeline.h"
39
40
41
42 struct vertex_stage_data {
43 GLvector4f eye;
44 GLvector4f clip;
45 GLvector4f proj;
46 GLubyte *clipmask;
47 GLubyte ormask;
48 GLubyte andmask;
49 };
50
51 #define VERTEX_STAGE_DATA(stage) ((struct vertex_stage_data *)stage->privatePtr)
52
53
54
55
56 /* This function implements cliptesting for user-defined clip planes.
57 * The clipping of primitives to these planes is implemented in
58 * t_render_clip.h.
59 */
60 #define USER_CLIPTEST(NAME, SZ) \
61 static void NAME( struct gl_context *ctx, \
62 GLvector4f *clip, \
63 GLubyte *clipmask, \
64 GLubyte *clipormask, \
65 GLubyte *clipandmask ) \
66 { \
67 GLuint p; \
68 \
69 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) \
70 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) { \
71 GLuint nr, i; \
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; \
79 \
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; \
84 \
85 if (dp < 0) { \
86 nr++; \
87 clipmask[i] |= CLIP_USER_BIT; \
88 } \
89 \
90 STRIDE_F(coord, stride); \
91 } \
92 \
93 if (nr > 0) { \
94 *clipormask |= CLIP_USER_BIT; \
95 if (nr == count) { \
96 *clipandmask |= CLIP_USER_BIT; \
97 return; \
98 } \
99 } \
100 } \
101 }
102
103
104 USER_CLIPTEST(userclip2, 2)
105 USER_CLIPTEST(userclip3, 3)
106 USER_CLIPTEST(userclip4, 4)
107
108 static void (*(usercliptab[5]))( struct gl_context *,
109 GLvector4f *, GLubyte *,
110 GLubyte *, GLubyte * ) =
111 {
112 NULL,
113 NULL,
114 userclip2,
115 userclip3,
116 userclip4
117 };
118
119
120
121 static GLboolean run_vertex_stage( struct gl_context *ctx,
122 struct tnl_pipeline_stage *stage )
123 {
124 struct vertex_stage_data *store = (struct vertex_stage_data *)stage->privatePtr;
125 TNLcontext *tnl = TNL_CONTEXT(ctx);
126 struct vertex_buffer *VB = &tnl->vb;
127
128 if (ctx->_NeedEyeCoords) {
129 /* Separate modelview transformation:
130 * Use combined ModelProject to avoid some depth artifacts
131 */
132 if (ctx->ModelviewMatrixStack.Top->type == MATRIX_IDENTITY)
133 VB->EyePtr = VB->AttribPtr[_TNL_ATTRIB_POS];
134 else
135 VB->EyePtr = TransformRaw( &store->eye,
136 ctx->ModelviewMatrixStack.Top,
137 VB->AttribPtr[_TNL_ATTRIB_POS]);
138 }
139
140 VB->ClipPtr = TransformRaw( &store->clip,
141 &ctx->_ModelProjectMatrix,
142 VB->AttribPtr[_TNL_ATTRIB_POS] );
143
144 /* Drivers expect this to be clean to element 4...
145 */
146 switch (VB->ClipPtr->size) {
147 case 1:
148 /* impossible */
149 case 2:
150 _mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 2 );
151 /* fall-through */
152 case 3:
153 _mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 3 );
154 /* fall-through */
155 case 4:
156 break;
157 }
158
159
160 /* Cliptest and perspective divide. Clip functions must clear
161 * the clipmask.
162 */
163 store->ormask = 0;
164 store->andmask = CLIP_FRUSTUM_BITS;
165
166 if (tnl->NeedNdcCoords) {
167 VB->NdcPtr =
168 _mesa_clip_tab[VB->ClipPtr->size]( VB->ClipPtr,
169 &store->proj,
170 store->clipmask,
171 &store->ormask,
172 &store->andmask);
173 }
174 else {
175 VB->NdcPtr = NULL;
176 _mesa_clip_np_tab[VB->ClipPtr->size]( VB->ClipPtr,
177 NULL,
178 store->clipmask,
179 &store->ormask,
180 &store->andmask );
181 }
182
183 if (store->andmask)
184 return GL_FALSE;
185
186
187 /* Test userclip planes. This contributes to VB->ClipMask, so
188 * is essentially required to be in this stage.
189 */
190 if (ctx->Transform.ClipPlanesEnabled) {
191 usercliptab[VB->ClipPtr->size]( ctx,
192 VB->ClipPtr,
193 store->clipmask,
194 &store->ormask,
195 &store->andmask );
196
197 if (store->andmask)
198 return GL_FALSE;
199 }
200
201 VB->ClipAndMask = store->andmask;
202 VB->ClipOrMask = store->ormask;
203 VB->ClipMask = store->clipmask;
204
205 return GL_TRUE;
206 }
207
208
209 static GLboolean init_vertex_stage( struct gl_context *ctx,
210 struct tnl_pipeline_stage *stage )
211 {
212 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
213 struct vertex_stage_data *store;
214 GLuint size = VB->Size;
215
216 stage->privatePtr = CALLOC(sizeof(*store));
217 store = VERTEX_STAGE_DATA(stage);
218 if (!store)
219 return GL_FALSE;
220
221 _mesa_vector4f_alloc( &store->eye, 0, size, 32 );
222 _mesa_vector4f_alloc( &store->clip, 0, size, 32 );
223 _mesa_vector4f_alloc( &store->proj, 0, size, 32 );
224
225 store->clipmask = (GLubyte *) _mesa_align_malloc(sizeof(GLubyte)*size, 32 );
226
227 if (!store->clipmask ||
228 !store->eye.data ||
229 !store->clip.data ||
230 !store->proj.data)
231 return GL_FALSE;
232
233 return GL_TRUE;
234 }
235
236 static void dtr( struct tnl_pipeline_stage *stage )
237 {
238 struct vertex_stage_data *store = VERTEX_STAGE_DATA(stage);
239
240 if (store) {
241 _mesa_vector4f_free( &store->eye );
242 _mesa_vector4f_free( &store->clip );
243 _mesa_vector4f_free( &store->proj );
244 _mesa_align_free( store->clipmask );
245 FREE(store);
246 stage->privatePtr = NULL;
247 stage->run = init_vertex_stage;
248 }
249 }
250
251
252 const struct tnl_pipeline_stage _tnl_vertex_transform_stage =
253 {
254 "modelview/project/cliptest/divide",
255 NULL, /* private data */
256 init_vertex_stage,
257 dtr, /* destructor */
258 NULL,
259 run_vertex_stage /* run -- initially set to init */
260 };