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