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