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_surface.h"
33 #include "lp_scene_queue.h"
39 #include "lp_rast_priv.h"
40 #include "lp_tile_soa.h"
41 #include "gallivm/lp_bld_debug.h"
46 * Begin rasterizing a scene.
47 * Called once per scene by one thread.
50 lp_rast_begin( struct lp_rasterizer
*rast
,
51 struct lp_scene
*scene
)
53 const struct pipe_framebuffer_state
*fb
= &scene
->fb
;
56 rast
->curr_scene
= scene
;
58 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
60 rast
->state
.nr_cbufs
= scene
->fb
.nr_cbufs
;
62 for (i
= 0; i
< rast
->state
.nr_cbufs
; i
++) {
63 struct pipe_surface
*cbuf
= scene
->fb
.cbufs
[i
];
64 rast
->cbuf
[i
].format
= cbuf
->texture
->format
;
65 rast
->cbuf
[i
].tiles_per_row
= align(cbuf
->width
, TILE_SIZE
) / TILE_SIZE
;
66 rast
->cbuf
[i
].blocksize
=
67 util_format_get_blocksize(cbuf
->texture
->format
);
68 rast
->cbuf
[i
].map
= llvmpipe_resource_map(cbuf
->texture
,
72 LP_TEX_USAGE_READ_WRITE
,
77 struct pipe_surface
*zsbuf
= scene
->fb
.zsbuf
;
78 rast
->zsbuf
.stride
= llvmpipe_resource_stride(zsbuf
->texture
, zsbuf
->level
);
79 rast
->zsbuf
.blocksize
=
80 util_format_get_blocksize(zsbuf
->texture
->format
);
82 rast
->zsbuf
.map
= llvmpipe_resource_map(zsbuf
->texture
,
86 LP_TEX_USAGE_READ_WRITE
,
88 assert(rast
->zsbuf
.map
);
91 lp_scene_bin_iter_begin( scene
);
96 lp_rast_end( struct lp_rasterizer
*rast
)
98 struct lp_scene
*scene
= rast
->curr_scene
;
101 /* Unmap color buffers */
102 for (i
= 0; i
< rast
->state
.nr_cbufs
; i
++) {
103 struct pipe_surface
*cbuf
= scene
->fb
.cbufs
[i
];
104 llvmpipe_resource_unmap(cbuf
->texture
,
108 rast
->cbuf
[i
].map
= NULL
;
111 /* Unmap z/stencil buffer */
112 if (rast
->zsbuf
.map
) {
113 struct pipe_surface
*zsbuf
= scene
->fb
.zsbuf
;
114 llvmpipe_resource_unmap(zsbuf
->texture
,
118 rast
->zsbuf
.map
= NULL
;
121 lp_scene_reset( rast
->curr_scene
);
123 rast
->curr_scene
= NULL
;
127 debug_printf("Post render scene: tile unswizzle: %u tile swizzle: %u\n",
128 lp_tile_unswizzle_count
, lp_tile_swizzle_count
);
134 * Begining rasterization of a tile.
135 * \param x window X position of the tile, in pixels
136 * \param y window Y position of the tile, in pixels
139 lp_rast_tile_begin(struct lp_rasterizer_task
*task
,
140 unsigned x
, unsigned y
)
142 struct lp_rasterizer
*rast
= task
->rast
;
143 struct lp_scene
*scene
= rast
->curr_scene
;
144 enum lp_texture_usage usage
;
147 LP_DBG(DEBUG_RAST
, "%s %d,%d\n", __FUNCTION__
, x
, y
);
149 assert(x
% TILE_SIZE
== 0);
150 assert(y
% TILE_SIZE
== 0);
155 if (scene
->has_color_clear
)
156 usage
= LP_TEX_USAGE_WRITE_ALL
;
158 usage
= LP_TEX_USAGE_READ_WRITE
;
160 /* get pointers to color tile(s) */
161 for (buf
= 0; buf
< rast
->state
.nr_cbufs
; buf
++) {
162 struct pipe_surface
*cbuf
= rast
->curr_scene
->fb
.cbufs
[buf
];
163 struct llvmpipe_resource
*lpt
;
165 lpt
= llvmpipe_resource(cbuf
->texture
);
166 task
->color_tiles
[buf
] = llvmpipe_get_texture_tile(lpt
,
167 cbuf
->face
+ cbuf
->zslice
,
171 assert(task
->color_tiles
[buf
]);
174 /* get pointer to depth/stencil tile */
176 struct pipe_surface
*zsbuf
= rast
->curr_scene
->fb
.zsbuf
;
178 struct llvmpipe_resource
*lpt
= llvmpipe_resource(zsbuf
->texture
);
180 if (scene
->has_depth_clear
)
181 usage
= LP_TEX_USAGE_WRITE_ALL
;
183 usage
= LP_TEX_USAGE_READ_WRITE
;
185 /* "prime" the tile: convert data from linear to tiled if necessary
186 * and update the tile's layout info.
188 (void) llvmpipe_get_texture_tile(lpt
,
189 zsbuf
->face
+ zsbuf
->zslice
,
193 /* Get actual pointer to the tile data. Note that depth/stencil
194 * data is tiled differently than color data.
196 task
->depth_tile
= lp_rast_get_depth_block_pointer(rast
, x
, y
);
198 assert(task
->depth_tile
);
201 task
->depth_tile
= NULL
;
208 * Clear the rasterizer's current color tile.
209 * This is a bin command called during bin processing.
212 lp_rast_clear_color(struct lp_rasterizer_task
*task
,
213 const union lp_rast_cmd_arg arg
)
215 struct lp_rasterizer
*rast
= task
->rast
;
216 const uint8_t *clear_color
= arg
.clear_color
;
220 LP_DBG(DEBUG_RAST
, "%s 0x%x,0x%x,0x%x,0x%x\n", __FUNCTION__
,
226 if (clear_color
[0] == clear_color
[1] &&
227 clear_color
[1] == clear_color
[2] &&
228 clear_color
[2] == clear_color
[3]) {
229 /* clear to grayscale value {x, x, x, x} */
230 for (i
= 0; i
< rast
->state
.nr_cbufs
; i
++) {
231 uint8_t *ptr
= task
->color_tiles
[i
];
232 memset(ptr
, clear_color
[0], TILE_SIZE
* TILE_SIZE
* 4);
237 * Note: if the swizzled tile layout changes (see TILE_PIXEL) this code
238 * will need to change. It'll be pretty obvious when clearing no longer
241 const unsigned chunk
= TILE_SIZE
/ 4;
242 for (i
= 0; i
< rast
->state
.nr_cbufs
; i
++) {
243 uint8_t *c
= task
->color_tiles
[i
];
246 for (j
= 0; j
< 4 * TILE_SIZE
; j
++) {
247 memset(c
, clear_color
[0], chunk
);
249 memset(c
, clear_color
[1], chunk
);
251 memset(c
, clear_color
[2], chunk
);
253 memset(c
, clear_color
[3], chunk
);
259 LP_COUNT(nr_color_tile_clear
);
264 * Clear the rasterizer's current z/stencil tile.
265 * This is a bin command called during bin processing.
268 lp_rast_clear_zstencil(struct lp_rasterizer_task
*task
,
269 const union lp_rast_cmd_arg arg
)
271 struct lp_rasterizer
*rast
= task
->rast
;
272 const unsigned height
= TILE_SIZE
/ TILE_VECTOR_HEIGHT
;
273 const unsigned width
= TILE_SIZE
* TILE_VECTOR_HEIGHT
;
274 const unsigned block_size
= rast
->zsbuf
.blocksize
;
275 const unsigned dst_stride
= rast
->zsbuf
.stride
* TILE_VECTOR_HEIGHT
;
279 LP_DBG(DEBUG_RAST
, "%s 0x%x\n", __FUNCTION__
, arg
.clear_zstencil
);
282 * Clear the aera of the swizzled depth/depth buffer matching this tile, in
283 * stripes of TILE_VECTOR_HEIGHT x TILE_SIZE at a time.
285 * The swizzled depth format is such that the depths for
286 * TILE_VECTOR_HEIGHT x TILE_VECTOR_WIDTH pixels have consecutive offsets.
289 dst
= task
->depth_tile
;
291 assert(dst
== lp_rast_get_depth_block_pointer(rast
, task
->x
, task
->y
));
293 switch (block_size
) {
295 memset(dst
, (uint8_t) arg
.clear_zstencil
, height
* width
);
298 for (i
= 0; i
< height
; i
++) {
299 uint16_t *row
= (uint16_t *)dst
;
300 for (j
= 0; j
< width
; j
++)
301 *row
++ = (uint16_t) arg
.clear_zstencil
;
306 for (i
= 0; i
< height
; i
++) {
307 uint32_t *row
= (uint32_t *)dst
;
308 for (j
= 0; j
< width
; j
++)
309 *row
++ = arg
.clear_zstencil
;
321 * Load tile color from the framebuffer surface.
322 * This is a bin command called during bin processing.
326 lp_rast_load_color(struct lp_rasterizer_task
*task
,
327 const union lp_rast_cmd_arg arg
)
329 struct lp_rasterizer
*rast
= task
->rast
;
331 enum lp_texture_usage usage
;
333 LP_DBG(DEBUG_RAST
, "%s at %u, %u\n", __FUNCTION__
, x
, y
);
335 if (scene
->has_color_clear
)
336 usage
= LP_TEX_USAGE_WRITE_ALL
;
338 usage
= LP_TEX_USAGE_READ_WRITE
;
340 /* Get pointers to color tile(s).
341 * This will convert linear data to tiled if needed.
343 for (buf
= 0; buf
< rast
->state
.nr_cbufs
; buf
++) {
344 struct pipe_surface
*cbuf
= rast
->curr_scene
->fb
.cbufs
[buf
];
345 struct llvmpipe_texture
*lpt
;
347 lpt
= llvmpipe_texture(cbuf
->texture
);
348 task
->color_tiles
[buf
] = llvmpipe_get_texture_tile(lpt
,
349 cbuf
->face
+ cbuf
->zslice
,
353 assert(task
->color_tiles
[buf
]);
360 * Convert the color tile from tiled to linear layout.
361 * This is generally only done when we're flushing the scene just prior to
362 * SwapBuffers. If we didn't do this here, we'd have to convert the entire
363 * tiled color buffer to linear layout in the llvmpipe_texture_unmap()
364 * function. It's better to do it here to take advantage of
365 * threading/parallelism.
366 * This is a bin command which is stored in all bins.
369 lp_rast_store_color( struct lp_rasterizer_task
*task
,
370 const union lp_rast_cmd_arg arg
)
372 struct lp_rasterizer
*rast
= task
->rast
;
373 struct lp_scene
*scene
= rast
->curr_scene
;
376 for (buf
= 0; buf
< rast
->state
.nr_cbufs
; buf
++) {
377 struct pipe_surface
*cbuf
= scene
->fb
.cbufs
[buf
];
378 const unsigned face
= cbuf
->face
, level
= cbuf
->level
;
379 struct llvmpipe_resource
*lpt
= llvmpipe_resource(cbuf
->texture
);
380 /* this will convert the tiled data to linear if needed */
381 (void) llvmpipe_get_texture_tile_linear(lpt
, face
, level
,
389 * This is a bin command called during bin processing.
392 lp_rast_set_state(struct lp_rasterizer_task
*task
,
393 const union lp_rast_cmd_arg arg
)
395 const struct lp_rast_state
*state
= arg
.set_state
;
397 LP_DBG(DEBUG_RAST
, "%s %p\n", __FUNCTION__
, (void *) state
);
399 /* just set the current state pointer for this rasterizer */
400 task
->current_state
= state
;
405 * Run the shader on all blocks in a tile. This is used when a tile is
406 * completely contained inside a triangle.
407 * This is a bin command called during bin processing.
410 lp_rast_shade_tile(struct lp_rasterizer_task
*task
,
411 const union lp_rast_cmd_arg arg
)
413 struct lp_rasterizer
*rast
= task
->rast
;
414 const struct lp_rast_state
*state
= task
->current_state
;
415 const struct lp_rast_shader_inputs
*inputs
= arg
.shade_tile
;
416 struct lp_fragment_shader_variant
*variant
= state
->variant
;
417 const unsigned tile_x
= task
->x
, tile_y
= task
->y
;
420 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
422 /* render the whole 64x64 tile in 4x4 chunks */
423 for (y
= 0; y
< TILE_SIZE
; y
+= 4){
424 for (x
= 0; x
< TILE_SIZE
; x
+= 4) {
425 uint8_t *color
[PIPE_MAX_COLOR_BUFS
];
430 for (i
= 0; i
< rast
->state
.nr_cbufs
; i
++)
431 color
[i
] = lp_rast_get_color_block_pointer(task
, i
,
432 tile_x
+ x
, tile_y
+ y
);
435 depth
= lp_rast_get_depth_block_pointer(rast
, tile_x
+ x
, tile_y
+ y
);
437 /* run shader on 4x4 block */
438 variant
->jit_function
[RAST_WHOLE
]( &state
->jit_context
,
439 tile_x
+ x
, tile_y
+ y
,
446 INT_MIN
, INT_MIN
, INT_MIN
,
447 NULL
, NULL
, NULL
, &task
->vis_counter
);
454 * Compute shading for a 4x4 block of pixels.
455 * This is a bin command called during bin processing.
456 * \param x X position of quad in window coords
457 * \param y Y position of quad in window coords
459 void lp_rast_shade_quads( struct lp_rasterizer_task
*task
,
460 const struct lp_rast_shader_inputs
*inputs
,
461 unsigned x
, unsigned y
,
462 int32_t c1
, int32_t c2
, int32_t c3
)
464 const struct lp_rast_state
*state
= task
->current_state
;
465 struct lp_fragment_shader_variant
*variant
= state
->variant
;
466 struct lp_rasterizer
*rast
= task
->rast
;
467 uint8_t *color
[PIPE_MAX_COLOR_BUFS
];
474 assert(x
% TILE_VECTOR_WIDTH
== 0);
475 assert(y
% TILE_VECTOR_HEIGHT
== 0);
477 assert((x
% 4) == 0);
478 assert((y
% 4) == 0);
481 for (i
= 0; i
< rast
->state
.nr_cbufs
; i
++) {
482 color
[i
] = lp_rast_get_color_block_pointer(task
, i
, x
, y
);
483 assert(lp_check_alignment(color
[i
], 16));
487 depth
= lp_rast_get_depth_block_pointer(rast
, x
, y
);
490 assert(lp_check_alignment(state
->jit_context
.blend_color
, 16));
492 assert(lp_check_alignment(inputs
->step
[0], 16));
493 assert(lp_check_alignment(inputs
->step
[1], 16));
494 assert(lp_check_alignment(inputs
->step
[2], 16));
496 /* run shader on 4x4 block */
497 variant
->jit_function
[RAST_EDGE_TEST
]( &state
->jit_context
,
514 * Set top row and left column of the tile's pixels to white. For debugging.
517 outline_tile(uint8_t *tile
)
519 const uint8_t val
= 0xff;
522 for (i
= 0; i
< TILE_SIZE
; i
++) {
523 TILE_PIXEL(tile
, i
, 0, 0) = val
;
524 TILE_PIXEL(tile
, i
, 0, 1) = val
;
525 TILE_PIXEL(tile
, i
, 0, 2) = val
;
526 TILE_PIXEL(tile
, i
, 0, 3) = val
;
528 TILE_PIXEL(tile
, 0, i
, 0) = val
;
529 TILE_PIXEL(tile
, 0, i
, 1) = val
;
530 TILE_PIXEL(tile
, 0, i
, 2) = val
;
531 TILE_PIXEL(tile
, 0, i
, 3) = val
;
537 * Draw grid of gray lines at 16-pixel intervals across the tile to
538 * show the sub-tile boundaries. For debugging.
541 outline_subtiles(uint8_t *tile
)
543 const uint8_t val
= 0x80;
544 const unsigned step
= 16;
547 for (i
= 0; i
< TILE_SIZE
; i
+= step
) {
548 for (j
= 0; j
< TILE_SIZE
; j
++) {
549 TILE_PIXEL(tile
, i
, j
, 0) = val
;
550 TILE_PIXEL(tile
, i
, j
, 1) = val
;
551 TILE_PIXEL(tile
, i
, j
, 2) = val
;
552 TILE_PIXEL(tile
, i
, j
, 3) = val
;
554 TILE_PIXEL(tile
, j
, i
, 0) = val
;
555 TILE_PIXEL(tile
, j
, i
, 1) = val
;
556 TILE_PIXEL(tile
, j
, i
, 2) = val
;
557 TILE_PIXEL(tile
, j
, i
, 3) = val
;
567 * Called when we're done writing to a color tile.
570 lp_rast_tile_end(struct lp_rasterizer_task
*task
)
573 if (LP_DEBUG
& (DEBUG_SHOW_SUBTILES
| DEBUG_SHOW_TILES
)) {
574 struct lp_rasterizer
*rast
= task
->rast
;
577 for (buf
= 0; buf
< rast
->state
.nr_cbufs
; buf
++) {
578 uint8_t *color
= lp_rast_get_color_block_pointer(task
, buf
,
581 if (LP_DEBUG
& DEBUG_SHOW_SUBTILES
)
582 outline_subtiles(color
);
583 else if (LP_DEBUG
& DEBUG_SHOW_TILES
)
588 (void) outline_subtiles
;
592 memset(task
->color_tiles
, 0, sizeof(task
->color_tiles
));
593 task
->depth_tile
= NULL
;
599 * Signal on a fence. This is called during bin execution/rasterization.
603 lp_rast_fence(struct lp_rasterizer_task
*task
,
604 const union lp_rast_cmd_arg arg
)
606 struct lp_fence
*fence
= arg
.fence
;
607 lp_fence_signal(fence
);
612 * Begin a new occlusion query.
613 * This is a bin command put in all bins.
617 lp_rast_begin_query(struct lp_rasterizer_task
*task
,
618 const union lp_rast_cmd_arg arg
)
620 /* Reset the the per-task counter */
621 task
->vis_counter
= 0;
626 * End the current occlusion query.
627 * This is a bin command put in all bins.
631 lp_rast_end_query(struct lp_rasterizer_task
*task
,
632 const union lp_rast_cmd_arg arg
)
634 struct llvmpipe_query
*pq
= arg
.query_obj
;
636 pipe_mutex_lock(pq
->mutex
);
638 /* Accumulate the visible fragment counter from this tile in
641 pq
->count
[task
->thread_index
] += task
->vis_counter
;
643 /* check if this is the last tile in the scene */
645 if (pq
->tile_count
== pq
->num_tiles
) {
648 /* sum the per-thread counters for the query */
650 for (i
= 0; i
< LP_MAX_THREADS
; i
++) {
651 pq
->result
+= pq
->count
[i
];
654 /* reset counters (in case this query is re-used in the scene) */
655 memset(pq
->count
, 0, sizeof(pq
->count
));
662 pipe_mutex_unlock(pq
->mutex
);
668 * Rasterize commands for a single bin.
669 * \param x, y position of the bin's tile in the framebuffer
670 * Must be called between lp_rast_begin() and lp_rast_end().
674 rasterize_bin(struct lp_rasterizer_task
*task
,
675 const struct cmd_bin
*bin
,
678 const struct cmd_block_list
*commands
= &bin
->commands
;
679 struct cmd_block
*block
;
682 lp_rast_tile_begin( task
, x
* TILE_SIZE
, y
* TILE_SIZE
);
684 /* simply execute each of the commands in the block list */
685 for (block
= commands
->head
; block
; block
= block
->next
) {
686 for (k
= 0; k
< block
->count
; k
++) {
687 block
->cmd
[k
]( task
, block
->arg
[k
] );
691 lp_rast_tile_end(task
);
693 /* Free data for this bin.
695 lp_scene_bin_reset( task
->rast
->curr_scene
, x
, y
);
699 #define RAST(x) { lp_rast_##x, #x }
707 RAST(clear_zstencil
),
718 debug_bin( const struct cmd_bin
*bin
)
720 const struct cmd_block
*head
= bin
->commands
.head
;
723 for (i
= 0; i
< head
->count
; i
++) {
724 debug_printf("%d: ", i
);
725 for (j
= 0; j
< Elements(cmd_names
); j
++) {
726 if (head
->cmd
[i
] == cmd_names
[j
].cmd
) {
727 debug_printf("%s\n", cmd_names
[j
].name
);
731 if (j
== Elements(cmd_names
))
732 debug_printf("...other\n");
737 /* An empty bin is one that just loads the contents of the tile and
738 * stores them again unchanged. This typically happens when bins have
739 * been flushed for some reason in the middle of a frame, or when
740 * incremental updates are being made to a render target.
742 * Try to avoid doing pointless work in this case.
745 is_empty_bin( const struct cmd_bin
*bin
)
747 const struct cmd_block
*head
= bin
->commands
.head
;
753 /* We emit at most two load-tile commands at the start of the first
754 * command block. In addition we seem to emit a couple of
755 * set-state commands even in empty bins.
757 * As a heuristic, if a bin has more than 4 commands, consider it
760 if (head
->next
!= NULL
||
765 for (i
= 0; i
< head
->count
; i
++)
766 if (head
->cmd
[i
] != lp_rast_set_state
) {
776 * Rasterize/execute all bins within a scene.
780 rasterize_scene(struct lp_rasterizer_task
*task
,
781 struct lp_scene
*scene
)
783 /* loop over scene bins, rasterize each */
787 for (i
= 0; i
< scene
->tiles_x
; i
++) {
788 for (j
= 0; j
< scene
->tiles_y
; j
++) {
789 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, i
, j
);
790 rasterize_bin(task
, bin
, i
, j
);
800 while ((bin
= lp_scene_bin_iter_next(scene
, &x
, &y
))) {
801 if (!is_empty_bin( bin
))
802 rasterize_bin(task
, bin
, x
, y
);
810 * Called by setup module when it has something for us to render.
813 lp_rast_queue_scene( struct lp_rasterizer
*rast
,
814 struct lp_scene
*scene
)
816 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
818 if (rast
->num_threads
== 0) {
821 lp_rast_begin( rast
, scene
);
823 rasterize_scene( &rast
->tasks
[0], scene
);
825 lp_scene_reset( scene
);
829 rast
->curr_scene
= NULL
;
832 /* threaded rendering! */
835 lp_scene_enqueue( rast
->full_scenes
, scene
);
837 /* signal the threads that there's work to do */
838 for (i
= 0; i
< rast
->num_threads
; i
++) {
839 pipe_semaphore_signal(&rast
->tasks
[i
].work_ready
);
843 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
848 lp_rast_finish( struct lp_rasterizer
*rast
)
850 if (rast
->num_threads
== 0) {
856 /* wait for work to complete */
857 for (i
= 0; i
< rast
->num_threads
; i
++) {
858 pipe_semaphore_wait(&rast
->tasks
[i
].work_done
);
865 * This is the thread's main entrypoint.
866 * It's a simple loop:
869 * 3. signal that we're done
871 static PIPE_THREAD_ROUTINE( thread_func
, init_data
)
873 struct lp_rasterizer_task
*task
= (struct lp_rasterizer_task
*) init_data
;
874 struct lp_rasterizer
*rast
= task
->rast
;
875 boolean debug
= false;
880 debug_printf("thread %d waiting for work\n", task
->thread_index
);
881 pipe_semaphore_wait(&task
->work_ready
);
886 if (task
->thread_index
== 0) {
888 * - get next scene to rasterize
889 * - map the framebuffer surfaces
892 lp_scene_dequeue( rast
->full_scenes
, TRUE
) );
895 /* Wait for all threads to get here so that threads[1+] don't
896 * get a null rast->curr_scene pointer.
898 pipe_barrier_wait( &rast
->barrier
);
902 debug_printf("thread %d doing work\n", task
->thread_index
);
904 rasterize_scene(task
,
907 /* wait for all threads to finish with this scene */
908 pipe_barrier_wait( &rast
->barrier
);
910 /* XXX: shouldn't be necessary:
912 if (task
->thread_index
== 0) {
916 /* signal done with work */
918 debug_printf("thread %d done working\n", task
->thread_index
);
920 pipe_semaphore_signal(&task
->work_done
);
928 * Initialize semaphores and spawn the threads.
931 create_rast_threads(struct lp_rasterizer
*rast
)
935 /* NOTE: if num_threads is zero, we won't use any threads */
936 for (i
= 0; i
< rast
->num_threads
; i
++) {
937 pipe_semaphore_init(&rast
->tasks
[i
].work_ready
, 0);
938 pipe_semaphore_init(&rast
->tasks
[i
].work_done
, 0);
939 rast
->threads
[i
] = pipe_thread_create(thread_func
,
940 (void *) &rast
->tasks
[i
]);
947 * Create new lp_rasterizer. If num_threads is zero, don't create any
948 * new threads, do rendering synchronously.
949 * \param num_threads number of rasterizer threads to create
951 struct lp_rasterizer
*
952 lp_rast_create( unsigned num_threads
)
954 struct lp_rasterizer
*rast
;
957 rast
= CALLOC_STRUCT(lp_rasterizer
);
961 rast
->full_scenes
= lp_scene_queue_create();
963 for (i
= 0; i
< Elements(rast
->tasks
); i
++) {
964 struct lp_rasterizer_task
*task
= &rast
->tasks
[i
];
966 task
->thread_index
= i
;
969 rast
->num_threads
= num_threads
;
971 create_rast_threads(rast
);
973 /* for synchronizing rasterization threads */
974 pipe_barrier_init( &rast
->barrier
, rast
->num_threads
);
982 void lp_rast_destroy( struct lp_rasterizer
*rast
)
986 /* Set exit_flag and signal each thread's work_ready semaphore.
987 * Each thread will be woken up, notice that the exit_flag is set and
988 * break out of its main loop. The thread will then exit.
990 rast
->exit_flag
= TRUE
;
991 for (i
= 0; i
< rast
->num_threads
; i
++) {
992 pipe_semaphore_signal(&rast
->tasks
[i
].work_ready
);
995 /* Wait for threads to terminate before cleaning up per-thread data */
996 for (i
= 0; i
< rast
->num_threads
; i
++) {
997 pipe_thread_wait(rast
->threads
[i
]);
1000 /* Clean up per-thread data */
1001 for (i
= 0; i
< rast
->num_threads
; i
++) {
1002 pipe_semaphore_destroy(&rast
->tasks
[i
].work_ready
);
1003 pipe_semaphore_destroy(&rast
->tasks
[i
].work_done
);
1006 /* for synchronizing rasterization threads */
1007 pipe_barrier_destroy( &rast
->barrier
);
1009 lp_scene_queue_destroy(rast
->full_scenes
);
1015 /** Return number of rasterization threads */
1017 lp_rast_get_num_threads( struct lp_rasterizer
*rast
)
1019 return rast
->num_threads
;