Merge branch 'gallium-nopointsizeminmax'
[mesa.git] / src / gallium / drivers / llvmpipe / lp_rast_tri.c
1 /**************************************************************************
2 *
3 * Copyright 2007-2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /*
29 * Rasterization for binned triangles within a tile
30 */
31
32 #include <limits.h>
33 #include "util/u_math.h"
34 #include "lp_debug.h"
35 #include "lp_perf.h"
36 #include "lp_rast_priv.h"
37 #include "lp_tile_soa.h"
38
39
40 /**
41 * Map an index in [0,15] to an x,y position, multiplied by 4.
42 * This is used to get the position of each subtile in a 4x4
43 * grid of edge step values.
44 * Note: we can use some bit twiddling to compute these values instead
45 * of using a look-up table, but there's no measurable performance
46 * difference.
47 */
48 static const int pos_table4[16][2] = {
49 { 0, 0 },
50 { 4, 0 },
51 { 0, 4 },
52 { 4, 4 },
53 { 8, 0 },
54 { 12, 0 },
55 { 8, 4 },
56 { 12, 4 },
57 { 0, 8 },
58 { 4, 8 },
59 { 0, 12 },
60 { 4, 12 },
61 { 8, 8 },
62 { 12, 8 },
63 { 8, 12 },
64 { 12, 12 }
65 };
66
67
68 static const int pos_table16[16][2] = {
69 { 0, 0 },
70 { 16, 0 },
71 { 0, 16 },
72 { 16, 16 },
73 { 32, 0 },
74 { 48, 0 },
75 { 32, 16 },
76 { 48, 16 },
77 { 0, 32 },
78 { 16, 32 },
79 { 0, 48 },
80 { 16, 48 },
81 { 32, 32 },
82 { 48, 32 },
83 { 32, 48 },
84 { 48, 48 }
85 };
86
87
88 /**
89 * Shade all pixels in a 4x4 block.
90 */
91 static void
92 block_full_4( struct lp_rasterizer_task *rast_task,
93 const struct lp_rast_triangle *tri,
94 int x, int y )
95 {
96 lp_rast_shade_quads_all(rast_task->rast,
97 rast_task->thread_index,
98 &tri->inputs,
99 x, y);
100 }
101
102
103 /**
104 * Shade all pixels in a 16x16 block.
105 */
106 static void
107 block_full_16( struct lp_rasterizer_task *rast_task,
108 const struct lp_rast_triangle *tri,
109 int x, int y )
110 {
111 unsigned ix, iy;
112 assert(x % 16 == 0);
113 assert(y % 16 == 0);
114 for (iy = 0; iy < 16; iy += 4)
115 for (ix = 0; ix < 16; ix += 4)
116 block_full_4(rast_task, tri, x + ix, y + iy);
117 }
118
119
120 /**
121 * Pass the 4x4 pixel block to the shader function.
122 * Determination of which of the 16 pixels lies inside the triangle
123 * will be done as part of the fragment shader.
124 */
125 static void
126 do_block_4( struct lp_rasterizer_task *rast_task,
127 const struct lp_rast_triangle *tri,
128 int x, int y,
129 int c1,
130 int c2,
131 int c3 )
132 {
133 lp_rast_shade_quads(rast_task->rast,
134 rast_task->thread_index,
135 &tri->inputs,
136 x, y,
137 -c1, -c2, -c3);
138 }
139
140
141 /**
142 * Evaluate a 16x16 block of pixels to determine which 4x4 subblocks are in/out
143 * of the triangle's bounds.
144 */
145 static void
146 do_block_16( struct lp_rasterizer_task *rast_task,
147 const struct lp_rast_triangle *tri,
148 int x, int y,
149 int c1,
150 int c2,
151 int c3 )
152 {
153 const int eo1 = tri->eo1 * 4;
154 const int eo2 = tri->eo2 * 4;
155 const int eo3 = tri->eo3 * 4;
156 const int *step0 = tri->inputs.step[0];
157 const int *step1 = tri->inputs.step[1];
158 const int *step2 = tri->inputs.step[2];
159 int i;
160
161 assert(x % 16 == 0);
162 assert(y % 16 == 0);
163
164 for (i = 0; i < 16; i++) {
165 int cx1 = c1 + step0[i] * 4;
166 int cx2 = c2 + step1[i] * 4;
167 int cx3 = c3 + step2[i] * 4;
168
169 if (cx1 + eo1 < 0 ||
170 cx2 + eo2 < 0 ||
171 cx3 + eo3 < 0) {
172 /* the block is completely outside the triangle - nop */
173 LP_COUNT(nr_empty_4);
174 }
175 else {
176 int px = x + pos_table4[i][0];
177 int py = y + pos_table4[i][1];
178 /* Don't bother testing if the 4x4 block is entirely in/out of
179 * the triangle. It's a little faster to do it in the jit code.
180 */
181 LP_COUNT(nr_non_empty_4);
182 do_block_4(rast_task, tri, px, py, cx1, cx2, cx3);
183 }
184 }
185 }
186
187
188 /**
189 * Scan the tile in chunks and figure out which pixels to rasterize
190 * for this triangle.
191 */
192 void
193 lp_rast_triangle( struct lp_rasterizer *rast,
194 unsigned thread_index,
195 const union lp_rast_cmd_arg arg )
196 {
197 struct lp_rasterizer_task *rast_task = &rast->tasks[thread_index];
198 const struct lp_rast_triangle *tri = arg.triangle;
199
200 int x = rast_task->x;
201 int y = rast_task->y;
202 unsigned i;
203
204 int c1 = tri->c1 + tri->dx12 * y - tri->dy12 * x;
205 int c2 = tri->c2 + tri->dx23 * y - tri->dy23 * x;
206 int c3 = tri->c3 + tri->dx31 * y - tri->dy31 * x;
207
208 int ei1 = tri->ei1 * 16;
209 int ei2 = tri->ei2 * 16;
210 int ei3 = tri->ei3 * 16;
211
212 int eo1 = tri->eo1 * 16;
213 int eo2 = tri->eo2 * 16;
214 int eo3 = tri->eo3 * 16;
215
216 LP_DBG(DEBUG_RAST, "lp_rast_triangle\n");
217
218 /* Walk over the tile to build a list of 4x4 pixel blocks which will
219 * be filled/shaded. We do this at two granularities: 16x16 blocks
220 * and then 4x4 blocks.
221 */
222 for (i = 0; i < 16; i++) {
223 int cx1 = c1 + (tri->inputs.step[0][i] * 16);
224 int cx2 = c2 + (tri->inputs.step[1][i] * 16);
225 int cx3 = c3 + (tri->inputs.step[2][i] * 16);
226
227 if (cx1 + eo1 < 0 ||
228 cx2 + eo2 < 0 ||
229 cx3 + eo3 < 0) {
230 /* the block is completely outside the triangle - nop */
231 LP_COUNT(nr_empty_16);
232 }
233 else {
234 int px = x + pos_table16[i][0];
235 int py = y + pos_table16[i][1];
236
237 if (cx1 + ei1 > 0 &&
238 cx2 + ei2 > 0 &&
239 cx3 + ei3 > 0) {
240 /* the block is completely inside the triangle */
241 LP_COUNT(nr_fully_covered_16);
242 block_full_16(rast_task, tri, px, py);
243 }
244 else {
245 /* the block is partially in/out of the triangle */
246 LP_COUNT(nr_partially_covered_16);
247 do_block_16(rast_task, tri, px, py, cx1, cx2, cx3);
248 }
249 }
250 }
251 }