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