summaryrefslogtreecommitdiff
path: root/xc/extras/Mesa/src/FX/fxclip.c
blob: 7067516be3b969649c168f0fa4bf5c62b633b899 (plain)
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/* -*- mode: C; tab-width:8; c-basic-offset:2 -*- */

/*
 * Mesa 3-D graphics library
 * Version:  3.3
 *
 * Copyright (C) 1999-2000  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.
 *
 *
 * Original Mesa / 3Dfx device driver (C) 1999 David Bucciarelli, by the
 * terms stated above.
 *
 * Thank you for your contribution, David!
 *
 * Please make note of the above copyright/license statement.  If you
 * contributed code or bug fixes to this code under the previous (GNU
 * Library) license and object to the new license, your code will be
 * removed at your request.  Please see the Mesa docs/COPYRIGHT file
 * for more information.
 *
 * Additional Mesa/3Dfx driver developers:
 *   Daryll Strauss <daryll@precisioninsight.com>
 *   Keith Whitwell <keith@precisioninsight.com>
 *
 * See fxapi.h for more revision/author details.
 */


#ifdef HAVE_CONFIG_H
#include "conf.h"
#endif

#if defined(FX)

#include "fxdrv.h"
#include "mmath.h"
#include "macros.h"
#include "vector.h"
#include "types.h"


#if 0 && defined(__i386__)
#define NEGATIVE(f)          ((*(int *)&f) < 0)
#define DIFFERENT_SIGNS(a,b) (((*(int *)&a)^(*(int *)&b)) < 0)
#else
#define NEGATIVE(f)          (f < 0)
#define DIFFERENT_SIGNS(a,b) ((a*b) < 0)
#endif

#define LINTERP( T, A, B )   ( (A) + (T) * ( (B) - (A) ) )


#define CLIP(sgn,v)						\
do {								\
   GLuint out = in ^ 1;						\
   GLfloat **indata = inlist[in];				\
   GrVertex **inverts = vertlist[in];				\
   GrVertex **outverts = vertlist[out];				\
   GLfloat **outdata = inlist[in = out];			\
   GLfloat *J = indata[n-1];					\
   GLfloat dpJ = (sgn J[v]) + J[3];				\
   GLuint nr = n;						\
								\
   for (i = n = 0 ; i < nr ; i++) {				\
      GLfloat *I = indata[i];					\
      GLfloat dpI = (sgn I[v]) + I[3];				\
								\
      if (DIFFERENT_SIGNS(dpI, dpJ)) {				\
	 GLuint j;						\
         GLfloat t = dpI / (dpI - dpJ);                         \
	 outverts[n] = 0;					\
	 outdata[n++] = store;					\
	 store[3] = LINTERP(t, I[3], J[3]);			\
	 store[v] = - sgn store[3];				\
	 if (v != 0) store[0] = LINTERP(t, I[0], J[0]);		\
	 if (v != 1) store[1] = LINTERP(t, I[1], J[1]);		\
	 if (v != 2) store[2] = LINTERP(t, I[2], J[2]);		\
	 store += 4;						\
	 for (j = 4 ; j < sz ; j+=4,store+=4) {			\
	    store[0] = LINTERP(t, I[j],   J[j] );		\
	    store[1] = LINTERP(t, I[j+1], J[j+1] );		\
	    store[2] = LINTERP(t, I[j+2], J[j+2] );		\
	    store[3] = LINTERP(t, I[j+3], J[j+3] );		\
	 }							\
      }								\
								\
      if (!NEGATIVE(dpI)) {					\
	 outverts[n] = inverts[i];				\
	 outdata[n++] = I;					\
      }								\
								\
								\
      J = I;							\
      dpJ = dpI;						\
   }								\
								\
   if (n < 3) return 0;						\
} while (0)


/* Originally used this for the viewclip planes as well, as in
 * CLIP(-1,0,0,1), which was just as fast, but tended to lead to
 * cracks.  I haven't figured out exactly why this is, but the above
 * code only really differs in the way it sets store[v] to +- w.
 */
