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