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