c3e186b2a0636a6002b263dcdd30406e4f2aa06b
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"
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
;
126 debug_printf("Post render scene: tile read: %d tile write: %d\n",
127 tile_read_count
, tile_write_count
);
132 * Begining rasterization of a tile.
133 * \param x window X position of the tile, in pixels
134 * \param y window Y position of the tile, in pixels
137 lp_rast_tile_begin(struct lp_rasterizer_task
*task
,
138 unsigned x
, unsigned y
)
140 struct lp_rasterizer
*rast
= task
->rast
;
141 struct lp_scene
*scene
= rast
->curr_scene
;
142 enum lp_texture_usage usage
;
145 LP_DBG(DEBUG_RAST
, "%s %d,%d\n", __FUNCTION__
, x
, y
);
147 assert(x
% TILE_SIZE
== 0);
148 assert(y
% TILE_SIZE
== 0);
153 if (scene
->has_color_clear
)
154 usage
= LP_TEX_USAGE_WRITE_ALL
;
156 usage
= LP_TEX_USAGE_READ_WRITE
;
158 /* get pointers to color tile(s) */
159 for (buf
= 0; buf
< rast
->state
.nr_cbufs
; buf
++) {
160 struct pipe_surface
*cbuf
= rast
->curr_scene
->fb
.cbufs
[buf
];
161 struct llvmpipe_resource
*lpt
;
163 lpt
= llvmpipe_resource(cbuf
->texture
);
164 task
->color_tiles
[buf
] = llvmpipe_get_texture_tile(lpt
,
165 cbuf
->face
+ cbuf
->zslice
,
169 assert(task
->color_tiles
[buf
]);
172 /* get pointer to depth/stencil tile */
174 struct pipe_surface
*zsbuf
= rast
->curr_scene
->fb
.zsbuf
;
176 struct llvmpipe_resource
*lpt
= llvmpipe_resource(zsbuf
->texture
);
178 if (scene
->has_depth_clear
)
179 usage
= LP_TEX_USAGE_WRITE_ALL
;
181 usage
= LP_TEX_USAGE_READ_WRITE
;
183 /* "prime" the tile: convert data from linear to tiled if necessary
184 * and update the tile's layout info.
186 (void) llvmpipe_get_texture_tile(lpt
,
187 zsbuf
->face
+ zsbuf
->zslice
,
191 /* Get actual pointer to the tile data. Note that depth/stencil
192 * data is tiled differently than color data.
194 task
->depth_tile
= lp_rast_get_depth_block_pointer(rast
, x
, y
);
196 assert(task
->depth_tile
);
199 task
->depth_tile
= NULL
;
206 * Clear the rasterizer's current color tile.
207 * This is a bin command called during bin processing.
210 lp_rast_clear_color(struct lp_rasterizer_task
*task
,
211 const union lp_rast_cmd_arg arg
)
213 struct lp_rasterizer
*rast
= task
->rast
;
214 const uint8_t *clear_color
= arg
.clear_color
;
218 LP_DBG(DEBUG_RAST
, "%s 0x%x,0x%x,0x%x,0x%x\n", __FUNCTION__
,
224 if (clear_color
[0] == clear_color
[1] &&
225 clear_color
[1] == clear_color
[2] &&
226 clear_color
[2] == clear_color
[3]) {
227 /* clear to grayscale value {x, x, x, x} */
228 for (i
= 0; i
< rast
->state
.nr_cbufs
; i
++) {
229 uint8_t *ptr
= task
->color_tiles
[i
];
230 memset(ptr
, clear_color
[0], TILE_SIZE
* TILE_SIZE
* 4);
235 * Note: if the swizzled tile layout changes (see TILE_PIXEL) this code
236 * will need to change. It'll be pretty obvious when clearing no longer
239 const unsigned chunk
= TILE_SIZE
/ 4;
240 for (i
= 0; i
< rast
->state
.nr_cbufs
; i
++) {
241 uint8_t *c
= task
->color_tiles
[i
];
244 for (j
= 0; j
< 4 * TILE_SIZE
; j
++) {
245 memset(c
, clear_color
[0], chunk
);
247 memset(c
, clear_color
[1], chunk
);
249 memset(c
, clear_color
[2], chunk
);
251 memset(c
, clear_color
[3], chunk
);
257 LP_COUNT(nr_color_tile_clear
);
262 * Clear the rasterizer's current z/stencil tile.
263 * This is a bin command called during bin processing.
266 lp_rast_clear_zstencil(struct lp_rasterizer_task
*task
,
267 const union lp_rast_cmd_arg arg
)
269 struct lp_rasterizer
*rast
= task
->rast
;
270 const unsigned height
= TILE_SIZE
/ TILE_VECTOR_HEIGHT
;
271 const unsigned width
= TILE_SIZE
* TILE_VECTOR_HEIGHT
;
272 const unsigned block_size
= rast
->zsbuf
.blocksize
;
273 const unsigned dst_stride
= rast
->zsbuf
.stride
* TILE_VECTOR_HEIGHT
;
277 LP_DBG(DEBUG_RAST
, "%s 0x%x\n", __FUNCTION__
, arg
.clear_zstencil
);
280 * Clear the aera of the swizzled depth/depth buffer matching this tile, in
281 * stripes of TILE_VECTOR_HEIGHT x TILE_SIZE at a time.
283 * The swizzled depth format is such that the depths for
284 * TILE_VECTOR_HEIGHT x TILE_VECTOR_WIDTH pixels have consecutive offsets.
287 dst
= task
->depth_tile
;
289 assert(dst
== lp_rast_get_depth_block_pointer(rast
, task
->x
, task
->y
));
291 switch (block_size
) {
293 memset(dst
, (uint8_t) arg
.clear_zstencil
, height
* width
);
296 for (i
= 0; i
< height
; i
++) {
297 uint16_t *row
= (uint16_t *)dst
;
298 for (j
= 0; j
< width
; j
++)
299 *row
++ = (uint16_t) arg
.clear_zstencil
;
304 for (i
= 0; i
< height
; i
++) {
305 uint32_t *row
= (uint32_t *)dst
;
306 for (j
= 0; j
< width
; j
++)
307 *row
++ = arg
.clear_zstencil
;
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_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 const unsigned tile_x
= task
->x
, tile_y
= task
->y
;
417 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
419 /* render the whole 64x64 tile in 4x4 chunks */
420 for (y
= 0; y
< TILE_SIZE
; y
+= 4){
421 for (x
= 0; x
< TILE_SIZE
; x
+= 4) {
422 uint8_t *color
[PIPE_MAX_COLOR_BUFS
];
427 for (i
= 0; i
< rast
->state
.nr_cbufs
; i
++)
428 color
[i
] = lp_rast_get_color_block_pointer(task
, i
,
429 tile_x
+ x
, tile_y
+ y
);
432 depth
= lp_rast_get_depth_block_pointer(rast
, tile_x
+ x
, tile_y
+ y
);
434 /* run shader on 4x4 block */
435 state
->jit_function
[RAST_WHOLE
]( &state
->jit_context
,
436 tile_x
+ x
, tile_y
+ y
,
443 INT_MIN
, INT_MIN
, INT_MIN
,
451 * Compute shading for a 4x4 block of pixels.
452 * This is a bin command called during bin processing.
453 * \param x X position of quad in window coords
454 * \param y Y position of quad in window coords
456 void lp_rast_shade_quads( struct lp_rasterizer_task
*task
,
457 const struct lp_rast_shader_inputs
*inputs
,
458 unsigned x
, unsigned y
,
459 int32_t c1
, int32_t c2
, int32_t c3
)
461 const struct lp_rast_state
*state
= task
->current_state
;
462 struct lp_rasterizer
*rast
= task
->rast
;
463 uint8_t *color
[PIPE_MAX_COLOR_BUFS
];
470 assert(x
% TILE_VECTOR_WIDTH
== 0);
471 assert(y
% TILE_VECTOR_HEIGHT
== 0);
473 assert((x
% 4) == 0);
474 assert((y
% 4) == 0);
477 for (i
= 0; i
< rast
->state
.nr_cbufs
; i
++) {
478 color
[i
] = lp_rast_get_color_block_pointer(task
, i
, x
, y
);
479 assert(lp_check_alignment(color
[i
], 16));
483 depth
= lp_rast_get_depth_block_pointer(rast
, x
, y
);
486 assert(lp_check_alignment(state
->jit_context
.blend_color
, 16));
488 assert(lp_check_alignment(inputs
->step
[0], 16));
489 assert(lp_check_alignment(inputs
->step
[1], 16));
490 assert(lp_check_alignment(inputs
->step
[2], 16));
492 /* run shader on 4x4 block */
493 state
->jit_function
[RAST_EDGE_TEST
]( &state
->jit_context
,
509 * Set top row and left column of the tile's pixels to white. For debugging.
512 outline_tile(uint8_t *tile
)
514 const uint8_t val
= 0xff;
517 for (i
= 0; i
< TILE_SIZE
; i
++) {
518 TILE_PIXEL(tile
, i
, 0, 0) = val
;
519 TILE_PIXEL(tile
, i
, 0, 1) = val
;
520 TILE_PIXEL(tile
, i
, 0, 2) = val
;
521 TILE_PIXEL(tile
, i
, 0, 3) = val
;
523 TILE_PIXEL(tile
, 0, i
, 0) = val
;
524 TILE_PIXEL(tile
, 0, i
, 1) = val
;
525 TILE_PIXEL(tile
, 0, i
, 2) = val
;
526 TILE_PIXEL(tile
, 0, i
, 3) = val
;
532 * Draw grid of gray lines at 16-pixel intervals across the tile to
533 * show the sub-tile boundaries. For debugging.
536 outline_subtiles(uint8_t *tile
)
538 const uint8_t val
= 0x80;
539 const unsigned step
= 16;
542 for (i
= 0; i
< TILE_SIZE
; i
+= step
) {
543 for (j
= 0; j
< TILE_SIZE
; j
++) {
544 TILE_PIXEL(tile
, i
, j
, 0) = val
;
545 TILE_PIXEL(tile
, i
, j
, 1) = val
;
546 TILE_PIXEL(tile
, i
, j
, 2) = val
;
547 TILE_PIXEL(tile
, i
, j
, 3) = val
;
549 TILE_PIXEL(tile
, j
, i
, 0) = val
;
550 TILE_PIXEL(tile
, j
, i
, 1) = val
;
551 TILE_PIXEL(tile
, j
, i
, 2) = val
;
552 TILE_PIXEL(tile
, j
, i
, 3) = val
;
562 * Called when we're done writing to a color tile.
565 lp_rast_tile_end(struct lp_rasterizer_task
*task
)
568 struct lp_rasterizer
*rast
= task
->rast
;
571 for (buf
= 0; buf
< rast
->state
.nr_cbufs
; buf
++) {
572 uint8_t *color
= lp_rast_get_color_block_pointer(task
, buf
,
575 if (LP_DEBUG
& DEBUG_SHOW_SUBTILES
)
576 outline_subtiles(color
);
577 else if (LP_DEBUG
& DEBUG_SHOW_TILES
)
581 (void) outline_subtiles
;
585 memset(task
->color_tiles
, 0, sizeof(task
->color_tiles
));
586 task
->depth_tile
= NULL
;
592 * Signal on a fence. This is called during bin execution/rasterization.
596 lp_rast_fence(struct lp_rasterizer_task
*task
,
597 const union lp_rast_cmd_arg arg
)
599 struct lp_fence
*fence
= arg
.fence
;
600 lp_fence_signal(fence
);
607 * Rasterize commands for a single bin.
608 * \param x, y position of the bin's tile in the framebuffer
609 * Must be called between lp_rast_begin() and lp_rast_end().
613 rasterize_bin(struct lp_rasterizer_task
*task
,
614 const struct cmd_bin
*bin
,
617 const struct cmd_block_list
*commands
= &bin
->commands
;
618 struct cmd_block
*block
;
621 lp_rast_tile_begin( task
, x
* TILE_SIZE
, y
* TILE_SIZE
);
623 /* simply execute each of the commands in the block list */
624 for (block
= commands
->head
; block
; block
= block
->next
) {
625 for (k
= 0; k
< block
->count
; k
++) {
626 block
->cmd
[k
]( task
, block
->arg
[k
] );
630 lp_rast_tile_end(task
);
632 /* Free data for this bin.
634 lp_scene_bin_reset( task
->rast
->curr_scene
, x
, y
);
638 #define RAST(x) { lp_rast_##x, #x }
646 RAST(clear_zstencil
),
655 debug_bin( const struct cmd_bin
*bin
)
657 const struct cmd_block
*head
= bin
->commands
.head
;
660 for (i
= 0; i
< head
->count
; i
++) {
661 debug_printf("%d: ", i
);
662 for (j
= 0; j
< Elements(cmd_names
); j
++) {
663 if (head
->cmd
[i
] == cmd_names
[j
].cmd
) {
664 debug_printf("%s\n", cmd_names
[j
].name
);
668 if (j
== Elements(cmd_names
))
669 debug_printf("...other\n");
674 /* An empty bin is one that just loads the contents of the tile and
675 * stores them again unchanged. This typically happens when bins have
676 * been flushed for some reason in the middle of a frame, or when
677 * incremental updates are being made to a render target.
679 * Try to avoid doing pointless work in this case.
682 is_empty_bin( const struct cmd_bin
*bin
)
684 const struct cmd_block
*head
= bin
->commands
.head
;
690 /* We emit at most two load-tile commands at the start of the first
691 * command block. In addition we seem to emit a couple of
692 * set-state commands even in empty bins.
694 * As a heuristic, if a bin has more than 4 commands, consider it
697 if (head
->next
!= NULL
||
702 for (i
= 0; i
< head
->count
; i
++)
703 if (head
->cmd
[i
] != lp_rast_set_state
) {
713 * Rasterize/execute all bins within a scene.
717 rasterize_scene(struct lp_rasterizer_task
*task
,
718 struct lp_scene
*scene
)
720 /* loop over scene bins, rasterize each */
724 for (i
= 0; i
< scene
->tiles_x
; i
++) {
725 for (j
= 0; j
< scene
->tiles_y
; j
++) {
726 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, i
, j
);
727 rasterize_bin(task
, bin
, i
, j
);
737 while ((bin
= lp_scene_bin_iter_next(scene
, &x
, &y
))) {
738 if (!is_empty_bin( bin
))
739 rasterize_bin(task
, bin
, x
, y
);
747 * Called by setup module when it has something for us to render.
750 lp_rast_queue_scene( struct lp_rasterizer
*rast
,
751 struct lp_scene
*scene
)
753 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
755 if (rast
->num_threads
== 0) {
758 lp_rast_begin( rast
, scene
);
760 rasterize_scene( &rast
->tasks
[0], scene
);
762 lp_scene_reset( scene
);
766 rast
->curr_scene
= NULL
;
769 /* threaded rendering! */
772 lp_scene_enqueue( rast
->full_scenes
, scene
);
774 /* signal the threads that there's work to do */
775 for (i
= 0; i
< rast
->num_threads
; i
++) {
776 pipe_semaphore_signal(&rast
->tasks
[i
].work_ready
);
780 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
785 lp_rast_finish( struct lp_rasterizer
*rast
)
787 if (rast
->num_threads
== 0) {
793 /* wait for work to complete */
794 for (i
= 0; i
< rast
->num_threads
; i
++) {
795 pipe_semaphore_wait(&rast
->tasks
[i
].work_done
);
802 * This is the thread's main entrypoint.
803 * It's a simple loop:
806 * 3. signal that we're done
808 static PIPE_THREAD_ROUTINE( thread_func
, init_data
)
810 struct lp_rasterizer_task
*task
= (struct lp_rasterizer_task
*) init_data
;
811 struct lp_rasterizer
*rast
= task
->rast
;
812 boolean debug
= false;
817 debug_printf("thread %d waiting for work\n", task
->thread_index
);
818 pipe_semaphore_wait(&task
->work_ready
);
823 if (task
->thread_index
== 0) {
825 * - get next scene to rasterize
826 * - map the framebuffer surfaces
829 lp_scene_dequeue( rast
->full_scenes
, TRUE
) );
832 /* Wait for all threads to get here so that threads[1+] don't
833 * get a null rast->curr_scene pointer.
835 pipe_barrier_wait( &rast
->barrier
);
839 debug_printf("thread %d doing work\n", task
->thread_index
);
841 rasterize_scene(task
,
844 /* wait for all threads to finish with this scene */
845 pipe_barrier_wait( &rast
->barrier
);
847 /* XXX: shouldn't be necessary:
849 if (task
->thread_index
== 0) {
853 /* signal done with work */
855 debug_printf("thread %d done working\n", task
->thread_index
);
857 pipe_semaphore_signal(&task
->work_done
);
865 * Initialize semaphores and spawn the threads.
868 create_rast_threads(struct lp_rasterizer
*rast
)
872 /* NOTE: if num_threads is zero, we won't use any threads */
873 for (i
= 0; i
< rast
->num_threads
; i
++) {
874 pipe_semaphore_init(&rast
->tasks
[i
].work_ready
, 0);
875 pipe_semaphore_init(&rast
->tasks
[i
].work_done
, 0);
876 rast
->threads
[i
] = pipe_thread_create(thread_func
,
877 (void *) &rast
->tasks
[i
]);
884 * Create new lp_rasterizer.
885 * \param empty the queue to put empty scenes on after we've finished
888 struct lp_rasterizer
*
889 lp_rast_create( unsigned num_threads
)
891 struct lp_rasterizer
*rast
;
894 rast
= CALLOC_STRUCT(lp_rasterizer
);
898 rast
->full_scenes
= lp_scene_queue_create();
900 for (i
= 0; i
< Elements(rast
->tasks
); i
++) {
901 struct lp_rasterizer_task
*task
= &rast
->tasks
[i
];
903 task
->thread_index
= i
;
906 rast
->num_threads
= num_threads
;
908 create_rast_threads(rast
);
910 /* for synchronizing rasterization threads */
911 pipe_barrier_init( &rast
->barrier
, rast
->num_threads
);
919 void lp_rast_destroy( struct lp_rasterizer
*rast
)
923 /* Set exit_flag and signal each thread's work_ready semaphore.
924 * Each thread will be woken up, notice that the exit_flag is set and
925 * break out of its main loop. The thread will then exit.
927 rast
->exit_flag
= TRUE
;
928 for (i
= 0; i
< rast
->num_threads
; i
++) {
929 pipe_semaphore_signal(&rast
->tasks
[i
].work_ready
);
932 /* Wait for threads to terminate before cleaning up per-thread data */
933 for (i
= 0; i
< rast
->num_threads
; i
++) {
934 pipe_thread_wait(rast
->threads
[i
]);
937 /* Clean up per-thread data */
938 for (i
= 0; i
< rast
->num_threads
; i
++) {
939 pipe_semaphore_destroy(&rast
->tasks
[i
].work_ready
);
940 pipe_semaphore_destroy(&rast
->tasks
[i
].work_done
);
943 /* for synchronizing rasterization threads */
944 pipe_barrier_destroy( &rast
->barrier
);
950 /** Return number of rasterization threads */
952 lp_rast_get_num_threads( struct lp_rasterizer
*rast
)
954 return rast
->num_threads
;