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