Merge commit 'origin/gallium-0.1'
[mesa.git] / src / gallium / drivers / softpipe / sp_quad_stipple.c
1
2 /**
3 * quad polygon stipple stage
4 */
5
6 #include "sp_context.h"
7 #include "sp_quad.h"
8 #include "sp_quad_pipe.h"
9 #include "pipe/p_defines.h"
10 #include "util/u_memory.h"
11
12
13 /**
14 * Apply polygon stipple to quads produced by triangle rasterization
15 */
16 static void
17 stipple_quad(struct quad_stage *qs, struct quad_header *quad)
18 {
19 static const uint bit31 = 1 << 31;
20 static const uint bit30 = 1 << 30;
21
22 if (quad->input.prim == QUAD_PRIM_TRI) {
23 struct softpipe_context *softpipe = qs->softpipe;
24 /* need to invert Y to index into OpenGL's stipple pattern */
25 int y0, y1;
26 uint stipple0, stipple1;
27 const int col0 = quad->input.x0 % 32;
28
29 if (softpipe->rasterizer->origin_lower_left) {
30 y0 = softpipe->framebuffer.height - 1 - quad->input.y0;
31 y1 = y0 - 1;
32 }
33 else {
34 y0 = quad->input.y0;
35 y1 = y0 + 1;
36 }
37
38 stipple0 = softpipe->poly_stipple.stipple[y0 % 32];
39 stipple1 = softpipe->poly_stipple.stipple[y1 % 32];
40
41 /* turn off quad mask bits that fail the stipple test */
42 if ((stipple0 & (bit31 >> col0)) == 0)
43 quad->inout.mask &= ~MASK_TOP_LEFT;
44
45 if ((stipple0 & (bit30 >> col0)) == 0)
46 quad->inout.mask &= ~MASK_TOP_RIGHT;
47
48 if ((stipple1 & (bit31 >> col0)) == 0)
49 quad->inout.mask &= ~MASK_BOTTOM_LEFT;
50
51 if ((stipple1 & (bit30 >> col0)) == 0)
52 quad->inout.mask &= ~MASK_BOTTOM_RIGHT;
53
54 if (!quad->inout.mask) {
55 /* all fragments failed stipple test, end of quad pipeline */
56 return;
57 }
58 }
59
60 qs->next->run(qs->next, quad);
61 }
62
63
64 static void stipple_begin(struct quad_stage *qs)
65 {
66 qs->next->begin(qs->next);
67 }
68
69
70 static void stipple_destroy(struct quad_stage *qs)
71 {
72 FREE( qs );
73 }
74
75
76 struct quad_stage *
77 sp_quad_polygon_stipple_stage( struct softpipe_context *softpipe )
78 {
79 struct quad_stage *stage = CALLOC_STRUCT(quad_stage);
80
81 stage->softpipe = softpipe;
82 stage->begin = stipple_begin;
83 stage->run = stipple_quad;
84 stage->destroy = stipple_destroy;
85
86 return stage;
87 }