Merge commit 'origin/gallium-master-merge'
[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_headers.h"
8 #include "sp_quad.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 == 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 if (softpipe->rasterizer->origin_lower_left) {
28 y0 = softpipe->framebuffer.height - 1 - quad->input.y0;
29 y1 = y0 - 1;
30 }
31 else {
32 y0 = quad->input.y0;
33 y1 = y0 + 1;
34 }
35 stipple0 = softpipe->poly_stipple.stipple[y0 % 32];
36 stipple1 = softpipe->poly_stipple.stipple[y1 % 32];
37
38 #if 1
39 {
40 const int col0 = quad->input.x0 % 32;
41 if ((stipple0 & (bit31 >> col0)) == 0)
42 quad->inout.mask &= ~MASK_TOP_LEFT;
43
44 if ((stipple0 & (bit30 >> col0)) == 0)
45 quad->inout.mask &= ~MASK_TOP_RIGHT;
46
47 if ((stipple1 & (bit31 >> col0)) == 0)
48 quad->inout.mask &= ~MASK_BOTTOM_LEFT;
49
50 if ((stipple1 & (bit30 >> col0)) == 0)
51 quad->inout.mask &= ~MASK_BOTTOM_RIGHT;
52 }
53 #else
54 /* We'd like to use this code, but we'd need to redefine
55 * MASK_TOP_LEFT to be (1 << 1) and MASK_TOP_RIGHT to be (1 << 0),
56 * and similarly for the BOTTOM bits. But that may have undesirable
57 * side effects elsewhere.
58 */
59 const int col0 = 30 - (quad->input.x0 % 32);
60 quad->inout.mask &= (((stipple0 >> col0) & 0x3) |
61 (((stipple1 >> col0) & 0x3) << 2));
62 #endif
63 if (!quad->inout.mask)
64 return;
65 }
66
67 qs->next->run(qs->next, quad);
68 }
69
70
71 static void stipple_begin(struct quad_stage *qs)
72 {
73 qs->next->begin(qs->next);
74 }
75
76
77 static void stipple_destroy(struct quad_stage *qs)
78 {
79 FREE( qs );
80 }
81
82
83 struct quad_stage *
84 sp_quad_polygon_stipple_stage( struct softpipe_context *softpipe )
85 {
86 struct quad_stage *stage = CALLOC_STRUCT(quad_stage);
87
88 stage->softpipe = softpipe;
89 stage->begin = stipple_begin;
90 stage->run = stipple_quad;
91 stage->destroy = stipple_destroy;
92
93 return stage;
94 }