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"
33 #include "lp_rast_priv.h"
34 #include "lp_tile_soa.h"
35 #include "lp_bld_debug.h"
39 struct lp_rasterizer
*lp_rast_create( struct pipe_screen
*screen
)
41 struct lp_rasterizer
*rast
;
43 rast
= CALLOC_STRUCT(lp_rasterizer
);
47 rast
->screen
= screen
;
48 rast
->tile
.color
= align_malloc( TILE_SIZE
*TILE_SIZE
*4, 16 );
49 rast
->tile
.depth
= align_malloc( TILE_SIZE
*TILE_SIZE
*4, 16 );
56 * Begin the rasterization phase.
57 * Map the framebuffer surfaces. Initialize the 'rast' state.
60 lp_rast_begin( struct lp_rasterizer
*rast
,
61 struct pipe_surface
*cbuf
,
62 struct pipe_surface
*zsbuf
,
64 boolean write_zstencil
,
68 struct pipe_screen
*screen
= rast
->screen
;
70 LP_DBG(DEBUG_RAST
, "%s %dx%d\n", __FUNCTION__
, width
, height
);
72 pipe_surface_reference(&rast
->state
.cbuf
, cbuf
);
73 pipe_surface_reference(&rast
->state
.zsbuf
, zsbuf
);
76 rast
->height
= height
;
77 rast
->state
.write_zstencil
= write_zstencil
;
78 rast
->state
.write_color
= write_color
;
80 rast
->check_for_clipped_tiles
= (width
% TILE_SIZE
!= 0 ||
81 height
% TILE_SIZE
!= 0);
84 rast
->cbuf_transfer
= screen
->get_tex_transfer(rast
->screen
,
89 PIPE_TRANSFER_READ_WRITE
,
91 if (!rast
->cbuf_transfer
)
94 rast
->cbuf_map
= screen
->transfer_map(rast
->screen
,
101 rast
->zsbuf_transfer
= screen
->get_tex_transfer(rast
->screen
,
106 PIPE_TRANSFER_READ_WRITE
,
107 0, 0, width
, height
);
108 if (!rast
->zsbuf_transfer
)
111 rast
->zsbuf_map
= screen
->transfer_map(rast
->screen
,
112 rast
->zsbuf_transfer
);
113 if (!rast
->zsbuf_map
)
122 * Finish the rasterization phase.
123 * Unmap framebuffer surfaces.
126 lp_rast_end( struct lp_rasterizer
*rast
)
128 struct pipe_screen
*screen
= rast
->screen
;
131 screen
->transfer_unmap(screen
, rast
->cbuf_transfer
);
134 screen
->transfer_unmap(screen
, rast
->zsbuf_transfer
);
136 if (rast
->cbuf_transfer
)
137 screen
->tex_transfer_destroy(rast
->cbuf_transfer
);
139 if (rast
->zsbuf_transfer
)
140 screen
->tex_transfer_destroy(rast
->zsbuf_transfer
);
142 rast
->cbuf_transfer
= NULL
;
143 rast
->zsbuf_transfer
= NULL
;
144 rast
->cbuf_map
= NULL
;
145 rast
->zsbuf_map
= NULL
;
150 * Begining rasterization of a tile.
151 * \param x window X position of the tile, in pixels
152 * \param y window Y position of the tile, in pixels
155 lp_rast_start_tile( struct lp_rasterizer
*rast
,
156 unsigned x
, unsigned y
)
158 LP_DBG(DEBUG_RAST
, "%s %d,%d\n", __FUNCTION__
, x
, y
);
166 * Clear the rasterizer's current color tile.
167 * This is a bin command called during bin processing.
169 void lp_rast_clear_color( struct lp_rasterizer
*rast
,
170 const union lp_rast_cmd_arg arg
)
172 const uint8_t *clear_color
= arg
.clear_color
;
174 LP_DBG(DEBUG_RAST
, "%s 0x%x,0x%x,0x%x,0x%x\n", __FUNCTION__
,
180 if (clear_color
[0] == clear_color
[1] &&
181 clear_color
[1] == clear_color
[2] &&
182 clear_color
[2] == clear_color
[3]) {
183 memset(rast
->tile
.color
, clear_color
[0], TILE_SIZE
* TILE_SIZE
* 4);
187 for (y
= 0; y
< TILE_SIZE
; y
++)
188 for (x
= 0; x
< TILE_SIZE
; x
++)
189 for (chan
= 0; chan
< 4; ++chan
)
190 TILE_PIXEL(rast
->tile
.color
, x
, y
, chan
) = clear_color
[chan
];
196 * Clear the rasterizer's current z/stencil tile.
197 * This is a bin command called during bin processing.
199 void lp_rast_clear_zstencil( struct lp_rasterizer
*rast
,
200 const union lp_rast_cmd_arg arg
)
204 LP_DBG(DEBUG_RAST
, "%s 0x%x\n", __FUNCTION__
, arg
.clear_zstencil
);
206 for (i
= 0; i
< TILE_SIZE
; i
++)
207 for (j
= 0; j
< TILE_SIZE
; j
++)
208 rast
->tile
.depth
[i
*TILE_SIZE
+ j
] = arg
.clear_zstencil
;
213 * Load tile color from the framebuffer surface.
214 * This is a bin command called during bin processing.
216 void lp_rast_load_color( struct lp_rasterizer
*rast
,
217 const union lp_rast_cmd_arg arg
)
219 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
221 /* call u_tile func to load colors from surface */
226 * Load tile z/stencil from the framebuffer surface.
227 * This is a bin command called during bin processing.
229 void lp_rast_load_zstencil( struct lp_rasterizer
*rast
,
230 const union lp_rast_cmd_arg arg
)
232 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
234 /* call u_tile func to load depth (and stencil?) from surface */
238 void lp_rast_set_state( struct lp_rasterizer
*rast
,
239 const union lp_rast_cmd_arg arg
)
241 const struct lp_rast_state
*state
= arg
.set_state
;
243 LP_DBG(DEBUG_RAST
, "%s %p\n", __FUNCTION__
, (void *) state
);
245 /* just set the current state pointer for this rasterizer */
246 rast
->current_state
= state
;
255 * Run the shader on all blocks in a tile. This is used when a tile is
256 * completely contained inside a triangle.
257 * This is a bin command called during bin processing.
259 void lp_rast_shade_tile( struct lp_rasterizer
*rast
,
260 const union lp_rast_cmd_arg arg
)
262 const struct lp_rast_shader_inputs
*inputs
= arg
.shade_tile
;
263 const unsigned mask
= ~0;
266 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
268 /* Use the existing preference for 4x4 (four quads) shading:
270 for (y
= 0; y
< TILE_SIZE
; y
+= 4)
271 for (x
= 0; x
< TILE_SIZE
; x
+= 4)
272 lp_rast_shade_quads( rast
, inputs
, rast
->x
+ x
, rast
->y
+ y
, mask
);
277 * Compute shading for a 4x4 block of pixels.
278 * This is a bin command called during bin processing.
280 void lp_rast_shade_quads( struct lp_rasterizer
*rast
,
281 const struct lp_rast_shader_inputs
*inputs
,
282 unsigned x
, unsigned y
,
286 const struct lp_rast_state
*state
= rast
->current_state
;
287 struct lp_rast_tile
*tile
= &rast
->tile
;
290 uint32_t ALIGN16_ATTRIB masks
[2][2][2][2];
297 assert(x
% TILE_VECTOR_WIDTH
== 0);
298 assert(y
% TILE_VECTOR_HEIGHT
== 0);
300 /* mask: the rasterizer wants to treat pixels in 4x4 blocks, but
301 * the pixel shader wants to swizzle them into 4 2x2 quads.
303 * Additionally, the pixel shader wants masks as full dword ~0,
304 * while the rasterizer wants to pack per-pixel bits tightly.
308 for (qy
= 0; qy
< 2; ++qy
)
309 for (qx
= 0; qx
< 2; ++qx
)
310 for (iy
= 0; iy
< 2; ++iy
)
311 for (ix
= 0; ix
< 2; ++ix
)
312 masks
[qy
][qx
][iy
][ix
] = mask
& (1 << (qy
*8+iy
*4+qx
*2+ix
)) ? ~0 : 0;
314 masks
[0][0][0][0] = mask
& (1 << (0*8+0*4+0*2+0)) ? ~0 : 0;
315 masks
[0][0][0][1] = mask
& (1 << (0*8+0*4+0*2+1)) ? ~0 : 0;
316 masks
[0][0][1][0] = mask
& (1 << (0*8+1*4+0*2+0)) ? ~0 : 0;
317 masks
[0][0][1][1] = mask
& (1 << (0*8+1*4+0*2+1)) ? ~0 : 0;
318 masks
[0][1][0][0] = mask
& (1 << (0*8+0*4+1*2+0)) ? ~0 : 0;
319 masks
[0][1][0][1] = mask
& (1 << (0*8+0*4+1*2+1)) ? ~0 : 0;
320 masks
[0][1][1][0] = mask
& (1 << (0*8+1*4+1*2+0)) ? ~0 : 0;
321 masks
[0][1][1][1] = mask
& (1 << (0*8+1*4+1*2+1)) ? ~0 : 0;
323 masks
[1][0][0][0] = mask
& (1 << (1*8+0*4+0*2+0)) ? ~0 : 0;
324 masks
[1][0][0][1] = mask
& (1 << (1*8+0*4+0*2+1)) ? ~0 : 0;
325 masks
[1][0][1][0] = mask
& (1 << (1*8+1*4+0*2+0)) ? ~0 : 0;
326 masks
[1][0][1][1] = mask
& (1 << (1*8+1*4+0*2+1)) ? ~0 : 0;
327 masks
[1][1][0][0] = mask
& (1 << (1*8+0*4+1*2+0)) ? ~0 : 0;
328 masks
[1][1][0][1] = mask
& (1 << (1*8+0*4+1*2+1)) ? ~0 : 0;
329 masks
[1][1][1][0] = mask
& (1 << (1*8+1*4+1*2+0)) ? ~0 : 0;
330 masks
[1][1][1][1] = mask
& (1 << (1*8+1*4+1*2+1)) ? ~0 : 0;
333 assert((x
% 2) == 0);
334 assert((y
% 2) == 0);
339 /* offset of the 16x16 pixel block within the tile */
340 block_offset
= ((iy
/4)*(16*16) + (ix
/4)*16);
343 color
= tile
->color
+ 4 * block_offset
;
346 depth
= tile
->depth
+ block_offset
;
348 /* XXX: This will most likely fail on 32bit x86 without -mstackrealign */
349 assert(lp_check_alignment(masks
, 16));
351 assert(lp_check_alignment(depth
, 16));
352 assert(lp_check_alignment(color
, 16));
353 assert(lp_check_alignment(state
->jit_context
.blend_color
, 16));
356 state
->jit_function( &state
->jit_context
,
365 struct lp_rast_tile
*tile
= &rast
->tile
;
373 for (q
= 0; q
< 4; ++q
)
374 for(iy
= 0; iy
< 2; ++iy
)
375 for(ix
= 0; ix
< 2; ++ix
)
376 if(masks
[q
] & (1 << (iy
*2 + ix
)))
377 for (chan_index
= 0; chan_index
< NUM_CHANNELS
; ++chan_index
)
378 TILE_PIXEL(tile
->color
, x
+ q
*2 + ix
, y
+ iy
, chan_index
) = 0xff;
389 * Write the rasterizer's color tile to the framebuffer.
391 static void lp_rast_store_color( struct lp_rasterizer
*rast
)
393 const unsigned x
= rast
->x
;
394 const unsigned y
= rast
->y
;
395 unsigned w
= TILE_SIZE
;
396 unsigned h
= TILE_SIZE
;
398 if (x
+ w
> rast
->width
)
399 w
-= x
+ w
- rast
->width
;
401 if (y
+ h
> rast
->height
)
402 h
-= y
+ h
- rast
->height
;
404 LP_DBG(DEBUG_RAST
, "%s %d,%d %dx%d\n", __FUNCTION__
, x
, y
, w
, h
);
406 lp_tile_write_4ub(rast
->cbuf_transfer
->format
,
409 rast
->cbuf_transfer
->stride
,
416 lp_tile_write_z32(const uint32_t *src
, uint8_t *dst
, unsigned dst_stride
,
417 unsigned x0
, unsigned y0
, unsigned w
, unsigned h
)
420 uint8_t *dst_row
= dst
+ y0
*dst_stride
;
421 for (y
= 0; y
< h
; ++y
) {
422 uint32_t *dst_pixel
= (uint32_t *)(dst_row
+ x0
*4);
423 for (x
= 0; x
< w
; ++x
) {
424 *dst_pixel
++ = *src
++;
426 dst_row
+= dst_stride
;
431 * Write the rasterizer's z/stencil tile to the framebuffer.
433 static void lp_rast_store_zstencil( struct lp_rasterizer
*rast
)
435 const unsigned x
= rast
->x
;
436 const unsigned y
= rast
->y
;
437 unsigned w
= TILE_SIZE
;
438 unsigned h
= TILE_SIZE
;
440 if (x
+ w
> rast
->width
)
441 w
-= x
+ w
- rast
->width
;
443 if (y
+ h
> rast
->height
)
444 h
-= y
+ h
- rast
->height
;
446 LP_DBG(DEBUG_RAST
, "%s %d,%d %dx%d\n", __FUNCTION__
, x
, y
, w
, h
);
448 assert(rast
->zsbuf_transfer
->format
== PIPE_FORMAT_Z32_UNORM
);
449 lp_tile_write_z32(rast
->tile
.depth
,
451 rast
->zsbuf_transfer
->stride
,
457 * Write the rasterizer's tiles to the framebuffer.
460 lp_rast_end_tile( struct lp_rasterizer
*rast
)
462 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
464 if (rast
->state
.write_color
)
465 lp_rast_store_color(rast
);
467 if (rast
->state
.write_zstencil
)
468 lp_rast_store_zstencil(rast
);
473 * Rasterize commands for a single bin.
474 * \param x, y position of the bin's tile in the framebuffer
475 * Must be called between lp_rast_begin() and lp_rast_end().
478 rasterize_bin( struct lp_rasterizer
*rast
,
479 const struct cmd_bin
*bin
,
482 const struct cmd_block_list
*commands
= &bin
->commands
;
483 struct cmd_block
*block
;
486 lp_rast_start_tile( rast
, x
, y
);
488 /* simply execute each of the commands in the block list */
489 for (block
= commands
->head
; block
; block
= block
->next
) {
490 for (k
= 0; k
< block
->count
; k
++) {
491 block
->cmd
[k
]( rast
, block
->arg
[k
] );
495 lp_rast_end_tile( rast
);
500 * Rasterize/execute all bins.
503 lp_rasterize_bins( struct lp_rasterizer
*rast
,
504 struct lp_bins
*bins
,
505 const struct pipe_framebuffer_state
*fb
,
508 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
513 fb
->cbufs
[0] != NULL
,
514 fb
->zsbuf
!= NULL
&& write_depth
,
518 /* loop over tile bins, rasterize each */
522 for (i
= 0; i
< bins
->tiles_x
; i
++) {
523 for (j
= 0; j
< bins
->tiles_y
; j
++) {
524 struct cmd_bin
*bin
= lp_get_bin(bins
, i
, j
);
525 rasterize_bin( rast
, bin
, i
* TILE_SIZE
, j
* TILE_SIZE
);
534 lp_bin_iter_begin( bins
);
536 while ((bin
= lp_bin_iter_next(bins
, &x
, &y
))) {
537 rasterize_bin( rast
, bin
, x
* TILE_SIZE
, y
* TILE_SIZE
);
544 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
551 void lp_rast_destroy( struct lp_rasterizer
*rast
)
553 pipe_surface_reference(&rast
->state
.cbuf
, NULL
);
554 pipe_surface_reference(&rast
->state
.zsbuf
, NULL
);
555 align_free(rast
->tile
.depth
);
556 align_free(rast
->tile
.color
);