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