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