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