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