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