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