llvmpipe: repartition lp_rasterizer state for threading
[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 "util/u_math.h"
33 #include "lp_debug.h"
34 #include "lp_rast_priv.h"
35 #include "lp_tile_soa.h"
36
37
38 /**
39 * Add a 4x4 block of pixels to the block list.
40 * All pixels are known to be inside the triangle's bounds.
41 */
42 static void
43 block_full_4( struct lp_rasterizer_task *rast_task, int x, int y )
44 {
45 const unsigned i = rast_task->nr_blocks;
46 assert(x % 4 == 0);
47 assert(y % 4 == 0);
48 rast_task->blocks[i].x = x;
49 rast_task->blocks[i].y = y;
50 rast_task->blocks[i].mask = ~0;
51 rast_task->nr_blocks++;
52 }
53
54
55 /**
56 * Add a 16x16 block of pixels to the block list.
57 * All pixels are known to be inside the triangle's bounds.
58 */
59 static void
60 block_full_16( struct lp_rasterizer_task *rast_task, int x, int y )
61 {
62 unsigned ix, iy;
63 assert(x % 16 == 0);
64 assert(y % 16 == 0);
65 for (iy = 0; iy < 16; iy += 4)
66 for (ix = 0; ix < 16; ix += 4)
67 block_full_4(rast_task, x + ix, y + iy);
68 }
69
70
71 /**
72 * Evaluate each pixel in a 4x4 block to determine if it lies within
73 * the triangle's bounds.
74 * Generate a mask of in/out flags and add the block to the blocks list.
75 */
76 static void
77 do_block_4( struct lp_rasterizer_task *rast_task,
78 const struct lp_rast_triangle *tri,
79 int x, int y,
80 int c1,
81 int c2,
82 int c3 )
83 {
84 int i;
85 unsigned mask = 0;
86
87 assert(x % 4 == 0);
88 assert(y % 4 == 0);
89
90 for (i = 0; i < 16; i++) {
91 int any_negative = ((c1 + tri->step[0][i]) |
92 (c2 + tri->step[1][i]) |
93 (c3 + tri->step[2][i])) >> 31;
94 mask |= (~any_negative) & (1 << i);
95 }
96
97 /* As we do trivial reject already, masks should rarely be all zero:
98 */
99 if (mask) {
100 const unsigned i = rast_task->nr_blocks;
101 rast_task->blocks[i].x = x;
102 rast_task->blocks[i].y = y;
103 rast_task->blocks[i].mask = mask;
104 rast_task->nr_blocks++;
105 }
106 }
107
108
109 /**
110 * Evaluate a 16x16 block of pixels to determine which 4x4 subblocks are in/out
111 * of the triangle's bounds.
112 */
113 static void
114 do_block_16( struct lp_rasterizer_task *rast_task,
115 const struct lp_rast_triangle *tri,
116 int x, int y,
117 int c1,
118 int c2,
119 int c3 )
120 {
121 int ix, iy, i = 0;
122
123 int ei1 = tri->ei1 * 4;
124 int ei2 = tri->ei2 * 4;
125 int ei3 = tri->ei3 * 4;
126
127 int eo1 = tri->eo1 * 4;
128 int eo2 = tri->eo2 * 4;
129 int eo3 = tri->eo3 * 4;
130
131 assert(x % 16 == 0);
132 assert(y % 16 == 0);
133
134 for (iy = 0; iy < 16; iy+=4) {
135 for (ix = 0; ix < 16; ix+=4, i++) {
136 int cx1 = c1 + (tri->step[0][i] * 4);
137 int cx2 = c2 + (tri->step[1][i] * 4);
138 int cx3 = c3 + (tri->step[2][i] * 4);
139
140 if (cx1 + eo1 < 0 ||
141 cx2 + eo2 < 0 ||
142 cx3 + eo3 < 0) {
143 /* the block is completely outside the triangle - nop */
144 }
145 else if (cx1 + ei1 > 0 &&
146 cx2 + ei2 > 0 &&
147 cx3 + ei3 > 0) {
148 /* the block is completely inside the triangle */
149 block_full_4(rast_task, x+ix, y+iy);
150 }
151 else {
152 /* the block is partially in/out of the triangle */
153 do_block_4(rast_task, tri, x+ix, y+iy, cx1, cx2, cx3);
154 }
155 }
156 }
157 }
158
159
160 /**
161 * Scan the tile in chunks and figure out which pixels to rasterize
162 * for this triangle.
163 */
164 void
165 lp_rast_triangle( struct lp_rasterizer *rast,
166 unsigned thread_index,
167 const union lp_rast_cmd_arg arg )
168 {
169 struct lp_rasterizer_task *rast_task = &rast->tasks[thread_index];
170 const struct lp_rast_triangle *tri = arg.triangle;
171
172 int x = rast_task->x;
173 int y = rast_task->y;
174 int ix, iy;
175 unsigned i = 0;
176
177 int c1 = tri->c1 + tri->dx12 * y - tri->dy12 * x;
178 int c2 = tri->c2 + tri->dx23 * y - tri->dy23 * x;
179 int c3 = tri->c3 + tri->dx31 * y - tri->dy31 * x;
180
181 int ei1 = tri->ei1 * 16;
182 int ei2 = tri->ei2 * 16;
183 int ei3 = tri->ei3 * 16;
184
185 int eo1 = tri->eo1 * 16;
186 int eo2 = tri->eo2 * 16;
187 int eo3 = tri->eo3 * 16;
188
189 assert(Elements(rast_task->blocks) == (TILE_SIZE * TILE_SIZE) / (4*4));
190
191 LP_DBG(DEBUG_RAST, "lp_rast_triangle\n");
192
193 rast_task->nr_blocks = 0;
194
195 /* Walk over the tile to build a list of 4x4 pixel blocks which will
196 * be filled/shaded. We do this at two granularities: 16x16 blocks
197 * and then 4x4 blocks.
198 */
199 for (iy = 0; iy < TILE_SIZE; iy += 16) {
200 for (ix = 0; ix < TILE_SIZE; ix += 16, i++) {
201 int cx1 = c1 + (tri->step[0][i] * 16);
202 int cx2 = c2 + (tri->step[1][i] * 16);
203 int cx3 = c3 + (tri->step[2][i] * 16);
204
205 if (cx1 + eo1 < 0 ||
206 cx2 + eo2 < 0 ||
207 cx3 + eo3 < 0) {
208 /* the block is completely outside the triangle - nop */
209 }
210 else if (cx1 + ei1 > 0 &&
211 cx2 + ei2 > 0 &&
212 cx3 + ei3 > 0) {
213 /* the block is completely inside the triangle */
214 block_full_16(rast_task, x+ix, y+iy);
215 }
216 else {
217 /* the block is partially in/out of the triangle */
218 do_block_16(rast_task, tri, x+ix, y+iy, cx1, cx2, cx3);
219 }
220 }
221 }
222
223 assert(rast_task->nr_blocks <= Elements(rast_task->blocks));
224
225 /* Shade the 4x4 pixel blocks */
226 for (i = 0; i < rast_task->nr_blocks; i++)
227 lp_rast_shade_quads(rast,
228 thread_index,
229 &tri->inputs,
230 rast_task->blocks[i].x,
231 rast_task->blocks[i].y,
232 rast_task->blocks[i].mask);
233 }