29
29
* Binning code for lines
32
#include "util/u_math.h"
33
#include "util/u_memory.h"
32
35
#include "lp_setup_context.h"
34
static void line_nop( struct setup_context *setup,
36
const float (*v1)[4] )
42
lp_setup_choose_line( struct setup_context *setup )
44
setup->line = line_nop;
37
#include "lp_state_fs.h"
39
#define NUM_CHANNELS 4
53
* Compute a0 for a constant-valued coefficient (GL_FLAT shading).
55
static void constant_coef( struct lp_setup_context *setup,
56
struct lp_rast_triangle *tri,
61
tri->inputs.a0[slot][i] = value;
62
tri->inputs.dadx[slot][i] = 0.0f;
63
tri->inputs.dady[slot][i] = 0.0f;
68
* Compute a0, dadx and dady for a linearly interpolated coefficient,
71
static void linear_coef( struct lp_setup_context *setup,
72
struct lp_rast_triangle *tri,
73
struct lp_line_info *info,
78
float a1 = info->v1[vert_attr][i];
79
float a2 = info->v2[vert_attr][i];
82
float dadx = da21 * info->dx * info->oneoverarea;
83
float dady = da21 * info->dy * info->oneoverarea;
85
tri->inputs.dadx[slot][i] = dadx;
86
tri->inputs.dady[slot][i] = dady;
88
tri->inputs.a0[slot][i] = (a1 -
89
(dadx * (info->v1[0][0] - setup->pixel_offset) +
90
dady * (info->v1[0][1] - setup->pixel_offset)));
95
* Compute a0, dadx and dady for a perspective-corrected interpolant,
97
* We basically multiply the vertex value by 1/w before computing
98
* the plane coefficients (a0, dadx, dady).
99
* Later, when we compute the value at a particular fragment position we'll
100
* divide the interpolated value by the interpolated W at that fragment.
102
static void perspective_coef( struct lp_setup_context *setup,
103
struct lp_rast_triangle *tri,
104
struct lp_line_info *info,
109
/* premultiply by 1/w (v[0][3] is always 1/w):
111
float a1 = info->v1[vert_attr][i] * info->v1[0][3];
112
float a2 = info->v2[vert_attr][i] * info->v2[0][3];
114
float da21 = a1 - a2;
115
float dadx = da21 * info->dx * info->oneoverarea;
116
float dady = da21 * info->dy * info->oneoverarea;
118
tri->inputs.dadx[slot][i] = dadx;
119
tri->inputs.dady[slot][i] = dady;
121
tri->inputs.a0[slot][i] = (a1 -
122
(dadx * (info->v1[0][0] - setup->pixel_offset) +
123
dady * (info->v1[0][1] - setup->pixel_offset)));
127
setup_fragcoord_coef( struct lp_setup_context *setup,
128
struct lp_rast_triangle *tri,
129
struct lp_line_info *info,
134
if (usage_mask & TGSI_WRITEMASK_X) {
135
tri->inputs.a0[slot][0] = 0.0;
136
tri->inputs.dadx[slot][0] = 1.0;
137
tri->inputs.dady[slot][0] = 0.0;
141
if (usage_mask & TGSI_WRITEMASK_Y) {
142
tri->inputs.a0[slot][1] = 0.0;
143
tri->inputs.dadx[slot][1] = 0.0;
144
tri->inputs.dady[slot][1] = 1.0;
148
if (usage_mask & TGSI_WRITEMASK_Z) {
149
linear_coef(setup, tri, info, slot, 0, 2);
153
if (usage_mask & TGSI_WRITEMASK_W) {
154
linear_coef(setup, tri, info, slot, 0, 3);
159
* Compute the tri->coef[] array dadx, dady, a0 values.
161
static void setup_line_coefficients( struct lp_setup_context *setup,
162
struct lp_rast_triangle *tri,
163
struct lp_line_info *info)
165
unsigned fragcoord_usage_mask = TGSI_WRITEMASK_XYZ;
168
/* setup interpolation for all the remaining attributes:
170
for (slot = 0; slot < setup->fs.nr_inputs; slot++) {
171
unsigned vert_attr = setup->fs.input[slot].src_index;
172
unsigned usage_mask = setup->fs.input[slot].usage_mask;
175
switch (setup->fs.input[slot].interp) {
176
case LP_INTERP_CONSTANT:
177
if (setup->flatshade_first) {
178
for (i = 0; i < NUM_CHANNELS; i++)
179
if (usage_mask & (1 << i))
180
constant_coef(setup, tri, slot+1, info->v1[vert_attr][i], i);
183
for (i = 0; i < NUM_CHANNELS; i++)
184
if (usage_mask & (1 << i))
185
constant_coef(setup, tri, slot+1, info->v2[vert_attr][i], i);
189
case LP_INTERP_LINEAR:
190
for (i = 0; i < NUM_CHANNELS; i++)
191
if (usage_mask & (1 << i))
192
linear_coef(setup, tri, info, slot+1, vert_attr, i);
195
case LP_INTERP_PERSPECTIVE:
196
for (i = 0; i < NUM_CHANNELS; i++)
197
if (usage_mask & (1 << i))
198
perspective_coef(setup, tri, info, slot+1, vert_attr, i);
199
fragcoord_usage_mask |= TGSI_WRITEMASK_W;
202
case LP_INTERP_POSITION:
204
* The generated pixel interpolators will pick up the coeffs from
205
* slot 0, so all need to ensure that the usage mask is covers all
208
fragcoord_usage_mask |= usage_mask;
216
/* The internal position input is in slot zero:
218
setup_fragcoord_coef(setup, tri, info, 0,
219
fragcoord_usage_mask);
224
static INLINE int subpixel_snap( float a )
226
return util_iround(FIXED_ONE * a);
231
* Print line vertex attribs (for debug).
234
print_line(struct lp_setup_context *setup,
235
const float (*v1)[4],
236
const float (*v2)[4])
240
debug_printf("llvmpipe line\n");
241
for (i = 0; i < 1 + setup->fs.nr_inputs; i++) {
242
debug_printf(" v1[%d]: %f %f %f %f\n", i,
243
v1[i][0], v1[i][1], v1[i][2], v1[i][3]);
245
for (i = 0; i < 1 + setup->fs.nr_inputs; i++) {
246
debug_printf(" v2[%d]: %f %f %f %f\n", i,
247
v2[i][0], v2[i][1], v2[i][2], v2[i][3]);
252
static INLINE boolean sign(float x){
257
/* Used on positive floats only:
259
static INLINE float fracf(float f)
261
return f - floorf(f);
267
try_setup_line( struct lp_setup_context *setup,
268
const float (*v1)[4],
269
const float (*v2)[4])
271
struct lp_scene *scene = setup->scene;
272
struct lp_rast_triangle *line;
273
struct lp_line_info info;
274
float width = MAX2(1.0, setup->line_width);
282
/* linewidth should be interpreted as integer */
283
int fixed_width = util_iround(width) * FIXED_ONE;
287
float x_offset_end=0;
288
float y_offset_end=0;
298
boolean will_draw_start;
299
boolean will_draw_end;
302
print_line(setup, v1, v2);
304
if (setup->scissor_test) {
312
dx = v1[0][0] - v2[0][0];
313
dy = v1[0][1] - v2[0][1];
316
if (fabsf(dx) >= fabsf(dy)) {
317
float dydx = dy / dx;
319
x1diff = v1[0][0] - (float) floor(v1[0][0]) - 0.5;
320
y1diff = v1[0][1] - (float) floor(v1[0][1]) - 0.5;
321
x2diff = v2[0][0] - (float) floor(v2[0][0]) - 0.5;
322
y2diff = v2[0][1] - (float) floor(v2[0][1]) - 0.5;
324
if (y2diff==-0.5 && dy<0){
329
* Diamond exit rule test for starting point
331
if (fabsf(x1diff) + fabsf(y1diff) < 0.5) {
334
else if (sign(x1diff) == sign(-dx)) {
337
else if (sign(-y1diff) != sign(dy)) {
341
/* do intersection test */
342
float yintersect = fracf(v1[0][1]) + x1diff * dydx;
343
draw_start = (yintersect < 1.0 && yintersect > 0.0);
348
* Diamond exit rule test for ending point
350
if (fabsf(x2diff) + fabsf(y2diff) < 0.5) {
353
else if (sign(x2diff) != sign(-dx)) {
356
else if (sign(-y2diff) == sign(dy)) {
360
/* do intersection test */
361
float yintersect = fracf(v2[0][1]) + x2diff * dydx;
362
draw_end = (yintersect < 1.0 && yintersect > 0.0);
365
/* Are we already drawing start/end?
367
will_draw_start = sign(-x1diff) != sign(dx);
368
will_draw_end = (sign(x2diff) == sign(-dx)) || x2diff==0;
371
/* if v2 is to the right of v1, swap pointers */
372
const float (*temp)[4] = v1;
377
/* Otherwise shift planes appropriately */
378
if (will_draw_start != draw_start) {
379
x_offset_end = - x1diff - 0.5;
380
y_offset_end = x_offset_end * dydx;
383
if (will_draw_end != draw_end) {
384
x_offset = - x2diff - 0.5;
385
y_offset = x_offset * dydx;
390
/* Otherwise shift planes appropriately */
391
if (will_draw_start != draw_start) {
392
x_offset = - x1diff + 0.5;
393
y_offset = x_offset * dydx;
395
if (will_draw_end != draw_end) {
396
x_offset_end = - x2diff + 0.5;
397
y_offset_end = x_offset_end * dydx;
401
/* x/y positions in fixed point */
402
x[0] = subpixel_snap(v1[0][0] + x_offset - setup->pixel_offset);
403
x[1] = subpixel_snap(v2[0][0] + x_offset_end - setup->pixel_offset);
404
x[2] = subpixel_snap(v2[0][0] + x_offset_end - setup->pixel_offset);
405
x[3] = subpixel_snap(v1[0][0] + x_offset - setup->pixel_offset);
407
y[0] = subpixel_snap(v1[0][1] + y_offset - setup->pixel_offset) - fixed_width/2;
408
y[1] = subpixel_snap(v2[0][1] + y_offset_end - setup->pixel_offset) - fixed_width/2;
409
y[2] = subpixel_snap(v2[0][1] + y_offset_end - setup->pixel_offset) + fixed_width/2;
410
y[3] = subpixel_snap(v1[0][1] + y_offset - setup->pixel_offset) + fixed_width/2;
414
const float dxdy = dx / dy;
417
x1diff = v1[0][0] - (float) floor(v1[0][0]) - 0.5;
418
y1diff = v1[0][1] - (float) floor(v1[0][1]) - 0.5;
419
x2diff = v2[0][0] - (float) floor(v2[0][0]) - 0.5;
420
y2diff = v2[0][1] - (float) floor(v2[0][1]) - 0.5;
422
if (x2diff==-0.5 && dx<0) {
427
* Diamond exit rule test for starting point
429
if (fabsf(x1diff) + fabsf(y1diff) < 0.5) {
432
else if (sign(-y1diff) == sign(dy)) {
435
else if (sign(x1diff) != sign(-dx)) {
439
/* do intersection test */
440
float xintersect = fracf(v1[0][0]) + y1diff * dxdy;
441
draw_start = (xintersect < 1.0 && xintersect > 0.0);
445
* Diamond exit rule test for ending point
447
if (fabsf(x2diff) + fabsf(y2diff) < 0.5) {
450
else if (sign(-y2diff) != sign(dy) ) {
453
else if (sign(x2diff) == sign(-dx) ) {
457
/* do intersection test */
458
float xintersect = fracf(v2[0][0]) + y2diff * dxdy;
459
draw_end = (xintersect < 1.0 && xintersect > 0.0);
462
/* Are we already drawing start/end?
464
will_draw_start = sign(y1diff) == sign(dy);
465
will_draw_end = (sign(-y2diff) == sign(dy)) || y2diff==0;
468
/* if v2 is on top of v1, swap pointers */
469
const float (*temp)[4] = v1;
475
/* Otherwise shift planes appropriately */
476
if (will_draw_start != draw_start) {
477
y_offset_end = - y1diff + 0.5;
478
x_offset_end = y_offset_end * dxdy;
480
if (will_draw_end != draw_end) {
481
y_offset = - y2diff + 0.5;
482
x_offset = y_offset * dxdy;
486
/* Otherwise shift planes appropriately */
487
if (will_draw_start != draw_start) {
488
y_offset = - y1diff - 0.5;
489
x_offset = y_offset * dxdy;
492
if (will_draw_end != draw_end) {
493
y_offset_end = - y2diff - 0.5;
494
x_offset_end = y_offset_end * dxdy;
498
/* x/y positions in fixed point */
499
x[0] = subpixel_snap(v1[0][0] + x_offset - setup->pixel_offset) - fixed_width/2;
500
x[1] = subpixel_snap(v2[0][0] + x_offset_end - setup->pixel_offset) - fixed_width/2;
501
x[2] = subpixel_snap(v2[0][0] + x_offset_end - setup->pixel_offset) + fixed_width/2;
502
x[3] = subpixel_snap(v1[0][0] + x_offset - setup->pixel_offset) + fixed_width/2;
504
y[0] = subpixel_snap(v1[0][1] + y_offset - setup->pixel_offset);
505
y[1] = subpixel_snap(v2[0][1] + y_offset_end - setup->pixel_offset);
506
y[2] = subpixel_snap(v2[0][1] + y_offset_end - setup->pixel_offset);
507
y[3] = subpixel_snap(v1[0][1] + y_offset - setup->pixel_offset);
515
/* Bounding rectangle (in pixels) */
517
/* Yes this is necessary to accurately calculate bounding boxes
518
* with the two fill-conventions we support. GL (normally) ends
519
* up needing a bottom-left fill convention, which requires
520
* slightly different rounding.
522
int adj = (setup->pixel_offset != 0) ? 1 : 0;
524
bbox.x0 = (MIN4(x[0], x[1], x[2], x[3]) + (FIXED_ONE-1)) >> FIXED_ORDER;
525
bbox.x1 = (MAX4(x[0], x[1], x[2], x[3]) + (FIXED_ONE-1)) >> FIXED_ORDER;
526
bbox.y0 = (MIN4(y[0], y[1], y[2], y[3]) + (FIXED_ONE-1) + adj) >> FIXED_ORDER;
527
bbox.y1 = (MAX4(y[0], y[1], y[2], y[3]) + (FIXED_ONE-1) + adj) >> FIXED_ORDER;
529
/* Inclusive coordinates:
535
if (bbox.x1 < bbox.x0 ||
537
if (0) debug_printf("empty bounding box\n");
538
LP_COUNT(nr_culled_tris);
542
if (!u_rect_test_intersection(&setup->draw_region, &bbox)) {
543
if (0) debug_printf("offscreen\n");
544
LP_COUNT(nr_culled_tris);
548
u_rect_find_intersection(&setup->draw_region, &bbox);
550
line = lp_setup_alloc_triangle(scene,
558
line->v[0][0] = v1[0][0];
559
line->v[1][0] = v2[0][0];
560
line->v[0][1] = v1[0][1];
561
line->v[1][1] = v2[0][1];
564
/* calculate the deltas */
565
line->plane[0].dcdy = x[0] - x[1];
566
line->plane[1].dcdy = x[1] - x[2];
567
line->plane[2].dcdy = x[2] - x[3];
568
line->plane[3].dcdy = x[3] - x[0];
570
line->plane[0].dcdx = y[0] - y[1];
571
line->plane[1].dcdx = y[1] - y[2];
572
line->plane[2].dcdx = y[2] - y[3];
573
line->plane[3].dcdx = y[3] - y[0];
576
info.oneoverarea = 1.0f / (dx * dx + dy * dy);
582
/* Setup parameter interpolants:
584
setup_line_coefficients( setup, line, &info);
586
line->inputs.facing = 1.0F;
587
line->inputs.state = setup->fs.stored;
588
line->inputs.disable = FALSE;
589
line->inputs.opaque = FALSE;
591
for (i = 0; i < 4; i++) {
592
struct lp_rast_plane *plane = &line->plane[i];
594
/* half-edge constants, will be interated over the whole render
597
plane->c = plane->dcdx * x[i] - plane->dcdy * y[i];
600
/* correct for top-left vs. bottom-left fill convention.
602
* note that we're overloading gl_rasterization_rules to mean
603
* both (0.5,0.5) pixel centers *and* bottom-left filling
606
* GL actually has a top-left filling convention, but GL's
607
* notion of "top" differs from gallium's...
609
* Also, sometimes (in FBO cases) GL will render upside down
610
* to its usual method, in which case it will probably want
611
* to use the opposite, top-left convention.
613
if (plane->dcdx < 0) {
614
/* both fill conventions want this - adjust for left edges */
617
else if (plane->dcdx == 0) {
618
if (setup->pixel_offset == 0) {
619
/* correct for top-left fill convention:
621
if (plane->dcdy > 0) plane->c++;
624
/* correct for bottom-left fill convention:
626
if (plane->dcdy < 0) plane->c++;
630
plane->dcdx *= FIXED_ONE;
631
plane->dcdy *= FIXED_ONE;
633
/* find trivial reject offsets for each edge for a single-pixel
634
* sized block. These will be scaled up at each recursive level to
635
* match the active blocksize. Scaling in this way works best if
636
* the blocks are square.
639
if (plane->dcdx < 0) plane->eo -= plane->dcdx;
640
if (plane->dcdy > 0) plane->eo += plane->dcdy;
642
/* Calculate trivial accept offsets from the above.
644
plane->ei = plane->dcdy - plane->dcdx - plane->eo;
649
* When rasterizing scissored tris, use the intersection of the
650
* triangle bounding box and the scissor rect to generate the
653
* This permits us to cut off the triangle "tails" that are present
654
* in the intermediate recursive levels caused when two of the
655
* triangles edges don't diverge quickly enough to trivially reject
656
* exterior blocks from the triangle.
658
* It's not really clear if it's worth worrying about these tails,
659
* but since we generate the planes for each scissored tri, it's
660
* free to trim them in this case.
662
* Note that otherwise, the scissor planes only vary in 'C' value,
663
* and even then only on state-changes. Could alternatively store
664
* these planes elsewhere.
666
if (nr_planes == 8) {
667
line->plane[4].dcdx = -1;
668
line->plane[4].dcdy = 0;
669
line->plane[4].c = 1-bbox.x0;
670
line->plane[4].ei = 0;
671
line->plane[4].eo = 1;
673
line->plane[5].dcdx = 1;
674
line->plane[5].dcdy = 0;
675
line->plane[5].c = bbox.x1+1;
676
line->plane[5].ei = -1;
677
line->plane[5].eo = 0;
679
line->plane[6].dcdx = 0;
680
line->plane[6].dcdy = 1;
681
line->plane[6].c = 1-bbox.y0;
682
line->plane[6].ei = 0;
683
line->plane[6].eo = 1;
685
line->plane[7].dcdx = 0;
686
line->plane[7].dcdy = -1;
687
line->plane[7].c = bbox.y1+1;
688
line->plane[7].ei = -1;
689
line->plane[7].eo = 0;
692
return lp_setup_bin_triangle(setup, line, &bbox, nr_planes);
696
static void lp_setup_line( struct lp_setup_context *setup,
697
const float (*v0)[4],
698
const float (*v1)[4] )
700
if (!try_setup_line( setup, v0, v1 ))
702
lp_setup_flush_and_restart(setup);
704
if (!try_setup_line( setup, v0, v1 ))
710
void lp_setup_choose_line( struct lp_setup_context *setup )
712
setup->line = lp_setup_line;