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