1 /**************************************************************************
3 * Copyright 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 **************************************************************************/
28 #include "util/u_memory.h"
32 #include "lp_rast_priv.h"
33 #include "lp_tile_soa.h"
34 #include "lp_bld_debug.h"
36 #define RAST_DEBUG debug_printf
38 struct lp_rasterizer
*lp_rast_create( struct pipe_screen
*screen
)
40 struct lp_rasterizer
*rast
;
42 rast
= CALLOC_STRUCT(lp_rasterizer
);
46 rast
->screen
= screen
;
47 rast
->tile
.color
= align_malloc( TILE_SIZE
*TILE_SIZE
*4, 16 );
48 rast
->tile
.depth
= align_malloc( TILE_SIZE
*TILE_SIZE
*4, 16 );
55 * Begin the rasterization phase.
56 * Map the framebuffer surfaces. Initialize the 'rast' state.
58 boolean
lp_rast_begin( struct lp_rasterizer
*rast
,
59 struct pipe_surface
*cbuf
,
60 struct pipe_surface
*zsbuf
,
62 boolean write_zstencil
,
66 struct pipe_screen
*screen
= rast
->screen
;
68 RAST_DEBUG("%s %dx%d\n", __FUNCTION__
, width
, height
);
70 pipe_surface_reference(&rast
->state
.cbuf
, cbuf
);
71 pipe_surface_reference(&rast
->state
.zsbuf
, zsbuf
);
74 rast
->height
= height
;
75 rast
->state
.write_zstencil
= write_zstencil
;
76 rast
->state
.write_color
= write_color
;
78 rast
->check_for_clipped_tiles
= (width
% TILE_SIZE
!= 0 ||
79 height
% TILE_SIZE
!= 0);
82 rast
->cbuf_transfer
= screen
->get_tex_transfer(rast
->screen
,
87 PIPE_TRANSFER_READ_WRITE
,
89 if (!rast
->cbuf_transfer
)
92 rast
->cbuf_map
= screen
->transfer_map(rast
->screen
,
99 rast
->zsbuf_transfer
= screen
->get_tex_transfer(rast
->screen
,
104 PIPE_TRANSFER_READ_WRITE
,
105 0, 0, width
, height
);
106 if (!rast
->zsbuf_transfer
)
109 rast
->zsbuf_map
= screen
->transfer_map(rast
->screen
,
110 rast
->zsbuf_transfer
);
111 if (!rast
->zsbuf_map
)
120 * Finish the rasterization phase.
121 * Unmap framebuffer surfaces.
123 void lp_rast_end( struct lp_rasterizer
*rast
)
125 struct pipe_screen
*screen
= rast
->screen
;
128 screen
->transfer_unmap(screen
, rast
->cbuf_transfer
);
131 screen
->transfer_unmap(screen
, rast
->zsbuf_transfer
);
133 if (rast
->cbuf_transfer
)
134 screen
->tex_transfer_destroy(rast
->cbuf_transfer
);
136 if (rast
->zsbuf_transfer
)
137 screen
->tex_transfer_destroy(rast
->zsbuf_transfer
);
139 rast
->cbuf_transfer
= NULL
;
140 rast
->zsbuf_transfer
= NULL
;
141 rast
->cbuf_map
= NULL
;
142 rast
->zsbuf_map
= NULL
;
147 * Begining rasterization of a tile.
148 * \param x window X position of the tile, in pixels
149 * \param y window Y position of the tile, in pixels
151 void lp_rast_start_tile( struct lp_rasterizer
*rast
,
155 RAST_DEBUG("%s %d,%d\n", __FUNCTION__
, x
, y
);
163 * Clear the rasterizer's current color tile.
165 void lp_rast_clear_color( struct lp_rasterizer
*rast
,
166 const union lp_rast_cmd_arg arg
)
168 const uint8_t *clear_color
= arg
.clear_color
;
170 RAST_DEBUG("%s 0x%x,0x%x,0x%x,0x%x\n", __FUNCTION__
,
176 if (clear_color
[0] == clear_color
[1] &&
177 clear_color
[1] == clear_color
[2] &&
178 clear_color
[2] == clear_color
[3]) {
179 memset(rast
->tile
.color
, clear_color
[0], TILE_SIZE
* TILE_SIZE
* 4);
183 for (y
= 0; y
< TILE_SIZE
; y
++)
184 for (x
= 0; x
< TILE_SIZE
; x
++)
185 for (chan
= 0; chan
< 4; ++chan
)
186 TILE_PIXEL(rast
->tile
.color
, x
, y
, chan
) = clear_color
[chan
];
192 * Clear the rasterizer's current z/stencil tile.
194 void lp_rast_clear_zstencil( struct lp_rasterizer
*rast
,
195 const union lp_rast_cmd_arg arg
)
199 RAST_DEBUG("%s 0x%x\n", __FUNCTION__
, arg
.clear_zstencil
);
201 for (i
= 0; i
< TILE_SIZE
; i
++)
202 for (j
= 0; j
< TILE_SIZE
; j
++)
203 rast
->tile
.depth
[i
*TILE_SIZE
+ j
] = arg
.clear_zstencil
;
207 void lp_rast_load_color( struct lp_rasterizer
*rast
,
208 const union lp_rast_cmd_arg arg
)
210 RAST_DEBUG("%s\n", __FUNCTION__
);
212 /* call u_tile func to load colors from surface */
215 void lp_rast_load_zstencil( struct lp_rasterizer
*rast
,
216 const union lp_rast_cmd_arg arg
)
218 RAST_DEBUG("%s\n", __FUNCTION__
);
220 /* call u_tile func to load depth (and stencil?) from surface */
226 void lp_rast_shade_tile( struct lp_rasterizer
*rast
,
227 const union lp_rast_cmd_arg arg
)
229 const struct lp_rast_shader_inputs
*inputs
= arg
.shade_tile
;
230 const unsigned mask
= ~0;
233 RAST_DEBUG("%s\n", __FUNCTION__
);
235 /* Use the existing preference for 4x4 (four quads) shading:
237 for (y
= 0; y
< TILE_SIZE
; y
+= 4)
238 for (x
= 0; x
< TILE_SIZE
; x
+= 4)
239 lp_rast_shade_quads( rast
, inputs
, rast
->x
+ x
, rast
->y
+ y
, mask
);
244 * Compute shading for a 4x4 block of pixels.
246 void lp_rast_shade_quads( struct lp_rasterizer
*rast
,
247 const struct lp_rast_shader_inputs
*inputs
,
248 unsigned x
, unsigned y
,
252 const struct lp_rast_state
*state
= inputs
->state
;
253 struct lp_rast_tile
*tile
= &rast
->tile
;
256 uint32_t ALIGN16_ATTRIB masks
[2][2][2][2];
261 assert(x
% TILE_VECTOR_WIDTH
== 0);
262 assert(y
% TILE_VECTOR_HEIGHT
== 0);
264 /* mask: the rasterizer wants to treat pixels in 4x4 blocks, but
265 * the pixel shader wants to swizzle them into 4 2x2 quads.
267 * Additionally, the pixel shader wants masks as full dword ~0,
268 * while the rasterizer wants to pack per-pixel bits tightly.
272 for (qy
= 0; qy
< 2; ++qy
)
273 for (qx
= 0; qx
< 2; ++qx
)
274 for (iy
= 0; iy
< 2; ++iy
)
275 for (ix
= 0; ix
< 2; ++ix
)
276 masks
[qy
][qx
][iy
][ix
] = mask
& (1 << (qy
*8+iy
*4+qx
*2+ix
)) ? ~0 : 0;
278 masks
[0][0][0][0] = mask
& (1 << (0*8+0*4+0*2+0)) ? ~0 : 0;
279 masks
[0][0][0][1] = mask
& (1 << (0*8+0*4+0*2+1)) ? ~0 : 0;
280 masks
[0][0][1][0] = mask
& (1 << (0*8+1*4+0*2+0)) ? ~0 : 0;
281 masks
[0][0][1][1] = mask
& (1 << (0*8+1*4+0*2+1)) ? ~0 : 0;
282 masks
[0][1][0][0] = mask
& (1 << (0*8+0*4+1*2+0)) ? ~0 : 0;
283 masks
[0][1][0][1] = mask
& (1 << (0*8+0*4+1*2+1)) ? ~0 : 0;
284 masks
[0][1][1][0] = mask
& (1 << (0*8+1*4+1*2+0)) ? ~0 : 0;
285 masks
[0][1][1][1] = mask
& (1 << (0*8+1*4+1*2+1)) ? ~0 : 0;
287 masks
[1][0][0][0] = mask
& (1 << (1*8+0*4+0*2+0)) ? ~0 : 0;
288 masks
[1][0][0][1] = mask
& (1 << (1*8+0*4+0*2+1)) ? ~0 : 0;
289 masks
[1][0][1][0] = mask
& (1 << (1*8+1*4+0*2+0)) ? ~0 : 0;
290 masks
[1][0][1][1] = mask
& (1 << (1*8+1*4+0*2+1)) ? ~0 : 0;
291 masks
[1][1][0][0] = mask
& (1 << (1*8+0*4+1*2+0)) ? ~0 : 0;
292 masks
[1][1][0][1] = mask
& (1 << (1*8+0*4+1*2+1)) ? ~0 : 0;
293 masks
[1][1][1][0] = mask
& (1 << (1*8+1*4+1*2+0)) ? ~0 : 0;
294 masks
[1][1][1][1] = mask
& (1 << (1*8+1*4+1*2+1)) ? ~0 : 0;
297 assert((x
% 2) == 0);
298 assert((y
% 2) == 0);
303 /* offset of the 16x16 pixel block within the tile */
304 block_offset
= ((iy
/4)*(16*16) + (ix
/4)*16);
307 color
= tile
->color
+ 4 * block_offset
;
310 depth
= tile
->depth
+ block_offset
;
312 /* XXX: This will most likely fail on 32bit x86 without -mstackrealign */
313 assert(lp_check_alignment(masks
, 16));
315 assert(lp_check_alignment(depth
, 16));
316 assert(lp_check_alignment(color
, 16));
317 assert(lp_check_alignment(state
->jit_context
.blend_color
, 16));
320 state
->jit_function( &state
->jit_context
,
329 struct lp_rast_tile
*tile
= &rast
->tile
;
337 for (q
= 0; q
< 4; ++q
)
338 for(iy
= 0; iy
< 2; ++iy
)
339 for(ix
= 0; ix
< 2; ++ix
)
340 if(masks
[q
] & (1 << (iy
*2 + ix
)))
341 for (chan_index
= 0; chan_index
< NUM_CHANNELS
; ++chan_index
)
342 TILE_PIXEL(tile
->color
, x
+ q
*2 + ix
, y
+ iy
, chan_index
) = 0xff;
353 * Write the rasterizer's color tile to the framebuffer.
355 static void lp_rast_store_color( struct lp_rasterizer
*rast
)
357 const unsigned x
= rast
->x
;
358 const unsigned y
= rast
->y
;
359 unsigned w
= TILE_SIZE
;
360 unsigned h
= TILE_SIZE
;
362 if (x
+ w
> rast
->width
)
363 w
-= x
+ w
- rast
->width
;
365 if (y
+ h
> rast
->height
)
366 h
-= y
+ h
- rast
->height
;
368 RAST_DEBUG("%s %d,%d %dx%d\n", __FUNCTION__
, x
, y
, w
, h
);
370 lp_tile_write_4ub(rast
->cbuf_transfer
->format
,
373 rast
->cbuf_transfer
->stride
,
380 lp_tile_write_z32(const uint32_t *src
, uint8_t *dst
, unsigned dst_stride
,
381 unsigned x0
, unsigned y0
, unsigned w
, unsigned h
)
384 uint8_t *dst_row
= dst
+ y0
*dst_stride
;
385 for (y
= 0; y
< h
; ++y
) {
386 uint32_t *dst_pixel
= (uint32_t *)(dst_row
+ x0
*4);
387 for (x
= 0; x
< w
; ++x
) {
388 *dst_pixel
++ = *src
++;
390 dst_row
+= dst_stride
;
395 * Write the rasterizer's z/stencil tile to the framebuffer.
397 static void lp_rast_store_zstencil( struct lp_rasterizer
*rast
)
399 const unsigned x
= rast
->x
;
400 const unsigned y
= rast
->y
;
401 unsigned w
= TILE_SIZE
;
402 unsigned h
= TILE_SIZE
;
404 if (x
+ w
> rast
->width
)
405 w
-= x
+ w
- rast
->width
;
407 if (y
+ h
> rast
->height
)
408 h
-= y
+ h
- rast
->height
;
410 RAST_DEBUG("%s %d,%d %dx%d\n", __FUNCTION__
, x
, y
, w
, h
);
412 assert(rast
->zsbuf_transfer
->format
== PIPE_FORMAT_Z32_UNORM
);
413 lp_tile_write_z32(rast
->tile
.depth
,
415 rast
->zsbuf_transfer
->stride
,
421 * Write the rasterizer's tiles to the framebuffer.
423 void lp_rast_end_tile( struct lp_rasterizer
*rast
)
425 RAST_DEBUG("%s\n", __FUNCTION__
);
427 if (rast
->state
.write_color
)
428 lp_rast_store_color(rast
);
430 if (rast
->state
.write_zstencil
)
431 lp_rast_store_zstencil(rast
);
437 void lp_rast_destroy( struct lp_rasterizer
*rast
)
439 pipe_surface_reference(&rast
->state
.cbuf
, NULL
);
440 pipe_surface_reference(&rast
->state
.zsbuf
, NULL
);
441 align_free(rast
->tile
.depth
);
442 align_free(rast
->tile
.color
);