lp_setup_line.c 23 KB
Newer Older
1
2
/**************************************************************************
 *
Jose Fonseca's avatar
Jose Fonseca committed
3
 * Copyright 2007 VMware, Inc.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 * 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.
Jose Fonseca's avatar
Jose Fonseca committed
21
 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22
23
24
25
26
27
28
29
30
31
 * 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.
 *
 **************************************************************************/

/*
 * Binning code for lines
 */

32
33
34
#include "util/u_math.h"
#include "util/u_memory.h"
#include "lp_perf.h"
35
#include "lp_setup_context.h"
36
37
#include "lp_rast.h"
#include "lp_state_fs.h"
38
#include "lp_state_setup.h"
39
#include "lp_context.h"
40
#include "draw/draw_context.h"
41

42
43
#define NUM_CHANNELS 4

44
45
46
47
48
struct lp_line_info {

   float dx;
   float dy;
   float oneoverarea;
49
   boolean frontfacing;
50
51
52

   const float (*v1)[4];
   const float (*v2)[4];
53
54
55
56

   float (*a0)[4];
   float (*dadx)[4];
   float (*dady)[4];
57
};
58
59
60
61
62
63


/**
 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
 */
static void constant_coef( struct lp_setup_context *setup,
64
                           struct lp_line_info *info,
65
66
67
68
                           unsigned slot,
                           const float value,
                           unsigned i )
{
69
70
71
   info->a0[slot][i] = value;
   info->dadx[slot][i] = 0.0f;
   info->dady[slot][i] = 0.0f;
72
73
74
75
76
77
78
79
}


/**
 * Compute a0, dadx and dady for a linearly interpolated coefficient,
 * for a triangle.
 */
static void linear_coef( struct lp_setup_context *setup,
80
                         struct lp_line_info *info,
81
82
83
84
                         unsigned slot,
                         unsigned vert_attr,
                         unsigned i)
{
85
86
   float a1 = info->v1[vert_attr][i]; 
   float a2 = info->v2[vert_attr][i];
87
88
      
   float da21 = a1 - a2;   
89
90
   float dadx = da21 * info->dx * info->oneoverarea;
   float dady = da21 * info->dy * info->oneoverarea;
91

92
93
   info->dadx[slot][i] = dadx;
   info->dady[slot][i] = dady;  
94
   
95
   info->a0[slot][i] = (a1 -
96
97
                              (dadx * (info->v1[0][0] - setup->pixel_offset) +
                               dady * (info->v1[0][1] - setup->pixel_offset)));
98
99
100
101
102
103
104
105
106
107
108
109
}


/**
 * Compute a0, dadx and dady for a perspective-corrected interpolant,
 * for a triangle.
 * We basically multiply the vertex value by 1/w before computing
 * the plane coefficients (a0, dadx, dady).
 * Later, when we compute the value at a particular fragment position we'll
 * divide the interpolated value by the interpolated W at that fragment.
 */
static void perspective_coef( struct lp_setup_context *setup,
110
                              struct lp_line_info *info,
111
112
113
114
115
116
                              unsigned slot,
                              unsigned vert_attr,
                              unsigned i)
{
   /* premultiply by 1/w  (v[0][3] is always 1/w):
    */
117
118
   float a1 = info->v1[vert_attr][i] * info->v1[0][3];
   float a2 = info->v2[vert_attr][i] * info->v2[0][3];
119
120

   float da21 = a1 - a2;   
121
122
   float dadx = da21 * info->dx * info->oneoverarea;
   float dady = da21 * info->dy * info->oneoverarea;
123

124
125
   info->dadx[slot][i] = dadx;
   info->dady[slot][i] = dady;
126
   
127
128
129
   info->a0[slot][i] = (a1 -
                        (dadx * (info->v1[0][0] - setup->pixel_offset) +
                         dady * (info->v1[0][1] - setup->pixel_offset)));
130
131
}

