clover: Wrap event::wait_count in a method taking care of the required locking.
[mesa.git] / src / gallium / drivers / llvmpipe / lp_rast.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 #include <limits.h>
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
36 #include "os/os_time.h"
37
38 #include "lp_scene_queue.h"
39 #include "lp_context.h"
40 #include "lp_debug.h"
41 #include "lp_fence.h"
42 #include "lp_perf.h"
43 #include "lp_query.h"
44 #include "lp_rast.h"
45 #include "lp_rast_priv.h"
46 #include "gallivm/lp_bld_format.h"
47 #include "gallivm/lp_bld_debug.h"
48 #include "lp_scene.h"
49 #include "lp_tex_sample.h"
50
51
52 #ifdef DEBUG
53 int jit_line = 0;
54 const struct lp_rast_state *jit_state = NULL;
55 const struct lp_rasterizer_task *jit_task = NULL;
56 #endif
57
58
59 /**
60 * Begin rasterizing a scene.
61 * Called once per scene by one thread.
62 */
63 static void
64 lp_rast_begin( struct lp_rasterizer *rast,
65 struct lp_scene *scene )
66 {
67 rast->curr_scene = scene;
68
69 LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
70
71 lp_scene_begin_rasterization( scene );
72 lp_scene_bin_iter_begin( scene );
73 }
74
75
76 static void
77 lp_rast_end( struct lp_rasterizer *rast )
78 {
79 lp_scene_end_rasterization( rast->curr_scene );
80
81 rast->curr_scene = NULL;
82 }
83
84
85 /**
86 * Beginning rasterization of a tile.
87 * \param x window X position of the tile, in pixels
88 * \param y window Y position of the tile, in pixels
89 */
90 static void
91 lp_rast_tile_begin(struct lp_rasterizer_task *task,
92 const struct cmd_bin *bin,
93 int x, int y)
94 {
95 unsigned i;
96 struct lp_scene *scene = task->scene;
97
98 LP_DBG(DEBUG_RAST, "%s %d,%d\n", __FUNCTION__, x, y);
99
100 task->bin = bin;
101 task->x = x * TILE_SIZE;
102 task->y = y * TILE_SIZE;
103 task->width = TILE_SIZE + x * TILE_SIZE > task->scene->fb.width ?
104 task->scene->fb.width - x * TILE_SIZE : TILE_SIZE;
105 task->height = TILE_SIZE + y * TILE_SIZE > task->scene->fb.height ?
106 task->scene->fb.height - y * TILE_SIZE : TILE_SIZE;
107
108 task->thread_data.vis_counter = 0;
109 task->ps_invocations = 0;
110
111 for (i = 0; i < task->scene->fb.nr_cbufs; i++) {
112 if (task->scene->fb.cbufs[i]) {
113 task->color_tiles[i] = scene->cbufs[i].map +
114 scene->cbufs[i].stride * task->y +
115 scene->cbufs[i].format_bytes * task->x;
116 }
117 }
118 if (task->scene->fb.zsbuf) {
119 task->depth_tile = scene->zsbuf.map +
120 scene->zsbuf.stride * task->y +
121 scene->zsbuf.format_bytes * task->x;
122 }
123 }
124
125
126 /**
127 * Clear the rasterizer's current color tile.
128 * This is a bin command called during bin processing.
129 * Clear commands always clear all bound layers.
130 */
131 static void
132 lp_rast_clear_color(struct lp_rasterizer_task *task,
133 const union lp_rast_cmd_arg arg)
134 {
135 const struct lp_scene *scene = task->scene;
136 unsigned cbuf = arg.clear_rb->cbuf;
137 union util_color uc;
138 enum pipe_format format;
139
140 /* we never bin clear commands for non-existing buffers */
141 assert(cbuf < scene->fb.nr_cbufs);
142 assert(scene->fb.cbufs[cbuf]);
143
144 format = scene->fb.cbufs[cbuf]->format;
145 uc = arg.clear_rb->color_val;
146
147 /*
148 * this is pretty rough since we have target format (bunch of bytes...) here.
149 * dump it as raw 4 dwords.
150 */
151 LP_DBG(DEBUG_RAST, "%s clear value (target format %d) raw 0x%x,0x%x,0x%x,0x%x\n",
152 __FUNCTION__, format, uc.ui[0], uc.ui[1], uc.ui[2], uc.ui[3]);
153
154
155 util_fill_box(scene->cbufs[cbuf].map,
156 format,
157 scene->cbufs[cbuf].stride,
158 scene->cbufs[cbuf].layer_stride,
159 task->x,
160 task->y,
161 0,
162 task->width,
163 task->height,
164 scene->fb_max_layer + 1,
165 &uc);
166
167 /* this will increase for each rb which probably doesn't mean much */
168 LP_COUNT(nr_color_tile_clear);
169 }
170
171
172 /**
173 * Clear the rasterizer's current z/stencil tile.
174 * This is a bin command called during bin processing.
175 * Clear commands always clear all bound layers.
176 */
177 static void
178 lp_rast_clear_zstencil(struct lp_rasterizer_task *task,
179 const union lp_rast_cmd_arg arg)
180 {
181 const struct lp_scene *scene = task->scene;
182 uint64_t clear_value64 = arg.clear_zstencil.value;
183 uint64_t clear_mask64 = arg.clear_zstencil.mask;
184 uint32_t clear_value = (uint32_t) clear_value64;
185 uint32_t clear_mask = (uint32_t) clear_mask64;
186 const unsigned height = task->height;
187 const unsigned width = task->width;
188 const unsigned dst_stride = scene->zsbuf.stride;
189 uint8_t *dst;
190 unsigned i, j;
191 unsigned block_size;
192
193 LP_DBG(DEBUG_RAST, "%s: value=0x%08x, mask=0x%08x\n",
194 __FUNCTION__, clear_value, clear_mask);
195
196 /*
197 * Clear the area of the depth/depth buffer matching this tile.
198 */
199
200 if (scene->fb.zsbuf) {
201 unsigned layer;
202 uint8_t *dst_layer = task->depth_tile;
203 block_size = util_format_get_blocksize(scene->fb.zsbuf->format);
204
205 clear_value &= clear_mask;
206
207 for (layer = 0; layer <= scene->fb_max_layer; layer++) {
208 dst = dst_layer;
209
210 switch (block_size) {
211 case 1:
212 assert(clear_mask == 0xff);
213 memset(dst, (uint8_t) clear_value, height * width);
214 break;
215 case 2:
216 if (clear_mask == 0xffff) {
217 for (i = 0; i < height; i++) {
218 uint16_t *row = (uint16_t *)dst;
219 for (j = 0; j < width; j++)
220 *row++ = (uint16_t) clear_value;
221 dst += dst_stride;
222 }
223 }
224 else {
225 for (i = 0; i < height; i++) {
226 uint16_t *row = (uint16_t *)dst;
227 for (j = 0; j < width; j++) {
228 uint16_t tmp = ~clear_mask & *row;
229 *row++ = clear_value | tmp;
230 }
231 dst += dst_stride;
232 }
233 }
234 break;
235 case 4:
236 if (clear_mask == 0xffffffff) {
237 for (i = 0; i < height; i++) {
238 uint32_t *row = (uint32_t *)dst;
239 for (j = 0; j < width; j++)
240 *row++ = clear_value;
241 dst += dst_stride;
242 }
243 }
244 else {
245 for (i = 0; i < height; i++) {
246 uint32_t *row = (uint32_t *)dst;
247 for (j = 0; j < width; j++) {
248 uint32_t tmp = ~clear_mask & *row;
249 *row++ = clear_value | tmp;
250 }
251 dst += dst_stride;
252 }
253 }
254 break;
255 case 8:
256 clear_value64 &= clear_mask64;
257 if (clear_mask64 == 0xffffffffffULL) {
258 for (i = 0; i < height; i++) {
259 uint64_t *row = (uint64_t *)dst;
260 for (j = 0; j < width; j++)
261 *row++ = clear_value64;
262 dst += dst_stride;
263 }
264 }
265 else {
266 for (i = 0; i < height; i++) {
267 uint64_t *row = (uint64_t *)dst;
268 for (j = 0; j < width; j++) {
269 uint64_t tmp = ~clear_mask64 & *row;
270 *row++ = clear_value64 | tmp;
271 }
272 dst += dst_stride;
273 }
274 }
275 break;
276
277 default:
278 assert(0);
279 break;
280 }
281 dst_layer += scene->zsbuf.layer_stride;
282 }
283 }
284 }
285
286
287
288 /**
289 * Run the shader on all blocks in a tile. This is used when a tile is
290 * completely contained inside a triangle.
291 * This is a bin command called during bin processing.
292 */
293 static void
294 lp_rast_shade_tile(struct lp_rasterizer_task *task,
295 const union lp_rast_cmd_arg arg)
296 {
297 const struct lp_scene *scene = task->scene;
298 const struct lp_rast_shader_inputs *inputs = arg.shade_tile;
299 const struct lp_rast_state *state;
300 struct lp_fragment_shader_variant *variant;
301 const unsigned tile_x = task->x, tile_y = task->y;
302 unsigned x, y;
303
304 if (inputs->disable) {
305 /* This command was partially binned and has been disabled */
306 return;
307 }
308
309 LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
310
311 state = task->state;
312 assert(state);
313 if (!state) {
314 return;
315 }
316 variant = state->variant;
317
318 /* render the whole 64x64 tile in 4x4 chunks */
319 for (y = 0; y < task->height; y += 4){
320 for (x = 0; x < task->width; x += 4) {
321 uint8_t *color[PIPE_MAX_COLOR_BUFS];
322 unsigned stride[PIPE_MAX_COLOR_BUFS];
323 uint8_t *depth = NULL;
324 unsigned depth_stride = 0;
325 unsigned i;
326
327 /* color buffer */
328 for (i = 0; i < scene->fb.nr_cbufs; i++){
329 if (scene->fb.cbufs[i]) {
330 stride[i] = scene->cbufs[i].stride;
331 color[i] = lp_rast_get_color_block_pointer(task, i, tile_x + x,
332 tile_y + y, inputs->layer);
333 }
334 else {
335 stride[i] = 0;
336 color[i] = NULL;
337 }
338 }
339
340 /* depth buffer */
341 if (scene->zsbuf.map) {
342 depth = lp_rast_get_depth_block_pointer(task, tile_x + x,
343 tile_y + y, inputs->layer);
344 depth_stride = scene->zsbuf.stride;
345 }
346
347 /* Propagate non-interpolated raster state. */
348 task->thread_data.raster_state.viewport_index = inputs->viewport_index;
349
350 /* run shader on 4x4 block */
351 BEGIN_JIT_CALL(state, task);
352 variant->jit_function[RAST_WHOLE]( &state->jit_context,
353 tile_x + x, tile_y + y,
354 inputs->frontfacing,
355 GET_A0(inputs),
356 GET_DADX(inputs),
357 GET_DADY(inputs),
358 color,
359 depth,
360 0xffff,
361 &task->thread_data,
362 stride,
363 depth_stride);
364 END_JIT_CALL();
365 }
366 }
367 }
368
369
370 /**
371 * Run the shader on all blocks in a tile. This is used when a tile is
372 * completely contained inside a triangle, and the shader is opaque.
373 * This is a bin command called during bin processing.
374 */
375 static void
376 lp_rast_shade_tile_opaque(struct lp_rasterizer_task *task,
377 const union lp_rast_cmd_arg arg)
378 {
379 LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
380
381 assert(task->state);
382 if (!task->state) {
383 return;
384 }
385
386 lp_rast_shade_tile(task, arg);
387 }
388
389
390 /**
391 * Compute shading for a 4x4 block of pixels inside a triangle.
392 * This is a bin command called during bin processing.
393 * \param x X position of quad in window coords
394 * \param y Y position of quad in window coords
395 */
396 void
397 lp_rast_shade_quads_mask(struct lp_rasterizer_task *task,
398 const struct lp_rast_shader_inputs *inputs,
399 unsigned x, unsigned y,
400 unsigned mask)
401 {
402 const struct lp_rast_state *state = task->state;
403 struct lp_fragment_shader_variant *variant = state->variant;
404 const struct lp_scene *scene = task->scene;
405 uint8_t *color[PIPE_MAX_COLOR_BUFS];
406 unsigned stride[PIPE_MAX_COLOR_BUFS];
407 uint8_t *depth = NULL;
408 unsigned depth_stride = 0;
409 unsigned i;
410
411 assert(state);
412
413 /* Sanity checks */
414 assert(x < scene->tiles_x * TILE_SIZE);
415 assert(y < scene->tiles_y * TILE_SIZE);
416 assert(x % TILE_VECTOR_WIDTH == 0);
417 assert(y % TILE_VECTOR_HEIGHT == 0);
418
419 assert((x % 4) == 0);
420 assert((y % 4) == 0);
421
422 /* color buffer */
423 for (i = 0; i < scene->fb.nr_cbufs; i++) {
424 if (scene->fb.cbufs[i]) {
425 stride[i] = scene->cbufs[i].stride;
426 color[i] = lp_rast_get_color_block_pointer(task, i, x, y,
427 inputs->layer);
428 }
429 else {
430 stride[i] = 0;
431 color[i] = NULL;
432 }
433 }
434
435 /* depth buffer */
436 if (scene->zsbuf.map) {
437 depth_stride = scene->zsbuf.stride;
438 depth = lp_rast_get_depth_block_pointer(task, x, y, inputs->layer);
439 }
440
441 assert(lp_check_alignment(state->jit_context.u8_blend_color, 16));
442
443 /*
444 * The rasterizer may produce fragments outside our
445 * allocated 4x4 blocks hence need to filter them out here.
446 */
447 if ((x % TILE_SIZE) < task->width && (y % TILE_SIZE) < task->height) {
448 /* not very accurate would need a popcount on the mask */
449 /* always count this not worth bothering? */
450 task->ps_invocations += 1 * variant->ps_inv_multiplier;
451
452 /* Propagate non-interpolated raster state. */
453 task->thread_data.raster_state.viewport_index = inputs->viewport_index;
454
455 /* run shader on 4x4 block */
456 BEGIN_JIT_CALL(state, task);
457 variant->jit_function[RAST_EDGE_TEST](&state->jit_context,
458 x, y,
459 inputs->frontfacing,
460 GET_A0(inputs),
461 GET_DADX(inputs),
462 GET_DADY(inputs),
463 color,
464 depth,
465 mask,
466 &task->thread_data,
467 stride,
468 depth_stride);
469 END_JIT_CALL();
470 }
471 }
472
473
474
475 /**
476 * Begin a new occlusion query.
477 * This is a bin command put in all bins.
478 * Called per thread.
479 */
480 static void
481 lp_rast_begin_query(struct lp_rasterizer_task *task,
482 const union lp_rast_cmd_arg arg)
483 {
484 struct llvmpipe_query *pq = arg.query_obj;
485
486 switch (pq->type) {
487 case PIPE_QUERY_OCCLUSION_COUNTER:
488 case PIPE_QUERY_OCCLUSION_PREDICATE:
489 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
490 pq->start[task->thread_index] = task->thread_data.vis_counter;
491 break;
492 case PIPE_QUERY_PIPELINE_STATISTICS:
493 pq->start[task->thread_index] = task->ps_invocations;
494 break;
495 default:
496 assert(0);
497 break;
498 }
499 }
500
501
502 /**
503 * End the current occlusion query.
504 * This is a bin command put in all bins.
505 * Called per thread.
506 */
507 static void
508 lp_rast_end_query(struct lp_rasterizer_task *task,
509 const union lp_rast_cmd_arg arg)
510 {
511 struct llvmpipe_query *pq = arg.query_obj;
512
513 switch (pq->type) {
514 case PIPE_QUERY_OCCLUSION_COUNTER:
515 case PIPE_QUERY_OCCLUSION_PREDICATE:
516 case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
517 pq->end[task->thread_index] +=
518 task->thread_data.vis_counter - pq->start[task->thread_index];
519 pq->start[task->thread_index] = 0;
520 break;
521 case PIPE_QUERY_TIMESTAMP:
522 pq->end[task->thread_index] = os_time_get_nano();
523 break;
524 case PIPE_QUERY_PIPELINE_STATISTICS:
525 pq->end[task->thread_index] +=
526 task->ps_invocations - pq->start[task->thread_index];
527 pq->start[task->thread_index] = 0;
528 break;
529 default:
530 assert(0);
531 break;
532 }
533 }
534
535
536 void
537 lp_rast_set_state(struct lp_rasterizer_task *task,
538 const union lp_rast_cmd_arg arg)
539 {
540 task->state = arg.state;
541 }
542
543
544
545 /**
546 * Called when we're done writing to a color tile.
547 */
548 static void
549 lp_rast_tile_end(struct lp_rasterizer_task *task)
550 {
551 unsigned i;
552
553 for (i = 0; i < task->scene->num_active_queries; ++i) {
554 lp_rast_end_query(task, lp_rast_arg_query(task->scene->active_queries[i]));
555 }
556
557 /* debug */
558 memset(task->color_tiles, 0, sizeof(task->color_tiles));
559 task->depth_tile = NULL;
560
561 task->bin = NULL;
562 }
563
564 static lp_rast_cmd_func dispatch[LP_RAST_OP_MAX] =
565 {
566 lp_rast_clear_color,
567 lp_rast_clear_zstencil,
568 lp_rast_triangle_1,
569 lp_rast_triangle_2,
570 lp_rast_triangle_3,
571 lp_rast_triangle_4,
572 lp_rast_triangle_5,
573 lp_rast_triangle_6,
574 lp_rast_triangle_7,
575 lp_rast_triangle_8,
576 lp_rast_triangle_3_4,
577 lp_rast_triangle_3_16,
578 lp_rast_triangle_4_16,
579 lp_rast_shade_tile,
580 lp_rast_shade_tile_opaque,
581 lp_rast_begin_query,
582 lp_rast_end_query,
583 lp_rast_set_state,
584 lp_rast_triangle_32_1,
585 lp_rast_triangle_32_2,
586 lp_rast_triangle_32_3,
587 lp_rast_triangle_32_4,
588 lp_rast_triangle_32_5,
589 lp_rast_triangle_32_6,
590 lp_rast_triangle_32_7,
591 lp_rast_triangle_32_8,
592 lp_rast_triangle_32_3_4,
593 lp_rast_triangle_32_3_16,
594 lp_rast_triangle_32_4_16
595 };
596
597
598 static void
599 do_rasterize_bin(struct lp_rasterizer_task *task,
600 const struct cmd_bin *bin,
601 int x, int y)
602 {
603 const struct cmd_block *block;
604 unsigned k;
605
606 if (0)
607 lp_debug_bin(bin, x, y);
608
609 for (block = bin->head; block; block = block->next) {
610 for (k = 0; k < block->count; k++) {
611 dispatch[block->cmd[k]]( task, block->arg[k] );
612 }
613 }
614 }
615
616
617
618 /**
619 * Rasterize commands for a single bin.
620 * \param x, y position of the bin's tile in the framebuffer
621 * Must be called between lp_rast_begin() and lp_rast_end().
622 * Called per thread.
623 */
624 static void
625 rasterize_bin(struct lp_rasterizer_task *task,
626 const struct cmd_bin *bin, int x, int y )
627 {
628 lp_rast_tile_begin( task, bin, x, y );
629
630 do_rasterize_bin(task, bin, x, y);
631
632 lp_rast_tile_end(task);
633
634
635 /* Debug/Perf flags:
636 */
637 if (bin->head->count == 1) {
638 if (bin->head->cmd[0] == LP_RAST_OP_SHADE_TILE_OPAQUE)
639 LP_COUNT(nr_pure_shade_opaque_64);
640 else if (bin->head->cmd[0] == LP_RAST_OP_SHADE_TILE)
641 LP_COUNT(nr_pure_shade_64);
642 }
643 }
644
645
646 /* An empty bin is one that just loads the contents of the tile and
647 * stores them again unchanged. This typically happens when bins have
648 * been flushed for some reason in the middle of a frame, or when
649 * incremental updates are being made to a render target.
650 *
651 * Try to avoid doing pointless work in this case.
652 */
653 static boolean
654 is_empty_bin( const struct cmd_bin *bin )
655 {
656 return bin->head == NULL;
657 }
658
659
660 /**
661 * Rasterize/execute all bins within a scene.
662 * Called per thread.
663 */
664 static void
665 rasterize_scene(struct lp_rasterizer_task *task,
666 struct lp_scene *scene)
667 {
668 task->scene = scene;
669
670 /* Clear the cache tags. This should not always be necessary but
671 simpler for now. */
672 #if LP_USE_TEXTURE_CACHE
673 memset(task->thread_data.cache->cache_tags, 0,
674 sizeof(task->thread_data.cache->cache_tags));
675 #if LP_BUILD_FORMAT_CACHE_DEBUG
676 task->thread_data.cache->cache_access_total = 0;
677 task->thread_data.cache->cache_access_miss = 0;
678 #endif
679 #endif
680
681 if (!task->rast->no_rast && !scene->discard) {
682 /* loop over scene bins, rasterize each */
683 {
684 struct cmd_bin *bin;
685 int i, j;
686
687 assert(scene);
688 while ((bin = lp_scene_bin_iter_next(scene, &i, &j))) {
689 if (!is_empty_bin( bin ))
690 rasterize_bin(task, bin, i, j);
691 }
692 }
693 }
694
695
696 #if LP_BUILD_FORMAT_CACHE_DEBUG
697 {
698 uint64_t total, miss;
699 total = task->thread_data.cache->cache_access_total;
700 miss = task->thread_data.cache->cache_access_miss;
701 if (total) {
702 debug_printf("thread %d cache access %llu miss %llu hit rate %f\n",
703 task->thread_index, (long long unsigned)total,
704 (long long unsigned)miss,
705 (float)(total - miss)/(float)total);
706 }
707 }
708 #endif
709
710 if (scene->fence) {
711 lp_fence_signal(scene->fence);
712 }
713
714 task->scene = NULL;
715 }
716
717
718 /**
719 * Called by setup module when it has something for us to render.
720 */
721 void
722 lp_rast_queue_scene( struct lp_rasterizer *rast,
723 struct lp_scene *scene)
724 {
725 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
726
727 if (rast->num_threads == 0) {
728 /* no threading */
729 unsigned fpstate = util_fpstate_get();
730
731 /* Make sure that denorms are treated like zeros. This is
732 * the behavior required by D3D10. OpenGL doesn't care.
733 */
734 util_fpstate_set_denorms_to_zero(fpstate);
735
736 lp_rast_begin( rast, scene );
737
738 rasterize_scene( &rast->tasks[0], scene );
739
740 lp_rast_end( rast );
741
742 util_fpstate_set(fpstate);
743
744 rast->curr_scene = NULL;
745 }
746 else {
747 /* threaded rendering! */
748 unsigned i;
749
750 lp_scene_enqueue( rast->full_scenes, scene );
751
752 /* signal the threads that there's work to do */
753 for (i = 0; i < rast->num_threads; i++) {
754 pipe_semaphore_signal(&rast->tasks[i].work_ready);
755 }
756 }
757
758 LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
759 }
760
761
762 void
763 lp_rast_finish( struct lp_rasterizer *rast )
764 {
765 if (rast->num_threads == 0) {
766 /* nothing to do */
767 }
768 else {
769 int i;
770
771 /* wait for work to complete */
772 for (i = 0; i < rast->num_threads; i++) {
773 pipe_semaphore_wait(&rast->tasks[i].work_done);
774 }
775 }
776 }
777
778
779 /**
780 * This is the thread's main entrypoint.
781 * It's a simple loop:
782 * 1. wait for work
783 * 2. do work
784 * 3. signal that we're done
785 */
786 static int
787 thread_function(void *init_data)
788 {
789 struct lp_rasterizer_task *task = (struct lp_rasterizer_task *) init_data;
790 struct lp_rasterizer *rast = task->rast;
791 boolean debug = false;
792 char thread_name[16];
793 unsigned fpstate;
794
795 util_snprintf(thread_name, sizeof thread_name, "llvmpipe-%u", task->thread_index);
796 u_thread_setname(thread_name);
797
798 /* Make sure that denorms are treated like zeros. This is
799 * the behavior required by D3D10. OpenGL doesn't care.
800 */
801 fpstate = util_fpstate_get();
802 util_fpstate_set_denorms_to_zero(fpstate);
803
804 while (1) {
805 /* wait for work */
806 if (debug)
807 debug_printf("thread %d waiting for work\n", task->thread_index);
808 pipe_semaphore_wait(&task->work_ready);
809
810 if (rast->exit_flag)
811 break;
812
813 if (task->thread_index == 0) {
814 /* thread[0]:
815 * - get next scene to rasterize
816 * - map the framebuffer surfaces
817 */
818 lp_rast_begin( rast,
819 lp_scene_dequeue( rast->full_scenes, TRUE ) );
820 }
821
822 /* Wait for all threads to get here so that threads[1+] don't
823 * get a null rast->curr_scene pointer.
824 */
825 pipe_barrier_wait( &rast->barrier );
826
827 /* do work */
828 if (debug)
829 debug_printf("thread %d doing work\n", task->thread_index);
830
831 rasterize_scene(task,
832 rast->curr_scene);
833
834 /* wait for all threads to finish with this scene */
835 pipe_barrier_wait( &rast->barrier );
836
837 /* XXX: shouldn't be necessary:
838 */
839 if (task->thread_index == 0) {
840 lp_rast_end( rast );
841 }
842
843 /* signal done with work */
844 if (debug)
845 debug_printf("thread %d done working\n", task->thread_index);
846
847 pipe_semaphore_signal(&task->work_done);
848 }
849
850 #ifdef _WIN32
851 pipe_semaphore_signal(&task->work_done);
852 #endif
853
854 return 0;
855 }
856
857
858 /**
859 * Initialize semaphores and spawn the threads.
860 */
861 static void
862 create_rast_threads(struct lp_rasterizer *rast)
863 {
864 unsigned i;
865
866 /* NOTE: if num_threads is zero, we won't use any threads */
867 for (i = 0; i < rast->num_threads; i++) {
868 pipe_semaphore_init(&rast->tasks[i].work_ready, 0);
869 pipe_semaphore_init(&rast->tasks[i].work_done, 0);
870 rast->threads[i] = u_thread_create(thread_function,
871 (void *) &rast->tasks[i]);
872 }
873 }
874
875
876
877 /**
878 * Create new lp_rasterizer. If num_threads is zero, don't create any
879 * new threads, do rendering synchronously.
880 * \param num_threads number of rasterizer threads to create
881 */
882 struct lp_rasterizer *
883 lp_rast_create( unsigned num_threads )
884 {
885 struct lp_rasterizer *rast;
886 unsigned i;
887
888 rast = CALLOC_STRUCT(lp_rasterizer);
889 if (!rast) {
890 goto no_rast;
891 }
892
893 rast->full_scenes = lp_scene_queue_create();
894 if (!rast->full_scenes) {
895 goto no_full_scenes;
896 }
897
898 for (i = 0; i < MAX2(1, num_threads); i++) {
899 struct lp_rasterizer_task *task = &rast->tasks[i];
900 task->rast = rast;
901 task->thread_index = i;
902 task->thread_data.cache = align_malloc(sizeof(struct lp_build_format_cache),
903 16);
904 if (!task->thread_data.cache) {
905 goto no_thread_data_cache;
906 }
907 }
908
909 rast->num_threads = num_threads;
910
911 rast->no_rast = debug_get_bool_option("LP_NO_RAST", FALSE);
912
913 create_rast_threads(rast);
914
915 /* for synchronizing rasterization threads */
916 if (rast->num_threads > 0) {
917 pipe_barrier_init( &rast->barrier, rast->num_threads );
918 }
919
920 memset(lp_dummy_tile, 0, sizeof lp_dummy_tile);
921
922 return rast;
923
924 no_thread_data_cache:
925 for (i = 0; i < MAX2(1, rast->num_threads); i++) {
926 if (rast->tasks[i].thread_data.cache) {
927 align_free(rast->tasks[i].thread_data.cache);
928 }
929 }
930
931 lp_scene_queue_destroy(rast->full_scenes);
932 no_full_scenes:
933 FREE(rast);
934 no_rast:
935 return NULL;
936 }
937
938
939 /* Shutdown:
940 */
941 void lp_rast_destroy( struct lp_rasterizer *rast )
942 {
943 unsigned i;
944
945 /* Set exit_flag and signal each thread's work_ready semaphore.
946 * Each thread will be woken up, notice that the exit_flag is set and
947 * break out of its main loop. The thread will then exit.
948 */
949 rast->exit_flag = TRUE;
950 for (i = 0; i < rast->num_threads; i++) {
951 pipe_semaphore_signal(&rast->tasks[i].work_ready);
952 }
953
954 /* Wait for threads to terminate before cleaning up per-thread data.
955 * We don't actually call pipe_thread_wait to avoid dead lock on Windows
956 * per https://bugs.freedesktop.org/show_bug.cgi?id=76252 */
957 for (i = 0; i < rast->num_threads; i++) {
958 #ifdef _WIN32
959 pipe_semaphore_wait(&rast->tasks[i].work_done);
960 #else
961 thrd_join(rast->threads[i], NULL);
962 #endif
963 }
964
965 /* Clean up per-thread data */
966 for (i = 0; i < rast->num_threads; i++) {
967 pipe_semaphore_destroy(&rast->tasks[i].work_ready);
968 pipe_semaphore_destroy(&rast->tasks[i].work_done);
969 }
970 for (i = 0; i < MAX2(1, rast->num_threads); i++) {
971 align_free(rast->tasks[i].thread_data.cache);
972 }
973
974 /* for synchronizing rasterization threads */
975 if (rast->num_threads > 0) {
976 pipe_barrier_destroy( &rast->barrier );
977 }
978
979 lp_scene_queue_destroy(rast->full_scenes);
980
981 FREE(rast);
982 }
983
984