#define UCLIP(a,b,c,d)					\
do {							\
   GLuint out = in ^ 1;					\
   GLfloat **indata = inlist[in];			\
   GrVertex **inverts = vertlist[in];			\
   GrVertex **outverts = vertlist[out];			\
   GLfloat **outdata = inlist[in = out];		\
   GLfloat *J = indata[n-1];				\
   GLfloat dpJ = DOT4V(J,a,b,c,d);			\
   GLuint nr = n;					\
							\
   for (i = n = 0 ; i < nr ; i++) {			\
      GLfloat *I = indata[i];				\
      GLfloat dpI = DOT4V(I,a,b,c,d);			\
							\
      if (DIFFERENT_SIGNS(dpI, dpJ)) {			\
	 GLuint j;					\
	 GLfloat t = dpI / (dpI - dpJ);			\
	 outverts[n] = 0;				\
	 outdata[n++] = store;				\
	 for (j = 0 ; j < sz ; j+=4,store+=4) {		\
	    store[0] = LINTERP(t, I[j],   J[j] );	\
	    store[1] = LINTERP(t, I[j+1], J[j+1] );	\
	    store[2] = LINTERP(t, I[j+2], J[j+2] );	\
	    store[3] = LINTERP(t, I[j+3], J[j+3] );	\
	 }						\
      }							\
							\
      if (!NEGATIVE(dpI)) {				\
	 outverts[n] = inverts[i];			\
	 outdata[n++] = I;				\
      }							\
							\
							\
      J = I;						\
      dpJ = dpI;					\
   }							\
							\
   if (n < 3) return 0;					\
} while (0)



/* Data parameter is organized as 4 clip coordinates followed by an
 * arbitary number (sz-4) of additional data.  The three original
 * vertices are packed together at the start, and there is room for at
 * least VB_MAX_CLIPPED_VERTS vertices of the same size in this
 * storage.
 *
 */
static INLINE GLuint fx_clip_triangle( GLcontext *ctx,
				       GLfloat *inoutlist[],
				       GrVertex **verts,
				       GLuint sz,
				       GLuint mask )
{
   GLuint n = 3;
   GLfloat *store = inoutlist[n-1] + sz;
   GLfloat *vlist[VB_MAX_CLIPPED_VERTS];
   GrVertex *verts2[VB_MAX_CLIPPED_VERTS];
   GLfloat **inlist[2];
   GrVertex **vertlist[2];
   GLuint in = 0;
   GLuint i;

   inlist[0] = inoutlist;
   inlist[1] = vlist;

   vertlist[0] = verts;
   vertlist[1] = verts2;

   if (mask & CLIP_ALL_BITS)
   {
      if (mask & CLIP_RIGHT_BIT)
	 CLIP(-,0);

      if (mask & CLIP_LEFT_BIT)
	 CLIP(+,0);

      if (mask & CLIP_TOP_BIT)
	 CLIP(-,1);

      if (mask & CLIP_BOTTOM_BIT)
	 CLIP(+,1);

      if (mask & CLIP_FAR_BIT)
	 CLIP(-,2);

      if (mask & CLIP_NEAR_BIT)
	 CLIP(+,2);
   }

   if (mask & CLIP_USER_BIT) {
      GLuint bit;
      GLfloat (*plane)[4] = &ctx->Transform.ClipUserPlane[0];
      for (bit = 0x100 ; bit < mask ; plane++, bit *= 2) {
	 if (mask & bit) {
	    GLfloat a = plane[0][0];
	    GLfloat b = plane[0][1];
	    GLfloat c = plane[0][2];
	    GLfloat d = plane[0][3];
	    UCLIP(a,b,c,d);
	 }
      }
   }

   if (inlist[in] != inoutlist)
      for (i = 0 ; i < n ; i++) {
	 inoutlist[i] = inlist[in][i];
	 verts[i] = verts2[i];
      }

   return n;
}



