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