llvmpipe: generate two shader varients, one omits triangle in/out testing
[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_rast_priv.h"
36 #include "lp_tile_soa.h"
37
38
39 /**
40 * Map an index in [0,15] to an x,y position, multiplied by 4.
41 * This is used to get the position of each subtile in a 4x4
42 * grid of edge step values.
43 */
44 static const int pos_table4[16][2] = {
45 { 0, 0 },
46 { 4, 0 },
47 { 0, 4 },
48 { 4, 4 },
49 { 8, 0 },
50 { 12, 0 },
51 { 8, 4 },
52 { 12, 4 },
53 { 0, 8 },
54 { 4, 8 },
55 { 0, 12 },
56 { 4, 12 },
57 { 8, 8 },
58 { 12, 8 },
59 { 8, 12 },
60 { 12, 12 }
61 };
62
63
64 static const int pos_table16[16][2] = {
65 { 0, 0 },
66 { 16, 0 },
67 { 0, 16 },
68 { 16, 16 },
69 { 32, 0 },
70 { 48, 0 },
71 { 32, 16 },
72 { 48, 16 },
73 { 0, 32 },
74 { 16, 32 },
75 { 0, 48 },
76 { 16, 48 },
77 { 32, 32 },
78 { 48, 32 },
79 { 32, 48 },
80 { 48, 48 }
81 };
82
83
84 /**
85 * Shade all pixels in a 4x4 block.
86 */
87 static void
88 block_full_4( struct lp_rasterizer_task *rast_task,
89 const struct lp_rast_triangle *tri,
90 int x, int y )
91 {
92 lp_rast_shade_quads_all(rast_task->rast,
93 rast_task->thread_index,
94 &tri->inputs,
95 x, y);
96 }
97
98
99 /**
100 * Shade all pixels in a 16x16 block.
101 */
102 static void
103 block_full_16( struct lp_rasterizer_task *rast_task,
104 const struct lp_rast_triangle *tri,
105 int x, int y )
106 {
107 unsigned ix, iy;
108 assert(x % 16 == 0);
109 assert(y % 16 == 0);
110 for (iy = 0; iy < 16; iy += 4)
111 for (ix = 0; ix < 16; ix += 4)
112 block_full_4(rast_task, tri, x + ix, y + iy);
113 }
114
115
116 /**
117 * Pass the 4x4 pixel block to the shader function.
118 * Determination of which of the 16 pixels lies inside the triangle
119 * will be done as part of the fragment shader.
120 */
121 static void
122 do_block_4( struct lp_rasterizer_task *rast_task,
123 const struct lp_rast_triangle *tri,
124 int x, int y,
125 int c1,
126 int c2,
127 int c3 )
128 {
129 lp_rast_shade_quads(rast_task->rast,
130 rast_task->thread_index,
131 &tri->inputs,
132 x, y,
133 -c1, -c2, -c3);
134 }
135
136
137 /**
138 * Evaluate a 16x16 block of pixels to determine which 4x4 subblocks are in/out
139 * of the triangle's bounds.
140 */
141 static void
142 do_block_16( struct lp_rasterizer_task *rast_task,
143 const struct lp_rast_triangle *tri,
144 int x, int y,
145 int c1,
146 int c2,
147 int c3 )
148 {
149 const int ei1 = tri->ei1 * 4;
150 const int ei2 = tri->ei2 * 4;
151 const int ei3 = tri->ei3 * 4;
152
153 const int eo1 = tri->eo1 * 4;
154 const int eo2 = tri->eo2 * 4;
155 const int eo3 = tri->eo3 * 4;
156
157 int i;
158
159 assert(x % 16 == 0);
160 assert(y % 16 == 0);
161
162 for (i = 0; i < 16; i++) {
163 int cx1 = c1 + (tri->inputs.step[0][i] * 4);
164 int cx2 = c2 + (tri->inputs.step[1][i] * 4);
165 int cx3 = c3 + (tri->inputs.step[2][i] * 4);
166
167 if (cx1 + eo1 < 0 ||
168 cx2 + eo2 < 0 ||
169 cx3 + eo3 < 0) {
170 /* the block is completely outside the triangle - nop */
171 }
172 else {
173 int px = x + pos_table4[i][0];
174 int py = y + pos_table4[i][1];
175 if (cx1 + ei1 > 0 &&
176 cx2 + ei2 > 0 &&
177 cx3 + ei3 > 0) {
178 /* the block is completely inside the triangle */
179 block_full_4(rast_task, tri, px, py);
180 }
181 else {
182 /* the block is partially in/out of the triangle */
183 do_block_4(rast_task, tri, px, py, cx1, cx2, cx3);
184 }
185 }
186 }
187 }
188
189
190 /**
191 * Scan the tile in chunks and figure out which pixels to rasterize
192 * for this triangle.
193 */
194 void
195 lp_rast_triangle( struct lp_rasterizer *rast,
196 unsigned thread_index,
197 const union lp_rast_cmd_arg arg )
198 {
199 struct lp_rasterizer_task *rast_task = &rast->tasks[thread_index];
200 const struct lp_rast_triangle *tri = arg.triangle;
201
202 int x = rast_task->x;
203 int y = rast_task->y;
204 unsigned i;
205
206 int c1 = tri->c1 + tri->dx12 * y - tri->dy12 * x;
207 int c2 = tri->c2 + tri->dx23 * y - tri->dy23 * x;
208 int c3 = tri->c3 + tri->dx31 * y - tri->dy31 * x;
209
210 int ei1 = tri->ei1 * 16;
211 int ei2 = tri->ei2 * 16;
212 int ei3 = tri->ei3 * 16;
213
214 int eo1 = tri->eo1 * 16;
215 int eo2 = tri->eo2 * 16;
216 int eo3 = tri->eo3 * 16;
217
218 LP_DBG(DEBUG_RAST, "lp_rast_triangle\n");
219
220 /* Walk over the tile to build a list of 4x4 pixel blocks which will
221 * be filled/shaded. We do this at two granularities: 16x16 blocks
222 * and then 4x4 blocks.
223 */
224 for (i = 0; i < 16; i++) {
225 int cx1 = c1 + (tri->inputs.step[0][i] * 16);
226 int cx2 = c2 + (tri->inputs.step[1][i] * 16);
227 int cx3 = c3 + (tri->inputs.step[2][i] * 16);
228
229 if (cx1 + eo1 < 0 ||
230 cx2 + eo2 < 0 ||
231 cx3 + eo3 < 0) {
232 /* the block is completely outside the triangle - nop */
233 }
234 else {
235 int px = x + pos_table16[i][0];
236 int py = y + pos_table16[i][1];
237
238 if (cx1 + ei1 > 0 &&
239 cx2 + ei2 > 0 &&
240 cx3 + ei3 > 0) {
241 /* the block is completely inside the triangle */
242 block_full_16(rast_task, tri, px, py);
243 }
244 else {
245 /* the block is partially in/out of the triangle */
246 do_block_16(rast_task, tri, px, py, cx1, cx2, cx3);
247 }
248 }
249 }
250 }