be5a286e3daa94267f40bc68b3f21455c3f57720
[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 int x, int y)
92 {
93 LP_DBG(DEBUG_RAST, "%s %d,%d\n", __FUNCTION__, x, y);
94
95 task->bin = bin;
96 task->x = x * TILE_SIZE;
97 task->y = y * TILE_SIZE;
98 task->width = TILE_SIZE + x * TILE_SIZE > task->scene->width_aligned ?
99 task->scene->width_aligned - x * TILE_SIZE : TILE_SIZE;
100 task->height = TILE_SIZE + y * TILE_SIZE > task->scene->height_aligned ?
101 task->scene->height_aligned - y * TILE_SIZE : TILE_SIZE;
102
103 /* reset pointers to color and depth tile(s) */
104 memset(task->color_tiles, 0, sizeof(task->color_tiles));
105 task->depth_tile = NULL;
106 }
107
108
109 /**
110 * Clear the rasterizer's current color tile.
111 * This is a bin command called during bin processing.
112 */
113 static void
114 lp_rast_clear_color(struct lp_rasterizer_task *task,
115 const union lp_rast_cmd_arg arg)
116 {
117 const struct lp_scene *scene = task->scene;
118
119 if (scene->fb.nr_cbufs) {
120 unsigned i;
121 union util_color uc;
122
123 if (util_format_is_pure_integer(scene->fb.cbufs[0]->format)) {
124 /*
125 * We expect int/uint clear values here, though some APIs
126 * might disagree (but in any case util_pack_color()
127 * couldn't handle it)...
128 */
129 LP_DBG(DEBUG_RAST, "%s pure int 0x%x,0x%x,0x%x,0x%x\n", __FUNCTION__,
130 arg.clear_color.ui[0],
131 arg.clear_color.ui[1],
132 arg.clear_color.ui[2],
133 arg.clear_color.ui[3]);
134
135 for (i = 0; i < scene->fb.nr_cbufs; i++) {
136 enum pipe_format format = scene->fb.cbufs[i]->format;
137
138 if (util_format_is_pure_sint(format)) {
139 util_format_write_4i(format, arg.clear_color.i, 0, &uc, 0, 0, 0, 1, 1);
140 }
141 else {
142 assert(util_format_is_pure_uint(format));
143 util_format_write_4ui(format, arg.clear_color.ui, 0, &uc, 0, 0, 0, 1, 1);
144 }
145
146 util_fill_rect(scene->cbufs[i].map,
147 scene->fb.cbufs[i]->format,
148 scene->cbufs[i].stride,
149 task->x,
150 task->y,
151 task->width,
152 task->height,
153 &uc);
154 }
155 }
156 else {
157 uint8_t clear_color[4];
158
159 for (i = 0; i < 4; ++i) {
160 clear_color[i] = float_to_ubyte(arg.clear_color.f[i]);
161 }
162
163 LP_DBG(DEBUG_RAST, "%s 0x%x,0x%x,0x%x,0x%x\n", __FUNCTION__,
164 clear_color[0],
165 clear_color[1],
166 clear_color[2],
167 clear_color[3]);
168
169 for (i = 0; i < scene->fb.nr_cbufs; i++) {
170
171 util_pack_color(arg.clear_color.f,
172 scene->fb.cbufs[i]->format, &uc);
173
174 util_fill_rect(scene->cbufs[i].map,
175 scene->fb.cbufs[i]->format,
176 scene->cbufs[i].stride,
177 task->x,
178 task->y,
179 task->width,
180 task->height,
181 &uc);
182 }
183 }
184 }
185
186 LP_COUNT(nr_color_tile_clear);
187 }
188
189
190
191
192 /**
193 * Clear the rasterizer's current z/stencil tile.
194 * This is a bin command called during bin processing.
195 */
196 static void
197 lp_rast_clear_zstencil(struct lp_rasterizer_task *task,
198 const union lp_rast_cmd_arg arg)
199 {
200 const struct lp_scene *scene = task->scene;
201 uint64_t clear_value64 = arg.clear_zstencil.value;
202 uint64_t clear_mask64 = arg.clear_zstencil.mask;
203 uint32_t clear_value = (uint32_t) clear_value64;
204 uint32_t clear_mask = (uint32_t) clear_mask64;
205 const unsigned height = task->height;
206 const unsigned width = task->width;
207 const unsigned block_size = scene->zsbuf.blocksize;
208 const unsigned dst_stride = scene->zsbuf.stride;
209 uint8_t *dst;
210 unsigned i, j;
211
212 LP_DBG(DEBUG_RAST, "%s: value=0x%08x, mask=0x%08x\n",
213 __FUNCTION__, clear_value, clear_mask);
214
215 /*
216 * Clear the area of the depth/depth buffer matching this tile.
217 */
218
219 if (scene->fb.zsbuf) {
220
221 dst = lp_rast_get_unswizzled_depth_tile_pointer(task, LP_TEX_USAGE_READ_WRITE);
222
223 clear_value &= clear_mask;
224
225 switch (block_size) {
226 case 1:
227 assert(clear_mask == 0xff);
228 memset(dst, (uint8_t) clear_value, height * width);
229 break;
230 case 2:
231 if (clear_mask == 0xffff) {
232 for (i = 0; i < height; i++) {
233 uint16_t *row = (uint16_t *)dst;
234 for (j = 0; j < width; j++)
235 *row++ = (uint16_t) clear_value;
236 dst += dst_stride;
237 }
238 }
239 else {
240 for (i = 0; i < height; i++) {
241 uint16_t *row = (uint16_t *)dst;
242 for (j = 0; j < width; j++) {
243 uint16_t tmp = ~clear_mask & *row;
244 *row++ = clear_value | tmp;
245 }
246 dst += dst_stride;
247 }
248 }
249 break;
250 case 4:
251 if (clear_mask == 0xffffffff) {
252 for (i = 0; i < height; i++) {
253 uint32_t *row = (uint32_t *)dst;
254 for (j = 0; j < width; j++)
255 *row++ = clear_value;
256 dst += dst_stride;
257 }
258 }
259 else {
260 for (i = 0; i < height; i++) {
261 uint32_t *row = (uint32_t *)dst;
262 for (j = 0; j < width; j++) {
263 uint32_t tmp = ~clear_mask & *row;
264 *row++ = clear_value | tmp;
265 }
266 dst += dst_stride;
267 }
268 }
269 break;
270 case 8:
271 clear_value64 &= clear_mask64;
272 if (clear_mask64 == 0xffffffffffULL) {
273 for (i = 0; i < height; i++) {
274 uint64_t *row = (uint64_t *)dst;
275 for (j = 0; j < width; j++)
276 *row++ = clear_value64;
277 dst += dst_stride;
278 }
279 }
280 else {
281 for (i = 0; i < height; i++) {
282 uint64_t *row = (uint64_t *)dst;
283 for (j = 0; j < width; j++) {
284 uint64_t tmp = ~clear_mask64 & *row;
285 *row++ = clear_value64 | tmp;
286 }
287 dst += dst_stride;
288 }
289 }
290 break;
291
292 default:
293 assert(0);
294 break;
295 }
296 }
297 }
298
299
300
301 /**
302 * Run the shader on all blocks in a tile. This is used when a tile is
303 * completely contained inside a triangle.
304 * This is a bin command called during bin processing.
305 */
306 static void
307 lp_rast_shade_tile(struct lp_rasterizer_task *task,
308 const union lp_rast_cmd_arg arg)
309 {
310 const struct lp_scene *scene = task->scene;
311 const struct lp_rast_shader_inputs *inputs = arg.shade_tile;
312 const struct lp_rast_state *state;
313 struct lp_fragment_shader_variant *variant;
314 const unsigned tile_x = task->x, tile_y = task->y;
315 unsigned x, y;
316
317 if (inputs->disable) {
318 /* This command was partially binned and has been disabled */
319 return;
320 }
321
322 LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
323
324 state = task->state;
325 assert(state);
326 if (!state) {
327 return;
328 }
329 variant = state->variant;
330
331 /* render the whole 64x64 tile in 4x4 chunks */
332 for (y = 0; y < task->height; y += 4){
333 for (x = 0; x < task->width; x += 4) {
334 uint8_t *color[PIPE_MAX_COLOR_BUFS];
335 unsigned stride[PIPE_MAX_COLOR_BUFS];
336 uint8_t *depth = NULL;
337 unsigned depth_stride = 0;
338 unsigned i;
339
340 /* color buffer */
341 for (i = 0; i < scene->fb.nr_cbufs; i++){
342 stride[i] = scene->cbufs[i].stride;
343
344 color[i] = lp_rast_get_unswizzled_color_block_pointer(task, i, tile_x + x, tile_y + y);
345 }
346
347 /* depth buffer */
348 if (scene->zsbuf.map) {
349 depth = lp_rast_get_unswizzled_depth_block_pointer(task, tile_x + x, tile_y + y);
350 depth_stride = scene->zsbuf.stride;
351 }
352
353
354 /* run shader on 4x4 block */
355 BEGIN_JIT_CALL(state, task);
356 variant->jit_function[RAST_WHOLE]( &state->jit_context,
357 tile_x + x, tile_y + y,
358 inputs->frontfacing,
359 GET_A0(inputs),
360 GET_DADX(inputs),
361 GET_DADY(inputs),
362 color,
363 depth,
364 0xffff,
365 &task->thread_data,
366 stride,
367 depth_stride);
368 END_JIT_CALL();
369 }
370 }
371 }
372
373
374 /**
375 * Run the shader on all blocks in a tile. This is used when a tile is
376 * completely contained inside a triangle, and the shader is opaque.
377 * This is a bin command called during bin processing.
378 */
379 static void
380 lp_rast_shade_tile_opaque(struct lp_rasterizer_task *task,
381 const union lp_rast_cmd_arg arg)
382 {
383 LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
384
385 assert(task->state);
386 if (!task->state) {
387 return;
388 }
389
390 lp_rast_shade_tile(task, arg);
391 }
392
393
394 /**
395 * Compute shading for a 4x4 block of pixels inside a triangle.
396 * This is a bin command called during bin processing.
397 * \param x X position of quad in window coords
398 * \param y Y position of quad in window coords
399 */
400 void
401 lp_rast_shade_quads_mask(struct lp_rasterizer_task *task,
402 const struct lp_rast_shader_inputs *inputs,
403 unsigned x, unsigned y,
404 unsigned mask)
405 {
406 const struct lp_rast_state *state = task->state;
407 struct lp_fragment_shader_variant *variant = state->variant;
408 const struct lp_scene *scene = task->scene;
409 uint8_t *color[PIPE_MAX_COLOR_BUFS];
410 unsigned stride[PIPE_MAX_COLOR_BUFS];
411 void *depth = NULL;
412 unsigned depth_stride = 0;
413 unsigned i;
414
415 assert(state);
416
417 /* Sanity checks */
418 assert(x < scene->tiles_x * TILE_SIZE);
419 assert(y < scene->tiles_y * TILE_SIZE);
420 assert(x % TILE_VECTOR_WIDTH == 0);
421 assert(y % TILE_VECTOR_HEIGHT == 0);
422
423 assert((x % 4) == 0);
424 assert((y % 4) == 0);
425
426 /* color buffer */
427 for (i = 0; i < scene->fb.nr_cbufs; i++) {
428 stride[i] = scene->cbufs[i].stride;
429
430 color[i] = lp_rast_get_unswizzled_color_block_pointer(task, i, x, y);
431 }
432
433 /* depth buffer */
434 if (scene->zsbuf.map) {
435 depth_stride = scene->zsbuf.stride;
436 depth = lp_rast_get_unswizzled_depth_block_pointer(task, x, y);
437 }
438
439 assert(lp_check_alignment(state->jit_context.u8_blend_color, 16));
440
441 /*
442 * The rasterizer may produce fragments outside our
443 * allocated 4x4 blocks hence need to filter them out here.
444 */
445 if ((x % TILE_SIZE) < task->width && (y % TILE_SIZE) < task->height) {
446 /* run shader on 4x4 block */
447 BEGIN_JIT_CALL(state, task);
448 variant->jit_function[RAST_EDGE_TEST](&state->jit_context,
449 x, y,
450 inputs->frontfacing,
451 GET_A0(inputs),
452 GET_DADX(inputs),
453 GET_DADY(inputs),
454 color,
455 depth,
456 mask,
457 &task->thread_data,
458 stride,
459 depth_stride);
460 END_JIT_CALL();
461 }
462 }
463
464
465
466 /**
467 * Begin a new occlusion query.
468 * This is a bin command put in all bins.
469 * Called per thread.
470 */
471 static void
472 lp_rast_begin_query(struct lp_rasterizer_task *task,
473 const union lp_rast_cmd_arg arg)
474 {
475 struct llvmpipe_query *pq = arg.query_obj;
476
477 assert(task->query[pq->type] == NULL);
478
479 switch (pq->type) {
480 case PIPE_QUERY_OCCLUSION_COUNTER:
481 task->thread_data.vis_counter = 0;
482 break;
483 case PIPE_QUERY_PRIMITIVES_GENERATED:
484 case PIPE_QUERY_PRIMITIVES_EMITTED:
485 case PIPE_QUERY_SO_STATISTICS:
486 case PIPE_QUERY_PIPELINE_STATISTICS:
487 case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
488 break;
489 default:
490 assert(0);
491 break;
492 }
493
494 task->query[pq->type] = pq;
495 }
496
497
498 /**
499 * End the current occlusion query.
500 * This is a bin command put in all bins.
501 * Called per thread.
502 */
503 static void
504 lp_rast_end_query(struct lp_rasterizer_task *task,
505 const union lp_rast_cmd_arg arg)
506 {
507 struct llvmpipe_query *pq = arg.query_obj;
508 assert(task->query[pq->type] == pq || pq->type == PIPE_QUERY_TIMESTAMP);
509
510 switch (pq->type) {
511 case PIPE_QUERY_OCCLUSION_COUNTER:
512 pq->count[task->thread_index] += task->thread_data.vis_counter;
513 break;
514 case PIPE_QUERY_TIMESTAMP:
515 pq->count[task->thread_index] = os_time_get_nano();
516 break;
517 case PIPE_QUERY_PRIMITIVES_GENERATED:
518 case PIPE_QUERY_PRIMITIVES_EMITTED:
519 case PIPE_QUERY_SO_STATISTICS:
520 case PIPE_QUERY_PIPELINE_STATISTICS:
521 case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
522 break;
523 default:
524 assert(0);
525 break;
526 }
527
528 if (task->query[pq->type] == pq) {
529 task->query[pq->type] = NULL;
530 }
531 }
532
533
534 void
535 lp_rast_set_state(struct lp_rasterizer_task *task,
536 const union lp_rast_cmd_arg arg)
537 {
538 task->state = arg.state;
539 }
540
541
542
543 /**
544 * Called when we're done writing to a color tile.
545 */
546 static void
547 lp_rast_tile_end(struct lp_rasterizer_task *task)
548 {
549 unsigned i;
550
551 for (i = 0; i < PIPE_QUERY_TYPES; ++i) {
552 if (task->query[i]) {
553 lp_rast_end_query(task, lp_rast_arg_query(task->query[i]));
554 }
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 };
585
586
587 static void
588 do_rasterize_bin(struct lp_rasterizer_task *task,
589 const struct cmd_bin *bin,
590 int x, int y)
591 {
592 const struct cmd_block *block;
593 unsigned k;
594
595 if (0)
596 lp_debug_bin(bin, x, y);
597
598 for (block = bin->head; block; block = block->next) {
599 for (k = 0; k < block->count; k++) {
600 dispatch[block->cmd[k]]( task, block->arg[k] );
601 }
602 }
603 }
604
605
606
607 /**
608 * Rasterize commands for a single bin.
609 * \param x, y position of the bin's tile in the framebuffer
610 * Must be called between lp_rast_begin() and lp_rast_end().
611 * Called per thread.
612 */
613 static void
614 rasterize_bin(struct lp_rasterizer_task *task,
615 const struct cmd_bin *bin, int x, int y )
616 {
617 lp_rast_tile_begin( task, bin, x, y );
618
619 do_rasterize_bin(task, bin, x, y);
620
621 lp_rast_tile_end(task);
622
623
624 /* Debug/Perf flags:
625 */
626 if (bin->head->count == 1) {
627 if (bin->head->cmd[0] == LP_RAST_OP_SHADE_TILE_OPAQUE)
628 LP_COUNT(nr_pure_shade_opaque_64);
629 else if (bin->head->cmd[0] == LP_RAST_OP_SHADE_TILE)
630 LP_COUNT(nr_pure_shade_64);
631 }
632 }
633
634
635 /* An empty bin is one that just loads the contents of the tile and
636 * stores them again unchanged. This typically happens when bins have
637 * been flushed for some reason in the middle of a frame, or when
638 * incremental updates are being made to a render target.
639 *
640 * Try to avoid doing pointless work in this case.
641 */
642 static boolean
643 is_empty_bin( const struct cmd_bin *bin )
644 {
645 return bin->head == NULL;
646 }
647
648
649 /**
650 * Rasterize/execute all bins within a scene.
651 * Called per thread.
652 */
653 static void
654 rasterize_scene(struct lp_rasterizer_task *task,
655 struct lp_scene *scene)
656 {
657 task->scene = scene;
658
659 if (!task->rast->no_rast && !scene->discard) {
660 /* loop over scene bins, rasterize each */
661 {
662 struct cmd_bin *bin;
663 int i, j;
664
665 assert(scene);
666 while ((bin = lp_scene_bin_iter_next(scene, &i, &j))) {
667 if (!is_empty_bin( bin ))
668 rasterize_bin(task, bin, i, j);
669 }
670 }
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