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