summaryrefslogtreecommitdiffstats
path: root/src/mesa/softpipe/generic/g_prim_setup.c
blob: ce93c0a6f9680a638dcb30bde9dc3181d1e4718d (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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/**************************************************************************
 * 
 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
 * 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, sub license, 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 (including the
 * next paragraph) 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 NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS 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 <keith@tungstengraphics.com>
 */

#include "imports.h"
#include "macros.h"

#include "g_context.h"
#include "g_prim.h"
#include "g_tile.h"


struct edge {
   GLfloat dx;			/* X(v1) - X(v0), used only during setup */
   GLfloat dy;			/* Y(v1) - Y(v0), used only during setup */
   GLfloat dxdy;		/* dx/dy */
   GLfloat sx;			/* first sample point x coord */
   GLfloat sy;
   GLint lines;			/* number of lines  on this edge */
};


struct setup_stage {
   struct prim_stage stage;
   
   /* Vertices are just an array of floats making up each attribute in
    * turn.  Currently fixed at 4 floats, but should change in time.
    * Codegen will help cope with this.
    */
   const struct vertex_header *vmax;
   const struct vertex_header *vmid;
   const struct vertex_header *vmin;
   const struct vertex_header *vprovoke;
   
   struct edge ebot;
   struct edge etop;
   struct edge emaj;

   GLfloat oneoverarea;

   struct setup_coefficient coef[FRAG_ATTRIB_MAX];
   struct quad_header quad; 

   struct {
      GLint left[2];   /**< [0] = row0, [1] = row1 */
      GLint right[2];
      GLint y;
      GLuint y_flags;
      GLuint mask;     /**< mask of MASK_BOTTOM/TOP_LEFT/RIGHT bits */
   } span;

};



/**
 * Basically a cast wrapper.
 */
static inline struct setup_stage *setup_stage( struct prim_stage *stage )
{
   return (struct setup_stage *)stage;
}


/**
 * Given an X or Y coordinate, return the block/quad coordinate that it
 * belongs to.
 */
static inline GLint block( GLint x )
{
   return x & ~1;
}



static void setup_begin( struct prim_stage *stage )
{
   setup_stage(stage)->quad.nr_attrs = stage->generic->nr_frag_attrs;
}


/**
 * Run shader on a quad/block.
 */
static void run_shader_block( struct setup_stage *setup, 
			      GLint x, GLint y, GLuint mask )
{
   setup->quad.x0 = x;
   setup->quad.y0 = y;
   setup->quad.mask = mask;

   quad_shade( setup->stage.generic, &setup->quad );
}


/**
 * Compute mask which indicates which pixels in the 2x2 quad are actually inside
 * the triangle's bounds.
 *
 * this is pretty nasty...  may need to rework flush_spans again to
 * fix it, if possible.
 */
static GLuint calculate_mask( struct setup_stage *setup,
			    GLint x )
{
   GLuint mask = 0;

   if (x >= setup->span.left[0] && x < setup->span.right[0]) 
      mask |= MASK_BOTTOM_LEFT;

   if (x >= setup->span.left[1] && x < setup->span.right[1]) 
      mask |= MASK_TOP_LEFT;
      
   if (x+1 >= setup->span.left[0] && x+1 < setup->span.right[0]) 
      mask |= MASK_BOTTOM_RIGHT;

   if (x+1 >= setup->span.left[1] && x+1 < setup->span.right[1]) 
      mask |= MASK_TOP_RIGHT;

   return mask;
}


/**
 * Render a horizontal span of quads
 */
static void flush_spans( struct setup_stage *setup )
{
   GLint minleft, maxright;
   GLint x;

   switch (setup->span.y_flags) {      
   case 3:
      minleft = MIN2(setup->span.left[0], setup->span.left[1]);
      maxright = MAX2(setup->span.right[0], setup->span.right[1]);
      break;

   case 1:
      minleft = setup->span.left[0];
      maxright = setup->span.right[0];
      break;

   case 2:
      minleft = setup->span.left[1];
      maxright = setup->span.right[1];
      break;

   default:
      return;
   }


   for (x = block(minleft); x <= block(maxright); )
   {
      run_shader_block( setup, x,
			setup->span.y, 
			calculate_mask( setup, x ) );
      x += 2;
   }

   setup->span.y = 0;
   setup->span.y_flags = 0;
   setup->span.right[0] = 0;
   setup->span.right[1] = 0;
}

     
/**
 * Do setup for point rasterization, then render the point.
 */
static void
setup_point( struct prim_stage *stage, 
	     struct prim_header *header )
{
}


/**
 * Do setup for line rasterization, then render the line.
 */
static void
setup_line( struct prim_stage *stage,
	    struct prim_header *header )
{
}


static GLboolean setup_sort_vertices( struct setup_stage *setup,
				      const struct prim_header *prim )
{
   
   const struct vertex_header *v0 = prim->v[0];
   const struct vertex_header *v1 = prim->v[1];
   const struct vertex_header *v2 = prim->v[2];

   setup->vprovoke = v2;
//   setup->oneoverarea = -1.0 / prim->det;

   /* determine bottom to top order of vertices */
   {
      GLfloat y0 = v0->data[0][1];
      GLfloat y1 = v1->data[0][1];
      GLfloat y2 = v2->data[0][1];
      if (y0 <= y1) {
	 if (y1 <= y2) {
	    /* y0<=y1<=y2 */
	    setup->vmin = v0;   
	    setup->vmid = v1;   
	    setup->vmax = v2;
	 }
	 else if (y2 <= y0) {
	    /* y2<=y0<=y1 */
	    setup->vmin = v2;   
	    setup->vmid = v0;   
	    setup->vmax = v1;   
	 }
	 else {
	    /* y0<=y2<=y1 */
	    setup->vmin = v0;   
	    setup->vmid = v2;   
	    setup->vmax = v1;  
//	    setup->oneoverarea = -setup->oneoverarea;
	 }
      }
      else {
	 if (y0 <= y2) {
	    /* y1<=y0<=y2 */
	    setup->vmin = v1;   
	    setup->vmid = v0;   
	    setup->vmax = v2;  
//	    setup->oneoverarea = -setup->oneoverarea;
	 }
	 else if (y2 <= y1) {
	    /* y2<=y1<=y0 */
	    setup->vmin = v2;   
	    setup->vmid = v1;   
	    setup->vmax = v0;  
//	    setup->oneoverarea = -setup->oneoverarea;
	 }
	 else {
	    /* y1<=y2<=y0 */
	    setup->vmin = v1;   
	    setup->vmid = v2;   
	    setup->vmax = v0;
	 }
      }
   }

   setup->ebot.dx = setup->vmid->data[0][0] - setup->vmin->data[0][0];
   setup->ebot.dy = setup->vmid->data[0][1] - setup->vmin->data[0][1];
   setup->emaj.dx = setup->vmax->data[0][0] - setup->vmin->data[0][0];
   setup->emaj.dy = setup->vmax->data[0][1] - setup->vmin->data[0][1];
   setup->etop.dx = setup->vmax->data[0][0] - setup->vmid->data[0][0];
   setup->etop.dy = setup->vmax->data[0][1] - setup->vmid->data[0][1];

   /* xxx: may need to adjust this sign according to the if-tree
    * above:
    *
    * XXX: this is like 'det', but calculated from screen coords??
    */
   {
      const GLfloat area = (setup->emaj.dx * setup->ebot.dy - 
			    setup->ebot.dx * setup->emaj.dy);

      setup->oneoverarea = 1.0 / area;
   }

   /* XXX need to know if this is a front or back-facing triangle:
    *  - the GLSL gl_FrontFacing fragment attribute (bool)
    *  - two-sided stencil test
    */
   setup->quad.facing = 0;

   _mesa_printf("%s one-over-area %f\n", __FUNCTION__, setup->oneoverarea );


   return GL_TRUE;
}


/**
 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
 */
static void const_coeff( struct setup_stage *setup,
			 GLuint slot,
			 GLuint i )
{
   setup->coef[slot].dadx[i] = 0;
   setup->coef[slot].dady[i] = 0;

   /* need provoking vertex info!
    */
   setup->coef[slot].a0[i] = setup->vprovoke->data[slot][i];
}


/**
 * Compute a0, dadx and dady for a linearly interpolated coefficient.
 */
static void linear_coeff( struct setup_stage *setup,
			  GLuint slot,
			  GLuint i)
{
   GLfloat botda = setup->vmid->data[slot][i] - setup->vmin->data[slot][i];
   GLfloat majda = setup->vmax->data[slot][i] - setup->vmin->data[slot][i];
   GLfloat a = setup->ebot.dy * majda - botda * setup->emaj.dy;
   GLfloat b = setup->emaj.dx * botda - majda * setup->ebot.dx;
   
   setup->coef[slot].dadx[i] = a * setup->oneoverarea;
   setup->coef[slot].dady[i] = b * setup->oneoverarea;

   /* calculate a0 as the value which would be sampled for the
    * fragment at (0,0), taking into account that we want to sample at
    * pixel centers, in other words (0.5, 0.5).
    *
    * this is neat but unfortunately not a good way to do things for
    * triangles with very large values of dadx or dady as it will
    * result in the subtraction and re-addition from a0 of a very
    * large number, which means we'll end up loosing a lot of the
    * fractional bits and precision from a0.  the way to fix this is
    * to define a0 as the sample at a pixel center somewhere near vmin
    * instead - i'll switch to this later.
    */
   setup->coef[slot].a0[i] = (setup->vmin->data[slot][i] - 
			    (setup->coef[slot].dadx[i] * (setup->vmin->data[0][0] - 0.5) + 
			     setup->coef[slot].dady[i] * (setup->vmin->data[0][1] - 0.5)));

   _mesa_printf("attr[%d].%c: %f dx:%f dy:%f\n",
		slot, "xyzw"[i], 
		setup->coef[slot].a0[i],
		setup->coef[slot].dadx[i],
		setup->coef[slot].dady[i]);
}


/**
 * Compute a0, dadx and dady for a perspective-corrected interpolant.
 */
static void persp_coeff( struct setup_stage *setup,
			 GLuint slot,
			 GLuint i )
{
   /* premultiply by 1/w:
    */
   GLfloat mina = setup->vmin->data[slot][i] * setup->vmin->data[0][3];
   GLfloat mida = setup->vmid->data[slot][i] * setup->vmid->data[0][3];
   GLfloat maxa = setup->vmax->data[slot][i] * setup->vmax->data[0][3];

   GLfloat botda = mida - mina;
   GLfloat majda = maxa - mina;
   GLfloat a = setup->ebot.dy * majda - botda * setup->emaj.dy;
   GLfloat b = setup->emaj.dx * botda - majda * setup->ebot.dx;
      
   setup->coef[slot].dadx[i] = a * setup->oneoverarea;
   setup->coef[slot].dady[i] = b * setup->oneoverarea;
   setup->coef[slot].a0[i] = (mina - 
			    (setup->coef[slot].dadx[i] * (setup->vmin->data[0][0] - 0.5) + 
			     setup->coef[slot].dady[i] * (setup->vmin->data[0][1] - 0.5)));
}




static void setup_coefficients( struct setup_stage *setup )
{
   const enum interp_mode *interp = setup->stage.generic->interp;
   GLuint slot, j;

   /* z and w are done by linear interpolation:
    */
   linear_coeff(setup, 0, 2);
   linear_coeff(setup, 0, 3);

   /* setup interpolation for all the remaining attributes:
    */
   for (slot = 1; slot < setup->quad.nr_attrs; slot++) {
      switch (interp[slot]) {
      case INTERP_CONSTANT:
	 for (j = 0; j < NUM_CHANNELS; j++)
	    const_coeff(setup, slot, j);
	 break;
      
      case INTERP_LINEAR:
	 for (j = 0; j < NUM_CHANNELS; j++)
	    linear_coeff(setup, slot, j);
	 break;

      case INTERP_PERSPECTIVE:
	 for (j = 0; j < NUM_CHANNELS; j++)
	    persp_coeff(setup, slot, j);
	 break;
      }
   }
}



static void setup_edges( struct setup_stage *setup )
{
   GLfloat vmin_x = setup->vmin->data[0][0] + 0.5;
   GLfloat vmid_x = setup->vmid->data[0][0] + 0.5;

   GLfloat vmin_y = setup->vmin->data[0][1] - 0.5;
   GLfloat vmid_y = setup->vmid->data[0][1] - 0.5;
   GLfloat vmax_y = setup->vmax->data[0][1] - 0.5;

   setup->emaj.sy = ceilf(vmin_y);
   setup->emaj.lines = (GLint) ceilf(vmax_y - setup->emaj.sy);
   setup->emaj.dxdy = setup->emaj.dx / setup->emaj.dy;
   setup->emaj.sx = vmin_x + (setup->emaj.sy - vmin_y) * setup->emaj.dxdy;

   setup->etop.sy = ceilf(vmid_y);
   setup->etop.lines = (GLint) ceilf(vmax_y - setup->etop.sy);
   setup->etop.dxdy = setup->etop.dx / setup->etop.dy;
   setup->etop.sx = vmid_x + (setup->etop.sy - vmid_y) * setup->etop.dxdy;

   setup->ebot.sy = ceilf(vmin_y);
   setup->ebot.lines = (GLint) ceilf(vmid_y - setup->ebot.sy);
   setup->ebot.dxdy = setup->ebot.dx / setup->ebot.dy;
   setup->ebot.sx = vmin_x + (setup->ebot.sy - vmin_y) * setup->ebot.dxdy;
}


/**
 * Render the upper or lower half of a triangle.
 * Scissoring is applied here too.
 */
static void subtriangle( struct setup_stage *setup,
			 struct edge *eleft,
			 struct edge *eright,
			 GLuint lines )
{
   GLint y, start_y, finish_y;
   GLint sy = (GLint)eleft->sy;

   assert((GLint)eleft->sy == (GLint) eright->sy);
   assert((GLint)eleft->sy >= 0);	/* catch bug in x64? */

   /* scissor y:
    */
   if (setup->stage.generic->setup.scissor) {
      start_y = sy;
      finish_y = start_y + lines;

      if (start_y < setup->stage.generic->scissor.miny) 
	 start_y = setup->stage.generic->scissor.miny;

      if (finish_y > setup->stage.generic->scissor.maxy) 
	 finish_y = setup->stage.generic->scissor.maxy;

      start_y -= sy;
      finish_y -= sy;
   }
   else {
      start_y = 0;
      finish_y = lines;
   }

   _mesa_printf("%s %d %d\n", __FUNCTION__, start_y, finish_y);  

   for (y = start_y; y < finish_y; y++) {

      /* avoid accumulating adds as floats don't have the precision to
       * accurately iterate large triangle edges that way.  luckily we
       * can just multiply these days.
       *
       * this is all drowned out by the attribute interpolation anyway.
       */
      GLint left = (GLint)(eleft->sx + y * eleft->dxdy);
      GLint right = (GLint)(eright->sx + y * eright->dxdy);

      /* scissor x: 
       */
      if (setup->stage.generic->setup.scissor) {
	 if (left  < setup->stage.generic->scissor.minx) 
	    left  = setup->stage.generic->scissor.minx;

	 if (right > setup->stage.generic->scissor.maxx) 
	    right = setup->stage.generic->scissor.maxx;
      }

      if (left < right) {
	 GLint _y = sy+y;
	 if (block(_y) != setup->span.y) {
	    flush_spans(setup);
	    setup->span.y = block(_y);
	 }
   
	 setup->span.left[_y&1] = left;
	 setup->span.right[_y&1] = right;
	 setup->span.y_flags |= 1<<(_y&1);
      }
   } 


   /* save the values so that emaj can be restarted:
    */
   eleft->sx += lines * eleft->dxdy;
   eright->sx += lines * eright->dxdy;
   eleft->sy += lines;
   eright->sy += lines;
}


/**
 * Do setup for triangle rasterization, then render the triangle.
 */
static void setup_tri( struct prim_stage *stage,
		       struct prim_header *prim )
{
   struct setup_stage *setup = setup_stage( stage );

   _mesa_printf("%s\n", __FUNCTION__ );

   setup_sort_vertices( setup, prim );
   setup_coefficients( setup );
   setup_edges( setup );

   setup->span.y = 0;
   setup->span.y_flags = 0;
   setup->span.right[0] = 0;
   setup->span.right[1] = 0;
//   setup->span.z_mode = tri_z_mode( setup->ctx );

//   init_constant_attribs( setup );
      
   if (setup->oneoverarea < 0.0) {
      /* emaj on left:
       */
      subtriangle( setup, &setup->emaj, &setup->ebot, setup->ebot.lines );
      subtriangle( setup, &setup->emaj, &setup->etop, setup->etop.lines );
   }
   else {
      /* emaj on right:
       */
      subtriangle( setup, &setup->ebot, &setup->emaj, setup->ebot.lines );
      subtriangle( setup, &setup->etop, &setup->emaj, setup->etop.lines );
   }

   flush_spans( setup );
}

static void setup_end( struct prim_stage *stage )
{
}


struct prim_stage *prim_setup( struct generic_context *generic )
{
   struct setup_stage *setup = CALLOC_STRUCT(setup_stage);

   setup->stage.generic = generic;
   setup->stage.begin = setup_begin;
   setup->stage.point = setup_point;
   setup->stage.line = setup_line;
   setup->stage.tri = setup_tri;
   setup->stage.end = setup_end;

   setup->quad.coef = setup->coef;

   return &setup->stage;
}