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