1 /**************************************************************************
2  *
3  * Copyright 2007 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  * Binning code for triangles
30  */
31 
32 #include "util/u_math.h"
33 #include "util/u_memory.h"
34 #include "util/u_rect.h"
35 #include "util/u_sse.h"
36 #include "lp_perf.h"
37 #include "lp_setup_context.h"
38 #include "lp_rast.h"
39 #include "lp_state_fs.h"
40 #include "lp_state_setup.h"
41 #include "lp_context.h"
42 
43 #include <inttypes.h>
44 
45 #define NUM_CHANNELS 4
46 
47 #if defined(PIPE_ARCH_SSE)
48 #include <emmintrin.h>
49 #elif defined(_ARCH_PWR8) && UTIL_ARCH_LITTLE_ENDIAN
50 #include <altivec.h>
51 #include "util/u_pwr8.h"
52 #endif
53 
54 #if !defined(PIPE_ARCH_SSE)
55 
56 static inline int
subpixel_snap(float a)57 subpixel_snap(float a)
58 {
59    return util_iround(FIXED_ONE * a);
60 }
61 
62 #endif
63 
64 /* Position and area in fixed point coordinates */
65 struct fixed_position {
66    int32_t x[4];
67    int32_t y[4];
68    int32_t dx01;
69    int32_t dy01;
70    int32_t dx20;
71    int32_t dy20;
72 };
73 
74 
75 /**
76  * Alloc space for a new triangle plus the input.a0/dadx/dady arrays
77  * immediately after it.
78  * The memory is allocated from the per-scene pool, not per-tile.
79  * \param tri_size  returns number of bytes allocated
80  * \param num_inputs  number of fragment shader inputs
81  * \return pointer to triangle space
82  */
83 struct lp_rast_triangle *
lp_setup_alloc_triangle(struct lp_scene * scene,unsigned nr_inputs,unsigned nr_planes,unsigned * tri_size)84 lp_setup_alloc_triangle(struct lp_scene *scene,
85                         unsigned nr_inputs,
86                         unsigned nr_planes,
87                         unsigned *tri_size)
88 {
89    unsigned input_array_sz = NUM_CHANNELS * (nr_inputs + 1) * sizeof(float);
90    unsigned plane_sz = nr_planes * sizeof(struct lp_rast_plane);
91    struct lp_rast_triangle *tri;
92 
93    STATIC_ASSERT(sizeof(struct lp_rast_plane) % 8 == 0);
94 
95    *tri_size = (sizeof(struct lp_rast_triangle) +
96                 3 * input_array_sz +
97                 plane_sz);
98 
99    tri = lp_scene_alloc_aligned( scene, *tri_size, 16 );
100    if (!tri)
101       return NULL;
102 
103    tri->inputs.stride = input_array_sz;
104 
105    {
106       ASSERTED char *a = (char *)tri;
107       ASSERTED char *b = (char *)&GET_PLANES(tri)[nr_planes];
108 
109       assert(b - a == *tri_size);
110    }
111 
112    return tri;
113 }
114 
115 void
lp_setup_print_vertex(struct lp_setup_context * setup,const char * name,const float (* v)[4])116 lp_setup_print_vertex(struct lp_setup_context *setup,
117                       const char *name,
118                       const float (*v)[4])
119 {
120    const struct lp_setup_variant_key *key = &setup->setup.variant->key;
121    int i, j;
122 
123    debug_printf("   wpos (%s[0]) xyzw %f %f %f %f\n",
124                 name,
125                 v[0][0], v[0][1], v[0][2], v[0][3]);
126 
127    for (i = 0; i < key->num_inputs; i++) {
128       const float *in = v[key->inputs[i].src_index];
129 
130       debug_printf("  in[%d] (%s[%d]) %s%s%s%s ",
131                    i,
132                    name, key->inputs[i].src_index,
133                    (key->inputs[i].usage_mask & 0x1) ? "x" : " ",
134                    (key->inputs[i].usage_mask & 0x2) ? "y" : " ",
135                    (key->inputs[i].usage_mask & 0x4) ? "z" : " ",
136                    (key->inputs[i].usage_mask & 0x8) ? "w" : " ");
137 
138       for (j = 0; j < 4; j++)
139          if (key->inputs[i].usage_mask & (1<<j))
140             debug_printf("%.5f ", in[j]);
141 
142       debug_printf("\n");
143    }
144 }
145 
146 
147 /**
148  * Print triangle vertex attribs (for debug).
149  */
150 void
lp_setup_print_triangle(struct lp_setup_context * setup,const float (* v0)[4],const float (* v1)[4],const float (* v2)[4])151 lp_setup_print_triangle(struct lp_setup_context *setup,
152                         const float (*v0)[4],
153                         const float (*v1)[4],
154                         const float (*v2)[4])
155 {
156    debug_printf("triangle\n");
157 
158    {
159       const float ex = v0[0][0] - v2[0][0];
160       const float ey = v0[0][1] - v2[0][1];
161       const float fx = v1[0][0] - v2[0][0];
162       const float fy = v1[0][1] - v2[0][1];
163 
164       /* det = cross(e,f).z */
165       const float det = ex * fy - ey * fx;
166       if (det < 0.0f)
167          debug_printf("   - ccw\n");
168       else if (det > 0.0f)
169          debug_printf("   - cw\n");
170       else
171          debug_printf("   - zero area\n");
172    }
173 
174    lp_setup_print_vertex(setup, "v0", v0);
175    lp_setup_print_vertex(setup, "v1", v1);
176    lp_setup_print_vertex(setup, "v2", v2);
177 }
178 
179 
180 #define MAX_PLANES 8
181 static unsigned
182 lp_rast_tri_tab[MAX_PLANES+1] = {
183    0,               /* should be impossible */
184    LP_RAST_OP_TRIANGLE_1,
185    LP_RAST_OP_TRIANGLE_2,
186    LP_RAST_OP_TRIANGLE_3,
187    LP_RAST_OP_TRIANGLE_4,
188    LP_RAST_OP_TRIANGLE_5,
189    LP_RAST_OP_TRIANGLE_6,
190    LP_RAST_OP_TRIANGLE_7,
191    LP_RAST_OP_TRIANGLE_8
192 };
193 
194 static unsigned
195 lp_rast_32_tri_tab[MAX_PLANES+1] = {
196    0,               /* should be impossible */
197    LP_RAST_OP_TRIANGLE_32_1,
198    LP_RAST_OP_TRIANGLE_32_2,
199    LP_RAST_OP_TRIANGLE_32_3,
200    LP_RAST_OP_TRIANGLE_32_4,
201    LP_RAST_OP_TRIANGLE_32_5,
202    LP_RAST_OP_TRIANGLE_32_6,
203    LP_RAST_OP_TRIANGLE_32_7,
204    LP_RAST_OP_TRIANGLE_32_8
205 };
206 
207 
208 static unsigned
209 lp_rast_ms_tri_tab[MAX_PLANES+1] = {
210    0,               /* should be impossible */
211    LP_RAST_OP_MS_TRIANGLE_1,
212    LP_RAST_OP_MS_TRIANGLE_2,
213    LP_RAST_OP_MS_TRIANGLE_3,
214    LP_RAST_OP_MS_TRIANGLE_4,
215    LP_RAST_OP_MS_TRIANGLE_5,
216    LP_RAST_OP_MS_TRIANGLE_6,
217    LP_RAST_OP_MS_TRIANGLE_7,
218    LP_RAST_OP_MS_TRIANGLE_8
219 };
220 
221 /*
222  * Detect big primitives drawn with an alpha == 1.0.
223  *
224  * This is used when simulating anti-aliasing primitives in shaders, e.g.,
225  * when drawing the windows client area in Aero's flip-3d effect.
226  */
227 static boolean
check_opaque(struct lp_setup_context * setup,const float (* v1)[4],const float (* v2)[4],const float (* v3)[4])228 check_opaque(struct lp_setup_context *setup,
229              const float (*v1)[4],
230              const float (*v2)[4],
231              const float (*v3)[4])
232 {
233    const struct lp_fragment_shader_variant *variant =
234       setup->fs.current.variant;
235 
236    if (variant->opaque)
237       return TRUE;
238 
239    if (!variant->potentially_opaque)
240       return FALSE;
241 
242    const struct lp_tgsi_channel_info *alpha_info = &variant->shader->info.cbuf[0][3];
243    if (alpha_info->file == TGSI_FILE_CONSTANT) {
244       const float *constants = setup->fs.current.jit_context.constants[0];
245       float alpha = constants[alpha_info->u.index*4 +
246                               alpha_info->swizzle];
247       return alpha == 1.0f;
248    }
249 
250    if (alpha_info->file == TGSI_FILE_INPUT) {
251       return (v1[1 + alpha_info->u.index][alpha_info->swizzle] == 1.0f &&
252               v2[1 + alpha_info->u.index][alpha_info->swizzle] == 1.0f &&
253               v3[1 + alpha_info->u.index][alpha_info->swizzle] == 1.0f);
254    }
255 
256    return FALSE;
257 }
258 
259 
260 
261 /**
262  * Do basic setup for triangle rasterization and determine which
263  * framebuffer tiles are touched.  Put the triangle in the scene's
264  * bins for the tiles which we overlap.
265  */
266 static boolean
do_triangle_ccw(struct lp_setup_context * setup,struct fixed_position * position,const float (* v0)[4],const float (* v1)[4],const float (* v2)[4],boolean frontfacing)267 do_triangle_ccw(struct lp_setup_context *setup,
268                 struct fixed_position* position,
269                 const float (*v0)[4],
270                 const float (*v1)[4],
271                 const float (*v2)[4],
272                 boolean frontfacing )
273 {
274    struct lp_scene *scene = setup->scene;
275    const struct lp_setup_variant_key *key = &setup->setup.variant->key;
276    struct lp_rast_triangle *tri;
277    struct lp_rast_plane *plane;
278    const struct u_rect *scissor = NULL;
279    struct u_rect bbox;
280    boolean s_planes[4];
281    unsigned tri_bytes;
282    int nr_planes = 3;
283    unsigned viewport_index = 0;
284    unsigned layer = 0;
285    const float (*pv)[4];
286 
287    if (0)
288       lp_setup_print_triangle(setup, v0, v1, v2);
289 
290    if (setup->flatshade_first) {
291       pv = v0;
292    }
293    else {
294       pv = v2;
295    }
296    if (setup->viewport_index_slot > 0) {
297       unsigned *udata = (unsigned*)pv[setup->viewport_index_slot];
298       viewport_index = lp_clamp_viewport_idx(*udata);
299    }
300    if (setup->layer_slot > 0) {
301       layer = *(unsigned*)pv[setup->layer_slot];
302       layer = MIN2(layer, scene->fb_max_layer);
303    }
304 
305    /* Bounding rectangle (in pixels) */
306    {
307       /* Yes this is necessary to accurately calculate bounding boxes
308        * with the two fill-conventions we support.  GL (normally) ends
309        * up needing a bottom-left fill convention, which requires
310        * slightly different rounding.
311        */
312       int adj = (setup->bottom_edge_rule != 0) ? 1 : 0;
313 
314       /* Inclusive x0, exclusive x1 */
315       bbox.x0 =  MIN3(position->x[0], position->x[1], position->x[2]) >> FIXED_ORDER;
316       bbox.x1 = (MAX3(position->x[0], position->x[1], position->x[2]) - 1) >> FIXED_ORDER;
317 
318       /* Inclusive / exclusive depending upon adj (bottom-left or top-right) */
319       bbox.y0 = (MIN3(position->y[0], position->y[1], position->y[2]) + adj) >> FIXED_ORDER;
320       bbox.y1 = (MAX3(position->y[0], position->y[1], position->y[2]) - 1 + adj) >> FIXED_ORDER;
321    }
322 
323    if (!u_rect_test_intersection(&setup->draw_regions[viewport_index], &bbox)) {
324       if (0) debug_printf("no intersection\n");
325       LP_COUNT(nr_culled_tris);
326       return TRUE;
327    }
328 
329    int max_szorig = ((bbox.x1 - (bbox.x0 & ~3)) |
330                      (bbox.y1 - (bbox.y0 & ~3)));
331    boolean use_32bits = max_szorig <= MAX_FIXED_LENGTH32;
332 #if defined(_ARCH_PWR8) && UTIL_ARCH_LITTLE_ENDIAN
333    boolean pwr8_limit_check = (bbox.x1 - bbox.x0) <= MAX_FIXED_LENGTH32 &&
334       (bbox.y1 - bbox.y0) <= MAX_FIXED_LENGTH32;
335 #endif
336 
337    /* Can safely discard negative regions, but need to keep hold of
338     * information about when the triangle extends past screen
339     * boundaries.  See trimmed_box in lp_setup_bin_triangle().
340     */
341    bbox.x0 = MAX2(bbox.x0, 0);
342    bbox.y0 = MAX2(bbox.y0, 0);
343 
344    nr_planes = 3;
345    /*
346     * Determine how many scissor planes we need, that is drop scissor
347     * edges if the bounding box of the tri is fully inside that edge.
348     */
349    scissor = &setup->draw_regions[viewport_index];
350    scissor_planes_needed(s_planes, &bbox, scissor);
351    nr_planes += s_planes[0] + s_planes[1] + s_planes[2] + s_planes[3];
352 
353    tri = lp_setup_alloc_triangle(scene,
354                                  key->num_inputs,
355                                  nr_planes,
356                                  &tri_bytes);
357    if (!tri)
358       return FALSE;
359 
360 #ifdef DEBUG
361    tri->v[0][0] = v0[0][0];
362    tri->v[1][0] = v1[0][0];
363    tri->v[2][0] = v2[0][0];
364    tri->v[0][1] = v0[0][1];
365    tri->v[1][1] = v1[0][1];
366    tri->v[2][1] = v2[0][1];
367 #endif
368 
369    LP_COUNT(nr_tris);
370 
371    /*
372     * Rotate the tri such that v0 is closest to the fb origin.
373     * This can give more accurate a0 value (which is at fb origin)
374     * when calculating the interpolants.
375     * It can't work when there's flat shading for instance in one
376     * of the attributes, hence restrict this to just a single attribute
377     * which is what causes some test failures.
378     * (This does not address the problem that interpolation may be
379     * inaccurate if gradients are relatively steep in small tris far
380     * away from the origin. It does however fix the (silly) wgf11rasterizer
381     * Interpolator test.)
382     * XXX This causes problems with mipgen -EmuTexture for not yet really
383     * understood reasons (if the vertices would be submitted in a different
384     * order, we'd also generate the same "wrong" results here without
385     * rotation). In any case, that we generate different values if a prim
386     * has the vertices rotated but is otherwise the same (which is due to
387     * numerical issues) is not a nice property. An additional problem by
388     * swapping the vertices here (which is possibly worse) is that
389     * the same primitive coming in twice might generate different values
390     * (in particular for z) due to the swapping potentially not happening
391     * both times, if the attributes to be interpolated are different. For now,
392     * just restrict this to not get used with dx9 (by checking pixel offset),
393     * could also restrict it further to only trigger with wgf11Interpolator
394     * Rasterizer test (the only place which needs it, with always the same
395     * vertices even).
396     */
397    if ((LP_DEBUG & DEBUG_ACCURATE_A0) &&
398        setup->pixel_offset == 0.5f &&
399        key->num_inputs == 1 &&
400        (key->inputs[0].interp == LP_INTERP_LINEAR ||
401         key->inputs[0].interp == LP_INTERP_PERSPECTIVE)) {
402       float dist0 = v0[0][0] * v0[0][0] + v0[0][1] * v0[0][1];
403       float dist1 = v1[0][0] * v1[0][0] + v1[0][1] * v1[0][1];
404       float dist2 = v2[0][0] * v2[0][0] + v2[0][1] * v2[0][1];
405       if (dist0 > dist1 && dist1 < dist2) {
406          const float (*vt)[4];
407          int x, y;
408          vt = v0;
409          v0 = v1;
410          v1 = v2;
411          v2 = vt;
412          x = position->x[0];
413          y = position->y[0];
414          position->x[0] = position->x[1];
415          position->y[0] = position->y[1];
416          position->x[1] = position->x[2];
417          position->y[1] = position->y[2];
418          position->x[2] = x;
419          position->y[2] = y;
420 
421          position->dx20 = position->dx01;
422          position->dy20 = position->dy01;
423          position->dx01 = position->x[0] - position->x[1];
424          position->dy01 = position->y[0] - position->y[1];
425       }
426       else if (dist0 > dist2) {
427          const float (*vt)[4];
428          int x, y;
429          vt = v0;
430          v0 = v2;
431          v2 = v1;
432          v1 = vt;
433          x = position->x[0];
434          y = position->y[0];
435          position->x[0] = position->x[2];
436          position->y[0] = position->y[2];
437          position->x[2] = position->x[1];
438          position->y[2] = position->y[1];
439          position->x[1] = x;
440          position->y[1] = y;
441 
442          position->dx01 = position->dx20;
443          position->dy01 = position->dy20;
444          position->dx20 = position->x[2] - position->x[0];
445          position->dy20 = position->y[2] - position->y[0];
446       }
447    }
448 
449    /* Setup parameter interpolants:
450     */
451    setup->setup.variant->jit_function(v0, v1, v2,
452                                       frontfacing,
453                                       GET_A0(&tri->inputs),
454                                       GET_DADX(&tri->inputs),
455                                       GET_DADY(&tri->inputs),
456                                       &setup->setup.variant->key);
457 
458    tri->inputs.frontfacing = frontfacing;
459    tri->inputs.disable = FALSE;
460    tri->inputs.is_blit = FALSE;
461    tri->inputs.layer = layer;
462    tri->inputs.viewport_index = viewport_index;
463    tri->inputs.view_index = setup->view_index;
464 
465    if (0)
466       lp_dump_setup_coef(&setup->setup.variant->key,
467                          (const float (*)[4])GET_A0(&tri->inputs),
468                          (const float (*)[4])GET_DADX(&tri->inputs),
469                          (const float (*)[4])GET_DADY(&tri->inputs));
470 
471    plane = GET_PLANES(tri);
472 
473 #if defined(PIPE_ARCH_SSE)
474    if (1) {
475       __m128i vertx, verty;
476       __m128i shufx, shufy;
477       __m128i dcdx, dcdy;
478       __m128i cdx02, cdx13, cdy02, cdy13, c02, c13;
479       __m128i c01, c23, unused;
480       __m128i dcdx_neg_mask;
481       __m128i dcdy_neg_mask;
482       __m128i dcdx_zero_mask;
483       __m128i top_left_flag, c_dec;
484       __m128i eo, p0, p1, p2;
485       __m128i zero = _mm_setzero_si128();
486 
487       vertx = _mm_load_si128((__m128i *)position->x); /* vertex x coords */
488       verty = _mm_load_si128((__m128i *)position->y); /* vertex y coords */
489 
490       shufx = _mm_shuffle_epi32(vertx, _MM_SHUFFLE(3,0,2,1));
491       shufy = _mm_shuffle_epi32(verty, _MM_SHUFFLE(3,0,2,1));
492 
493       dcdx = _mm_sub_epi32(verty, shufy);
494       dcdy = _mm_sub_epi32(vertx, shufx);
495 
496       dcdx_neg_mask = _mm_srai_epi32(dcdx, 31);
497       dcdx_zero_mask = _mm_cmpeq_epi32(dcdx, zero);
498       dcdy_neg_mask = _mm_srai_epi32(dcdy, 31);
499 
500       top_left_flag = _mm_set1_epi32((setup->bottom_edge_rule == 0) ? ~0 : 0);
501 
502       c_dec = _mm_or_si128(dcdx_neg_mask,
503                            _mm_and_si128(dcdx_zero_mask,
504                                          _mm_xor_si128(dcdy_neg_mask,
505                                                        top_left_flag)));
506 
507       /*
508        * 64 bit arithmetic.
509        * Note we need _signed_ mul (_mm_mul_epi32) which we emulate.
510        */
511       cdx02 = mm_mullohi_epi32(dcdx, vertx, &cdx13);
512       cdy02 = mm_mullohi_epi32(dcdy, verty, &cdy13);
513       c02 = _mm_sub_epi64(cdx02, cdy02);
514       c13 = _mm_sub_epi64(cdx13, cdy13);
515       c02 = _mm_sub_epi64(c02, _mm_shuffle_epi32(c_dec,
516                                                  _MM_SHUFFLE(2,2,0,0)));
517       c13 = _mm_sub_epi64(c13, _mm_shuffle_epi32(c_dec,
518                                                  _MM_SHUFFLE(3,3,1,1)));
519 
520       /*
521        * Useful for very small fbs/tris (or fewer subpixel bits) only:
522        * c = _mm_sub_epi32(mm_mullo_epi32(dcdx, vertx),
523        *                   mm_mullo_epi32(dcdy, verty));
524        *
525        * c = _mm_sub_epi32(c, c_dec);
526        */
527 
528       /* Scale up to match c:
529        */
530       dcdx = _mm_slli_epi32(dcdx, FIXED_ORDER);
531       dcdy = _mm_slli_epi32(dcdy, FIXED_ORDER);
532 
533       /*
534        * Calculate trivial reject values:
535        * Note eo cannot overflow even if dcdx/dcdy would already have
536        * 31 bits (which they shouldn't have). This is because eo
537        * is never negative (albeit if we rely on that need to be careful...)
538        */
539       eo = _mm_sub_epi32(_mm_andnot_si128(dcdy_neg_mask, dcdy),
540                          _mm_and_si128(dcdx_neg_mask, dcdx));
541 
542       /* ei = _mm_sub_epi32(_mm_sub_epi32(dcdy, dcdx), eo); */
543 
544       /*
545        * Pointless transpose which gets undone immediately in
546        * rasterization.
547        * It is actually difficult to do away with it - would essentially
548        * need GET_PLANES_DX, GET_PLANES_DY etc., but the calculations
549        * for this then would need to depend on the number of planes.
550        * The transpose is quite special here due to c being 64bit...
551        * The store has to be unaligned (unless we'd make the plane size
552        * a multiple of 128), and of course storing eo separately...
553        */
554       c01 = _mm_unpacklo_epi64(c02, c13);
555       c23 = _mm_unpackhi_epi64(c02, c13);
556       transpose2_64_2_32(&c01, &c23, &dcdx, &dcdy,
557                          &p0, &p1, &p2, &unused);
558       _mm_storeu_si128((__m128i *)&plane[0], p0);
559       plane[0].eo = (uint32_t)_mm_cvtsi128_si32(eo);
560       _mm_storeu_si128((__m128i *)&plane[1], p1);
561       eo = _mm_shuffle_epi32(eo, _MM_SHUFFLE(3,2,0,1));
562       plane[1].eo = (uint32_t)_mm_cvtsi128_si32(eo);
563       _mm_storeu_si128((__m128i *)&plane[2], p2);
564       eo = _mm_shuffle_epi32(eo, _MM_SHUFFLE(0,0,0,2));
565       plane[2].eo = (uint32_t)_mm_cvtsi128_si32(eo);
566    } else
567 #elif defined(_ARCH_PWR8) && UTIL_ARCH_LITTLE_ENDIAN
568    /*
569     * XXX this code is effectively disabled for all practical purposes,
570     * as the allowed fb size is tiny if FIXED_ORDER is 8.
571     */
572    if (setup->fb.width <= MAX_FIXED_LENGTH32 &&
573        setup->fb.height <= MAX_FIXED_LENGTH32 &&
574        pwr8_limit_check) {
575       unsigned int bottom_edge;
576       __m128i vertx, verty;
577       __m128i shufx, shufy;
578       __m128i dcdx, dcdy, c;
579       __m128i unused;
580       __m128i dcdx_neg_mask;
581       __m128i dcdy_neg_mask;
582       __m128i dcdx_zero_mask;
583       __m128i top_left_flag;
584       __m128i c_inc_mask, c_inc;
585       __m128i eo, p0, p1, p2;
586       __m128i_union vshuf_mask;
587       __m128i zero = vec_splats((unsigned char) 0);
588       PIPE_ALIGN_VAR(16) int32_t temp_vec[4];
589 
590 #if UTIL_ARCH_LITTLE_ENDIAN
591       vshuf_mask.i[0] = 0x07060504;
592       vshuf_mask.i[1] = 0x0B0A0908;
593       vshuf_mask.i[2] = 0x03020100;
594       vshuf_mask.i[3] = 0x0F0E0D0C;
595 #else
596       vshuf_mask.i[0] = 0x00010203;
597       vshuf_mask.i[1] = 0x0C0D0E0F;
598       vshuf_mask.i[2] = 0x04050607;
599       vshuf_mask.i[3] = 0x08090A0B;
600 #endif
601 
602       /* vertex x coords */
603       vertx = vec_load_si128((const uint32_t *) position->x);
604       /* vertex y coords */
605       verty = vec_load_si128((const uint32_t *) position->y);
606 
607       shufx = vec_perm (vertx, vertx, vshuf_mask.m128i);
608       shufy = vec_perm (verty, verty, vshuf_mask.m128i);
609 
610       dcdx = vec_sub_epi32(verty, shufy);
611       dcdy = vec_sub_epi32(vertx, shufx);
612 
613       dcdx_neg_mask = vec_srai_epi32(dcdx, 31);
614       dcdx_zero_mask = vec_cmpeq_epi32(dcdx, zero);
615       dcdy_neg_mask = vec_srai_epi32(dcdy, 31);
616 
617       bottom_edge = (setup->bottom_edge_rule == 0) ? ~0 : 0;
618       top_left_flag = (__m128i) vec_splats(bottom_edge);
619 
620       c_inc_mask = vec_or(dcdx_neg_mask,
621                                 vec_and(dcdx_zero_mask,
622                                               vec_xor(dcdy_neg_mask,
623                                                             top_left_flag)));
624 
625       c_inc = vec_srli_epi32(c_inc_mask, 31);
626 
627       c = vec_sub_epi32(vec_mullo_epi32(dcdx, vertx),
628                         vec_mullo_epi32(dcdy, verty));
629 
630       c = vec_add_epi32(c, c_inc);
631 
632       /* Scale up to match c:
633        */
634       dcdx = vec_slli_epi32(dcdx, FIXED_ORDER);
635       dcdy = vec_slli_epi32(dcdy, FIXED_ORDER);
636 
637       /* Calculate trivial reject values:
638        */
639       eo = vec_sub_epi32(vec_andnot_si128(dcdy_neg_mask, dcdy),
640                          vec_and(dcdx_neg_mask, dcdx));
641 
642       /* ei = _mm_sub_epi32(_mm_sub_epi32(dcdy, dcdx), eo); */
643 
644       /* Pointless transpose which gets undone immediately in
645        * rasterization:
646        */
647       transpose4_epi32(&c, &dcdx, &dcdy, &eo,
648                        &p0, &p1, &p2, &unused);
649 
650 #define STORE_PLANE(plane, vec) do {                  \
651          vec_store_si128((uint32_t *)&temp_vec, vec); \
652          plane.c    = (int64_t)temp_vec[0];           \
653          plane.dcdx = temp_vec[1];                    \
654          plane.dcdy = temp_vec[2];                    \
655          plane.eo   = temp_vec[3];                    \
656       } while(0)
657 
658       STORE_PLANE(plane[0], p0);
659       STORE_PLANE(plane[1], p1);
660       STORE_PLANE(plane[2], p2);
661 #undef STORE_PLANE
662    } else
663 #endif
664    {
665       int i;
666       plane[0].dcdy = position->dx01;
667       plane[1].dcdy = position->x[1] - position->x[2];
668       plane[2].dcdy = position->dx20;
669       plane[0].dcdx = position->dy01;
670       plane[1].dcdx = position->y[1] - position->y[2];
671       plane[2].dcdx = position->dy20;
672 
673       for (i = 0; i < 3; i++) {
674          /* half-edge constants, will be iterated over the whole render
675           * target.
676           */
677          plane[i].c = IMUL64(plane[i].dcdx, position->x[i]) -
678                       IMUL64(plane[i].dcdy, position->y[i]);
679 
680          /* correct for top-left vs. bottom-left fill convention.
681           */
682          if (plane[i].dcdx < 0) {
683             /* both fill conventions want this - adjust for left edges */
684             plane[i].c++;
685          }
686          else if (plane[i].dcdx == 0) {
687             if (setup->bottom_edge_rule == 0){
688                /* correct for top-left fill convention:
689                 */
690                if (plane[i].dcdy > 0) plane[i].c++;
691             }
692             else {
693                /* correct for bottom-left fill convention:
694                 */
695                if (plane[i].dcdy < 0) plane[i].c++;
696             }
697          }
698 
699          /* Scale up to match c:
700           */
701          assert((plane[i].dcdx << FIXED_ORDER) >> FIXED_ORDER == plane[i].dcdx);
702          assert((plane[i].dcdy << FIXED_ORDER) >> FIXED_ORDER == plane[i].dcdy);
703          plane[i].dcdx <<= FIXED_ORDER;
704          plane[i].dcdy <<= FIXED_ORDER;
705 
706          /* find trivial reject offsets for each edge for a single-pixel
707           * sized block.  These will be scaled up at each recursive level to
708           * match the active blocksize.  Scaling in this way works best if
709           * the blocks are square.
710           */
711          plane[i].eo = 0;
712          if (plane[i].dcdx < 0) plane[i].eo -= plane[i].dcdx;
713          if (plane[i].dcdy > 0) plane[i].eo += plane[i].dcdy;
714       }
715    }
716 
717    if (0) {
718       debug_printf("p0: %"PRIx64"/%08x/%08x/%08x\n",
719                    plane[0].c,
720                    plane[0].dcdx,
721                    plane[0].dcdy,
722                    plane[0].eo);
723 
724       debug_printf("p1: %"PRIx64"/%08x/%08x/%08x\n",
725                    plane[1].c,
726                    plane[1].dcdx,
727                    plane[1].dcdy,
728                    plane[1].eo);
729 
730       debug_printf("p2: %"PRIx64"/%08x/%08x/%08x\n",
731                    plane[2].c,
732                    plane[2].dcdx,
733                    plane[2].dcdy,
734                    plane[2].eo);
735    }
736 
737    if (nr_planes > 3) {
738       lp_setup_add_scissor_planes(scissor, &plane[3], s_planes, setup->multisample);
739    }
740 
741    return lp_setup_bin_triangle(setup, tri, use_32bits,
742                                 check_opaque(setup, v0, v1, v2),
743                                 &bbox, nr_planes, viewport_index);
744 }
745 
746 /*
747  * Round to nearest less or equal power of two of the input.
748  *
749  * Undefined if no bit set exists, so code should check against 0 first.
750  */
751 static inline uint32_t
floor_pot(uint32_t n)752 floor_pot(uint32_t n)
753 {
754 #if defined(PIPE_CC_GCC) && (defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64))
755    if (n == 0)
756       return 0;
757 
758    __asm__("bsr %1,%0"
759           : "=r" (n)
760           : "rm" (n)
761           : "cc");
762    return 1 << n;
763 #else
764    n |= (n >>  1);
765    n |= (n >>  2);
766    n |= (n >>  4);
767    n |= (n >>  8);
768    n |= (n >> 16);
769    return n - (n >> 1);
770 #endif
771 }
772 
773 
774 boolean
lp_setup_bin_triangle(struct lp_setup_context * setup,struct lp_rast_triangle * tri,boolean use_32bits,boolean opaque,const struct u_rect * bbox,int nr_planes,unsigned viewport_index)775 lp_setup_bin_triangle(struct lp_setup_context *setup,
776                       struct lp_rast_triangle *tri,
777                       boolean use_32bits,
778                       boolean opaque,
779                       const struct u_rect *bbox,
780                       int nr_planes,
781                       unsigned viewport_index)
782 {
783    struct lp_scene *scene = setup->scene;
784    struct u_rect trimmed_box = *bbox;
785    int i;
786    unsigned cmd;
787 
788    /* What is the largest power-of-two boundary this triangle crosses:
789     */
790    int dx = floor_pot((bbox->x0 ^ bbox->x1) |
791 		      (bbox->y0 ^ bbox->y1));
792 
793    /* The largest dimension of the rasterized area of the triangle
794     * (aligned to a 4x4 grid), rounded down to the nearest power of two:
795     */
796    int max_sz = ((bbox->x1 - (bbox->x0 & ~3)) |
797                  (bbox->y1 - (bbox->y0 & ~3)));
798    int sz = floor_pot(max_sz);
799 
800    /*
801     * NOTE: It is important to use the original bounding box
802     * which might contain negative values here, because if the
803     * plane math may overflow or not with the 32bit rasterization
804     * functions depends on the original extent of the triangle.
805     */
806 
807    /* Now apply scissor, etc to the bounding box.  Could do this
808     * earlier, but it confuses the logic for tri-16 and would force
809     * the rasterizer to also respect scissor, etc, just for the rare
810     * cases where a small triangle extends beyond the scissor.
811     */
812    u_rect_find_intersection(&setup->draw_regions[viewport_index],
813                             &trimmed_box);
814 
815    /* Determine which tile(s) intersect the triangle's bounding box
816     */
817    if (dx < TILE_SIZE)
818    {
819       int ix0 = bbox->x0 / TILE_SIZE;
820       int iy0 = bbox->y0 / TILE_SIZE;
821       unsigned px = bbox->x0 & 63 & ~3;
822       unsigned py = bbox->y0 & 63 & ~3;
823 
824       assert(iy0 == bbox->y1 / TILE_SIZE &&
825 	     ix0 == bbox->x1 / TILE_SIZE);
826 
827       if (nr_planes == 3) {
828          if (sz < 4)
829          {
830             /* Triangle is contained in a single 4x4 stamp:
831              */
832             assert(px + 4 <= TILE_SIZE);
833             assert(py + 4 <= TILE_SIZE);
834             if (setup->multisample)
835                cmd = LP_RAST_OP_MS_TRIANGLE_3_4;
836             else
837                cmd = use_32bits ? LP_RAST_OP_TRIANGLE_32_3_4 : LP_RAST_OP_TRIANGLE_3_4;
838             return lp_scene_bin_cmd_with_state( scene, ix0, iy0,
839                                                 setup->fs.stored, cmd,
840                                                 lp_rast_arg_triangle_contained(tri, px, py) );
841          }
842 
843          if (sz < 16)
844          {
845             /* Triangle is contained in a single 16x16 block:
846              */
847 
848             /*
849              * The 16x16 block is only 4x4 aligned, and can exceed the tile
850              * dimensions if the triangle is 16 pixels in one dimension but 4
851              * in the other. So budge the 16x16 back inside the tile.
852              */
853             px = MIN2(px, TILE_SIZE - 16);
854             py = MIN2(py, TILE_SIZE - 16);
855 
856             assert(px + 16 <= TILE_SIZE);
857             assert(py + 16 <= TILE_SIZE);
858 
859             if (setup->multisample)
860                cmd = LP_RAST_OP_MS_TRIANGLE_3_16;
861             else
862                cmd = use_32bits ? LP_RAST_OP_TRIANGLE_32_3_16 : LP_RAST_OP_TRIANGLE_3_16;
863             return lp_scene_bin_cmd_with_state( scene, ix0, iy0,
864                                                 setup->fs.stored, cmd,
865                                                 lp_rast_arg_triangle_contained(tri, px, py) );
866          }
867       }
868       else if (nr_planes == 4 && sz < 16)
869       {
870          px = MIN2(px, TILE_SIZE - 16);
871          py = MIN2(py, TILE_SIZE - 16);
872 
873          assert(px + 16 <= TILE_SIZE);
874          assert(py + 16 <= TILE_SIZE);
875 
876          if (setup->multisample)
877             cmd = LP_RAST_OP_MS_TRIANGLE_4_16;
878          else
879             cmd = use_32bits ? LP_RAST_OP_TRIANGLE_32_4_16 : LP_RAST_OP_TRIANGLE_4_16;
880          return lp_scene_bin_cmd_with_state(scene, ix0, iy0,
881                                             setup->fs.stored, cmd,
882                                             lp_rast_arg_triangle_contained(tri, px, py));
883       }
884 
885 
886       /* Triangle is contained in a single tile:
887        */
888       if (setup->multisample)
889          cmd = lp_rast_ms_tri_tab[nr_planes];
890       else
891          cmd = use_32bits ? lp_rast_32_tri_tab[nr_planes] : lp_rast_tri_tab[nr_planes];
892       return lp_scene_bin_cmd_with_state(
893          scene, ix0, iy0, setup->fs.stored, cmd,
894          lp_rast_arg_triangle(tri, (1<<nr_planes)-1));
895    }
896    else
897    {
898       struct lp_rast_plane *plane = GET_PLANES(tri);
899       int64_t c[MAX_PLANES];
900       int64_t ei[MAX_PLANES];
901 
902       int64_t eo[MAX_PLANES];
903       int64_t xstep[MAX_PLANES];
904       int64_t ystep[MAX_PLANES];
905       int x, y;
906 
907       int ix0 = trimmed_box.x0 / TILE_SIZE;
908       int iy0 = trimmed_box.y0 / TILE_SIZE;
909       int ix1 = trimmed_box.x1 / TILE_SIZE;
910       int iy1 = trimmed_box.y1 / TILE_SIZE;
911 
912       for (i = 0; i < nr_planes; i++) {
913          c[i] = (plane[i].c +
914                  IMUL64(plane[i].dcdy, iy0) * TILE_SIZE -
915                  IMUL64(plane[i].dcdx, ix0) * TILE_SIZE);
916 
917          ei[i] = (plane[i].dcdy -
918                   plane[i].dcdx -
919                   (int64_t)plane[i].eo) << TILE_ORDER;
920 
921          eo[i] = (int64_t)plane[i].eo << TILE_ORDER;
922          xstep[i] = -(((int64_t)plane[i].dcdx) << TILE_ORDER);
923          ystep[i] = ((int64_t)plane[i].dcdy) << TILE_ORDER;
924       }
925 
926       tri->inputs.is_blit = lp_setup_is_blit(setup, &tri->inputs);
927 
928       /* Test tile-sized blocks against the triangle.
929        * Discard blocks fully outside the tri.  If the block is fully
930        * contained inside the tri, bin an lp_rast_shade_tile command.
931        * Else, bin a lp_rast_triangle command.
932        */
933       for (y = iy0; y <= iy1; y++)
934       {
935          boolean in = FALSE;  /* are we inside the triangle? */
936          int64_t cx[MAX_PLANES];
937 
938          for (i = 0; i < nr_planes; i++)
939             cx[i] = c[i];
940 
941          for (x = ix0; x <= ix1; x++)
942          {
943             int out = 0;
944             int partial = 0;
945 
946             for (i = 0; i < nr_planes; i++) {
947                int64_t planeout = cx[i] + eo[i];
948                int64_t planepartial = cx[i] + ei[i] - 1;
949                out |= (int) (planeout >> 63);
950                partial |= ((int) (planepartial >> 63)) & (1<<i);
951             }
952 
953             if (out) {
954                /* do nothing */
955                if (in)
956                   break;  /* exiting triangle, all done with this row */
957                LP_COUNT(nr_empty_64);
958             }
959             else if (partial) {
960                /* Not trivially accepted by at least one plane -
961                 * rasterize/shade partial tile
962                 */
963                int count = util_bitcount(partial);
964                in = TRUE;
965 
966                if (setup->multisample)
967                   cmd = lp_rast_ms_tri_tab[count];
968                else
969                   cmd = use_32bits ? lp_rast_32_tri_tab[count] : lp_rast_tri_tab[count];
970                if (!lp_scene_bin_cmd_with_state( scene, x, y,
971                                                  setup->fs.stored, cmd,
972                                                  lp_rast_arg_triangle(tri, partial) ))
973                   goto fail;
974 
975                LP_COUNT(nr_partially_covered_64);
976             }
977             else {
978                /* triangle covers the whole tile- shade whole tile */
979                LP_COUNT(nr_fully_covered_64);
980                in = TRUE;
981                if (!lp_setup_whole_tile(setup, &tri->inputs, x, y, opaque))
982                   goto fail;
983             }
984 
985             /* Iterate cx values across the region: */
986             for (i = 0; i < nr_planes; i++)
987                cx[i] += xstep[i];
988          }
989 
990          /* Iterate c values down the region: */
991          for (i = 0; i < nr_planes; i++)
992             c[i] += ystep[i];
993       }
994    }
995 
996    return TRUE;
997 
998 fail:
999    /* Need to disable any partially binned triangle.  This is easier
1000     * than trying to locate all the triangle, shade-tile, etc,
1001     * commands which may have been binned.
1002     */
1003    tri->inputs.disable = TRUE;
1004    return FALSE;
1005 }
1006 
1007 
1008 /**
1009  * Try to draw the triangle, restart the scene on failure.
1010  */
retry_triangle_ccw(struct lp_setup_context * setup,struct fixed_position * position,const float (* v0)[4],const float (* v1)[4],const float (* v2)[4],boolean front)1011 static inline void retry_triangle_ccw( struct lp_setup_context *setup,
1012                                 struct fixed_position* position,
1013                                 const float (*v0)[4],
1014                                 const float (*v1)[4],
1015                                 const float (*v2)[4],
1016                                 boolean front)
1017 {
1018    if (!do_triangle_ccw( setup, position, v0, v1, v2, front ))
1019    {
1020       if (!lp_setup_flush_and_restart(setup))
1021          return;
1022 
1023       if (!do_triangle_ccw( setup, position, v0, v1, v2, front ))
1024          return;
1025    }
1026 }
1027 
1028 /**
1029  * Calculate fixed position data for a triangle
1030  * It is unfortunate we need to do that here (as we need area
1031  * calculated in fixed point), as there's quite some code duplication
1032  * to what is done in the jit setup prog.
1033  */
1034 static inline int8_t
calc_fixed_position(struct lp_setup_context * setup,struct fixed_position * position,const float (* v0)[4],const float (* v1)[4],const float (* v2)[4])1035 calc_fixed_position(struct lp_setup_context *setup,
1036                     struct fixed_position* position,
1037                     const float (*v0)[4],
1038                     const float (*v1)[4],
1039                     const float (*v2)[4])
1040 {
1041    float pixel_offset = setup->multisample ? 0.0 : setup->pixel_offset;
1042    /*
1043     * The rounding may not be quite the same with PIPE_ARCH_SSE
1044     * (util_iround right now only does nearest/even on x87,
1045     * otherwise nearest/away-from-zero).
1046     * Both should be acceptable, I think.
1047     */
1048 #if defined(PIPE_ARCH_SSE)
1049    __m128 v0r, v1r;
1050    __m128 vxy0xy2, vxy1xy0;
1051    __m128i vxy0xy2i, vxy1xy0i;
1052    __m128i dxdy0120, x0x2y0y2, x1x0y1y0, x0120, y0120;
1053    __m128 pix_offset = _mm_set1_ps(pixel_offset);
1054    __m128 fixed_one = _mm_set1_ps((float)FIXED_ONE);
1055    v0r = _mm_castpd_ps(_mm_load_sd((double *)v0[0]));
1056    vxy0xy2 = _mm_loadh_pi(v0r, (__m64 *)v2[0]);
1057    v1r = _mm_castpd_ps(_mm_load_sd((double *)v1[0]));
1058    vxy1xy0 = _mm_movelh_ps(v1r, vxy0xy2);
1059    vxy0xy2 = _mm_sub_ps(vxy0xy2, pix_offset);
1060    vxy1xy0 = _mm_sub_ps(vxy1xy0, pix_offset);
1061    vxy0xy2 = _mm_mul_ps(vxy0xy2, fixed_one);
1062    vxy1xy0 = _mm_mul_ps(vxy1xy0, fixed_one);
1063    vxy0xy2i = _mm_cvtps_epi32(vxy0xy2);
1064    vxy1xy0i = _mm_cvtps_epi32(vxy1xy0);
1065    dxdy0120 = _mm_sub_epi32(vxy0xy2i, vxy1xy0i);
1066    _mm_store_si128((__m128i *)&position->dx01, dxdy0120);
1067    /*
1068     * For the mul, would need some more shuffles, plus emulation
1069     * for the signed mul (without sse41), so don't bother.
1070     */
1071    x0x2y0y2 = _mm_shuffle_epi32(vxy0xy2i, _MM_SHUFFLE(3,1,2,0));
1072    x1x0y1y0 = _mm_shuffle_epi32(vxy1xy0i, _MM_SHUFFLE(3,1,2,0));
1073    x0120 = _mm_unpacklo_epi32(x0x2y0y2, x1x0y1y0);
1074    y0120 = _mm_unpackhi_epi32(x0x2y0y2, x1x0y1y0);
1075    _mm_store_si128((__m128i *)&position->x[0], x0120);
1076    _mm_store_si128((__m128i *)&position->y[0], y0120);
1077 
1078 #else
1079    position->x[0] = subpixel_snap(v0[0][0] - pixel_offset);
1080    position->x[1] = subpixel_snap(v1[0][0] - pixel_offset);
1081    position->x[2] = subpixel_snap(v2[0][0] - pixel_offset);
1082    position->x[3] = 0; // should be unused
1083 
1084    position->y[0] = subpixel_snap(v0[0][1] - pixel_offset);
1085    position->y[1] = subpixel_snap(v1[0][1] - pixel_offset);
1086    position->y[2] = subpixel_snap(v2[0][1] - pixel_offset);
1087    position->y[3] = 0; // should be unused
1088 
1089    position->dx01 = position->x[0] - position->x[1];
1090    position->dy01 = position->y[0] - position->y[1];
1091 
1092    position->dx20 = position->x[2] - position->x[0];
1093    position->dy20 = position->y[2] - position->y[0];
1094 #endif
1095 
1096    uint64_t area = IMUL64(position->dx01, position->dy20) -
1097       IMUL64(position->dx20, position->dy01);
1098    return area == 0 ? 0 : (area & (1ULL << 63)) ? -1 : 1;
1099 }
1100 
1101 
1102 /**
1103  * Rotate a triangle, flipping its clockwise direction,
1104  * Swaps values for xy[0] and xy[1]
1105  */
1106 static inline void
rotate_fixed_position_01(struct fixed_position * position)1107 rotate_fixed_position_01( struct fixed_position* position )
1108 {
1109    int x, y;
1110 
1111    x = position->x[1];
1112    y = position->y[1];
1113    position->x[1] = position->x[0];
1114    position->y[1] = position->y[0];
1115    position->x[0] = x;
1116    position->y[0] = y;
1117 
1118    position->dx01 = -position->dx01;
1119    position->dy01 = -position->dy01;
1120    position->dx20 = position->x[2] - position->x[0];
1121    position->dy20 = position->y[2] - position->y[0];
1122 }
1123 
1124 
1125 /**
1126  * Rotate a triangle, flipping its clockwise direction,
1127  * Swaps values for xy[1] and xy[2]
1128  */
1129 static inline void
rotate_fixed_position_12(struct fixed_position * position)1130 rotate_fixed_position_12( struct fixed_position* position )
1131 {
1132    int x, y;
1133 
1134    x = position->x[2];
1135    y = position->y[2];
1136    position->x[2] = position->x[1];
1137    position->y[2] = position->y[1];
1138    position->x[1] = x;
1139    position->y[1] = y;
1140 
1141    x = position->dx01;
1142    y = position->dy01;
1143    position->dx01 = -position->dx20;
1144    position->dy01 = -position->dy20;
1145    position->dx20 = -x;
1146    position->dy20 = -y;
1147 }
1148 
1149 
1150 /**
1151  * Draw triangle if it's CW, cull otherwise.
1152  */
triangle_cw(struct lp_setup_context * setup,const float (* v0)[4],const float (* v1)[4],const float (* v2)[4])1153 static void triangle_cw(struct lp_setup_context *setup,
1154                         const float (*v0)[4],
1155                         const float (*v1)[4],
1156                         const float (*v2)[4])
1157 {
1158    PIPE_ALIGN_VAR(16) struct fixed_position position;
1159    struct llvmpipe_context *lp_context = (struct llvmpipe_context *)setup->pipe;
1160 
1161    if (lp_context->active_statistics_queries) {
1162       lp_context->pipeline_statistics.c_primitives++;
1163    }
1164 
1165    int8_t area_sign = calc_fixed_position(setup, &position, v0, v1, v2);
1166 
1167    if (area_sign < 0) {
1168       if (setup->flatshade_first) {
1169          rotate_fixed_position_12(&position);
1170          retry_triangle_ccw(setup, &position, v0, v2, v1, !setup->ccw_is_frontface);
1171       } else {
1172          rotate_fixed_position_01(&position);
1173          retry_triangle_ccw(setup, &position, v1, v0, v2, !setup->ccw_is_frontface);
1174       }
1175    }
1176 }
1177 
1178 
triangle_ccw(struct lp_setup_context * setup,const float (* v0)[4],const float (* v1)[4],const float (* v2)[4])1179 static void triangle_ccw(struct lp_setup_context *setup,
1180                          const float (*v0)[4],
1181                          const float (*v1)[4],
1182                          const float (*v2)[4])
1183 {
1184    PIPE_ALIGN_VAR(16) struct fixed_position position;
1185    struct llvmpipe_context *lp_context = (struct llvmpipe_context *)setup->pipe;
1186 
1187    if (lp_context->active_statistics_queries) {
1188       lp_context->pipeline_statistics.c_primitives++;
1189    }
1190 
1191    int8_t area_sign = calc_fixed_position(setup, &position, v0, v1, v2);
1192 
1193    if (area_sign > 0)
1194       retry_triangle_ccw(setup, &position, v0, v1, v2, setup->ccw_is_frontface);
1195 }
1196 
1197 /**
1198  * Draw triangle whether it's CW or CCW.
1199  */
triangle_both(struct lp_setup_context * setup,const float (* v0)[4],const float (* v1)[4],const float (* v2)[4])1200 static void triangle_both(struct lp_setup_context *setup,
1201                           const float (*v0)[4],
1202                           const float (*v1)[4],
1203                           const float (*v2)[4])
1204 {
1205    PIPE_ALIGN_VAR(16) struct fixed_position position;
1206    struct llvmpipe_context *lp_context = (struct llvmpipe_context *)setup->pipe;
1207 
1208    if (lp_context->active_statistics_queries) {
1209       lp_context->pipeline_statistics.c_primitives++;
1210    }
1211 
1212    int8_t area_sign = calc_fixed_position(setup, &position, v0, v1, v2);
1213 
1214    if (0) {
1215       assert(!util_is_inf_or_nan(v0[0][0]));
1216       assert(!util_is_inf_or_nan(v0[0][1]));
1217       assert(!util_is_inf_or_nan(v1[0][0]));
1218       assert(!util_is_inf_or_nan(v1[0][1]));
1219       assert(!util_is_inf_or_nan(v2[0][0]));
1220       assert(!util_is_inf_or_nan(v2[0][1]));
1221    }
1222 
1223    if (area_sign > 0)
1224       retry_triangle_ccw( setup, &position, v0, v1, v2, setup->ccw_is_frontface );
1225    else if (area_sign < 0) {
1226       if (setup->flatshade_first) {
1227          rotate_fixed_position_12( &position );
1228          retry_triangle_ccw( setup, &position, v0, v2, v1, !setup->ccw_is_frontface );
1229       } else {
1230          rotate_fixed_position_01( &position );
1231          retry_triangle_ccw( setup, &position, v1, v0, v2, !setup->ccw_is_frontface );
1232       }
1233    }
1234 }
1235 
1236 
triangle_noop(struct lp_setup_context * setup,const float (* v0)[4],const float (* v1)[4],const float (* v2)[4])1237 static void triangle_noop(struct lp_setup_context *setup,
1238                           const float (*v0)[4],
1239                           const float (*v1)[4],
1240                           const float (*v2)[4])
1241 {
1242 }
1243 
1244 
1245 void
lp_setup_choose_triangle(struct lp_setup_context * setup)1246 lp_setup_choose_triangle(struct lp_setup_context *setup)
1247 {
1248    if (setup->rasterizer_discard) {
1249       setup->triangle = triangle_noop;
1250       return;
1251    }
1252    switch (setup->cullmode) {
1253    case PIPE_FACE_NONE:
1254       setup->triangle = triangle_both;
1255       break;
1256    case PIPE_FACE_BACK:
1257       setup->triangle = setup->ccw_is_frontface ? triangle_ccw : triangle_cw;
1258       break;
1259    case PIPE_FACE_FRONT:
1260       setup->triangle = setup->ccw_is_frontface ? triangle_cw : triangle_ccw;
1261       break;
1262    default:
1263       setup->triangle = triangle_noop;
1264       break;
1265    }
1266 }
1267