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