summaryrefslogtreecommitdiff
path: root/xc/lib/GL/mesa/src/drv/mga/mgaeltpath.c
blob: 3dcfa5eed65faffe1e989e5d32a704bb3590d01c (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
/*
 * GLX Hardware Device Driver for Matrox G400
 * Copyright (C) 1999 Keith Whitwell
 *
 * 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
 * KEITH WHITWELL, OR ANY OTHER CONTRIBUTORS 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.
 *
 */
/* $XFree86: xc/lib/GL/mesa/src/drv/mga/mgaeltpath.c,v 1.3 2000/08/28 02:43:12 tsi Exp $ */

#include <stdio.h>

#include "types.h"
#include "enums.h"
#include "cva.h"
#include "vertices.h"
#include "mmath.h"
#include "xform.h"

#include "mgacontext.h"
#include "mgapipeline.h"
#include "mgatris.h"
#include "mgastate.h"
#include "mgavb.h"


/* Always use a full-sized stride for vertices. [FIXME]
 * Stride in the buffers must be a quadword multiple.
 */
#define BUFFER_STRIDE 12 
#define CLIP_STRIDE 10 


static void fire_elts( mgaContextPtr mmesa )
{
   LOCK_HARDWARE( mmesa );

   /* Fire queued elements and discard that buffer if its contents
    * won't be referenced by future elements.
    */
   if (mmesa->elt_buf) {
      GLuint retain = (mmesa->elt_buf == mmesa->retained_buf);

      if (mmesa->first_elt != mmesa->next_elt) {
	 mgaFireEltsLocked( mmesa, 
			    ((GLuint)mmesa->first_elt - 
			     (GLuint)mmesa->elt_buf->address),
			    ((GLuint)mmesa->next_elt - 
			     (GLuint)mmesa->elt_buf->address),
			    !retain );
      } else if (!retain) 
	 mgaReleaseBufLocked( mmesa, mmesa->elt_buf );
      
      mmesa->elt_buf = 0;
   } 
   else if (mmesa->vertex_dma_buffer) 
   {
      mgaFlushVerticesLocked( mmesa );
   }

   mgaGetEltBufLocked( mmesa );
      
   UNLOCK_HARDWARE( mmesa );

   mmesa->next_vert = (GLfloat *)((GLuint)mmesa->elt_buf->address + 
				  mmesa->elt_buf->total - 
				  BUFFER_STRIDE * sizeof(GLfloat));

   mmesa->next_vert_phys = (mmesa->mgaScreen->dmaOffset + 
			    mmesa->elt_buf->idx * MGA_DMA_BUF_SZ +
			    mmesa->elt_buf->total -
			    BUFFER_STRIDE * sizeof(GLfloat));

   mmesa->first_elt = (GLuint *)mmesa->elt_buf->address;
   mmesa->next_elt = (GLuint *)mmesa->elt_buf->address;

}


static void release_bufs( mgaContextPtr mmesa )
{
   if (mmesa->retained_buf && mmesa->retained_buf != mmesa->elt_buf) 
   {
      LOCK_HARDWARE( mmesa );
      if (mmesa->first_elt != mmesa->next_elt) {
	 mgaFireEltsLocked( mmesa, 
			    ((GLuint)mmesa->first_elt - 
			     (GLuint)mmesa->elt_buf->address),
			    ((GLuint)mmesa->next_elt - 
			     (GLuint)mmesa->elt_buf->address),
			    0 );

	 mmesa->first_elt = mmesa->next_elt;
      }

      mgaReleaseBufLocked( mmesa, mmesa->retained_buf );
      UNLOCK_HARDWARE( mmesa );
   }

   mmesa->retained_buf = 0;
}




#define NEGATIVE(f)          (f < 0)
#define DIFFERENT_SIGNS(a,b) ((a*b) < 0)
#define LINTERP( T, A, B )   ( (A) + (T) * ( (B) - (A) ) )


#define INTERP_RGBA(t, out, a, b) {			\
   int i;						\
   for (i = 0; i < 4; i++) {				\
      GLfloat fa = UBYTE_COLOR_TO_FLOAT_COLOR(a[i]);	\
      GLfloat fb = UBYTE_COLOR_TO_FLOAT_COLOR(b[i]);	\
      GLfloat fo = LINTERP(t, fa, fb);			\
      FLOAT_COLOR_TO_UBYTE_COLOR(out[i], fo);		\
   }							\
}


