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