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"
38 #include "lp_rast_priv.h"
39 #include "lp_tile_soa.h"
40 #include "gallivm/lp_bld_debug.h"
45 * Begin rasterizing a scene.
46 * Called once per scene by one thread.
49 lp_rast_begin( struct lp_rasterizer
*rast
,
50 struct lp_scene
*scene
)
52 const struct pipe_framebuffer_state
*fb
= &scene
->fb
;
55 rast
->curr_scene
= scene
;
57 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
59 rast
->state
.nr_cbufs
= scene
->fb
.nr_cbufs
;
61 for (i
= 0; i
< rast
->state
.nr_cbufs
; i
++) {
62 struct pipe_surface
*cbuf
= scene
->fb
.cbufs
[i
];
63 rast
->cbuf
[i
].format
= cbuf
->texture
->format
;
64 rast
->cbuf
[i
].tiles_per_row
= align(cbuf
->width
, TILE_SIZE
) / TILE_SIZE
;
65 rast
->cbuf
[i
].blocksize
=
66 util_format_get_blocksize(cbuf
->texture
->format
);
67 rast
->cbuf
[i
].map
= llvmpipe_resource_map(cbuf
->texture
,
71 LP_TEX_USAGE_READ_WRITE
,
76 struct pipe_surface
*zsbuf
= scene
->fb
.zsbuf
;
77 rast
->zsbuf
.stride
= llvmpipe_resource_stride(zsbuf
->texture
, zsbuf
->level
);
78 rast
->zsbuf
.blocksize
=
79 util_format_get_blocksize(zsbuf
->texture
->format
);
81 rast
->zsbuf
.map
= llvmpipe_resource_map(zsbuf
->texture
,
85 LP_TEX_USAGE_READ_WRITE
,
87 assert(rast
->zsbuf
.map
);
90 lp_scene_bin_iter_begin( scene
);
95 lp_rast_end( struct lp_rasterizer
*rast
)
97 struct lp_scene
*scene
= rast
->curr_scene
;
100 /* Unmap color buffers */
101 for (i
= 0; i
< rast
->state
.nr_cbufs
; i
++) {
102 struct pipe_surface
*cbuf
= scene
->fb
.cbufs
[i
];
103 llvmpipe_resource_unmap(cbuf
->texture
,
107 rast
->cbuf
[i
].map
= NULL
;
110 /* Unmap z/stencil buffer */
111 if (rast
->zsbuf
.map
) {
112 struct pipe_surface
*zsbuf
= scene
->fb
.zsbuf
;
113 llvmpipe_resource_unmap(zsbuf
->texture
,
117 rast
->zsbuf
.map
= NULL
;
120 lp_scene_reset( rast
->curr_scene
);
122 rast
->curr_scene
= NULL
;
126 debug_printf("Post render scene: tile unswizzle: %u tile swizzle: %u\n",
127 lp_tile_unswizzle_count
, lp_tile_swizzle_count
);
133 * Begining rasterization of a tile.
134 * \param x window X position of the tile, in pixels
135 * \param y window Y position of the tile, in pixels
138 lp_rast_tile_begin(struct lp_rasterizer_task
*task
,
139 unsigned x
, unsigned y
)
141 struct lp_rasterizer
*rast
= task
->rast
;
142 struct lp_scene
*scene
= rast
->curr_scene
;
143 enum lp_texture_usage usage
;
146 LP_DBG(DEBUG_RAST
, "%s %d,%d\n", __FUNCTION__
, x
, y
);
148 assert(x
% TILE_SIZE
== 0);
149 assert(y
% TILE_SIZE
== 0);
154 if (scene
->has_color_clear
)
155 usage
= LP_TEX_USAGE_WRITE_ALL
;
157 usage
= LP_TEX_USAGE_READ_WRITE
;
159 /* get pointers to color tile(s) */
160 for (buf
= 0; buf
< rast
->state
.nr_cbufs
; buf
++) {
161 struct pipe_surface
*cbuf
= rast
->curr_scene
->fb
.cbufs
[buf
];
162 struct llvmpipe_resource
*lpt
;
164 lpt
= llvmpipe_resource(cbuf
->texture
);
165 task
->color_tiles
[buf
] = llvmpipe_get_texture_tile(lpt
,
166 cbuf
->face
+ cbuf
->zslice
,
170 assert(task
->color_tiles
[buf
]);
173 /* get pointer to depth/stencil tile */
175 struct pipe_surface
*zsbuf
= rast
->curr_scene
->fb
.zsbuf
;
177 struct llvmpipe_resource
*lpt
= llvmpipe_resource(zsbuf
->texture
);
179 if (scene
->has_depth_clear
)
180 usage
= LP_TEX_USAGE_WRITE_ALL
;
182 usage
= LP_TEX_USAGE_READ_WRITE
;
184 /* "prime" the tile: convert data from linear to tiled if necessary
185 * and update the tile's layout info.
187 (void) llvmpipe_get_texture_tile(lpt
,
188 zsbuf
->face
+ zsbuf
->zslice
,
192 /* Get actual pointer to the tile data. Note that depth/stencil
193 * data is tiled differently than color data.
195 task
->depth_tile
= lp_rast_get_depth_block_pointer(rast
, x
, y
);
197 assert(task
->depth_tile
);
200 task
->depth_tile
= NULL
;
207 * Clear the rasterizer's current color tile.
208 * This is a bin command called during bin processing.
211 lp_rast_clear_color(struct lp_rasterizer_task
*task
,
212 const union lp_rast_cmd_arg arg
)
214 struct lp_rasterizer
*rast
= task
->rast
;
215 const uint8_t *clear_color
= arg
.clear_color
;
219 LP_DBG(DEBUG_RAST
, "%s 0x%x,0x%x,0x%x,0x%x\n", __FUNCTION__
,
225 if (clear_color
[0] == clear_color
[1] &&
226 clear_color
[1] == clear_color
[2] &&
227 clear_color
[2] == clear_color
[3]) {
228 /* clear to grayscale value {x, x, x, x} */
229 for (i
= 0; i
< rast
->state
.nr_cbufs
; i
++) {
230 uint8_t *ptr
= task
->color_tiles
[i
];
231 memset(ptr
, clear_color
[0], TILE_SIZE
* TILE_SIZE
* 4);
236 * Note: if the swizzled tile layout changes (see TILE_PIXEL) this code
237 * will need to change. It'll be pretty obvious when clearing no longer
240 const unsigned chunk
= TILE_SIZE
/ 4;
241 for (i
= 0; i
< rast
->state
.nr_cbufs
; i
++) {
242 uint8_t *c
= task
->color_tiles
[i
];
245 for (j
= 0; j
< 4 * TILE_SIZE
; j
++) {
246 memset(c
, clear_color
[0], chunk
);
248 memset(c
, clear_color
[1], chunk
);
250 memset(c
, clear_color
[2], chunk
);
252 memset(c
, clear_color
[3], chunk
);
258 LP_COUNT(nr_color_tile_clear
);
263 * Clear the rasterizer's current z/stencil tile.
264 * This is a bin command called during bin processing.
267 lp_rast_clear_zstencil(struct lp_rasterizer_task
*task
,
268 const union lp_rast_cmd_arg arg
)
270 struct lp_rasterizer
*rast
= task
->rast
;
271 const unsigned height
= TILE_SIZE
/ TILE_VECTOR_HEIGHT
;
272 const unsigned width
= TILE_SIZE
* TILE_VECTOR_HEIGHT
;
273 const unsigned block_size
= rast
->zsbuf
.blocksize
;
274 const unsigned dst_stride
= rast
->zsbuf
.stride
* TILE_VECTOR_HEIGHT
;
278 LP_DBG(DEBUG_RAST
, "%s 0x%x\n", __FUNCTION__
, arg
.clear_zstencil
);
281 * Clear the aera of the swizzled depth/depth buffer matching this tile, in
282 * stripes of TILE_VECTOR_HEIGHT x TILE_SIZE at a time.
284 * The swizzled depth format is such that the depths for
285 * TILE_VECTOR_HEIGHT x TILE_VECTOR_WIDTH pixels have consecutive offsets.
288 dst
= task
->depth_tile
;
290 assert(dst
== lp_rast_get_depth_block_pointer(rast
, task
->x
, task
->y
));
292 switch (block_size
) {
294 memset(dst
, (uint8_t) arg
.clear_zstencil
, height
* width
);
297 for (i
= 0; i
< height
; i
++) {
298 uint16_t *row
= (uint16_t *)dst
;
299 for (j
= 0; j
< width
; j
++)
300 *row
++ = (uint16_t) arg
.clear_zstencil
;
305 for (i
= 0; i
< height
; i
++) {
306 uint32_t *row
= (uint32_t *)dst
;
307 for (j
= 0; j
< width
; j
++)
308 *row
++ = arg
.clear_zstencil
;
320 * Load tile color from the framebuffer surface.
321 * This is a bin command called during bin processing.
325 lp_rast_load_color(struct lp_rasterizer_task
*task
,
326 const union lp_rast_cmd_arg arg
)
328 struct lp_rasterizer
*rast
= task
->rast
;
330 enum lp_texture_usage usage
;
332 LP_DBG(DEBUG_RAST
, "%s at %u, %u\n", __FUNCTION__
, x
, y
);
334 if (scene
->has_color_clear
)
335 usage
= LP_TEX_USAGE_WRITE_ALL
;
337 usage
= LP_TEX_USAGE_READ_WRITE
;
339 /* Get pointers to color tile(s).
340 * This will convert linear data to tiled if needed.
342 for (buf
= 0; buf
< rast
->state
.nr_cbufs
; buf
++) {
343 struct pipe_surface
*cbuf
= rast
->curr_scene
->fb
.cbufs
[buf
];
344 struct llvmpipe_texture
*lpt
;
346 lpt
= llvmpipe_texture(cbuf
->texture
);
347 task
->color_tiles
[buf
] = llvmpipe_get_texture_tile(lpt
,
348 cbuf
->face
+ cbuf
->zslice
,
352 assert(task
->color_tiles
[buf
]);
359 * Convert the color tile from tiled to linear layout.
360 * This is generally only done when we're flushing the scene just prior to
361 * SwapBuffers. If we didn't do this here, we'd have to convert the entire
362 * tiled color buffer to linear layout in the llvmpipe_texture_unmap()
363 * function. It's better to do it here to take advantage of
364 * threading/parallelism.
365 * This is a bin command which is stored in all bins.
368 lp_rast_store_color( struct lp_rasterizer_task
*task
,
369 const union lp_rast_cmd_arg arg
)
371 struct lp_rasterizer
*rast
= task
->rast
;
372 struct lp_scene
*scene
= rast
->curr_scene
;
375 for (buf
= 0; buf
< rast
->state
.nr_cbufs
; buf
++) {
376 struct pipe_surface
*cbuf
= scene
->fb
.cbufs
[buf
];
377 const unsigned face
= cbuf
->face
, level
= cbuf
->level
;
378 struct llvmpipe_resource
*lpt
= llvmpipe_resource(cbuf
->texture
);
379 /* this will convert the tiled data to linear if needed */
380 (void) llvmpipe_get_texture_tile_linear(lpt
, face
, level
,
388 * This is a bin command called during bin processing.
391 lp_rast_set_state(struct lp_rasterizer_task
*task
,
392 const union lp_rast_cmd_arg arg
)
394 const struct lp_rast_state
*state
= arg
.set_state
;
396 LP_DBG(DEBUG_RAST
, "%s %p\n", __FUNCTION__
, (void *) state
);
398 /* just set the current state pointer for this rasterizer */
399 task
->current_state
= state
;
404 * Run the shader on all blocks in a tile. This is used when a tile is
405 * completely contained inside a triangle.
406 * This is a bin command called during bin processing.
409 lp_rast_shade_tile(struct lp_rasterizer_task
*task
,
410 const union lp_rast_cmd_arg arg
)
412 struct lp_rasterizer
*rast
= task
->rast
;
413 const struct lp_rast_state
*state
= task
->current_state
;
414 const struct lp_rast_shader_inputs
*inputs
= arg
.shade_tile
;
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(rast
, tile_x
+ x
, tile_y
+ y
);
435 /* run shader on 4x4 block */
436 state
->jit_function
[RAST_WHOLE
]( &state
->jit_context
,
437 tile_x
+ x
, tile_y
+ y
,
444 INT_MIN
, INT_MIN
, INT_MIN
,
452 * Compute shading for a 4x4 block of pixels.
453 * This is a bin command called during bin processing.
454 * \param x X position of quad in window coords
455 * \param y Y position of quad in window coords
457 void lp_rast_shade_quads( struct lp_rasterizer_task
*task
,
458 const struct lp_rast_shader_inputs
*inputs
,
459 unsigned x
, unsigned y
,
460 int32_t c1
, int32_t c2
, int32_t c3
)
462 const struct lp_rast_state
*state
= task
->current_state
;
463 struct lp_rasterizer
*rast
= task
->rast
;
464 uint8_t *color
[PIPE_MAX_COLOR_BUFS
];
471 assert(x
% TILE_VECTOR_WIDTH
== 0);
472 assert(y
% TILE_VECTOR_HEIGHT
== 0);
474 assert((x
% 4) == 0);
475 assert((y
% 4) == 0);
478 for (i
= 0; i
< rast
->state
.nr_cbufs
; i
++) {
479 color
[i
] = lp_rast_get_color_block_pointer(task
, i
, x
, y
);
480 assert(lp_check_alignment(color
[i
], 16));
484 depth
= lp_rast_get_depth_block_pointer(rast
, x
, y
);
487 assert(lp_check_alignment(state
->jit_context
.blend_color
, 16));
489 assert(lp_check_alignment(inputs
->step
[0], 16));
490 assert(lp_check_alignment(inputs
->step
[1], 16));
491 assert(lp_check_alignment(inputs
->step
[2], 16));
493 /* run shader on 4x4 block */
494 state
->jit_function
[RAST_EDGE_TEST
]( &state
->jit_context
,
510 * Set top row and left column of the tile's pixels to white. For debugging.
513 outline_tile(uint8_t *tile
)
515 const uint8_t val
= 0xff;
518 for (i
= 0; i
< TILE_SIZE
; i
++) {
519 TILE_PIXEL(tile
, i
, 0, 0) = val
;
520 TILE_PIXEL(tile
, i
, 0, 1) = val
;
521 TILE_PIXEL(tile
, i
, 0, 2) = val
;
522 TILE_PIXEL(tile
, i
, 0, 3) = val
;
524 TILE_PIXEL(tile
, 0, i
, 0) = val
;
525 TILE_PIXEL(tile
, 0, i
, 1) = val
;
526 TILE_PIXEL(tile
, 0, i
, 2) = val
;
527 TILE_PIXEL(tile
, 0, i
, 3) = val
;
533 * Draw grid of gray lines at 16-pixel intervals across the tile to
534 * show the sub-tile boundaries. For debugging.
537 outline_subtiles(uint8_t *tile
)
539 const uint8_t val
= 0x80;
540 const unsigned step
= 16;
543 for (i
= 0; i
< TILE_SIZE
; i
+= step
) {
544 for (j
= 0; j
< TILE_SIZE
; j
++) {
545 TILE_PIXEL(tile
, i
, j
, 0) = val
;
546 TILE_PIXEL(tile
, i
, j
, 1) = val
;
547 TILE_PIXEL(tile
, i
, j
, 2) = val
;
548 TILE_PIXEL(tile
, i
, j
, 3) = val
;
550 TILE_PIXEL(tile
, j
, i
, 0) = val
;
551 TILE_PIXEL(tile
, j
, i
, 1) = val
;
552 TILE_PIXEL(tile
, j
, i
, 2) = val
;
553 TILE_PIXEL(tile
, j
, i
, 3) = val
;
563 * Called when we're done writing to a color tile.
566 lp_rast_tile_end(struct lp_rasterizer_task
*task
)
569 struct lp_rasterizer
*rast
= task
->rast
;
572 for (buf
= 0; buf
< rast
->state
.nr_cbufs
; buf
++) {
573 uint8_t *color
= lp_rast_get_color_block_pointer(task
, buf
,
576 if (LP_DEBUG
& DEBUG_SHOW_SUBTILES
)
577 outline_subtiles(color
);
578 else if (LP_DEBUG
& DEBUG_SHOW_TILES
)
582 (void) outline_subtiles
;
586 memset(task
->color_tiles
, 0, sizeof(task
->color_tiles
));
587 task
->depth_tile
= NULL
;
593 * Signal on a fence. This is called during bin execution/rasterization.
597 lp_rast_fence(struct lp_rasterizer_task
*task
,
598 const union lp_rast_cmd_arg arg
)
600 struct lp_fence
*fence
= arg
.fence
;
601 lp_fence_signal(fence
);
608 * Rasterize commands for a single bin.
609 * \param x, y position of the bin's tile in the framebuffer
610 * Must be called between lp_rast_begin() and lp_rast_end().
614 rasterize_bin(struct lp_rasterizer_task
*task
,
615 const struct cmd_bin
*bin
,
618 const struct cmd_block_list
*commands
= &bin
->commands
;
619 struct cmd_block
*block
;
622 lp_rast_tile_begin( task
, x
* TILE_SIZE
, y
* TILE_SIZE
);
624 /* simply execute each of the commands in the block list */
625 for (block
= commands
->head
; block
; block
= block
->next
) {
626 for (k
= 0; k
< block
->count
; k
++) {
627 block
->cmd
[k
]( task
, block
->arg
[k
] );
631 lp_rast_tile_end(task
);
633 /* Free data for this bin.
635 lp_scene_bin_reset( task
->rast
->curr_scene
, x
, y
);
639 #define RAST(x) { lp_rast_##x, #x }
647 RAST(clear_zstencil
),
656 debug_bin( const struct cmd_bin
*bin
)
658 const struct cmd_block
*head
= bin
->commands
.head
;
661 for (i
= 0; i
< head
->count
; i
++) {
662 debug_printf("%d: ", i
);
663 for (j
= 0; j
< Elements(cmd_names
); j
++) {
664 if (head
->cmd
[i
] == cmd_names
[j
].cmd
) {
665 debug_printf("%s\n", cmd_names
[j
].name
);
669 if (j
== Elements(cmd_names
))
670 debug_printf("...other\n");
675 /* An empty bin is one that just loads the contents of the tile and
676 * stores them again unchanged. This typically happens when bins have
677 * been flushed for some reason in the middle of a frame, or when
678 * incremental updates are being made to a render target.
680 * Try to avoid doing pointless work in this case.
683 is_empty_bin( const struct cmd_bin
*bin
)
685 const struct cmd_block
*head
= bin
->commands
.head
;
691 /* We emit at most two load-tile commands at the start of the first
692 * command block. In addition we seem to emit a couple of
693 * set-state commands even in empty bins.
695 * As a heuristic, if a bin has more than 4 commands, consider it
698 if (head
->next
!= NULL
||
703 for (i
= 0; i
< head
->count
; i
++)
704 if (head
->cmd
[i
] != lp_rast_set_state
) {
714 * Rasterize/execute all bins within a scene.
718 rasterize_scene(struct lp_rasterizer_task
*task
,
719 struct lp_scene
*scene
)
721 /* loop over scene bins, rasterize each */
725 for (i
= 0; i
< scene
->tiles_x
; i
++) {
726 for (j
= 0; j
< scene
->tiles_y
; j
++) {
727 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, i
, j
);
728 rasterize_bin(task
, bin
, i
, j
);
738 while ((bin
= lp_scene_bin_iter_next(scene
, &x
, &y
))) {
739 if (!is_empty_bin( bin
))
740 rasterize_bin(task
, bin
, x
, y
);
748 * Called by setup module when it has something for us to render.
751 lp_rast_queue_scene( struct lp_rasterizer
*rast
,
752 struct lp_scene
*scene
)
754 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
756 if (rast
->num_threads
== 0) {
759 lp_rast_begin( rast
, scene
);
761 rasterize_scene( &rast
->tasks
[0], scene
);
763 lp_scene_reset( scene
);
767 rast
->curr_scene
= NULL
;
770 /* threaded rendering! */
773 lp_scene_enqueue( rast
->full_scenes
, scene
);
775 /* signal the threads that there's work to do */
776 for (i
= 0; i
< rast
->num_threads
; i
++) {
777 pipe_semaphore_signal(&rast
->tasks
[i
].work_ready
);
781 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
786 lp_rast_finish( struct lp_rasterizer
*rast
)
788 if (rast
->num_threads
== 0) {
794 /* wait for work to complete */
795 for (i
= 0; i
< rast
->num_threads
; i
++) {
796 pipe_semaphore_wait(&rast
->tasks
[i
].work_done
);
803 * This is the thread's main entrypoint.
804 * It's a simple loop:
807 * 3. signal that we're done
809 static PIPE_THREAD_ROUTINE( thread_func
, init_data
)
811 struct lp_rasterizer_task
*task
= (struct lp_rasterizer_task
*) init_data
;
812 struct lp_rasterizer
*rast
= task
->rast
;
813 boolean debug
= false;
818 debug_printf("thread %d waiting for work\n", task
->thread_index
);
819 pipe_semaphore_wait(&task
->work_ready
);
824 if (task
->thread_index
== 0) {
826 * - get next scene to rasterize
827 * - map the framebuffer surfaces
830 lp_scene_dequeue( rast
->full_scenes
, TRUE
) );
833 /* Wait for all threads to get here so that threads[1+] don't
834 * get a null rast->curr_scene pointer.
836 pipe_barrier_wait( &rast
->barrier
);
840 debug_printf("thread %d doing work\n", task
->thread_index
);
842 rasterize_scene(task
,
845 /* wait for all threads to finish with this scene */
846 pipe_barrier_wait( &rast
->barrier
);
848 /* XXX: shouldn't be necessary:
850 if (task
->thread_index
== 0) {
854 /* signal done with work */
856 debug_printf("thread %d done working\n", task
->thread_index
);
858 pipe_semaphore_signal(&task
->work_done
);
866 * Initialize semaphores and spawn the threads.
869 create_rast_threads(struct lp_rasterizer
*rast
)
873 /* NOTE: if num_threads is zero, we won't use any threads */
874 for (i
= 0; i
< rast
->num_threads
; i
++) {
875 pipe_semaphore_init(&rast
->tasks
[i
].work_ready
, 0);
876 pipe_semaphore_init(&rast
->tasks
[i
].work_done
, 0);
877 rast
->threads
[i
] = pipe_thread_create(thread_func
,
878 (void *) &rast
->tasks
[i
]);
885 * Create new lp_rasterizer. If num_threads is zero, don't create any
886 * new threads, do rendering synchronously.
887 * \param num_threads number of rasterizer threads to create
889 struct lp_rasterizer
*
890 lp_rast_create( unsigned num_threads
)
892 struct lp_rasterizer
*rast
;
895 rast
= CALLOC_STRUCT(lp_rasterizer
);
899 rast
->full_scenes
= lp_scene_queue_create();
901 for (i
= 0; i
< Elements(rast
->tasks
); i
++) {
902 struct lp_rasterizer_task
*task
= &rast
->tasks
[i
];
904 task
->thread_index
= i
;
907 rast
->num_threads
= num_threads
;
909 create_rast_threads(rast
);
911 /* for synchronizing rasterization threads */
912 pipe_barrier_init( &rast
->barrier
, rast
->num_threads
);
920 void lp_rast_destroy( struct lp_rasterizer
*rast
)
924 /* Set exit_flag and signal each thread's work_ready semaphore.
925 * Each thread will be woken up, notice that the exit_flag is set and
926 * break out of its main loop. The thread will then exit.
928 rast
->exit_flag
= TRUE
;
929 for (i
= 0; i
< rast
->num_threads
; i
++) {
930 pipe_semaphore_signal(&rast
->tasks
[i
].work_ready
);
933 /* Wait for threads to terminate before cleaning up per-thread data */
934 for (i
= 0; i
< rast
->num_threads
; i
++) {
935 pipe_thread_wait(rast
->threads
[i
]);
938 /* Clean up per-thread data */
939 for (i
= 0; i
< rast
->num_threads
; i
++) {
940 pipe_semaphore_destroy(&rast
->tasks
[i
].work_ready
);
941 pipe_semaphore_destroy(&rast
->tasks
[i
].work_done
);
944 /* for synchronizing rasterization threads */
945 pipe_barrier_destroy( &rast
->barrier
);
951 /** Return number of rasterization threads */
953 lp_rast_get_num_threads( struct lp_rasterizer
*rast
)
955 return rast
->num_threads
;