softpipe: don't abuse the draw vertex_info struct for something different
[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 sp_setup_info *sinfo = &softpipe->setup_info;
603 uint fragSlot;
604 float v[3];
605
606 assert(sinfo->valid);
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 = sinfo->attrib[fragSlot].src_index;
624 uint j;
625
626 switch (sinfo->attrib[fragSlot].interp) {
627 case INTERP_CONSTANT:
628 for (j = 0; j < TGSI_NUM_CHANNELS; j++) {
629 const_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
630 }
631 break;
632 case INTERP_LINEAR:
633 for (j = 0; j < TGSI_NUM_CHANNELS; j++) {
634 tri_apply_cylindrical_wrap(setup->vmin[vertSlot][j],
635 setup->vmid[vertSlot][j],
636 setup->vmax[vertSlot][j],
637 fsInfo->input_cylindrical_wrap[fragSlot] & (1 << j),
638 v);
639 tri_linear_coeff(setup, &setup->coef[fragSlot], j, v);
640 }
641 break;
642 case INTERP_PERSPECTIVE:
643 for (j = 0; j < TGSI_NUM_CHANNELS; j++) {
644 tri_apply_cylindrical_wrap(setup->vmin[vertSlot][j],
645 setup->vmid[vertSlot][j],
646 setup->vmax[vertSlot][j],
647 fsInfo->input_cylindrical_wrap[fragSlot] & (1 << j),
648 v);
649 tri_persp_coeff(setup, &setup->coef[fragSlot], j, v);
650 }
651 break;
652 case INTERP_POS:
653 setup_fragcoord_coeff(setup, fragSlot);
654 break;
655 default:
656 assert(0);
657 }
658
659 if (fsInfo->input_semantic_name[fragSlot] == TGSI_SEMANTIC_FACE) {
660 /* convert 0 to 1.0 and 1 to -1.0 */
661 setup->coef[fragSlot].a0[0] = setup->facing * -2.0f + 1.0f;
662 setup->coef[fragSlot].dadx[0] = 0.0;
663 setup->coef[fragSlot].dady[0] = 0.0;
664 }
665
666 if (0) {
667 for (j = 0; j < TGSI_NUM_CHANNELS; j++) {
668 debug_printf("attr[%d].%c: a0:%f dx:%f dy:%f\n",
669 fragSlot, "xyzw"[j],
670 setup->coef[fragSlot].a0[j],
671 setup->coef[fragSlot].dadx[j],
672 setup->coef[fragSlot].dady[j]);
673 }
674 }
675 }
676 }
677
678
679 static void
680 setup_tri_edges(struct setup_context *setup)
681 {
682 float vmin_x = setup->vmin[0][0] + setup->pixel_offset;
683 float vmid_x = setup->vmid[0][0] + setup->pixel_offset;
684
685 float vmin_y = setup->vmin[0][1] - setup->pixel_offset;
686 float vmid_y = setup->vmid[0][1] - setup->pixel_offset;
687 float vmax_y = setup->vmax[0][1] - setup->pixel_offset;
688
689 setup->emaj.sy = ceilf(vmin_y);
690 setup->emaj.lines = (int) ceilf(vmax_y - setup->emaj.sy);
691 setup->emaj.dxdy = setup->emaj.dy ? setup->emaj.dx / setup->emaj.dy : .0f;
692 setup->emaj.sx = vmin_x + (setup->emaj.sy - vmin_y) * setup->emaj.dxdy;
693
694 setup->etop.sy = ceilf(vmid_y);
695 setup->etop.lines = (int) ceilf(vmax_y - setup->etop.sy);
696 setup->etop.dxdy = setup->etop.dy ? setup->etop.dx / setup->etop.dy : .0f;
697 setup->etop.sx = vmid_x + (setup->etop.sy - vmid_y) * setup->etop.dxdy;
698
699 setup->ebot.sy = ceilf(vmin_y);
700 setup->ebot.lines = (int) ceilf(vmid_y - setup->ebot.sy);
701 setup->ebot.dxdy = setup->ebot.dy ? setup->ebot.dx / setup->ebot.dy : .0f;
702 setup->ebot.sx = vmin_x + (setup->ebot.sy - vmin_y) * setup->ebot.dxdy;
703 }
704
705
706 /**
707 * Render the upper or lower half of a triangle.
708 * Scissoring/cliprect is applied here too.
709 */
710 static void
711 subtriangle(struct setup_context *setup,
712 struct edge *eleft,
713 struct edge *eright,
714 int lines,
715 unsigned viewport_index)
716 {
717 const struct pipe_scissor_state *cliprect = &setup->softpipe->cliprect[viewport_index];
718 const int minx = (int) cliprect->minx;
719 const int maxx = (int) cliprect->maxx;
720 const int miny = (int) cliprect->miny;
721 const int maxy = (int) cliprect->maxy;
722 int y, start_y, finish_y;
723 int sy = (int)eleft->sy;
724
725 assert((int)eleft->sy == (int) eright->sy);
726 assert(lines >= 0);
727
728 /* clip top/bottom */
729 start_y = sy;
730 if (start_y < miny)
731 start_y = miny;
732
733 finish_y = sy + lines;
734 if (finish_y > maxy)
735 finish_y = maxy;
736
737 start_y -= sy;
738 finish_y -= sy;
739
740 /*
741 debug_printf("%s %d %d\n", __FUNCTION__, start_y, finish_y);
742 */
743
744 for (y = start_y; y < finish_y; y++) {
745
746 /* avoid accumulating adds as floats don't have the precision to
747 * accurately iterate large triangle edges that way. luckily we
748 * can just multiply these days.
749 *
750 * this is all drowned out by the attribute interpolation anyway.
751 */
752 int left = (int)(eleft->sx + y * eleft->dxdy);
753 int right = (int)(eright->sx + y * eright->dxdy);
754
755 /* clip left/right */
756 if (left < minx)
757 left = minx;
758 if (right > maxx)
759 right = maxx;
760
761 if (left < right) {
762 int _y = sy + y;
763 if (block(_y) != setup->span.y) {
764 flush_spans(setup);
765 setup->span.y = block(_y);
766 }
767
768 setup->span.left[_y&1] = left;
769 setup->span.right[_y&1] = right;
770 }
771 }
772
773
774 /* save the values so that emaj can be restarted:
775 */
776 eleft->sx += lines * eleft->dxdy;
777 eright->sx += lines * eright->dxdy;
778 eleft->sy += lines;
779 eright->sy += lines;
780 }
781
782
783 /**
784 * Recalculate prim's determinant. This is needed as we don't have
785 * get this information through the vbuf_render interface & we must
786 * calculate it here.
787 */
788 static float
789 calc_det(const float (*v0)[4],
790 const float (*v1)[4],
791 const float (*v2)[4])
792 {
793 /* edge vectors e = v0 - v2, f = v1 - v2 */
794 const float ex = v0[0][0] - v2[0][0];
795 const float ey = v0[0][1] - v2[0][1];
796 const float fx = v1[0][0] - v2[0][0];
797 const float fy = v1[0][1] - v2[0][1];
798
799 /* det = cross(e,f).z */
800 return ex * fy - ey * fx;
801 }
802
803
804 /**
805 * Do setup for triangle rasterization, then render the triangle.
806 */
807 void
808 sp_setup_tri(struct setup_context *setup,
809 const float (*v0)[4],
810 const float (*v1)[4],
811 const float (*v2)[4])
812 {
813 float det;
814 uint layer = 0;
815 unsigned viewport_index = 0;
816 #if DEBUG_VERTS
817 debug_printf("Setup triangle:\n");
818 print_vertex(setup, v0);
819 print_vertex(setup, v1);
820 print_vertex(setup, v2);
821 #endif
822
823 if (setup->softpipe->no_rast || setup->softpipe->rasterizer->rasterizer_discard)
824 return;
825
826 det = calc_det(v0, v1, v2);
827 /*
828 debug_printf("%s\n", __FUNCTION__ );
829 */
830
831 #if DEBUG_FRAGS
832 setup->numFragsEmitted = 0;
833 setup->numFragsWritten = 0;
834 #endif
835
836 if (!setup_sort_vertices( setup, det, v0, v1, v2 ))
837 return;
838
839 setup_tri_coefficients( setup );
840 setup_tri_edges( setup );
841
842 assert(setup->softpipe->reduced_prim == PIPE_PRIM_TRIANGLES);
843
844 setup->span.y = 0;
845 setup->span.right[0] = 0;
846 setup->span.right[1] = 0;
847 /* setup->span.z_mode = tri_z_mode( setup->ctx ); */
848 if (setup->softpipe->layer_slot > 0) {
849 layer = *(unsigned *)setup->vprovoke[setup->softpipe->layer_slot];
850 layer = MIN2(layer, setup->max_layer);
851 }
852 setup->quad[0].input.layer = layer;
853
854 if (setup->softpipe->viewport_index_slot > 0) {
855 unsigned *udata = (unsigned*)v0[setup->softpipe->viewport_index_slot];
856 viewport_index = sp_clamp_viewport_idx(*udata);
857 }
858 setup->quad[0].input.viewport_index = viewport_index;
859
860 /* init_constant_attribs( setup ); */
861
862 if (setup->oneoverarea < 0.0) {
863 /* emaj on left:
864 */
865 subtriangle(setup, &setup->emaj, &setup->ebot, setup->ebot.lines, viewport_index);
866 subtriangle(setup, &setup->emaj, &setup->etop, setup->etop.lines, viewport_index);
867 }
868 else {
869 /* emaj on right:
870 */
871 subtriangle(setup, &setup->ebot, &setup->emaj, setup->ebot.lines, viewport_index);
872 subtriangle(setup, &setup->etop, &setup->emaj, setup->etop.lines, viewport_index);
873 }
874
875 flush_spans( setup );
876
877 if (setup->softpipe->active_statistics_queries) {
878 setup->softpipe->pipeline_statistics.c_primitives++;
879 }
880
881 #if DEBUG_FRAGS
882 printf("Tri: %u frags emitted, %u written\n",
883 setup->numFragsEmitted,
884 setup->numFragsWritten);
885 #endif
886 }
887
888
889 /* Apply cylindrical wrapping to v0, v1 coordinates, if enabled.
890 * Input coordinates must be in [0, 1] range, otherwise results are undefined.
891 */
892 static void
893 line_apply_cylindrical_wrap(float v0,
894 float v1,
895 uint cylindrical_wrap,
896 float output[2])
897 {
898 if (cylindrical_wrap) {
899 float delta;
900
901 delta = v1 - v0;
902 if (delta > 0.5f) {
903 v0 += 1.0f;
904 }
905 else if (delta < -0.5f) {
906 v1 += 1.0f;
907 }
908 }
909
910 output[0] = v0;
911 output[1] = v1;
912 }
913
914
915 /**
916 * Compute a0, dadx and dady for a linearly interpolated coefficient,
917 * for a line.
918 * v[0] and v[1] are vmin and vmax, respectively.
919 */
920 static void
921 line_linear_coeff(const struct setup_context *setup,
922 struct tgsi_interp_coef *coef,
923 uint i,
924 const float v[2])
925 {
926 const float da = v[1] - v[0];
927 const float dadx = da * setup->emaj.dx * setup->oneoverarea;
928 const float dady = da * setup->emaj.dy * setup->oneoverarea;
929 coef->dadx[i] = dadx;
930 coef->dady[i] = dady;
931 coef->a0[i] = (v[0] -
932 (dadx * (setup->vmin[0][0] - setup->pixel_offset) +
933 dady * (setup->vmin[0][1] - setup->pixel_offset)));
934 }
935
936
937 /**
938 * Compute a0, dadx and dady for a perspective-corrected interpolant,
939 * for a line.
940 * v[0] and v[1] are vmin and vmax, respectively.
941 */
942 static void
943 line_persp_coeff(const struct setup_context *setup,
944 struct tgsi_interp_coef *coef,
945 uint i,
946 const float v[2])
947 {
948 const float a0 = v[0] * setup->vmin[0][3];
949 const float a1 = v[1] * setup->vmax[0][3];
950 const float da = a1 - a0;
951 const float dadx = da * setup->emaj.dx * setup->oneoverarea;
952 const float dady = da * setup->emaj.dy * setup->oneoverarea;
953 coef->dadx[i] = dadx;
954 coef->dady[i] = dady;
955 coef->a0[i] = (a0 -
956 (dadx * (setup->vmin[0][0] - setup->pixel_offset) +
957 dady * (setup->vmin[0][1] - setup->pixel_offset)));
958 }
959
960
961 /**
962 * Compute the setup->coef[] array dadx, dady, a0 values.
963 * Must be called after setup->vmin,vmax are initialized.
964 */
965 static boolean
966 setup_line_coefficients(struct setup_context *setup,
967 const float (*v0)[4],
968 const float (*v1)[4])
969 {
970 struct softpipe_context *softpipe = setup->softpipe;
971 const struct tgsi_shader_info *fsInfo = &setup->softpipe->fs_variant->info;
972 const struct sp_setup_info *sinfo = &softpipe->setup_info;
973 uint fragSlot;
974 float area;
975 float v[2];
976
977 assert(sinfo->valid);
978
979 /* use setup->vmin, vmax to point to vertices */
980 if (softpipe->rasterizer->flatshade_first)
981 setup->vprovoke = v0;
982 else
983 setup->vprovoke = v1;
984 setup->vmin = v0;
985 setup->vmax = v1;
986
987 setup->emaj.dx = setup->vmax[0][0] - setup->vmin[0][0];
988 setup->emaj.dy = setup->vmax[0][1] - setup->vmin[0][1];
989
990 /* NOTE: this is not really area but something proportional to it */
991 area = setup->emaj.dx * setup->emaj.dx + setup->emaj.dy * setup->emaj.dy;
992 if (area == 0.0f || util_is_inf_or_nan(area))
993 return FALSE;
994 setup->oneoverarea = 1.0f / area;
995
996 /* z and w are done by linear interpolation:
997 */
998 v[0] = setup->vmin[0][2];
999 v[1] = setup->vmax[0][2];
1000 line_linear_coeff(setup, &setup->posCoef, 2, v);
1001
1002 v[0] = setup->vmin[0][3];
1003 v[1] = setup->vmax[0][3];
1004 line_linear_coeff(setup, &setup->posCoef, 3, v);
1005
1006 /* setup interpolation for all the remaining attributes:
1007 */
1008 for (fragSlot = 0; fragSlot < fsInfo->num_inputs; fragSlot++) {
1009 const uint vertSlot = sinfo->attrib[fragSlot].src_index;
1010 uint j;
1011
1012 switch (sinfo->attrib[fragSlot].interp) {
1013 case INTERP_CONSTANT:
1014 for (j = 0; j < TGSI_NUM_CHANNELS; j++)
1015 const_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
1016 break;
1017 case INTERP_LINEAR:
1018 for (j = 0; j < TGSI_NUM_CHANNELS; j++) {
1019 line_apply_cylindrical_wrap(setup->vmin[vertSlot][j],
1020 setup->vmax[vertSlot][j],
1021 fsInfo->input_cylindrical_wrap[fragSlot] & (1 << j),
1022 v);
1023 line_linear_coeff(setup, &setup->coef[fragSlot], j, v);
1024 }
1025 break;
1026 case INTERP_PERSPECTIVE:
1027 for (j = 0; j < TGSI_NUM_CHANNELS; j++) {
1028 line_apply_cylindrical_wrap(setup->vmin[vertSlot][j],
1029 setup->vmax[vertSlot][j],
1030 fsInfo->input_cylindrical_wrap[fragSlot] & (1 << j),
1031 v);
1032 line_persp_coeff(setup, &setup->coef[fragSlot], j, v);
1033 }
1034 break;
1035 case INTERP_POS:
1036 setup_fragcoord_coeff(setup, fragSlot);
1037 break;
1038 default:
1039 assert(0);
1040 }
1041
1042 if (fsInfo->input_semantic_name[fragSlot] == TGSI_SEMANTIC_FACE) {
1043 /* convert 0 to 1.0 and 1 to -1.0 */
1044 setup->coef[fragSlot].a0[0] = setup->facing * -2.0f + 1.0f;
1045 setup->coef[fragSlot].dadx[0] = 0.0;
1046 setup->coef[fragSlot].dady[0] = 0.0;
1047 }
1048 }
1049 return TRUE;
1050 }
1051
1052
1053 /**
1054 * Plot a pixel in a line segment.
1055 */
1056 static inline void
1057 plot(struct setup_context *setup, int x, int y)
1058 {
1059 const int iy = y & 1;
1060 const int ix = x & 1;
1061 const int quadX = x - ix;
1062 const int quadY = y - iy;
1063 const int mask = (1 << ix) << (2 * iy);
1064
1065 if (quadX != setup->quad[0].input.x0 ||
1066 quadY != setup->quad[0].input.y0)
1067 {
1068 /* flush prev quad, start new quad */
1069
1070 if (setup->quad[0].input.x0 != -1)
1071 clip_emit_quad(setup, &setup->quad[0]);
1072
1073 setup->quad[0].input.x0 = quadX;
1074 setup->quad[0].input.y0 = quadY;
1075 setup->quad[0].inout.mask = 0x0;
1076 }
1077
1078 setup->quad[0].inout.mask |= mask;
1079 }
1080
1081
1082 /**
1083 * Do setup for line rasterization, then render the line.
1084 * Single-pixel width, no stipple, etc. We rely on the 'draw' module
1085 * to handle stippling and wide lines.
1086 */
1087 void
1088 sp_setup_line(struct setup_context *setup,
1089 const float (*v0)[4],
1090 const float (*v1)[4])
1091 {
1092 int x0 = (int) v0[0][0];
1093 int x1 = (int) v1[0][0];
1094 int y0 = (int) v0[0][1];
1095 int y1 = (int) v1[0][1];
1096 int dx = x1 - x0;
1097 int dy = y1 - y0;
1098 int xstep, ystep;
1099 uint layer = 0;
1100 unsigned viewport_index = 0;
1101
1102 #if DEBUG_VERTS
1103 debug_printf("Setup line:\n");
1104 print_vertex(setup, v0);
1105 print_vertex(setup, v1);
1106 #endif
1107
1108 if (setup->softpipe->no_rast || setup->softpipe->rasterizer->rasterizer_discard)
1109 return;
1110
1111 if (dx == 0 && dy == 0)
1112 return;
1113
1114 if (!setup_line_coefficients(setup, v0, v1))
1115 return;
1116
1117 assert(v0[0][0] < 1.0e9);
1118 assert(v0[0][1] < 1.0e9);
1119 assert(v1[0][0] < 1.0e9);
1120 assert(v1[0][1] < 1.0e9);
1121
1122 if (dx < 0) {
1123 dx = -dx; /* make positive */
1124 xstep = -1;
1125 }
1126 else {
1127 xstep = 1;
1128 }
1129
1130 if (dy < 0) {
1131 dy = -dy; /* make positive */
1132 ystep = -1;
1133 }
1134 else {
1135 ystep = 1;
1136 }
1137
1138 assert(dx >= 0);
1139 assert(dy >= 0);
1140 assert(setup->softpipe->reduced_prim == PIPE_PRIM_LINES);
1141
1142 setup->quad[0].input.x0 = setup->quad[0].input.y0 = -1;
1143 setup->quad[0].inout.mask = 0x0;
1144 if (setup->softpipe->layer_slot > 0) {
1145 layer = *(unsigned *)setup->vprovoke[setup->softpipe->layer_slot];
1146 layer = MIN2(layer, setup->max_layer);
1147 }
1148 setup->quad[0].input.layer = layer;
1149
1150 if (setup->softpipe->viewport_index_slot > 0) {
1151 unsigned *udata = (unsigned*)setup->vprovoke[setup->softpipe->viewport_index_slot];
1152 viewport_index = sp_clamp_viewport_idx(*udata);
1153 }
1154 setup->quad[0].input.viewport_index = viewport_index;
1155
1156 /* XXX temporary: set coverage to 1.0 so the line appears
1157 * if AA mode happens to be enabled.
1158 */
1159 setup->quad[0].input.coverage[0] =
1160 setup->quad[0].input.coverage[1] =
1161 setup->quad[0].input.coverage[2] =
1162 setup->quad[0].input.coverage[3] = 1.0;
1163
1164 if (dx > dy) {
1165 /*** X-major line ***/
1166 int i;
1167 const int errorInc = dy + dy;
1168 int error = errorInc - dx;
1169 const int errorDec = error - dx;
1170
1171 for (i = 0; i < dx; i++) {
1172 plot(setup, x0, y0);
1173
1174 x0 += xstep;
1175 if (error < 0) {
1176 error += errorInc;
1177 }
1178 else {
1179 error += errorDec;
1180 y0 += ystep;
1181 }
1182 }
1183 }
1184 else {
1185 /*** Y-major line ***/
1186 int i;
1187 const int errorInc = dx + dx;
1188 int error = errorInc - dy;
1189 const int errorDec = error - dy;
1190
1191 for (i = 0; i < dy; i++) {
1192 plot(setup, x0, y0);
1193
1194 y0 += ystep;
1195 if (error < 0) {
1196 error += errorInc;
1197 }
1198 else {
1199 error += errorDec;
1200 x0 += xstep;
1201 }
1202 }
1203 }
1204
1205 /* draw final quad */
1206 if (setup->quad[0].inout.mask) {
1207 clip_emit_quad(setup, &setup->quad[0]);
1208 }
1209 }
1210
1211
1212 static void
1213 point_persp_coeff(const struct setup_context *setup,
1214 const float (*vert)[4],
1215 struct tgsi_interp_coef *coef,
1216 uint vertSlot, uint i)
1217 {
1218 assert(i <= 3);
1219 coef->dadx[i] = 0.0F;
1220 coef->dady[i] = 0.0F;
1221 coef->a0[i] = vert[vertSlot][i] * vert[0][3];
1222 }
1223
1224
1225 /**
1226 * Do setup for point rasterization, then render the point.
1227 * Round or square points...
1228 * XXX could optimize a lot for 1-pixel points.
1229 */
1230 void
1231 sp_setup_point(struct setup_context *setup,
1232 const float (*v0)[4])
1233 {
1234 struct softpipe_context *softpipe = setup->softpipe;
1235 const struct tgsi_shader_info *fsInfo = &setup->softpipe->fs_variant->info;
1236 const int sizeAttr = setup->softpipe->psize_slot;
1237 const float size
1238 = sizeAttr > 0 ? v0[sizeAttr][0]
1239 : setup->softpipe->rasterizer->point_size;
1240 const float halfSize = 0.5F * size;
1241 const boolean round = (boolean) setup->softpipe->rasterizer->point_smooth;
1242 const float x = v0[0][0]; /* Note: data[0] is always position */
1243 const float y = v0[0][1];
1244 const struct sp_setup_info *sinfo = &softpipe->setup_info;
1245 uint fragSlot;
1246 uint layer = 0;
1247 unsigned viewport_index = 0;
1248 #if DEBUG_VERTS
1249 debug_printf("Setup point:\n");
1250 print_vertex(setup, v0);
1251 #endif
1252
1253 assert(sinfo->valid);
1254
1255 if (setup->softpipe->no_rast || setup->softpipe->rasterizer->rasterizer_discard)
1256 return;
1257
1258 assert(setup->softpipe->reduced_prim == PIPE_PRIM_POINTS);
1259
1260 if (setup->softpipe->layer_slot > 0) {
1261 layer = *(unsigned *)v0[setup->softpipe->layer_slot];
1262 layer = MIN2(layer, setup->max_layer);
1263 }
1264 setup->quad[0].input.layer = layer;
1265
1266 if (setup->softpipe->viewport_index_slot > 0) {
1267 unsigned *udata = (unsigned*)v0[setup->softpipe->viewport_index_slot];
1268 viewport_index = sp_clamp_viewport_idx(*udata);
1269 }
1270 setup->quad[0].input.viewport_index = viewport_index;
1271
1272 /* For points, all interpolants are constant-valued.
1273 * However, for point sprites, we'll need to setup texcoords appropriately.
1274 * XXX: which coefficients are the texcoords???
1275 * We may do point sprites as textured quads...
1276 *
1277 * KW: We don't know which coefficients are texcoords - ultimately
1278 * the choice of what interpolation mode to use for each attribute
1279 * should be determined by the fragment program, using
1280 * per-attribute declaration statements that include interpolation
1281 * mode as a parameter. So either the fragment program will have
1282 * to be adjusted for pointsprite vs normal point behaviour, or
1283 * otherwise a special interpolation mode will have to be defined
1284 * which matches the required behaviour for point sprites. But -
1285 * the latter is not a feature of normal hardware, and as such
1286 * probably should be ruled out on that basis.
1287 */
1288 setup->vprovoke = v0;
1289
1290 /* setup Z, W */
1291 const_coeff(setup, &setup->posCoef, 0, 2);
1292 const_coeff(setup, &setup->posCoef, 0, 3);
1293
1294 for (fragSlot = 0; fragSlot < fsInfo->num_inputs; fragSlot++) {
1295 const uint vertSlot = sinfo->attrib[fragSlot].src_index;
1296 uint j;
1297
1298 switch (sinfo->attrib[fragSlot].interp) {
1299 case INTERP_CONSTANT:
1300 /* fall-through */
1301 case INTERP_LINEAR:
1302 for (j = 0; j < TGSI_NUM_CHANNELS; j++)
1303 const_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
1304 break;
1305 case INTERP_PERSPECTIVE:
1306 for (j = 0; j < TGSI_NUM_CHANNELS; j++)
1307 point_persp_coeff(setup, setup->vprovoke,
1308 &setup->coef[fragSlot], vertSlot, j);
1309 break;
1310 case INTERP_POS:
1311 setup_fragcoord_coeff(setup, fragSlot);
1312 break;
1313 default:
1314 assert(0);
1315 }
1316
1317 if (fsInfo->input_semantic_name[fragSlot] == TGSI_SEMANTIC_FACE) {
1318 /* convert 0 to 1.0 and 1 to -1.0 */
1319 setup->coef[fragSlot].a0[0] = setup->facing * -2.0f + 1.0f;
1320 setup->coef[fragSlot].dadx[0] = 0.0;
1321 setup->coef[fragSlot].dady[0] = 0.0;
1322 }
1323 }
1324
1325
1326 if (halfSize <= 0.5 && !round) {
1327 /* special case for 1-pixel points */
1328 const int ix = ((int) x) & 1;
1329 const int iy = ((int) y) & 1;
1330 setup->quad[0].input.x0 = (int) x - ix;
1331 setup->quad[0].input.y0 = (int) y - iy;
1332 setup->quad[0].inout.mask = (1 << ix) << (2 * iy);
1333 clip_emit_quad(setup, &setup->quad[0]);
1334 }
1335 else {
1336 if (round) {
1337 /* rounded points */
1338 const int ixmin = block((int) (x - halfSize));
1339 const int ixmax = block((int) (x + halfSize));
1340 const int iymin = block((int) (y - halfSize));
1341 const int iymax = block((int) (y + halfSize));
1342 const float rmin = halfSize - 0.7071F; /* 0.7071 = sqrt(2)/2 */
1343 const float rmax = halfSize + 0.7071F;
1344 const float rmin2 = MAX2(0.0F, rmin * rmin);
1345 const float rmax2 = rmax * rmax;
1346 const float cscale = 1.0F / (rmax2 - rmin2);
1347 int ix, iy;
1348
1349 for (iy = iymin; iy <= iymax; iy += 2) {
1350 for (ix = ixmin; ix <= ixmax; ix += 2) {
1351 float dx, dy, dist2, cover;
1352
1353 setup->quad[0].inout.mask = 0x0;
1354
1355 dx = (ix + 0.5f) - x;
1356 dy = (iy + 0.5f) - y;
1357 dist2 = dx * dx + dy * dy;
1358 if (dist2 <= rmax2) {
1359 cover = 1.0F - (dist2 - rmin2) * cscale;
1360 setup->quad[0].input.coverage[QUAD_TOP_LEFT] = MIN2(cover, 1.0f);
1361 setup->quad[0].inout.mask |= MASK_TOP_LEFT;
1362 }
1363
1364 dx = (ix + 1.5f) - x;
1365 dy = (iy + 0.5f) - y;
1366 dist2 = dx * dx + dy * dy;
1367 if (dist2 <= rmax2) {
1368 cover = 1.0F - (dist2 - rmin2) * cscale;
1369 setup->quad[0].input.coverage[QUAD_TOP_RIGHT] = MIN2(cover, 1.0f);
1370 setup->quad[0].inout.mask |= MASK_TOP_RIGHT;
1371 }
1372
1373 dx = (ix + 0.5f) - x;
1374 dy = (iy + 1.5f) - y;
1375 dist2 = dx * dx + dy * dy;
1376 if (dist2 <= rmax2) {
1377 cover = 1.0F - (dist2 - rmin2) * cscale;
1378 setup->quad[0].input.coverage[QUAD_BOTTOM_LEFT] = MIN2(cover, 1.0f);
1379 setup->quad[0].inout.mask |= MASK_BOTTOM_LEFT;
1380 }
1381
1382 dx = (ix + 1.5f) - x;
1383 dy = (iy + 1.5f) - y;
1384 dist2 = dx * dx + dy * dy;
1385 if (dist2 <= rmax2) {
1386 cover = 1.0F - (dist2 - rmin2) * cscale;
1387 setup->quad[0].input.coverage[QUAD_BOTTOM_RIGHT] = MIN2(cover, 1.0f);
1388 setup->quad[0].inout.mask |= MASK_BOTTOM_RIGHT;
1389 }
1390
1391 if (setup->quad[0].inout.mask) {
1392 setup->quad[0].input.x0 = ix;
1393 setup->quad[0].input.y0 = iy;
1394 clip_emit_quad(setup, &setup->quad[0]);
1395 }
1396 }
1397 }
1398 }
1399 else {
1400 /* square points */
1401 const int xmin = (int) (x + 0.75 - halfSize);
1402 const int ymin = (int) (y + 0.25 - halfSize);
1403 const int xmax = xmin + (int) size;
1404 const int ymax = ymin + (int) size;
1405 /* XXX could apply scissor to xmin,ymin,xmax,ymax now */
1406 const int ixmin = block(xmin);
1407 const int ixmax = block(xmax - 1);
1408 const int iymin = block(ymin);
1409 const int iymax = block(ymax - 1);
1410 int ix, iy;
1411
1412 /*
1413 debug_printf("(%f, %f) -> X:%d..%d Y:%d..%d\n", x, y, xmin, xmax,ymin,ymax);
1414 */
1415 for (iy = iymin; iy <= iymax; iy += 2) {
1416 uint rowMask = 0xf;
1417 if (iy < ymin) {
1418 /* above the top edge */
1419 rowMask &= (MASK_BOTTOM_LEFT | MASK_BOTTOM_RIGHT);
1420 }
1421 if (iy + 1 >= ymax) {
1422 /* below the bottom edge */
1423 rowMask &= (MASK_TOP_LEFT | MASK_TOP_RIGHT);
1424 }
1425
1426 for (ix = ixmin; ix <= ixmax; ix += 2) {
1427 uint mask = rowMask;
1428
1429 if (ix < xmin) {
1430 /* fragment is past left edge of point, turn off left bits */
1431 mask &= (MASK_BOTTOM_RIGHT | MASK_TOP_RIGHT);
1432 }
1433 if (ix + 1 >= xmax) {
1434 /* past the right edge */
1435 mask &= (MASK_BOTTOM_LEFT | MASK_TOP_LEFT);
1436 }
1437
1438 setup->quad[0].inout.mask = mask;
1439 setup->quad[0].input.x0 = ix;
1440 setup->quad[0].input.y0 = iy;
1441 clip_emit_quad(setup, &setup->quad[0]);
1442 }
1443 }
1444 }
1445 }
1446 }
1447
1448
1449 /**
1450 * Called by vbuf code just before we start buffering primitives.
1451 */
1452 void
1453 sp_setup_prepare(struct setup_context *setup)
1454 {
1455 struct softpipe_context *sp = setup->softpipe;
1456 int i;
1457 unsigned max_layer = ~0;
1458 if (sp->dirty) {
1459 softpipe_update_derived(sp, sp->reduced_api_prim);
1460 }
1461
1462 /* Note: nr_attrs is only used for debugging (vertex printing) */
1463 setup->nr_vertex_attrs = draw_num_shader_outputs(sp->draw);
1464
1465 /*
1466 * Determine how many layers the fb has (used for clamping layer value).
1467 * OpenGL (but not d3d10) permits different amount of layers per rt, however
1468 * results are undefined if layer exceeds the amount of layers of ANY
1469 * attachment hence don't need separate per cbuf and zsbuf max.
1470 */
1471 for (i = 0; i < setup->softpipe->framebuffer.nr_cbufs; i++) {
1472 struct pipe_surface *cbuf = setup->softpipe->framebuffer.cbufs[i];
1473 if (cbuf) {
1474 max_layer = MIN2(max_layer,
1475 cbuf->u.tex.last_layer - cbuf->u.tex.first_layer);
1476
1477 }
1478 }
1479
1480 setup->max_layer = max_layer;
1481
1482 sp->quad.first->begin( sp->quad.first );
1483
1484 if (sp->reduced_api_prim == PIPE_PRIM_TRIANGLES &&
1485 sp->rasterizer->fill_front == PIPE_POLYGON_MODE_FILL &&
1486 sp->rasterizer->fill_back == PIPE_POLYGON_MODE_FILL) {
1487 /* we'll do culling */
1488 setup->cull_face = sp->rasterizer->cull_face;
1489 }
1490 else {
1491 /* 'draw' will do culling */
1492 setup->cull_face = PIPE_FACE_NONE;
1493 }
1494 }
1495
1496
1497 void
1498 sp_setup_destroy_context(struct setup_context *setup)
1499 {
1500 FREE( setup );
1501 }
1502
1503
1504 /**
1505 * Create a new primitive setup/render stage.
1506 */
1507 struct setup_context *
1508 sp_setup_create_context(struct softpipe_context *softpipe)
1509 {
1510 struct setup_context *setup = CALLOC_STRUCT(setup_context);
1511 unsigned i;
1512
1513 setup->softpipe = softpipe;
1514
1515 for (i = 0; i < MAX_QUADS; i++) {
1516 setup->quad[i].coef = setup->coef;
1517 setup->quad[i].posCoef = &setup->posCoef;
1518 }
1519
1520 setup->span.left[0] = 1000000; /* greater than right[0] */
1521 setup->span.left[1] = 1000000; /* greater than right[1] */
1522
1523 return setup;
1524 }