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