llvmpipe: pass fewer parameters to rasterization functions
[mesa.git] / src / gallium / drivers / llvmpipe / lp_rast_tri.c
1 /**************************************************************************
2 *
3 * Copyright 2007-2009 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 #include <limits.h>
33 #include "util/u_math.h"
34 #include "lp_debug.h"
35 #include "lp_perf.h"
36 #include "lp_rast_priv.h"
37 #include "lp_tile_soa.h"
38
39
40 /**
41 * Map an index in [0,15] to an x,y position, multiplied by 4.
42 * This is used to get the position of each subtile in a 4x4
43 * grid of edge step values.
44 * Note: we can use some bit twiddling to compute these values instead
45 * of using a look-up table, but there's no measurable performance
46 * difference.
47 */
48 static const int pos_table4[16][2] = {
49 { 0, 0 },
50 { 4, 0 },
51 { 0, 4 },
52 { 4, 4 },
53 { 8, 0 },
54 { 12, 0 },
55 { 8, 4 },
56 { 12, 4 },
57 { 0, 8 },
58 { 4, 8 },
59 { 0, 12 },
60 { 4, 12 },
61 { 8, 8 },
62 { 12, 8 },
63 { 8, 12 },
64 { 12, 12 }
65 };
66
67
68 static const int pos_table16[16][2] = {
69 { 0, 0 },
70 { 16, 0 },
71 { 0, 16 },
72 { 16, 16 },
73 { 32, 0 },
74 { 48, 0 },
75 { 32, 16 },
76 { 48, 16 },
77 { 0, 32 },
78 { 16, 32 },
79 { 0, 48 },
80 { 16, 48 },
81 { 32, 32 },
82 { 48, 32 },
83 { 32, 48 },
84 { 48, 48 }
85 };
86
87
88 /**
89 * Shade all pixels in a 4x4 block.
90 */
91 static void
92 block_full_4( struct lp_rasterizer_task *rast_task,
93 const struct lp_rast_triangle *tri,
94 int x, int y )
95 {
96 lp_rast_shade_quads_all(rast_task, &tri->inputs, x, y);
97 }
98
99
100 /**
101 * Shade all pixels in a 16x16 block.
102 */
103 static void
104 block_full_16( struct lp_rasterizer_task *rast_task,
105 const struct lp_rast_triangle *tri,
106 int x, int y )
107 {
108 unsigned ix, iy;
109 assert(x % 16 == 0);
110 assert(y % 16 == 0);
111 for (iy = 0; iy < 16; iy += 4)
112 for (ix = 0; ix < 16; ix += 4)
113 block_full_4(rast_task, tri, x + ix, y + iy);
114 }
115
116
117 /**
118 * Pass the 4x4 pixel block to the shader function.
119 * Determination of which of the 16 pixels lies inside the triangle
120 * will be done as part of the fragment shader.
121 */
122 static void
123 do_block_4( struct lp_rasterizer_task *rast_task,
124 const struct lp_rast_triangle *tri,
125 int x, int y,
126 int c1,
127 int c2,
128 int c3 )
129 {
130 assert(x >= 0);
131 assert(y >= 0);
132
133 lp_rast_shade_quads(rast_task,
134 &tri->inputs,
135 x, y,
136 -c1, -c2, -c3);
137 }
138
139
140 /**
141 * Evaluate a 16x16 block of pixels to determine which 4x4 subblocks are in/out
142 * of the triangle's bounds.
143 */
144 static void
145 do_block_16( struct lp_rasterizer_task *rast_task,
146 const struct lp_rast_triangle *tri,
147 int x, int y,
148 int c0,
149 int c1,
150 int c2 )
151 {
152 unsigned mask = 0;
153 int eo[3];
154 int c[3];
155 int i, j;
156
157 assert(x >= 0);
158 assert(y >= 0);
159 assert(x % 16 == 0);
160 assert(y % 16 == 0);
161
162 eo[0] = tri->eo1 * 4;
163 eo[1] = tri->eo2 * 4;
164 eo[2] = tri->eo3 * 4;
165
166 c[0] = c0;
167 c[1] = c1;
168 c[2] = c2;
169
170 for (j = 0; j < 3; j++) {
171 const int *step = tri->inputs.step[j];
172 const int cx = c[j] + eo[j];
173
174 /* Mask has bits set whenever we are outside any of the edges.
175 */
176 for (i = 0; i < 16; i++) {
177 int out = cx + step[i] * 4;
178 mask |= (out >> 31) & (1 << i);
179 }
180 }
181
182 mask = ~mask & 0xffff;
183 while (mask) {
184 int i = ffs(mask) - 1;
185 int px = x + pos_table4[i][0];
186 int py = y + pos_table4[i][1];
187 int cx1 = c0 + tri->inputs.step[0][i] * 4;
188 int cx2 = c1 + tri->inputs.step[1][i] * 4;
189 int cx3 = c2 + tri->inputs.step[2][i] * 4;
190
191 mask &= ~(1 << i);
192
193 /* Don't bother testing if the 4x4 block is entirely in/out of
194 * the triangle. It's a little faster to do it in the jit code.
195 */
196 LP_COUNT(nr_non_empty_4);
197 do_block_4(rast_task, tri, px, py, cx1, cx2, cx3);
198 }
199 }
200
201
202 /**
203 * Scan the tile in chunks and figure out which pixels to rasterize
204 * for this triangle.
205 */
206 void
207 lp_rast_triangle( struct lp_rasterizer *rast,
208 unsigned thread_index,
209 const union lp_rast_cmd_arg arg )
210 {
211 struct lp_rasterizer_task *rast_task = &rast->tasks[thread_index];
212 const struct lp_rast_triangle *tri = arg.triangle;
213
214 int x = rast_task->x;
215 int y = rast_task->y;
216 int ei[3], eo[3], c[3];
217 unsigned outmask, inmask, partial_mask;
218 unsigned i, j;
219
220 c[0] = tri->c1 + tri->dx12 * y - tri->dy12 * x;
221 c[1] = tri->c2 + tri->dx23 * y - tri->dy23 * x;
222 c[2] = tri->c3 + tri->dx31 * y - tri->dy31 * x;
223
224 eo[0] = tri->eo1 * 16;
225 eo[1] = tri->eo2 * 16;
226 eo[2] = tri->eo3 * 16;
227
228 ei[0] = tri->ei1 * 16;
229 ei[1] = tri->ei2 * 16;
230 ei[2] = tri->ei3 * 16;
231
232 outmask = 0;
233 inmask = 0xffff;
234
235 for (j = 0; j < 3; j++) {
236 const int *step = tri->inputs.step[j];
237 const int cox = c[j] + eo[j];
238 const int cio = ei[j]- eo[j];
239
240 /* Outmask has bits set whenever we are outside any of the
241 * edges.
242 */
243 /* Inmask has bits set whenever we are inside all of the edges.
244 */
245 for (i = 0; i < 16; i++) {
246 int out = cox + step[i] * 16;
247 int in = out + cio;
248 outmask |= (out >> 31) & (1 << i);
249 inmask &= ~((in >> 31) & (1 << i));
250 }
251 }
252
253 assert((outmask & inmask) == 0);
254
255 if (outmask == 0xffff)
256 return;
257
258 /* Invert mask, so that bits are set whenever we are at least
259 * partially inside all of the edges:
260 */
261 partial_mask = ~inmask & ~outmask & 0xffff;
262
263 /* Iterate over partials:
264 */
265 while (partial_mask) {
266 int i = ffs(partial_mask) - 1;
267 int px = x + pos_table16[i][0];
268 int py = y + pos_table16[i][1];
269 int cx1 = c[0] + tri->inputs.step[0][i] * 16;
270 int cx2 = c[1] + tri->inputs.step[1][i] * 16;
271 int cx3 = c[2] + tri->inputs.step[2][i] * 16;
272
273 partial_mask &= ~(1 << i);
274
275 LP_COUNT(nr_partially_covered_16);
276 do_block_16(rast_task, tri, px, py, cx1, cx2, cx3);
277 }
278
279 /* Iterate over fulls:
280 */
281 while (inmask) {
282 int i = ffs(inmask) - 1;
283 int px = x + pos_table16[i][0];
284 int py = y + pos_table16[i][1];
285
286 inmask &= ~(1 << i);
287
288 LP_COUNT(nr_fully_covered_16);
289 block_full_16(rast_task, tri, px, py);
290 }
291 }