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 the rasterization phase.
47 * Map the framebuffer surfaces. Initialize the 'rast' state.
50 lp_rast_begin( struct lp_rasterizer
*rast
,
51 const struct pipe_framebuffer_state
*fb
,
53 boolean write_zstencil
)
55 struct pipe_screen
*screen
= rast
->screen
;
56 struct pipe_surface
*cbuf
, *zsbuf
;
59 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
61 util_copy_framebuffer_state(&rast
->state
.fb
, fb
);
63 rast
->state
.write_zstencil
= write_zstencil
;
64 rast
->state
.write_color
= write_color
;
66 rast
->check_for_clipped_tiles
= (fb
->width
% TILE_SIZE
!= 0 ||
67 fb
->height
% TILE_SIZE
!= 0);
70 for (i
= 0; i
< rast
->state
.fb
.nr_cbufs
; i
++) {
71 cbuf
= rast
->state
.fb
.cbufs
[i
];
73 rast
->cbuf_transfer
[i
] = screen
->get_tex_transfer(rast
->screen
,
78 PIPE_TRANSFER_READ_WRITE
,
82 if (!rast
->cbuf_transfer
[i
])
85 rast
->cbuf_map
[i
] = screen
->transfer_map(rast
->screen
,
86 rast
->cbuf_transfer
[i
]);
87 if (!rast
->cbuf_map
[i
])
92 zsbuf
= rast
->state
.fb
.zsbuf
;
94 rast
->zsbuf_transfer
= screen
->get_tex_transfer(rast
->screen
,
99 PIPE_TRANSFER_READ_WRITE
,
103 if (!rast
->zsbuf_transfer
)
106 rast
->zsbuf_map
= screen
->transfer_map(rast
->screen
,
107 rast
->zsbuf_transfer
);
108 if (!rast
->zsbuf_map
)
115 /* Unmap and release transfers?
122 * Finish the rasterization phase.
123 * Unmap framebuffer surfaces.
126 lp_rast_end( struct lp_rasterizer
*rast
)
128 struct pipe_screen
*screen
= rast
->screen
;
131 for (i
= 0; i
< rast
->state
.fb
.nr_cbufs
; i
++) {
132 if (rast
->cbuf_map
[i
])
133 screen
->transfer_unmap(screen
, rast
->cbuf_transfer
[i
]);
135 if (rast
->cbuf_transfer
[i
])
136 screen
->tex_transfer_destroy(rast
->cbuf_transfer
[i
]);
138 rast
->cbuf_transfer
[i
] = NULL
;
139 rast
->cbuf_map
[i
] = NULL
;
143 screen
->transfer_unmap(screen
, rast
->zsbuf_transfer
);
145 if (rast
->zsbuf_transfer
)
146 screen
->tex_transfer_destroy(rast
->zsbuf_transfer
);
148 rast
->zsbuf_transfer
= NULL
;
149 rast
->zsbuf_map
= NULL
;
154 * Begining rasterization of a tile.
155 * \param x window X position of the tile, in pixels
156 * \param y window Y position of the tile, in pixels
159 lp_rast_start_tile( struct lp_rasterizer
*rast
,
160 unsigned thread_index
,
161 unsigned x
, unsigned y
)
163 LP_DBG(DEBUG_RAST
, "%s %d,%d\n", __FUNCTION__
, x
, y
);
165 rast
->tasks
[thread_index
].x
= x
;
166 rast
->tasks
[thread_index
].y
= y
;
171 * Clear the rasterizer's current color tile.
172 * This is a bin command called during bin processing.
174 void lp_rast_clear_color( struct lp_rasterizer
*rast
,
175 unsigned thread_index
,
176 const union lp_rast_cmd_arg arg
)
178 const uint8_t *clear_color
= arg
.clear_color
;
179 uint8_t **color_tile
= rast
->tasks
[thread_index
].tile
.color
;
182 LP_DBG(DEBUG_RAST
, "%s 0x%x,0x%x,0x%x,0x%x\n", __FUNCTION__
,
188 if (clear_color
[0] == clear_color
[1] &&
189 clear_color
[1] == clear_color
[2] &&
190 clear_color
[2] == clear_color
[3]) {
191 /* clear to grayscale value {x, x, x, x} */
192 for (i
= 0; i
< rast
->state
.fb
.nr_cbufs
; i
++) {
193 memset(color_tile
[i
], clear_color
[0], TILE_SIZE
* TILE_SIZE
* 4);
198 * Note: if the swizzled tile layout changes (see TILE_PIXEL) this code
199 * will need to change. It'll be pretty obvious when clearing no longer
202 const unsigned chunk
= TILE_SIZE
/ 4;
203 for (i
= 0; i
< rast
->state
.fb
.nr_cbufs
; i
++) {
204 uint8_t *c
= color_tile
[i
];
206 for (j
= 0; j
< 4 * TILE_SIZE
; j
++) {
207 memset(c
, clear_color
[0], chunk
);
209 memset(c
, clear_color
[1], chunk
);
211 memset(c
, clear_color
[2], chunk
);
213 memset(c
, clear_color
[3], chunk
);
216 assert(c
- color_tile
[i
] == TILE_SIZE
* TILE_SIZE
* 4);
220 LP_COUNT(nr_color_tile_clear
);
225 * Clear the rasterizer's current z/stencil tile.
226 * This is a bin command called during bin processing.
228 void lp_rast_clear_zstencil( struct lp_rasterizer
*rast
,
229 unsigned thread_index
,
230 const union lp_rast_cmd_arg arg
)
232 struct lp_rasterizer_task
*task
= &rast
->tasks
[thread_index
];
233 const unsigned tile_x
= task
->x
;
234 const unsigned tile_y
= task
->y
;
235 const unsigned height
= TILE_SIZE
/TILE_VECTOR_HEIGHT
;
236 const unsigned width
= TILE_SIZE
*TILE_VECTOR_HEIGHT
;
237 unsigned block_size
= util_format_get_blocksize(rast
->zsbuf_transfer
->texture
->format
);
239 unsigned dst_stride
= rast
->zsbuf_transfer
->stride
*TILE_VECTOR_HEIGHT
;
242 LP_DBG(DEBUG_RAST
, "%s 0x%x\n", __FUNCTION__
, arg
.clear_zstencil
);
244 assert(rast
->zsbuf_map
);
245 if (!rast
->zsbuf_map
)
248 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
251 * Clear the aera of the swizzled depth/depth buffer matching this tile, in
252 * stripes of TILE_VECTOR_HEIGHT x TILE_SIZE at a time.
254 * The swizzled depth format is such that the depths for
255 * TILE_VECTOR_HEIGHT x TILE_VECTOR_WIDTH pixels have consecutive offsets.
258 dst
= lp_rast_depth_pointer(rast
, tile_x
, tile_y
);
260 switch (block_size
) {
262 memset(dst
, (uint8_t) arg
.clear_zstencil
, height
* width
);
265 for (i
= 0; i
< height
; i
++) {
266 uint16_t *row
= (uint16_t *)dst
;
267 for (j
= 0; j
< width
; j
++)
268 *row
++ = (uint16_t) arg
.clear_zstencil
;
273 for (i
= 0; i
< height
; i
++) {
274 uint32_t *row
= (uint32_t *)dst
;
275 for (j
= 0; j
< width
; j
++)
276 *row
++ = arg
.clear_zstencil
;
288 * Load tile color from the framebuffer surface.
289 * This is a bin command called during bin processing.
291 void lp_rast_load_color( struct lp_rasterizer
*rast
,
292 unsigned thread_index
,
293 const union lp_rast_cmd_arg arg
)
295 struct lp_rasterizer_task
*task
= &rast
->tasks
[thread_index
];
296 const unsigned x
= task
->x
;
297 const unsigned y
= task
->y
;
300 LP_DBG(DEBUG_RAST
, "%s at %u, %u\n", __FUNCTION__
, x
, y
);
302 for (i
= 0; i
< rast
->state
.fb
.nr_cbufs
; i
++) {
303 struct pipe_transfer
*transfer
= rast
->cbuf_transfer
[i
];
307 if (x
>= transfer
->width
)
310 if (y
>= transfer
->height
)
315 assert(w
<= TILE_SIZE
);
316 assert(h
<= TILE_SIZE
);
318 lp_tile_read_4ub(transfer
->texture
->format
,
325 LP_COUNT(nr_color_tile_load
);
330 void lp_rast_set_state( struct lp_rasterizer
*rast
,
331 unsigned thread_index
,
332 const union lp_rast_cmd_arg arg
)
334 const struct lp_rast_state
*state
= arg
.set_state
;
336 LP_DBG(DEBUG_RAST
, "%s %p\n", __FUNCTION__
, (void *) state
);
338 /* just set the current state pointer for this rasterizer */
339 rast
->tasks
[thread_index
].current_state
= state
;
345 * Run the shader on all blocks in a tile. This is used when a tile is
346 * completely contained inside a triangle.
347 * This is a bin command called during bin processing.
349 void lp_rast_shade_tile( struct lp_rasterizer
*rast
,
350 unsigned thread_index
,
351 const union lp_rast_cmd_arg arg
)
353 struct lp_rasterizer_task
*task
= &rast
->tasks
[thread_index
];
354 const struct lp_rast_state
*state
= task
->current_state
;
355 struct lp_rast_tile
*tile
= &task
->tile
;
356 const struct lp_rast_shader_inputs
*inputs
= arg
.shade_tile
;
357 const unsigned tile_x
= task
->x
;
358 const unsigned tile_y
= task
->y
;
361 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
363 /* render the whole 64x64 tile in 4x4 chunks */
364 for (y
= 0; y
< TILE_SIZE
; y
+= 4){
365 for (x
= 0; x
< TILE_SIZE
; x
+= 4) {
366 uint8_t *color
[PIPE_MAX_COLOR_BUFS
];
368 unsigned block_offset
, i
;
370 /* offset of the 16x16 pixel block within the tile */
371 block_offset
= ((y
/ 4) * (16 * 16) + (x
/ 4) * 16);
374 for (i
= 0; i
< rast
->state
.fb
.nr_cbufs
; i
++)
375 color
[i
] = tile
->color
[i
] + 4 * block_offset
;
378 depth
= lp_rast_depth_pointer(rast
, tile_x
+ x
, tile_y
+ y
);
381 state
->jit_function
[0]( &state
->jit_context
,
382 tile_x
+ x
, tile_y
+ y
,
388 INT_MIN
, INT_MIN
, INT_MIN
,
396 * Compute shading for a 4x4 block of pixels.
397 * This is a bin command called during bin processing.
399 void lp_rast_shade_quads( struct lp_rasterizer
*rast
,
400 unsigned thread_index
,
401 const struct lp_rast_shader_inputs
*inputs
,
402 unsigned x
, unsigned y
,
403 int32_t c1
, int32_t c2
, int32_t c3
)
405 struct lp_rasterizer_task
*task
= &rast
->tasks
[thread_index
];
406 const struct lp_rast_state
*state
= task
->current_state
;
407 struct lp_rast_tile
*tile
= &task
->tile
;
408 uint8_t *color
[PIPE_MAX_COLOR_BUFS
];
418 assert(x
% TILE_VECTOR_WIDTH
== 0);
419 assert(y
% TILE_VECTOR_HEIGHT
== 0);
421 assert((x
% 4) == 0);
422 assert((y
% 4) == 0);
428 /* offset of the 16x16 pixel block within the tile */
429 block_offset
= ((iy
/ 4) * (16 * 16) + (ix
/ 4) * 16);
432 for (i
= 0; i
< rast
->state
.fb
.nr_cbufs
; i
++)
433 color
[i
] = tile
->color
[i
] + 4 * block_offset
;
436 depth
= lp_rast_depth_pointer(rast
, x
, y
);
441 assert(lp_check_alignment(tile
->color
[0], 16));
442 assert(lp_check_alignment(state
->jit_context
.blend_color
, 16));
444 assert(lp_check_alignment(inputs
->step
[0], 16));
445 assert(lp_check_alignment(inputs
->step
[1], 16));
446 assert(lp_check_alignment(inputs
->step
[2], 16));
450 state
->jit_function
[1]( &state
->jit_context
,
458 inputs
->step
[0], inputs
->step
[1], inputs
->step
[2]);
463 * Set top row and left column of the tile's pixels to white. For debugging.
466 outline_tile(uint8_t *tile
)
468 const uint8_t val
= 0xff;
471 for (i
= 0; i
< TILE_SIZE
; i
++) {
472 TILE_PIXEL(tile
, i
, 0, 0) = val
;
473 TILE_PIXEL(tile
, i
, 0, 1) = val
;
474 TILE_PIXEL(tile
, i
, 0, 2) = val
;
475 TILE_PIXEL(tile
, i
, 0, 3) = val
;
477 TILE_PIXEL(tile
, 0, i
, 0) = val
;
478 TILE_PIXEL(tile
, 0, i
, 1) = val
;
479 TILE_PIXEL(tile
, 0, i
, 2) = val
;
480 TILE_PIXEL(tile
, 0, i
, 3) = val
;
486 * Draw grid of gray lines at 16-pixel intervals across the tile to
487 * show the sub-tile boundaries. For debugging.
490 outline_subtiles(uint8_t *tile
)
492 const uint8_t val
= 0x80;
493 const unsigned step
= 16;
496 for (i
= 0; i
< TILE_SIZE
; i
+= step
) {
497 for (j
= 0; j
< TILE_SIZE
; j
++) {
498 TILE_PIXEL(tile
, i
, j
, 0) = val
;
499 TILE_PIXEL(tile
, i
, j
, 1) = val
;
500 TILE_PIXEL(tile
, i
, j
, 2) = val
;
501 TILE_PIXEL(tile
, i
, j
, 3) = val
;
503 TILE_PIXEL(tile
, j
, i
, 0) = val
;
504 TILE_PIXEL(tile
, j
, i
, 1) = val
;
505 TILE_PIXEL(tile
, j
, i
, 2) = val
;
506 TILE_PIXEL(tile
, j
, i
, 3) = val
;
516 * Write the rasterizer's color tile to the framebuffer.
518 static void lp_rast_store_color( struct lp_rasterizer
*rast
,
519 unsigned thread_index
)
521 struct lp_rasterizer_task
*task
= &rast
->tasks
[thread_index
];
522 const unsigned x
= task
->x
;
523 const unsigned y
= task
->y
;
526 for (i
= 0; i
< rast
->state
.fb
.nr_cbufs
; i
++) {
527 struct pipe_transfer
*transfer
= rast
->cbuf_transfer
[i
];
531 if (x
>= transfer
->width
)
534 if (y
>= transfer
->height
)
537 LP_DBG(DEBUG_RAST
, "%s [%u] %d,%d %dx%d\n", __FUNCTION__
,
538 thread_index
, x
, y
, w
, h
);
540 if (LP_DEBUG
& DEBUG_SHOW_SUBTILES
)
541 outline_subtiles(task
->tile
.color
[i
]);
542 else if (LP_DEBUG
& DEBUG_SHOW_TILES
)
543 outline_tile(task
->tile
.color
[i
]);
545 lp_tile_write_4ub(transfer
->texture
->format
,
552 LP_COUNT(nr_color_tile_store
);
558 * Write the rasterizer's tiles to the framebuffer.
561 lp_rast_end_tile( struct lp_rasterizer
*rast
,
562 unsigned thread_index
)
564 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
566 if (rast
->state
.write_color
)
567 lp_rast_store_color(rast
, thread_index
);
572 * Signal on a fence. This is called during bin execution/rasterization.
575 void lp_rast_fence( struct lp_rasterizer
*rast
,
576 unsigned thread_index
,
577 const union lp_rast_cmd_arg arg
)
579 struct lp_fence
*fence
= arg
.fence
;
581 pipe_mutex_lock( fence
->mutex
);
584 assert(fence
->count
<= fence
->rank
);
586 LP_DBG(DEBUG_RAST
, "%s count=%u rank=%u\n", __FUNCTION__
,
587 fence
->count
, fence
->rank
);
589 pipe_condvar_signal( fence
->signalled
);
591 pipe_mutex_unlock( fence
->mutex
);
596 * When all the threads are done rasterizing a scene, one thread will
597 * call this function to reset the scene and put it onto the empty queue.
600 release_scene( struct lp_rasterizer
*rast
,
601 struct lp_scene
*scene
)
603 util_unreference_framebuffer_state( &scene
->fb
);
605 lp_scene_reset( scene
);
606 lp_scene_enqueue( rast
->empty_scenes
, scene
);
607 rast
->curr_scene
= NULL
;
612 * Rasterize commands for a single bin.
613 * \param x, y position of the bin's tile in the framebuffer
614 * Must be called between lp_rast_begin() and lp_rast_end().
618 rasterize_bin( struct lp_rasterizer
*rast
,
619 unsigned thread_index
,
620 const struct cmd_bin
*bin
,
623 const struct cmd_block_list
*commands
= &bin
->commands
;
624 struct cmd_block
*block
;
627 lp_rast_start_tile( rast
, thread_index
, x
, y
);
629 /* simply execute each of the commands in the block list */
630 for (block
= commands
->head
; block
; block
= block
->next
) {
631 for (k
= 0; k
< block
->count
; k
++) {
632 block
->cmd
[k
]( rast
, thread_index
, block
->arg
[k
] );
636 lp_rast_end_tile( rast
, thread_index
);
640 #define RAST(x) { lp_rast_##x, #x }
649 RAST(clear_zstencil
),
657 debug_bin( const struct cmd_bin
*bin
)
659 const struct cmd_block
*head
= bin
->commands
.head
;
662 for (i
= 0; i
< head
->count
; i
++) {
663 debug_printf("%d: ", i
);
664 for (j
= 0; j
< Elements(cmd_names
); j
++) {
665 if (head
->cmd
[i
] == cmd_names
[j
].cmd
) {
666 debug_printf("%s\n", cmd_names
[j
].name
);
670 if (j
== Elements(cmd_names
))
671 debug_printf("...other\n");
676 /* An empty bin is one that just loads the contents of the tile and
677 * stores them again unchanged. This typically happens when bins have
678 * been flushed for some reason in the middle of a frame, or when
679 * incremental updates are being made to a render target.
681 * Try to avoid doing pointless work in this case.
684 is_empty_bin( const struct cmd_bin
*bin
)
686 const struct cmd_block
*head
= bin
->commands
.head
;
692 /* We emit at most two load-tile commands at the start of the first
693 * command block. In addition we seem to emit a couple of
694 * set-state commands even in empty bins.
696 * As a heuristic, if a bin has more than 4 commands, consider it
699 if (head
->next
!= NULL
||
704 for (i
= 0; i
< head
->count
; i
++)
705 if (head
->cmd
[i
] != lp_rast_load_color
&&
706 head
->cmd
[i
] != lp_rast_set_state
) {
716 * Rasterize/execute all bins within a scene.
720 rasterize_scene( struct lp_rasterizer
*rast
,
721 unsigned thread_index
,
722 struct lp_scene
*scene
,
725 /* loop over scene bins, rasterize each */
729 for (i
= 0; i
< scene
->tiles_x
; i
++) {
730 for (j
= 0; j
< scene
->tiles_y
; j
++) {
731 struct cmd_bin
*bin
= lp_get_bin(scene
, i
, j
);
732 rasterize_bin( rast
, thread_index
,
733 bin
, i
* TILE_SIZE
, j
* TILE_SIZE
);
743 while ((bin
= lp_scene_bin_iter_next(scene
, &x
, &y
))) {
744 if (!is_empty_bin( bin
))
745 rasterize_bin( rast
, thread_index
, bin
, x
* TILE_SIZE
, y
* TILE_SIZE
);
753 * Called by setup module when it has something for us to render.
756 lp_rasterize_scene( struct lp_rasterizer
*rast
,
757 struct lp_scene
*scene
,
758 const struct pipe_framebuffer_state
*fb
,
761 boolean debug
= false;
763 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
767 debug_printf("rasterize scene:\n");
768 debug_printf(" data size: %u\n", lp_scene_data_size(scene
));
769 for (y
= 0; y
< scene
->tiles_y
; y
++) {
770 for (x
= 0; x
< scene
->tiles_x
; x
++) {
771 debug_printf(" bin %u, %u size: %u\n", x
, y
,
772 lp_scene_bin_size(scene
, x
, y
));
777 /* save framebuffer state in the bin */
778 util_copy_framebuffer_state(&scene
->fb
, fb
);
779 scene
->write_depth
= write_depth
;
781 if (rast
->num_threads
== 0) {
784 lp_rast_begin( rast
, fb
,
785 fb
->nr_cbufs
!= 0, /* always write color if cbufs present */
786 fb
->zsbuf
!= NULL
&& write_depth
);
788 lp_scene_bin_iter_begin( scene
);
789 rasterize_scene( rast
, 0, scene
, write_depth
);
791 release_scene( rast
, scene
);
796 /* threaded rendering! */
799 lp_scene_enqueue( rast
->full_scenes
, scene
);
801 /* signal the threads that there's work to do */
802 for (i
= 0; i
< rast
->num_threads
; i
++) {
803 pipe_semaphore_signal(&rast
->tasks
[i
].work_ready
);
806 /* wait for work to complete */
807 for (i
= 0; i
< rast
->num_threads
; i
++) {
808 pipe_semaphore_wait(&rast
->tasks
[i
].work_done
);
812 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
817 * This is the thread's main entrypoint.
818 * It's a simple loop:
821 * 3. signal that we're done
823 static PIPE_THREAD_ROUTINE( thread_func
, init_data
)
825 struct lp_rasterizer_task
*task
= (struct lp_rasterizer_task
*) init_data
;
826 struct lp_rasterizer
*rast
= task
->rast
;
827 boolean debug
= false;
832 debug_printf("thread %d waiting for work\n", task
->thread_index
);
833 pipe_semaphore_wait(&task
->work_ready
);
835 if (task
->thread_index
== 0) {
837 * - get next scene to rasterize
838 * - map the framebuffer surfaces
840 const struct pipe_framebuffer_state
*fb
;
843 rast
->curr_scene
= lp_scene_dequeue( rast
->full_scenes
, TRUE
);
845 lp_scene_bin_iter_begin( rast
->curr_scene
);
847 fb
= &rast
->curr_scene
->fb
;
848 write_depth
= rast
->curr_scene
->write_depth
;
850 lp_rast_begin( rast
, fb
,
852 fb
->zsbuf
!= NULL
&& write_depth
);
855 /* Wait for all threads to get here so that threads[1+] don't
856 * get a null rast->curr_scene pointer.
858 pipe_barrier_wait( &rast
->barrier
);
862 debug_printf("thread %d doing work\n", task
->thread_index
);
863 rasterize_scene(rast
,
866 rast
->curr_scene
->write_depth
);
868 /* wait for all threads to finish with this scene */
869 pipe_barrier_wait( &rast
->barrier
);
871 if (task
->thread_index
== 0) {
873 * - release the scene object
874 * - unmap the framebuffer surfaces
876 release_scene( rast
, rast
->curr_scene
);
880 /* signal done with work */
882 debug_printf("thread %d done working\n", task
->thread_index
);
883 pipe_semaphore_signal(&task
->work_done
);
891 * Initialize semaphores and spawn the threads.
894 create_rast_threads(struct lp_rasterizer
*rast
)
898 #ifdef PIPE_OS_WINDOWS
899 /* Multithreading not supported on windows until conditions and barriers are
900 * properly implemented. */
901 rast
->num_threads
= 0;
903 rast
->num_threads
= util_cpu_caps
.nr_cpus
;
904 rast
->num_threads
= debug_get_num_option("LP_NUM_THREADS", rast
->num_threads
);
905 rast
->num_threads
= MIN2(rast
->num_threads
, MAX_THREADS
);
908 /* NOTE: if num_threads is zero, we won't use any threads */
909 for (i
= 0; i
< rast
->num_threads
; i
++) {
910 pipe_semaphore_init(&rast
->tasks
[i
].work_ready
, 0);
911 pipe_semaphore_init(&rast
->tasks
[i
].work_done
, 0);
912 rast
->threads
[i
] = pipe_thread_create(thread_func
,
913 (void *) &rast
->tasks
[i
]);
920 * Create new lp_rasterizer.
921 * \param empty the queue to put empty scenes on after we've finished
924 struct lp_rasterizer
*
925 lp_rast_create( struct pipe_screen
*screen
, struct lp_scene_queue
*empty
)
927 struct lp_rasterizer
*rast
;
930 rast
= CALLOC_STRUCT(lp_rasterizer
);
934 rast
->screen
= screen
;
936 rast
->empty_scenes
= empty
;
937 rast
->full_scenes
= lp_scene_queue_create();
939 for (i
= 0; i
< Elements(rast
->tasks
); i
++) {
940 struct lp_rasterizer_task
*task
= &rast
->tasks
[i
];
942 for (cbuf
= 0; cbuf
< PIPE_MAX_COLOR_BUFS
; cbuf
++ )
943 task
->tile
.color
[cbuf
] = align_malloc(TILE_SIZE
* TILE_SIZE
* 4, 16);
946 task
->thread_index
= i
;
949 create_rast_threads(rast
);
951 /* for synchronizing rasterization threads */
952 pipe_barrier_init( &rast
->barrier
, rast
->num_threads
);
960 void lp_rast_destroy( struct lp_rasterizer
*rast
)
964 util_unreference_framebuffer_state(&rast
->state
.fb
);
966 for (i
= 0; i
< Elements(rast
->tasks
); i
++) {
967 for (cbuf
= 0; cbuf
< PIPE_MAX_COLOR_BUFS
; cbuf
++ )
968 align_free(rast
->tasks
[i
].tile
.color
[cbuf
]);
971 /* for synchronizing rasterization threads */
972 pipe_barrier_destroy( &rast
->barrier
);
978 /** Return number of rasterization threads */
980 lp_rast_get_num_threads( struct lp_rasterizer
*rast
)
982 return rast
->num_threads
;