#define CLIP(SGN,V,PLANE)			\
if (mask & PLANE) {				\
   GLuint *indata = inlist[in];			\
   GLuint *outdata = inlist[in ^= 1];		\
   GLuint nr = n;				\
   GLfloat *J = verts[indata[nr-1]];		\
   GLfloat dpJ = (SGN J[V]) + J[3];		\
						\
   for (i = n = 0 ; i < nr ; i++) {		\
      GLuint elt_i = indata[i];			\
      GLfloat *I = verts[elt_i];		\
      GLfloat dpI = (SGN I[V]) + I[3];		\
						\
      if (DIFFERENT_SIGNS(dpI, dpJ)) {		\
	 GLfloat *O = verts[next_vert];	\
	 outdata[n++] = next_vert++;		\
						\
	 if (NEGATIVE(dpI)) {			\
	    GLfloat t = dpI / (dpI - dpJ);	\
	    interp(t, O, I, J);			\
	 }					\
         else					\
	 {					\
	    GLfloat t = dpJ / (dpJ - dpI);	\
	    interp(t, O, J, I);			\
	 }					\
      }						\
						\
      if (!NEGATIVE(dpI))			\
	 outdata[n++] = elt_i;			\
						\
      J = I;					\
      dpJ = dpI;				\
   }						\
						\
   if (n < 3) return;				\
}


static void mga_tri_clip( mgaContextPtr mmesa,
			  struct vertex_buffer *VB,
			  GLuint *elt,
			  GLubyte mask )
{
   struct mga_elt_tab *tab = mmesa->elt_tab;
   mga_interp_func interp = tab->interp;
   GLuint inlist[2][VB_MAX_CLIPPED_VERTS];
   GLuint in = 0;
   GLuint n = 3, next_vert = 3;
   GLuint i;
   GLfloat verts[VB_MAX_CLIPPED_VERTS][CLIP_STRIDE];

   /* Build temporary vertices in clipspace.  This is the potential
    * downside to this path.
    */
   tab->build_tri_verts( mmesa, VB, (GLfloat *)verts, elt );

   inlist[0][0] = 0;
   inlist[0][1] = 1;
   inlist[0][2] = 2;

   CLIP(-,0,CLIP_RIGHT_BIT);
   CLIP(+,0,CLIP_LEFT_BIT);
   CLIP(-,1,CLIP_TOP_BIT);
   CLIP(+,1,CLIP_BOTTOM_BIT);
   CLIP(-,2,CLIP_FAR_BIT);
   CLIP(+,2,CLIP_NEAR_BIT); 


   {
      GLuint *out = inlist[in];
      GLuint space = (GLuint)mmesa->next_vert -	(GLuint)mmesa->next_elt;

      if (space < n * (BUFFER_STRIDE + 3) * sizeof(GLuint))
         fire_elts(mmesa); 

      /* Project the new vertices and emit to dma buffers.  Translate
       * out values to physical addresses for setup dma.  
       */
      tab->project_and_emit_verts( mmesa, (GLfloat *)verts, out, n );
      
      /* Convert the planar polygon to a list of triangles and emit to
       * elt buffers. 
       */
      for (i = 2 ; i < n ; i++) {
	 mmesa->next_elt[0] = out[0];
	 mmesa->next_elt[1] = out[i-1];
	 mmesa->next_elt[2] = out[i];
	 mmesa->next_elt += 3;
      }
   }
}




/* Build a table of functions to clip each primitive type.  These
 * produce a list of elements in the appropriate 'reduced' primitive,
 * ie (points, lines, triangles) containing all the clipped and
 * unclipped primitives from the original list.
 */

#define INIT(x)

#define TRI_THRESHOLD         (3 * sizeof(GLuint))

#define UNCLIPPED_VERT(x) (mmesa->first_vert_phys - x * BUFFER_STRIDE * 4)

