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_rect.h"
32 #include "util/u_surface.h"
34 #include "lp_scene_queue.h"
40 #include "lp_rast_priv.h"
41 #include "lp_tile_soa.h"
42 #include "gallivm/lp_bld_debug.h"
47 * Begin rasterizing a scene.
48 * Called once per scene by one thread.
51 lp_rast_begin( struct lp_rasterizer
*rast
,
52 struct lp_scene
*scene
)
54 const struct pipe_framebuffer_state
*fb
= &scene
->fb
;
57 rast
->curr_scene
= scene
;
59 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
61 rast
->state
.nr_cbufs
= scene
->fb
.nr_cbufs
;
63 for (i
= 0; i
< rast
->state
.nr_cbufs
; i
++) {
64 struct pipe_surface
*cbuf
= scene
->fb
.cbufs
[i
];
65 llvmpipe_resource_map(cbuf
->texture
,
69 LP_TEX_USAGE_READ_WRITE
,
74 struct pipe_surface
*zsbuf
= scene
->fb
.zsbuf
;
75 rast
->zsbuf
.stride
= llvmpipe_resource_stride(zsbuf
->texture
, zsbuf
->level
);
76 rast
->zsbuf
.blocksize
=
77 util_format_get_blocksize(zsbuf
->texture
->format
);
79 rast
->zsbuf
.map
= llvmpipe_resource_map(zsbuf
->texture
,
83 LP_TEX_USAGE_READ_WRITE
,
87 lp_scene_bin_iter_begin( scene
);
92 lp_rast_end( struct lp_rasterizer
*rast
)
94 struct lp_scene
*scene
= rast
->curr_scene
;
97 /* Unmap color buffers */
98 for (i
= 0; i
< rast
->state
.nr_cbufs
; i
++) {
99 struct pipe_surface
*cbuf
= scene
->fb
.cbufs
[i
];
100 llvmpipe_resource_unmap(cbuf
->texture
,
106 /* Unmap z/stencil buffer */
107 if (rast
->zsbuf
.map
) {
108 struct pipe_surface
*zsbuf
= scene
->fb
.zsbuf
;
109 llvmpipe_resource_unmap(zsbuf
->texture
,
113 rast
->zsbuf
.map
= NULL
;
116 lp_scene_reset( rast
->curr_scene
);
118 rast
->curr_scene
= NULL
;
122 debug_printf("Post render scene: tile unswizzle: %u tile swizzle: %u\n",
123 lp_tile_unswizzle_count
, lp_tile_swizzle_count
);
129 * Begining rasterization of a tile.
130 * \param x window X position of the tile, in pixels
131 * \param y window Y position of the tile, in pixels
134 lp_rast_tile_begin(struct lp_rasterizer_task
*task
,
135 unsigned x
, unsigned y
)
137 struct lp_rasterizer
*rast
= task
->rast
;
138 struct lp_scene
*scene
= rast
->curr_scene
;
139 enum lp_texture_usage usage
;
141 LP_DBG(DEBUG_RAST
, "%s %d,%d\n", __FUNCTION__
, x
, y
);
143 assert(x
% TILE_SIZE
== 0);
144 assert(y
% TILE_SIZE
== 0);
149 /* reset pointers to color tile(s) */
150 memset(task
->color_tiles
, 0, sizeof(task
->color_tiles
));
152 /* get pointer to depth/stencil tile */
154 struct pipe_surface
*zsbuf
= rast
->curr_scene
->fb
.zsbuf
;
156 struct llvmpipe_resource
*lpt
= llvmpipe_resource(zsbuf
->texture
);
158 if (scene
->has_depthstencil_clear
)
159 usage
= LP_TEX_USAGE_WRITE_ALL
;
161 usage
= LP_TEX_USAGE_READ_WRITE
;
163 /* "prime" the tile: convert data from linear to tiled if necessary
164 * and update the tile's layout info.
166 (void) llvmpipe_get_texture_tile(lpt
,
167 zsbuf
->face
+ zsbuf
->zslice
,
171 /* Get actual pointer to the tile data. Note that depth/stencil
172 * data is tiled differently than color data.
174 task
->depth_tile
= lp_rast_get_depth_block_pointer(task
, x
, y
);
176 assert(task
->depth_tile
);
179 task
->depth_tile
= NULL
;
186 * Clear the rasterizer's current color tile.
187 * This is a bin command called during bin processing.
190 lp_rast_clear_color(struct lp_rasterizer_task
*task
,
191 const union lp_rast_cmd_arg arg
)
193 struct lp_rasterizer
*rast
= task
->rast
;
194 const uint8_t *clear_color
= arg
.clear_color
;
198 LP_DBG(DEBUG_RAST
, "%s 0x%x,0x%x,0x%x,0x%x\n", __FUNCTION__
,
204 if (clear_color
[0] == clear_color
[1] &&
205 clear_color
[1] == clear_color
[2] &&
206 clear_color
[2] == clear_color
[3]) {
207 /* clear to grayscale value {x, x, x, x} */
208 for (i
= 0; i
< rast
->state
.nr_cbufs
; i
++) {
210 lp_rast_get_color_tile_pointer(task
, i
, LP_TEX_USAGE_WRITE_ALL
);
211 memset(ptr
, clear_color
[0], TILE_SIZE
* TILE_SIZE
* 4);
216 * Note: if the swizzled tile layout changes (see TILE_PIXEL) this code
217 * will need to change. It'll be pretty obvious when clearing no longer
220 const unsigned chunk
= TILE_SIZE
/ 4;
221 for (i
= 0; i
< rast
->state
.nr_cbufs
; i
++) {
223 lp_rast_get_color_tile_pointer(task
, i
, LP_TEX_USAGE_WRITE_ALL
);
226 for (j
= 0; j
< 4 * TILE_SIZE
; j
++) {
227 memset(c
, clear_color
[0], chunk
);
229 memset(c
, clear_color
[1], chunk
);
231 memset(c
, clear_color
[2], chunk
);
233 memset(c
, clear_color
[3], chunk
);
239 LP_COUNT(nr_color_tile_clear
);
244 * Clear the rasterizer's current z/stencil tile.
245 * This is a bin command called during bin processing.
248 lp_rast_clear_zstencil(struct lp_rasterizer_task
*task
,
249 const union lp_rast_cmd_arg arg
)
251 struct lp_rasterizer
*rast
= task
->rast
;
252 const struct lp_rast_clearzs
*clearzs
= arg
.clear_zstencil
;
253 unsigned clear_value
= clearzs
->clearzs_value
;
254 unsigned clear_mask
= clearzs
->clearzs_mask
;
255 const unsigned height
= TILE_SIZE
/ TILE_VECTOR_HEIGHT
;
256 const unsigned width
= TILE_SIZE
* TILE_VECTOR_HEIGHT
;
257 const unsigned block_size
= rast
->zsbuf
.blocksize
;
258 const unsigned dst_stride
= rast
->zsbuf
.stride
* TILE_VECTOR_HEIGHT
;
262 LP_DBG(DEBUG_RAST
, "%s 0x%x%x\n", __FUNCTION__
, clear_value
, clear_mask
);
265 * Clear the aera of the swizzled depth/depth buffer matching this tile, in
266 * stripes of TILE_VECTOR_HEIGHT x TILE_SIZE at a time.
268 * The swizzled depth format is such that the depths for
269 * TILE_VECTOR_HEIGHT x TILE_VECTOR_WIDTH pixels have consecutive offsets.
272 dst
= task
->depth_tile
;
274 if (lp_is_dummy_tile(dst
))
277 assert(dst
== lp_rast_get_depth_block_pointer(task
, task
->x
, task
->y
));
279 switch (block_size
) {
281 memset(dst
, (uint8_t) clear_value
, height
* width
);
284 for (i
= 0; i
< height
; i
++) {
285 uint16_t *row
= (uint16_t *)dst
;
286 for (j
= 0; j
< width
; j
++)
287 *row
++ = (uint16_t) clear_value
;
292 if (clear_mask
== 0xffffffff) {
293 for (i
= 0; i
< height
; i
++) {
294 uint32_t *row
= (uint32_t *)dst
;
295 for (j
= 0; j
< width
; j
++)
296 *row
++ = clear_value
;
301 for (i
= 0; i
< height
; i
++) {
302 uint32_t *row
= (uint32_t *)dst
;
303 for (j
= 0; j
< width
; j
++) {
304 uint32_t tmp
= ~clear_mask
& *row
;
305 *row
++ = (clear_value
& clear_mask
) | tmp
;
319 * Load tile color from the framebuffer surface.
320 * This is a bin command called during bin processing.
324 lp_rast_load_color(struct lp_rasterizer_task
*task
,
325 const union lp_rast_cmd_arg arg
)
327 struct lp_rasterizer
*rast
= task
->rast
;
329 enum lp_texture_usage usage
;
331 LP_DBG(DEBUG_RAST
, "%s at %u, %u\n", __FUNCTION__
, x
, y
);
333 if (scene
->has_color_clear
)
334 usage
= LP_TEX_USAGE_WRITE_ALL
;
336 usage
= LP_TEX_USAGE_READ_WRITE
;
338 /* Get pointers to color tile(s).
339 * This will convert linear data to tiled if needed.
341 for (buf
= 0; buf
< rast
->state
.nr_cbufs
; buf
++) {
342 struct pipe_surface
*cbuf
= rast
->curr_scene
->fb
.cbufs
[buf
];
343 struct llvmpipe_texture
*lpt
;
345 lpt
= llvmpipe_texture(cbuf
->texture
);
346 task
->color_tiles
[buf
] = llvmpipe_get_texture_tile(lpt
,
347 cbuf
->face
+ cbuf
->zslice
,
351 assert(task
->color_tiles
[buf
]);
358 * Convert the color tile from tiled to linear layout.
359 * This is generally only done when we're flushing the scene just prior to
360 * SwapBuffers. If we didn't do this here, we'd have to convert the entire
361 * tiled color buffer to linear layout in the llvmpipe_texture_unmap()
362 * function. It's better to do it here to take advantage of
363 * threading/parallelism.
364 * This is a bin command which is stored in all bins.
367 lp_rast_store_linear_color( struct lp_rasterizer_task
*task
,
368 const union lp_rast_cmd_arg arg
)
370 struct lp_rasterizer
*rast
= task
->rast
;
371 struct lp_scene
*scene
= rast
->curr_scene
;
374 for (buf
= 0; buf
< rast
->state
.nr_cbufs
; buf
++) {
375 struct pipe_surface
*cbuf
= scene
->fb
.cbufs
[buf
];
376 const unsigned face
= cbuf
->face
, level
= cbuf
->level
;
377 struct llvmpipe_resource
*lpt
= llvmpipe_resource(cbuf
->texture
);
378 /* this will convert the tiled data to linear if needed */
379 (void) llvmpipe_get_texture_tile_linear(lpt
, face
, level
,
387 * This is a bin command called during bin processing.
390 lp_rast_set_state(struct lp_rasterizer_task
*task
,
391 const union lp_rast_cmd_arg arg
)
393 const struct lp_rast_state
*state
= arg
.set_state
;
395 LP_DBG(DEBUG_RAST
, "%s %p\n", __FUNCTION__
, (void *) state
);
397 /* just set the current state pointer for this rasterizer */
398 task
->current_state
= state
;
403 * Run the shader on all blocks in a tile. This is used when a tile is
404 * completely contained inside a triangle.
405 * This is a bin command called during bin processing.
408 lp_rast_shade_tile(struct lp_rasterizer_task
*task
,
409 const union lp_rast_cmd_arg arg
)
411 struct lp_rasterizer
*rast
= task
->rast
;
412 const struct lp_rast_state
*state
= task
->current_state
;
413 const struct lp_rast_shader_inputs
*inputs
= arg
.shade_tile
;
414 struct lp_fragment_shader_variant
*variant
= state
->variant
;
415 const unsigned tile_x
= task
->x
, tile_y
= task
->y
;
418 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
420 /* render the whole 64x64 tile in 4x4 chunks */
421 for (y
= 0; y
< TILE_SIZE
; y
+= 4){
422 for (x
= 0; x
< TILE_SIZE
; x
+= 4) {
423 uint8_t *color
[PIPE_MAX_COLOR_BUFS
];
428 for (i
= 0; i
< rast
->state
.nr_cbufs
; i
++)
429 color
[i
] = lp_rast_get_color_block_pointer(task
, i
,
430 tile_x
+ x
, tile_y
+ y
);
433 depth
= lp_rast_get_depth_block_pointer(task
, tile_x
+ x
, tile_y
+ y
);
435 /* run shader on 4x4 block */
436 variant
->jit_function
[RAST_WHOLE
]( &state
->jit_context
,
437 tile_x
+ x
, tile_y
+ y
,
452 * Run the shader on all blocks in a tile. This is used when a tile is
453 * completely contained inside a triangle, and the shader is opaque.
454 * This is a bin command called during bin processing.
457 lp_rast_shade_tile_opaque(struct lp_rasterizer_task
*task
,
458 const union lp_rast_cmd_arg arg
)
460 struct lp_rasterizer
*rast
= task
->rast
;
463 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
465 /* this will prevent converting the layout from tiled to linear */
466 for (i
= 0; i
< rast
->state
.nr_cbufs
; i
++) {
467 (void)lp_rast_get_color_tile_pointer(task
, i
, LP_TEX_USAGE_WRITE_ALL
);
470 lp_rast_shade_tile(task
, arg
);
475 * Compute shading for a 4x4 block of pixels inside a triangle.
476 * This is a bin command called during bin processing.
477 * \param x X position of quad in window coords
478 * \param y Y position of quad in window coords
481 lp_rast_shade_quads_mask(struct lp_rasterizer_task
*task
,
482 const struct lp_rast_shader_inputs
*inputs
,
483 unsigned x
, unsigned y
,
486 const struct lp_rast_state
*state
= task
->current_state
;
487 struct lp_fragment_shader_variant
*variant
= state
->variant
;
488 struct lp_rasterizer
*rast
= task
->rast
;
489 uint8_t *color
[PIPE_MAX_COLOR_BUFS
];
496 assert(x
% TILE_VECTOR_WIDTH
== 0);
497 assert(y
% TILE_VECTOR_HEIGHT
== 0);
499 assert((x
% 4) == 0);
500 assert((y
% 4) == 0);
503 for (i
= 0; i
< rast
->state
.nr_cbufs
; i
++) {
504 color
[i
] = lp_rast_get_color_block_pointer(task
, i
, x
, y
);
505 assert(lp_check_alignment(color
[i
], 16));
509 depth
= lp_rast_get_depth_block_pointer(task
, x
, y
);
512 assert(lp_check_alignment(state
->jit_context
.blend_color
, 16));
514 /* run shader on 4x4 block */
515 variant
->jit_function
[RAST_EDGE_TEST
](&state
->jit_context
,
530 * Set top row and left column of the tile's pixels to white. For debugging.
533 outline_tile(uint8_t *tile
)
535 const uint8_t val
= 0xff;
538 for (i
= 0; i
< TILE_SIZE
; i
++) {
539 TILE_PIXEL(tile
, i
, 0, 0) = val
;
540 TILE_PIXEL(tile
, i
, 0, 1) = val
;
541 TILE_PIXEL(tile
, i
, 0, 2) = val
;
542 TILE_PIXEL(tile
, i
, 0, 3) = val
;
544 TILE_PIXEL(tile
, 0, i
, 0) = val
;
545 TILE_PIXEL(tile
, 0, i
, 1) = val
;
546 TILE_PIXEL(tile
, 0, i
, 2) = val
;
547 TILE_PIXEL(tile
, 0, i
, 3) = val
;
553 * Draw grid of gray lines at 16-pixel intervals across the tile to
554 * show the sub-tile boundaries. For debugging.
557 outline_subtiles(uint8_t *tile
)
559 const uint8_t val
= 0x80;
560 const unsigned step
= 16;
563 for (i
= 0; i
< TILE_SIZE
; i
+= step
) {
564 for (j
= 0; j
< TILE_SIZE
; j
++) {
565 TILE_PIXEL(tile
, i
, j
, 0) = val
;
566 TILE_PIXEL(tile
, i
, j
, 1) = val
;
567 TILE_PIXEL(tile
, i
, j
, 2) = val
;
568 TILE_PIXEL(tile
, i
, j
, 3) = val
;
570 TILE_PIXEL(tile
, j
, i
, 0) = val
;
571 TILE_PIXEL(tile
, j
, i
, 1) = val
;
572 TILE_PIXEL(tile
, j
, i
, 2) = val
;
573 TILE_PIXEL(tile
, j
, i
, 3) = val
;
583 * Called when we're done writing to a color tile.
586 lp_rast_tile_end(struct lp_rasterizer_task
*task
)
589 if (LP_DEBUG
& (DEBUG_SHOW_SUBTILES
| DEBUG_SHOW_TILES
)) {
590 struct lp_rasterizer
*rast
= task
->rast
;
593 for (buf
= 0; buf
< rast
->state
.nr_cbufs
; buf
++) {
594 uint8_t *color
= lp_rast_get_color_block_pointer(task
, buf
,
597 if (LP_DEBUG
& DEBUG_SHOW_SUBTILES
)
598 outline_subtiles(color
);
599 else if (LP_DEBUG
& DEBUG_SHOW_TILES
)
604 (void) outline_subtiles
;
608 memset(task
->color_tiles
, 0, sizeof(task
->color_tiles
));
609 task
->depth_tile
= NULL
;
615 * Signal on a fence. This is called during bin execution/rasterization.
619 lp_rast_fence(struct lp_rasterizer_task
*task
,
620 const union lp_rast_cmd_arg arg
)
622 struct lp_fence
*fence
= arg
.fence
;
623 lp_fence_signal(fence
);
628 * Begin a new occlusion query.
629 * This is a bin command put in all bins.
633 lp_rast_begin_query(struct lp_rasterizer_task
*task
,
634 const union lp_rast_cmd_arg arg
)
636 /* Reset the per-task counter */
637 task
->vis_counter
= 0;
642 * End the current occlusion query.
643 * This is a bin command put in all bins.
647 lp_rast_end_query(struct lp_rasterizer_task
*task
,
648 const union lp_rast_cmd_arg arg
)
650 struct llvmpipe_query
*pq
= arg
.query_obj
;
652 pipe_mutex_lock(pq
->mutex
);
654 /* Accumulate the visible fragment counter from this tile in
657 pq
->count
[task
->thread_index
] += task
->vis_counter
;
659 /* check if this is the last tile in the scene */
661 if (pq
->tile_count
== pq
->num_tiles
) {
664 /* sum the per-thread counters for the query */
666 for (i
= 0; i
< LP_MAX_THREADS
; i
++) {
667 pq
->result
+= pq
->count
[i
];
670 /* reset counters (in case this query is re-used in the scene) */
671 memset(pq
->count
, 0, sizeof(pq
->count
));
678 pipe_mutex_unlock(pq
->mutex
);
684 * Rasterize commands for a single bin.
685 * \param x, y position of the bin's tile in the framebuffer
686 * Must be called between lp_rast_begin() and lp_rast_end().
690 rasterize_bin(struct lp_rasterizer_task
*task
,
691 const struct cmd_bin
*bin
,
694 const struct cmd_block_list
*commands
= &bin
->commands
;
695 struct cmd_block
*block
;
698 lp_rast_tile_begin( task
, x
* TILE_SIZE
, y
* TILE_SIZE
);
700 /* simply execute each of the commands in the block list */
701 for (block
= commands
->head
; block
; block
= block
->next
) {
702 for (k
= 0; k
< block
->count
; k
++) {
703 block
->cmd
[k
]( task
, block
->arg
[k
] );
707 lp_rast_tile_end(task
);
709 /* Free data for this bin.
711 lp_scene_bin_reset( task
->rast
->curr_scene
, x
, y
);
715 #define RAST(x) { lp_rast_##x, #x }
723 RAST(clear_zstencil
),
732 RAST(shade_tile_opaque
),
734 RAST(store_linear_color
),
741 debug_bin( const struct cmd_bin
*bin
)
743 const struct cmd_block
*head
= bin
->commands
.head
;
746 for (i
= 0; i
< head
->count
; i
++) {
747 debug_printf("%d: ", i
);
748 for (j
= 0; j
< Elements(cmd_names
); j
++) {
749 if (head
->cmd
[i
] == cmd_names
[j
].cmd
) {
750 debug_printf("%s\n", cmd_names
[j
].name
);
754 if (j
== Elements(cmd_names
))
755 debug_printf("...other\n");
760 /* An empty bin is one that just loads the contents of the tile and
761 * stores them again unchanged. This typically happens when bins have
762 * been flushed for some reason in the middle of a frame, or when
763 * incremental updates are being made to a render target.
765 * Try to avoid doing pointless work in this case.
768 is_empty_bin( const struct cmd_bin
*bin
)
770 const struct cmd_block
*head
= bin
->commands
.head
;
776 /* We emit at most two load-tile commands at the start of the first
777 * command block. In addition we seem to emit a couple of
778 * set-state commands even in empty bins.
780 * As a heuristic, if a bin has more than 4 commands, consider it
783 if (head
->next
!= NULL
||
788 for (i
= 0; i
< head
->count
; i
++)
789 if (head
->cmd
[i
] != lp_rast_set_state
&&
790 head
->cmd
[i
] != lp_rast_store_linear_color
) {
800 * Rasterize/execute all bins within a scene.
804 rasterize_scene(struct lp_rasterizer_task
*task
,
805 struct lp_scene
*scene
)
807 /* loop over scene bins, rasterize each */
811 for (i
= 0; i
< scene
->tiles_x
; i
++) {
812 for (j
= 0; j
< scene
->tiles_y
; j
++) {
813 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, i
, j
);
814 rasterize_bin(task
, bin
, i
, j
);
824 while ((bin
= lp_scene_bin_iter_next(scene
, &x
, &y
))) {
825 if (!is_empty_bin( bin
))
826 rasterize_bin(task
, bin
, x
, y
);
832 lp_rast_fence(task
, lp_rast_arg_fence(scene
->fence
));
838 * Called by setup module when it has something for us to render.
841 lp_rast_queue_scene( struct lp_rasterizer
*rast
,
842 struct lp_scene
*scene
)
844 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
846 if (rast
->num_threads
== 0) {
849 lp_rast_begin( rast
, scene
);
851 rasterize_scene( &rast
->tasks
[0], scene
);
853 lp_scene_reset( scene
);
857 rast
->curr_scene
= NULL
;
860 /* threaded rendering! */
863 lp_scene_enqueue( rast
->full_scenes
, scene
);
865 /* signal the threads that there's work to do */
866 for (i
= 0; i
< rast
->num_threads
; i
++) {
867 pipe_semaphore_signal(&rast
->tasks
[i
].work_ready
);
871 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
876 lp_rast_finish( struct lp_rasterizer
*rast
)
878 if (rast
->num_threads
== 0) {
884 /* wait for work to complete */
885 for (i
= 0; i
< rast
->num_threads
; i
++) {
886 pipe_semaphore_wait(&rast
->tasks
[i
].work_done
);
893 * This is the thread's main entrypoint.
894 * It's a simple loop:
897 * 3. signal that we're done
899 static PIPE_THREAD_ROUTINE( thread_func
, init_data
)
901 struct lp_rasterizer_task
*task
= (struct lp_rasterizer_task
*) init_data
;
902 struct lp_rasterizer
*rast
= task
->rast
;
903 boolean debug
= false;
908 debug_printf("thread %d waiting for work\n", task
->thread_index
);
909 pipe_semaphore_wait(&task
->work_ready
);
914 if (task
->thread_index
== 0) {
916 * - get next scene to rasterize
917 * - map the framebuffer surfaces
920 lp_scene_dequeue( rast
->full_scenes
, TRUE
) );
923 /* Wait for all threads to get here so that threads[1+] don't
924 * get a null rast->curr_scene pointer.
926 pipe_barrier_wait( &rast
->barrier
);
930 debug_printf("thread %d doing work\n", task
->thread_index
);
932 rasterize_scene(task
,
935 /* wait for all threads to finish with this scene */
936 pipe_barrier_wait( &rast
->barrier
);
938 /* XXX: shouldn't be necessary:
940 if (task
->thread_index
== 0) {
944 /* signal done with work */
946 debug_printf("thread %d done working\n", task
->thread_index
);
948 pipe_semaphore_signal(&task
->work_done
);
956 * Initialize semaphores and spawn the threads.
959 create_rast_threads(struct lp_rasterizer
*rast
)
963 /* NOTE: if num_threads is zero, we won't use any threads */
964 for (i
= 0; i
< rast
->num_threads
; i
++) {
965 pipe_semaphore_init(&rast
->tasks
[i
].work_ready
, 0);
966 pipe_semaphore_init(&rast
->tasks
[i
].work_done
, 0);
967 rast
->threads
[i
] = pipe_thread_create(thread_func
,
968 (void *) &rast
->tasks
[i
]);
975 * Create new lp_rasterizer. If num_threads is zero, don't create any
976 * new threads, do rendering synchronously.
977 * \param num_threads number of rasterizer threads to create
979 struct lp_rasterizer
*
980 lp_rast_create( unsigned num_threads
)
982 struct lp_rasterizer
*rast
;
985 rast
= CALLOC_STRUCT(lp_rasterizer
);
989 rast
->full_scenes
= lp_scene_queue_create();
991 for (i
= 0; i
< Elements(rast
->tasks
); i
++) {
992 struct lp_rasterizer_task
*task
= &rast
->tasks
[i
];
994 task
->thread_index
= i
;
997 rast
->num_threads
= num_threads
;
999 create_rast_threads(rast
);
1001 /* for synchronizing rasterization threads */
1002 pipe_barrier_init( &rast
->barrier
, rast
->num_threads
);
1010 void lp_rast_destroy( struct lp_rasterizer
*rast
)
1014 /* Set exit_flag and signal each thread's work_ready semaphore.
1015 * Each thread will be woken up, notice that the exit_flag is set and
1016 * break out of its main loop. The thread will then exit.
1018 rast
->exit_flag
= TRUE
;
1019 for (i
= 0; i
< rast
->num_threads
; i
++) {
1020 pipe_semaphore_signal(&rast
->tasks
[i
].work_ready
);
1023 /* Wait for threads to terminate before cleaning up per-thread data */
1024 for (i
= 0; i
< rast
->num_threads
; i
++) {
1025 pipe_thread_wait(rast
->threads
[i
]);
1028 /* Clean up per-thread data */
1029 for (i
= 0; i
< rast
->num_threads
; i
++) {
1030 pipe_semaphore_destroy(&rast
->tasks
[i
].work_ready
);
1031 pipe_semaphore_destroy(&rast
->tasks
[i
].work_done
);
1034 /* for synchronizing rasterization threads */
1035 pipe_barrier_destroy( &rast
->barrier
);
1037 lp_scene_queue_destroy(rast
->full_scenes
);
1043 /** Return number of rasterization threads */
1045 lp_rast_get_num_threads( struct lp_rasterizer
*rast
)
1047 return rast
->num_threads
;