• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    int64_t area;
73 };
74 
75 
76 /**
77  * Alloc space for a new triangle plus the input.a0/dadx/dady arrays
78  * immediately after it.
79  * The memory is allocated from the per-scene pool, not per-tile.
80  * \param tri_size  returns number of bytes allocated
81  * \param num_inputs  number of fragment shader inputs
82  * \return pointer to triangle space
83  */
84 struct lp_rast_triangle *
lp_setup_alloc_triangle(struct lp_scene * scene,unsigned nr_inputs,unsigned nr_planes,unsigned * tri_size)85 lp_setup_alloc_triangle(struct lp_scene *scene,
86                         unsigned nr_inputs,
87                         unsigned nr_planes,
88                         unsigned *tri_size)
89 {
90    unsigned input_array_sz = NUM_CHANNELS * (nr_inputs + 1) * sizeof(float);
91    unsigned plane_sz = nr_planes * sizeof(struct lp_rast_plane);
92    struct lp_rast_triangle *tri;
93 
94    STATIC_ASSERT(sizeof(struct lp_rast_plane) % 8 == 0);
95 
96    *tri_size = (sizeof(struct lp_rast_triangle) +
97                 3 * input_array_sz +
98                 plane_sz);
99 
100    tri = lp_scene_alloc_aligned( scene, *tri_size, 16 );
101    if (!tri)
102       return NULL;
103 
104    tri->inputs.stride = input_array_sz;
105 
106    {
107       ASSERTED char *a = (char *)tri;
108       ASSERTED char *b = (char *)&GET_PLANES(tri)[nr_planes];
109 
110       assert(b - a == *tri_size);
111    }
112 
113    return tri;
114 }
115 
116 void
lp_setup_print_vertex(struct lp_setup_context * setup,const char * name,const float (* v)[4])117 lp_setup_print_vertex(struct lp_setup_context *setup,
118                       const char *name,
119                       const float (*v)[4])
120 {
121    const struct lp_setup_variant_key *key = &setup->setup.variant->key;
122    int i, j;
123 
124    debug_printf("   wpos (%s[0]) xyzw %f %f %f %f\n",
125                 name,
126                 v[0][0], v[0][1], v[0][2], v[0][3]);
127 
128    for (i = 0; i < key->num_inputs; i++) {
129       const float *in = v[key->inputs[i].src_index];
130 
131       debug_printf("  in[%d] (%s[%d]) %s%s%s%s ",
132                    i,
133                    name, key->inputs[i].src_index,
134                    (key->inputs[i].usage_mask & 0x1) ? "x" : " ",
135                    (key->inputs[i].usage_mask & 0x2) ? "y" : " ",
136                    (key->inputs[i].usage_mask & 0x4) ? "z" : " ",
137                    (key->inputs[i].usage_mask & 0x8) ? "w" : " ");
138 
139       for (j = 0; j < 4; j++)
140          if (key->inputs[i].usage_mask & (1<<j))
141             debug_printf("%.5f ", in[j]);
142 
143       debug_printf("\n");
144    }
145 }
146 
147 
148 /**
149  * Print triangle vertex attribs (for debug).
150  */
151 void
lp_setup_print_triangle(struct lp_setup_context * setup,const float (* v0)[4],const float (* v1)[4],const float (* v2)[4])152 lp_setup_print_triangle(struct lp_setup_context *setup,
153                         const float (*v0)[4],
154                         const float (*v1)[4],
155                         const float (*v2)[4])
156 {
157    debug_printf("triangle\n");
158 
159    {
160       const float ex = v0[0][0] - v2[0][0];
161       const float ey = v0[0][1] - v2[0][1];
162       const float fx = v1[0][0] - v2[0][0];
163       const float fy = v1[0][1] - v2[0][1];
164 
165       /* det = cross(e,f).z */
166       const float det = ex * fy - ey * fx;
167       if (det < 0.0f)
168          debug_printf("   - ccw\n");
169       else if (det > 0.0f)
170          debug_printf("   - cw\n");
171       else
172          debug_printf("   - zero area\n");
173    }
174 
175    lp_setup_print_vertex(setup, "v0", v0);
176    lp_setup_print_vertex(setup, "v1", v1);
177    lp_setup_print_vertex(setup, "v2", v2);
178 }
179 
180 
181 #define MAX_PLANES 8
182 static unsigned
183 lp_rast_tri_tab[MAX_PLANES+1] = {
184    0,               /* should be impossible */
185    LP_RAST_OP_TRIANGLE_1,
186    LP_RAST_OP_TRIANGLE_2,
187    LP_RAST_OP_TRIANGLE_3,
188    LP_RAST_OP_TRIANGLE_4,
189    LP_RAST_OP_TRIANGLE_5,
190    LP_RAST_OP_TRIANGLE_6,
191    LP_RAST_OP_TRIANGLE_7,
192    LP_RAST_OP_TRIANGLE_8
193 };
194 
195 static unsigned
196 lp_rast_32_tri_tab[MAX_PLANES+1] = {
197    0,               /* should be impossible */
198    LP_RAST_OP_TRIANGLE_32_1,
199    LP_RAST_OP_TRIANGLE_32_2,
200    LP_RAST_OP_TRIANGLE_32_3,
201    LP_RAST_OP_TRIANGLE_32_4,
202    LP_RAST_OP_TRIANGLE_32_5,
203    LP_RAST_OP_TRIANGLE_32_6,
204    LP_RAST_OP_TRIANGLE_32_7,
205    LP_RAST_OP_TRIANGLE_32_8
206 };
207 
208 
209 static unsigned
210 lp_rast_ms_tri_tab[MAX_PLANES+1] = {
211    0,               /* should be impossible */
212    LP_RAST_OP_MS_TRIANGLE_1,
213    LP_RAST_OP_MS_TRIANGLE_2,
214    LP_RAST_OP_MS_TRIANGLE_3,
215    LP_RAST_OP_MS_TRIANGLE_4,
216    LP_RAST_OP_MS_TRIANGLE_5,
217    LP_RAST_OP_MS_TRIANGLE_6,
218    LP_RAST_OP_MS_TRIANGLE_7,
219    LP_RAST_OP_MS_TRIANGLE_8
220 };
221 
222 /*
223  * Detect big primitives drawn with an alpha == 1.0.
224  *
225  * This is used when simulating anti-aliasing primitives in shaders, e.g.,
226  * when drawing the windows client area in Aero's flip-3d effect.
227  */
228 static boolean
check_opaque(struct lp_setup_context * setup,const float (* v1)[4],const float (* v2)[4],const float (* v3)[4])229 check_opaque(struct lp_setup_context *setup,
230              const float (*v1)[4],
231              const float (*v2)[4],
232              const float (*v3)[4])
233 {
234    const struct lp_fragment_shader_variant *variant =
235       setup->fs.current.variant;
236    const struct lp_tgsi_channel_info *alpha_info = &variant->shader->info.cbuf[0][3];
237 
238    if (variant->opaque)
239       return TRUE;
240 
241    if (!variant->potentially_opaque)
242       return FALSE;
243 
244    if (alpha_info->file == TGSI_FILE_CONSTANT) {
245       const float *constants = setup->fs.current.jit_context.constants[0];
246       float alpha = constants[alpha_info->u.index*4 +
247                               alpha_info->swizzle];
248       return alpha == 1.0f;
249    }
250 
251    if (alpha_info->file == TGSI_FILE_INPUT) {
252       return (v1[1 + alpha_info->u.index][alpha_info->swizzle] == 1.0f &&
253               v2[1 + alpha_info->u.index][alpha_info->swizzle] == 1.0f &&
254               v3[1 + alpha_info->u.index][alpha_info->swizzle] == 1.0f);
255    }
256 
257    return FALSE;
258 }
259 
260 
261 
262 /**
263  * Do basic setup for triangle rasterization and determine which
264  * framebuffer tiles are touched.  Put the triangle in the scene's
265  * bins for the tiles which we overlap.
266  */
267 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)268 do_triangle_ccw(struct lp_setup_context *setup,
269                 struct fixed_position* position,
270                 const float (*v0)[4],
271                 const float (*v1)[4],
272                 const float (*v2)[4],
273                 boolean frontfacing )
274 {
275    struct lp_scene *scene = setup->scene;
276    const struct lp_setup_variant_key *key = &setup->setup.variant->key;
277    struct lp_rast_triangle *tri;
278    struct lp_rast_plane *plane;
279    const struct u_rect *scissor = NULL;
280    struct u_rect bbox, bboxpos;
281    boolean s_planes[4];
282    unsigned tri_bytes;
283    int nr_planes = 3;
284    unsigned viewport_index = 0;
285    unsigned layer = 0;
286    const float (*pv)[4];
287 
288    /* Area should always be positive here */
289    assert(position->area > 0);
290 
291    if (0)
292       lp_setup_print_triangle(setup, v0, v1, v2);
293 
294    if (setup->flatshade_first) {
295       pv = v0;
296    }
297    else {
298       pv = v2;
299    }
300    if (setup->viewport_index_slot > 0) {
301       unsigned *udata = (unsigned*)pv[setup->viewport_index_slot];
302       viewport_index = lp_clamp_viewport_idx(*udata);
303    }
304    if (setup->layer_slot > 0) {
305       layer = *(unsigned*)pv[setup->layer_slot];
306       layer = MIN2(layer, scene->fb_max_layer);
307    }
308 
309    /* Bounding rectangle (in pixels) */
310    {
311       /* Yes this is necessary to accurately calculate bounding boxes
312        * with the two fill-conventions we support.  GL (normally) ends
313        * up needing a bottom-left fill convention, which requires
314        * slightly different rounding.
315        */
316       int adj = (setup->bottom_edge_rule != 0) ? 1 : 0;
317 
318       /* Inclusive x0, exclusive x1 */
319       bbox.x0 =  MIN3(position->x[0], position->x[1], position->x[2]) >> FIXED_ORDER;
320       bbox.x1 = (MAX3(position->x[0], position->x[1], position->x[2]) - 1) >> FIXED_ORDER;
321 
322       /* Inclusive / exclusive depending upon adj (bottom-left or top-right) */
323       bbox.y0 = (MIN3(position->y[0], position->y[1], position->y[2]) + adj) >> FIXED_ORDER;
324       bbox.y1 = (MAX3(position->y[0], position->y[1], position->y[2]) - 1 + adj) >> FIXED_ORDER;
325    }
326 
327    if (!u_rect_test_intersection(&setup->draw_regions[viewport_index], &bbox)) {
328       if (0) debug_printf("no intersection\n");
329       LP_COUNT(nr_culled_tris);
330       return TRUE;
331    }
332 
333    bboxpos = bbox;
334 
335    /* Can safely discard negative regions, but need to keep hold of
336     * information about when the triangle extends past screen
337     * boundaries.  See trimmed_box in lp_setup_bin_triangle().
338     */
339    bboxpos.x0 = MAX2(bboxpos.x0, 0);
340    bboxpos.y0 = MAX2(bboxpos.y0, 0);
341 
342    nr_planes = 3;
343    /*
344     * Determine how many scissor planes we need, that is drop scissor
345     * edges if the bounding box of the tri is fully inside that edge.
346     */
347    scissor = &setup->draw_regions[viewport_index];
348    scissor_planes_needed(s_planes, &bboxpos, scissor);
349    nr_planes += s_planes[0] + s_planes[1] + s_planes[2] + s_planes[3];
350 
351    tri = lp_setup_alloc_triangle(scene,
352                                  key->num_inputs,
353                                  nr_planes,
354                                  &tri_bytes);
355    if (!tri)
356       return FALSE;
357 
358 #ifdef DEBUG
359    tri->v[0][0] = v0[0][0];
360    tri->v[1][0] = v1[0][0];
361    tri->v[2][0] = v2[0][0];
362    tri->v[0][1] = v0[0][1];
363    tri->v[1][1] = v1[0][1];
364    tri->v[2][1] = v2[0][1];
365 #endif
366 
367    LP_COUNT(nr_tris);
368 
369    /*
370     * Rotate the tri such that v0 is closest to the fb origin.
371     * This can give more accurate a0 value (which is at fb origin)
372     * when calculating the interpolants.
373     * It can't work when there's flat shading for instance in one
374     * of the attributes, hence restrict this to just a single attribute
375     * which is what causes some test failures.
376     * (This does not address the problem that interpolation may be
377     * inaccurate if gradients are relatively steep in small tris far
378     * away from the origin. It does however fix the (silly) wgf11rasterizer
379     * Interpolator test.)
380     * XXX This causes problems with mipgen -EmuTexture for not yet really
381     * understood reasons (if the vertices would be submitted in a different
382     * order, we'd also generate the same "wrong" results here without
383     * rotation). In any case, that we generate different values if a prim
384     * has the vertices rotated but is otherwise the same (which is due to
385     * numerical issues) is not a nice property. An additional problem by
386     * swapping the vertices here (which is possibly worse) is that
387     * the same primitive coming in twice might generate different values
388     * (in particular for z) due to the swapping potentially not happening
389     * both times, if the attributes to be interpolated are different. For now,
390     * just restrict this to not get used with dx9 (by checking pixel offset),
391     * could also restrict it further to only trigger with wgf11Interpolator
392     * Rasterizer test (the only place which needs it, with always the same
393     * vertices even).
394     */
395    if ((LP_DEBUG & DEBUG_ACCURATE_A0) &&
396        setup->pixel_offset == 0.5f &&
397        key->num_inputs == 1 &&
398        (key->inputs[0].interp == LP_INTERP_LINEAR ||
399         key->inputs[0].interp == LP_INTERP_PERSPECTIVE)) {
400       float dist0 = v0[0][0] * v0[0][0] + v0[0][1] * v0[0][1];
401       float dist1 = v1[0][0] * v1[0][0] + v1[0][1] * v1[0][1];
402       float dist2 = v2[0][0] * v2[0][0] + v2[0][1] * v2[0][1];
403       if (dist0 > dist1 && dist1 < dist2) {
404          const float (*vt)[4];
405          int x, y;
406          vt = v0;
407          v0 = v1;
408          v1 = v2;
409          v2 = vt;
410          x = position->x[0];
411          y = position->y[0];
412          position->x[0] = position->x[1];
413          position->y[0] = position->y[1];
414          position->x[1] = position->x[2];
415          position->y[1] = position->y[2];
416          position->x[2] = x;
417          position->y[2] = y;
418 
419          position->dx20 = position->dx01;
420          position->dy20 = position->dy01;
421          position->dx01 = position->x[0] - position->x[1];
422          position->dy01 = position->y[0] - position->y[1];
423       }
424       else if (dist0 > dist2) {
425          const float (*vt)[4];
426          int x, y;
427          vt = v0;
428          v0 = v2;
429          v2 = v1;
430          v1 = vt;
431          x = position->x[0];
432          y = position->y[0];
433          position->x[0] = position->x[2];
434          position->y[0] = position->y[2];
435          position->x[2] = position->x[1];
436          position->y[2] = position->y[1];
437          position->x[1] = x;
438          position->y[1] = y;
439 
440          position->dx01 = position->dx20;
441          position->dy01 = position->dy20;
442          position->dx20 = position->x[2] - position->x[0];
443          position->dy20 = position->y[2] - position->y[0];
444       }
445    }
446 
447    /* Setup parameter interpolants:
448     */
449    setup->setup.variant->jit_function(v0, v1, v2,
450                                       frontfacing,
451                                       GET_A0(&tri->inputs),
452                                       GET_DADX(&tri->inputs),
453                                       GET_DADY(&tri->inputs),
454                                       &setup->setup.variant->key);
455 
456    tri->inputs.frontfacing = frontfacing;
457    tri->inputs.disable = FALSE;
458    tri->inputs.is_blit = FALSE;
459    tri->inputs.opaque = check_opaque(setup, v0, v1, v2);
460    tri->inputs.layer = layer;
461    tri->inputs.viewport_index = viewport_index;
462    tri->inputs.view_index = setup->view_index;
463 
464    if (0)
465       lp_dump_setup_coef(&setup->setup.variant->key,
466                          (const float (*)[4])GET_A0(&tri->inputs),
467                          (const float (*)[4])GET_DADX(&tri->inputs),
468                          (const float (*)[4])GET_DADY(&tri->inputs));
469 
470    plane = GET_PLANES(tri);
471 
472 #if defined(PIPE_ARCH_SSE)
473    if (1) {
474       __m128i vertx, verty;
475       __m128i shufx, shufy;
476       __m128i dcdx, dcdy;
477       __m128i cdx02, cdx13, cdy02, cdy13, c02, c13;
478       __m128i c01, c23, unused;
479       __m128i dcdx_neg_mask;
480       __m128i dcdy_neg_mask;
481       __m128i dcdx_zero_mask;
482       __m128i top_left_flag, c_dec;
483       __m128i eo, p0, p1, p2;
484       __m128i zero = _mm_setzero_si128();
485 
486       vertx = _mm_load_si128((__m128i *)position->x); /* vertex x coords */
487       verty = _mm_load_si128((__m128i *)position->y); /* vertex y coords */
488 
489       shufx = _mm_shuffle_epi32(vertx, _MM_SHUFFLE(3,0,2,1));
490       shufy = _mm_shuffle_epi32(verty, _MM_SHUFFLE(3,0,2,1));
491 
492       dcdx = _mm_sub_epi32(verty, shufy);
493       dcdy = _mm_sub_epi32(vertx, shufx);
494 
495       dcdx_neg_mask = _mm_srai_epi32(dcdx, 31);
496       dcdx_zero_mask = _mm_cmpeq_epi32(dcdx, zero);
497       dcdy_neg_mask = _mm_srai_epi32(dcdy, 31);
498 
499       top_left_flag = _mm_set1_epi32((setup->bottom_edge_rule == 0) ? ~0 : 0);
500 
501       c_dec = _mm_or_si128(dcdx_neg_mask,
502                            _mm_and_si128(dcdx_zero_mask,
503                                          _mm_xor_si128(dcdy_neg_mask,
504                                                        top_left_flag)));
505 
506       /*
507        * 64 bit arithmetic.
508        * Note we need _signed_ mul (_mm_mul_epi32) which we emulate.
509        */
510       cdx02 = mm_mullohi_epi32(dcdx, vertx, &cdx13);
511       cdy02 = mm_mullohi_epi32(dcdy, verty, &cdy13);
512       c02 = _mm_sub_epi64(cdx02, cdy02);
513       c13 = _mm_sub_epi64(cdx13, cdy13);
514       c02 = _mm_sub_epi64(c02, _mm_shuffle_epi32(c_dec,
515                                                  _MM_SHUFFLE(2,2,0,0)));
516       c13 = _mm_sub_epi64(c13, _mm_shuffle_epi32(c_dec,
517                                                  _MM_SHUFFLE(3,3,1,1)));
518 
519       /*
520        * Useful for very small fbs/tris (or fewer subpixel bits) only:
521        * c = _mm_sub_epi32(mm_mullo_epi32(dcdx, vertx),
522        *                   mm_mullo_epi32(dcdy, verty));
523        *
524        * c = _mm_sub_epi32(c, c_dec);
525        */
526 
527       /* Scale up to match c:
528        */
529       dcdx = _mm_slli_epi32(dcdx, FIXED_ORDER);
530       dcdy = _mm_slli_epi32(dcdy, FIXED_ORDER);
531 
532       /*
533        * Calculate trivial reject values:
534        * Note eo cannot overflow even if dcdx/dcdy would already have
535        * 31 bits (which they shouldn't have). This is because eo
536        * is never negative (albeit if we rely on that need to be careful...)
537        */
538       eo = _mm_sub_epi32(_mm_andnot_si128(dcdy_neg_mask, dcdy),
539                          _mm_and_si128(dcdx_neg_mask, dcdx));
540 
541       /* ei = _mm_sub_epi32(_mm_sub_epi32(dcdy, dcdx), eo); */
542 
543       /*
544        * Pointless transpose which gets undone immediately in
545        * rasterization.
546        * It is actually difficult to do away with it - would essentially
547        * need GET_PLANES_DX, GET_PLANES_DY etc., but the calculations
548        * for this then would need to depend on the number of planes.
549        * The transpose is quite special here due to c being 64bit...
550        * The store has to be unaligned (unless we'd make the plane size
551        * a multiple of 128), and of course storing eo separately...
552        */
553       c01 = _mm_unpacklo_epi64(c02, c13);
554       c23 = _mm_unpackhi_epi64(c02, c13);
555       transpose2_64_2_32(&c01, &c23, &dcdx, &dcdy,
556                          &p0, &p1, &p2, &unused);
557       _mm_storeu_si128((__m128i *)&plane[0], p0);
558       plane[0].eo = (uint32_t)_mm_cvtsi128_si32(eo);
559       _mm_storeu_si128((__m128i *)&plane[1], p1);
560       eo = _mm_shuffle_epi32(eo, _MM_SHUFFLE(3,2,0,1));
561       plane[1].eo = (uint32_t)_mm_cvtsi128_si32(eo);
562       _mm_storeu_si128((__m128i *)&plane[2], p2);
563       eo = _mm_shuffle_epi32(eo, _MM_SHUFFLE(0,0,0,2));
564       plane[2].eo = (uint32_t)_mm_cvtsi128_si32(eo);
565    } else
566 #elif defined(_ARCH_PWR8) && UTIL_ARCH_LITTLE_ENDIAN
567    /*
568     * XXX this code is effectively disabled for all practical purposes,
569     * as the allowed fb size is tiny if FIXED_ORDER is 8.
570     */
571    if (setup->fb.width <= MAX_FIXED_LENGTH32 &&
572        setup->fb.height <= MAX_FIXED_LENGTH32 &&
573        (bbox.x1 - bbox.x0) <= MAX_FIXED_LENGTH32 &&
574        (bbox.y1 - bbox.y0) <= MAX_FIXED_LENGTH32) {
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, &bbox, &bboxpos, nr_planes, viewport_index);
742 }
743 
744 /*
745  * Round to nearest less or equal power of two of the input.
746  *
747  * Undefined if no bit set exists, so code should check against 0 first.
748  */
749 static inline uint32_t
floor_pot(uint32_t n)750 floor_pot(uint32_t n)
751 {
752 #if defined(PIPE_CC_GCC) && (defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64))
753    if (n == 0)
754       return 0;
755 
756    __asm__("bsr %1,%0"
757           : "=r" (n)
758           : "rm" (n)
759           : "cc");
760    return 1 << n;
761 #else
762    n |= (n >>  1);
763    n |= (n >>  2);
764    n |= (n >>  4);
765    n |= (n >>  8);
766    n |= (n >> 16);
767    return n - (n >> 1);
768 #endif
769 }
770 
771 
772 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)773 lp_setup_bin_triangle(struct lp_setup_context *setup,
774                       struct lp_rast_triangle *tri,
775                       const struct u_rect *bboxorig,
776                       const struct u_rect *bbox,
777                       int nr_planes,
778                       unsigned viewport_index)
779 {
780    struct lp_scene *scene = setup->scene;
781    struct u_rect trimmed_box = *bbox;
782    int i;
783    unsigned cmd;
784 
785    /* What is the largest power-of-two boundary this triangle crosses:
786     */
787    int dx = floor_pot((bbox->x0 ^ bbox->x1) |
788 		      (bbox->y0 ^ bbox->y1));
789 
790    /* The largest dimension of the rasterized area of the triangle
791     * (aligned to a 4x4 grid), rounded down to the nearest power of two:
792     */
793    int max_sz = ((bbox->x1 - (bbox->x0 & ~3)) |
794                  (bbox->y1 - (bbox->y0 & ~3)));
795    int sz = floor_pot(max_sz);
796 
797    /*
798     * NOTE: It is important to use the original bounding box
799     * which might contain negative values here, because if the
800     * plane math may overflow or not with the 32bit rasterization
801     * functions depends on the original extent of the triangle.
802     */
803    int max_szorig = ((bboxorig->x1 - (bboxorig->x0 & ~3)) |
804                      (bboxorig->y1 - (bboxorig->y0 & ~3)));
805    boolean use_32bits = max_szorig <= MAX_FIXED_LENGTH32;
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))
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 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 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])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    position->area = IMUL64(position->dx01, position->dy20) -
1097          IMUL64(position->dx20, position->dy01);
1098 }
1099 
1100 
1101 /**
1102  * Rotate a triangle, flipping its clockwise direction,
1103  * Swaps values for xy[0] and xy[1]
1104  */
1105 static inline void
rotate_fixed_position_01(struct fixed_position * position)1106 rotate_fixed_position_01( struct fixed_position* position )
1107 {
1108    int x, y;
1109 
1110    x = position->x[1];
1111    y = position->y[1];
1112    position->x[1] = position->x[0];
1113    position->y[1] = position->y[0];
1114    position->x[0] = x;
1115    position->y[0] = y;
1116 
1117    position->dx01 = -position->dx01;
1118    position->dy01 = -position->dy01;
1119    position->dx20 = position->x[2] - position->x[0];
1120    position->dy20 = position->y[2] - position->y[0];
1121 
1122    position->area = -position->area;
1123 }
1124 
1125 
1126 /**
1127  * Rotate a triangle, flipping its clockwise direction,
1128  * Swaps values for xy[1] and xy[2]
1129  */
1130 static inline void
rotate_fixed_position_12(struct fixed_position * position)1131 rotate_fixed_position_12( struct fixed_position* position )
1132 {
1133    int x, y;
1134 
1135    x = position->x[2];
1136    y = position->y[2];
1137    position->x[2] = position->x[1];
1138    position->y[2] = position->y[1];
1139    position->x[1] = x;
1140    position->y[1] = y;
1141 
1142    x = position->dx01;
1143    y = position->dy01;
1144    position->dx01 = -position->dx20;
1145    position->dy01 = -position->dy20;
1146    position->dx20 = -x;
1147    position->dy20 = -y;
1148 
1149    position->area = -position->area;
1150 }
1151 
1152 
1153 /**
1154  * Draw triangle if it's CW, cull otherwise.
1155  */
triangle_cw(struct lp_setup_context * setup,const float (* v0)[4],const float (* v1)[4],const float (* v2)[4])1156 static void triangle_cw(struct lp_setup_context *setup,
1157                         const float (*v0)[4],
1158                         const float (*v1)[4],
1159                         const float (*v2)[4])
1160 {
1161    PIPE_ALIGN_VAR(16) struct fixed_position position;
1162    struct llvmpipe_context *lp_context = (struct llvmpipe_context *)setup->pipe;
1163 
1164    if (lp_context->active_statistics_queries) {
1165       lp_context->pipeline_statistics.c_primitives++;
1166    }
1167 
1168    calc_fixed_position(setup, &position, v0, v1, v2);
1169 
1170    if (position.area < 0) {
1171       if (setup->flatshade_first) {
1172          rotate_fixed_position_12(&position);
1173          retry_triangle_ccw(setup, &position, v0, v2, v1, !setup->ccw_is_frontface);
1174       } else {
1175          rotate_fixed_position_01(&position);
1176          retry_triangle_ccw(setup, &position, v1, v0, v2, !setup->ccw_is_frontface);
1177       }
1178    }
1179 }
1180 
1181 
triangle_ccw(struct lp_setup_context * setup,const float (* v0)[4],const float (* v1)[4],const float (* v2)[4])1182 static void triangle_ccw(struct lp_setup_context *setup,
1183                          const float (*v0)[4],
1184                          const float (*v1)[4],
1185                          const float (*v2)[4])
1186 {
1187    PIPE_ALIGN_VAR(16) struct fixed_position position;
1188    struct llvmpipe_context *lp_context = (struct llvmpipe_context *)setup->pipe;
1189 
1190    if (lp_context->active_statistics_queries) {
1191       lp_context->pipeline_statistics.c_primitives++;
1192    }
1193 
1194    calc_fixed_position(setup, &position, v0, v1, v2);
1195 
1196    if (position.area > 0)
1197       retry_triangle_ccw(setup, &position, v0, v1, v2, setup->ccw_is_frontface);
1198 }
1199 
1200 /**
1201  * Draw triangle whether it's CW or CCW.
1202  */
triangle_both(struct lp_setup_context * setup,const float (* v0)[4],const float (* v1)[4],const float (* v2)[4])1203 static void triangle_both(struct lp_setup_context *setup,
1204                           const float (*v0)[4],
1205                           const float (*v1)[4],
1206                           const float (*v2)[4])
1207 {
1208    PIPE_ALIGN_VAR(16) struct fixed_position position;
1209    struct llvmpipe_context *lp_context = (struct llvmpipe_context *)setup->pipe;
1210 
1211    if (lp_context->active_statistics_queries) {
1212       lp_context->pipeline_statistics.c_primitives++;
1213    }
1214 
1215    calc_fixed_position(setup, &position, v0, v1, v2);
1216 
1217    if (0) {
1218       assert(!util_is_inf_or_nan(v0[0][0]));
1219       assert(!util_is_inf_or_nan(v0[0][1]));
1220       assert(!util_is_inf_or_nan(v1[0][0]));
1221       assert(!util_is_inf_or_nan(v1[0][1]));
1222       assert(!util_is_inf_or_nan(v2[0][0]));
1223       assert(!util_is_inf_or_nan(v2[0][1]));
1224    }
1225 
1226    if (position.area > 0)
1227       retry_triangle_ccw( setup, &position, v0, v1, v2, setup->ccw_is_frontface );
1228    else if (position.area < 0) {
1229       if (setup->flatshade_first) {
1230          rotate_fixed_position_12( &position );
1231          retry_triangle_ccw( setup, &position, v0, v2, v1, !setup->ccw_is_frontface );
1232       } else {
1233          rotate_fixed_position_01( &position );
1234          retry_triangle_ccw( setup, &position, v1, v0, v2, !setup->ccw_is_frontface );
1235       }
1236    }
1237 }
1238 
1239 
triangle_noop(struct lp_setup_context * setup,const float (* v0)[4],const float (* v1)[4],const float (* v2)[4])1240 static void triangle_noop(struct lp_setup_context *setup,
1241                           const float (*v0)[4],
1242                           const float (*v1)[4],
1243                           const float (*v2)[4])
1244 {
1245 }
1246 
1247 
1248 void
lp_setup_choose_triangle(struct lp_setup_context * setup)1249 lp_setup_choose_triangle(struct lp_setup_context *setup)
1250 {
1251    if (setup->rasterizer_discard) {
1252       setup->triangle = triangle_noop;
1253       return;
1254    }
1255    switch (setup->cullmode) {
1256    case PIPE_FACE_NONE:
1257       setup->triangle = triangle_both;
1258       break;
1259    case PIPE_FACE_BACK:
1260       setup->triangle = setup->ccw_is_frontface ? triangle_ccw : triangle_cw;
1261       break;
1262    case PIPE_FACE_FRONT:
1263       setup->triangle = setup->ccw_is_frontface ? triangle_cw : triangle_ccw;
1264       break;
1265    default:
1266       setup->triangle = triangle_noop;
1267       break;
1268    }
1269 }
1270