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