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 **************************************************************************/
29 #include "util/u_memory.h"
30 #include "util/u_math.h"
31 #include "util/u_cpu_detect.h"
32 #include "util/u_surface.h"
34 #include "lp_scene_queue.h"
38 #include "lp_rast_priv.h"
39 #include "lp_tile_soa.h"
40 #include "lp_bld_debug.h"
45 * Begin the rasterization phase.
46 * Map the framebuffer surfaces. Initialize the 'rast' state.
49 lp_rast_begin( struct lp_rasterizer
*rast
,
50 const struct pipe_framebuffer_state
*fb
,
52 boolean write_zstencil
)
54 struct pipe_screen
*screen
= rast
->screen
;
55 struct pipe_surface
*cbuf
, *zsbuf
;
58 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
60 util_copy_framebuffer_state(&rast
->state
.fb
, fb
);
62 rast
->state
.write_zstencil
= write_zstencil
;
63 rast
->state
.write_color
= write_color
;
65 rast
->check_for_clipped_tiles
= (fb
->width
% TILE_SIZE
!= 0 ||
66 fb
->height
% TILE_SIZE
!= 0);
69 for (i
= 0; i
< rast
->state
.fb
.nr_cbufs
; i
++) {
70 cbuf
= rast
->state
.fb
.cbufs
[i
];
72 rast
->cbuf_transfer
[i
] = screen
->get_tex_transfer(rast
->screen
,
77 PIPE_TRANSFER_READ_WRITE
,
81 if (!rast
->cbuf_transfer
[i
])
84 rast
->cbuf_map
[i
] = screen
->transfer_map(rast
->screen
,
85 rast
->cbuf_transfer
[i
]);
86 if (!rast
->cbuf_map
[i
])
91 zsbuf
= rast
->state
.fb
.zsbuf
;
93 rast
->zsbuf_transfer
= screen
->get_tex_transfer(rast
->screen
,
98 PIPE_TRANSFER_READ_WRITE
,
102 if (!rast
->zsbuf_transfer
)
105 rast
->zsbuf_map
= screen
->transfer_map(rast
->screen
,
106 rast
->zsbuf_transfer
);
107 if (!rast
->zsbuf_map
)
114 /* Unmap and release transfers?
121 * Finish the rasterization phase.
122 * Unmap framebuffer surfaces.
125 lp_rast_end( struct lp_rasterizer
*rast
)
127 struct pipe_screen
*screen
= rast
->screen
;
130 for (i
= 0; i
< rast
->state
.fb
.nr_cbufs
; i
++) {
131 if (rast
->cbuf_map
[i
])
132 screen
->transfer_unmap(screen
, rast
->cbuf_transfer
[i
]);
134 if (rast
->cbuf_transfer
[i
])
135 screen
->tex_transfer_destroy(rast
->cbuf_transfer
[i
]);
137 rast
->cbuf_transfer
[i
] = NULL
;
138 rast
->cbuf_map
[i
] = NULL
;
142 screen
->transfer_unmap(screen
, rast
->zsbuf_transfer
);
144 if (rast
->zsbuf_transfer
)
145 screen
->tex_transfer_destroy(rast
->zsbuf_transfer
);
147 rast
->zsbuf_transfer
= NULL
;
148 rast
->zsbuf_map
= NULL
;
153 * Begining rasterization of a tile.
154 * \param x window X position of the tile, in pixels
155 * \param y window Y position of the tile, in pixels
158 lp_rast_start_tile( struct lp_rasterizer
*rast
,
159 unsigned thread_index
,
160 unsigned x
, unsigned y
)
162 LP_DBG(DEBUG_RAST
, "%s %d,%d\n", __FUNCTION__
, x
, y
);
164 rast
->tasks
[thread_index
].x
= x
;
165 rast
->tasks
[thread_index
].y
= y
;
170 * Clear the rasterizer's current color tile.
171 * This is a bin command called during bin processing.
173 void lp_rast_clear_color( struct lp_rasterizer
*rast
,
174 unsigned thread_index
,
175 const union lp_rast_cmd_arg arg
)
177 const uint8_t *clear_color
= arg
.clear_color
;
178 uint8_t **color_tile
= rast
->tasks
[thread_index
].tile
.color
;
181 LP_DBG(DEBUG_RAST
, "%s 0x%x,0x%x,0x%x,0x%x\n", __FUNCTION__
,
187 if (clear_color
[0] == clear_color
[1] &&
188 clear_color
[1] == clear_color
[2] &&
189 clear_color
[2] == clear_color
[3]) {
190 /* clear to grayscale value {x, x, x, x} */
191 for (i
= 0; i
< rast
->state
.fb
.nr_cbufs
; i
++) {
192 memset(color_tile
[i
], clear_color
[0], TILE_SIZE
* TILE_SIZE
* 4);
197 * Note: if the swizzled tile layout changes (see TILE_PIXEL) this code
198 * will need to change. It'll be pretty obvious when clearing no longer
201 const unsigned chunk
= TILE_SIZE
/ 4;
202 for (i
= 0; i
< rast
->state
.fb
.nr_cbufs
; i
++) {
203 uint8_t *c
= color_tile
[i
];
205 for (j
= 0; j
< 4 * TILE_SIZE
; j
++) {
206 memset(c
, clear_color
[0], chunk
);
208 memset(c
, clear_color
[1], chunk
);
210 memset(c
, clear_color
[2], chunk
);
212 memset(c
, clear_color
[3], chunk
);
215 assert(c
- color_tile
[i
] == TILE_SIZE
* TILE_SIZE
* 4);
222 * Clear the rasterizer's current z/stencil tile.
223 * This is a bin command called during bin processing.
225 void lp_rast_clear_zstencil( struct lp_rasterizer
*rast
,
226 unsigned thread_index
,
227 const union lp_rast_cmd_arg arg
)
230 uint32_t *depth_tile
= rast
->tasks
[thread_index
].tile
.depth
;
232 LP_DBG(DEBUG_RAST
, "%s 0x%x\n", __FUNCTION__
, arg
.clear_zstencil
);
234 for (i
= 0; i
< TILE_SIZE
* TILE_SIZE
; i
++)
235 depth_tile
[i
] = arg
.clear_zstencil
;
240 * Load tile color from the framebuffer surface.
241 * This is a bin command called during bin processing.
243 void lp_rast_load_color( struct lp_rasterizer
*rast
,
244 unsigned thread_index
,
245 const union lp_rast_cmd_arg arg
)
247 struct lp_rasterizer_task
*task
= &rast
->tasks
[thread_index
];
248 const unsigned x
= task
->x
;
249 const unsigned y
= task
->y
;
252 LP_DBG(DEBUG_RAST
, "%s at %u, %u\n", __FUNCTION__
, x
, y
);
254 for (i
= 0; i
< rast
->state
.fb
.nr_cbufs
; i
++) {
255 struct pipe_transfer
*transfer
= rast
->cbuf_transfer
[i
];
259 if (x
>= transfer
->width
)
262 if (y
>= transfer
->height
)
267 assert(w
<= TILE_SIZE
);
268 assert(h
<= TILE_SIZE
);
270 lp_tile_read_4ub(transfer
->texture
->format
,
281 lp_tile_read_z32(uint32_t *tile
,
284 unsigned x0
, unsigned y0
, unsigned w
, unsigned h
)
287 const uint8_t *map_row
= map
+ y0
*map_stride
;
288 for (y
= 0; y
< h
; ++y
) {
289 const uint32_t *map_pixel
= (uint32_t *)(map_row
+ x0
*4);
290 for (x
= 0; x
< w
; ++x
) {
291 *tile
++ = *map_pixel
++;
293 map_row
+= map_stride
;
298 * Load tile z/stencil from the framebuffer surface.
299 * This is a bin command called during bin processing.
301 void lp_rast_load_zstencil( struct lp_rasterizer
*rast
,
302 unsigned thread_index
,
303 const union lp_rast_cmd_arg arg
)
305 struct lp_rasterizer_task
*task
= &rast
->tasks
[thread_index
];
306 const unsigned x
= task
->x
;
307 const unsigned y
= task
->y
;
308 unsigned w
= TILE_SIZE
;
309 unsigned h
= TILE_SIZE
;
311 if (x
+ w
> rast
->state
.fb
.width
)
312 w
-= x
+ w
- rast
->state
.fb
.width
;
314 if (y
+ h
> rast
->state
.fb
.height
)
315 h
-= y
+ h
- rast
->state
.fb
.height
;
317 LP_DBG(DEBUG_RAST
, "%s %d,%d %dx%d\n", __FUNCTION__
, x
, y
, w
, h
);
319 assert(rast
->zsbuf_transfer
->texture
->format
== PIPE_FORMAT_Z32_UNORM
);
320 lp_tile_read_z32(task
->tile
.depth
,
322 rast
->zsbuf_transfer
->stride
,
327 void lp_rast_set_state( struct lp_rasterizer
*rast
,
328 unsigned thread_index
,
329 const union lp_rast_cmd_arg arg
)
331 const struct lp_rast_state
*state
= arg
.set_state
;
333 LP_DBG(DEBUG_RAST
, "%s %p\n", __FUNCTION__
, (void *) state
);
335 /* just set the current state pointer for this rasterizer */
336 rast
->tasks
[thread_index
].current_state
= state
;
342 * Run the shader on all blocks in a tile. This is used when a tile is
343 * completely contained inside a triangle.
344 * This is a bin command called during bin processing.
346 void lp_rast_shade_tile( struct lp_rasterizer
*rast
,
347 unsigned thread_index
,
348 const union lp_rast_cmd_arg arg
)
350 struct lp_rasterizer_task
*task
= &rast
->tasks
[thread_index
];
351 const struct lp_rast_state
*state
= task
->current_state
;
352 struct lp_rast_tile
*tile
= &task
->tile
;
353 const struct lp_rast_shader_inputs
*inputs
= arg
.shade_tile
;
354 const unsigned tile_x
= task
->x
;
355 const unsigned tile_y
= task
->y
;
358 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
360 /* render the whole 64x64 tile in 4x4 chunks */
361 for (y
= 0; y
< TILE_SIZE
; y
+= 4){
362 for (x
= 0; x
< TILE_SIZE
; x
+= 4) {
363 uint8_t *color
[PIPE_MAX_COLOR_BUFS
];
365 unsigned block_offset
, i
;
367 /* offset of the 16x16 pixel block within the tile */
368 block_offset
= ((y
/ 4) * (16 * 16) + (x
/ 4) * 16);
371 for (i
= 0; i
< rast
->state
.fb
.nr_cbufs
; i
++)
372 color
[i
] = tile
->color
[i
] + 4 * block_offset
;
375 depth
= tile
->depth
+ block_offset
;
378 state
->jit_function
[0]( &state
->jit_context
,
379 tile_x
+ x
, tile_y
+ y
,
385 INT_MIN
, INT_MIN
, INT_MIN
,
393 * Compute shading for a 4x4 block of pixels.
394 * This is a bin command called during bin processing.
396 void lp_rast_shade_quads( struct lp_rasterizer
*rast
,
397 unsigned thread_index
,
398 const struct lp_rast_shader_inputs
*inputs
,
399 unsigned x
, unsigned y
,
400 int32_t c1
, int32_t c2
, int32_t c3
)
402 struct lp_rasterizer_task
*task
= &rast
->tasks
[thread_index
];
403 const struct lp_rast_state
*state
= task
->current_state
;
404 struct lp_rast_tile
*tile
= &task
->tile
;
405 uint8_t *color
[PIPE_MAX_COLOR_BUFS
];
415 assert(x
% TILE_VECTOR_WIDTH
== 0);
416 assert(y
% TILE_VECTOR_HEIGHT
== 0);
418 assert((x
% 4) == 0);
419 assert((y
% 4) == 0);
425 /* offset of the 16x16 pixel block within the tile */
426 block_offset
= ((iy
/ 4) * (16 * 16) + (ix
/ 4) * 16);
429 for (i
= 0; i
< rast
->state
.fb
.nr_cbufs
; i
++)
430 color
[i
] = tile
->color
[i
] + 4 * block_offset
;
433 depth
= tile
->depth
+ block_offset
;
438 assert(lp_check_alignment(tile
->depth
, 16));
439 assert(lp_check_alignment(tile
->color
[0], 16));
440 assert(lp_check_alignment(state
->jit_context
.blend_color
, 16));
442 assert(lp_check_alignment(inputs
->step
[0], 16));
443 assert(lp_check_alignment(inputs
->step
[1], 16));
444 assert(lp_check_alignment(inputs
->step
[2], 16));
448 state
->jit_function
[1]( &state
->jit_context
,
456 inputs
->step
[0], inputs
->step
[1], inputs
->step
[2]);
461 * Set top row and left column of the tile's pixels to white. For debugging.
464 outline_tile(uint8_t *tile
)
466 const uint8_t val
= 0xff;
469 for (i
= 0; i
< TILE_SIZE
; i
++) {
470 TILE_PIXEL(tile
, i
, 0, 0) = val
;
471 TILE_PIXEL(tile
, i
, 0, 1) = val
;
472 TILE_PIXEL(tile
, i
, 0, 2) = val
;
473 TILE_PIXEL(tile
, i
, 0, 3) = val
;
475 TILE_PIXEL(tile
, 0, i
, 0) = val
;
476 TILE_PIXEL(tile
, 0, i
, 1) = val
;
477 TILE_PIXEL(tile
, 0, i
, 2) = val
;
478 TILE_PIXEL(tile
, 0, i
, 3) = val
;
484 * Draw grid of gray lines at 16-pixel intervals across the tile to
485 * show the sub-tile boundaries. For debugging.
488 outline_subtiles(uint8_t *tile
)
490 const uint8_t val
= 0x80;
491 const unsigned step
= 16;
494 for (i
= 0; i
< TILE_SIZE
; i
+= step
) {
495 for (j
= 0; j
< TILE_SIZE
; j
++) {
496 TILE_PIXEL(tile
, i
, j
, 0) = val
;
497 TILE_PIXEL(tile
, i
, j
, 1) = val
;
498 TILE_PIXEL(tile
, i
, j
, 2) = val
;
499 TILE_PIXEL(tile
, i
, j
, 3) = val
;
501 TILE_PIXEL(tile
, j
, i
, 0) = val
;
502 TILE_PIXEL(tile
, j
, i
, 1) = val
;
503 TILE_PIXEL(tile
, j
, i
, 2) = val
;
504 TILE_PIXEL(tile
, j
, i
, 3) = val
;
514 * Write the rasterizer's color tile to the framebuffer.
516 static void lp_rast_store_color( struct lp_rasterizer
*rast
,
517 unsigned thread_index
)
519 struct lp_rasterizer_task
*task
= &rast
->tasks
[thread_index
];
520 const unsigned x
= task
->x
;
521 const unsigned y
= task
->y
;
524 for (i
= 0; i
< rast
->state
.fb
.nr_cbufs
; i
++) {
525 struct pipe_transfer
*transfer
= rast
->cbuf_transfer
[i
];
529 if (x
>= transfer
->width
)
532 if (y
>= transfer
->height
)
535 LP_DBG(DEBUG_RAST
, "%s [%u] %d,%d %dx%d\n", __FUNCTION__
,
536 thread_index
, x
, y
, w
, h
);
538 if (LP_DEBUG
& DEBUG_SHOW_SUBTILES
)
539 outline_subtiles(task
->tile
.color
[i
]);
540 else if (LP_DEBUG
& DEBUG_SHOW_TILES
)
541 outline_tile(task
->tile
.color
[i
]);
543 lp_tile_write_4ub(transfer
->texture
->format
,
554 lp_tile_write_z32(const uint32_t *src
, uint8_t *dst
, unsigned dst_stride
,
555 unsigned x0
, unsigned y0
, unsigned w
, unsigned h
)
558 uint8_t *dst_row
= dst
+ y0
*dst_stride
;
559 for (y
= 0; y
< h
; ++y
) {
560 uint32_t *dst_pixel
= (uint32_t *)(dst_row
+ x0
*4);
561 for (x
= 0; x
< w
; ++x
) {
562 *dst_pixel
++ = *src
++;
564 dst_row
+= dst_stride
;
569 * Write the rasterizer's z/stencil tile to the framebuffer.
571 static void lp_rast_store_zstencil( struct lp_rasterizer
*rast
,
572 unsigned thread_index
)
574 struct lp_rasterizer_task
*task
= &rast
->tasks
[thread_index
];
575 const unsigned x
= task
->x
;
576 const unsigned y
= task
->y
;
577 unsigned w
= TILE_SIZE
;
578 unsigned h
= TILE_SIZE
;
580 if (x
+ w
> rast
->state
.fb
.width
)
581 w
-= x
+ w
- rast
->state
.fb
.width
;
583 if (y
+ h
> rast
->state
.fb
.height
)
584 h
-= y
+ h
- rast
->state
.fb
.height
;
586 LP_DBG(DEBUG_RAST
, "%s %d,%d %dx%d\n", __FUNCTION__
, x
, y
, w
, h
);
588 assert(rast
->zsbuf_transfer
->texture
->format
== PIPE_FORMAT_Z32_UNORM
);
589 lp_tile_write_z32(task
->tile
.depth
,
591 rast
->zsbuf_transfer
->stride
,
597 * Write the rasterizer's tiles to the framebuffer.
600 lp_rast_end_tile( struct lp_rasterizer
*rast
,
601 unsigned thread_index
)
603 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
605 if (rast
->state
.write_color
)
606 lp_rast_store_color(rast
, thread_index
);
608 if (rast
->state
.write_zstencil
)
609 lp_rast_store_zstencil(rast
, thread_index
);
614 * Signal on a fence. This is called during bin execution/rasterization.
617 void lp_rast_fence( struct lp_rasterizer
*rast
,
618 unsigned thread_index
,
619 const union lp_rast_cmd_arg arg
)
621 struct lp_fence
*fence
= arg
.fence
;
623 pipe_mutex_lock( fence
->mutex
);
626 assert(fence
->count
<= fence
->rank
);
628 LP_DBG(DEBUG_RAST
, "%s count=%u rank=%u\n", __FUNCTION__
,
629 fence
->count
, fence
->rank
);
631 pipe_condvar_signal( fence
->signalled
);
633 pipe_mutex_unlock( fence
->mutex
);
638 * When all the threads are done rasterizing a scene, one thread will
639 * call this function to reset the scene and put it onto the empty queue.
642 release_scene( struct lp_rasterizer
*rast
,
643 struct lp_scene
*scene
)
645 util_unreference_framebuffer_state( &scene
->fb
);
647 lp_scene_reset( scene
);
648 lp_scene_enqueue( rast
->empty_scenes
, scene
);
649 rast
->curr_scene
= NULL
;
654 * Rasterize commands for a single bin.
655 * \param x, y position of the bin's tile in the framebuffer
656 * Must be called between lp_rast_begin() and lp_rast_end().
660 rasterize_bin( struct lp_rasterizer
*rast
,
661 unsigned thread_index
,
662 const struct cmd_bin
*bin
,
665 const struct cmd_block_list
*commands
= &bin
->commands
;
666 struct cmd_block
*block
;
669 lp_rast_start_tile( rast
, thread_index
, x
, y
);
671 /* simply execute each of the commands in the block list */
672 for (block
= commands
->head
; block
; block
= block
->next
) {
673 for (k
= 0; k
< block
->count
; k
++) {
674 block
->cmd
[k
]( rast
, thread_index
, block
->arg
[k
] );
678 lp_rast_end_tile( rast
, thread_index
);
682 #define RAST(x) { lp_rast_##x, #x }
692 RAST(clear_zstencil
),
700 debug_bin( const struct cmd_bin
*bin
)
702 const struct cmd_block
*head
= bin
->commands
.head
;
705 for (i
= 0; i
< head
->count
; i
++) {
706 debug_printf("%d: ", i
);
707 for (j
= 0; j
< Elements(cmd_names
); j
++) {
708 if (head
->cmd
[i
] == cmd_names
[j
].cmd
) {
709 debug_printf("%s\n", cmd_names
[j
].name
);
713 if (j
== Elements(cmd_names
))
714 debug_printf("...other\n");
719 /* An empty bin is one that just loads the contents of the tile and
720 * stores them again unchanged. This typically happens when bins have
721 * been flushed for some reason in the middle of a frame, or when
722 * incremental updates are being made to a render target.
724 * Try to avoid doing pointless work in this case.
727 is_empty_bin( const struct cmd_bin
*bin
)
729 const struct cmd_block
*head
= bin
->commands
.head
;
735 /* We emit at most two load-tile commands at the start of the first
736 * command block. In addition we seem to emit a couple of
737 * set-state commands even in empty bins.
739 * As a heuristic, if a bin has more than 4 commands, consider it
742 if (head
->next
!= NULL
||
747 for (i
= 0; i
< head
->count
; i
++)
748 if (head
->cmd
[i
] != lp_rast_load_color
&&
749 head
->cmd
[i
] != lp_rast_load_zstencil
&&
750 head
->cmd
[i
] != lp_rast_set_state
) {
760 * Rasterize/execute all bins within a scene.
764 rasterize_scene( struct lp_rasterizer
*rast
,
765 unsigned thread_index
,
766 struct lp_scene
*scene
,
769 /* loop over scene bins, rasterize each */
773 for (i
= 0; i
< scene
->tiles_x
; i
++) {
774 for (j
= 0; j
< scene
->tiles_y
; j
++) {
775 struct cmd_bin
*bin
= lp_get_bin(scene
, i
, j
);
776 rasterize_bin( rast
, thread_index
,
777 bin
, i
* TILE_SIZE
, j
* TILE_SIZE
);
787 while ((bin
= lp_scene_bin_iter_next(scene
, &x
, &y
))) {
788 if (!is_empty_bin( bin
))
789 rasterize_bin( rast
, thread_index
, bin
, x
* TILE_SIZE
, y
* TILE_SIZE
);
797 * Called by setup module when it has something for us to render.
800 lp_rasterize_scene( struct lp_rasterizer
*rast
,
801 struct lp_scene
*scene
,
802 const struct pipe_framebuffer_state
*fb
,
805 boolean debug
= false;
807 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
811 debug_printf("rasterize scene:\n");
812 debug_printf(" data size: %u\n", lp_scene_data_size(scene
));
813 for (y
= 0; y
< scene
->tiles_y
; y
++) {
814 for (x
= 0; x
< scene
->tiles_x
; x
++) {
815 debug_printf(" bin %u, %u size: %u\n", x
, y
,
816 lp_scene_bin_size(scene
, x
, y
));
821 /* save framebuffer state in the bin */
822 util_copy_framebuffer_state(&scene
->fb
, fb
);
823 scene
->write_depth
= write_depth
;
825 if (rast
->num_threads
== 0) {
828 lp_rast_begin( rast
, fb
,
829 fb
->nr_cbufs
!= 0, /* always write color if cbufs present */
830 fb
->zsbuf
!= NULL
&& write_depth
);
832 lp_scene_bin_iter_begin( scene
);
833 rasterize_scene( rast
, 0, scene
, write_depth
);
835 release_scene( rast
, scene
);
840 /* threaded rendering! */
843 lp_scene_enqueue( rast
->full_scenes
, scene
);
845 /* signal the threads that there's work to do */
846 for (i
= 0; i
< rast
->num_threads
; i
++) {
847 pipe_semaphore_signal(&rast
->tasks
[i
].work_ready
);
850 /* wait for work to complete */
851 for (i
= 0; i
< rast
->num_threads
; i
++) {
852 pipe_semaphore_wait(&rast
->tasks
[i
].work_done
);
856 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
861 * This is the thread's main entrypoint.
862 * It's a simple loop:
865 * 3. signal that we're done
867 static PIPE_THREAD_ROUTINE( thread_func
, init_data
)
869 struct lp_rasterizer_task
*task
= (struct lp_rasterizer_task
*) init_data
;
870 struct lp_rasterizer
*rast
= task
->rast
;
871 boolean debug
= false;
876 debug_printf("thread %d waiting for work\n", task
->thread_index
);
877 pipe_semaphore_wait(&task
->work_ready
);
879 if (task
->thread_index
== 0) {
881 * - get next scene to rasterize
882 * - map the framebuffer surfaces
884 const struct pipe_framebuffer_state
*fb
;
887 rast
->curr_scene
= lp_scene_dequeue( rast
->full_scenes
, TRUE
);
889 lp_scene_bin_iter_begin( rast
->curr_scene
);
891 fb
= &rast
->curr_scene
->fb
;
892 write_depth
= rast
->curr_scene
->write_depth
;
894 lp_rast_begin( rast
, fb
,
896 fb
->zsbuf
!= NULL
&& write_depth
);
899 /* Wait for all threads to get here so that threads[1+] don't
900 * get a null rast->curr_scene pointer.
902 pipe_barrier_wait( &rast
->barrier
);
906 debug_printf("thread %d doing work\n", task
->thread_index
);
907 rasterize_scene(rast
,
910 rast
->curr_scene
->write_depth
);
912 /* wait for all threads to finish with this scene */
913 pipe_barrier_wait( &rast
->barrier
);
915 if (task
->thread_index
== 0) {
917 * - release the scene object
918 * - unmap the framebuffer surfaces
920 release_scene( rast
, rast
->curr_scene
);
924 /* signal done with work */
926 debug_printf("thread %d done working\n", task
->thread_index
);
927 pipe_semaphore_signal(&task
->work_done
);
935 * Initialize semaphores and spawn the threads.
938 create_rast_threads(struct lp_rasterizer
*rast
)
942 #ifdef PIPE_OS_WINDOWS
943 /* Multithreading not supported on windows until conditions and barriers are
944 * properly implemented. */
945 rast
->num_threads
= 0;
947 rast
->num_threads
= util_cpu_caps
.nr_cpus
;
948 rast
->num_threads
= debug_get_num_option("LP_NUM_THREADS", rast
->num_threads
);
949 rast
->num_threads
= MIN2(rast
->num_threads
, MAX_THREADS
);
952 /* NOTE: if num_threads is zero, we won't use any threads */
953 for (i
= 0; i
< rast
->num_threads
; i
++) {
954 pipe_semaphore_init(&rast
->tasks
[i
].work_ready
, 0);
955 pipe_semaphore_init(&rast
->tasks
[i
].work_done
, 0);
956 rast
->threads
[i
] = pipe_thread_create(thread_func
,
957 (void *) &rast
->tasks
[i
]);
964 * Create new lp_rasterizer.
965 * \param empty the queue to put empty scenes on after we've finished
968 struct lp_rasterizer
*
969 lp_rast_create( struct pipe_screen
*screen
, struct lp_scene_queue
*empty
)
971 struct lp_rasterizer
*rast
;
974 rast
= CALLOC_STRUCT(lp_rasterizer
);
978 rast
->screen
= screen
;
980 rast
->empty_scenes
= empty
;
981 rast
->full_scenes
= lp_scene_queue_create();
983 for (i
= 0; i
< Elements(rast
->tasks
); i
++) {
984 struct lp_rasterizer_task
*task
= &rast
->tasks
[i
];
986 for (cbuf
= 0; cbuf
< PIPE_MAX_COLOR_BUFS
; cbuf
++ )
987 task
->tile
.color
[cbuf
] = align_malloc(TILE_SIZE
* TILE_SIZE
* 4, 16);
989 task
->tile
.depth
= align_malloc(TILE_SIZE
* TILE_SIZE
* 4, 16);
991 task
->thread_index
= i
;
994 create_rast_threads(rast
);
996 /* for synchronizing rasterization threads */
997 pipe_barrier_init( &rast
->barrier
, rast
->num_threads
);
1005 void lp_rast_destroy( struct lp_rasterizer
*rast
)
1009 util_unreference_framebuffer_state(&rast
->state
.fb
);
1011 for (i
= 0; i
< Elements(rast
->tasks
); i
++) {
1012 align_free(rast
->tasks
[i
].tile
.depth
);
1013 for (cbuf
= 0; cbuf
< PIPE_MAX_COLOR_BUFS
; cbuf
++ )
1014 align_free(rast
->tasks
[i
].tile
.color
[cbuf
]);
1017 /* for synchronizing rasterization threads */
1018 pipe_barrier_destroy( &rast
->barrier
);
1024 /** Return number of rasterization threads */
1026 lp_rast_get_num_threads( struct lp_rasterizer
*rast
)
1028 return rast
->num_threads
;