llvmpipe: implement PIPE_QUERY_SO_STATISTICS
[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
35 #include "os/os_time.h"
36
37 #include "lp_scene_queue.h"
38 #include "lp_debug.h"
39 #include "lp_fence.h"
40 #include "lp_perf.h"
41 #include "lp_query.h"
42 #include "lp_rast.h"
43 #include "lp_rast_priv.h"
44 #include "gallivm/lp_bld_debug.h"
45 #include "lp_scene.h"
46 #include "lp_tex_sample.h"
47
48
49 #ifdef DEBUG
50 int jit_line = 0;
51 const struct lp_rast_state *jit_state = NULL;
52 const struct lp_rasterizer_task *jit_task = NULL;
53 #endif
54
55
56 /**
57 * Begin rasterizing a scene.
58 * Called once per scene by one thread.
59 */
60 static void
61 lp_rast_begin( struct lp_rasterizer *rast,
62 struct lp_scene *scene )
63 {
64
65 rast->curr_scene = scene;
66
67 LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
68
69 lp_scene_begin_rasterization( scene );
70 lp_scene_bin_iter_begin( scene );
71 }
72
73
74 static void
75 lp_rast_end( struct lp_rasterizer *rast )
76 {
77 lp_scene_end_rasterization( rast->curr_scene );
78
79 rast->curr_scene = NULL;
80 }
81
82
83 /**
84 * Begining rasterization of a tile.
85 * \param x window X position of the tile, in pixels
86 * \param y window Y position of the tile, in pixels
87 */
88 static void
89 lp_rast_tile_begin(struct lp_rasterizer_task *task,
90 const struct cmd_bin *bin)
91 {
92 const struct lp_scene *scene = task->scene;
93 enum lp_texture_usage usage;
94
95 LP_DBG(DEBUG_RAST, "%s %d,%d\n", __FUNCTION__, bin->x, bin->y);
96
97 task->bin = bin;
98 task->x = bin->x * TILE_SIZE;
99 task->y = bin->y * TILE_SIZE;
100
101 /* reset pointers to color tile(s) */
102 memset(task->color_tiles, 0, sizeof(task->color_tiles));
103
104 /* get pointer to depth/stencil tile */
105 {
106 struct pipe_surface *zsbuf = task->scene->fb.zsbuf;
107 if (zsbuf) {
108 struct llvmpipe_resource *lpt = llvmpipe_resource(zsbuf->texture);
109
110 if (scene->has_depthstencil_clear)
111 usage = LP_TEX_USAGE_WRITE_ALL;
112 else
113 usage = LP_TEX_USAGE_READ_WRITE;
114
115 /* "prime" the tile: convert data from linear to tiled if necessary
116 * and update the tile's layout info.
117 */
118 (void) llvmpipe_get_texture_tile(lpt,
119 zsbuf->u.tex.first_layer,
120 zsbuf->u.tex.level,
121 usage,
122 task->x,
123 task->y);
124 /* Get actual pointer to the tile data. Note that depth/stencil
125 * data is tiled differently than color data.
126 */
127 task->depth_tile = lp_rast_get_depth_block_pointer(task,
128 task->x,
129 task->y);
130
131 assert(task->depth_tile);
132 }
133 else {
134 task->depth_tile = NULL;
135 }
136 }
137 }
138
139
140 /**
141 * Clear the rasterizer's current color tile.
142 * This is a bin command called during bin processing.
143 */
144 static void
145 lp_rast_clear_color(struct lp_rasterizer_task *task,
146 const union lp_rast_cmd_arg arg)
147 {
148 const struct lp_scene *scene = task->scene;
149
150 if (scene->fb.nr_cbufs) {
151 unsigned i;
152 union util_color uc;
153
154 if (util_format_is_pure_integer(scene->fb.cbufs[0]->format)) {
155 /*
156 * We expect int/uint clear values here, though some APIs
157 * might disagree (but in any case util_pack_color()
158 * couldn't handle it)...
159 */
160 LP_DBG(DEBUG_RAST, "%s pure int 0x%x,0x%x,0x%x,0x%x\n", __FUNCTION__,
161 arg.clear_color.ui[0],
162 arg.clear_color.ui[1],
163 arg.clear_color.ui[2],
164 arg.clear_color.ui[3]);
165
166 for (i = 0; i < scene->fb.nr_cbufs; i++) {
167 enum pipe_format format = scene->fb.cbufs[i]->format;
168
169 if (util_format_is_pure_sint(format)) {
170 util_format_write_4i(format, arg.clear_color.i, 0, &uc, 0, 0, 0, 1, 1);
171 }
172 else {
173 assert(util_format_is_pure_uint(format));
174 util_format_write_4ui(format, arg.clear_color.ui, 0, &uc, 0, 0, 0, 1, 1);
175 }
176
177 util_fill_rect(scene->cbufs[i].map,
178 scene->fb.cbufs[i]->format,
179 scene->cbufs[i].stride,
180 task->x,
181 task->y,
182 TILE_SIZE,
183 TILE_SIZE,
184 &uc);
185 }
186 }
187 else {
188 uint8_t clear_color[4];
189
190 for (i = 0; i < 4; ++i) {
191 clear_color[i] = float_to_ubyte(arg.clear_color.f[i]);
192 }
193
194 LP_DBG(DEBUG_RAST, "%s 0x%x,0x%x,0x%x,0x%x\n", __FUNCTION__,
195 clear_color[0],
196 clear_color[1],
197 clear_color[2],
198 clear_color[3]);
199
200 for (i = 0; i < scene->fb.nr_cbufs; i++) {
201
202 util_pack_color(arg.clear_color.f,
203 scene->fb.cbufs[i]->format, &uc);
204
205 util_fill_rect(scene->cbufs[i].map,
206 scene->fb.cbufs[i]->format,
207 scene->cbufs[i].stride,
208 task->x,
209 task->y,
210 TILE_SIZE,
211 TILE_SIZE,
212 &uc);
213 }
214 }
215 }
216
217 LP_COUNT(nr_color_tile_clear);
218 }
219
220
221
222
223
224
225 /**
226 * Clear the rasterizer's current z/stencil tile.
227 * This is a bin command called during bin processing.
228 */
229 static void
230 lp_rast_clear_zstencil(struct lp_rasterizer_task *task,
231 const union lp_rast_cmd_arg arg)
232 {
233 const struct lp_scene *scene = task->scene;
234 uint32_t clear_value = arg.clear_zstencil.value;
235 uint32_t clear_mask = arg.clear_zstencil.mask;
236 const unsigned height = TILE_SIZE / TILE_VECTOR_HEIGHT;
237 const unsigned width = TILE_SIZE * TILE_VECTOR_HEIGHT;
238 const unsigned block_size = scene->zsbuf.blocksize;
239 const unsigned dst_stride = scene->zsbuf.stride * TILE_VECTOR_HEIGHT;
240 uint8_t *dst;
241 unsigned i, j;
242
243 LP_DBG(DEBUG_RAST, "%s: value=0x%08x, mask=0x%08x\n",
244 __FUNCTION__, clear_value, clear_mask);
245
246 /*
247 * Clear the area of the swizzled depth/depth buffer matching this tile, in
248 * stripes of TILE_VECTOR_HEIGHT x TILE_SIZE at a time.
249 *
250 * The swizzled depth format is such that the depths for
251 * TILE_VECTOR_HEIGHT x TILE_VECTOR_WIDTH pixels have consecutive offsets.
252 */
253
254 dst = task->depth_tile;
255
256 clear_value &= clear_mask;
257
258 switch (block_size) {
259 case 1:
260 assert(clear_mask == 0xff);
261 memset(dst, (uint8_t) clear_value, height * width);
262 break;
263 case 2:
264 if (clear_mask == 0xffff) {
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) clear_value;
269 dst += dst_stride;
270 }
271 }
272 else {
273 for (i = 0; i < height; i++) {
274 uint16_t *row = (uint16_t *)dst;
275 for (j = 0; j < width; j++) {
276 uint16_t tmp = ~clear_mask & *row;
277 *row++ = clear_value | tmp;
278 }
279 dst += dst_stride;
280 }
281 }
282 break;
283 case 4:
284 if (clear_mask == 0xffffffff) {
285 for (i = 0; i < height; i++) {
286 uint32_t *row = (uint32_t *)dst;
287 for (j = 0; j < width; j++)
288 *row++ = clear_value;
289 dst += dst_stride;
290 }
291 }
292 else {
293 for (i = 0; i < height; i++) {
294 uint32_t *row = (uint32_t *)dst;
295 for (j = 0; j < width; j++) {
296 uint32_t tmp = ~clear_mask & *row;
297 *row++ = clear_value | tmp;
298 }
299 dst += dst_stride;
300 }
301 }
302 break;
303 default:
304 assert(0);
305 break;
306 }
307 }
308
309
310
311 /**
312 * Run the shader on all blocks in a tile. This is used when a tile is
313 * completely contained inside a triangle.
314 * This is a bin command called during bin processing.
315 */
316 static void
317 lp_rast_shade_tile(struct lp_rasterizer_task *task,
318 const union lp_rast_cmd_arg arg)
319 {
320 const struct lp_scene *scene = task->scene;
321 const struct lp_rast_shader_inputs *inputs = arg.shade_tile;
322 const struct lp_rast_state *state;
323 struct lp_fragment_shader_variant *variant;
324 const unsigned tile_x = task->x, tile_y = task->y;
325 unsigned x, y;
326
327 if (inputs->disable) {
328 /* This command was partially binned and has been disabled */
329 return;
330 }
331
332 LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
333
334 state = task->state;
335 assert(state);
336 if (!state) {
337 return;
338 }
339 variant = state->variant;
340
341 /* render the whole 64x64 tile in 4x4 chunks */
342 for (y = 0; y < TILE_SIZE; y += 4){
343 for (x = 0; x < TILE_SIZE; x += 4) {
344 uint8_t *color[PIPE_MAX_COLOR_BUFS];
345 unsigned stride[PIPE_MAX_COLOR_BUFS];
346 uint32_t *depth;
347 unsigned i;
348
349 /* color buffer */
350 for (i = 0; i < scene->fb.nr_cbufs; i++){
351 stride[i] = scene->cbufs[i].stride;
352
353 color[i] = lp_rast_get_unswizzled_color_block_pointer(task, i, tile_x + x, tile_y + y);
354 }
355
356 /* depth buffer */
357 depth = lp_rast_get_depth_block_pointer(task, tile_x + x, tile_y + y);
358
359 /* run shader on 4x4 block */
360 BEGIN_JIT_CALL(state, task);
361 variant->jit_function[RAST_WHOLE]( &state->jit_context,
362 tile_x + x, tile_y + y,
363 inputs->frontfacing,
364 GET_A0(inputs),
365 GET_DADX(inputs),
366 GET_DADY(inputs),
367 color,
368 depth,
369 0xffff,
370 &task->thread_data,
371 stride);
372 END_JIT_CALL();
373 }
374 }
375 }
376
377
378 /**
379 * Run the shader on all blocks in a tile. This is used when a tile is
380 * completely contained inside a triangle, and the shader is opaque.
381 * This is a bin command called during bin processing.
382 */
383 static void
384 lp_rast_shade_tile_opaque(struct lp_rasterizer_task *task,
385 const union lp_rast_cmd_arg arg)
386 {
387 LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
388
389 assert(task->state);
390 if (!task->state) {
391 return;
392 }
393
394 lp_rast_shade_tile(task, arg);
395 }
396
397
398 /**
399 * Compute shading for a 4x4 block of pixels inside a triangle.
400 * This is a bin command called during bin processing.
401 * \param x X position of quad in window coords
402 * \param y Y position of quad in window coords
403 */
404 void
405 lp_rast_shade_quads_mask(struct lp_rasterizer_task *task,
406 const struct lp_rast_shader_inputs *inputs,
407 unsigned x, unsigned y,
408 unsigned mask)
409 {
410 const struct lp_rast_state *state = task->state;
411 struct lp_fragment_shader_variant *variant = state->variant;
412 const struct lp_scene *scene = task->scene;
413 uint8_t *color[PIPE_MAX_COLOR_BUFS];
414 unsigned stride[PIPE_MAX_COLOR_BUFS];
415 void *depth;
416 unsigned i;
417
418 assert(state);
419
420 /* Sanity checks */
421 assert(x < scene->tiles_x * TILE_SIZE);
422 assert(y < scene->tiles_y * TILE_SIZE);
423 assert(x % TILE_VECTOR_WIDTH == 0);
424 assert(y % TILE_VECTOR_HEIGHT == 0);
425
426 assert((x % 4) == 0);
427 assert((y % 4) == 0);
428
429 /* color buffer */
430 for (i = 0; i < scene->fb.nr_cbufs; i++) {
431 stride[i] = scene->cbufs[i].stride;
432
433 color[i] = lp_rast_get_unswizzled_color_block_pointer(task, i, x, y);
434 }
435
436 /* depth buffer */
437 depth = lp_rast_get_depth_block_pointer(task, x, y);
438
439
440 assert(lp_check_alignment(state->jit_context.u8_blend_color, 16));
441
442 /* run shader on 4x4 block */
443 BEGIN_JIT_CALL(state, task);
444 variant->jit_function[RAST_EDGE_TEST](&state->jit_context,
445 x, y,
446 inputs->frontfacing,
447 GET_A0(inputs),
448 GET_DADX(inputs),
449 GET_DADY(inputs),
450 color,
451 depth,
452 mask,
453 &task->thread_data,
454 stride);
455 END_JIT_CALL();
456 }
457
458
459
460 /**
461 * Begin a new occlusion query.
462 * This is a bin command put in all bins.
463 * Called per thread.
464 */
465 static void
466 lp_rast_begin_query(struct lp_rasterizer_task *task,
467 const union lp_rast_cmd_arg arg)
468 {
469 struct llvmpipe_query *pq = arg.query_obj;
470
471 assert(task->query[pq->type] == NULL);
472
473 switch (pq->type) {
474 case PIPE_QUERY_OCCLUSION_COUNTER:
475 task->thread_data.vis_counter = 0;
476 break;
477 case PIPE_QUERY_PRIMITIVES_GENERATED:
478 case PIPE_QUERY_PRIMITIVES_EMITTED:
479 case PIPE_QUERY_SO_STATISTICS:
480 break;
481 default:
482 assert(0);
483 break;
484 }
485
486 task->query[pq->type] = pq;
487 }
488
489
490 /**
491 * End the current occlusion query.
492 * This is a bin command put in all bins.
493 * Called per thread.
494 */
495 static void
496 lp_rast_end_query(struct lp_rasterizer_task *task,
497 const union lp_rast_cmd_arg arg)
498 {
499 struct llvmpipe_query *pq = arg.query_obj;
500 assert(task->query[pq->type] == pq || pq->type == PIPE_QUERY_TIMESTAMP);
501
502 switch (pq->type) {
503 case PIPE_QUERY_OCCLUSION_COUNTER:
504 pq->count[task->thread_index] += task->thread_data.vis_counter;
505 break;
506 case PIPE_QUERY_TIMESTAMP:
507 pq->count[task->thread_index] = os_time_get_nano();
508 break;
509 case PIPE_QUERY_PRIMITIVES_GENERATED:
510 case PIPE_QUERY_PRIMITIVES_EMITTED:
511 case PIPE_QUERY_SO_STATISTICS:
512 break;
513 default:
514 assert(0);
515 break;
516 }
517
518 if (task->query[pq->type] == pq) {
519 task->query[pq->type] = NULL;
520 }
521 }
522
523
524 void
525 lp_rast_set_state(struct lp_rasterizer_task *task,
526 const union lp_rast_cmd_arg arg)
527 {
528 task->state = arg.state;
529 }
530
531
532
533 /**
534 * Called when we're done writing to a color tile.
535 */
536 static void
537 lp_rast_tile_end(struct lp_rasterizer_task *task)
538 {
539 unsigned i;
540
541 for (i = 0; i < PIPE_QUERY_TYPES; ++i) {
542 if (task->query[i]) {
543 lp_rast_end_query(task, lp_rast_arg_query(task->query[i]));
544 }
545 }
546
547 /* debug */
548 memset(task->color_tiles, 0, sizeof(task->color_tiles));
549 task->depth_tile = NULL;
550
551 task->bin = NULL;
552 }
553
554 static lp_rast_cmd_func dispatch[LP_RAST_OP_MAX] =
555 {
556 lp_rast_clear_color,
557 lp_rast_clear_zstencil,
558 lp_rast_triangle_1,
559 lp_rast_triangle_2,
560 lp_rast_triangle_3,
561 lp_rast_triangle_4,
562 lp_rast_triangle_5,
563 lp_rast_triangle_6,
564 lp_rast_triangle_7,
565 lp_rast_triangle_8,
566 lp_rast_triangle_3_4,
567 lp_rast_triangle_3_16,
568 lp_rast_triangle_4_16,
569 lp_rast_shade_tile,
570 lp_rast_shade_tile_opaque,
571 lp_rast_begin_query,
572 lp_rast_end_query,
573 lp_rast_set_state,
574 };
575
576
577 static void
578 do_rasterize_bin(struct lp_rasterizer_task *task,
579 const struct cmd_bin *bin)
580 {
581 const struct cmd_block *block;
582 unsigned k;
583
584 if (0)
585 lp_debug_bin(bin);
586
587 for (block = bin->head; block; block = block->next) {
588 for (k = 0; k < block->count; k++) {
589 dispatch[block->cmd[k]]( task, block->arg[k] );
590 }
591 }
592 }
593
594
595
596 /**
597 * Rasterize commands for a single bin.
598 * \param x, y position of the bin's tile in the framebuffer
599 * Must be called between lp_rast_begin() and lp_rast_end().
600 * Called per thread.
601 */
602 static void
603 rasterize_bin(struct lp_rasterizer_task *task,
604 const struct cmd_bin *bin )
605 {
606 lp_rast_tile_begin( task, bin );
607
608 do_rasterize_bin(task, bin);
609
610 lp_rast_tile_end(task);
611
612
613 /* Debug/Perf flags:
614 */
615 if (bin->head->count == 1) {
616 if (bin->head->cmd[0] == LP_RAST_OP_SHADE_TILE_OPAQUE)
617 LP_COUNT(nr_pure_shade_opaque_64);
618 else if (bin->head->cmd[0] == LP_RAST_OP_SHADE_TILE)
619 LP_COUNT(nr_pure_shade_64);
620 }
621 }
622
623
624 /* An empty bin is one that just loads the contents of the tile and
625 * stores them again unchanged. This typically happens when bins have
626 * been flushed for some reason in the middle of a frame, or when
627 * incremental updates are being made to a render target.
628 *
629 * Try to avoid doing pointless work in this case.
630 */
631 static boolean
632 is_empty_bin( const struct cmd_bin *bin )
633 {
634 return bin->head == NULL;
635 }
636
637
638 /**
639 * Rasterize/execute all bins within a scene.
640 * Called per thread.
641 */
642 static void
643 rasterize_scene(struct lp_rasterizer_task *task,
644 struct lp_scene *scene)
645 {
646 task->scene = scene;
647
648 if (!task->rast->no_rast && !scene->discard) {
649 /* loop over scene bins, rasterize each */
650 #if 0
651 {
652 unsigned i, j;
653 for (i = 0; i < scene->tiles_x; i++) {
654 for (j = 0; j < scene->tiles_y; j++) {
655 struct cmd_bin *bin = lp_scene_get_bin(scene, i, j);
656 rasterize_bin(task, bin, i, j);
657 }
658 }
659 }
660 #else
661 {
662 struct cmd_bin *bin;
663
664 assert(scene);
665 while ((bin = lp_scene_bin_iter_next(scene))) {
666 if (!is_empty_bin( bin ))
667 rasterize_bin(task, bin);
668 }
669 }
670 #endif
671 }
672
673
674 if (scene->fence) {
675 lp_fence_signal(scene->fence);
676 }
677
678 task->scene = NULL;
679 }
680
681
682 /**
683 * Called by setup module when it has something for us to render.
684 */
685 void
686 lp_rast_queue_scene( struct lp_rasterizer *rast,
687 struct lp_scene *scene)
688 {
689 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
690
691 if (rast->num_threads == 0) {
692 /* no threading */
693
694 lp_rast_begin( rast, scene );
695
696 rasterize_scene( &rast->tasks[0], scene );
697
698 lp_rast_end( rast );
699
700 rast->curr_scene = NULL;
701 }
702 else {
703 /* threaded rendering! */
704 unsigned i;
705
706 lp_scene_enqueue( rast->full_scenes, scene );
707
708 /* signal the threads that there's work to do */
709 for (i = 0; i < rast->num_threads; i++) {
710 pipe_semaphore_signal(&rast->tasks[i].work_ready);
711 }
712 }
713
714 LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
715 }
716
717
718 void
719 lp_rast_finish( struct lp_rasterizer *rast )
720 {
721 if (rast->num_threads == 0) {
722 /* nothing to do */
723 }
724 else {
725 int i;
726
727 /* wait for work to complete */
728 for (i = 0; i < rast->num_threads; i++) {
729 pipe_semaphore_wait(&rast->tasks[i].work_done);
730 }
731 }
732 }
733
734
735 /**
736 * This is the thread's main entrypoint.
737 * It's a simple loop:
738 * 1. wait for work
739 * 2. do work
740 * 3. signal that we're done
741 */
742 static PIPE_THREAD_ROUTINE( thread_function, init_data )
743 {
744 struct lp_rasterizer_task *task = (struct lp_rasterizer_task *) init_data;
745 struct lp_rasterizer *rast = task->rast;
746 boolean debug = false;
747
748 while (1) {
749 /* wait for work */
750 if (debug)
751 debug_printf("thread %d waiting for work\n", task->thread_index);
752 pipe_semaphore_wait(&task->work_ready);
753
754 if (rast->exit_flag)
755 break;
756
757 if (task->thread_index == 0) {
758 /* thread[0]:
759 * - get next scene to rasterize
760 * - map the framebuffer surfaces
761 */
762 lp_rast_begin( rast,
763 lp_scene_dequeue( rast->full_scenes, TRUE ) );
764 }
765
766 /* Wait for all threads to get here so that threads[1+] don't
767 * get a null rast->curr_scene pointer.
768 */
769 pipe_barrier_wait( &rast->barrier );
770
771 /* do work */
772 if (debug)
773 debug_printf("thread %d doing work\n", task->thread_index);
774
775 rasterize_scene(task,
776 rast->curr_scene);
777
778 /* wait for all threads to finish with this scene */
779 pipe_barrier_wait( &rast->barrier );
780
781 /* XXX: shouldn't be necessary:
782 */
783 if (task->thread_index == 0) {
784 lp_rast_end( rast );
785 }
786
787 /* signal done with work */
788 if (debug)
789 debug_printf("thread %d done working\n", task->thread_index);
790
791 pipe_semaphore_signal(&task->work_done);
792 }
793
794 return NULL;
795 }
796
797
798 /**
799 * Initialize semaphores and spawn the threads.
800 */
801 static void
802 create_rast_threads(struct lp_rasterizer *rast)
803 {
804 unsigned i;
805
806 /* NOTE: if num_threads is zero, we won't use any threads */
807 for (i = 0; i < rast->num_threads; i++) {
808 pipe_semaphore_init(&rast->tasks[i].work_ready, 0);
809 pipe_semaphore_init(&rast->tasks[i].work_done, 0);
810 rast->threads[i] = pipe_thread_create(thread_function,
811 (void *) &rast->tasks[i]);
812 }
813 }
814
815
816
817 /**
818 * Create new lp_rasterizer. If num_threads is zero, don't create any
819 * new threads, do rendering synchronously.
820 * \param num_threads number of rasterizer threads to create
821 */
822 struct lp_rasterizer *
823 lp_rast_create( unsigned num_threads )
824 {
825 struct lp_rasterizer *rast;
826 unsigned i;
827
828 rast = CALLOC_STRUCT(lp_rasterizer);
829 if (!rast) {
830 goto no_rast;
831 }
832
833 rast->full_scenes = lp_scene_queue_create();
834 if (!rast->full_scenes) {
835 goto no_full_scenes;
836 }
837
838 for (i = 0; i < Elements(rast->tasks); i++) {
839 struct lp_rasterizer_task *task = &rast->tasks[i];
840 task->rast = rast;
841 task->thread_index = i;
842 }
843
844 rast->num_threads = num_threads;
845
846 rast->no_rast = debug_get_bool_option("LP_NO_RAST", FALSE);
847
848 create_rast_threads(rast);
849
850 /* for synchronizing rasterization threads */
851 pipe_barrier_init( &rast->barrier, rast->num_threads );
852
853 memset(lp_dummy_tile, 0, sizeof lp_dummy_tile);
854
855 return rast;
856
857 no_full_scenes:
858 FREE(rast);
859 no_rast:
860 return NULL;
861 }
862
863
864 /* Shutdown:
865 */
866 void lp_rast_destroy( struct lp_rasterizer *rast )
867 {
868 unsigned i;
869
870 /* Set exit_flag and signal each thread's work_ready semaphore.
871 * Each thread will be woken up, notice that the exit_flag is set and
872 * break out of its main loop. The thread will then exit.
873 */
874 rast->exit_flag = TRUE;
875 for (i = 0; i < rast->num_threads; i++) {
876 pipe_semaphore_signal(&rast->tasks[i].work_ready);
877 }
878
879 /* Wait for threads to terminate before cleaning up per-thread data */
880 for (i = 0; i < rast->num_threads; i++) {
881 pipe_thread_wait(rast->threads[i]);
882 }
883
884 /* Clean up per-thread data */
885 for (i = 0; i < rast->num_threads; i++) {
886 pipe_semaphore_destroy(&rast->tasks[i].work_ready);
887 pipe_semaphore_destroy(&rast->tasks[i].work_done);
888 }
889
890 /* for synchronizing rasterization threads */
891 pipe_barrier_destroy( &rast->barrier );
892
893 lp_scene_queue_destroy(rast->full_scenes);
894
895 FREE(rast);
896 }
897
898
899 /** Return number of rasterization threads */
900 unsigned
901 lp_rast_get_num_threads( struct lp_rasterizer *rast )
902 {
903 return rast->num_threads;
904 }
905
906