static INLINE GLuint fx_view_clip_triangle( GLcontext *ctx,
					    GLfloat *inoutlist[],
					    GrVertex **verts,
					    GLuint sz,
					    GLubyte mask )
{
   GLuint n = 3;
   GLfloat *store = inoutlist[n-1] + sz;
   GLfloat *vlist[VB_MAX_CLIPPED_VERTS];
   GrVertex *verts2[VB_MAX_CLIPPED_VERTS];
   GLfloat **inlist[2];
   GrVertex **vertlist[2];
   GLuint in = 0;
   GLuint i;

   inlist[0] = inoutlist;
   inlist[1] = vlist;

   vertlist[0] = verts;
   vertlist[1] = verts2;

   if (mask & CLIP_RIGHT_BIT)
      CLIP(-,0);

   if (mask & CLIP_LEFT_BIT)
      CLIP(+,0);

   if (mask & CLIP_TOP_BIT)
      CLIP(-,1);

   if (mask & CLIP_BOTTOM_BIT)
      CLIP(+,1);

   if (mask & CLIP_FAR_BIT)
      CLIP(-,2);

   if (mask & CLIP_NEAR_BIT)
      CLIP(+,2);

   if (inlist[in] != inoutlist)
      for (i = 0 ; i < n ; i++) {
	 inoutlist[i] = inlist[in][i];
	 verts[i] = verts2[i];
      }

   return n;
}



#undef CLIP

#define CLIP(x,y,z,w)					\
do {							\
   GLfloat dpI = DOT4V(I,x,y,z,w);			\
   GLfloat dpJ = DOT4V(J,x,y,z,w);			\
							\
   if (DIFFERENT_SIGNS(dpI, dpJ)) {			\
      GLuint j;						\
      GLfloat t = dpI / (dpI - dpJ); 			\
      GLfloat *tmp = store;				\
							\
      for (j = 0 ; j < sz ; j+=2) {			\
	 *store++ = LINTERP(t, I[j],   J[j] );		\
	 *store++ = LINTERP(t, I[j+1], J[j+1] );	\
      }							\
							\
      if (NEGATIVE(dpI)) 				\
	 I = tmp;					\
      else						\
	 J = tmp;					\
							\
   } 							\
   else if (NEGATIVE(dpI))				\
      return 0;						\
							\
} while (0)


static GLuint fx_clip_line( GLcontext *ctx,
			    GLfloat *inoutlist[],
			    GLuint sz,
			    GLubyte clipor )
{
   GLfloat *I = inoutlist[0];
   GLfloat *J = inoutlist[1];
   GLfloat *store = J + sz;

   if (clipor & CLIP_ALL_BITS)
   {
      if (clipor & CLIP_LEFT_BIT)
	 CLIP(1,0,0,1);
 
      if (clipor & CLIP_RIGHT_BIT)
	 CLIP(-1,0,0,1);
 
      if (clipor & CLIP_TOP_BIT)
	 CLIP(0,-1,0,1);
 
      if (clipor & CLIP_BOTTOM_BIT)
	 CLIP(0,1,0,1);
 
      if (clipor & CLIP_FAR_BIT)
	 CLIP(0,0,-1,1);
 
      if (clipor & CLIP_NEAR_BIT)
	 CLIP(0,0,1,1);
   }

   if (clipor & CLIP_USER_BIT) {
      GLuint i;
      for (i = 0 ; i < MAX_CLIP_PLANES ; i++) {
	 if (ctx->Transform.ClipEnabled[i]) {
	    GLfloat a = ctx->Transform.ClipUserPlane[i][0];
	    GLfloat b = ctx->Transform.ClipUserPlane[i][1];
	    GLfloat c = ctx->Transform.ClipUserPlane[i][2];
	    GLfloat d = ctx->Transform.ClipUserPlane[i][3];
	    CLIP(a,b,c,d);
	 }
      }
   }

   inoutlist[0] = I;
   inoutlist[1] = J;

   return 2;
}






#if defined(FX_V2)

#define VARS_XYZW				\
  GLfloat vsx = mat[MAT_SX];			\
  GLfloat vsy = mat[MAT_SY];			\
  GLfloat vsz = mat[MAT_SZ];			\
  GLfloat vtx = mat[MAT_TX];			\
  GLfloat vty = mat[MAT_TY];			\
  GLfloat vtz = mat[MAT_TZ];			

