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