#define TRIANGLE( e2, e1, e0 )			\
do {						\
   if ((GLuint)mmesa->next_vert - 		\
       (GLuint)mmesa->next_elt < TRI_THRESHOLD)	\
      fire_elts(mmesa);			\
   mmesa->next_elt[0] = UNCLIPPED_VERT(e2);	\
   mmesa->next_elt[1] = UNCLIPPED_VERT(e1);	\
   mmesa->next_elt[2] = UNCLIPPED_VERT(e0);	\
   mmesa->next_elt+=3;				\
} while (0)

#define CLIP_TRIANGLE( e2, e1, e0 )				\
do {								\
   GLubyte ormask = mask[e2] | mask[e1] | mask[e0];		\
   if (ormask == 0) {						\
      TRIANGLE( e2, e1, e0 );					\
   } else if ((mask[e2] & mask[e1] & mask[e0]) == 0) {		\
      out[0] = e2;						\
      out[1] = e1;						\
      out[2] = e0;						\
      mga_tri_clip( mmesa, VB, out, ormask );			\
   }								\
} while (0)

#define LOCAL_VARS						\
   mgaContextPtr mmesa = MGA_CONTEXT( VB->ctx );		\
   GLuint *elt = VB->EltPtr->data;				\
   GLuint out[VB_MAX_CLIPPED_VERTS];				\
   GLubyte *mask = VB->ClipMask; \
   (void) mask; (void) out; (void) elt; (void) mmesa;



#define RENDER_POINTS(start, count) 
#define RENDER_LINE(i1, i0) 
#define RENDER_TRI(i2, i1, i0, pv, parity)		\
do {							\
   GLuint e2 = elt[i2], e1 = elt[i1], e0 = elt[i0];	\
   if (parity) e2 = elt[i1], e1 = elt[i2];		\
   CLIP_TRIANGLE( e2, e1, e0 );				\
} while (0)

#define RENDER_QUAD(i3, i2, i1, i0, pv )		\
  CLIP_TRIANGLE(elt[i3], elt[i2], elt[i0]);     	\
  CLIP_TRIANGLE(elt[i2], elt[i1], elt[i0])

#define TAG(x) mga_##x##_elt
#include "render_tmp.h"



#define LOCAL_VARS						\
   mgaContextPtr mmesa = MGA_CONTEXT( VB->ctx );		\
   GLuint *elt = VB->EltPtr->data;				\
   (void) elt; (void) mmesa;

#define RENDER_POINTS(start, count) 
#define RENDER_LINE(i1, i0) 
#define RENDER_TRI(i2, i1, i0, pv, parity)		\
do {							\
   GLuint e2 = elt[i2], e1 = elt[i1], e0 = elt[i0];	\
   if (parity) e2 = elt[i1], e1 = elt[i2];		\
   TRIANGLE( e2, e1, e0 );				\
} while (0)

#define RENDER_QUAD(i3, i2, i1, i0, pv )	\
  TRIANGLE(elt[i3], elt[i2], elt[i0]);		\
  TRIANGLE(elt[i2], elt[i1], elt[i0])

#define TAG(x) mga_##x##_elt_unclipped
#include "render_tmp.h"




static void refresh_projection_matrix( GLcontext *ctx )
{
   mgaContextPtr mmesa = MGA_CONTEXT(ctx);
   GLfloat *m = mmesa->device_matrix;
   GLmatrix *mat = &ctx->Viewport.WindowMap;

   REFRESH_DRAWABLE_INFO(mmesa);

   m[MAT_SX] =   mat->m[MAT_SX];
   m[MAT_TX] =   mat->m[MAT_TX] + mmesa->drawX + .5;
   m[MAT_SY] = (- mat->m[MAT_SY]);
   m[MAT_TY] = (- mat->m[MAT_TY]) + mmesa->driDrawable->h + mmesa->drawY - .5;
   m[MAT_SZ] =   mat->m[MAT_SZ] * mmesa->depth_scale;
   m[MAT_TZ] =   mat->m[MAT_TZ] * mmesa->depth_scale;
}

#define CLIP_UBYTE_B 0   
#define CLIP_UBYTE_G 1  
#define CLIP_UBYTE_R 2
#define CLIP_UBYTE_A 3   


#define TYPE (0)
#define TAG(x) x
#include "mgaelttmp.h"

#define TYPE (MGA_RGBA_BIT)
#define TAG(x) x##_RGBA
#include "mgaelttmp.h"

#define TYPE (MGA_TEX0_BIT)
#define TAG(x) x##_TEX0
#include "mgaelttmp.h"

