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