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