#define TYPE (MGA_RGBA_BIT|MGA_TEX0_BIT)
#define TAG(x) x##_RGBA_TEX0
#include "mgaelttmp.h"

#define TYPE (MGA_RGBA_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
#define TAG(x) x##_RGBA_TEX0_TEX1
#include "mgaelttmp.h"

#define TYPE (MGA_TEX0_BIT|MGA_TEX1_BIT)
#define TAG(x) x##_TEX0_TEX1
#include "mgaelttmp.h"


/* Very sparsely popluated array - fix the indices.
 */
static struct mga_elt_tab mgaEltTab[0x80];

void mgaDDEltPathInit( void )
{
   mga_render_init_elt();
   mga_render_init_elt_unclipped();

   mga_init_eltpath( &mgaEltTab[0] );
   mga_init_eltpath_RGBA( &mgaEltTab[MGA_RGBA_BIT] );
   mga_init_eltpath_TEX0( &mgaEltTab[MGA_TEX0_BIT] );
   mga_init_eltpath_RGBA_TEX0( &mgaEltTab[MGA_RGBA_BIT|MGA_TEX0_BIT] );
   mga_init_eltpath_TEX0_TEX1( &mgaEltTab[MGA_TEX0_BIT|MGA_TEX1_BIT] );
   mga_init_eltpath_RGBA_TEX0_TEX1( &mgaEltTab[MGA_RGBA_BIT|MGA_TEX0_BIT|
						  MGA_TEX1_BIT] );
}

#define VALID_SETUP (MGA_RGBA_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)



/* Use a temporary array for device coordinates, so that we can easily
 * tap into existing mesa assembly.  Otherwise consider emitting
 * device coordinates to dma buffers directly from the project/cliptest
 * routine.  (requires output stride, potential loss of writecombining 
 * efficiency?)  
 *
 * This path is a lot closer to the standard vertex path in the
 * initial stages than the original fastpath.  A slightly more optimal
 * path could be constructed, but would require us to write new
 * assembly.  
 */
void mgaDDEltPath( struct vertex_buffer *VB )
{
   GLcontext *ctx = VB->ctx;
   GLenum prim = ctx->CVA.elt_mode;
   mgaContextPtr mmesa = MGA_CONTEXT( ctx ); 
   struct mga_elt_tab *tab = &mgaEltTab[mmesa->setupindex & VALID_SETUP];

   VB->ClipPtr = TransformRaw(&VB->Clip, &ctx->ModelProjectMatrix, VB->ObjPtr );

   refresh_projection_matrix( ctx );

   VB->ClipAndMask = ~0; 
   VB->ClipOrMask = 0;
   VB->Projected = gl_clip_tab[VB->ClipPtr->size]( VB->ClipPtr,
						   &VB->Win,
						   VB->ClipMask,
						   &VB->ClipOrMask,
						   &VB->ClipAndMask );

   if (VB->ClipAndMask) 
      return;

   if (mmesa->vertex_dma_buffer) 
      mgaFlushVertices( mmesa );

   if (mmesa->new_state)
      mgaDDUpdateHwState( ctx );

   /* Allocate a single buffer to hold unclipped vertices.  All
    * unclipped vertices must be contiguous.  
    */
   if ((GLuint)mmesa->next_vert - (GLuint)mmesa->next_elt <  
       VB->Count * BUFFER_STRIDE * sizeof(GLuint))	
      fire_elts( mmesa );

   mmesa->retained_buf = mmesa->elt_buf;
   
   /* Emit unclipped vertices to the buffer.
    */
   tab->emit_unclipped_verts( VB ); 
	 
   /* Emit indices and clipped vertices to one or more buffers.
    */
   if (VB->ClipOrMask) {
      mmesa->elt_tab = tab;
      mga_render_tab_elt[prim]( VB, 0, VB->EltPtr->count, 0 ); 
   } else
      mga_render_tab_elt_unclipped[prim]( VB, 0, VB->EltPtr->count, 0 );

   /* Send to hardware and release any retained buffers.
    */
   release_bufs( mmesa );

   /* This indicates that there is no cached data to reuse.  
    */
   VB->pipeline->data_valid = 0;
   VB->pipeline->new_state = 0;
}