1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
29 * \brief Primitive rasterization/rendering (points, lines, triangles)
31 * \author Keith Whitwell <keith@tungstengraphics.com>
35 #include "sp_context.h"
36 #include "sp_prim_setup.h"
38 #include "sp_quad_pipe.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"
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 */
64 #if SP_NUM_QUAD_THREADS > 1
66 /* Set to 1 if you want other threads to be instantly
67 * notified of pending jobs.
69 #define INSTANT_NOTEMPTY_NOTIFY 0
73 struct setup_context *setup;
80 typedef void (* quad_job_routine)( struct setup_context *setup, uint thread, struct quad_job *job );
84 struct quad_header_input input;
85 struct quad_header_inout inout;
86 quad_job_routine routine;
89 #define NUM_QUAD_JOBS 64
93 struct quad_job jobs[NUM_QUAD_JOBS];
97 pipe_condvar que_notfull_condvar;
98 pipe_condvar que_notempty_condvar;
101 pipe_condvar que_done_condvar;
105 add_quad_job( struct quad_job_que *que, struct quad_header *quad, quad_job_routine routine )
107 #if INSTANT_NOTEMPTY_NOTIFY
111 /* Wait for empty slot, see if the que is empty.
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 );
118 pipe_condvar_wait( que->que_notfull_condvar, que->que_mutex );
120 #if INSTANT_NOTEMPTY_NOTIFY
121 empty = que->last == que->first;
124 pipe_mutex_unlock( que->que_mutex );
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;
133 #if INSTANT_NOTEMPTY_NOTIFY
134 /* If the que was empty, notify consumers there's a job to be done.
137 pipe_mutex_lock( que->que_mutex );
138 pipe_condvar_broadcast( que->que_notempty_condvar );
139 pipe_mutex_unlock( que->que_mutex );
147 * Triangle setup info (derived from draw_stage).
148 * Also used for line drawing (taking some liberties).
150 struct setup_context {
151 struct softpipe_context *softpipe;
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.
157 const float (*vmax)[4];
158 const float (*vmid)[4];
159 const float (*vmin)[4];
160 const float (*vprovoke)[4];
168 struct tgsi_interp_coef coef[PIPE_MAX_SHADER_INPUTS];
169 struct tgsi_interp_coef posCoef; /* For Z, W */
170 struct quad_header quad;
172 #if SP_NUM_QUAD_THREADS > 1
173 struct quad_job_que que;
174 struct thread_info threads[SP_NUM_QUAD_THREADS];
178 int left[2]; /**< [0] = row0, [1] = row1 */
182 unsigned mask; /**< mask of MASK_BOTTOM/TOP_LEFT/RIGHT bits */
186 uint numFragsEmitted; /**< per primitive */
187 uint numFragsWritten; /**< per primitive */
190 unsigned winding; /* which winding to cull */
193 #if SP_NUM_QUAD_THREADS > 1
195 static PIPE_THREAD_ROUTINE( quad_thread, param )
197 struct thread_info *info = (struct thread_info *) param;
198 struct quad_job_que *que = &info->setup->que;
204 /* Wait for an available job.
206 pipe_mutex_lock( que->que_mutex );
207 while (que->last == que->first)
208 pipe_condvar_wait( que->que_notempty_condvar, que->que_mutex );
210 /* See if the que is full.
212 full = (que->last + 1) % NUM_QUAD_JOBS == que->first;
214 /* Take a job and remove it from que.
216 job = que->jobs[que->first];
217 que->first = (que->first + 1) % NUM_QUAD_JOBS;
219 /* Notify the producer if the que is not full.
222 pipe_condvar_signal( que->que_notfull_condvar );
223 pipe_mutex_unlock( que->que_mutex );
225 job.routine( info->setup, info->id, &job );
227 /* Notify the producer if that's the last finished job.
229 pipe_mutex_lock( que->que_mutex );
231 if (que->jobs_added == que->jobs_done)
232 pipe_condvar_signal( que->que_done_condvar );
233 pipe_mutex_unlock( que->que_mutex );
239 #define WAIT_FOR_COMPLETION(setup) \
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 );\
251 #define WAIT_FOR_COMPLETION(setup) ((void) 0)
258 * Do triangle cull test using tri determinant (sign indicates orientation)
259 * \return true if triangle is to be culled.
261 static INLINE boolean
262 cull_tri(const struct setup_context *setup, float det)
265 /* if (det < 0 then Z points toward camera and triangle is
266 * counter-clockwise winding.
268 unsigned winding = (det < 0) ? PIPE_WINDING_CCW : PIPE_WINDING_CW;
270 if ((winding & setup->winding) == 0)
282 * Clip setup->quad against the scissor/surface bounds.
285 quad_clip( struct setup_context *setup, struct quad_header *quad )
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;
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;
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);
313 * Emit a quad (pass to next stage) with clipping.
316 clip_emit_quad( struct setup_context *setup, struct quad_header *quad, uint thread )
318 quad_clip( setup, quad );
319 if (quad->inout.mask) {
320 struct softpipe_context *sp = setup->softpipe;
322 sp->quad[thread].first->run( sp->quad[thread].first, quad );
326 #if SP_NUM_QUAD_THREADS > 1
329 clip_emit_quad_job( struct setup_context *setup, uint thread, struct quad_job *job )
331 struct quad_header quad;
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 );
341 #define CLIP_EMIT_QUAD(setup) add_quad_job( &setup->que, &setup->quad, clip_emit_quad_job )
345 #define CLIP_EMIT_QUAD(setup) clip_emit_quad( setup, &setup->quad, 0 )
350 * Emit a quad (pass to next stage). No clipping is done.
353 emit_quad( struct setup_context *setup, struct quad_header *quad, uint thread )
355 struct softpipe_context *sp = setup->softpipe;
357 uint mask = quad->inout.mask;
361 if (mask & 1) setup->numFragsEmitted++;
362 if (mask & 2) setup->numFragsEmitted++;
363 if (mask & 4) setup->numFragsEmitted++;
364 if (mask & 8) setup->numFragsEmitted++;
366 sp->quad[thread].first->run( sp->quad[thread].first, quad );
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++;
376 #if SP_NUM_QUAD_THREADS > 1
379 emit_quad_job( struct setup_context *setup, uint thread, struct quad_job *job )
381 struct quad_header quad;
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 );
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 );\
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 );\
410 * Given an X or Y coordinate, return the block/quad coordinate that it
413 static INLINE int block( int x )
420 * Render a horizontal span of quads
422 static void flush_spans( struct setup_context *setup )
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;
431 switch (setup->span.y_flags) {
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 */
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;
448 EMIT_QUAD( setup, x, setup->span.y, mask );
453 /* only even line written (quad top row) */
454 minleft = block(xleft0);
455 maxright = block(xright0);
456 for (x = minleft; x <= maxright; x += 2) {
458 if (x >= xleft0 && x < xright0)
459 mask |= MASK_TOP_LEFT;
460 if (x+1 >= xleft0 && x+1 < xright0)
461 mask |= MASK_TOP_RIGHT;
463 EMIT_QUAD( setup, x, setup->span.y, mask );
468 /* only odd line written (quad bottom row) */
469 minleft = block(xleft1);
470 maxright = block(xright1);
471 for (x = minleft; x <= maxright; x += 2) {
473 if (x >= xleft1 && x < xright1)
474 mask |= MASK_BOTTOM_LEFT;
475 if (x+1 >= xleft1 && x+1 < xright1)
476 mask |= MASK_BOTTOM_RIGHT;
478 EMIT_QUAD( setup, x, setup->span.y, mask );
487 setup->span.y_flags = 0;
488 setup->span.right[0] = 0;
489 setup->span.right[1] = 0;
494 static void print_vertex(const struct setup_context *setup,
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");
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
514 static boolean setup_sort_vertices( struct setup_context *setup,
516 const float (*v0)[4],
517 const float (*v1)[4],
518 const float (*v2)[4] )
520 setup->vprovoke = v2;
522 /* determine bottom to top order of vertices */
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];
577 * Compute triangle's area. Use 1/area to compute partial
578 * derivatives of attributes later.
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.
583 * To determine whether the primitive is front or back facing we
584 * use the prim->det value because its sign is correct.
587 const float area = (setup->emaj.dx * setup->ebot.dy -
588 setup->ebot.dx * setup->emaj.dy);
590 setup->oneoverarea = 1.0f / area;
593 debug_printf("%s one-over-area %f area %f det %f\n",
594 __FUNCTION__, setup->oneoverarea, area, det );
596 if (util_is_inf_or_nan(setup->oneoverarea))
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
604 setup->quad.input.facing = (det > 0.0) ^ (setup->softpipe->rasterizer->front_winding == PIPE_WINDING_CW);
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)
617 static void const_coeff( struct setup_context *setup,
618 struct tgsi_interp_coef *coef,
619 uint vertSlot, uint i)
626 /* need provoking vertex info!
628 coef->a0[i] = setup->vprovoke[vertSlot][i];
633 * Compute a0, dadx and dady for a linearly interpolated coefficient,
636 static void tri_linear_coeff( struct setup_context *setup,
637 struct tgsi_interp_coef *coef,
638 uint vertSlot, uint i)
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;
649 coef->dadx[i] = dadx;
650 coef->dady[i] = dady;
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).
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.
664 coef->a0[i] = (setup->vmin[vertSlot][i] -
665 (dadx * (setup->vmin[0][0] - 0.5f) +
666 dady * (setup->vmin[0][1] - 0.5f)));
669 debug_printf("attr[%d].%c: %f dx:%f dy:%f\n",
671 setup->coef[slot].a0[i],
672 setup->coef[slot].dadx[i],
673 setup->coef[slot].dady[i]);
679 * Compute a0, dadx and dady for a perspective-corrected interpolant,
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.
686 static void tri_persp_coeff( struct setup_context *setup,
687 struct tgsi_interp_coef *coef,
688 uint vertSlot, uint i)
690 /* premultiply by 1/w (v[0][3] is always W):
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;
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]
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)));
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.
726 setup_fragcoord_coeff(struct setup_context *setup, uint slot)
729 setup->coef[slot].a0[0] = 0;
730 setup->coef[slot].dadx[0] = 1.0;
731 setup->coef[slot].dady[0] = 0.0;
733 setup->coef[slot].a0[1] = 0.0;
734 setup->coef[slot].dadx[1] = 0.0;
735 setup->coef[slot].dady[1] = 1.0;
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];
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];
749 * Compute the setup->coef[] array dadx, dady, a0 values.
750 * Must be called after setup->vmin,vmid,vmax,vprovoke are initialized.
752 static void setup_tri_coefficients( struct setup_context *setup )
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);
759 /* z and w are done by linear interpolation:
761 tri_linear_coeff(setup, &setup->posCoef, 0, 2);
762 tri_linear_coeff(setup, &setup->posCoef, 0, 3);
764 /* setup interpolation for all the remaining attributes:
766 for (fragSlot = 0; fragSlot < spfs->info.num_inputs; fragSlot++) {
767 const uint vertSlot = vinfo->attrib[fragSlot].src_index;
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);
776 for (j = 0; j < NUM_CHANNELS; j++)
777 tri_linear_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
779 case INTERP_PERSPECTIVE:
780 for (j = 0; j < NUM_CHANNELS; j++)
781 tri_persp_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
784 setup_fragcoord_coeff(setup, fragSlot);
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;
801 static void setup_tri_edges( struct setup_context *setup )
803 float vmin_x = setup->vmin[0][0] + 0.5f;
804 float vmid_x = setup->vmid[0][0] + 0.5f;
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;
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;
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;
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;
828 * Render the upper or lower half of a triangle.
829 * Scissoring/cliprect is applied here too.
831 static void subtriangle( struct setup_context *setup,
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;
844 assert((int)eleft->sy == (int) eright->sy);
846 /* clip top/bottom */
848 finish_y = sy + lines;
860 debug_printf("%s %d %d\n", __FUNCTION__, start_y, finish_y);
863 for (y = start_y; y < finish_y; y++) {
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.
869 * this is all drowned out by the attribute interpolation anyway.
871 int left = (int)(eleft->sx + y * eleft->dxdy);
872 int right = (int)(eright->sx + y * eright->dxdy);
874 /* clip left/right */
882 if (block(_y) != setup->span.y) {
884 setup->span.y = block(_y);
887 setup->span.left[_y&1] = left;
888 setup->span.right[_y&1] = right;
889 setup->span.y_flags |= 1<<(_y&1);
894 /* save the values so that emaj can be restarted:
896 eleft->sx += lines * eleft->dxdy;
897 eright->sx += lines * eright->dxdy;
904 * Recalculate prim's determinant. This is needed as we don't have
905 * get this information through the vbuf_render interface & we must
909 calc_det( const float (*v0)[4],
910 const float (*v1)[4],
911 const float (*v2)[4] )
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];
919 /* det = cross(e,f).z */
920 return ex * fy - ey * fx;
925 * Do setup for triangle rasterization, then render the triangle.
927 void setup_tri( struct setup_context *setup,
928 const float (*v0)[4],
929 const float (*v1)[4],
930 const float (*v2)[4] )
935 debug_printf("Setup triangle:\n");
936 print_vertex(setup, v0);
937 print_vertex(setup, v1);
938 print_vertex(setup, v2);
941 if (setup->softpipe->no_rast)
944 det = calc_det(v0, v1, v2);
946 debug_printf("%s\n", __FUNCTION__ );
950 setup->numFragsEmitted = 0;
951 setup->numFragsWritten = 0;
954 if (cull_tri( setup, det ))
957 if (!setup_sort_vertices( setup, det, v0, v1, v2 ))
959 setup_tri_coefficients( setup );
960 setup_tri_edges( setup );
962 setup->quad.input.prim = QUAD_PRIM_TRI;
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 ); */
970 /* init_constant_attribs( setup ); */
972 if (setup->oneoverarea < 0.0) {
975 subtriangle( setup, &setup->emaj, &setup->ebot, setup->ebot.lines );
976 subtriangle( setup, &setup->emaj, &setup->etop, setup->etop.lines );
981 subtriangle( setup, &setup->ebot, &setup->emaj, setup->ebot.lines );
982 subtriangle( setup, &setup->etop, &setup->emaj, setup->etop.lines );
985 flush_spans( setup );
987 WAIT_FOR_COMPLETION(setup);
990 printf("Tri: %u frags emitted, %u written\n",
991 setup->numFragsEmitted,
992 setup->numFragsWritten);
999 * Compute a0, dadx and dady for a linearly interpolated coefficient,
1003 line_linear_coeff(const struct setup_context *setup,
1004 struct tgsi_interp_coef *coef,
1005 uint vertSlot, uint i)
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)));
1019 * Compute a0, dadx and dady for a perspective-corrected interpolant,
1023 line_persp_coeff(const struct setup_context *setup,
1024 struct tgsi_interp_coef *coef,
1025 uint vertSlot, uint i)
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)));
1042 * Compute the setup->coef[] array dadx, dady, a0 values.
1043 * Must be called after setup->vmin,vmax are initialized.
1045 static INLINE boolean
1046 setup_line_coefficients(struct setup_context *setup,
1047 const float (*v0)[4],
1048 const float (*v1)[4])
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);
1056 /* use setup->vmin, vmax to point to vertices */
1057 if (softpipe->rasterizer->flatshade_first)
1058 setup->vprovoke = v0;
1060 setup->vprovoke = v1;
1064 setup->emaj.dx = setup->vmax[0][0] - setup->vmin[0][0];
1065 setup->emaj.dy = setup->vmax[0][1] - setup->vmin[0][1];
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))
1071 setup->oneoverarea = 1.0f / area;
1073 /* z and w are done by linear interpolation:
1075 line_linear_coeff(setup, &setup->posCoef, 0, 2);
1076 line_linear_coeff(setup, &setup->posCoef, 0, 3);
1078 /* setup interpolation for all the remaining attributes:
1080 for (fragSlot = 0; fragSlot < spfs->info.num_inputs; fragSlot++) {
1081 const uint vertSlot = vinfo->attrib[fragSlot].src_index;
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);
1090 for (j = 0; j < NUM_CHANNELS; j++)
1091 line_linear_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
1093 case INTERP_PERSPECTIVE:
1094 for (j = 0; j < NUM_CHANNELS; j++)
1095 line_persp_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
1098 setup_fragcoord_coeff(setup, fragSlot);
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;
1116 * Plot a pixel in a line segment.
1119 plot(struct setup_context *setup, int x, int y)
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);
1127 if (quadX != setup->quad.input.x0 ||
1128 quadY != setup->quad.input.y0)
1130 /* flush prev quad, start new quad */
1132 if (setup->quad.input.x0 != -1)
1133 CLIP_EMIT_QUAD(setup);
1135 setup->quad.input.x0 = quadX;
1136 setup->quad.input.y0 = quadY;
1137 setup->quad.inout.mask = 0x0;
1140 setup->quad.inout.mask |= mask;
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.
1150 setup_line(struct setup_context *setup,
1151 const float (*v0)[4],
1152 const float (*v1)[4])
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];
1163 debug_printf("Setup line:\n");
1164 print_vertex(setup, v0);
1165 print_vertex(setup, v1);
1168 if (setup->softpipe->no_rast)
1171 if (dx == 0 && dy == 0)
1174 if (!setup_line_coefficients(setup, v0, v1))
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);
1183 dx = -dx; /* make positive */
1191 dy = -dy; /* make positive */
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.
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;
1213 /*** X-major line ***/
1215 const int errorInc = dy + dy;
1216 int error = errorInc - dx;
1217 const int errorDec = error - dx;
1219 for (i = 0; i < dx; i++) {
1220 plot(setup, x0, y0);
1233 /*** Y-major line ***/
1235 const int errorInc = dx + dx;
1236 int error = errorInc - dy;
1237 const int errorDec = error - dy;
1239 for (i = 0; i < dy; i++) {
1240 plot(setup, x0, y0);
1253 /* draw final quad */
1254 if (setup->quad.inout.mask) {
1255 CLIP_EMIT_QUAD(setup);
1258 WAIT_FOR_COMPLETION(setup);
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)
1269 coef->dadx[i] = 0.0F;
1270 coef->dady[i] = 0.0F;
1271 coef->a0[i] = vert[vertSlot][i] * vert[0][3];
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.
1281 setup_point( struct setup_context *setup,
1282 const float (*v0)[4] )
1284 struct softpipe_context *softpipe = setup->softpipe;
1285 const struct sp_fragment_shader *spfs = softpipe->fs;
1286 const int sizeAttr = setup->softpipe->psize_slot;
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);
1298 debug_printf("Setup point:\n");
1299 print_vertex(setup, v0);
1302 if (softpipe->no_rast)
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...
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.
1321 setup->vprovoke = v0;
1324 const_coeff(setup, &setup->posCoef, 0, 2);
1325 const_coeff(setup, &setup->posCoef, 0, 3);
1327 for (fragSlot = 0; fragSlot < spfs->info.num_inputs; fragSlot++) {
1328 const uint vertSlot = vinfo->attrib[fragSlot].src_index;
1331 switch (vinfo->attrib[fragSlot].interp_mode) {
1332 case INTERP_CONSTANT:
1335 for (j = 0; j < NUM_CHANNELS; j++)
1336 const_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
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);
1344 setup_fragcoord_coeff(setup, fragSlot);
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;
1358 setup->quad.input.prim = QUAD_PRIM_POINT;
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);
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);
1383 for (iy = iymin; iy <= iymax; iy += 2) {
1384 for (ix = ixmin; ix <= ixmax; ix += 2) {
1385 float dx, dy, dist2, cover;
1387 setup->quad.inout.mask = 0x0;
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;
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;
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;
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;
1425 if (setup->quad.inout.mask) {
1426 setup->quad.input.x0 = ix;
1427 setup->quad.input.y0 = iy;
1428 CLIP_EMIT_QUAD(setup);
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);
1447 debug_printf("(%f, %f) -> X:%d..%d Y:%d..%d\n", x, y, xmin, xmax,ymin,ymax);
1449 for (iy = iymin; iy <= iymax; iy += 2) {
1452 /* above the top edge */
1453 rowMask &= (MASK_BOTTOM_LEFT | MASK_BOTTOM_RIGHT);
1455 if (iy + 1 >= ymax) {
1456 /* below the bottom edge */
1457 rowMask &= (MASK_TOP_LEFT | MASK_TOP_RIGHT);
1460 for (ix = ixmin; ix <= ixmax; ix += 2) {
1461 uint mask = rowMask;
1464 /* fragment is past left edge of point, turn off left bits */
1465 mask &= (MASK_BOTTOM_RIGHT | MASK_TOP_RIGHT);
1467 if (ix + 1 >= xmax) {
1468 /* past the right edge */
1469 mask &= (MASK_BOTTOM_LEFT | MASK_TOP_LEFT);
1472 setup->quad.inout.mask = mask;
1473 setup->quad.input.x0 = ix;
1474 setup->quad.input.y0 = iy;
1475 CLIP_EMIT_QUAD(setup);
1481 WAIT_FOR_COMPLETION(setup);
1484 void setup_prepare( struct setup_context *setup )
1486 struct softpipe_context *sp = setup->softpipe;
1490 softpipe_update_derived(sp);
1493 /* Note: nr_attrs is only used for debugging (vertex printing) */
1494 setup->quad.nr_attrs = draw_num_vs_outputs(sp->draw);
1496 for (i = 0; i < SP_NUM_QUAD_THREADS; i++) {
1497 sp->quad[i].first->begin( sp->quad[i].first );
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;
1507 /* 'draw' will do culling */
1508 setup->winding = PIPE_WINDING_NONE;
1514 void setup_destroy_context( struct setup_context *setup )
1521 * Create a new primitive setup/render stage.
1523 struct setup_context *setup_create_context( struct softpipe_context *softpipe )
1525 struct setup_context *setup = CALLOC_STRUCT(setup_context);
1526 #if SP_NUM_QUAD_THREADS > 1
1530 setup->softpipe = softpipe;
1532 setup->quad.coef = setup->coef;
1533 setup->quad.posCoef = &setup->posCoef;
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] );