llvmpipe: EXT_transform_feedback support (v1.1)
[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 case PIPE_QUERY_PRIMITIVES_GENERATED:
444 case PIPE_QUERY_PRIMITIVES_EMITTED:
445 break;
446 default:
447 assert(0);
448 break;
449 }
450
451 task->query[pq->type] = pq;
452 }
453
454
455 /**
456 * End the current occlusion query.
457 * This is a bin command put in all bins.
458 * Called per thread.
459 */
460 static void
461 lp_rast_end_query(struct lp_rasterizer_task *task,
462 const union lp_rast_cmd_arg arg)
463 {
464 struct llvmpipe_query *pq = arg.query_obj;
465 assert(task->query[pq->type] == pq || pq->type == PIPE_QUERY_TIMESTAMP);
466
467 switch (pq->type) {
468 case PIPE_QUERY_OCCLUSION_COUNTER:
469 pq->count[task->thread_index] += task->vis_counter;
470 break;
471 case PIPE_QUERY_TIME_ELAPSED:
472 pq->count[task->thread_index] = os_time_get_nano() - task->query_start;
473 break;
474 case PIPE_QUERY_TIMESTAMP:
475 pq->count[task->thread_index] = os_time_get_nano();
476 break;
477 case PIPE_QUERY_PRIMITIVES_GENERATED:
478 case PIPE_QUERY_PRIMITIVES_EMITTED:
479 break;
480 default:
481 assert(0);
482 break;
483 }
484
485 if (task->query[pq->type] == pq) {
486 task->query[pq->type] = NULL;
487 }
488 }
489
490
491 void
492 lp_rast_set_state(struct lp_rasterizer_task *task,
493 const union lp_rast_cmd_arg arg)
494 {
495 task->state = arg.state;
496 }
497
498
499
500 /**
501 * Called when we're done writing to a color tile.
502 */
503 static void
504 lp_rast_tile_end(struct lp_rasterizer_task *task)
505 {
506 unsigned i;
507
508 for (i = 0; i < PIPE_QUERY_TYPES; ++i) {
509 if (task->query[i]) {
510 lp_rast_end_query(task, lp_rast_arg_query(task->query[i]));
511 }
512 }
513
514 /* debug */
515 memset(task->color_tiles, 0, sizeof(task->color_tiles));
516 task->depth_tile = NULL;
517
518 task->bin = NULL;
519 }
520
521 static lp_rast_cmd_func dispatch[LP_RAST_OP_MAX] =
522 {
523 lp_rast_clear_color,
524 lp_rast_clear_zstencil,
525 lp_rast_triangle_1,
526 lp_rast_triangle_2,
527 lp_rast_triangle_3,
528 lp_rast_triangle_4,
529 lp_rast_triangle_5,
530 lp_rast_triangle_6,
531 lp_rast_triangle_7,
532 lp_rast_triangle_8,
533 lp_rast_triangle_3_4,
534 lp_rast_triangle_3_16,
535 lp_rast_triangle_4_16,
536 lp_rast_shade_tile,
537 lp_rast_shade_tile_opaque,
538 lp_rast_begin_query,
539 lp_rast_end_query,
540 lp_rast_set_state,
541 };
542
543
544 static void
545 do_rasterize_bin(struct lp_rasterizer_task *task,
546 const struct cmd_bin *bin)
547 {
548 const struct cmd_block *block;
549 unsigned k;
550
551 if (0)
552 lp_debug_bin(bin);
553
554 for (block = bin->head; block; block = block->next) {
555 for (k = 0; k < block->count; k++) {
556 dispatch[block->cmd[k]]( task, block->arg[k] );
557 }
558 }
559 }
560
561
562
563 /**
564 * Rasterize commands for a single bin.
565 * \param x, y position of the bin's tile in the framebuffer
566 * Must be called between lp_rast_begin() and lp_rast_end().
567 * Called per thread.
568 */
569 static void
570 rasterize_bin(struct lp_rasterizer_task *task,
571 const struct cmd_bin *bin )
572 {
573 lp_rast_tile_begin( task, bin );
574
575 do_rasterize_bin(task, bin);
576
577 lp_rast_tile_end(task);
578
579
580 /* Debug/Perf flags:
581 */
582 if (bin->head->count == 1) {
583 if (bin->head->cmd[0] == LP_RAST_OP_SHADE_TILE_OPAQUE)
584 LP_COUNT(nr_pure_shade_opaque_64);
585 else if (bin->head->cmd[0] == LP_RAST_OP_SHADE_TILE)
586 LP_COUNT(nr_pure_shade_64);
587 }
588 }
589
590
591 /* An empty bin is one that just loads the contents of the tile and
592 * stores them again unchanged. This typically happens when bins have
593 * been flushed for some reason in the middle of a frame, or when
594 * incremental updates are being made to a render target.
595 *
596 * Try to avoid doing pointless work in this case.
597 */
598 static boolean
599 is_empty_bin( const struct cmd_bin *bin )
600 {
601 return bin->head == NULL;
602 }
603
604
605 /**
606 * Rasterize/execute all bins within a scene.
607 * Called per thread.
608 */
609 static void
610 rasterize_scene(struct lp_rasterizer_task *task,
611 struct lp_scene *scene)
612 {
613 task->scene = scene;
614
615 if (!task->rast->no_rast && !scene->discard) {
616 /* loop over scene bins, rasterize each */
617 #if 0
618 {
619 unsigned i, j;
620 for (i = 0; i < scene->tiles_x; i++) {
621 for (j = 0; j < scene->tiles_y; j++) {
622 struct cmd_bin *bin = lp_scene_get_bin(scene, i, j);
623 rasterize_bin(task, bin, i, j);
624 }
625 }
626 }
627 #else
628 {
629 struct cmd_bin *bin;
630
631 assert(scene);
632 while ((bin = lp_scene_bin_iter_next(scene))) {
633 if (!is_empty_bin( bin ))
634 rasterize_bin(task, bin);
635 }
636 }
637 #endif
638 }
639
640
641 if (scene->fence) {
642 lp_fence_signal(scene->fence);
643 }
644
645 task->scene = NULL;
646 }
647
648
649 /**
650 * Called by setup module when it has something for us to render.
651 */
652 void
653 lp_rast_queue_scene( struct lp_rasterizer *rast,
654 struct lp_scene *scene)
655 {
656 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
657
658 if (rast->num_threads == 0) {
659 /* no threading */
660
661 lp_rast_begin( rast, scene );
662
663 rasterize_scene( &rast->tasks[0], scene );
664
665 lp_rast_end( rast );
666
667 rast->curr_scene = NULL;
668 }
669 else {
670 /* threaded rendering! */
671 unsigned i;
672
673 lp_scene_enqueue( rast->full_scenes, scene );
674
675 /* signal the threads that there's work to do */
676 for (i = 0; i < rast->num_threads; i++) {
677 pipe_semaphore_signal(&rast->tasks[i].work_ready);
678 }
679 }
680
681 LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
682 }
683
684
685 void
686 lp_rast_finish( struct lp_rasterizer *rast )
687 {
688 if (rast->num_threads == 0) {
689 /* nothing to do */
690 }
691 else {
692 int i;
693
694 /* wait for work to complete */
695 for (i = 0; i < rast->num_threads; i++) {
696 pipe_semaphore_wait(&rast->tasks[i].work_done);
697 }
698 }
699 }
700
701
702 /**
703 * This is the thread's main entrypoint.
704 * It's a simple loop:
705 * 1. wait for work
706 * 2. do work
707 * 3. signal that we're done
708 */
709 static PIPE_THREAD_ROUTINE( thread_function, init_data )
710 {
711 struct lp_rasterizer_task *task = (struct lp_rasterizer_task *) init_data;
712 struct lp_rasterizer *rast = task->rast;
713 boolean debug = false;
714
715 while (1) {
716 /* wait for work */
717 if (debug)
718 debug_printf("thread %d waiting for work\n", task->thread_index);
719 pipe_semaphore_wait(&task->work_ready);
720
721 if (rast->exit_flag)
722 break;
723
724 if (task->thread_index == 0) {
725 /* thread[0]:
726 * - get next scene to rasterize
727 * - map the framebuffer surfaces
728 */
729 lp_rast_begin( rast,
730 lp_scene_dequeue( rast->full_scenes, TRUE ) );
731 }
732
733 /* Wait for all threads to get here so that threads[1+] don't
734 * get a null rast->curr_scene pointer.
735 */
736 pipe_barrier_wait( &rast->barrier );
737
738 /* do work */
739 if (debug)
740 debug_printf("thread %d doing work\n", task->thread_index);
741
742 rasterize_scene(task,
743 rast->curr_scene);
744
745 /* wait for all threads to finish with this scene */
746 pipe_barrier_wait( &rast->barrier );
747
748 /* XXX: shouldn't be necessary:
749 */
750 if (task->thread_index == 0) {
751 lp_rast_end( rast );
752 }
753
754 /* signal done with work */
755 if (debug)
756 debug_printf("thread %d done working\n", task->thread_index);
757
758 pipe_semaphore_signal(&task->work_done);
759 }
760
761 return NULL;
762 }
763
764
765 /**
766 * Initialize semaphores and spawn the threads.
767 */
768 static void
769 create_rast_threads(struct lp_rasterizer *rast)
770 {
771 unsigned i;
772
773 /* NOTE: if num_threads is zero, we won't use any threads */
774 for (i = 0; i < rast->num_threads; i++) {
775 pipe_semaphore_init(&rast->tasks[i].work_ready, 0);
776 pipe_semaphore_init(&rast->tasks[i].work_done, 0);
777 rast->threads[i] = pipe_thread_create(thread_function,
778 (void *) &rast->tasks[i]);
779 }
780 }
781
782
783
784 /**
785 * Create new lp_rasterizer. If num_threads is zero, don't create any
786 * new threads, do rendering synchronously.
787 * \param num_threads number of rasterizer threads to create
788 */
789 struct lp_rasterizer *
790 lp_rast_create( unsigned num_threads )
791 {
792 struct lp_rasterizer *rast;
793 unsigned i;
794
795 rast = CALLOC_STRUCT(lp_rasterizer);
796 if (!rast) {
797 goto no_rast;
798 }
799
800 rast->full_scenes = lp_scene_queue_create();
801 if (!rast->full_scenes) {
802 goto no_full_scenes;
803 }
804
805 for (i = 0; i < Elements(rast->tasks); i++) {
806 struct lp_rasterizer_task *task = &rast->tasks[i];
807 task->rast = rast;
808 task->thread_index = i;
809 }
810
811 rast->num_threads = num_threads;
812
813 rast->no_rast = debug_get_bool_option("LP_NO_RAST", FALSE);
814
815 create_rast_threads(rast);
816
817 /* for synchronizing rasterization threads */
818 pipe_barrier_init( &rast->barrier, rast->num_threads );
819
820 memset(lp_swizzled_cbuf, 0, sizeof lp_swizzled_cbuf);
821
822 memset(lp_dummy_tile, 0, sizeof lp_dummy_tile);
823
824 return rast;
825
826 no_full_scenes:
827 FREE(rast);
828 no_rast:
829 return NULL;
830 }
831
832
833 /* Shutdown:
834 */
835 void lp_rast_destroy( struct lp_rasterizer *rast )
836 {
837 unsigned i;
838
839 /* Set exit_flag and signal each thread's work_ready semaphore.
840 * Each thread will be woken up, notice that the exit_flag is set and
841 * break out of its main loop. The thread will then exit.
842 */
843 rast->exit_flag = TRUE;
844 for (i = 0; i < rast->num_threads; i++) {
845 pipe_semaphore_signal(&rast->tasks[i].work_ready);
846 }
847
848 /* Wait for threads to terminate before cleaning up per-thread data */
849 for (i = 0; i < rast->num_threads; i++) {
850 pipe_thread_wait(rast->threads[i]);
851 }
852
853 /* Clean up per-thread data */
854 for (i = 0; i < rast->num_threads; i++) {
855 pipe_semaphore_destroy(&rast->tasks[i].work_ready);
856 pipe_semaphore_destroy(&rast->tasks[i].work_done);
857 }
858
859 /* for synchronizing rasterization threads */
860 pipe_barrier_destroy( &rast->barrier );
861
862 lp_scene_queue_destroy(rast->full_scenes);
863
864 FREE(rast);
865 }
866
867
868 /** Return number of rasterization threads */
869 unsigned
870 lp_rast_get_num_threads( struct lp_rasterizer *rast )
871 {
872 return rast->num_threads;
873 }
874
875