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