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