softpipe: Implement cylindrical wrapping for linear interpolator.
[mesa.git] / src / gallium / drivers / softpipe / sp_setup.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * \brief Primitive rasterization/rendering (points, lines, triangles)
30 *
31 * \author Keith Whitwell <keith@tungstengraphics.com>
32 * \author Brian Paul
33 */
34
35 #include "sp_context.h"
36 #include "sp_quad.h"
37 #include "sp_quad_pipe.h"
38 #include "sp_setup.h"
39 #include "sp_state.h"
40 #include "draw/draw_context.h"
41 #include "draw/draw_vertex.h"
42 #include "pipe/p_shader_tokens.h"
43 #include "util/u_math.h"
44 #include "util/u_memory.h"
45
46
47 #define DEBUG_VERTS 0
48 #define DEBUG_FRAGS 0
49
50 /**
51 * Triangle edge info
52 */
53 struct edge {
54 float dx; /**< X(v1) - X(v0), used only during setup */
55 float dy; /**< Y(v1) - Y(v0), used only during setup */
56 float dxdy; /**< dx/dy */
57 float sx, sy; /**< first sample point coord */
58 int lines; /**< number of lines on this edge */
59 };
60
61
62 #define MAX_QUADS 16
63
64
65 /**
66 * Triangle setup info (derived from draw_stage).
67 * Also used for line drawing (taking some liberties).
68 */
69 struct setup_context {
70 struct softpipe_context *softpipe;
71
72 /* Vertices are just an array of floats making up each attribute in
73 * turn. Currently fixed at 4 floats, but should change in time.
74 * Codegen will help cope with this.
75 */
76 const float (*vmax)[4];
77 const float (*vmid)[4];
78 const float (*vmin)[4];
79 const float (*vprovoke)[4];
80
81 struct edge ebot;
82 struct edge etop;
83 struct edge emaj;
84
85 float oneoverarea;
86 int facing;
87
88 float pixel_offset;
89
90 struct quad_header quad[MAX_QUADS];
91 struct quad_header *quad_ptrs[MAX_QUADS];
92 unsigned count;
93
94 struct tgsi_interp_coef coef[PIPE_MAX_SHADER_INPUTS];
95 struct tgsi_interp_coef posCoef; /* For Z, W */
96
97 struct {
98 int left[2]; /**< [0] = row0, [1] = row1 */
99 int right[2];
100 int y;
101 } span;
102
103 #if DEBUG_FRAGS
104 uint numFragsEmitted; /**< per primitive */
105 uint numFragsWritten; /**< per primitive */
106 #endif
107
108 unsigned winding; /* which winding to cull */
109 unsigned nr_vertex_attrs;
110 };
111
112
113
114
115 /**
116 * Do triangle cull test using tri determinant (sign indicates orientation)
117 * \return true if triangle is to be culled.
118 */
119 static INLINE boolean
120 cull_tri(const struct setup_context *setup, float det)
121 {
122 if (det != 0) {
123 /* if (det < 0 then Z points toward camera and triangle is
124 * counter-clockwise winding.
125 */
126 unsigned winding = (det < 0) ? PIPE_WINDING_CCW : PIPE_WINDING_CW;
127
128 if ((winding & setup->winding) == 0)
129 return FALSE;
130 }
131
132 /* Culled:
133 */
134 return TRUE;
135 }
136
137
138
139 /**
140 * Clip setup->quad against the scissor/surface bounds.
141 */
142 static INLINE void
143 quad_clip( struct setup_context *setup, struct quad_header *quad )
144 {
145 const struct pipe_scissor_state *cliprect = &setup->softpipe->cliprect;
146 const int minx = (int) cliprect->minx;
147 const int maxx = (int) cliprect->maxx;
148 const int miny = (int) cliprect->miny;
149 const int maxy = (int) cliprect->maxy;
150
151 if (quad->input.x0 >= maxx ||
152 quad->input.y0 >= maxy ||
153 quad->input.x0 + 1 < minx ||
154 quad->input.y0 + 1 < miny) {
155 /* totally clipped */
156 quad->inout.mask = 0x0;
157 return;
158 }
159 if (quad->input.x0 < minx)
160 quad->inout.mask &= (MASK_BOTTOM_RIGHT | MASK_TOP_RIGHT);
161 if (quad->input.y0 < miny)
162 quad->inout.mask &= (MASK_BOTTOM_LEFT | MASK_BOTTOM_RIGHT);
163 if (quad->input.x0 == maxx - 1)
164 quad->inout.mask &= (MASK_BOTTOM_LEFT | MASK_TOP_LEFT);
165 if (quad->input.y0 == maxy - 1)
166 quad->inout.mask &= (MASK_TOP_LEFT | MASK_TOP_RIGHT);
167 }
168
169
170 /**
171 * Emit a quad (pass to next stage) with clipping.
172 */
173 static INLINE void
174 clip_emit_quad( struct setup_context *setup, struct quad_header *quad )
175 {
176 quad_clip( setup, quad );
177
178 if (quad->inout.mask) {
179 struct softpipe_context *sp = setup->softpipe;
180
181 sp->quad.first->run( sp->quad.first, &quad, 1 );
182 }
183 }
184
185
186
187 /**
188 * Given an X or Y coordinate, return the block/quad coordinate that it
189 * belongs to.
190 */
191 static INLINE int block( int x )
192 {
193 return x & ~(2-1);
194 }
195
196 static INLINE int block_x( int x )
197 {
198 return x & ~(16-1);
199 }
200
201
202 /**
203 * Render a horizontal span of quads
204 */
205 static void flush_spans( struct setup_context *setup )
206 {
207 const int step = 16;
208 const int xleft0 = setup->span.left[0];
209 const int xleft1 = setup->span.left[1];
210 const int xright0 = setup->span.right[0];
211 const int xright1 = setup->span.right[1];
212 struct quad_stage *pipe = setup->softpipe->quad.first;
213
214
215 int minleft = block_x(MIN2(xleft0, xleft1));
216 int maxright = MAX2(xright0, xright1);
217 int x;
218
219 for (x = minleft; x < maxright; x += step) {
220 unsigned skip_left0 = CLAMP(xleft0 - x, 0, step);
221 unsigned skip_left1 = CLAMP(xleft1 - x, 0, step);
222 unsigned skip_right0 = CLAMP(x + step - xright0, 0, step);
223 unsigned skip_right1 = CLAMP(x + step - xright1, 0, step);
224 unsigned lx = x;
225 unsigned q = 0;
226
227 unsigned skipmask_left0 = (1U << skip_left0) - 1U;
228 unsigned skipmask_left1 = (1U << skip_left1) - 1U;
229
230 /* These calculations fail when step == 32 and skip_right == 0.
231 */
232 unsigned skipmask_right0 = ~0U << (unsigned)(step - skip_right0);
233 unsigned skipmask_right1 = ~0U << (unsigned)(step - skip_right1);
234
235 unsigned mask0 = ~skipmask_left0 & ~skipmask_right0;
236 unsigned mask1 = ~skipmask_left1 & ~skipmask_right1;
237
238 if (mask0 | mask1) {
239 do {
240 unsigned quadmask = (mask0 & 3) | ((mask1 & 3) << 2);
241 if (quadmask) {
242 setup->quad[q].input.x0 = lx;
243 setup->quad[q].input.y0 = setup->span.y;
244 setup->quad[q].input.facing = setup->facing;
245 setup->quad[q].inout.mask = quadmask;
246 setup->quad_ptrs[q] = &setup->quad[q];
247 q++;
248 }
249 mask0 >>= 2;
250 mask1 >>= 2;
251 lx += 2;
252 } while (mask0 | mask1);
253
254 pipe->run( pipe, setup->quad_ptrs, q );
255 }
256 }
257
258
259 setup->span.y = 0;
260 setup->span.right[0] = 0;
261 setup->span.right[1] = 0;
262 setup->span.left[0] = 1000000; /* greater than right[0] */
263 setup->span.left[1] = 1000000; /* greater than right[1] */
264 }
265
266
267 #if DEBUG_VERTS
268 static void print_vertex(const struct setup_context *setup,
269 const float (*v)[4])
270 {
271 int i;
272 debug_printf(" Vertex: (%p)\n", (void *) v);
273 for (i = 0; i < setup->nr_vertex_attrs; i++) {
274 debug_printf(" %d: %f %f %f %f\n", i,
275 v[i][0], v[i][1], v[i][2], v[i][3]);
276 if (util_is_inf_or_nan(v[i][0])) {
277 debug_printf(" NaN!\n");
278 }
279 }
280 }
281 #endif
282
283 /**
284 * Sort the vertices from top to bottom order, setting up the triangle
285 * edge fields (ebot, emaj, etop).
286 * \return FALSE if coords are inf/nan (cull the tri), TRUE otherwise
287 */
288 static boolean setup_sort_vertices( struct setup_context *setup,
289 float det,
290 const float (*v0)[4],
291 const float (*v1)[4],
292 const float (*v2)[4] )
293 {
294 setup->vprovoke = v2;
295
296 /* determine bottom to top order of vertices */
297 {
298 float y0 = v0[0][1];
299 float y1 = v1[0][1];
300 float y2 = v2[0][1];
301 if (y0 <= y1) {
302 if (y1 <= y2) {
303 /* y0<=y1<=y2 */
304 setup->vmin = v0;
305 setup->vmid = v1;
306 setup->vmax = v2;
307 }
308 else if (y2 <= y0) {
309 /* y2<=y0<=y1 */
310 setup->vmin = v2;
311 setup->vmid = v0;
312 setup->vmax = v1;
313 }
314 else {
315 /* y0<=y2<=y1 */
316 setup->vmin = v0;
317 setup->vmid = v2;
318 setup->vmax = v1;
319 }
320 }
321 else {
322 if (y0 <= y2) {
323 /* y1<=y0<=y2 */
324 setup->vmin = v1;
325 setup->vmid = v0;
326 setup->vmax = v2;
327 }
328 else if (y2 <= y1) {
329 /* y2<=y1<=y0 */
330 setup->vmin = v2;
331 setup->vmid = v1;
332 setup->vmax = v0;
333 }
334 else {
335 /* y1<=y2<=y0 */
336 setup->vmin = v1;
337 setup->vmid = v2;
338 setup->vmax = v0;
339 }
340 }
341 }
342
343 setup->ebot.dx = setup->vmid[0][0] - setup->vmin[0][0];
344 setup->ebot.dy = setup->vmid[0][1] - setup->vmin[0][1];
345 setup->emaj.dx = setup->vmax[0][0] - setup->vmin[0][0];
346 setup->emaj.dy = setup->vmax[0][1] - setup->vmin[0][1];
347 setup->etop.dx = setup->vmax[0][0] - setup->vmid[0][0];
348 setup->etop.dy = setup->vmax[0][1] - setup->vmid[0][1];
349
350 /*
351 * Compute triangle's area. Use 1/area to compute partial
352 * derivatives of attributes later.
353 *
354 * The area will be the same as prim->det, but the sign may be
355 * different depending on how the vertices get sorted above.
356 *
357 * To determine whether the primitive is front or back facing we
358 * use the prim->det value because its sign is correct.
359 */
360 {
361 const float area = (setup->emaj.dx * setup->ebot.dy -
362 setup->ebot.dx * setup->emaj.dy);
363
364 setup->oneoverarea = 1.0f / area;
365
366 /*
367 debug_printf("%s one-over-area %f area %f det %f\n",
368 __FUNCTION__, setup->oneoverarea, area, det );
369 */
370 if (util_is_inf_or_nan(setup->oneoverarea))
371 return FALSE;
372 }
373
374 /* We need to know if this is a front or back-facing triangle for:
375 * - the GLSL gl_FrontFacing fragment attribute (bool)
376 * - two-sided stencil test
377 */
378 setup->facing =
379 ((det > 0.0) ^
380 (setup->softpipe->rasterizer->front_winding == PIPE_WINDING_CW));
381
382 /* Prepare pixel offset for rasterisation:
383 * - pixel center (0.5, 0.5) for GL, or
384 * - assume (0.0, 0.0) for other APIs.
385 */
386 if (setup->softpipe->rasterizer->gl_rasterization_rules) {
387 setup->pixel_offset = 0.5f;
388 } else {
389 setup->pixel_offset = 0.0f;
390 }
391
392 return TRUE;
393 }
394
395
396 /**
397 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
398 * The value value comes from vertex[slot][i].
399 * The result will be put into setup->coef[slot].a0[i].
400 * \param slot which attribute slot
401 * \param i which component of the slot (0..3)
402 */
403 static void const_coeff( struct setup_context *setup,
404 struct tgsi_interp_coef *coef,
405 uint vertSlot, uint i)
406 {
407 assert(i <= 3);
408
409 coef->dadx[i] = 0;
410 coef->dady[i] = 0;
411
412 /* need provoking vertex info!
413 */
414 coef->a0[i] = setup->vprovoke[vertSlot][i];
415 }
416
417
418 /**
419 * Compute a0, dadx and dady for a linearly interpolated coefficient,
420 * for a triangle.
421 */
422 static void
423 tri_linear_coeff(struct setup_context *setup,
424 struct tgsi_interp_coef *coef,
425 uint vertSlot,
426 uint i,
427 uint cylindrical_wrap)
428 {
429 float botda = setup->vmid[vertSlot][i] - setup->vmin[vertSlot][i];
430 float majda = setup->vmax[vertSlot][i] - setup->vmin[vertSlot][i];
431 float a, b;
432 float dadx, dady;
433
434 assert(i <= 3);
435
436 if (cylindrical_wrap) {
437 if (botda > 0.5f) {
438 botda -= 1.0f;
439 } else if (botda < -0.5f) {
440 botda += 1.0f;
441 }
442 if (majda > 0.5f) {
443 majda -= 1.0f;
444 } else if (majda < -0.5f) {
445 majda += 1.0f;
446 }
447 }
448
449 a = setup->ebot.dy * majda - botda * setup->emaj.dy;
450 b = setup->emaj.dx * botda - majda * setup->ebot.dx;
451 dadx = a * setup->oneoverarea;
452 dady = b * setup->oneoverarea;
453
454 coef->dadx[i] = dadx;
455 coef->dady[i] = dady;
456
457 /* calculate a0 as the value which would be sampled for the
458 * fragment at (0,0), taking into account that we want to sample at
459 * pixel centers, in other words (pixel_offset, pixel_offset).
460 *
461 * this is neat but unfortunately not a good way to do things for
462 * triangles with very large values of dadx or dady as it will
463 * result in the subtraction and re-addition from a0 of a very
464 * large number, which means we'll end up loosing a lot of the
465 * fractional bits and precision from a0. the way to fix this is
466 * to define a0 as the sample at a pixel center somewhere near vmin
467 * instead - i'll switch to this later.
468 */
469 coef->a0[i] = (setup->vmin[vertSlot][i] -
470 (dadx * (setup->vmin[0][0] - setup->pixel_offset) +
471 dady * (setup->vmin[0][1] - setup->pixel_offset)));
472
473 /*
474 debug_printf("attr[%d].%c: %f dx:%f dy:%f\n",
475 slot, "xyzw"[i],
476 setup->coef[slot].a0[i],
477 setup->coef[slot].dadx[i],
478 setup->coef[slot].dady[i]);
479 */
480 }
481
482
483 /**
484 * Compute a0, dadx and dady for a perspective-corrected interpolant,
485 * for a triangle.
486 * We basically multiply the vertex value by 1/w before computing
487 * the plane coefficients (a0, dadx, dady).
488 * Later, when we compute the value at a particular fragment position we'll
489 * divide the interpolated value by the interpolated W at that fragment.
490 */
491 static void tri_persp_coeff( struct setup_context *setup,
492 struct tgsi_interp_coef *coef,
493 uint vertSlot, uint i)
494 {
495 /* premultiply by 1/w (v[0][3] is always W):
496 */
497 float mina = setup->vmin[vertSlot][i] * setup->vmin[0][3];
498 float mida = setup->vmid[vertSlot][i] * setup->vmid[0][3];
499 float maxa = setup->vmax[vertSlot][i] * setup->vmax[0][3];
500 float botda = mida - mina;
501 float majda = maxa - mina;
502 float a = setup->ebot.dy * majda - botda * setup->emaj.dy;
503 float b = setup->emaj.dx * botda - majda * setup->ebot.dx;
504 float dadx = a * setup->oneoverarea;
505 float dady = b * setup->oneoverarea;
506
507 /*
508 debug_printf("tri persp %d,%d: %f %f %f\n", vertSlot, i,
509 setup->vmin[vertSlot][i],
510 setup->vmid[vertSlot][i],
511 setup->vmax[vertSlot][i]
512 );
513 */
514 assert(i <= 3);
515
516 coef->dadx[i] = dadx;
517 coef->dady[i] = dady;
518 coef->a0[i] = (mina -
519 (dadx * (setup->vmin[0][0] - setup->pixel_offset) +
520 dady * (setup->vmin[0][1] - setup->pixel_offset)));
521 }
522
523
524 /**
525 * Special coefficient setup for gl_FragCoord.
526 * X and Y are trivial, though Y may have to be inverted for OpenGL.
527 * Z and W are copied from posCoef which should have already been computed.
528 * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.
529 */
530 static void
531 setup_fragcoord_coeff(struct setup_context *setup, uint slot)
532 {
533 struct sp_fragment_shader* spfs = setup->softpipe->fs;
534 /*X*/
535 setup->coef[slot].a0[0] = spfs->pixel_center_integer ? 0.0 : 0.5;
536 setup->coef[slot].dadx[0] = 1.0;
537 setup->coef[slot].dady[0] = 0.0;
538 /*Y*/
539 setup->coef[slot].a0[1] =
540 (spfs->origin_lower_left ? setup->softpipe->framebuffer.height : 0)
541 + (spfs->pixel_center_integer ? 0.0 : 0.5);
542 setup->coef[slot].dadx[1] = 0.0;
543 setup->coef[slot].dady[1] = spfs->origin_lower_left ? -1.0 : 1.0;
544 /*Z*/
545 setup->coef[slot].a0[2] = setup->posCoef.a0[2];
546 setup->coef[slot].dadx[2] = setup->posCoef.dadx[2];
547 setup->coef[slot].dady[2] = setup->posCoef.dady[2];
548 /*W*/
549 setup->coef[slot].a0[3] = setup->posCoef.a0[3];
550 setup->coef[slot].dadx[3] = setup->posCoef.dadx[3];
551 setup->coef[slot].dady[3] = setup->posCoef.dady[3];
552 }
553
554
555
556 /**
557 * Compute the setup->coef[] array dadx, dady, a0 values.
558 * Must be called after setup->vmin,vmid,vmax,vprovoke are initialized.
559 */
560 static void setup_tri_coefficients( struct setup_context *setup )
561 {
562 struct softpipe_context *softpipe = setup->softpipe;
563 const struct sp_fragment_shader *spfs = softpipe->fs;
564 const struct vertex_info *vinfo = softpipe_get_vertex_info(softpipe);
565 uint fragSlot;
566
567 /* z and w are done by linear interpolation:
568 */
569 tri_linear_coeff(setup, &setup->posCoef, 0, 2, 0);
570 tri_linear_coeff(setup, &setup->posCoef, 0, 3, 0);
571
572 /* setup interpolation for all the remaining attributes:
573 */
574 for (fragSlot = 0; fragSlot < spfs->info.num_inputs; fragSlot++) {
575 const uint vertSlot = vinfo->attrib[fragSlot].src_index;
576 uint j;
577
578 switch (vinfo->attrib[fragSlot].interp_mode) {
579 case INTERP_CONSTANT:
580 for (j = 0; j < NUM_CHANNELS; j++)
581 const_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
582 break;
583 case INTERP_LINEAR:
584 for (j = 0; j < NUM_CHANNELS; j++) {
585 tri_linear_coeff(setup,
586 &setup->coef[fragSlot],
587 vertSlot,
588 j,
589 spfs->info.input_cylindrical_wrap[fragSlot] & (1 << j));
590 }
591 break;
592 case INTERP_PERSPECTIVE:
593 for (j = 0; j < NUM_CHANNELS; j++)
594 tri_persp_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
595 break;
596 case INTERP_POS:
597 setup_fragcoord_coeff(setup, fragSlot);
598 break;
599 default:
600 assert(0);
601 }
602
603 if (spfs->info.input_semantic_name[fragSlot] == TGSI_SEMANTIC_FACE) {
604 setup->coef[fragSlot].a0[0] = 1.0f - setup->facing;
605 setup->coef[fragSlot].dadx[0] = 0.0;
606 setup->coef[fragSlot].dady[0] = 0.0;
607 }
608 }
609 }
610
611
612
613 static void setup_tri_edges( struct setup_context *setup )
614 {
615 float vmin_x = setup->vmin[0][0] + setup->pixel_offset;
616 float vmid_x = setup->vmid[0][0] + setup->pixel_offset;
617
618 float vmin_y = setup->vmin[0][1] - setup->pixel_offset;
619 float vmid_y = setup->vmid[0][1] - setup->pixel_offset;
620 float vmax_y = setup->vmax[0][1] - setup->pixel_offset;
621
622 setup->emaj.sy = ceilf(vmin_y);
623 setup->emaj.lines = (int) ceilf(vmax_y - setup->emaj.sy);
624 setup->emaj.dxdy = setup->emaj.dx / setup->emaj.dy;
625 setup->emaj.sx = vmin_x + (setup->emaj.sy - vmin_y) * setup->emaj.dxdy;
626
627 setup->etop.sy = ceilf(vmid_y);
628 setup->etop.lines = (int) ceilf(vmax_y - setup->etop.sy);
629 setup->etop.dxdy = setup->etop.dx / setup->etop.dy;
630 setup->etop.sx = vmid_x + (setup->etop.sy - vmid_y) * setup->etop.dxdy;
631
632 setup->ebot.sy = ceilf(vmin_y);
633 setup->ebot.lines = (int) ceilf(vmid_y - setup->ebot.sy);
634 setup->ebot.dxdy = setup->ebot.dx / setup->ebot.dy;
635 setup->ebot.sx = vmin_x + (setup->ebot.sy - vmin_y) * setup->ebot.dxdy;
636 }
637
638
639 /**
640 * Render the upper or lower half of a triangle.
641 * Scissoring/cliprect is applied here too.
642 */
643 static void subtriangle( struct setup_context *setup,
644 struct edge *eleft,
645 struct edge *eright,
646 unsigned lines )
647 {
648 const struct pipe_scissor_state *cliprect = &setup->softpipe->cliprect;
649 const int minx = (int) cliprect->minx;
650 const int maxx = (int) cliprect->maxx;
651 const int miny = (int) cliprect->miny;
652 const int maxy = (int) cliprect->maxy;
653 int y, start_y, finish_y;
654 int sy = (int)eleft->sy;
655
656 assert((int)eleft->sy == (int) eright->sy);
657
658 /* clip top/bottom */
659 start_y = sy;
660 if (start_y < miny)
661 start_y = miny;
662
663 finish_y = sy + lines;
664 if (finish_y > maxy)
665 finish_y = maxy;
666
667 start_y -= sy;
668 finish_y -= sy;
669
670 /*
671 debug_printf("%s %d %d\n", __FUNCTION__, start_y, finish_y);
672 */
673
674 for (y = start_y; y < finish_y; y++) {
675
676 /* avoid accumulating adds as floats don't have the precision to
677 * accurately iterate large triangle edges that way. luckily we
678 * can just multiply these days.
679 *
680 * this is all drowned out by the attribute interpolation anyway.
681 */
682 int left = (int)(eleft->sx + y * eleft->dxdy);
683 int right = (int)(eright->sx + y * eright->dxdy);
684
685 /* clip left/right */
686 if (left < minx)
687 left = minx;
688 if (right > maxx)
689 right = maxx;
690
691 if (left < right) {
692 int _y = sy + y;
693 if (block(_y) != setup->span.y) {
694 flush_spans(setup);
695 setup->span.y = block(_y);
696 }
697
698 setup->span.left[_y&1] = left;
699 setup->span.right[_y&1] = right;
700 }
701 }
702
703
704 /* save the values so that emaj can be restarted:
705 */
706 eleft->sx += lines * eleft->dxdy;
707 eright->sx += lines * eright->dxdy;
708 eleft->sy += lines;
709 eright->sy += lines;
710 }
711
712
713 /**
714 * Recalculate prim's determinant. This is needed as we don't have
715 * get this information through the vbuf_render interface & we must
716 * calculate it here.
717 */
718 static float
719 calc_det( const float (*v0)[4],
720 const float (*v1)[4],
721 const float (*v2)[4] )
722 {
723 /* edge vectors e = v0 - v2, f = v1 - v2 */
724 const float ex = v0[0][0] - v2[0][0];
725 const float ey = v0[0][1] - v2[0][1];
726 const float fx = v1[0][0] - v2[0][0];
727 const float fy = v1[0][1] - v2[0][1];
728
729 /* det = cross(e,f).z */
730 return ex * fy - ey * fx;
731 }
732
733
734 /**
735 * Do setup for triangle rasterization, then render the triangle.
736 */
737 void sp_setup_tri( struct setup_context *setup,
738 const float (*v0)[4],
739 const float (*v1)[4],
740 const float (*v2)[4] )
741 {
742 float det;
743
744 #if DEBUG_VERTS
745 debug_printf("Setup triangle:\n");
746 print_vertex(setup, v0);
747 print_vertex(setup, v1);
748 print_vertex(setup, v2);
749 #endif
750
751 if (setup->softpipe->no_rast)
752 return;
753
754 det = calc_det(v0, v1, v2);
755 /*
756 debug_printf("%s\n", __FUNCTION__ );
757 */
758
759 #if DEBUG_FRAGS
760 setup->numFragsEmitted = 0;
761 setup->numFragsWritten = 0;
762 #endif
763
764 if (cull_tri( setup, det ))
765 return;
766
767 if (!setup_sort_vertices( setup, det, v0, v1, v2 ))
768 return;
769 setup_tri_coefficients( setup );
770 setup_tri_edges( setup );
771
772 assert(setup->softpipe->reduced_prim == PIPE_PRIM_TRIANGLES);
773
774 setup->span.y = 0;
775 setup->span.right[0] = 0;
776 setup->span.right[1] = 0;
777 /* setup->span.z_mode = tri_z_mode( setup->ctx ); */
778
779 /* init_constant_attribs( setup ); */
780
781 if (setup->oneoverarea < 0.0) {
782 /* emaj on left:
783 */
784 subtriangle( setup, &setup->emaj, &setup->ebot, setup->ebot.lines );
785 subtriangle( setup, &setup->emaj, &setup->etop, setup->etop.lines );
786 }
787 else {
788 /* emaj on right:
789 */
790 subtriangle( setup, &setup->ebot, &setup->emaj, setup->ebot.lines );
791 subtriangle( setup, &setup->etop, &setup->emaj, setup->etop.lines );
792 }
793
794 flush_spans( setup );
795
796 #if DEBUG_FRAGS
797 printf("Tri: %u frags emitted, %u written\n",
798 setup->numFragsEmitted,
799 setup->numFragsWritten);
800 #endif
801 }
802
803
804
805 /**
806 * Compute a0, dadx and dady for a linearly interpolated coefficient,
807 * for a line.
808 */
809 static void
810 line_linear_coeff(const struct setup_context *setup,
811 struct tgsi_interp_coef *coef,
812 uint vertSlot, uint i)
813 {
814 const float da = setup->vmax[vertSlot][i] - setup->vmin[vertSlot][i];
815 const float dadx = da * setup->emaj.dx * setup->oneoverarea;
816 const float dady = da * setup->emaj.dy * setup->oneoverarea;
817 coef->dadx[i] = dadx;
818 coef->dady[i] = dady;
819 coef->a0[i] = (setup->vmin[vertSlot][i] -
820 (dadx * (setup->vmin[0][0] - setup->pixel_offset) +
821 dady * (setup->vmin[0][1] - setup->pixel_offset)));
822 }
823
824
825 /**
826 * Compute a0, dadx and dady for a perspective-corrected interpolant,
827 * for a line.
828 */
829 static void
830 line_persp_coeff(const struct setup_context *setup,
831 struct tgsi_interp_coef *coef,
832 uint vertSlot, uint i)
833 {
834 /* XXX double-check/verify this arithmetic */
835 const float a0 = setup->vmin[vertSlot][i] * setup->vmin[0][3];
836 const float a1 = setup->vmax[vertSlot][i] * setup->vmax[0][3];
837 const float da = a1 - a0;
838 const float dadx = da * setup->emaj.dx * setup->oneoverarea;
839 const float dady = da * setup->emaj.dy * setup->oneoverarea;
840 coef->dadx[i] = dadx;
841 coef->dady[i] = dady;
842 coef->a0[i] = (setup->vmin[vertSlot][i] -
843 (dadx * (setup->vmin[0][0] - setup->pixel_offset) +
844 dady * (setup->vmin[0][1] - setup->pixel_offset)));
845 }
846
847
848 /**
849 * Compute the setup->coef[] array dadx, dady, a0 values.
850 * Must be called after setup->vmin,vmax are initialized.
851 */
852 static INLINE boolean
853 setup_line_coefficients(struct setup_context *setup,
854 const float (*v0)[4],
855 const float (*v1)[4])
856 {
857 struct softpipe_context *softpipe = setup->softpipe;
858 const struct sp_fragment_shader *spfs = softpipe->fs;
859 const struct vertex_info *vinfo = softpipe_get_vertex_info(softpipe);
860 uint fragSlot;
861 float area;
862
863 /* use setup->vmin, vmax to point to vertices */
864 if (softpipe->rasterizer->flatshade_first)
865 setup->vprovoke = v0;
866 else
867 setup->vprovoke = v1;
868 setup->vmin = v0;
869 setup->vmax = v1;
870
871 setup->emaj.dx = setup->vmax[0][0] - setup->vmin[0][0];
872 setup->emaj.dy = setup->vmax[0][1] - setup->vmin[0][1];
873
874 /* NOTE: this is not really area but something proportional to it */
875 area = setup->emaj.dx * setup->emaj.dx + setup->emaj.dy * setup->emaj.dy;
876 if (area == 0.0f || util_is_inf_or_nan(area))
877 return FALSE;
878 setup->oneoverarea = 1.0f / area;
879
880 /* z and w are done by linear interpolation:
881 */
882 line_linear_coeff(setup, &setup->posCoef, 0, 2);
883 line_linear_coeff(setup, &setup->posCoef, 0, 3);
884
885 /* setup interpolation for all the remaining attributes:
886 */
887 for (fragSlot = 0; fragSlot < spfs->info.num_inputs; fragSlot++) {
888 const uint vertSlot = vinfo->attrib[fragSlot].src_index;
889 uint j;
890
891 switch (vinfo->attrib[fragSlot].interp_mode) {
892 case INTERP_CONSTANT:
893 for (j = 0; j < NUM_CHANNELS; j++)
894 const_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
895 break;
896 case INTERP_LINEAR:
897 for (j = 0; j < NUM_CHANNELS; j++)
898 line_linear_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
899 break;
900 case INTERP_PERSPECTIVE:
901 for (j = 0; j < NUM_CHANNELS; j++)
902 line_persp_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
903 break;
904 case INTERP_POS:
905 setup_fragcoord_coeff(setup, fragSlot);
906 break;
907 default:
908 assert(0);
909 }
910
911 if (spfs->info.input_semantic_name[fragSlot] == TGSI_SEMANTIC_FACE) {
912 setup->coef[fragSlot].a0[0] = 1.0f - setup->facing;
913 setup->coef[fragSlot].dadx[0] = 0.0;
914 setup->coef[fragSlot].dady[0] = 0.0;
915 }
916 }
917 return TRUE;
918 }
919
920
921 /**
922 * Plot a pixel in a line segment.
923 */
924 static INLINE void
925 plot(struct setup_context *setup, int x, int y)
926 {
927 const int iy = y & 1;
928 const int ix = x & 1;
929 const int quadX = x - ix;
930 const int quadY = y - iy;
931 const int mask = (1 << ix) << (2 * iy);
932
933 if (quadX != setup->quad[0].input.x0 ||
934 quadY != setup->quad[0].input.y0)
935 {
936 /* flush prev quad, start new quad */
937
938 if (setup->quad[0].input.x0 != -1)
939 clip_emit_quad( setup, &setup->quad[0] );
940
941 setup->quad[0].input.x0 = quadX;
942 setup->quad[0].input.y0 = quadY;
943 setup->quad[0].inout.mask = 0x0;
944 }
945
946 setup->quad[0].inout.mask |= mask;
947 }
948
949
950 /**
951 * Do setup for line rasterization, then render the line.
952 * Single-pixel width, no stipple, etc. We rely on the 'draw' module
953 * to handle stippling and wide lines.
954 */
955 void
956 sp_setup_line(struct setup_context *setup,
957 const float (*v0)[4],
958 const float (*v1)[4])
959 {
960 int x0 = (int) v0[0][0];
961 int x1 = (int) v1[0][0];
962 int y0 = (int) v0[0][1];
963 int y1 = (int) v1[0][1];
964 int dx = x1 - x0;
965 int dy = y1 - y0;
966 int xstep, ystep;
967
968 #if DEBUG_VERTS
969 debug_printf("Setup line:\n");
970 print_vertex(setup, v0);
971 print_vertex(setup, v1);
972 #endif
973
974 if (setup->softpipe->no_rast)
975 return;
976
977 if (dx == 0 && dy == 0)
978 return;
979
980 if (!setup_line_coefficients(setup, v0, v1))
981 return;
982
983 assert(v0[0][0] < 1.0e9);
984 assert(v0[0][1] < 1.0e9);
985 assert(v1[0][0] < 1.0e9);
986 assert(v1[0][1] < 1.0e9);
987
988 if (dx < 0) {
989 dx = -dx; /* make positive */
990 xstep = -1;
991 }
992 else {
993 xstep = 1;
994 }
995
996 if (dy < 0) {
997 dy = -dy; /* make positive */
998 ystep = -1;
999 }
1000 else {
1001 ystep = 1;
1002 }
1003
1004 assert(dx >= 0);
1005 assert(dy >= 0);
1006 assert(setup->softpipe->reduced_prim == PIPE_PRIM_LINES);
1007
1008 setup->quad[0].input.x0 = setup->quad[0].input.y0 = -1;
1009 setup->quad[0].inout.mask = 0x0;
1010
1011 /* XXX temporary: set coverage to 1.0 so the line appears
1012 * if AA mode happens to be enabled.
1013 */
1014 setup->quad[0].input.coverage[0] =
1015 setup->quad[0].input.coverage[1] =
1016 setup->quad[0].input.coverage[2] =
1017 setup->quad[0].input.coverage[3] = 1.0;
1018
1019 if (dx > dy) {
1020 /*** X-major line ***/
1021 int i;
1022 const int errorInc = dy + dy;
1023 int error = errorInc - dx;
1024 const int errorDec = error - dx;
1025
1026 for (i = 0; i < dx; i++) {
1027 plot(setup, x0, y0);
1028
1029 x0 += xstep;
1030 if (error < 0) {
1031 error += errorInc;
1032 }
1033 else {
1034 error += errorDec;
1035 y0 += ystep;
1036 }
1037 }
1038 }
1039 else {
1040 /*** Y-major line ***/
1041 int i;
1042 const int errorInc = dx + dx;
1043 int error = errorInc - dy;
1044 const int errorDec = error - dy;
1045
1046 for (i = 0; i < dy; i++) {
1047 plot(setup, x0, y0);
1048
1049 y0 += ystep;
1050 if (error < 0) {
1051 error += errorInc;
1052 }
1053 else {
1054 error += errorDec;
1055 x0 += xstep;
1056 }
1057 }
1058 }
1059
1060 /* draw final quad */
1061 if (setup->quad[0].inout.mask) {
1062 clip_emit_quad( setup, &setup->quad[0] );
1063 }
1064 }
1065
1066
1067 static void
1068 point_persp_coeff(const struct setup_context *setup,
1069 const float (*vert)[4],
1070 struct tgsi_interp_coef *coef,
1071 uint vertSlot, uint i)
1072 {
1073 assert(i <= 3);
1074 coef->dadx[i] = 0.0F;
1075 coef->dady[i] = 0.0F;
1076 coef->a0[i] = vert[vertSlot][i] * vert[0][3];
1077 }
1078
1079
1080 /**
1081 * Do setup for point rasterization, then render the point.
1082 * Round or square points...
1083 * XXX could optimize a lot for 1-pixel points.
1084 */
1085 void
1086 sp_setup_point( struct setup_context *setup,
1087 const float (*v0)[4] )
1088 {
1089 struct softpipe_context *softpipe = setup->softpipe;
1090 const struct sp_fragment_shader *spfs = softpipe->fs;
1091 const int sizeAttr = setup->softpipe->psize_slot;
1092 const float size
1093 = sizeAttr > 0 ? v0[sizeAttr][0]
1094 : setup->softpipe->rasterizer->point_size;
1095 const float halfSize = 0.5F * size;
1096 const boolean round = (boolean) setup->softpipe->rasterizer->point_smooth;
1097 const float x = v0[0][0]; /* Note: data[0] is always position */
1098 const float y = v0[0][1];
1099 const struct vertex_info *vinfo = softpipe_get_vertex_info(softpipe);
1100 uint fragSlot;
1101
1102 #if DEBUG_VERTS
1103 debug_printf("Setup point:\n");
1104 print_vertex(setup, v0);
1105 #endif
1106
1107 if (softpipe->no_rast)
1108 return;
1109
1110 assert(setup->softpipe->reduced_prim == PIPE_PRIM_POINTS);
1111
1112 /* For points, all interpolants are constant-valued.
1113 * However, for point sprites, we'll need to setup texcoords appropriately.
1114 * XXX: which coefficients are the texcoords???
1115 * We may do point sprites as textured quads...
1116 *
1117 * KW: We don't know which coefficients are texcoords - ultimately
1118 * the choice of what interpolation mode to use for each attribute
1119 * should be determined by the fragment program, using
1120 * per-attribute declaration statements that include interpolation
1121 * mode as a parameter. So either the fragment program will have
1122 * to be adjusted for pointsprite vs normal point behaviour, or
1123 * otherwise a special interpolation mode will have to be defined
1124 * which matches the required behaviour for point sprites. But -
1125 * the latter is not a feature of normal hardware, and as such
1126 * probably should be ruled out on that basis.
1127 */
1128 setup->vprovoke = v0;
1129
1130 /* setup Z, W */
1131 const_coeff(setup, &setup->posCoef, 0, 2);
1132 const_coeff(setup, &setup->posCoef, 0, 3);
1133
1134 for (fragSlot = 0; fragSlot < spfs->info.num_inputs; fragSlot++) {
1135 const uint vertSlot = vinfo->attrib[fragSlot].src_index;
1136 uint j;
1137
1138 switch (vinfo->attrib[fragSlot].interp_mode) {
1139 case INTERP_CONSTANT:
1140 /* fall-through */
1141 case INTERP_LINEAR:
1142 for (j = 0; j < NUM_CHANNELS; j++)
1143 const_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
1144 break;
1145 case INTERP_PERSPECTIVE:
1146 for (j = 0; j < NUM_CHANNELS; j++)
1147 point_persp_coeff(setup, setup->vprovoke,
1148 &setup->coef[fragSlot], vertSlot, j);
1149 break;
1150 case INTERP_POS:
1151 setup_fragcoord_coeff(setup, fragSlot);
1152 break;
1153 default:
1154 assert(0);
1155 }
1156
1157 if (spfs->info.input_semantic_name[fragSlot] == TGSI_SEMANTIC_FACE) {
1158 setup->coef[fragSlot].a0[0] = 1.0f - setup->facing;
1159 setup->coef[fragSlot].dadx[0] = 0.0;
1160 setup->coef[fragSlot].dady[0] = 0.0;
1161 }
1162 }
1163
1164
1165 if (halfSize <= 0.5 && !round) {
1166 /* special case for 1-pixel points */
1167 const int ix = ((int) x) & 1;
1168 const int iy = ((int) y) & 1;
1169 setup->quad[0].input.x0 = (int) x - ix;
1170 setup->quad[0].input.y0 = (int) y - iy;
1171 setup->quad[0].inout.mask = (1 << ix) << (2 * iy);
1172 clip_emit_quad( setup, &setup->quad[0] );
1173 }
1174 else {
1175 if (round) {
1176 /* rounded points */
1177 const int ixmin = block((int) (x - halfSize));
1178 const int ixmax = block((int) (x + halfSize));
1179 const int iymin = block((int) (y - halfSize));
1180 const int iymax = block((int) (y + halfSize));
1181 const float rmin = halfSize - 0.7071F; /* 0.7071 = sqrt(2)/2 */
1182 const float rmax = halfSize + 0.7071F;
1183 const float rmin2 = MAX2(0.0F, rmin * rmin);
1184 const float rmax2 = rmax * rmax;
1185 const float cscale = 1.0F / (rmax2 - rmin2);
1186 int ix, iy;
1187
1188 for (iy = iymin; iy <= iymax; iy += 2) {
1189 for (ix = ixmin; ix <= ixmax; ix += 2) {
1190 float dx, dy, dist2, cover;
1191
1192 setup->quad[0].inout.mask = 0x0;
1193
1194 dx = (ix + 0.5f) - x;
1195 dy = (iy + 0.5f) - y;
1196 dist2 = dx * dx + dy * dy;
1197 if (dist2 <= rmax2) {
1198 cover = 1.0F - (dist2 - rmin2) * cscale;
1199 setup->quad[0].input.coverage[QUAD_TOP_LEFT] = MIN2(cover, 1.0f);
1200 setup->quad[0].inout.mask |= MASK_TOP_LEFT;
1201 }
1202
1203 dx = (ix + 1.5f) - x;
1204 dy = (iy + 0.5f) - y;
1205 dist2 = dx * dx + dy * dy;
1206 if (dist2 <= rmax2) {
1207 cover = 1.0F - (dist2 - rmin2) * cscale;
1208 setup->quad[0].input.coverage[QUAD_TOP_RIGHT] = MIN2(cover, 1.0f);
1209 setup->quad[0].inout.mask |= MASK_TOP_RIGHT;
1210 }
1211
1212 dx = (ix + 0.5f) - x;
1213 dy = (iy + 1.5f) - y;
1214 dist2 = dx * dx + dy * dy;
1215 if (dist2 <= rmax2) {
1216 cover = 1.0F - (dist2 - rmin2) * cscale;
1217 setup->quad[0].input.coverage[QUAD_BOTTOM_LEFT] = MIN2(cover, 1.0f);
1218 setup->quad[0].inout.mask |= MASK_BOTTOM_LEFT;
1219 }
1220
1221 dx = (ix + 1.5f) - x;
1222 dy = (iy + 1.5f) - y;
1223 dist2 = dx * dx + dy * dy;
1224 if (dist2 <= rmax2) {
1225 cover = 1.0F - (dist2 - rmin2) * cscale;
1226 setup->quad[0].input.coverage[QUAD_BOTTOM_RIGHT] = MIN2(cover, 1.0f);
1227 setup->quad[0].inout.mask |= MASK_BOTTOM_RIGHT;
1228 }
1229
1230 if (setup->quad[0].inout.mask) {
1231 setup->quad[0].input.x0 = ix;
1232 setup->quad[0].input.y0 = iy;
1233 clip_emit_quad( setup, &setup->quad[0] );
1234 }
1235 }
1236 }
1237 }
1238 else {
1239 /* square points */
1240 const int xmin = (int) (x + 0.75 - halfSize);
1241 const int ymin = (int) (y + 0.25 - halfSize);
1242 const int xmax = xmin + (int) size;
1243 const int ymax = ymin + (int) size;
1244 /* XXX could apply scissor to xmin,ymin,xmax,ymax now */
1245 const int ixmin = block(xmin);
1246 const int ixmax = block(xmax - 1);
1247 const int iymin = block(ymin);
1248 const int iymax = block(ymax - 1);
1249 int ix, iy;
1250
1251 /*
1252 debug_printf("(%f, %f) -> X:%d..%d Y:%d..%d\n", x, y, xmin, xmax,ymin,ymax);
1253 */
1254 for (iy = iymin; iy <= iymax; iy += 2) {
1255 uint rowMask = 0xf;
1256 if (iy < ymin) {
1257 /* above the top edge */
1258 rowMask &= (MASK_BOTTOM_LEFT | MASK_BOTTOM_RIGHT);
1259 }
1260 if (iy + 1 >= ymax) {
1261 /* below the bottom edge */
1262 rowMask &= (MASK_TOP_LEFT | MASK_TOP_RIGHT);
1263 }
1264
1265 for (ix = ixmin; ix <= ixmax; ix += 2) {
1266 uint mask = rowMask;
1267
1268 if (ix < xmin) {
1269 /* fragment is past left edge of point, turn off left bits */
1270 mask &= (MASK_BOTTOM_RIGHT | MASK_TOP_RIGHT);
1271 }
1272 if (ix + 1 >= xmax) {
1273 /* past the right edge */
1274 mask &= (MASK_BOTTOM_LEFT | MASK_TOP_LEFT);
1275 }
1276
1277 setup->quad[0].inout.mask = mask;
1278 setup->quad[0].input.x0 = ix;
1279 setup->quad[0].input.y0 = iy;
1280 clip_emit_quad( setup, &setup->quad[0] );
1281 }
1282 }
1283 }
1284 }
1285 }
1286
1287 void sp_setup_prepare( struct setup_context *setup )
1288 {
1289 struct softpipe_context *sp = setup->softpipe;
1290
1291 if (sp->dirty) {
1292 softpipe_update_derived(sp);
1293 }
1294
1295 /* Note: nr_attrs is only used for debugging (vertex printing) */
1296 setup->nr_vertex_attrs = draw_num_shader_outputs(sp->draw);
1297
1298 sp->quad.first->begin( sp->quad.first );
1299
1300 if (sp->reduced_api_prim == PIPE_PRIM_TRIANGLES &&
1301 sp->rasterizer->fill_cw == PIPE_POLYGON_MODE_FILL &&
1302 sp->rasterizer->fill_ccw == PIPE_POLYGON_MODE_FILL) {
1303 /* we'll do culling */
1304 setup->winding = sp->rasterizer->cull_mode;
1305 }
1306 else {
1307 /* 'draw' will do culling */
1308 setup->winding = PIPE_WINDING_NONE;
1309 }
1310 }
1311
1312
1313
1314 void sp_setup_destroy_context( struct setup_context *setup )
1315 {
1316 FREE( setup );
1317 }
1318
1319
1320 /**
1321 * Create a new primitive setup/render stage.
1322 */
1323 struct setup_context *sp_setup_create_context( struct softpipe_context *softpipe )
1324 {
1325 struct setup_context *setup = CALLOC_STRUCT(setup_context);
1326 unsigned i;
1327
1328 setup->softpipe = softpipe;
1329
1330 for (i = 0; i < MAX_QUADS; i++) {
1331 setup->quad[i].coef = setup->coef;
1332 setup->quad[i].posCoef = &setup->posCoef;
1333 }
1334
1335 setup->span.left[0] = 1000000; /* greater than right[0] */
1336 setup->span.left[1] = 1000000; /* greater than right[1] */
1337
1338 return setup;
1339 }
1340