132
133
static void
setup_fragcoord_coef( struct lp_setup_context *setup,
134
                      struct lp_line_info *info,
135
136
137
138
139
                      unsigned slot,
                      unsigned usage_mask)
{
   /*X*/
   if (usage_mask & TGSI_WRITEMASK_X) {
140
141
142
      info->a0[slot][0] = 0.0;
      info->dadx[slot][0] = 1.0;
      info->dady[slot][0] = 0.0;
143
144
145
146
   }

   /*Y*/
   if (usage_mask & TGSI_WRITEMASK_Y) {
147
148
149
      info->a0[slot][1] = 0.0;
      info->dadx[slot][1] = 0.0;
      info->dady[slot][1] = 1.0;
150
151
152
153
   }

   /*Z*/
   if (usage_mask & TGSI_WRITEMASK_Z) {
154
      linear_coef(setup, info, slot, 0, 2);
155
156
157
158
   }

   /*W*/
   if (usage_mask & TGSI_WRITEMASK_W) {
159
      linear_coef(setup, info, slot, 0, 3);
160
161
162
   }
}

163
164
165
166
/**
 * Compute the tri->coef[] array dadx, dady, a0 values.
 */
static void setup_line_coefficients( struct lp_setup_context *setup,
167
                                     struct lp_line_info *info)
168
{
169
   const struct lp_setup_variant_key *key = &setup->setup.variant->key;
170
171
172
173
174
   unsigned fragcoord_usage_mask = TGSI_WRITEMASK_XYZ;
   unsigned slot;

   /* setup interpolation for all the remaining attributes:
    */
175
176
177
   for (slot = 0; slot < key->num_inputs; slot++) {
      unsigned vert_attr = key->inputs[slot].src_index;
      unsigned usage_mask = key->inputs[slot].usage_mask;
178
179
      unsigned i;
           
180
      switch (key->inputs[slot].interp) {
181
      case LP_INTERP_CONSTANT:
182
         if (key->flatshade_first) {
183
184
            for (i = 0; i < NUM_CHANNELS; i++)
               if (usage_mask & (1 << i))
185
                  constant_coef(setup, info, slot+1, info->v1[vert_attr][i], i);
186
187
188
189
         }
         else {
            for (i = 0; i < NUM_CHANNELS; i++)
               if (usage_mask & (1 << i))
190
                  constant_coef(setup, info, slot+1, info->v2[vert_attr][i], i);
191
192
193
194
195
196
         }
         break;

      case LP_INTERP_LINEAR:
         for (i = 0; i < NUM_CHANNELS; i++)
            if (usage_mask & (1 << i))
197
               linear_coef(setup, info, slot+1, vert_attr, i);
198
199
200
201
202
         break;

      case LP_INTERP_PERSPECTIVE:
         for (i = 0; i < NUM_CHANNELS; i++)
            if (usage_mask & (1 << i))
203
               perspective_coef(setup, info, slot+1, vert_attr, i);
204
205
206
207
208
209
210
211
212
213
214
215
         fragcoord_usage_mask |= TGSI_WRITEMASK_W;
         break;

      case LP_INTERP_POSITION:
         /*
          * The generated pixel interpolators will pick up the coeffs from
          * slot 0, so all need to ensure that the usage mask is covers all
          * usages.
          */
         fragcoord_usage_mask |= usage_mask;
         break;

216
217
218
      case LP_INTERP_FACING:
         for (i = 0; i < NUM_CHANNELS; i++)
            if (usage_mask & (1 << i))
219
220
               constant_coef(setup, info, slot+1,
                             info->frontfacing ? 1.0f : -1.0f, i);
221
222
         break;

223
224
225
226
227
228
229
      default:
         assert(0);
      }
   }

   /* The internal position input is in slot zero:
    */
230
   setup_fragcoord_coef(setup, info, 0,
231
                        fragcoord_usage_mask);
232
233
234
}


235

236
static inline int subpixel_snap( float a )
237
{
238
239
240
241
242
243
244
245
246
247
248
249
   return util_iround(FIXED_ONE * a);
}


/**
 * Print line vertex attribs (for debug).
 */
static void
print_line(struct lp_setup_context *setup,
           const float (*v1)[4],
           const float (*v2)[4])
{
250
   const struct lp_setup_variant_key *key = &setup->setup.variant->key;
251
252
253
   uint i;

   debug_printf("llvmpipe line\n");
254
   for (i = 0; i < 1 + key->num_inputs; i++) {
255
256
257
      debug_printf("  v1[%d]:  %f %f %f %f\n", i,
                   v1[i][0], v1[i][1], v1[i][2], v1[i][3]);
   }
258
   for (i = 0; i < 1 + key->num_inputs; i++) {
259
260
261
262
263
264
      debug_printf("  v2[%d]:  %f %f %f %f\n", i,
                   v2[i][0], v2[i][1], v2[i][2], v2[i][3]);
   }
}


