Use vertex_info struct for softpipe - lots of clean-up.
[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/p_util.h"
42
43 #include "pipe/draw/draw_vertex.h"
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->softpipe->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->setup.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 */
430 static void tri_persp_coeff( struct setup_stage *setup,
431 unsigned slot,
432 unsigned i )
433 {
434 /* premultiply by 1/w:
435 */
436 float mina = setup->vmin->data[slot][i] * setup->vmin->data[0][3];
437 float mida = setup->vmid->data[slot][i] * setup->vmid->data[0][3];
438 float maxa = setup->vmax->data[slot][i] * setup->vmax->data[0][3];
439
440 float botda = mida - mina;
441 float majda = maxa - mina;
442 float a = setup->ebot.dy * majda - botda * setup->emaj.dy;
443 float b = setup->emaj.dx * botda - majda * setup->ebot.dx;
444
445 assert(slot < TGSI_ATTRIB_MAX);
446 assert(i <= 3);
447
448 setup->coef[slot].dadx[i] = a * setup->oneoverarea;
449 setup->coef[slot].dady[i] = b * setup->oneoverarea;
450 setup->coef[slot].a0[i] = (mina -
451 (setup->coef[slot].dadx[i] * (setup->vmin->data[0][0] - 0.5f) +
452 setup->coef[slot].dady[i] * (setup->vmin->data[0][1] - 0.5f)));
453 }
454
455
456 /**
457 * Compute the setup->coef[] array dadx, dady, a0 values.
458 * Must be called after setup->vmin,vmid,vmax,vprovoke are initialized.
459 */
460 static void setup_tri_coefficients( struct setup_stage *setup )
461 {
462 const enum interp_mode *interp = setup->softpipe->vertex_info.interp_mode;
463 unsigned slot, j;
464
465 /* z and w are done by linear interpolation:
466 */
467 tri_linear_coeff(setup, 0, 2);
468 tri_linear_coeff(setup, 0, 3);
469
470 /* setup interpolation for all the remaining attributes:
471 */
472 for (slot = 1; slot < setup->quad.nr_attrs; slot++) {
473 switch (interp[slot]) {
474 case INTERP_CONSTANT:
475 for (j = 0; j < NUM_CHANNELS; j++)
476 const_coeff(setup, slot, j);
477 break;
478
479 case INTERP_LINEAR:
480 for (j = 0; j < NUM_CHANNELS; j++)
481 tri_linear_coeff(setup, slot, j);
482 break;
483
484 case INTERP_PERSPECTIVE:
485 for (j = 0; j < NUM_CHANNELS; j++)
486 tri_persp_coeff(setup, slot, j);
487 break;
488 }
489 }
490 }
491
492
493
494 static void setup_tri_edges( struct setup_stage *setup )
495 {
496 float vmin_x = setup->vmin->data[0][0] + 0.5f;
497 float vmid_x = setup->vmid->data[0][0] + 0.5f;
498
499 float vmin_y = setup->vmin->data[0][1] - 0.5f;
500 float vmid_y = setup->vmid->data[0][1] - 0.5f;
501 float vmax_y = setup->vmax->data[0][1] - 0.5f;
502
503 setup->emaj.sy = ceilf(vmin_y);
504 setup->emaj.lines = (int) ceilf(vmax_y - setup->emaj.sy);
505 setup->emaj.dxdy = setup->emaj.dx / setup->emaj.dy;
506 setup->emaj.sx = vmin_x + (setup->emaj.sy - vmin_y) * setup->emaj.dxdy;
507
508 setup->etop.sy = ceilf(vmid_y);
509 setup->etop.lines = (int) ceilf(vmax_y - setup->etop.sy);
510 setup->etop.dxdy = setup->etop.dx / setup->etop.dy;
511 setup->etop.sx = vmid_x + (setup->etop.sy - vmid_y) * setup->etop.dxdy;
512
513 setup->ebot.sy = ceilf(vmin_y);
514 setup->ebot.lines = (int) ceilf(vmid_y - setup->ebot.sy);
515 setup->ebot.dxdy = setup->ebot.dx / setup->ebot.dy;
516 setup->ebot.sx = vmin_x + (setup->ebot.sy - vmin_y) * setup->ebot.dxdy;
517 }
518
519
520 /**
521 * Render the upper or lower half of a triangle.
522 * Scissoring/cliprect is applied here too.
523 */
524 static void subtriangle( struct setup_stage *setup,
525 struct edge *eleft,
526 struct edge *eright,
527 unsigned lines )
528 {
529 const struct pipe_scissor_state *cliprect = &setup->softpipe->cliprect;
530 const int minx = (int) cliprect->minx;
531 const int maxx = (int) cliprect->maxx;
532 const int miny = (int) cliprect->miny;
533 const int maxy = (int) cliprect->maxy;
534 int y, start_y, finish_y;
535 int sy = (int)eleft->sy;
536
537 assert((int)eleft->sy == (int) eright->sy);
538
539 /* clip top/bottom */
540 start_y = sy;
541 finish_y = sy + lines;
542
543 if (start_y < miny)
544 start_y = miny;
545
546 if (finish_y > maxy)
547 finish_y = maxy;
548
549 start_y -= sy;
550 finish_y -= sy;
551
552 /*
553 _mesa_printf("%s %d %d\n", __FUNCTION__, start_y, finish_y);
554 */
555
556 for (y = start_y; y < finish_y; y++) {
557
558 /* avoid accumulating adds as floats don't have the precision to
559 * accurately iterate large triangle edges that way. luckily we
560 * can just multiply these days.
561 *
562 * this is all drowned out by the attribute interpolation anyway.
563 */
564 int left = (int)(eleft->sx + y * eleft->dxdy);
565 int right = (int)(eright->sx + y * eright->dxdy);
566
567 /* clip left/right */
568 if (left < minx)
569 left = minx;
570 if (right > maxx)
571 right = maxx;
572
573 if (left < right) {
574 int _y = sy + y;
575 if (block(_y) != setup->span.y) {
576 flush_spans(setup);
577 setup->span.y = block(_y);
578 }
579
580 setup->span.left[_y&1] = left;setup->span.right[_y&1] = right;
581 setup->span.y_flags |= 1<<(_y&1);
582 }
583 }
584
585
586 /* save the values so that emaj can be restarted:
587 */
588 eleft->sx += lines * eleft->dxdy;
589 eright->sx += lines * eright->dxdy;
590 eleft->sy += lines;
591 eright->sy += lines;
592 }
593
594
595 /**
596 * Do setup for triangle rasterization, then render the triangle.
597 */
598 static void setup_tri( struct draw_stage *stage,
599 struct prim_header *prim )
600 {
601 struct setup_stage *setup = setup_stage( stage );
602
603 /*
604 _mesa_printf("%s\n", __FUNCTION__ );
605 */
606
607 setup_sort_vertices( setup, prim );
608 setup_tri_coefficients( setup );
609 setup_tri_edges( setup );
610
611 setup->quad.prim = PRIM_TRI;
612
613 setup->span.y = 0;
614 setup->span.y_flags = 0;
615 setup->span.right[0] = 0;
616 setup->span.right[1] = 0;
617 /* setup->span.z_mode = tri_z_mode( setup->ctx ); */
618
619 /* init_constant_attribs( setup ); */
620
621 if (setup->oneoverarea < 0.0) {
622 /* emaj on left:
623 */
624 subtriangle( setup, &setup->emaj, &setup->ebot, setup->ebot.lines );
625 subtriangle( setup, &setup->emaj, &setup->etop, setup->etop.lines );
626 }
627 else {
628 /* emaj on right:
629 */
630 subtriangle( setup, &setup->ebot, &setup->emaj, setup->ebot.lines );
631 subtriangle( setup, &setup->etop, &setup->emaj, setup->etop.lines );
632 }
633
634 flush_spans( setup );
635 }
636
637
638
639 /**
640 * Compute a0, dadx and dady for a linearly interpolated coefficient,
641 * for a line.
642 */
643 static void
644 line_linear_coeff(struct setup_stage *setup, unsigned slot, unsigned i)
645 {
646 const float dz = setup->vmax->data[slot][i] - setup->vmin->data[slot][i];
647 const float dadx = dz * setup->emaj.dx * setup->oneoverarea;
648 const float dady = dz * setup->emaj.dy * setup->oneoverarea;
649 setup->coef[slot].dadx[i] = dadx;
650 setup->coef[slot].dady[i] = dady;
651 setup->coef[slot].a0[i]
652 = (setup->vmin->data[slot][i] -
653 (dadx * (setup->vmin->data[0][0] - 0.5f) +
654 dady * (setup->vmin->data[0][1] - 0.5f)));
655 }
656
657
658 /**
659 * Compute a0, dadx and dady for a perspective-corrected interpolant,
660 * for a line.
661 */
662 static void
663 line_persp_coeff(struct setup_stage *setup, unsigned slot, unsigned i)
664 {
665 /* XXX to do */
666 line_linear_coeff(setup, slot, i); /* XXX temporary */
667 }
668
669
670 /**
671 * Compute the setup->coef[] array dadx, dady, a0 values.
672 * Must be called after setup->vmin,vmax are initialized.
673 */
674 static INLINE void
675 setup_line_coefficients(struct setup_stage *setup, struct prim_header *prim)
676 {
677 const enum interp_mode *interp = setup->softpipe->vertex_info.interp_mode;
678 unsigned slot, j;
679
680 /* use setup->vmin, vmax to point to vertices */
681 setup->vprovoke = prim->v[1];
682 setup->vmin = prim->v[0];
683 setup->vmax = prim->v[1];
684
685 setup->emaj.dx = setup->vmax->data[0][0] - setup->vmin->data[0][0];
686 setup->emaj.dy = setup->vmax->data[0][1] - setup->vmin->data[0][1];
687 /* NOTE: this is not really 1/area */
688 setup->oneoverarea = 1.0f / (setup->emaj.dx * setup->emaj.dx +
689 setup->emaj.dy * setup->emaj.dy);
690
691 /* z and w are done by linear interpolation:
692 */
693 line_linear_coeff(setup, 0, 2);
694 line_linear_coeff(setup, 0, 3);
695
696 /* setup interpolation for all the remaining attributes:
697 */
698 for (slot = 1; slot < setup->quad.nr_attrs; slot++) {
699 switch (interp[slot]) {
700 case INTERP_CONSTANT:
701 for (j = 0; j < NUM_CHANNELS; j++)
702 const_coeff(setup, slot, j);
703 break;
704
705 case INTERP_LINEAR:
706 for (j = 0; j < NUM_CHANNELS; j++)
707 line_linear_coeff(setup, slot, j);
708 break;
709
710 case INTERP_PERSPECTIVE:
711 for (j = 0; j < NUM_CHANNELS; j++)
712 line_persp_coeff(setup, slot, j);
713 break;
714 }
715 }
716 }
717
718
719 /**
720 * Plot a pixel in a line segment.
721 */
722 static INLINE void
723 plot(struct setup_stage *setup, int x, int y)
724 {
725 const int iy = y & 1;
726 const int ix = x & 1;
727 const int quadX = x - ix;
728 const int quadY = y - iy;
729 const int mask = (1 << ix) << (2 * iy);
730
731 if (quadX != setup->quad.x0 ||
732 quadY != setup->quad.y0)
733 {
734 /* flush prev quad, start new quad */
735
736 if (setup->quad.x0 != -1)
737 clip_emit_quad(setup);
738
739 setup->quad.x0 = quadX;
740 setup->quad.y0 = quadY;
741 setup->quad.mask = 0x0;
742 }
743
744 setup->quad.mask |= mask;
745 }
746
747
748 /**
749 * Determine whether or not to emit a line fragment by checking
750 * line stipple pattern.
751 */
752 static INLINE unsigned
753 stipple_test(int counter, ushort pattern, int factor)
754 {
755 int b = (counter / factor) & 0xf;
756 return (1 << b) & pattern;
757 }
758
759
760 /**
761 * Do setup for line rasterization, then render the line.
762 * XXX single-pixel width, no stipple, etc
763 */
764 static void
765 setup_line(struct draw_stage *stage, struct prim_header *prim)
766 {
767 const struct vertex_header *v0 = prim->v[0];
768 const struct vertex_header *v1 = prim->v[1];
769 struct setup_stage *setup = setup_stage( stage );
770 struct softpipe_context *sp = setup->softpipe;
771
772 int x0 = (int) v0->data[0][0];
773 int x1 = (int) v1->data[0][0];
774 int y0 = (int) v0->data[0][1];
775 int y1 = (int) v1->data[0][1];
776 int dx = x1 - x0;
777 int dy = y1 - y0;
778 int xstep, ystep;
779
780 if (dx == 0 && dy == 0)
781 return;
782
783 setup_line_coefficients(setup, prim);
784
785 if (dx < 0) {
786 dx = -dx; /* make positive */
787 xstep = -1;
788 }
789 else {
790 xstep = 1;
791 }
792
793 if (dy < 0) {
794 dy = -dy; /* make positive */
795 ystep = -1;
796 }
797 else {
798 ystep = 1;
799 }
800
801 assert(dx >= 0);
802 assert(dy >= 0);
803
804 setup->quad.x0 = setup->quad.y0 = -1;
805 setup->quad.mask = 0x0;
806 setup->quad.prim = PRIM_LINE;
807 /* XXX temporary: set coverage to 1.0 so the line appears
808 * if AA mode happens to be enabled.
809 */
810 setup->quad.coverage[0] =
811 setup->quad.coverage[1] =
812 setup->quad.coverage[2] =
813 setup->quad.coverage[3] = 1.0;
814
815 if (dx > dy) {
816 /*** X-major line ***/
817 int i;
818 const int errorInc = dy + dy;
819 int error = errorInc - dx;
820 const int errorDec = error - dx;
821
822 for (i = 0; i < dx; i++) {
823 if (!sp->setup.line_stipple_enable ||
824 stipple_test(sp->line_stipple_counter,
825 sp->setup.line_stipple_pattern,
826 sp->setup.line_stipple_factor + 1)) {
827 plot(setup, x0, y0);
828 }
829
830 x0 += xstep;
831 if (error < 0) {
832 error += errorInc;
833 }
834 else {
835 error += errorDec;
836 y0 += ystep;
837 }
838
839 sp->line_stipple_counter++;
840 }
841 }
842 else {
843 /*** Y-major line ***/
844 int i;
845 const int errorInc = dx + dx;
846 int error = errorInc - dy;
847 const int errorDec = error - dy;
848
849 for (i = 0; i < dy; i++) {
850 if (!sp->setup.line_stipple_enable ||
851 stipple_test(sp->line_stipple_counter,
852 sp->setup.line_stipple_pattern,
853 sp->setup.line_stipple_factor + 1)) {
854 plot(setup, x0, y0);
855 }
856
857 y0 += ystep;
858
859 if (error < 0) {
860 error += errorInc;
861 }
862 else {
863 error += errorDec;
864 x0 += xstep;
865 }
866
867 sp->line_stipple_counter++;
868 }
869 }
870
871 /* draw final quad */
872 if (setup->quad.mask) {
873 clip_emit_quad(setup);
874 }
875 }
876
877
878 /**
879 * Do setup for point rasterization, then render the point.
880 * Round or square points...
881 * XXX could optimize a lot for 1-pixel points.
882 */
883 static void
884 setup_point(struct draw_stage *stage, struct prim_header *prim)
885 {
886 struct setup_stage *setup = setup_stage( stage );
887 /*XXX this should be a vertex attrib! */
888 const float halfSize = 0.5f * setup->softpipe->setup.point_size;
889 const boolean round = setup->softpipe->setup.point_smooth;
890 const struct vertex_header *v0 = prim->v[0];
891 const float x = v0->data[TGSI_ATTRIB_POS][0];
892 const float y = v0->data[TGSI_ATTRIB_POS][1];
893 unsigned slot, j;
894
895 /* For points, all interpolants are constant-valued.
896 * However, for point sprites, we'll need to setup texcoords appropriately.
897 * XXX: which coefficients are the texcoords???
898 * We may do point sprites as textured quads...
899 *
900 * KW: We don't know which coefficients are texcoords - ultimately
901 * the choice of what interpolation mode to use for each attribute
902 * should be determined by the fragment program, using
903 * per-attribute declaration statements that include interpolation
904 * mode as a parameter. So either the fragment program will have
905 * to be adjusted for pointsprite vs normal point behaviour, or
906 * otherwise a special interpolation mode will have to be defined
907 * which matches the required behaviour for point sprites. But -
908 * the latter is not a feature of normal hardware, and as such
909 * probably should be ruled out on that basis.
910 */
911 setup->vprovoke = prim->v[0];
912 const_coeff(setup, 0, 2);
913 const_coeff(setup, 0, 3);
914 for (slot = 1; slot < setup->quad.nr_attrs; slot++) {
915 for (j = 0; j < NUM_CHANNELS; j++)
916 const_coeff(setup, slot, j);
917 }
918
919 setup->quad.prim = PRIM_POINT;
920
921 if (halfSize <= 0.5 && !round) {
922 /* special case for 1-pixel points */
923 const int ix = ((int) x) & 1;
924 const int iy = ((int) y) & 1;
925 setup->quad.x0 = (int) x - ix;
926 setup->quad.y0 = (int) y - iy;
927 setup->quad.mask = (1 << ix) << (2 * iy);
928 clip_emit_quad(setup);
929 }
930 else {
931 const int ixmin = block((int) (x - halfSize));
932 const int ixmax = block((int) (x + halfSize));
933 const int iymin = block((int) (y - halfSize));
934 const int iymax = block((int) (y + halfSize));
935 int ix, iy;
936
937 if (round) {
938 /* rounded points */
939 const float rmin = halfSize - 0.7071F; /* 0.7071 = sqrt(2)/2 */
940 const float rmax = halfSize + 0.7071F;
941 const float rmin2 = MAX2(0.0F, rmin * rmin);
942 const float rmax2 = rmax * rmax;
943 const float cscale = 1.0F / (rmax2 - rmin2);
944
945 for (iy = iymin; iy <= iymax; iy += 2) {
946 for (ix = ixmin; ix <= ixmax; ix += 2) {
947 float dx, dy, dist2, cover;
948
949 setup->quad.mask = 0x0;
950
951 dx = (ix + 0.5f) - x;
952 dy = (iy + 0.5f) - y;
953 dist2 = dx * dx + dy * dy;
954 if (dist2 <= rmax2) {
955 cover = 1.0F - (dist2 - rmin2) * cscale;
956 setup->quad.coverage[QUAD_BOTTOM_LEFT] = MIN2(cover, 1.0f);
957 setup->quad.mask |= MASK_BOTTOM_LEFT;
958 }
959
960 dx = (ix + 1.5f) - x;
961 dy = (iy + 0.5f) - y;
962 dist2 = dx * dx + dy * dy;
963 if (dist2 <= rmax2) {
964 cover = 1.0F - (dist2 - rmin2) * cscale;
965 setup->quad.coverage[QUAD_BOTTOM_RIGHT] = MIN2(cover, 1.0f);
966 setup->quad.mask |= MASK_BOTTOM_RIGHT;
967 }
968
969 dx = (ix + 0.5f) - x;
970 dy = (iy + 1.5f) - y;
971 dist2 = dx * dx + dy * dy;
972 if (dist2 <= rmax2) {
973 cover = 1.0F - (dist2 - rmin2) * cscale;
974 setup->quad.coverage[QUAD_TOP_LEFT] = MIN2(cover, 1.0f);
975 setup->quad.mask |= MASK_TOP_LEFT;
976 }
977
978 dx = (ix + 1.5f) - x;
979 dy = (iy + 1.5f) - y;
980 dist2 = dx * dx + dy * dy;
981 if (dist2 <= rmax2) {
982 cover = 1.0F - (dist2 - rmin2) * cscale;
983 setup->quad.coverage[QUAD_TOP_RIGHT] = MIN2(cover, 1.0f);
984 setup->quad.mask |= MASK_TOP_RIGHT;
985 }
986
987 if (setup->quad.mask) {
988 setup->quad.x0 = ix;
989 setup->quad.y0 = iy;
990 clip_emit_quad(setup);
991 }
992 }
993 }
994 }
995 else {
996 /* square points */
997 for (iy = iymin; iy <= iymax; iy += 2) {
998 for (ix = ixmin; ix <= ixmax; ix += 2) {
999 setup->quad.mask = 0xf;
1000
1001 if (ix + 0.5 < x - halfSize) {
1002 /* fragment is past left edge of point, turn off left bits */
1003 setup->quad.mask &= ~(MASK_BOTTOM_LEFT | MASK_TOP_LEFT);
1004 }
1005
1006 if (ix + 1.5 > x + halfSize) {
1007 /* past the right edge */
1008 setup->quad.mask &= ~(MASK_BOTTOM_RIGHT | MASK_TOP_RIGHT);
1009 }
1010
1011 if (iy + 0.5 < y - halfSize) {
1012 /* below the bottom edge */
1013 setup->quad.mask &= ~(MASK_BOTTOM_LEFT | MASK_BOTTOM_RIGHT);
1014 }
1015
1016 if (iy + 1.5 > y + halfSize) {
1017 /* above the top edge */
1018 setup->quad.mask &= ~(MASK_TOP_LEFT | MASK_TOP_RIGHT);
1019 }
1020
1021 if (setup->quad.mask) {
1022 setup->quad.x0 = ix;
1023 setup->quad.y0 = iy;
1024 clip_emit_quad(setup);
1025 }
1026 }
1027 }
1028 }
1029 }
1030 }
1031
1032
1033
1034 static void setup_begin( struct draw_stage *stage )
1035 {
1036 struct setup_stage *setup = setup_stage(stage);
1037 struct softpipe_context *sp = setup->softpipe;
1038
1039 setup->quad.nr_attrs = setup->softpipe->nr_frag_attrs;
1040
1041 sp->quad.first->begin(sp->quad.first);
1042 }
1043
1044
1045 static void setup_end( struct draw_stage *stage )
1046 {
1047 }
1048
1049
1050 static void reset_stipple_counter( struct draw_stage *stage )
1051 {
1052 struct setup_stage *setup = setup_stage(stage);
1053 setup->softpipe->line_stipple_counter = 0;
1054 }
1055
1056
1057 /**
1058 * Create a new primitive setup/render stage.
1059 */
1060 struct draw_stage *sp_draw_render_stage( struct softpipe_context *softpipe )
1061 {
1062 struct setup_stage *setup = CALLOC_STRUCT(setup_stage);
1063
1064 setup->softpipe = softpipe;
1065 setup->stage.draw = softpipe->draw;
1066 setup->stage.begin = setup_begin;
1067 setup->stage.point = setup_point;
1068 setup->stage.line = setup_line;
1069 setup->stage.tri = setup_tri;
1070 setup->stage.end = setup_end;
1071 setup->stage.reset_stipple_counter = reset_stipple_counter;
1072
1073 setup->quad.coef = setup->coef;
1074
1075 return &setup->stage;
1076 }