llvmpipe: make nr_blocks unsigned
[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_rast_priv.h"
34 #include "lp_tile_soa.h"
35
36
37 #define BLOCKSIZE 4
38
39
40 /**
41 * Add a 4x4 block of pixels to the block list.
42 * All pixels are known to be inside the triangle's bounds.
43 */
44 static void
45 block_full_4( struct lp_rasterizer *rast, int x, int y )
46 {
47 const unsigned i = rast->nr_blocks;
48 assert(x % 4 == 0);
49 assert(y % 4 == 0);
50 rast->blocks[i].x = x;
51 rast->blocks[i].y = y;
52 rast->blocks[i].mask = ~0;
53 rast->nr_blocks++;
54 }
55
56
57 /**
58 * Add a 16x16 block of pixels to the block list.
59 * All pixels are known to be inside the triangle's bounds.
60 */
61 static void
62 block_full_16( struct lp_rasterizer *rast, int x, int y )
63 {
64 unsigned ix, iy;
65 assert(x % 16 == 0);
66 assert(y % 16 == 0);
67 for (iy = 0; iy < 16; iy += 4)
68 for (ix = 0; ix < 16; ix += 4)
69 block_full_4(rast, x + ix, y + iy);
70 }
71
72
73 /**
74 * Evaluate each pixel in a 4x4 block to determine if it lies within
75 * the triangle's bounds.
76 * Generate a mask of in/out flags and add the block to the blocks list.
77 */
78 static void
79 do_block_4( struct lp_rasterizer *rast,
80 const struct lp_rast_triangle *tri,
81 int x, int y,
82 int c1,
83 int c2,
84 int c3 )
85 {
86 int i;
87 unsigned mask = 0;
88
89 assert(x % 4 == 0);
90 assert(y % 4 == 0);
91
92 for (i = 0; i < 16; i++)
93 mask |= (~(((c1 + tri->step[0][i]) |
94 (c2 + tri->step[1][i]) |
95 (c3 + tri->step[2][i])) >> 31)) & (1 << i);
96
97 /* As we do trivial reject already, masks should rarely be all zero:
98 */
99 if (mask) {
100 const unsigned i = rast->nr_blocks;
101 rast->blocks[i].x = x;
102 rast->blocks[i].y = y;
103 rast->blocks[i].mask = mask;
104 rast->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 *rast,
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 << 2;
124 int ei2 = tri->ei2 << 2;
125 int ei3 = tri->ei3 << 2;
126
127 int eo1 = tri->eo1 << 2;
128 int eo2 = tri->eo2 << 2;
129 int eo3 = tri->eo3 << 2;
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] << 2);
137 int cx2 = c2 + (tri->step[1][i] << 2);
138 int cx3 = c3 + (tri->step[2][i] << 2);
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, x+ix, y+iy);
150 }
151 else {
152 /* the block is partially in/out of the triangle */
153 do_block_4(rast, 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 const union lp_rast_cmd_arg arg )
167 {
168 const struct lp_rast_triangle *tri = arg.triangle;
169
170 int x = rast->x;
171 int y = rast->y;
172 int ix, iy;
173 unsigned i = 0;
174
175 int c1 = tri->c1 + tri->dx12 * y - tri->dy12 * x;
176 int c2 = tri->c2 + tri->dx23 * y - tri->dy23 * x;
177 int c3 = tri->c3 + tri->dx31 * y - tri->dy31 * x;
178
179 int ei1 = tri->ei1 << 4;
180 int ei2 = tri->ei2 << 4;
181 int ei3 = tri->ei3 << 4;
182
183 int eo1 = tri->eo1 << 4;
184 int eo2 = tri->eo2 << 4;
185 int eo3 = tri->eo3 << 4;
186
187 debug_printf("%s\n", __FUNCTION__);
188
189 rast->nr_blocks = 0;
190
191 /* Walk over the tile to build a list of 4x4 pixel blocks which will
192 * be filled/shaded. We do this at two granularities: 16x16 blocks
193 * and then 4x4 blocks.
194 */
195 for (iy = 0; iy < TILE_SIZE; iy += 16) {
196 for (ix = 0; ix < TILE_SIZE; ix += 16, i++) {
197 int cx1 = c1 + (tri->step[0][i] << 4);
198 int cx2 = c2 + (tri->step[1][i] << 4);
199 int cx3 = c3 + (tri->step[2][i] << 4);
200
201 if (cx1 + eo1 < 0 ||
202 cx2 + eo2 < 0 ||
203 cx3 + eo3 < 0) {
204 /* the block is completely outside the triangle - nop */
205 }
206 else if (cx1 + ei1 > 0 &&
207 cx2 + ei2 > 0 &&
208 cx3 + ei3 > 0) {
209 /* the block is completely inside the triangle */
210 block_full_16(rast, x+ix, y+iy);
211 }
212 else {
213 /* the block is partially in/out of the triangle */
214 do_block_16(rast, tri, x+ix, y+iy, cx1, cx2, cx3);
215 }
216 }
217 }
218
219 /* Shade the 4x4 pixel blocks */
220 for (i = 0; i < rast->nr_blocks; i++)
221 lp_rast_shade_quads(rast, &tri->inputs,
222 rast->blocks[i].x,
223 rast->blocks[i].y,
224 rast->blocks[i].mask);
225 }