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"
33 #include "util/u_pack_color.h"
34 #include "util/u_string.h"
35 #include "util/u_thread.h"
36 #include "util/u_memset.h"
37 #include "util/os_time.h"
39 #include "lp_scene_queue.h"
40 #include "lp_context.h"
46 #include "lp_rast_priv.h"
47 #include "gallivm/lp_bld_format.h"
48 #include "gallivm/lp_bld_debug.h"
50 #include "lp_tex_sample.h"
55 const struct lp_rast_state
*jit_state
= NULL
;
56 const struct lp_rasterizer_task
*jit_task
= NULL
;
59 const float lp_sample_pos_4x
[4][2] = { { 0.375, 0.125 },
65 * Begin rasterizing a scene.
66 * Called once per scene by one thread.
69 lp_rast_begin( struct lp_rasterizer
*rast
,
70 struct lp_scene
*scene
)
72 rast
->curr_scene
= scene
;
74 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
76 lp_scene_begin_rasterization( scene
);
77 lp_scene_bin_iter_begin( scene
);
82 lp_rast_end( struct lp_rasterizer
*rast
)
84 lp_scene_end_rasterization( rast
->curr_scene
);
86 rast
->curr_scene
= NULL
;
91 * Beginning rasterization of a tile.
92 * \param x window X position of the tile, in pixels
93 * \param y window Y position of the tile, in pixels
96 lp_rast_tile_begin(struct lp_rasterizer_task
*task
,
97 const struct cmd_bin
*bin
,
101 struct lp_scene
*scene
= task
->scene
;
103 LP_DBG(DEBUG_RAST
, "%s %d,%d\n", __FUNCTION__
, x
, y
);
106 task
->x
= x
* TILE_SIZE
;
107 task
->y
= y
* TILE_SIZE
;
108 task
->width
= TILE_SIZE
+ x
* TILE_SIZE
> task
->scene
->fb
.width
?
109 task
->scene
->fb
.width
- x
* TILE_SIZE
: TILE_SIZE
;
110 task
->height
= TILE_SIZE
+ y
* TILE_SIZE
> task
->scene
->fb
.height
?
111 task
->scene
->fb
.height
- y
* TILE_SIZE
: TILE_SIZE
;
113 task
->thread_data
.vis_counter
= 0;
114 task
->thread_data
.ps_invocations
= 0;
116 for (i
= 0; i
< task
->scene
->fb
.nr_cbufs
; i
++) {
117 if (task
->scene
->fb
.cbufs
[i
]) {
118 task
->color_tiles
[i
] = scene
->cbufs
[i
].map
+
119 scene
->cbufs
[i
].stride
* task
->y
+
120 scene
->cbufs
[i
].format_bytes
* task
->x
;
123 if (task
->scene
->fb
.zsbuf
) {
124 task
->depth_tile
= scene
->zsbuf
.map
+
125 scene
->zsbuf
.stride
* task
->y
+
126 scene
->zsbuf
.format_bytes
* task
->x
;
132 * Clear the rasterizer's current color tile.
133 * This is a bin command called during bin processing.
134 * Clear commands always clear all bound layers.
137 lp_rast_clear_color(struct lp_rasterizer_task
*task
,
138 const union lp_rast_cmd_arg arg
)
140 const struct lp_scene
*scene
= task
->scene
;
141 unsigned cbuf
= arg
.clear_rb
->cbuf
;
143 enum pipe_format format
;
145 /* we never bin clear commands for non-existing buffers */
146 assert(cbuf
< scene
->fb
.nr_cbufs
);
147 assert(scene
->fb
.cbufs
[cbuf
]);
149 format
= scene
->fb
.cbufs
[cbuf
]->format
;
150 uc
= arg
.clear_rb
->color_val
;
153 * this is pretty rough since we have target format (bunch of bytes...) here.
154 * dump it as raw 4 dwords.
156 LP_DBG(DEBUG_RAST
, "%s clear value (target format %d) raw 0x%x,0x%x,0x%x,0x%x\n",
157 __FUNCTION__
, format
, uc
.ui
[0], uc
.ui
[1], uc
.ui
[2], uc
.ui
[3]);
159 for (unsigned s
= 0; s
< scene
->cbufs
[cbuf
].nr_samples
; s
++) {
160 void *map
= (char *)scene
->cbufs
[cbuf
].map
+ scene
->cbufs
[cbuf
].sample_stride
* s
;
163 scene
->cbufs
[cbuf
].stride
,
164 scene
->cbufs
[cbuf
].layer_stride
,
170 scene
->fb_max_layer
+ 1,
174 /* this will increase for each rb which probably doesn't mean much */
175 LP_COUNT(nr_color_tile_clear
);
180 * Clear the rasterizer's current z/stencil tile.
181 * This is a bin command called during bin processing.
182 * Clear commands always clear all bound layers.
185 lp_rast_clear_zstencil(struct lp_rasterizer_task
*task
,
186 const union lp_rast_cmd_arg arg
)
188 const struct lp_scene
*scene
= task
->scene
;
189 uint64_t clear_value64
= arg
.clear_zstencil
.value
;
190 uint64_t clear_mask64
= arg
.clear_zstencil
.mask
;
191 uint32_t clear_value
= (uint32_t) clear_value64
;
192 uint32_t clear_mask
= (uint32_t) clear_mask64
;
193 const unsigned height
= task
->height
;
194 const unsigned width
= task
->width
;
195 const unsigned dst_stride
= scene
->zsbuf
.stride
;
200 LP_DBG(DEBUG_RAST
, "%s: value=0x%08x, mask=0x%08x\n",
201 __FUNCTION__
, clear_value
, clear_mask
);
204 * Clear the area of the depth/depth buffer matching this tile.
207 if (scene
->fb
.zsbuf
) {
210 for (unsigned s
= 0; s
< scene
->zsbuf
.nr_samples
; s
++) {
211 uint8_t *dst_layer
= task
->depth_tile
+ (s
* scene
->zsbuf
.sample_stride
);
212 block_size
= util_format_get_blocksize(scene
->fb
.zsbuf
->format
);
214 clear_value
&= clear_mask
;
216 for (layer
= 0; layer
<= scene
->fb_max_layer
; layer
++) {
219 switch (block_size
) {
221 assert(clear_mask
== 0xff);
222 for (i
= 0; i
< height
; i
++) {
223 uint8_t *row
= (uint8_t *)dst
;
224 memset(row
, (uint8_t) clear_value
, width
);
229 if (clear_mask
== 0xffff) {
230 for (i
= 0; i
< height
; i
++) {
231 uint16_t *row
= (uint16_t *)dst
;
232 for (j
= 0; j
< width
; j
++)
233 *row
++ = (uint16_t) clear_value
;
238 for (i
= 0; i
< height
; i
++) {
239 uint16_t *row
= (uint16_t *)dst
;
240 for (j
= 0; j
< width
; j
++) {
241 uint16_t tmp
= ~clear_mask
& *row
;
242 *row
++ = clear_value
| tmp
;
249 if (clear_mask
== 0xffffffff) {
250 for (i
= 0; i
< height
; i
++) {
251 util_memset32(dst
, clear_value
, width
);
256 for (i
= 0; i
< height
; i
++) {
257 uint32_t *row
= (uint32_t *)dst
;
258 for (j
= 0; j
< width
; j
++) {
259 uint32_t tmp
= ~clear_mask
& *row
;
260 *row
++ = clear_value
| tmp
;
267 clear_value64
&= clear_mask64
;
268 if (clear_mask64
== 0xffffffffffULL
) {
269 for (i
= 0; i
< height
; i
++) {
270 uint64_t *row
= (uint64_t *)dst
;
271 for (j
= 0; j
< width
; j
++)
272 *row
++ = clear_value64
;
277 for (i
= 0; i
< height
; i
++) {
278 uint64_t *row
= (uint64_t *)dst
;
279 for (j
= 0; j
< width
; j
++) {
280 uint64_t tmp
= ~clear_mask64
& *row
;
281 *row
++ = clear_value64
| tmp
;
292 dst_layer
+= scene
->zsbuf
.layer_stride
;
301 * Run the shader on all blocks in a tile. This is used when a tile is
302 * completely contained inside a triangle.
303 * This is a bin command called during bin processing.
306 lp_rast_shade_tile(struct lp_rasterizer_task
*task
,
307 const union lp_rast_cmd_arg arg
)
309 const struct lp_scene
*scene
= task
->scene
;
310 const struct lp_rast_shader_inputs
*inputs
= arg
.shade_tile
;
311 const struct lp_rast_state
*state
;
312 struct lp_fragment_shader_variant
*variant
;
313 const unsigned tile_x
= task
->x
, tile_y
= task
->y
;
316 if (inputs
->disable
) {
317 /* This command was partially binned and has been disabled */
321 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
328 variant
= state
->variant
;
330 /* render the whole 64x64 tile in 4x4 chunks */
331 for (y
= 0; y
< task
->height
; y
+= 4){
332 for (x
= 0; x
< task
->width
; x
+= 4) {
333 uint8_t *color
[PIPE_MAX_COLOR_BUFS
];
334 unsigned stride
[PIPE_MAX_COLOR_BUFS
];
335 unsigned sample_stride
[PIPE_MAX_COLOR_BUFS
];
336 uint8_t *depth
= NULL
;
337 unsigned depth_stride
= 0;
338 unsigned depth_sample_stride
= 0;
342 for (i
= 0; i
< scene
->fb
.nr_cbufs
; i
++){
343 if (scene
->fb
.cbufs
[i
]) {
344 stride
[i
] = scene
->cbufs
[i
].stride
;
345 sample_stride
[i
] = scene
->cbufs
[i
].sample_stride
;
346 color
[i
] = lp_rast_get_color_block_pointer(task
, i
, tile_x
+ x
,
347 tile_y
+ y
, inputs
->layer
);
351 sample_stride
[i
] = 0;
357 if (scene
->zsbuf
.map
) {
358 depth
= lp_rast_get_depth_block_pointer(task
, tile_x
+ x
,
359 tile_y
+ y
, inputs
->layer
);
360 depth_stride
= scene
->zsbuf
.stride
;
361 depth_sample_stride
= scene
->zsbuf
.sample_stride
;
364 /* Propagate non-interpolated raster state. */
365 task
->thread_data
.raster_state
.viewport_index
= inputs
->viewport_index
;
367 /* run shader on 4x4 block */
368 BEGIN_JIT_CALL(state
, task
);
369 variant
->jit_function
[RAST_WHOLE
]( &state
->jit_context
,
370 tile_x
+ x
, tile_y
+ y
,
382 depth_sample_stride
);
390 * Run the shader on all blocks in a tile. This is used when a tile is
391 * completely contained inside a triangle, and the shader is opaque.
392 * This is a bin command called during bin processing.
395 lp_rast_shade_tile_opaque(struct lp_rasterizer_task
*task
,
396 const union lp_rast_cmd_arg arg
)
398 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
405 lp_rast_shade_tile(task
, arg
);
410 * Compute shading for a 4x4 block of pixels inside a triangle.
411 * This is a bin command called during bin processing.
412 * \param x X position of quad in window coords
413 * \param y Y position of quad in window coords
416 lp_rast_shade_quads_mask(struct lp_rasterizer_task
*task
,
417 const struct lp_rast_shader_inputs
*inputs
,
418 unsigned x
, unsigned y
,
421 const struct lp_rast_state
*state
= task
->state
;
422 struct lp_fragment_shader_variant
*variant
= state
->variant
;
423 const struct lp_scene
*scene
= task
->scene
;
424 uint8_t *color
[PIPE_MAX_COLOR_BUFS
];
425 unsigned stride
[PIPE_MAX_COLOR_BUFS
];
426 unsigned sample_stride
[PIPE_MAX_COLOR_BUFS
];
427 uint8_t *depth
= NULL
;
428 unsigned depth_stride
= 0;
429 unsigned depth_sample_stride
= 0;
435 assert(x
< scene
->tiles_x
* TILE_SIZE
);
436 assert(y
< scene
->tiles_y
* TILE_SIZE
);
437 assert(x
% TILE_VECTOR_WIDTH
== 0);
438 assert(y
% TILE_VECTOR_HEIGHT
== 0);
440 assert((x
% 4) == 0);
441 assert((y
% 4) == 0);
444 for (i
= 0; i
< scene
->fb
.nr_cbufs
; i
++) {
445 if (scene
->fb
.cbufs
[i
]) {
446 stride
[i
] = scene
->cbufs
[i
].stride
;
447 sample_stride
[i
] = scene
->cbufs
[i
].sample_stride
;
448 color
[i
] = lp_rast_get_color_block_pointer(task
, i
, x
, y
,
453 sample_stride
[i
] = 0;
459 if (scene
->zsbuf
.map
) {
460 depth_stride
= scene
->zsbuf
.stride
;
461 depth_sample_stride
= scene
->zsbuf
.sample_stride
;
462 depth
= lp_rast_get_depth_block_pointer(task
, x
, y
, inputs
->layer
);
465 assert(lp_check_alignment(state
->jit_context
.u8_blend_color
, 16));
468 * The rasterizer may produce fragments outside our
469 * allocated 4x4 blocks hence need to filter them out here.
471 if ((x
% TILE_SIZE
) < task
->width
&& (y
% TILE_SIZE
) < task
->height
) {
472 /* Propagate non-interpolated raster state. */
473 task
->thread_data
.raster_state
.viewport_index
= inputs
->viewport_index
;
475 /* run shader on 4x4 block */
476 BEGIN_JIT_CALL(state
, task
);
477 variant
->jit_function
[RAST_EDGE_TEST
](&state
->jit_context
,
490 depth_sample_stride
);
498 * Begin a new occlusion query.
499 * This is a bin command put in all bins.
503 lp_rast_begin_query(struct lp_rasterizer_task
*task
,
504 const union lp_rast_cmd_arg arg
)
506 struct llvmpipe_query
*pq
= arg
.query_obj
;
509 case PIPE_QUERY_OCCLUSION_COUNTER
:
510 case PIPE_QUERY_OCCLUSION_PREDICATE
:
511 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
:
512 pq
->start
[task
->thread_index
] = task
->thread_data
.vis_counter
;
514 case PIPE_QUERY_PIPELINE_STATISTICS
:
515 pq
->start
[task
->thread_index
] = task
->thread_data
.ps_invocations
;
525 * End the current occlusion query.
526 * This is a bin command put in all bins.
530 lp_rast_end_query(struct lp_rasterizer_task
*task
,
531 const union lp_rast_cmd_arg arg
)
533 struct llvmpipe_query
*pq
= arg
.query_obj
;
536 case PIPE_QUERY_OCCLUSION_COUNTER
:
537 case PIPE_QUERY_OCCLUSION_PREDICATE
:
538 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
:
539 pq
->end
[task
->thread_index
] +=
540 task
->thread_data
.vis_counter
- pq
->start
[task
->thread_index
];
541 pq
->start
[task
->thread_index
] = 0;
543 case PIPE_QUERY_TIMESTAMP
:
544 pq
->end
[task
->thread_index
] = os_time_get_nano();
546 case PIPE_QUERY_PIPELINE_STATISTICS
:
547 pq
->end
[task
->thread_index
] +=
548 task
->thread_data
.ps_invocations
- pq
->start
[task
->thread_index
];
549 pq
->start
[task
->thread_index
] = 0;
559 lp_rast_set_state(struct lp_rasterizer_task
*task
,
560 const union lp_rast_cmd_arg arg
)
562 task
->state
= arg
.state
;
568 * Called when we're done writing to a color tile.
571 lp_rast_tile_end(struct lp_rasterizer_task
*task
)
575 for (i
= 0; i
< task
->scene
->num_active_queries
; ++i
) {
576 lp_rast_end_query(task
, lp_rast_arg_query(task
->scene
->active_queries
[i
]));
580 memset(task
->color_tiles
, 0, sizeof(task
->color_tiles
));
581 task
->depth_tile
= NULL
;
586 static lp_rast_cmd_func dispatch
[LP_RAST_OP_MAX
] =
589 lp_rast_clear_zstencil
,
598 lp_rast_triangle_3_4
,
599 lp_rast_triangle_3_16
,
600 lp_rast_triangle_4_16
,
602 lp_rast_shade_tile_opaque
,
606 lp_rast_triangle_32_1
,
607 lp_rast_triangle_32_2
,
608 lp_rast_triangle_32_3
,
609 lp_rast_triangle_32_4
,
610 lp_rast_triangle_32_5
,
611 lp_rast_triangle_32_6
,
612 lp_rast_triangle_32_7
,
613 lp_rast_triangle_32_8
,
614 lp_rast_triangle_32_3_4
,
615 lp_rast_triangle_32_3_16
,
616 lp_rast_triangle_32_4_16
621 do_rasterize_bin(struct lp_rasterizer_task
*task
,
622 const struct cmd_bin
*bin
,
625 const struct cmd_block
*block
;
629 lp_debug_bin(bin
, x
, y
);
631 for (block
= bin
->head
; block
; block
= block
->next
) {
632 for (k
= 0; k
< block
->count
; k
++) {
633 dispatch
[block
->cmd
[k
]]( task
, block
->arg
[k
] );
641 * Rasterize commands for a single bin.
642 * \param x, y position of the bin's tile in the framebuffer
643 * Must be called between lp_rast_begin() and lp_rast_end().
647 rasterize_bin(struct lp_rasterizer_task
*task
,
648 const struct cmd_bin
*bin
, int x
, int y
)
650 lp_rast_tile_begin( task
, bin
, x
, y
);
652 do_rasterize_bin(task
, bin
, x
, y
);
654 lp_rast_tile_end(task
);
659 if (bin
->head
->count
== 1) {
660 if (bin
->head
->cmd
[0] == LP_RAST_OP_SHADE_TILE_OPAQUE
)
661 LP_COUNT(nr_pure_shade_opaque_64
);
662 else if (bin
->head
->cmd
[0] == LP_RAST_OP_SHADE_TILE
)
663 LP_COUNT(nr_pure_shade_64
);
669 /* An empty bin is one that just loads the contents of the tile and
670 * stores them again unchanged. This typically happens when bins have
671 * been flushed for some reason in the middle of a frame, or when
672 * incremental updates are being made to a render target.
674 * Try to avoid doing pointless work in this case.
677 is_empty_bin( const struct cmd_bin
*bin
)
679 return bin
->head
== NULL
;
684 * Rasterize/execute all bins within a scene.
688 rasterize_scene(struct lp_rasterizer_task
*task
,
689 struct lp_scene
*scene
)
693 /* Clear the cache tags. This should not always be necessary but
695 #if LP_USE_TEXTURE_CACHE
696 memset(task
->thread_data
.cache
->cache_tags
, 0,
697 sizeof(task
->thread_data
.cache
->cache_tags
));
698 #if LP_BUILD_FORMAT_CACHE_DEBUG
699 task
->thread_data
.cache
->cache_access_total
= 0;
700 task
->thread_data
.cache
->cache_access_miss
= 0;
704 if (!task
->rast
->no_rast
) {
705 /* loop over scene bins, rasterize each */
711 while ((bin
= lp_scene_bin_iter_next(scene
, &i
, &j
))) {
712 if (!is_empty_bin( bin
))
713 rasterize_bin(task
, bin
, i
, j
);
719 #if LP_BUILD_FORMAT_CACHE_DEBUG
721 uint64_t total
, miss
;
722 total
= task
->thread_data
.cache
->cache_access_total
;
723 miss
= task
->thread_data
.cache
->cache_access_miss
;
725 debug_printf("thread %d cache access %llu miss %llu hit rate %f\n",
726 task
->thread_index
, (long long unsigned)total
,
727 (long long unsigned)miss
,
728 (float)(total
- miss
)/(float)total
);
734 lp_fence_signal(scene
->fence
);
742 * Called by setup module when it has something for us to render.
745 lp_rast_queue_scene( struct lp_rasterizer
*rast
,
746 struct lp_scene
*scene
)
748 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
750 if (rast
->num_threads
== 0) {
752 unsigned fpstate
= util_fpstate_get();
754 /* Make sure that denorms are treated like zeros. This is
755 * the behavior required by D3D10. OpenGL doesn't care.
757 util_fpstate_set_denorms_to_zero(fpstate
);
759 lp_rast_begin( rast
, scene
);
761 rasterize_scene( &rast
->tasks
[0], scene
);
765 util_fpstate_set(fpstate
);
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
810 thread_function(void *init_data
)
812 struct lp_rasterizer_task
*task
= (struct lp_rasterizer_task
*) init_data
;
813 struct lp_rasterizer
*rast
= task
->rast
;
814 boolean debug
= false;
815 char thread_name
[16];
818 snprintf(thread_name
, sizeof thread_name
, "llvmpipe-%u", task
->thread_index
);
819 u_thread_setname(thread_name
);
821 /* Make sure that denorms are treated like zeros. This is
822 * the behavior required by D3D10. OpenGL doesn't care.
824 fpstate
= util_fpstate_get();
825 util_fpstate_set_denorms_to_zero(fpstate
);
830 debug_printf("thread %d waiting for work\n", task
->thread_index
);
831 pipe_semaphore_wait(&task
->work_ready
);
836 if (task
->thread_index
== 0) {
838 * - get next scene to rasterize
839 * - map the framebuffer surfaces
842 lp_scene_dequeue( rast
->full_scenes
, TRUE
) );
845 /* Wait for all threads to get here so that threads[1+] don't
846 * get a null rast->curr_scene pointer.
848 util_barrier_wait( &rast
->barrier
);
852 debug_printf("thread %d doing work\n", task
->thread_index
);
854 rasterize_scene(task
,
857 /* wait for all threads to finish with this scene */
858 util_barrier_wait( &rast
->barrier
);
860 /* XXX: shouldn't be necessary:
862 if (task
->thread_index
== 0) {
866 /* signal done with work */
868 debug_printf("thread %d done working\n", task
->thread_index
);
870 pipe_semaphore_signal(&task
->work_done
);
874 pipe_semaphore_signal(&task
->work_done
);
882 * Initialize semaphores and spawn the threads.
885 create_rast_threads(struct lp_rasterizer
*rast
)
889 /* NOTE: if num_threads is zero, we won't use any threads */
890 for (i
= 0; i
< rast
->num_threads
; i
++) {
891 pipe_semaphore_init(&rast
->tasks
[i
].work_ready
, 0);
892 pipe_semaphore_init(&rast
->tasks
[i
].work_done
, 0);
893 rast
->threads
[i
] = u_thread_create(thread_function
,
894 (void *) &rast
->tasks
[i
]);
895 if (!rast
->threads
[i
]) {
896 rast
->num_threads
= i
; /* previous thread is max */
905 * Create new lp_rasterizer. If num_threads is zero, don't create any
906 * new threads, do rendering synchronously.
907 * \param num_threads number of rasterizer threads to create
909 struct lp_rasterizer
*
910 lp_rast_create( unsigned num_threads
)
912 struct lp_rasterizer
*rast
;
915 rast
= CALLOC_STRUCT(lp_rasterizer
);
920 rast
->full_scenes
= lp_scene_queue_create();
921 if (!rast
->full_scenes
) {
925 for (i
= 0; i
< MAX2(1, num_threads
); i
++) {
926 struct lp_rasterizer_task
*task
= &rast
->tasks
[i
];
928 task
->thread_index
= i
;
929 task
->thread_data
.cache
= align_malloc(sizeof(struct lp_build_format_cache
),
931 if (!task
->thread_data
.cache
) {
932 goto no_thread_data_cache
;
936 rast
->num_threads
= num_threads
;
938 rast
->no_rast
= debug_get_bool_option("LP_NO_RAST", FALSE
);
940 create_rast_threads(rast
);
942 /* for synchronizing rasterization threads */
943 if (rast
->num_threads
> 0) {
944 util_barrier_init( &rast
->barrier
, rast
->num_threads
);
947 memset(lp_dummy_tile
, 0, sizeof lp_dummy_tile
);
951 no_thread_data_cache
:
952 for (i
= 0; i
< MAX2(1, rast
->num_threads
); i
++) {
953 if (rast
->tasks
[i
].thread_data
.cache
) {
954 align_free(rast
->tasks
[i
].thread_data
.cache
);
958 lp_scene_queue_destroy(rast
->full_scenes
);
968 void lp_rast_destroy( struct lp_rasterizer
*rast
)
972 /* Set exit_flag and signal each thread's work_ready semaphore.
973 * Each thread will be woken up, notice that the exit_flag is set and
974 * break out of its main loop. The thread will then exit.
976 rast
->exit_flag
= TRUE
;
977 for (i
= 0; i
< rast
->num_threads
; i
++) {
978 pipe_semaphore_signal(&rast
->tasks
[i
].work_ready
);
981 /* Wait for threads to terminate before cleaning up per-thread data.
982 * We don't actually call pipe_thread_wait to avoid dead lock on Windows
983 * per https://bugs.freedesktop.org/show_bug.cgi?id=76252 */
984 for (i
= 0; i
< rast
->num_threads
; i
++) {
986 pipe_semaphore_wait(&rast
->tasks
[i
].work_done
);
988 thrd_join(rast
->threads
[i
], NULL
);
992 /* Clean up per-thread data */
993 for (i
= 0; i
< rast
->num_threads
; i
++) {
994 pipe_semaphore_destroy(&rast
->tasks
[i
].work_ready
);
995 pipe_semaphore_destroy(&rast
->tasks
[i
].work_done
);
997 for (i
= 0; i
< MAX2(1, rast
->num_threads
); i
++) {
998 align_free(rast
->tasks
[i
].thread_data
.cache
);
1001 /* for synchronizing rasterization threads */
1002 if (rast
->num_threads
> 0) {
1003 util_barrier_destroy( &rast
->barrier
);
1006 lp_scene_queue_destroy(rast
->full_scenes
);