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