265
static inline boolean sign(float x){
266
267
268
269
   return x >= 0;  
}  


270
271
/* Used on positive floats only:
 */
272
static inline float fracf(float f)
273
274
275
276
277
278
{
   return f - floorf(f);
}



279
280
static boolean
try_setup_line( struct lp_setup_context *setup,
281
282
283
               const float (*v1)[4],
               const float (*v2)[4])
{
284
   struct llvmpipe_context *lp_context = (struct llvmpipe_context *)setup->pipe;
285
   struct lp_scene *scene = setup->scene;
286
   const struct lp_setup_variant_key *key = &setup->setup.variant->key;
287
   struct lp_rast_triangle *line;
288
   struct lp_rast_plane *plane;
289
   struct lp_line_info info;
290
   float width = MAX2(1.0, setup->line_width);
291
292
293
   const struct u_rect *scissor;
   struct u_rect bbox, bboxpos;
   boolean s_planes[4];
294
295
296
297
298
   unsigned tri_bytes;
   int x[4]; 
   int y[4];
   int i;
   int nr_planes = 4;
299
   unsigned viewport_index = 0;
300
   unsigned layer = 0;
301
302
   
   /* linewidth should be interpreted as integer */
303
   int fixed_width = util_iround(width) * FIXED_ONE;
304

305
306
307
308
   float x_offset=0;
   float y_offset=0;
   float x_offset_end=0;
   float y_offset_end=0;
309
310
311
312
313
      
   float x1diff;
   float y1diff;
   float x2diff;
   float y2diff;
314
   float dx, dy;
315
   float area;
316
   const float (*pv)[4];
317
318
319
320
321
322

   boolean draw_start;
   boolean draw_end;
   boolean will_draw_start;
   boolean will_draw_end;

323
324
325
   if (0)
      print_line(setup, v1, v2);

326
327
   if (setup->flatshade_first) {
      pv = v1;
328
329
   }
   else {
330
331
332
333
334
      pv = v2;
   }
   if (setup->viewport_index_slot > 0) {
      unsigned *udata = (unsigned*)pv[setup->viewport_index_slot];
      viewport_index = lp_clamp_viewport_idx(*udata);
335
   }
336
   if (setup->layer_slot > 0) {
337
      layer = *(unsigned*)pv[setup->layer_slot];
338
339
      layer = MIN2(layer, scene->fb_max_layer);
   }
340

341
342
   dx = v1[0][0] - v2[0][0];
   dy = v1[0][1] - v2[0][1];
343
344
345
346
347
348
349
350
351
352
353
354
   area = (dx * dx  + dy * dy);
   if (area == 0) {
      LP_COUNT(nr_culled_tris);
      return TRUE;
   }

   info.oneoverarea = 1.0f / area;
   info.dx = dx;
   info.dy = dy;
   info.v1 = v1;
   info.v2 = v2;

355
  
356
357
358
   /* X-MAJOR LINE */
   if (fabsf(dx) >= fabsf(dy)) {
      float dydx = dy / dx;
359
360
361
362
363
364

      x1diff = v1[0][0] - (float) floor(v1[0][0]) - 0.5;
      y1diff = v1[0][1] - (float) floor(v1[0][1]) - 0.5;
      x2diff = v2[0][0] - (float) floor(v2[0][0]) - 0.5;
      y2diff = v2[0][1] - (float) floor(v2[0][1]) - 0.5;

365
      if (y2diff==-0.5 && dy<0){
366
367
368
369
370
371
372
373
374
         y2diff = 0.5;
      }
      
      /* 
       * Diamond exit rule test for starting point 
       */    
      if (fabsf(x1diff) + fabsf(y1diff) < 0.5) {
         draw_start = TRUE;
      }
375
      else if (sign(x1diff) == sign(-dx)) {
376
377
         draw_start = FALSE;
      }
378
      else if (sign(-y1diff) != sign(dy)) {
379
380
381
382
         draw_start = TRUE;
      }
      else {
         /* do intersection test */
383
384
         float yintersect = fracf(v1[0][1]) + x1diff * dydx;
         draw_start = (yintersect < 1.0 && yintersect > 0.0);
385
386
387
388
389
390
391
392
393
      }


      /* 
       * Diamond exit rule test for ending point 
       */    
      if (fabsf(x2diff) + fabsf(y2diff) < 0.5) {
         draw_end = FALSE;
      }
394
      else if (sign(x2diff) != sign(-dx)) {
395
396
         draw_end = FALSE;
      }
397
      else if (sign(-y2diff) == sign(dy)) {
398
399
400
401
         draw_end = TRUE;
      }
      else {
         /* do intersection test */
402
403
         float yintersect = fracf(v2[0][1]) + x2diff * dydx;
         draw_end = (yintersect < 1.0 && yintersect > 0.0);
404
405
406
407
      }

      /* Are we already drawing start/end?
       */
408
409
      will_draw_start = sign(-x1diff) != sign(dx);
      will_draw_end = (sign(x2diff) == sign(-dx)) || x2diff==0;
410

411
      if (dx < 0) {
412
413
414
415
         /* if v2 is to the right of v1, swap pointers */
         const float (*temp)[4] = v1;
         v1 = v2;
         v2 = temp;
416
417
         dx = -dx;
         dy = -dy;
418
419
         /* Otherwise shift planes appropriately */
         if (will_draw_start != draw_start) {
420
421
            x_offset_end = - x1diff - 0.5;
            y_offset_end = x_offset_end * dydx;
422
423
424

         }
         if (will_draw_end != draw_end) {
425
426
            x_offset = - x2diff - 0.5;
            y_offset = x_offset * dydx;
427
428
         }

429
      }
430
431
432
      else{
         /* Otherwise shift planes appropriately */
         if (will_draw_start != draw_start) {
433
434
            x_offset = - x1diff + 0.5;
            y_offset = x_offset * dydx;
435
436
         }
         if (will_draw_end != draw_end) {
437
438
            x_offset_end = - x2diff + 0.5;
            y_offset_end = x_offset_end * dydx;
439
440
441
         }
      }
  
442
      /* x/y positions in fixed point */
443
444
445
446
      x[0] = subpixel_snap(v1[0][0] + x_offset     - setup->pixel_offset);
      x[1] = subpixel_snap(v2[0][0] + x_offset_end - setup->pixel_offset);
      x[2] = subpixel_snap(v2[0][0] + x_offset_end - setup->pixel_offset);
      x[3] = subpixel_snap(v1[0][0] + x_offset     - setup->pixel_offset);
447
      
448
449
450
451
      y[0] = subpixel_snap(v1[0][1] + y_offset     - setup->pixel_offset) - fixed_width/2;
      y[1] = subpixel_snap(v2[0][1] + y_offset_end - setup->pixel_offset) - fixed_width/2;
      y[2] = subpixel_snap(v2[0][1] + y_offset_end - setup->pixel_offset) + fixed_width/2;
      y[3] = subpixel_snap(v1[0][1] + y_offset     - setup->pixel_offset) + fixed_width/2;
452
453
      
   }
454
455
   else {
      const float dxdy = dx / dy;
456

457
      /* Y-MAJOR LINE */      
458
459
460
461
462
      x1diff = v1[0][0] - (float) floor(v1[0][0]) - 0.5;
      y1diff = v1[0][1] - (float) floor(v1[0][1]) - 0.5;
      x2diff = v2[0][0] - (float) floor(v2[0][0]) - 0.5;
      y2diff = v2[0][1] - (float) floor(v2[0][1]) - 0.5;

463
      if (x2diff==-0.5 && dx<0) {
464
465
466
         x2diff = 0.5;
      }

467
468
469
      /* 
       * Diamond exit rule test for starting point 
       */    
470
471
472
      if (fabsf(x1diff) + fabsf(y1diff) < 0.5) {
         draw_start = TRUE;
      }
473
      else if (sign(-y1diff) == sign(dy)) {
474
475
         draw_start = FALSE;
      }
476
      else if (sign(x1diff) != sign(-dx)) {
477
478
479
480
         draw_start = TRUE;
      }
      else {
         /* do intersection test */
481
482
         float xintersect = fracf(v1[0][0]) + y1diff * dxdy;
         draw_start = (xintersect < 1.0 && xintersect > 0.0);
483
484
485
486
487
488
489
490
      }

      /* 
       * Diamond exit rule test for ending point 
       */    
      if (fabsf(x2diff) + fabsf(y2diff) < 0.5) {
         draw_end = FALSE;
      }
491
      else if (sign(-y2diff) != sign(dy) ) {
492
493
         draw_end = FALSE;
      }
494
      else if (sign(x2diff) == sign(-dx) ) {
495
496
497
498
         draw_end = TRUE;
      }
      else {
         /* do intersection test */
499
         float xintersect = fracf(v2[0][0]) + y2diff * dxdy;
500
         draw_end = (xintersect < 1.0 && xintersect >= 0.0);
501
502
503
504
      }

      /* Are we already drawing start/end?
       */
505
506
      will_draw_start = sign(y1diff) == sign(dy);
      will_draw_end = (sign(-y2diff) == sign(dy)) || y2diff==0;
507

508
      if (dy > 0) {
509
510
511
512
         /* if v2 is on top of v1, swap pointers */
         const float (*temp)[4] = v1;
         v1 = v2;
         v2 = temp; 
513
514
         dx = -dx;
         dy = -dy;
515
516
517

         /* Otherwise shift planes appropriately */
         if (will_draw_start != draw_start) {
518
519
            y_offset_end = - y1diff + 0.5;
            x_offset_end = y_offset_end * dxdy;
520
521
         }
         if (will_draw_end != draw_end) {
522
523
            y_offset = - y2diff + 0.5;
            x_offset = y_offset * dxdy;
524
525
         }
      }
526
      else {
527
528
         /* Otherwise shift planes appropriately */
         if (will_draw_start != draw_start) {
529
530
            y_offset = - y1diff - 0.5;
            x_offset = y_offset * dxdy;
531
532
533
                     
         }
         if (will_draw_end != draw_end) {
534
535
            y_offset_end = - y2diff - 0.5;
            x_offset_end = y_offset_end * dxdy;
536
         }
537
      }
538

539
      /* x/y positions in fixed point */
540
541
542
543
      x[0] = subpixel_snap(v1[0][0] + x_offset     - setup->pixel_offset) - fixed_width/2;
      x[1] = subpixel_snap(v2[0][0] + x_offset_end - setup->pixel_offset) - fixed_width/2;
      x[2] = subpixel_snap(v2[0][0] + x_offset_end - setup->pixel_offset) + fixed_width/2;
      x[3] = subpixel_snap(v1[0][0] + x_offset     - setup->pixel_offset) + fixed_width/2;
544
     
545
546
547
548
      y[0] = subpixel_snap(v1[0][1] + y_offset     - setup->pixel_offset); 
      y[1] = subpixel_snap(v2[0][1] + y_offset_end - setup->pixel_offset);
      y[2] = subpixel_snap(v2[0][1] + y_offset_end - setup->pixel_offset);
      y[3] = subpixel_snap(v1[0][1] + y_offset     - setup->pixel_offset);
549
550
551
552
553
554
555
556
557
   }

   /* Bounding rectangle (in pixels) */
   {
      /* Yes this is necessary to accurately calculate bounding boxes
       * with the two fill-conventions we support.  GL (normally) ends
       * up needing a bottom-left fill convention, which requires
       * slightly different rounding.
       */
558
      int adj = (setup->bottom_edge_rule != 0) ? 1 : 0;
559

560
561
562
563
564
565
566
567
568
      bbox.x0 = (MIN4(x[0], x[1], x[2], x[3]) + (FIXED_ONE-1)) >> FIXED_ORDER;
      bbox.x1 = (MAX4(x[0], x[1], x[2], x[3]) + (FIXED_ONE-1)) >> FIXED_ORDER;
      bbox.y0 = (MIN4(y[0], y[1], y[2], y[3]) + (FIXED_ONE-1) + adj) >> FIXED_ORDER;
      bbox.y1 = (MAX4(y[0], y[1], y[2], y[3]) + (FIXED_ONE-1) + adj) >> FIXED_ORDER;

      /* Inclusive coordinates:
       */
      bbox.x1--;
      bbox.y1--;
569
570
   }

571
572
573
574
   if (bbox.x1 < bbox.x0 ||
       bbox.y1 < bbox.y0) {
      if (0) debug_printf("empty bounding box\n");
      LP_COUNT(nr_culled_tris);
575
      return TRUE;
576
   }
577

578
   if (!u_rect_test_intersection(&setup->draw_regions[viewport_index], &bbox)) {
579
580
      if (0) debug_printf("offscreen\n");
      LP_COUNT(nr_culled_tris);
581
      return TRUE;
582
583
   }

584
585
   bboxpos = bbox;

586
587
   /* Can safely discard negative regions:
    */
588
589
   bboxpos.x0 = MAX2(bboxpos.x0, 0);
   bboxpos.y0 = MAX2(bboxpos.y0, 0);
590

591
592
593
594
595
596
597
   nr_planes = 4;
   /*
    * Determine how many scissor planes we need, that is drop scissor
    * edges if the bounding box of the tri is fully inside that edge.
    */
   if (setup->scissor_test) {
      /* why not just use draw_regions */
598
599
      scissor = &setup->scissors[viewport_index];
      scissor_planes_needed(s_planes, &bboxpos, scissor);
600
      nr_planes += s_planes[0] + s_planes[1] + s_planes[2] + s_planes[3];
601
602
   }

603
   line = lp_setup_alloc_triangle(scene,
604
                                  key->num_inputs,
605
606
607
                                  nr_planes,
                                  &tri_bytes);
   if (!line)
608
      return FALSE;
609

610
611
612
613
614
615
616
#ifdef DEBUG
   line->v[0][0] = v1[0][0];
   line->v[1][0] = v2[0][0];   
   line->v[0][1] = v1[0][1];
   line->v[1][1] = v2[0][1];
#endif

617
618
   LP_COUNT(nr_tris);

619
   if (lp_context->active_statistics_queries) {
620
621
622
      lp_context->pipeline_statistics.c_primitives++;
   }

623
   /* calculate the deltas */
624
625
626
627
628
   plane = GET_PLANES(line);
   plane[0].dcdy = x[0] - x[1];
   plane[1].dcdy = x[1] - x[2];
   plane[2].dcdy = x[2] - x[3];
   plane[3].dcdy = x[3] - x[0];
629

630
631
632
633
   plane[0].dcdx = y[0] - y[1];
   plane[1].dcdx = y[1] - y[2];
   plane[2].dcdx = y[2] - y[3];
   plane[3].dcdx = y[3] - y[0];
634

635
636
637
638
639
640
   if (draw_will_inject_frontface(lp_context->draw) &&
       setup->face_slot > 0) {
      line->inputs.frontfacing = v1[setup->face_slot][0];
   } else {
      line->inputs.frontfacing = TRUE;
   }
641

642
643
   /* Setup parameter interpolants:
    */
644
645
646
   info.a0 = GET_A0(&line->inputs);
   info.dadx = GET_DADX(&line->inputs);
   info.dady = GET_DADY(&line->inputs);
647
   info.frontfacing = line->inputs.frontfacing;
648
   setup_line_coefficients(setup, &info); 
649

650
651
   line->inputs.disable = FALSE;
   line->inputs.opaque = FALSE;
652
   line->inputs.layer = layer;
653
   line->inputs.viewport_index = viewport_index;
654

655
656
657
658
659
660
661
   /*
    * XXX: this code is mostly identical to the one in lp_setup_tri, except it
    * uses 4 planes instead of 3. Could share the code (including the sse
    * assembly, in fact we'd get the 4th plane for free).
    * The only difference apart from storing the 4th plane would be some
    * different shuffle for calculating dcdx/dcdy.
    */
662
663
   for (i = 0; i < 4; i++) {

664
      /* half-edge constants, will be iterated over the whole render
665
666
       * target.
       */
667
      plane[i].c = IMUL64(plane[i].dcdx, x[i]) - IMUL64(plane[i].dcdy, y[i]);
668

669
670
      /* correct for top-left vs. bottom-left fill convention.
       */
671
      if (plane[i].dcdx < 0) {
672
         /* both fill conventions want this - adjust for left edges */
673
         plane[i].c++;
674
      }
675
      else if (plane[i].dcdx == 0) {
676
677
678
         if (setup->pixel_offset == 0) {
            /* correct for top-left fill convention:
             */
679
            if (plane[i].dcdy > 0) plane[i].c++;
680
681
682
683
         }
         else {
            /* correct for bottom-left fill convention:
             */
684
            if (plane[i].dcdy < 0) plane[i].c++;
685
686
687
         }
      }

688
689
      plane[i].dcdx *= FIXED_ONE;
      plane[i].dcdy *= FIXED_ONE;
690
691
692
693
694
695

      /* find trivial reject offsets for each edge for a single-pixel
       * sized block.  These will be scaled up at each recursive level to
       * match the active blocksize.  Scaling in this way works best if
       * the blocks are square.
       */
696
697
698
      plane[i].eo = 0;
      if (plane[i].dcdx < 0) plane[i].eo -= plane[i].dcdx;
      if (plane[i].dcdy > 0) plane[i].eo += plane[i].dcdy;
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
   }


   /* 
    * When rasterizing scissored tris, use the intersection of the
    * triangle bounding box and the scissor rect to generate the
    * scissor planes.
    *
    * This permits us to cut off the triangle "tails" that are present
    * in the intermediate recursive levels caused when two of the
    * triangles edges don't diverge quickly enough to trivially reject
    * exterior blocks from the triangle.
    *
    * It's not really clear if it's worth worrying about these tails,
    * but since we generate the planes for each scissored tri, it's
    * free to trim them in this case.
    * 
    * Note that otherwise, the scissor planes only vary in 'C' value,
    * and even then only on state-changes.  Could alternatively store
    * these planes elsewhere.
719
720
721
    * (Or only store the c value together with a bit indicating which
    * scissor edge this is, so rasterization would treat them differently
    * (easier to evaluate) to ordinary planes.)
722
    */
723
724
725
   if (nr_planes > 4) {
      struct lp_rast_plane *plane_s = &plane[4];

726
      if (s_planes[0]) {
727
         plane_s->dcdx = ~0U << 8;
728
729
730
731
732
         plane_s->dcdy = 0;
         plane_s->c = (1-scissor->x0) << 8;
         plane_s->eo = 1 << 8;
         plane_s++;
      }
733
      if (s_planes[1]) {
734
735
736
737
738
739
         plane_s->dcdx = 1 << 8;
         plane_s->dcdy = 0;
         plane_s->c = (scissor->x1+1) << 8;
         plane_s->eo = 0 << 8;
         plane_s++;
      }
740
      if (s_planes[2]) {
741
742
743
744
745
746
         plane_s->dcdx = 0;
         plane_s->dcdy = 1 << 8;
         plane_s->c = (1-scissor->y0) << 8;
         plane_s->eo = 1 << 8;
         plane_s++;
      }
747
      if (s_planes[3]) {
748
         plane_s->dcdx = 0;
749
         plane_s->dcdy = ~0U << 8;
750
751
752
753
754
         plane_s->c = (scissor->y1+1) << 8;
         plane_s->eo = 0;
         plane_s++;
      }
      assert(plane_s == &plane[nr_planes]);
755
756
   }

757
   return lp_setup_bin_triangle(setup, line, &bbox, &bboxpos, nr_planes, viewport_index);
758
}
759
760


761
762
763
static void lp_setup_line_discard(struct lp_setup_context *setup,
                                  const float (*v0)[4],
                                  const float (*v1)[4])
764
{
765
766
767
768
769
770
771
}

static void lp_setup_line(struct lp_setup_context *setup,
                          const float (*v0)[4],
                          const float (*v1)[4])
{
   if (!try_setup_line(setup, v0, v1)) {
772
773
      if (!lp_setup_flush_and_restart(setup))
         return;
774

775
      if (!try_setup_line(setup, v0, v1))
776
         return;
777
778
779
   }
}

780

781
void lp_setup_choose_line(struct lp_setup_context *setup)
782
{ 
783
784
785
786
787
   if (setup->rasterizer_discard) {
      setup->line = lp_setup_line_discard;
   } else {
      setup->line = lp_setup_line;
   }
788
789
790
}