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