#define DO_SETUP_XYZW				\
{						\
  GLfloat oow = 1.0 / data[3];			\
  v->x   = data[0]*oow*vsx + vtx;		\
  v->y   = data[1]*oow*vsy + vty;		\
  v->ooz = data[2]*oow*vsz + vtz;		\
  v->oow = oow; 				\
}
#else

#if defined(DRIVERTS)

#define VARS_XYZW				\
  GLfloat vsx = mat[MAT_SX];			\
  GLfloat vsy = mat[MAT_SY];			\
  GLfloat vsz = mat[MAT_SZ];			\
  GLfloat vtx = mat[MAT_TX]+fxMesa->x_offset;	\
  GLfloat vty = mat[MAT_TY]+fxMesa->y_delta;	\
  GLfloat vtz = mat[MAT_TZ];

#define DO_SETUP_XYZW				\
{						\
  GLfloat oow = 1.0 / data[3];			\
  v->x   = data[0]*oow*vsx + vtx;		\
  v->y   = data[1]*oow*vsy + vty;		\
  v->ooz = data[2]*oow*vsz + vtz;		\
  v->oow = oow;					\
}

#else
#define VARS_XYZW 				\
  GLfloat vsx = mat[MAT_SX];			\
  GLfloat vsy = mat[MAT_SY];			\
  GLfloat vsz = mat[MAT_SZ];			\
  const GLfloat snapper = (3L << 18);		\
  GLfloat snap_tx = mat[MAT_TX] + snapper;	\
  GLfloat snap_ty = mat[MAT_TY] + snapper;	\
  GLfloat vtz = mat[MAT_TZ];

#define DO_SETUP_XYZW				\
{						\
  GLfloat oow = 1.0 / data[3];			\
  v->x = data[0]*oow*vsx + snap_tx;		\
  v->y = data[1]*oow*vsy + snap_ty;		\
  v->ooz = data[2]*oow*vsz + vtz;		\
  v->oow = oow;					\
  v->x -= snapper;				\
  v->y -= snapper;				\
}

#endif
#endif

#define COPY_XYZW_STRIDE				\
  { GLfloat *clip = VEC_ELT(VB->ClipPtr, GLfloat, e);	\
    COPY_4FV(out, clip); }

#define VARS_RGBA

#define COPY_RGBA_STRIDE					\
  { GLubyte *color = VEC_ELT(VB->ColorPtr, GLubyte, e);		\
    UBYTE_RGBA_TO_FLOAT_255_RGBA((out+4), color); }

#if FX_USE_PARGB
#define DO_SETUP_RGBA				\
  GET_PARGB(v) = (((int)data[4+3]) << 24) | 	\
  		 (((int)data[4+0]) << 16) | 	\
  		 (((int)data[4+1]) << 8)  |	\
  		 (((int)data[4+2]) << 0);	
#else
#define DO_SETUP_RGBA				\
  v->r = data[4+0];				\
  v->g = data[4+1];				\
  v->b = data[4+2];				\
  v->a = data[4+3];
#endif /* FX_USE_PARGB */

#define VARS_TMU0							\
  struct gl_texture_unit *t0 = &ctx->Texture.Unit[tmu0_source];		\
  GLfloat sScale0 = fxTMGetTexInfo(t0->Current)->sScale;	\
  GLfloat tScale0 = fxTMGetTexInfo(t0->Current)->tScale;	\


#define COPY_TMU0_STRIDE(offset)					\
  { GLfloat *tc0 = VEC_ELT(tc0_vec, GLfloat, e);	\
    COPY_2V((out+offset), tc0); }


#define DO_SETUP_TMU0(offset)				\
  v->tmuvtx[0].sow = data[offset+0]*sScale0*v->oow;	\
  v->tmuvtx[0].tow = data[offset+1]*tScale0*v->oow;

