1/*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
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.
23 *
24 * Authors:
25 *    Keith Whitwell <keithw@vmware.com>
26 */
27
28
29#include "main/glheader.h"
30#include "main/macros.h"
31#include "main/mtypes.h"
32
33#include "math/m_xform.h"
34
35#include "util/bitscan.h"
36#include "util/u_memory.h"
37
38#include "t_context.h"
39#include "t_pipeline.h"
40
41
42
43struct 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)					\
62static void NAME( struct gl_context *ctx,				\
63		  GLvector4f *clip,				\
64		  GLubyte *clipmask,				\
65		  GLubyte *clipormask,				\
66		  GLubyte *clipandmask )			\
67{								\
68   GLbitfield mask = ctx->Transform.ClipPlanesEnabled;          \
69   while (mask) {                                               \
70      const int p = u_bit_scan(&mask);                          \
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
104USER_CLIPTEST(userclip2, 2)
105USER_CLIPTEST(userclip3, 3)
106USER_CLIPTEST(userclip4, 4)
107
108static 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
120void
121tnl_clip_prepare(struct gl_context *ctx)
122{
123   /* Neither the x86 nor sparc asm cliptest functions have been updated
124    * for ARB_depth_clamp, so force the C paths.
125    */
126   if (ctx->Transform.DepthClampNear && ctx->Transform.DepthClampFar) {
127      static GLboolean c_funcs_installed = GL_FALSE;
128      if (!c_funcs_installed) {
129         init_c_cliptest();
130         c_funcs_installed = GL_TRUE;
131      }
132   }
133}
134
135
136
137static GLboolean run_vertex_stage( struct gl_context *ctx,
138				   struct tnl_pipeline_stage *stage )
139{
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;
143
144   if (ctx->VertexProgram._Current)
145      return GL_TRUE;
146
147   tnl_clip_prepare(ctx);
148
149   if (ctx->_NeedEyeCoords) {
150      /* Separate modelview transformation:
151       * Use combined ModelProject to avoid some depth artifacts
152       */
153      if (ctx->ModelviewMatrixStack.Top->type == MATRIX_IDENTITY)
154	 VB->EyePtr = VB->AttribPtr[_TNL_ATTRIB_POS];
155      else
156	 VB->EyePtr = TransformRaw( &store->eye,
157				    ctx->ModelviewMatrixStack.Top,
158				    VB->AttribPtr[_TNL_ATTRIB_POS]);
159   }
160
161   /* make sure the inverse is up to date */
162   _math_matrix_analyse(&ctx->_ModelProjectMatrix);
163
164   VB->ClipPtr = TransformRaw( &store->clip,
165			       &ctx->_ModelProjectMatrix,
166			       VB->AttribPtr[_TNL_ATTRIB_POS] );
167
168   /* Drivers expect this to be clean to element 4...
169    */
170   switch (VB->ClipPtr->size) {
171   case 1:
172      /* impossible */
173   case 2:
174      _mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 2 );
175      FALLTHROUGH;
176   case 3:
177      _mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 3 );
178      FALLTHROUGH;
179   case 4:
180      break;
181   }
182
183
184   /* Cliptest and perspective divide.  Clip functions must clear
185    * the clipmask.
186    */
187   store->ormask = 0;
188   store->andmask = CLIP_FRUSTUM_BITS;
189
190   if (tnl->NeedNdcCoords) {
191      VB->NdcPtr =
192	 _mesa_clip_tab[VB->ClipPtr->size]( VB->ClipPtr,
193					    &store->proj,
194					    store->clipmask,
195					    &store->ormask,
196					    &store->andmask,
197					    !(ctx->Transform.DepthClampNear &&
198                                              ctx->Transform.DepthClampFar) );
199   }
200   else {
201      VB->NdcPtr = NULL;
202      _mesa_clip_np_tab[VB->ClipPtr->size]( VB->ClipPtr,
203					    NULL,
204					    store->clipmask,
205					    &store->ormask,
206					    &store->andmask,
207					    !(ctx->Transform.DepthClampNear &&
208                                              ctx->Transform.DepthClampFar) );
209   }
210
211   if (store->andmask)
212      return GL_FALSE;
213
214
215   /* Test userclip planes.  This contributes to VB->ClipMask, so
216    * is essentially required to be in this stage.
217    */
218   if (ctx->Transform.ClipPlanesEnabled) {
219      usercliptab[VB->ClipPtr->size]( ctx,
220				      VB->ClipPtr,
221				      store->clipmask,
222				      &store->ormask,
223				      &store->andmask );
224
225      if (store->andmask)
226	 return GL_FALSE;
227   }
228
229   VB->ClipAndMask = store->andmask;
230   VB->ClipOrMask = store->ormask;
231   VB->ClipMask = store->clipmask;
232
233   return GL_TRUE;
234}
235
236
237static GLboolean init_vertex_stage( struct gl_context *ctx,
238				    struct tnl_pipeline_stage *stage )
239{
240   struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
241   struct vertex_stage_data *store;
242   GLuint size = VB->Size;
243
244   stage->privatePtr = calloc(1, sizeof(*store));
245   store = VERTEX_STAGE_DATA(stage);
246   if (!store)
247      return GL_FALSE;
248
249   _mesa_vector4f_alloc( &store->eye, 0, size, 32 );
250   _mesa_vector4f_alloc( &store->clip, 0, size, 32 );
251   _mesa_vector4f_alloc( &store->proj, 0, size, 32 );
252
253   store->clipmask = align_malloc(sizeof(GLubyte)*size, 32 );
254
255   if (!store->clipmask ||
256       !store->eye.data ||
257       !store->clip.data ||
258       !store->proj.data)
259      return GL_FALSE;
260
261   return GL_TRUE;
262}
263
264static void dtr( struct tnl_pipeline_stage *stage )
265{
266   struct vertex_stage_data *store = VERTEX_STAGE_DATA(stage);
267
268   if (store) {
269      _mesa_vector4f_free( &store->eye );
270      _mesa_vector4f_free( &store->clip );
271      _mesa_vector4f_free( &store->proj );
272      align_free( store->clipmask );
273      free(store);
274      stage->privatePtr = NULL;
275      stage->run = init_vertex_stage;
276   }
277}
278
279
280const struct tnl_pipeline_stage _tnl_vertex_transform_stage =
281{
282   "modelview/project/cliptest/divide",
283   NULL,			/* private data */
284   init_vertex_stage,
285   dtr,				/* destructor */
286   NULL,
287   run_vertex_stage		/* run -- initially set to init */
288};
289