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