a1f309d4b01de20d5324091ecf3f44a55523fc9e
[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
42 /**
43 * Shade all pixels in a 4x4 block.
44 */
45 static void
46 block_full_4(struct lp_rasterizer_task *task,
47 const struct lp_rast_triangle *tri,
48 int x, int y)
49 {
50 lp_rast_shade_quads_all(task, &tri->inputs, x, y);
51 }
52
53
54 /**
55 * Shade all pixels in a 16x16 block.
56 */
57 static void
58 block_full_16(struct lp_rasterizer_task *task,
59 const struct lp_rast_triangle *tri,
60 int x, int y)
61 {
62 unsigned ix, iy;
63 assert(x % 16 == 0);
64 assert(y % 16 == 0);
65 for (iy = 0; iy < 16; iy += 4)
66 for (ix = 0; ix < 16; ix += 4)
67 block_full_4(task, tri, x + ix, y + iy);
68 }
69
70 #if !defined(PIPE_ARCH_SSE)
71
72 static INLINE unsigned
73 build_mask_linear(int c, int dcdx, int dcdy)
74 {
75 int mask = 0;
76
77 int c0 = c;
78 int c1 = c0 + dcdy;
79 int c2 = c1 + dcdy;
80 int c3 = c2 + dcdy;
81
82 mask |= ((c0 + 0 * dcdx) >> 31) & (1 << 0);
83 mask |= ((c0 + 1 * dcdx) >> 31) & (1 << 1);
84 mask |= ((c0 + 2 * dcdx) >> 31) & (1 << 2);
85 mask |= ((c0 + 3 * dcdx) >> 31) & (1 << 3);
86 mask |= ((c1 + 0 * dcdx) >> 31) & (1 << 4);
87 mask |= ((c1 + 1 * dcdx) >> 31) & (1 << 5);
88 mask |= ((c1 + 2 * dcdx) >> 31) & (1 << 6);
89 mask |= ((c1 + 3 * dcdx) >> 31) & (1 << 7);
90 mask |= ((c2 + 0 * dcdx) >> 31) & (1 << 8);
91 mask |= ((c2 + 1 * dcdx) >> 31) & (1 << 9);
92 mask |= ((c2 + 2 * dcdx) >> 31) & (1 << 10);
93 mask |= ((c2 + 3 * dcdx) >> 31) & (1 << 11);
94 mask |= ((c3 + 0 * dcdx) >> 31) & (1 << 12);
95 mask |= ((c3 + 1 * dcdx) >> 31) & (1 << 13);
96 mask |= ((c3 + 2 * dcdx) >> 31) & (1 << 14);
97 mask |= ((c3 + 3 * dcdx) >> 31) & (1 << 15);
98
99 return mask;
100 }
101
102
103 static INLINE void
104 build_masks(int c,
105 int cdiff,
106 int dcdx,
107 int dcdy,
108 unsigned *outmask,
109 unsigned *partmask)
110 {
111 *outmask |= build_mask_linear(c, dcdx, dcdy);
112 *partmask |= build_mask_linear(c + cdiff, dcdx, dcdy);
113 }
114
115 void
116 lp_rast_triangle_3_16(struct lp_rasterizer_task *task,
117 const union lp_rast_cmd_arg arg)
118 {
119 union lp_rast_cmd_arg arg2;
120 arg2.triangle.tri = arg.triangle.tri;
121 arg2.triangle.plane_mask = (1<<3)-1;
122 lp_rast_triangle_3(task, arg2);
123 }
124
125 void
126 lp_rast_triangle_3_4(struct lp_rasterizer_task *task,
127 const union lp_rast_cmd_arg arg)
128 {
129 lp_rast_triangle_3_16(task, arg);
130 }
131
132 #else
133 #include <emmintrin.h>
134 #include "util/u_sse.h"
135
136
137 static INLINE void
138 build_masks(int c,
139 int cdiff,
140 int dcdx,
141 int dcdy,
142 unsigned *outmask,
143 unsigned *partmask)
144 {
145 __m128i cstep0 = _mm_setr_epi32(c, c+dcdx, c+dcdx*2, c+dcdx*3);
146 __m128i xdcdy = _mm_set1_epi32(dcdy);
147
148 /* Get values across the quad
149 */
150 __m128i cstep1 = _mm_add_epi32(cstep0, xdcdy);
151 __m128i cstep2 = _mm_add_epi32(cstep1, xdcdy);
152 __m128i cstep3 = _mm_add_epi32(cstep2, xdcdy);
153
154 {
155 __m128i cstep01, cstep23, result;
156
157 cstep01 = _mm_packs_epi32(cstep0, cstep1);
158 cstep23 = _mm_packs_epi32(cstep2, cstep3);
159 result = _mm_packs_epi16(cstep01, cstep23);
160
161 *outmask |= _mm_movemask_epi8(result);
162 }
163
164
165 {
166 __m128i cio4 = _mm_set1_epi32(cdiff);
167 __m128i cstep01, cstep23, result;
168
169 cstep0 = _mm_add_epi32(cstep0, cio4);
170 cstep1 = _mm_add_epi32(cstep1, cio4);
171 cstep2 = _mm_add_epi32(cstep2, cio4);
172 cstep3 = _mm_add_epi32(cstep3, cio4);
173
174 cstep01 = _mm_packs_epi32(cstep0, cstep1);
175 cstep23 = _mm_packs_epi32(cstep2, cstep3);
176 result = _mm_packs_epi16(cstep01, cstep23);
177
178 *partmask |= _mm_movemask_epi8(result);
179 }
180 }
181
182
183 static INLINE unsigned
184 build_mask_linear(int c, int dcdx, int dcdy)
185 {
186 __m128i cstep0 = _mm_setr_epi32(c, c+dcdx, c+dcdx*2, c+dcdx*3);
187 __m128i xdcdy = _mm_set1_epi32(dcdy);
188
189 /* Get values across the quad
190 */
191 __m128i cstep1 = _mm_add_epi32(cstep0, xdcdy);
192 __m128i cstep2 = _mm_add_epi32(cstep1, xdcdy);
193 __m128i cstep3 = _mm_add_epi32(cstep2, xdcdy);
194
195 /* pack pairs of results into epi16
196 */
197 __m128i cstep01 = _mm_packs_epi32(cstep0, cstep1);
198 __m128i cstep23 = _mm_packs_epi32(cstep2, cstep3);
199
200 /* pack into epi8, preserving sign bits
201 */
202 __m128i result = _mm_packs_epi16(cstep01, cstep23);
203
204 /* extract sign bits to create mask
205 */
206 return _mm_movemask_epi8(result);
207 }
208
209 static INLINE unsigned
210 sign_bits4(const __m128i *cstep, int cdiff)
211 {
212
213 /* Adjust the step values
214 */
215 __m128i cio4 = _mm_set1_epi32(cdiff);
216 __m128i cstep0 = _mm_add_epi32(cstep[0], cio4);
217 __m128i cstep1 = _mm_add_epi32(cstep[1], cio4);
218 __m128i cstep2 = _mm_add_epi32(cstep[2], cio4);
219 __m128i cstep3 = _mm_add_epi32(cstep[3], cio4);
220
221 /* Pack down to epi8
222 */
223 __m128i cstep01 = _mm_packs_epi32(cstep0, cstep1);
224 __m128i cstep23 = _mm_packs_epi32(cstep2, cstep3);
225 __m128i result = _mm_packs_epi16(cstep01, cstep23);
226
227 /* Extract the sign bits
228 */
229 return _mm_movemask_epi8(result);
230 }
231
232
233 /* Special case for 3 plane triangle which is contained entirely
234 * within a 16x16 block.
235 */
236 void
237 lp_rast_triangle_3_16(struct lp_rasterizer_task *task,
238 const union lp_rast_cmd_arg arg)
239 {
240 const struct lp_rast_triangle *tri = arg.triangle.tri;
241 const struct lp_rast_plane *plane = tri->plane;
242 unsigned mask = arg.triangle.plane_mask;
243 const int x = task->x + (mask & 0xff);
244 const int y = task->y + (mask >> 8);
245 unsigned outmask, inmask, partmask, partial_mask;
246 unsigned j;
247 __m128i cstep4[3][4];
248
249 outmask = 0; /* outside one or more trivial reject planes */
250 partmask = 0; /* outside one or more trivial accept planes */
251
252 for (j = 0; j < 3; j++) {
253 const int dcdx = -plane[j].dcdx * 4;
254 const int dcdy = plane[j].dcdy * 4;
255 __m128i xdcdy = _mm_set1_epi32(dcdy);
256
257 cstep4[j][0] = _mm_setr_epi32(0, dcdx, dcdx*2, dcdx*3);
258 cstep4[j][1] = _mm_add_epi32(cstep4[j][0], xdcdy);
259 cstep4[j][2] = _mm_add_epi32(cstep4[j][1], xdcdy);
260 cstep4[j][3] = _mm_add_epi32(cstep4[j][2], xdcdy);
261
262 {
263 const int c = plane[j].c + plane[j].dcdy * y - plane[j].dcdx * x;
264 const int cox = plane[j].eo * 4;
265 const int cio = plane[j].ei * 4 - 1;
266
267 outmask |= sign_bits4(cstep4[j], c + cox);
268 partmask |= sign_bits4(cstep4[j], c + cio);
269 }
270 }
271
272 if (outmask == 0xffff)
273 return;
274
275 /* Mask of sub-blocks which are inside all trivial accept planes:
276 */
277 inmask = ~partmask & 0xffff;
278
279 /* Mask of sub-blocks which are inside all trivial reject planes,
280 * but outside at least one trivial accept plane:
281 */
282 partial_mask = partmask & ~outmask;
283
284 assert((partial_mask & inmask) == 0);
285
286 /* Iterate over partials:
287 */
288 while (partial_mask) {
289 int i = ffs(partial_mask) - 1;
290 int ix = (i & 3) * 4;
291 int iy = (i >> 2) * 4;
292 int px = x + ix;
293 int py = y + iy;
294 unsigned mask = 0xffff;
295
296 partial_mask &= ~(1 << i);
297
298 for (j = 0; j < 3; j++) {
299 const int cx = (plane[j].c
300 - plane[j].dcdx * px
301 + plane[j].dcdy * py) * 4;
302
303 mask &= ~sign_bits4(cstep4[j], cx);
304 }
305
306 if (mask)
307 lp_rast_shade_quads_mask(task, &tri->inputs, px, py, mask);
308 }
309
310 /* Iterate over fulls:
311 */
312 while (inmask) {
313 int i = ffs(inmask) - 1;
314 int ix = (i & 3) * 4;
315 int iy = (i >> 2) * 4;
316 int px = x + ix;
317 int py = y + iy;
318
319 inmask &= ~(1 << i);
320
321 block_full_4(task, tri, px, py);
322 }
323 }
324
325
326 void
327 lp_rast_triangle_3_4(struct lp_rasterizer_task *task,
328 const union lp_rast_cmd_arg arg)
329 {
330 const struct lp_rast_triangle *tri = arg.triangle.tri;
331 const struct lp_rast_plane *plane = tri->plane;
332 unsigned mask = arg.triangle.plane_mask;
333 const int x = task->x + (mask & 0xff);
334 const int y = task->y + (mask >> 8);
335 unsigned j;
336
337 /* Iterate over partials:
338 */
339 {
340 unsigned mask = 0xffff;
341
342 for (j = 0; j < 3; j++) {
343 const int cx = (plane[j].c
344 - plane[j].dcdx * x
345 + plane[j].dcdy * y);
346
347 const int dcdx = -plane[j].dcdx;
348 const int dcdy = plane[j].dcdy;
349 __m128i xdcdy = _mm_set1_epi32(dcdy);
350
351 __m128i cstep0 = _mm_setr_epi32(cx, cx + dcdx, cx + dcdx*2, cx + dcdx*3);
352 __m128i cstep1 = _mm_add_epi32(cstep0, xdcdy);
353 __m128i cstep2 = _mm_add_epi32(cstep1, xdcdy);
354 __m128i cstep3 = _mm_add_epi32(cstep2, xdcdy);
355
356 __m128i cstep01 = _mm_packs_epi32(cstep0, cstep1);
357 __m128i cstep23 = _mm_packs_epi32(cstep2, cstep3);
358 __m128i result = _mm_packs_epi16(cstep01, cstep23);
359
360 /* Extract the sign bits
361 */
362 mask &= ~_mm_movemask_epi8(result);
363 }
364
365 if (mask)
366 lp_rast_shade_quads_mask(task, &tri->inputs, x, y, mask);
367 }
368 }
369
370
371 #endif
372
373
374
375
376 #define TAG(x) x##_1
377 #define NR_PLANES 1
378 #include "lp_rast_tri_tmp.h"
379
380 #define TAG(x) x##_2
381 #define NR_PLANES 2
382 #include "lp_rast_tri_tmp.h"
383
384 #define TAG(x) x##_3
385 #define NR_PLANES 3
386 #include "lp_rast_tri_tmp.h"
387
388 #define TAG(x) x##_4
389 #define NR_PLANES 4
390 #include "lp_rast_tri_tmp.h"
391
392 #define TAG(x) x##_5
393 #define NR_PLANES 5
394 #include "lp_rast_tri_tmp.h"
395
396 #define TAG(x) x##_6
397 #define NR_PLANES 6
398 #include "lp_rast_tri_tmp.h"
399
400 #define TAG(x) x##_7
401 #define NR_PLANES 7
402 #include "lp_rast_tri_tmp.h"
403
404 #define TAG(x) x##_8
405 #define NR_PLANES 8
406 #include "lp_rast_tri_tmp.h"
407