#define VARS_TMU1							\
  struct gl_texture_unit *t1 = &ctx->Texture.Unit[tmu1_source];		\
  GLfloat sScale1 = fxTMGetTexInfo(t1->Current)->sScale;	\
  GLfloat tScale1 = fxTMGetTexInfo(t1->Current)->tScale;

#define COPY_TMU1_STRIDE(offset)					\
  { GLfloat *tc1 = VEC_ELT(tc1_vec, GLfloat, e);	\
    COPY_2V((out+offset), tc1); }


#define DO_SETUP_TMU1(offset)				\
  v->tmuvtx[1].sow = data[offset+0]*sScale1*v->oow;	\
  v->tmuvtx[1].tow = data[offset+1]*tScale1*v->oow;

#define COPY_NIL(offset) ASSIGN_2V((out+offset), 0, 0);

#define IND 0
#define TAG(x) x##_nil
#include "fxcliptmp.h"

#define IND SETUP_RGBA
#define TAG(x) x##_RGBA
#include "fxcliptmp.h"

#define IND SETUP_TMU0
#define TAG(x) x##_TMU0
#include "fxcliptmp.h"

#define IND (SETUP_TMU0|SETUP_TMU1)
#define TAG(x) x##_TMU0_TMU1
#include "fxcliptmp.h"

#define IND (SETUP_RGBA|SETUP_TMU0)
#define TAG(x) x##_RGBA_TMU0
#include "fxcliptmp.h"

#define IND (SETUP_RGBA|SETUP_TMU0|SETUP_TMU1)
#define TAG(x) x##_RGBA_TMU0_TMU1
#include "fxcliptmp.h"

tfxTriViewClipFunc fxTriViewClipTab[0x8];
tfxTriClipFunc fxTriClipStrideTab[0x8];
tfxLineClipFunc fxLineClipTab[0x8];


void fxDDClipInit()
{
   fxTriViewClipTab[0] = fx_tri_view_clip_nil;
   fxTriViewClipTab[SETUP_RGBA] = fx_tri_view_clip_RGBA;
   fxTriViewClipTab[SETUP_TMU0] = fx_tri_view_clip_TMU0;
   fxTriViewClipTab[SETUP_TMU0|SETUP_TMU1] = fx_tri_view_clip_TMU0_TMU1;
   fxTriViewClipTab[SETUP_RGBA|SETUP_TMU0] = fx_tri_view_clip_RGBA_TMU0;
   fxTriViewClipTab[SETUP_RGBA|SETUP_TMU0|SETUP_TMU1] = fx_tri_view_clip_RGBA_TMU0_TMU1;

   fxTriClipStrideTab[0] = fx_tri_clip_stride_nil;
   fxTriClipStrideTab[SETUP_RGBA] = fx_tri_clip_stride_RGBA;
   fxTriClipStrideTab[SETUP_TMU0] = fx_tri_clip_stride_TMU0;
   fxTriClipStrideTab[SETUP_TMU0|SETUP_TMU1] = fx_tri_clip_stride_TMU0_TMU1;
   fxTriClipStrideTab[SETUP_RGBA|SETUP_TMU0] = fx_tri_clip_stride_RGBA_TMU0;
   fxTriClipStrideTab[SETUP_RGBA|SETUP_TMU0|SETUP_TMU1] = fx_tri_clip_stride_RGBA_TMU0_TMU1;

   fxLineClipTab[0] = fx_line_clip_nil;
   fxLineClipTab[SETUP_RGBA] = fx_line_clip_RGBA;
   fxLineClipTab[SETUP_TMU0] = fx_line_clip_TMU0;
   fxLineClipTab[SETUP_TMU0|SETUP_TMU1] = fx_line_clip_TMU0_TMU1;
   fxLineClipTab[SETUP_RGBA|SETUP_TMU0] = fx_line_clip_RGBA_TMU0;
   fxLineClipTab[SETUP_RGBA|SETUP_TMU0|SETUP_TMU1] = fx_line_clip_RGBA_TMU0_TMU1;
}

#else

/*
 * Need this to provide at least one external definition.
 */
int gl_fxclip_dummy(void)
{
  return 0;
}

#endif