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