1 /**************************************************************************
3 * Copyright 2007-2009 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
29 * Rasterization for binned triangles within a tile
32 #include "util/u_math.h"
34 #include "lp_rast_priv.h"
35 #include "lp_tile_soa.h"
39 * Add a 4x4 block of pixels to the block list.
40 * All pixels are known to be inside the triangle's bounds.
43 block_full_4( struct lp_rasterizer_task
*rast_task
, int x
, int y
)
45 const unsigned i
= rast_task
->nr_blocks
;
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
++;
56 * Add a 16x16 block of pixels to the block list.
57 * All pixels are known to be inside the triangle's bounds.
60 block_full_16( struct lp_rasterizer_task
*rast_task
, int x
, int y
)
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
);
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.
77 do_block_4( struct lp_rasterizer_task
*rast_task
,
78 const struct lp_rast_triangle
*tri
,
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
);
97 /* As we do trivial reject already, masks should rarely be all zero:
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
++;
110 * Evaluate a 16x16 block of pixels to determine which 4x4 subblocks are in/out
111 * of the triangle's bounds.
114 do_block_16( struct lp_rasterizer_task
*rast_task
,
115 const struct lp_rast_triangle
*tri
,
123 int ei1
= tri
->ei1
* 4;
124 int ei2
= tri
->ei2
* 4;
125 int ei3
= tri
->ei3
* 4;
127 int eo1
= tri
->eo1
* 4;
128 int eo2
= tri
->eo2
* 4;
129 int eo3
= tri
->eo3
* 4;
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);
143 /* the block is completely outside the triangle - nop */
145 else if (cx1
+ ei1
> 0 &&
148 /* the block is completely inside the triangle */
149 block_full_4(rast_task
, x
+ix
, y
+iy
);
152 /* the block is partially in/out of the triangle */
153 do_block_4(rast_task
, tri
, x
+ix
, y
+iy
, cx1
, cx2
, cx3
);
161 * Scan the tile in chunks and figure out which pixels to rasterize
165 lp_rast_triangle( struct lp_rasterizer
*rast
,
166 unsigned thread_index
,
167 const union lp_rast_cmd_arg arg
)
169 struct lp_rasterizer_task
*rast_task
= &rast
->tasks
[thread_index
];
170 const struct lp_rast_triangle
*tri
= arg
.triangle
;
172 int x
= rast_task
->x
;
173 int y
= rast_task
->y
;
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
;
181 int ei1
= tri
->ei1
* 16;
182 int ei2
= tri
->ei2
* 16;
183 int ei3
= tri
->ei3
* 16;
185 int eo1
= tri
->eo1
* 16;
186 int eo2
= tri
->eo2
* 16;
187 int eo3
= tri
->eo3
* 16;
189 assert(Elements(rast_task
->blocks
) == (TILE_SIZE
* TILE_SIZE
) / (4*4));
191 LP_DBG(DEBUG_RAST
, "lp_rast_triangle\n");
193 rast_task
->nr_blocks
= 0;
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.
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);
208 /* the block is completely outside the triangle - nop */
210 else if (cx1
+ ei1
> 0 &&
213 /* the block is completely inside the triangle */
214 block_full_16(rast_task
, x
+ix
, y
+iy
);
217 /* the block is partially in/out of the triangle */
218 do_block_16(rast_task
, tri
, x
+ix
, y
+iy
, cx1
, cx2
, cx3
);
223 assert(rast_task
->nr_blocks
<= Elements(rast_task
->blocks
));
225 /* Shade the 4x4 pixel blocks */
226 for (i
= 0; i
< rast_task
->nr_blocks
; i
++)
227 lp_rast_shade_quads(rast
,
230 rast_task
->blocks
[i
].x
,
231 rast_task
->blocks
[i
].y
,
232 rast_task
->blocks
[i
].mask
);