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_depthstencil_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 struct lp_rast_clearzs
*clearzs
= arg
.clear_zstencil
;
273 unsigned clear_value
= clearzs
->clearzs_value
;
274 unsigned clear_mask
= clearzs
->clearzs_mask
;
275 const unsigned height
= TILE_SIZE
/ TILE_VECTOR_HEIGHT
;
276 const unsigned width
= TILE_SIZE
* TILE_VECTOR_HEIGHT
;
277 const unsigned block_size
= rast
->zsbuf
.blocksize
;
278 const unsigned dst_stride
= rast
->zsbuf
.stride
* TILE_VECTOR_HEIGHT
;
282 LP_DBG(DEBUG_RAST
, "%s 0x%x%x\n", __FUNCTION__
, clear_value
, clear_mask
);
285 * Clear the aera of the swizzled depth/depth buffer matching this tile, in
286 * stripes of TILE_VECTOR_HEIGHT x TILE_SIZE at a time.
288 * The swizzled depth format is such that the depths for
289 * TILE_VECTOR_HEIGHT x TILE_VECTOR_WIDTH pixels have consecutive offsets.
292 dst
= task
->depth_tile
;
294 assert(dst
== lp_rast_get_depth_block_pointer(rast
, task
->x
, task
->y
));
296 switch (block_size
) {
298 memset(dst
, (uint8_t) clear_value
, height
* width
);
301 for (i
= 0; i
< height
; i
++) {
302 uint16_t *row
= (uint16_t *)dst
;
303 for (j
= 0; j
< width
; j
++)
304 *row
++ = (uint16_t) clear_value
;
309 if (clear_mask
== 0xffffffff) {
310 for (i
= 0; i
< height
; i
++) {
311 uint32_t *row
= (uint32_t *)dst
;
312 for (j
= 0; j
< width
; j
++)
313 *row
++ = clear_value
;
318 for (i
= 0; i
< height
; i
++) {
319 uint32_t *row
= (uint32_t *)dst
;
320 for (j
= 0; j
< width
; j
++) {
321 uint32_t tmp
= ~clear_mask
& *row
;
322 *row
++ = (clear_value
& clear_mask
) | tmp
;
336 * Load tile color from the framebuffer surface.
337 * This is a bin command called during bin processing.
341 lp_rast_load_color(struct lp_rasterizer_task
*task
,
342 const union lp_rast_cmd_arg arg
)
344 struct lp_rasterizer
*rast
= task
->rast
;
346 enum lp_texture_usage usage
;
348 LP_DBG(DEBUG_RAST
, "%s at %u, %u\n", __FUNCTION__
, x
, y
);
350 if (scene
->has_color_clear
)
351 usage
= LP_TEX_USAGE_WRITE_ALL
;
353 usage
= LP_TEX_USAGE_READ_WRITE
;
355 /* Get pointers to color tile(s).
356 * This will convert linear data to tiled if needed.
358 for (buf
= 0; buf
< rast
->state
.nr_cbufs
; buf
++) {
359 struct pipe_surface
*cbuf
= rast
->curr_scene
->fb
.cbufs
[buf
];
360 struct llvmpipe_texture
*lpt
;
362 lpt
= llvmpipe_texture(cbuf
->texture
);
363 task
->color_tiles
[buf
] = llvmpipe_get_texture_tile(lpt
,
364 cbuf
->face
+ cbuf
->zslice
,
368 assert(task
->color_tiles
[buf
]);
375 * Convert the color tile from tiled to linear layout.
376 * This is generally only done when we're flushing the scene just prior to
377 * SwapBuffers. If we didn't do this here, we'd have to convert the entire
378 * tiled color buffer to linear layout in the llvmpipe_texture_unmap()
379 * function. It's better to do it here to take advantage of
380 * threading/parallelism.
381 * This is a bin command which is stored in all bins.
384 lp_rast_store_color( struct lp_rasterizer_task
*task
,
385 const union lp_rast_cmd_arg arg
)
387 struct lp_rasterizer
*rast
= task
->rast
;
388 struct lp_scene
*scene
= rast
->curr_scene
;
391 for (buf
= 0; buf
< rast
->state
.nr_cbufs
; buf
++) {
392 struct pipe_surface
*cbuf
= scene
->fb
.cbufs
[buf
];
393 const unsigned face
= cbuf
->face
, level
= cbuf
->level
;
394 struct llvmpipe_resource
*lpt
= llvmpipe_resource(cbuf
->texture
);
395 /* this will convert the tiled data to linear if needed */
396 (void) llvmpipe_get_texture_tile_linear(lpt
, face
, level
,
404 * This is a bin command called during bin processing.
407 lp_rast_set_state(struct lp_rasterizer_task
*task
,
408 const union lp_rast_cmd_arg arg
)
410 const struct lp_rast_state
*state
= arg
.set_state
;
412 LP_DBG(DEBUG_RAST
, "%s %p\n", __FUNCTION__
, (void *) state
);
414 /* just set the current state pointer for this rasterizer */
415 task
->current_state
= state
;
420 * Run the shader on all blocks in a tile. This is used when a tile is
421 * completely contained inside a triangle.
422 * This is a bin command called during bin processing.
425 lp_rast_shade_tile(struct lp_rasterizer_task
*task
,
426 const union lp_rast_cmd_arg arg
)
428 struct lp_rasterizer
*rast
= task
->rast
;
429 const struct lp_rast_state
*state
= task
->current_state
;
430 const struct lp_rast_shader_inputs
*inputs
= arg
.shade_tile
;
431 const unsigned tile_x
= task
->x
, tile_y
= task
->y
;
434 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
436 /* render the whole 64x64 tile in 4x4 chunks */
437 for (y
= 0; y
< TILE_SIZE
; y
+= 4){
438 for (x
= 0; x
< TILE_SIZE
; x
+= 4) {
439 uint8_t *color
[PIPE_MAX_COLOR_BUFS
];
444 for (i
= 0; i
< rast
->state
.nr_cbufs
; i
++)
445 color
[i
] = lp_rast_get_color_block_pointer(task
, i
,
446 tile_x
+ x
, tile_y
+ y
);
449 depth
= lp_rast_get_depth_block_pointer(rast
, tile_x
+ x
, tile_y
+ y
);
451 /* run shader on 4x4 block */
452 state
->jit_function
[RAST_WHOLE
]( &state
->jit_context
,
453 tile_x
+ x
, tile_y
+ y
,
460 INT_MIN
, INT_MIN
, INT_MIN
,
461 NULL
, NULL
, NULL
, &task
->vis_counter
);
468 * Compute shading for a 4x4 block of pixels.
469 * This is a bin command called during bin processing.
470 * \param x X position of quad in window coords
471 * \param y Y position of quad in window coords
473 void lp_rast_shade_quads( struct lp_rasterizer_task
*task
,
474 const struct lp_rast_shader_inputs
*inputs
,
475 unsigned x
, unsigned y
,
476 int32_t c1
, int32_t c2
, int32_t c3
)
478 const struct lp_rast_state
*state
= task
->current_state
;
479 struct lp_rasterizer
*rast
= task
->rast
;
480 uint8_t *color
[PIPE_MAX_COLOR_BUFS
];
487 assert(x
% TILE_VECTOR_WIDTH
== 0);
488 assert(y
% TILE_VECTOR_HEIGHT
== 0);
490 assert((x
% 4) == 0);
491 assert((y
% 4) == 0);
494 for (i
= 0; i
< rast
->state
.nr_cbufs
; i
++) {
495 color
[i
] = lp_rast_get_color_block_pointer(task
, i
, x
, y
);
496 assert(lp_check_alignment(color
[i
], 16));
500 depth
= lp_rast_get_depth_block_pointer(rast
, x
, y
);
503 assert(lp_check_alignment(state
->jit_context
.blend_color
, 16));
505 assert(lp_check_alignment(inputs
->step
[0], 16));
506 assert(lp_check_alignment(inputs
->step
[1], 16));
507 assert(lp_check_alignment(inputs
->step
[2], 16));
509 /* run shader on 4x4 block */
510 state
->jit_function
[RAST_EDGE_TEST
]( &state
->jit_context
,
527 * Set top row and left column of the tile's pixels to white. For debugging.
530 outline_tile(uint8_t *tile
)
532 const uint8_t val
= 0xff;
535 for (i
= 0; i
< TILE_SIZE
; i
++) {
536 TILE_PIXEL(tile
, i
, 0, 0) = val
;
537 TILE_PIXEL(tile
, i
, 0, 1) = val
;
538 TILE_PIXEL(tile
, i
, 0, 2) = val
;
539 TILE_PIXEL(tile
, i
, 0, 3) = val
;
541 TILE_PIXEL(tile
, 0, i
, 0) = val
;
542 TILE_PIXEL(tile
, 0, i
, 1) = val
;
543 TILE_PIXEL(tile
, 0, i
, 2) = val
;
544 TILE_PIXEL(tile
, 0, i
, 3) = val
;
550 * Draw grid of gray lines at 16-pixel intervals across the tile to
551 * show the sub-tile boundaries. For debugging.
554 outline_subtiles(uint8_t *tile
)
556 const uint8_t val
= 0x80;
557 const unsigned step
= 16;
560 for (i
= 0; i
< TILE_SIZE
; i
+= step
) {
561 for (j
= 0; j
< TILE_SIZE
; j
++) {
562 TILE_PIXEL(tile
, i
, j
, 0) = val
;
563 TILE_PIXEL(tile
, i
, j
, 1) = val
;
564 TILE_PIXEL(tile
, i
, j
, 2) = val
;
565 TILE_PIXEL(tile
, i
, j
, 3) = val
;
567 TILE_PIXEL(tile
, j
, i
, 0) = val
;
568 TILE_PIXEL(tile
, j
, i
, 1) = val
;
569 TILE_PIXEL(tile
, j
, i
, 2) = val
;
570 TILE_PIXEL(tile
, j
, i
, 3) = val
;
580 * Called when we're done writing to a color tile.
583 lp_rast_tile_end(struct lp_rasterizer_task
*task
)
586 struct lp_rasterizer
*rast
= task
->rast
;
589 for (buf
= 0; buf
< rast
->state
.nr_cbufs
; buf
++) {
590 uint8_t *color
= lp_rast_get_color_block_pointer(task
, buf
,
593 if (LP_DEBUG
& DEBUG_SHOW_SUBTILES
)
594 outline_subtiles(color
);
595 else if (LP_DEBUG
& DEBUG_SHOW_TILES
)
599 (void) outline_subtiles
;
603 memset(task
->color_tiles
, 0, sizeof(task
->color_tiles
));
604 task
->depth_tile
= NULL
;
610 * Signal on a fence. This is called during bin execution/rasterization.
614 lp_rast_fence(struct lp_rasterizer_task
*task
,
615 const union lp_rast_cmd_arg arg
)
617 struct lp_fence
*fence
= arg
.fence
;
618 lp_fence_signal(fence
);
623 * Begin a new occlusion query.
624 * This is a bin command put in all bins.
628 lp_rast_begin_query(struct lp_rasterizer_task
*task
,
629 const union lp_rast_cmd_arg arg
)
631 /* Reset the the per-task counter */
632 task
->vis_counter
= 0;
637 * End the current occlusion query.
638 * This is a bin command put in all bins.
642 lp_rast_end_query(struct lp_rasterizer_task
*task
,
643 const union lp_rast_cmd_arg arg
)
645 struct llvmpipe_query
*pq
= arg
.query_obj
;
647 pipe_mutex_lock(pq
->mutex
);
649 /* Accumulate the visible fragment counter from this tile in
652 pq
->count
[task
->thread_index
] += task
->vis_counter
;
654 /* check if this is the last tile in the scene */
656 if (pq
->tile_count
== pq
->num_tiles
) {
659 /* sum the per-thread counters for the query */
661 for (i
= 0; i
< LP_MAX_THREADS
; i
++) {
662 pq
->result
+= pq
->count
[i
];
665 /* reset counters (in case this query is re-used in the scene) */
666 memset(pq
->count
, 0, sizeof(pq
->count
));
673 pipe_mutex_unlock(pq
->mutex
);
679 * Rasterize commands for a single bin.
680 * \param x, y position of the bin's tile in the framebuffer
681 * Must be called between lp_rast_begin() and lp_rast_end().
685 rasterize_bin(struct lp_rasterizer_task
*task
,
686 const struct cmd_bin
*bin
,
689 const struct cmd_block_list
*commands
= &bin
->commands
;
690 struct cmd_block
*block
;
693 lp_rast_tile_begin( task
, x
* TILE_SIZE
, y
* TILE_SIZE
);
695 /* simply execute each of the commands in the block list */
696 for (block
= commands
->head
; block
; block
= block
->next
) {
697 for (k
= 0; k
< block
->count
; k
++) {
698 block
->cmd
[k
]( task
, block
->arg
[k
] );
702 lp_rast_tile_end(task
);
704 /* Free data for this bin.
706 lp_scene_bin_reset( task
->rast
->curr_scene
, x
, y
);
710 #define RAST(x) { lp_rast_##x, #x }
718 RAST(clear_zstencil
),
729 debug_bin( const struct cmd_bin
*bin
)
731 const struct cmd_block
*head
= bin
->commands
.head
;
734 for (i
= 0; i
< head
->count
; i
++) {
735 debug_printf("%d: ", i
);
736 for (j
= 0; j
< Elements(cmd_names
); j
++) {
737 if (head
->cmd
[i
] == cmd_names
[j
].cmd
) {
738 debug_printf("%s\n", cmd_names
[j
].name
);
742 if (j
== Elements(cmd_names
))
743 debug_printf("...other\n");
748 /* An empty bin is one that just loads the contents of the tile and
749 * stores them again unchanged. This typically happens when bins have
750 * been flushed for some reason in the middle of a frame, or when
751 * incremental updates are being made to a render target.
753 * Try to avoid doing pointless work in this case.
756 is_empty_bin( const struct cmd_bin
*bin
)
758 const struct cmd_block
*head
= bin
->commands
.head
;
764 /* We emit at most two load-tile commands at the start of the first
765 * command block. In addition we seem to emit a couple of
766 * set-state commands even in empty bins.
768 * As a heuristic, if a bin has more than 4 commands, consider it
771 if (head
->next
!= NULL
||
776 for (i
= 0; i
< head
->count
; i
++)
777 if (head
->cmd
[i
] != lp_rast_set_state
) {
787 * Rasterize/execute all bins within a scene.
791 rasterize_scene(struct lp_rasterizer_task
*task
,
792 struct lp_scene
*scene
)
794 /* loop over scene bins, rasterize each */
798 for (i
= 0; i
< scene
->tiles_x
; i
++) {
799 for (j
= 0; j
< scene
->tiles_y
; j
++) {
800 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, i
, j
);
801 rasterize_bin(task
, bin
, i
, j
);
811 while ((bin
= lp_scene_bin_iter_next(scene
, &x
, &y
))) {
812 if (!is_empty_bin( bin
))
813 rasterize_bin(task
, bin
, x
, y
);
821 * Called by setup module when it has something for us to render.
824 lp_rast_queue_scene( struct lp_rasterizer
*rast
,
825 struct lp_scene
*scene
)
827 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
829 if (rast
->num_threads
== 0) {
832 lp_rast_begin( rast
, scene
);
834 rasterize_scene( &rast
->tasks
[0], scene
);
836 lp_scene_reset( scene
);
840 rast
->curr_scene
= NULL
;
843 /* threaded rendering! */
846 lp_scene_enqueue( rast
->full_scenes
, scene
);
848 /* signal the threads that there's work to do */
849 for (i
= 0; i
< rast
->num_threads
; i
++) {
850 pipe_semaphore_signal(&rast
->tasks
[i
].work_ready
);
854 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
859 lp_rast_finish( struct lp_rasterizer
*rast
)
861 if (rast
->num_threads
== 0) {
867 /* wait for work to complete */
868 for (i
= 0; i
< rast
->num_threads
; i
++) {
869 pipe_semaphore_wait(&rast
->tasks
[i
].work_done
);
876 * This is the thread's main entrypoint.
877 * It's a simple loop:
880 * 3. signal that we're done
882 static PIPE_THREAD_ROUTINE( thread_func
, init_data
)
884 struct lp_rasterizer_task
*task
= (struct lp_rasterizer_task
*) init_data
;
885 struct lp_rasterizer
*rast
= task
->rast
;
886 boolean debug
= false;
891 debug_printf("thread %d waiting for work\n", task
->thread_index
);
892 pipe_semaphore_wait(&task
->work_ready
);
897 if (task
->thread_index
== 0) {
899 * - get next scene to rasterize
900 * - map the framebuffer surfaces
903 lp_scene_dequeue( rast
->full_scenes
, TRUE
) );
906 /* Wait for all threads to get here so that threads[1+] don't
907 * get a null rast->curr_scene pointer.
909 pipe_barrier_wait( &rast
->barrier
);
913 debug_printf("thread %d doing work\n", task
->thread_index
);
915 rasterize_scene(task
,
918 /* wait for all threads to finish with this scene */
919 pipe_barrier_wait( &rast
->barrier
);
921 /* XXX: shouldn't be necessary:
923 if (task
->thread_index
== 0) {
927 /* signal done with work */
929 debug_printf("thread %d done working\n", task
->thread_index
);
931 pipe_semaphore_signal(&task
->work_done
);
939 * Initialize semaphores and spawn the threads.
942 create_rast_threads(struct lp_rasterizer
*rast
)
946 /* NOTE: if num_threads is zero, we won't use any threads */
947 for (i
= 0; i
< rast
->num_threads
; i
++) {
948 pipe_semaphore_init(&rast
->tasks
[i
].work_ready
, 0);
949 pipe_semaphore_init(&rast
->tasks
[i
].work_done
, 0);
950 rast
->threads
[i
] = pipe_thread_create(thread_func
,
951 (void *) &rast
->tasks
[i
]);
958 * Create new lp_rasterizer. If num_threads is zero, don't create any
959 * new threads, do rendering synchronously.
960 * \param num_threads number of rasterizer threads to create
962 struct lp_rasterizer
*
963 lp_rast_create( unsigned num_threads
)
965 struct lp_rasterizer
*rast
;
968 rast
= CALLOC_STRUCT(lp_rasterizer
);
972 rast
->full_scenes
= lp_scene_queue_create();
974 for (i
= 0; i
< Elements(rast
->tasks
); i
++) {
975 struct lp_rasterizer_task
*task
= &rast
->tasks
[i
];
977 task
->thread_index
= i
;
980 rast
->num_threads
= num_threads
;
982 create_rast_threads(rast
);
984 /* for synchronizing rasterization threads */
985 pipe_barrier_init( &rast
->barrier
, rast
->num_threads
);
993 void lp_rast_destroy( struct lp_rasterizer
*rast
)
997 /* Set exit_flag and signal each thread's work_ready semaphore.
998 * Each thread will be woken up, notice that the exit_flag is set and
999 * break out of its main loop. The thread will then exit.
1001 rast
->exit_flag
= TRUE
;
1002 for (i
= 0; i
< rast
->num_threads
; i
++) {
1003 pipe_semaphore_signal(&rast
->tasks
[i
].work_ready
);
1006 /* Wait for threads to terminate before cleaning up per-thread data */
1007 for (i
= 0; i
< rast
->num_threads
; i
++) {
1008 pipe_thread_wait(rast
->threads
[i
]);
1011 /* Clean up per-thread data */
1012 for (i
= 0; i
< rast
->num_threads
; i
++) {
1013 pipe_semaphore_destroy(&rast
->tasks
[i
].work_ready
);
1014 pipe_semaphore_destroy(&rast
->tasks
[i
].work_done
);
1017 /* for synchronizing rasterization threads */
1018 pipe_barrier_destroy( &rast
->barrier
);
1020 lp_scene_queue_destroy(rast
->full_scenes
);
1026 /** Return number of rasterization threads */
1028 lp_rast_get_num_threads( struct lp_rasterizer
*rast
)
1030 return rast
->num_threads
;