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