llvmpipe: consolidate several loops in lp_rast_triangle
[mesa.git] / src / gallium / drivers / llvmpipe / lp_rast_tri_tmp.h
1 /**************************************************************************
2 *
3 * Copyright 2007-2010 VMware, Inc.
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 VMWARE 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 * Rasterization for binned triangles within a tile
30 */
31
32
33
34 /**
35 * Prototype for a 7 plane rasterizer function. Will codegenerate
36 * several of these.
37 *
38 * XXX: Varients for more/fewer planes.
39 * XXX: Need ways of dropping planes as we descend.
40 * XXX: SIMD
41 */
42 static void
43 TAG(do_block_4)(struct lp_rasterizer_task *task,
44 const struct lp_rast_triangle *tri,
45 const struct lp_rast_plane *plane,
46 int x, int y,
47 const int *c)
48 {
49 unsigned mask = 0xffff;
50 int j;
51
52 for (j = 0; j < NR_PLANES; j++) {
53 mask &= ~build_mask(c[j] - 1,
54 -plane[j].dcdx,
55 plane[j].dcdy);
56 }
57
58 /* Now pass to the shader:
59 */
60 if (mask)
61 lp_rast_shade_quads_mask(task, &tri->inputs, x, y, mask);
62 }
63
64 /**
65 * Evaluate a 16x16 block of pixels to determine which 4x4 subblocks are in/out
66 * of the triangle's bounds.
67 */
68 static void
69 TAG(do_block_16)(struct lp_rasterizer_task *task,
70 const struct lp_rast_triangle *tri,
71 const struct lp_rast_plane *plane,
72 int x, int y,
73 const int *c)
74 {
75 unsigned outmask, inmask, partmask, partial_mask;
76 unsigned j;
77
78 outmask = 0; /* outside one or more trivial reject planes */
79 partmask = 0; /* outside one or more trivial accept planes */
80
81 for (j = 0; j < NR_PLANES; j++) {
82 const int dcdx = -plane[j].dcdx * 4;
83 const int dcdy = plane[j].dcdy * 4;
84 const int cox = c[j] + plane[j].eo * 4;
85 const int cio = c[j] + plane[j].ei * 4 - 1;
86
87 outmask |= build_mask_linear(cox, dcdx, dcdy);
88 partmask |= build_mask_linear(cio, dcdx, dcdy);
89 }
90
91 if (outmask == 0xffff)
92 return;
93
94 /* Mask of sub-blocks which are inside all trivial accept planes:
95 */
96 inmask = ~partmask & 0xffff;
97
98 /* Mask of sub-blocks which are inside all trivial reject planes,
99 * but outside at least one trivial accept plane:
100 */
101 partial_mask = partmask & ~outmask;
102
103 assert((partial_mask & inmask) == 0);
104
105 /* Iterate over partials:
106 */
107 while (partial_mask) {
108 int i = ffs(partial_mask) - 1;
109 int ix = (i & 3) * 4;
110 int iy = (i >> 2) * 4;
111 int px = x + ix;
112 int py = y + iy;
113 int cx[NR_PLANES];
114
115 partial_mask &= ~(1 << i);
116
117 for (j = 0; j < NR_PLANES; j++)
118 cx[j] = (c[j]
119 - plane[j].dcdx * ix
120 + plane[j].dcdy * iy);
121
122 TAG(do_block_4)(task, tri, plane, px, py, cx);
123 }
124
125 /* Iterate over fulls:
126 */
127 while (inmask) {
128 int i = ffs(inmask) - 1;
129 int ix = (i & 3) * 4;
130 int iy = (i >> 2) * 4;
131 int px = x + ix;
132 int py = y + iy;
133
134 inmask &= ~(1 << i);
135
136 block_full_4(task, tri, px, py);
137 }
138 }
139
140
141 /**
142 * Scan the tile in chunks and figure out which pixels to rasterize
143 * for this triangle.
144 */
145 void
146 TAG(lp_rast_triangle)(struct lp_rasterizer_task *task,
147 const union lp_rast_cmd_arg arg)
148 {
149 const struct lp_rast_triangle *tri = arg.triangle.tri;
150 unsigned plane_mask = arg.triangle.plane_mask;
151 const int x = task->x, y = task->y;
152 struct lp_rast_plane plane[NR_PLANES];
153 int c[NR_PLANES];
154 unsigned outmask, inmask, partmask, partial_mask;
155 unsigned j = 0;
156
157 outmask = 0; /* outside one or more trivial reject planes */
158 partmask = 0; /* outside one or more trivial accept planes */
159
160 while (plane_mask) {
161 int i = ffs(plane_mask) - 1;
162 plane[j] = tri->plane[i];
163 plane_mask &= ~(1 << i);
164 c[j] = plane[j].c + plane[j].dcdy * y - plane[j].dcdx * x;
165
166 {
167 const int dcdx = -plane[j].dcdx * 16;
168 const int dcdy = plane[j].dcdy * 16;
169 const int cox = c[j] + plane[j].eo * 16;
170 const int cio = c[j] + plane[j].ei * 16 - 1;
171
172 outmask |= build_mask_linear(cox, dcdx, dcdy);
173 partmask |= build_mask_linear(cio, dcdx, dcdy);
174 }
175
176 j++;
177 }
178
179 if (outmask == 0xffff)
180 return;
181
182 /* Mask of sub-blocks which are inside all trivial accept planes:
183 */
184 inmask = ~partmask & 0xffff;
185
186 /* Mask of sub-blocks which are inside all trivial reject planes,
187 * but outside at least one trivial accept plane:
188 */
189 partial_mask = partmask & ~outmask;
190
191 assert((partial_mask & inmask) == 0);
192
193 /* Iterate over partials:
194 */
195 while (partial_mask) {
196 int i = ffs(partial_mask) - 1;
197 int ix = (i & 3) * 16;
198 int iy = (i >> 2) * 16;
199 int px = x + ix;
200 int py = y + iy;
201 int cx[NR_PLANES];
202
203 for (j = 0; j < NR_PLANES; j++)
204 cx[j] = (c[j]
205 - plane[j].dcdx * ix
206 + plane[j].dcdy * iy);
207
208 partial_mask &= ~(1 << i);
209
210 LP_COUNT(nr_partially_covered_16);
211 TAG(do_block_16)(task, tri, plane, px, py, cx);
212 }
213
214 /* Iterate over fulls:
215 */
216 while (inmask) {
217 int i = ffs(inmask) - 1;
218 int ix = (i & 3) * 16;
219 int iy = (i >> 2) * 16;
220 int px = x + ix;
221 int py = y + iy;
222
223 inmask &= ~(1 << i);
224
225 LP_COUNT(nr_fully_covered_16);
226 block_full_16(task, tri, px, py);
227 }
228 }
229
230 #undef TAG
231 #undef NR_PLANES
232