llvmpipe: use LP_DBG() macro everywhere
[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 *rast, int x, int y )
44 {
45 const unsigned i = rast->nr_blocks;
46 assert(x % 4 == 0);
47 assert(y % 4 == 0);
48 rast->blocks[i].x = x;
49 rast->blocks[i].y = y;
50 rast->blocks[i].mask = ~0;
51 rast->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 *rast, 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, 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 *rast,
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->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 * 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, 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 * 16;
180 int ei2 = tri->ei2 * 16;
181 int ei3 = tri->ei3 * 16;
182
183 int eo1 = tri->eo1 * 16;
184 int eo2 = tri->eo2 * 16;
185 int eo3 = tri->eo3 * 16;
186
187 assert(Elements(rast->blocks) == (TILE_SIZE * TILE_SIZE) / (4*4));
188
189 LP_DBG(DEBUG_RAST, "lp_rast_triangle\n");
190
191 rast->nr_blocks = 0;
192
193 /* Walk over the tile to build a list of 4x4 pixel blocks which will
194 * be filled/shaded. We do this at two granularities: 16x16 blocks
195 * and then 4x4 blocks.
196 */
197 for (iy = 0; iy < TILE_SIZE; iy += 16) {
198 for (ix = 0; ix < TILE_SIZE; ix += 16, i++) {
199 int cx1 = c1 + (tri->step[0][i] * 16);
200 int cx2 = c2 + (tri->step[1][i] * 16);
201 int cx3 = c3 + (tri->step[2][i] * 16);
202
203 if (cx1 + eo1 < 0 ||
204 cx2 + eo2 < 0 ||
205 cx3 + eo3 < 0) {
206 /* the block is completely outside the triangle - nop */
207 }
208 else if (cx1 + ei1 > 0 &&
209 cx2 + ei2 > 0 &&
210 cx3 + ei3 > 0) {
211 /* the block is completely inside the triangle */
212 block_full_16(rast, x+ix, y+iy);
213 }
214 else {
215 /* the block is partially in/out of the triangle */
216 do_block_16(rast, tri, x+ix, y+iy, cx1, cx2, cx3);
217 }
218 }
219 }
220
221 assert(rast->nr_blocks <= Elements(rast->blocks));
222
223 /* Shade the 4x4 pixel blocks */
224 for (i = 0; i < rast->nr_blocks; i++)
225 lp_rast_shade_quads(rast, &tri->inputs,
226 rast->blocks[i].x,
227 rast->blocks[i].y,
228 rast->blocks[i].mask);
229 }