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