1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
/* $Id: t_vb_vertex.c,v 1.14 2002/04/09 16:56:52 keithw Exp $ */
/*
* Mesa 3-D graphics library
* Version: 3.5
*
* Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Authors:
* Keith Whitwell <keithw@valinux.com>
*/
#include "glheader.h"
#include "colormac.h"
#include "context.h"
#include "macros.h"
#include "mem.h"
#include "mmath.h"
#include "mtypes.h"
#include "math/m_xform.h"
#include "t_context.h"
#include "t_pipeline.h"
struct vertex_stage_data {
GLvector4f eye;
GLvector4f clip;
GLvector4f proj;
GLubyte *clipmask;
GLubyte ormask;
GLubyte andmask;
/* Need these because it's difficult to replay the sideeffects
* analytically.
*/
GLvector4f *save_eyeptr;
GLvector4f *save_clipptr;
GLvector4f *save_ndcptr;
};
#define VERTEX_STAGE_DATA(stage) ((struct vertex_stage_data *)stage->privatePtr)
/* This function implements cliptesting for user-defined clip planes.
* The clipping of primitives to these planes is implemented in
* t_render_clip.h.
*/
#define USER_CLIPTEST(NAME, SZ) \
static void NAME( GLcontext *ctx, \
GLvector4f *clip, \
GLubyte *clipmask, \
GLubyte *clipormask, \
GLubyte *clipandmask ) \
{ \
GLuint p; \
\
for (p = 0; p < ctx->Const.MaxClipPlanes; p++) \
if (ctx->Transform.ClipPlanesEnabled & (1 << p)) { \
GLuint nr, i; \
const GLfloat a = ctx->Transform._ClipUserPlane[p][0]; \
const GLfloat b = ctx->Transform._ClipUserPlane[p][1]; \
const GLfloat c = ctx->Transform._ClipUserPlane[p][2]; \
const GLfloat d = ctx->Transform._ClipUserPlane[p][3]; \
GLfloat *coord = (GLfloat *)clip->data; \
GLuint stride = clip->stride; \
GLuint count = clip->count; \
\
for (nr = 0, i = 0 ; i < count ; i++) { \
GLfloat dp = coord[0] * a + coord[1] * b; \
if (SZ > 2) dp += coord[2] * c; \
if (SZ > 3) dp += coord[3] * d; else dp += d; \
\
if (dp < 0) { \
nr++; \
clipmask[i] |= CLIP_USER_BIT; \
} \
\
STRIDE_F(coord, stride); \
} \
\
if (nr > 0) { \
*clipormask |= CLIP_USER_BIT; \
if (nr == count) { \
*clipandmask |= CLIP_USER_BIT; \
return; \
} \
} \
} \
}
USER_CLIPTEST(userclip2, 2)
USER_CLIPTEST(userclip3, 3)
USER_CLIPTEST(userclip4, 4)
static void (*(usercliptab[5]))( GLcontext *,
GLvector4f *, GLubyte *,
GLubyte *, GLubyte * ) =
{
0,
0,
userclip2,
userclip3,
userclip4
};
static GLboolean run_vertex_stage( GLcontext *ctx,
struct gl_pipeline_stage *stage )
{
struct vertex_stage_data *store = (struct vertex_stage_data *)stage->privatePtr;
TNLcontext *tnl = TNL_CONTEXT(ctx);
struct vertex_buffer *VB = &tnl->vb;
if (stage->changed_inputs) {
if (ctx->_NeedEyeCoords) {
/* Separate modelview transformation:
* Use combined ModelProject to avoid some depth artifacts
*/
if (ctx->ModelviewMatrixStack.Top->type == MATRIX_IDENTITY)
VB->EyePtr = VB->ObjPtr;
else
VB->EyePtr = TransformRaw( &store->eye,
ctx->ModelviewMatrixStack.Top,
VB->ObjPtr);
if (ctx->ProjectionMatrixStack.Top->type == MATRIX_IDENTITY)
VB->ClipPtr = VB->EyePtr;
else
VB->ClipPtr = TransformRaw( &store->clip,
&ctx->_ModelProjectMatrix,
VB->ObjPtr );
}
else {
/* Combined modelviewproject transform:
*/
if (ctx->_ModelProjectMatrix.type == MATRIX_IDENTITY)
VB->ClipPtr = VB->ObjPtr;
else
VB->ClipPtr = TransformRaw( &store->clip,
&ctx->_ModelProjectMatrix,
VB->ObjPtr );
}
/* Drivers expect this to be clean to element 4...
*/
if (VB->ClipPtr->size < 4) {
if (VB->ClipPtr->flags & VEC_NOT_WRITEABLE) {
ASSERT(VB->ClipPtr == VB->ObjPtr);
VB->import_data( ctx, VERT_BIT_POS, VEC_NOT_WRITEABLE );
VB->ClipPtr = VB->ObjPtr;
}
if (VB->ClipPtr->size == 2)
_mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 2 );
_mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 3 );
}
/* Cliptest and perspective divide. Clip functions must clear
* the clipmask.
*/
store->ormask = 0;
store->andmask = CLIP_ALL_BITS;
if (tnl->NeedNdcCoords) {
VB->NdcPtr =
_mesa_clip_tab[VB->ClipPtr->size]( VB->ClipPtr,
&store->proj,
store->clipmask,
&store->ormask,
&store->andmask );
}
else {
VB->NdcPtr = 0;
_mesa_clip_np_tab[VB->ClipPtr->size]( VB->ClipPtr,
0,
store->clipmask,
&store->ormask,
&store->andmask );
}
if (store->andmask)
return GL_FALSE;
/* Test userclip planes. This contributes to VB->ClipMask, so
* is essentially required to be in this stage.
*/
if (ctx->Transform.ClipPlanesEnabled) {
usercliptab[VB->ClipPtr->size]( ctx,
VB->ClipPtr,
store->clipmask,
&store->ormask,
&store->andmask );
if (store->andmask)
return GL_FALSE;
}
VB->ClipOrMask = store->ormask;
VB->ClipMask = store->clipmask;
if (VB->ClipPtr == VB->ObjPtr && (VB->importable_data & VERT_BIT_POS))
VB->importable_data |= VERT_BIT_CLIP;
store->save_eyeptr = VB->EyePtr;
store->save_clipptr = VB->ClipPtr;
store->save_ndcptr = VB->NdcPtr;
}
else {
/* Replay the sideeffects.
*/
VB->EyePtr = store->save_eyeptr;
VB->ClipPtr = store->save_clipptr;
VB->NdcPtr = store->save_ndcptr;
VB->ClipMask = store->clipmask;
VB->ClipOrMask = store->ormask;
if (VB->ClipPtr == VB->ObjPtr && (VB->importable_data & VERT_BIT_POS))
VB->importable_data |= VERT_BIT_CLIP;
if (store->andmask)
return GL_FALSE;
}
return GL_TRUE;
}
static void check_vertex( GLcontext *ctx, struct gl_pipeline_stage *stage )
{
stage->active = !ctx->VertexProgram.Enabled;
}
static GLboolean init_vertex_stage( GLcontext *ctx,
struct gl_pipeline_stage *stage )
{
struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
struct vertex_stage_data *store;
GLuint size = VB->Size;
stage->privatePtr = CALLOC(sizeof(*store));
store = VERTEX_STAGE_DATA(stage);
if (!store)
return GL_FALSE;
_mesa_vector4f_alloc( &store->eye, 0, size, 32 );
_mesa_vector4f_alloc( &store->clip, 0, size, 32 );
_mesa_vector4f_alloc( &store->proj, 0, size, 32 );
store->clipmask = (GLubyte *) ALIGN_MALLOC(sizeof(GLubyte)*size, 32 );
if (!store->clipmask ||
!store->eye.data ||
!store->clip.data ||
!store->proj.data)
return GL_FALSE;
/* Now run the stage.
*/
stage->run = run_vertex_stage;
return stage->run( ctx, stage );
}
static void dtr( struct gl_pipeline_stage *stage )
{
struct vertex_stage_data *store = VERTEX_STAGE_DATA(stage);
if (store) {
_mesa_vector4f_free( &store->eye );
_mesa_vector4f_free( &store->clip );
_mesa_vector4f_free( &store->proj );
ALIGN_FREE( store->clipmask );
FREE(store);
stage->privatePtr = NULL;
stage->run = init_vertex_stage;
}
}
const struct gl_pipeline_stage _tnl_vertex_transform_stage =
{
"modelview/project/cliptest/divide",
0, /* re-check -- always on */
_MESA_NEW_NEED_EYE_COORDS |
_NEW_MODELVIEW|
_NEW_PROJECTION|
_NEW_TRANSFORM, /* re-run */
GL_TRUE, /* active */
VERT_BIT_POS, /* inputs */
VERT_BIT_EYE|VERT_BIT_CLIP, /* outputs */
0, /* changed_inputs */
NULL, /* private data */
dtr, /* destructor */
check_vertex, /* check */
init_vertex_stage /* run -- initially set to init */
};
|