Merge branch 'ext-provoking-vertex'
[profile/ivi/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_FOG) {
791          /* FOG.y = front/back facing  XXX fix this */
792          setup->coef[fragSlot].a0[1] = 1.0f - setup->quad.input.facing;
793          setup->coef[fragSlot].dadx[1] = 0.0;
794          setup->coef[fragSlot].dady[1] = 0.0;
795       }
796    }
797 }
798
799
800
801 static void setup_tri_edges( struct setup_context *setup )
802 {
803    float vmin_x = setup->vmin[0][0] + 0.5f;
804    float vmid_x = setup->vmid[0][0] + 0.5f;
805
806    float vmin_y = setup->vmin[0][1] - 0.5f;
807    float vmid_y = setup->vmid[0][1] - 0.5f;
808    float vmax_y = setup->vmax[0][1] - 0.5f;
809
810    setup->emaj.sy = ceilf(vmin_y);
811    setup->emaj.lines = (int) ceilf(vmax_y - setup->emaj.sy);
812    setup->emaj.dxdy = setup->emaj.dx / setup->emaj.dy;
813    setup->emaj.sx = vmin_x + (setup->emaj.sy - vmin_y) * setup->emaj.dxdy;
814
815    setup->etop.sy = ceilf(vmid_y);
816    setup->etop.lines = (int) ceilf(vmax_y - setup->etop.sy);
817    setup->etop.dxdy = setup->etop.dx / setup->etop.dy;
818    setup->etop.sx = vmid_x + (setup->etop.sy - vmid_y) * setup->etop.dxdy;
819
820    setup->ebot.sy = ceilf(vmin_y);
821    setup->ebot.lines = (int) ceilf(vmid_y - setup->ebot.sy);
822    setup->ebot.dxdy = setup->ebot.dx / setup->ebot.dy;
823    setup->ebot.sx = vmin_x + (setup->ebot.sy - vmin_y) * setup->ebot.dxdy;
824 }
825
826
827 /**
828  * Render the upper or lower half of a triangle.
829  * Scissoring/cliprect is applied here too.
830  */
831 static void subtriangle( struct setup_context *setup,
832                          struct edge *eleft,
833                          struct edge *eright,
834                          unsigned lines )
835 {
836    const struct pipe_scissor_state *cliprect = &setup->softpipe->cliprect;
837    const int minx = (int) cliprect->minx;
838    const int maxx = (int) cliprect->maxx;
839    const int miny = (int) cliprect->miny;
840    const int maxy = (int) cliprect->maxy;
841    int y, start_y, finish_y;
842    int sy = (int)eleft->sy;
843
844    assert((int)eleft->sy == (int) eright->sy);
845
846    /* clip top/bottom */
847    start_y = sy;
848    finish_y = sy + lines;
849
850    if (start_y < miny)
851       start_y = miny;
852
853    if (finish_y > maxy)
854       finish_y = maxy;
855
856    start_y -= sy;
857    finish_y -= sy;
858
859    /*
860    debug_printf("%s %d %d\n", __FUNCTION__, start_y, finish_y);
861    */
862
863    for (y = start_y; y < finish_y; y++) {
864
865       /* avoid accumulating adds as floats don't have the precision to
866        * accurately iterate large triangle edges that way.  luckily we
867        * can just multiply these days.
868        *
869        * this is all drowned out by the attribute interpolation anyway.
870        */
871       int left = (int)(eleft->sx + y * eleft->dxdy);
872       int right = (int)(eright->sx + y * eright->dxdy);
873
874       /* clip left/right */
875       if (left < minx)
876          left = minx;
877       if (right > maxx)
878          right = maxx;
879
880       if (left < right) {
881          int _y = sy + y;
882          if (block(_y) != setup->span.y) {
883             flush_spans(setup);
884             setup->span.y = block(_y);
885          }
886
887          setup->span.left[_y&1] = left;
888          setup->span.right[_y&1] = right;
889          setup->span.y_flags |= 1<<(_y&1);
890       }
891    }
892
893
894    /* save the values so that emaj can be restarted:
895     */
896    eleft->sx += lines * eleft->dxdy;
897    eright->sx += lines * eright->dxdy;
898    eleft->sy += lines;
899    eright->sy += lines;
900 }
901
902
903 /**
904  * Recalculate prim's determinant.  This is needed as we don't have
905  * get this information through the vbuf_render interface & we must
906  * calculate it here.
907  */
908 static float
909 calc_det( const float (*v0)[4],
910           const float (*v1)[4],
911           const float (*v2)[4] )
912 {
913    /* edge vectors e = v0 - v2, f = v1 - v2 */
914    const float ex = v0[0][0] - v2[0][0];
915    const float ey = v0[0][1] - v2[0][1];
916    const float fx = v1[0][0] - v2[0][0];
917    const float fy = v1[0][1] - v2[0][1];
918
919    /* det = cross(e,f).z */
920    return ex * fy - ey * fx;
921 }
922
923
924 /**
925  * Do setup for triangle rasterization, then render the triangle.
926  */
927 void setup_tri( struct setup_context *setup,
928                 const float (*v0)[4],
929                 const float (*v1)[4],
930                 const float (*v2)[4] )
931 {
932    float det;
933
934 #if DEBUG_VERTS
935    debug_printf("Setup triangle:\n");
936    print_vertex(setup, v0);
937    print_vertex(setup, v1);
938    print_vertex(setup, v2);
939 #endif
940
941    if (setup->softpipe->no_rast)
942       return;
943    
944    det = calc_det(v0, v1, v2);
945    /*
946    debug_printf("%s\n", __FUNCTION__ );
947    */
948
949 #if DEBUG_FRAGS
950    setup->numFragsEmitted = 0;
951    setup->numFragsWritten = 0;
952 #endif
953
954    if (cull_tri( setup, det ))
955       return;
956
957    if (!setup_sort_vertices( setup, det, v0, v1, v2 ))
958       return;
959    setup_tri_coefficients( setup );
960    setup_tri_edges( setup );
961
962    setup->quad.input.prim = QUAD_PRIM_TRI;
963
964    setup->span.y = 0;
965    setup->span.y_flags = 0;
966    setup->span.right[0] = 0;
967    setup->span.right[1] = 0;
968    /*   setup->span.z_mode = tri_z_mode( setup->ctx ); */
969
970    /*   init_constant_attribs( setup ); */
971
972    if (setup->oneoverarea < 0.0) {
973       /* emaj on left:
974        */
975       subtriangle( setup, &setup->emaj, &setup->ebot, setup->ebot.lines );
976       subtriangle( setup, &setup->emaj, &setup->etop, setup->etop.lines );
977    }
978    else {
979       /* emaj on right:
980        */
981       subtriangle( setup, &setup->ebot, &setup->emaj, setup->ebot.lines );
982       subtriangle( setup, &setup->etop, &setup->emaj, setup->etop.lines );
983    }
984
985    flush_spans( setup );
986
987    WAIT_FOR_COMPLETION(setup);
988
989 #if DEBUG_FRAGS
990    printf("Tri: %u frags emitted, %u written\n",
991           setup->numFragsEmitted,
992           setup->numFragsWritten);
993 #endif
994 }
995
996
997
998 /**
999  * Compute a0, dadx and dady for a linearly interpolated coefficient,
1000  * for a line.
1001  */
1002 static void
1003 line_linear_coeff(const struct setup_context *setup,
1004                   struct tgsi_interp_coef *coef,
1005                   uint vertSlot, uint i)
1006 {
1007    const float da = setup->vmax[vertSlot][i] - setup->vmin[vertSlot][i];
1008    const float dadx = da * setup->emaj.dx * setup->oneoverarea;
1009    const float dady = da * setup->emaj.dy * setup->oneoverarea;
1010    coef->dadx[i] = dadx;
1011    coef->dady[i] = dady;
1012    coef->a0[i] = (setup->vmin[vertSlot][i] -
1013                   (dadx * (setup->vmin[0][0] - 0.5f) +
1014                    dady * (setup->vmin[0][1] - 0.5f)));
1015 }
1016
1017
1018 /**
1019  * Compute a0, dadx and dady for a perspective-corrected interpolant,
1020  * for a line.
1021  */
1022 static void
1023 line_persp_coeff(const struct setup_context *setup,
1024                  struct tgsi_interp_coef *coef,
1025                  uint vertSlot, uint i)
1026 {
1027    /* XXX double-check/verify this arithmetic */
1028    const float a0 = setup->vmin[vertSlot][i] * setup->vmin[0][3];
1029    const float a1 = setup->vmax[vertSlot][i] * setup->vmax[0][3];
1030    const float da = a1 - a0;
1031    const float dadx = da * setup->emaj.dx * setup->oneoverarea;
1032    const float dady = da * setup->emaj.dy * setup->oneoverarea;
1033    coef->dadx[i] = dadx;
1034    coef->dady[i] = dady;
1035    coef->a0[i] = (setup->vmin[vertSlot][i] -
1036                   (dadx * (setup->vmin[0][0] - 0.5f) +
1037                    dady * (setup->vmin[0][1] - 0.5f)));
1038 }
1039
1040
1041 /**
1042  * Compute the setup->coef[] array dadx, dady, a0 values.
1043  * Must be called after setup->vmin,vmax are initialized.
1044  */
1045 static INLINE boolean
1046 setup_line_coefficients(struct setup_context *setup,
1047                         const float (*v0)[4],
1048                         const float (*v1)[4])
1049 {
1050    struct softpipe_context *softpipe = setup->softpipe;
1051    const struct sp_fragment_shader *spfs = softpipe->fs;
1052    const struct vertex_info *vinfo = softpipe_get_vertex_info(softpipe);
1053    uint fragSlot;
1054    float area;
1055
1056    /* use setup->vmin, vmax to point to vertices */
1057    if (softpipe->rasterizer->flatshade_first)
1058       setup->vprovoke = v0;
1059    else
1060       setup->vprovoke = v1;
1061    setup->vmin = v0;
1062    setup->vmax = v1;
1063
1064    setup->emaj.dx = setup->vmax[0][0] - setup->vmin[0][0];
1065    setup->emaj.dy = setup->vmax[0][1] - setup->vmin[0][1];
1066
1067    /* NOTE: this is not really area but something proportional to it */
1068    area = setup->emaj.dx * setup->emaj.dx + setup->emaj.dy * setup->emaj.dy;
1069    if (area == 0.0f || util_is_inf_or_nan(area))
1070       return FALSE;
1071    setup->oneoverarea = 1.0f / area;
1072
1073    /* z and w are done by linear interpolation:
1074     */
1075    line_linear_coeff(setup, &setup->posCoef, 0, 2);
1076    line_linear_coeff(setup, &setup->posCoef, 0, 3);
1077
1078    /* setup interpolation for all the remaining attributes:
1079     */
1080    for (fragSlot = 0; fragSlot < spfs->info.num_inputs; fragSlot++) {
1081       const uint vertSlot = vinfo->attrib[fragSlot].src_index;
1082       uint j;
1083
1084       switch (vinfo->attrib[fragSlot].interp_mode) {
1085       case INTERP_CONSTANT:
1086          for (j = 0; j < NUM_CHANNELS; j++)
1087             const_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
1088          break;
1089       case INTERP_LINEAR:
1090          for (j = 0; j < NUM_CHANNELS; j++)
1091             line_linear_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
1092          break;
1093       case INTERP_PERSPECTIVE:
1094          for (j = 0; j < NUM_CHANNELS; j++)
1095             line_persp_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
1096          break;
1097       case INTERP_POS:
1098          setup_fragcoord_coeff(setup, fragSlot);
1099          break;
1100       default:
1101          assert(0);
1102       }
1103
1104       if (spfs->info.input_semantic_name[fragSlot] == TGSI_SEMANTIC_FOG) {
1105          /* FOG.y = front/back facing  XXX fix this */
1106          setup->coef[fragSlot].a0[1] = 1.0f - setup->quad.input.facing;
1107          setup->coef[fragSlot].dadx[1] = 0.0;
1108          setup->coef[fragSlot].dady[1] = 0.0;
1109       }
1110    }
1111    return TRUE;
1112 }
1113
1114
1115 /**
1116  * Plot a pixel in a line segment.
1117  */
1118 static INLINE void
1119 plot(struct setup_context *setup, int x, int y)
1120 {
1121    const int iy = y & 1;
1122    const int ix = x & 1;
1123    const int quadX = x - ix;
1124    const int quadY = y - iy;
1125    const int mask = (1 << ix) << (2 * iy);
1126
1127    if (quadX != setup->quad.input.x0 ||
1128        quadY != setup->quad.input.y0)
1129    {
1130       /* flush prev quad, start new quad */
1131
1132       if (setup->quad.input.x0 != -1)
1133          CLIP_EMIT_QUAD(setup);
1134
1135       setup->quad.input.x0 = quadX;
1136       setup->quad.input.y0 = quadY;
1137       setup->quad.inout.mask = 0x0;
1138    }
1139
1140    setup->quad.inout.mask |= mask;
1141 }
1142
1143
1144 /**
1145  * Do setup for line rasterization, then render the line.
1146  * Single-pixel width, no stipple, etc.  We rely on the 'draw' module
1147  * to handle stippling and wide lines.
1148  */
1149 void
1150 setup_line(struct setup_context *setup,
1151            const float (*v0)[4],
1152            const float (*v1)[4])
1153 {
1154    int x0 = (int) v0[0][0];
1155    int x1 = (int) v1[0][0];
1156    int y0 = (int) v0[0][1];
1157    int y1 = (int) v1[0][1];
1158    int dx = x1 - x0;
1159    int dy = y1 - y0;
1160    int xstep, ystep;
1161
1162 #if DEBUG_VERTS
1163    debug_printf("Setup line:\n");
1164    print_vertex(setup, v0);
1165    print_vertex(setup, v1);
1166 #endif
1167
1168    if (setup->softpipe->no_rast)
1169       return;
1170
1171    if (dx == 0 && dy == 0)
1172       return;
1173
1174    if (!setup_line_coefficients(setup, v0, v1))
1175       return;
1176
1177    assert(v0[0][0] < 1.0e9);
1178    assert(v0[0][1] < 1.0e9);
1179    assert(v1[0][0] < 1.0e9);
1180    assert(v1[0][1] < 1.0e9);
1181
1182    if (dx < 0) {
1183       dx = -dx;   /* make positive */
1184       xstep = -1;
1185    }
1186    else {
1187       xstep = 1;
1188    }
1189
1190    if (dy < 0) {
1191       dy = -dy;   /* make positive */
1192       ystep = -1;
1193    }
1194    else {
1195       ystep = 1;
1196    }
1197
1198    assert(dx >= 0);
1199    assert(dy >= 0);
1200
1201    setup->quad.input.x0 = setup->quad.input.y0 = -1;
1202    setup->quad.inout.mask = 0x0;
1203    setup->quad.input.prim = QUAD_PRIM_LINE;
1204    /* XXX temporary: set coverage to 1.0 so the line appears
1205     * if AA mode happens to be enabled.
1206     */
1207    setup->quad.input.coverage[0] =
1208    setup->quad.input.coverage[1] =
1209    setup->quad.input.coverage[2] =
1210    setup->quad.input.coverage[3] = 1.0;
1211
1212    if (dx > dy) {
1213       /*** X-major line ***/
1214       int i;
1215       const int errorInc = dy + dy;
1216       int error = errorInc - dx;
1217       const int errorDec = error - dx;
1218
1219       for (i = 0; i < dx; i++) {
1220          plot(setup, x0, y0);
1221
1222          x0 += xstep;
1223          if (error < 0) {
1224             error += errorInc;
1225          }
1226          else {
1227             error += errorDec;
1228             y0 += ystep;
1229          }
1230       }
1231    }
1232    else {
1233       /*** Y-major line ***/
1234       int i;
1235       const int errorInc = dx + dx;
1236       int error = errorInc - dy;
1237       const int errorDec = error - dy;
1238
1239       for (i = 0; i < dy; i++) {
1240          plot(setup, x0, y0);
1241
1242          y0 += ystep;
1243          if (error < 0) {
1244             error += errorInc;
1245          }
1246          else {
1247             error += errorDec;
1248             x0 += xstep;
1249          }
1250       }
1251    }
1252
1253    /* draw final quad */
1254    if (setup->quad.inout.mask) {
1255       CLIP_EMIT_QUAD(setup);
1256    }
1257
1258    WAIT_FOR_COMPLETION(setup);
1259 }
1260
1261
1262 static void
1263 point_persp_coeff(const struct setup_context *setup,
1264                   const float (*vert)[4],
1265                   struct tgsi_interp_coef *coef,
1266                   uint vertSlot, uint i)
1267 {
1268    assert(i <= 3);
1269    coef->dadx[i] = 0.0F;
1270    coef->dady[i] = 0.0F;
1271    coef->a0[i] = vert[vertSlot][i] * vert[0][3];
1272 }
1273
1274
1275 /**
1276  * Do setup for point rasterization, then render the point.
1277  * Round or square points...
1278  * XXX could optimize a lot for 1-pixel points.
1279  */
1280 void
1281 setup_point( struct setup_context *setup,
1282              const float (*v0)[4] )
1283 {
1284    struct softpipe_context *softpipe = setup->softpipe;
1285    const struct sp_fragment_shader *spfs = softpipe->fs;
1286    const int sizeAttr = setup->softpipe->psize_slot;
1287    const float size
1288       = sizeAttr > 0 ? v0[sizeAttr][0]
1289       : setup->softpipe->rasterizer->point_size;
1290    const float halfSize = 0.5F * size;
1291    const boolean round = (boolean) setup->softpipe->rasterizer->point_smooth;
1292    const float x = v0[0][0];  /* Note: data[0] is always position */
1293    const float y = v0[0][1];
1294    const struct vertex_info *vinfo = softpipe_get_vertex_info(softpipe);
1295    uint fragSlot;
1296
1297 #if DEBUG_VERTS
1298    debug_printf("Setup point:\n");
1299    print_vertex(setup, v0);
1300 #endif
1301
1302    if (softpipe->no_rast)
1303       return;
1304
1305    /* For points, all interpolants are constant-valued.
1306     * However, for point sprites, we'll need to setup texcoords appropriately.
1307     * XXX: which coefficients are the texcoords???
1308     * We may do point sprites as textured quads...
1309     *
1310     * KW: We don't know which coefficients are texcoords - ultimately
1311     * the choice of what interpolation mode to use for each attribute
1312     * should be determined by the fragment program, using
1313     * per-attribute declaration statements that include interpolation
1314     * mode as a parameter.  So either the fragment program will have
1315     * to be adjusted for pointsprite vs normal point behaviour, or
1316     * otherwise a special interpolation mode will have to be defined
1317     * which matches the required behaviour for point sprites.  But -
1318     * the latter is not a feature of normal hardware, and as such
1319     * probably should be ruled out on that basis.
1320     */
1321    setup->vprovoke = v0;
1322
1323    /* setup Z, W */
1324    const_coeff(setup, &setup->posCoef, 0, 2);
1325    const_coeff(setup, &setup->posCoef, 0, 3);
1326
1327    for (fragSlot = 0; fragSlot < spfs->info.num_inputs; fragSlot++) {
1328       const uint vertSlot = vinfo->attrib[fragSlot].src_index;
1329       uint j;
1330
1331       switch (vinfo->attrib[fragSlot].interp_mode) {
1332       case INTERP_CONSTANT:
1333          /* fall-through */
1334       case INTERP_LINEAR:
1335          for (j = 0; j < NUM_CHANNELS; j++)
1336             const_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
1337          break;
1338       case INTERP_PERSPECTIVE:
1339          for (j = 0; j < NUM_CHANNELS; j++)
1340             point_persp_coeff(setup, setup->vprovoke,
1341                               &setup->coef[fragSlot], vertSlot, j);
1342          break;
1343       case INTERP_POS:
1344          setup_fragcoord_coeff(setup, fragSlot);
1345          break;
1346       default:
1347          assert(0);
1348       }
1349
1350       if (spfs->info.input_semantic_name[fragSlot] == TGSI_SEMANTIC_FOG) {
1351          /* FOG.y = front/back facing  XXX fix this */
1352          setup->coef[fragSlot].a0[1] = 1.0f - setup->quad.input.facing;
1353          setup->coef[fragSlot].dadx[1] = 0.0;
1354          setup->coef[fragSlot].dady[1] = 0.0;
1355       }
1356    }
1357
1358    setup->quad.input.prim = QUAD_PRIM_POINT;
1359
1360    if (halfSize <= 0.5 && !round) {
1361       /* special case for 1-pixel points */
1362       const int ix = ((int) x) & 1;
1363       const int iy = ((int) y) & 1;
1364       setup->quad.input.x0 = (int) x - ix;
1365       setup->quad.input.y0 = (int) y - iy;
1366       setup->quad.inout.mask = (1 << ix) << (2 * iy);
1367       CLIP_EMIT_QUAD(setup);
1368    }
1369    else {
1370       if (round) {
1371          /* rounded points */
1372          const int ixmin = block((int) (x - halfSize));
1373          const int ixmax = block((int) (x + halfSize));
1374          const int iymin = block((int) (y - halfSize));
1375          const int iymax = block((int) (y + halfSize));
1376          const float rmin = halfSize - 0.7071F;  /* 0.7071 = sqrt(2)/2 */
1377          const float rmax = halfSize + 0.7071F;
1378          const float rmin2 = MAX2(0.0F, rmin * rmin);
1379          const float rmax2 = rmax * rmax;
1380          const float cscale = 1.0F / (rmax2 - rmin2);
1381          int ix, iy;
1382
1383          for (iy = iymin; iy <= iymax; iy += 2) {
1384             for (ix = ixmin; ix <= ixmax; ix += 2) {
1385                float dx, dy, dist2, cover;
1386
1387                setup->quad.inout.mask = 0x0;
1388
1389                dx = (ix + 0.5f) - x;
1390                dy = (iy + 0.5f) - y;
1391                dist2 = dx * dx + dy * dy;
1392                if (dist2 <= rmax2) {
1393                   cover = 1.0F - (dist2 - rmin2) * cscale;
1394                   setup->quad.input.coverage[QUAD_TOP_LEFT] = MIN2(cover, 1.0f);
1395                   setup->quad.inout.mask |= MASK_TOP_LEFT;
1396                }
1397
1398                dx = (ix + 1.5f) - x;
1399                dy = (iy + 0.5f) - y;
1400                dist2 = dx * dx + dy * dy;
1401                if (dist2 <= rmax2) {
1402                   cover = 1.0F - (dist2 - rmin2) * cscale;
1403                   setup->quad.input.coverage[QUAD_TOP_RIGHT] = MIN2(cover, 1.0f);
1404                   setup->quad.inout.mask |= MASK_TOP_RIGHT;
1405                }
1406
1407                dx = (ix + 0.5f) - x;
1408                dy = (iy + 1.5f) - y;
1409                dist2 = dx * dx + dy * dy;
1410                if (dist2 <= rmax2) {
1411                   cover = 1.0F - (dist2 - rmin2) * cscale;
1412                   setup->quad.input.coverage[QUAD_BOTTOM_LEFT] = MIN2(cover, 1.0f);
1413                   setup->quad.inout.mask |= MASK_BOTTOM_LEFT;
1414                }
1415
1416                dx = (ix + 1.5f) - x;
1417                dy = (iy + 1.5f) - y;
1418                dist2 = dx * dx + dy * dy;
1419                if (dist2 <= rmax2) {
1420                   cover = 1.0F - (dist2 - rmin2) * cscale;
1421                   setup->quad.input.coverage[QUAD_BOTTOM_RIGHT] = MIN2(cover, 1.0f);
1422                   setup->quad.inout.mask |= MASK_BOTTOM_RIGHT;
1423                }
1424
1425                if (setup->quad.inout.mask) {
1426                   setup->quad.input.x0 = ix;
1427                   setup->quad.input.y0 = iy;
1428                   CLIP_EMIT_QUAD(setup);
1429                }
1430             }
1431          }
1432       }
1433       else {
1434          /* square points */
1435          const int xmin = (int) (x + 0.75 - halfSize);
1436          const int ymin = (int) (y + 0.25 - halfSize);
1437          const int xmax = xmin + (int) size;
1438          const int ymax = ymin + (int) size;
1439          /* XXX could apply scissor to xmin,ymin,xmax,ymax now */
1440          const int ixmin = block(xmin);
1441          const int ixmax = block(xmax - 1);
1442          const int iymin = block(ymin);
1443          const int iymax = block(ymax - 1);
1444          int ix, iy;
1445
1446          /*
1447          debug_printf("(%f, %f) -> X:%d..%d Y:%d..%d\n", x, y, xmin, xmax,ymin,ymax);
1448          */
1449          for (iy = iymin; iy <= iymax; iy += 2) {
1450             uint rowMask = 0xf;
1451             if (iy < ymin) {
1452                /* above the top edge */
1453                rowMask &= (MASK_BOTTOM_LEFT | MASK_BOTTOM_RIGHT);
1454             }
1455             if (iy + 1 >= ymax) {
1456                /* below the bottom edge */
1457                rowMask &= (MASK_TOP_LEFT | MASK_TOP_RIGHT);
1458             }
1459
1460             for (ix = ixmin; ix <= ixmax; ix += 2) {
1461                uint mask = rowMask;
1462
1463                if (ix < xmin) {
1464                   /* fragment is past left edge of point, turn off left bits */
1465                   mask &= (MASK_BOTTOM_RIGHT | MASK_TOP_RIGHT);
1466                }
1467                if (ix + 1 >= xmax) {
1468                   /* past the right edge */
1469                   mask &= (MASK_BOTTOM_LEFT | MASK_TOP_LEFT);
1470                }
1471
1472                setup->quad.inout.mask = mask;
1473                setup->quad.input.x0 = ix;
1474                setup->quad.input.y0 = iy;
1475                CLIP_EMIT_QUAD(setup);
1476             }
1477          }
1478       }
1479    }
1480
1481    WAIT_FOR_COMPLETION(setup);
1482 }
1483
1484 void setup_prepare( struct setup_context *setup )
1485 {
1486    struct softpipe_context *sp = setup->softpipe;
1487    unsigned i;
1488
1489    if (sp->dirty) {
1490       softpipe_update_derived(sp);
1491    }
1492
1493    /* Note: nr_attrs is only used for debugging (vertex printing) */
1494    setup->quad.nr_attrs = draw_num_vs_outputs(sp->draw);
1495
1496    for (i = 0; i < SP_NUM_QUAD_THREADS; i++) {
1497       sp->quad[i].first->begin( sp->quad[i].first );
1498    }
1499
1500    if (sp->reduced_api_prim == PIPE_PRIM_TRIANGLES &&
1501        sp->rasterizer->fill_cw == PIPE_POLYGON_MODE_FILL &&
1502        sp->rasterizer->fill_ccw == PIPE_POLYGON_MODE_FILL) {
1503       /* we'll do culling */
1504       setup->winding = sp->rasterizer->cull_mode;
1505    }
1506    else {
1507       /* 'draw' will do culling */
1508       setup->winding = PIPE_WINDING_NONE;
1509    }
1510 }
1511
1512
1513
1514 void setup_destroy_context( struct setup_context *setup )
1515 {
1516    FREE( setup );
1517 }
1518
1519
1520 /**
1521  * Create a new primitive setup/render stage.
1522  */
1523 struct setup_context *setup_create_context( struct softpipe_context *softpipe )
1524 {
1525    struct setup_context *setup = CALLOC_STRUCT(setup_context);
1526 #if SP_NUM_QUAD_THREADS > 1
1527    uint i;
1528 #endif
1529
1530    setup->softpipe = softpipe;
1531
1532    setup->quad.coef = setup->coef;
1533    setup->quad.posCoef = &setup->posCoef;
1534
1535 #if SP_NUM_QUAD_THREADS > 1
1536    setup->que.first = 0;
1537    setup->que.last = 0;
1538    pipe_mutex_init( setup->que.que_mutex );
1539    pipe_condvar_init( setup->que.que_notfull_condvar );
1540    pipe_condvar_init( setup->que.que_notempty_condvar );
1541    setup->que.jobs_added = 0;
1542    setup->que.jobs_done = 0;
1543    pipe_condvar_init( setup->que.que_done_condvar );
1544    for (i = 0; i < SP_NUM_QUAD_THREADS; i++) {
1545       setup->threads[i].setup = setup;
1546       setup->threads[i].id = i;
1547       setup->threads[i].handle = pipe_thread_create( quad_thread, &setup->threads[i] );
1548    }
1549 #endif
1550
1551    return setup;
1552 }
1553