8e9be755e074d37db30f9b41ade9781e563681e2
[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 "lp_scene_queue.h"
36 #include "lp_debug.h"
37 #include "lp_fence.h"
38 #include "lp_perf.h"
39 #include "lp_query.h"
40 #include "lp_rast.h"
41 #include "lp_rast_priv.h"
42 #include "lp_tile_soa.h"
43 #include "gallivm/lp_bld_debug.h"
44 #include "lp_scene.h"
45
46
47 #ifdef DEBUG
48 int jit_line = 0;
49 const struct lp_rast_state *jit_state = NULL;
50 #endif
51
52
53 /**
54 * Begin rasterizing a scene.
55 * Called once per scene by one thread.
56 */
57 static void
58 lp_rast_begin( struct lp_rasterizer *rast,
59 struct lp_scene *scene )
60 {
61
62 rast->curr_scene = scene;
63
64 LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
65
66 lp_scene_begin_rasterization( scene );
67 lp_scene_bin_iter_begin( scene );
68 }
69
70
71 static void
72 lp_rast_end( struct lp_rasterizer *rast )
73 {
74 lp_scene_end_rasterization( rast->curr_scene );
75
76 rast->curr_scene = NULL;
77
78 #ifdef DEBUG
79 if (0)
80 debug_printf("Post render scene: tile unswizzle: %u tile swizzle: %u\n",
81 lp_tile_unswizzle_count, lp_tile_swizzle_count);
82 #endif
83 }
84
85
86 /**
87 * Begining rasterization of a tile.
88 * \param x window X position of the tile, in pixels
89 * \param y window Y position of the tile, in pixels
90 */
91 static void
92 lp_rast_tile_begin(struct lp_rasterizer_task *task,
93 const struct cmd_bin *bin)
94 {
95 const struct lp_scene *scene = task->scene;
96 enum lp_texture_usage usage;
97
98 LP_DBG(DEBUG_RAST, "%s %d,%d\n", __FUNCTION__, bin->x, bin->y);
99
100 task->bin = bin;
101 task->x = bin->x * TILE_SIZE;
102 task->y = bin->y * TILE_SIZE;
103
104 /* reset pointers to color tile(s) */
105 memset(task->color_tiles, 0, sizeof(task->color_tiles));
106
107 /* get pointer to depth/stencil tile */
108 {
109 struct pipe_surface *zsbuf = task->scene->fb.zsbuf;
110 if (zsbuf) {
111 struct llvmpipe_resource *lpt = llvmpipe_resource(zsbuf->texture);
112
113 if (scene->has_depthstencil_clear)
114 usage = LP_TEX_USAGE_WRITE_ALL;
115 else
116 usage = LP_TEX_USAGE_READ_WRITE;
117
118 /* "prime" the tile: convert data from linear to tiled if necessary
119 * and update the tile's layout info.
120 */
121 (void) llvmpipe_get_texture_tile(lpt,
122 zsbuf->face + zsbuf->zslice,
123 zsbuf->level,
124 usage,
125 task->x,
126 task->y);
127 /* Get actual pointer to the tile data. Note that depth/stencil
128 * data is tiled differently than color data.
129 */
130 task->depth_tile = lp_rast_get_depth_block_pointer(task,
131 task->x,
132 task->y);
133
134 assert(task->depth_tile);
135 }
136 else {
137 task->depth_tile = NULL;
138 }
139 }
140 }
141
142
143 /**
144 * Clear the rasterizer's current color tile.
145 * This is a bin command called during bin processing.
146 */
147 static void
148 lp_rast_clear_color(struct lp_rasterizer_task *task,
149 const union lp_rast_cmd_arg arg)
150 {
151 const struct lp_scene *scene = task->scene;
152 const uint8_t *clear_color = arg.clear_color;
153
154 unsigned i;
155
156 LP_DBG(DEBUG_RAST, "%s 0x%x,0x%x,0x%x,0x%x\n", __FUNCTION__,
157 clear_color[0],
158 clear_color[1],
159 clear_color[2],
160 clear_color[3]);
161
162 if (clear_color[0] == clear_color[1] &&
163 clear_color[1] == clear_color[2] &&
164 clear_color[2] == clear_color[3]) {
165 /* clear to grayscale value {x, x, x, x} */
166 for (i = 0; i < scene->fb.nr_cbufs; i++) {
167 uint8_t *ptr =
168 lp_rast_get_color_tile_pointer(task, i, LP_TEX_USAGE_WRITE_ALL);
169 memset(ptr, clear_color[0], TILE_SIZE * TILE_SIZE * 4);
170 }
171 }
172 else {
173 /* Non-gray color.
174 * Note: if the swizzled tile layout changes (see TILE_PIXEL) this code
175 * will need to change. It'll be pretty obvious when clearing no longer
176 * works.
177 */
178 const unsigned chunk = TILE_SIZE / 4;
179 for (i = 0; i < scene->fb.nr_cbufs; i++) {
180 uint8_t *c =
181 lp_rast_get_color_tile_pointer(task, i, LP_TEX_USAGE_WRITE_ALL);
182 unsigned j;
183
184 for (j = 0; j < 4 * TILE_SIZE; j++) {
185 memset(c, clear_color[0], chunk);
186 c += chunk;
187 memset(c, clear_color[1], chunk);
188 c += chunk;
189 memset(c, clear_color[2], chunk);
190 c += chunk;
191 memset(c, clear_color[3], chunk);
192 c += chunk;
193 }
194 }
195 }
196
197 LP_COUNT(nr_color_tile_clear);
198 }
199
200
201
202
203
204
205 /**
206 * Clear the rasterizer's current z/stencil tile.
207 * This is a bin command called during bin processing.
208 */
209 static void
210 lp_rast_clear_zstencil(struct lp_rasterizer_task *task,
211 const union lp_rast_cmd_arg arg)
212 {
213 const struct lp_scene *scene = task->scene;
214 uint32_t clear_value = arg.clear_zstencil.value;
215 uint32_t clear_mask = arg.clear_zstencil.mask;
216 const unsigned height = TILE_SIZE / TILE_VECTOR_HEIGHT;
217 const unsigned width = TILE_SIZE * TILE_VECTOR_HEIGHT;
218 const unsigned block_size = scene->zsbuf.blocksize;
219 const unsigned dst_stride = scene->zsbuf.stride * TILE_VECTOR_HEIGHT;
220 uint8_t *dst;
221 unsigned i, j;
222
223 LP_DBG(DEBUG_RAST, "%s: value=0x%08x, mask=0x%08x\n",
224 __FUNCTION__, clear_value, clear_mask);
225
226 /*
227 * Clear the aera of the swizzled depth/depth buffer matching this tile, in
228 * stripes of TILE_VECTOR_HEIGHT x TILE_SIZE at a time.
229 *
230 * The swizzled depth format is such that the depths for
231 * TILE_VECTOR_HEIGHT x TILE_VECTOR_WIDTH pixels have consecutive offsets.
232 */
233
234 dst = task->depth_tile;
235
236 clear_value &= clear_mask;
237
238 switch (block_size) {
239 case 1:
240 assert(clear_mask == 0xff);
241 memset(dst, (uint8_t) clear_value, height * width);
242 break;
243 case 2:
244 if (clear_mask == 0xffff) {
245 for (i = 0; i < height; i++) {
246 uint16_t *row = (uint16_t *)dst;
247 for (j = 0; j < width; j++)
248 *row++ = (uint16_t) clear_value;
249 dst += dst_stride;
250 }
251 }
252 else {
253 for (i = 0; i < height; i++) {
254 uint16_t *row = (uint16_t *)dst;
255 for (j = 0; j < width; j++) {
256 uint16_t tmp = ~clear_mask & *row;
257 *row++ = clear_value | tmp;
258 }
259 dst += dst_stride;
260 }
261 }
262 break;
263 case 4:
264 if (clear_mask == 0xffffffff) {
265 for (i = 0; i < height; i++) {
266 uint32_t *row = (uint32_t *)dst;
267 for (j = 0; j < width; j++)
268 *row++ = clear_value;
269 dst += dst_stride;
270 }
271 }
272 else {
273 for (i = 0; i < height; i++) {
274 uint32_t *row = (uint32_t *)dst;
275 for (j = 0; j < width; j++) {
276 uint32_t tmp = ~clear_mask & *row;
277 *row++ = clear_value | tmp;
278 }
279 dst += dst_stride;
280 }
281 }
282 break;
283 default:
284 assert(0);
285 break;
286 }
287 }
288
289
290
291
292 /**
293 * Convert the color tile from tiled to linear layout.
294 * This is generally only done when we're flushing the scene just prior to
295 * SwapBuffers. If we didn't do this here, we'd have to convert the entire
296 * tiled color buffer to linear layout in the llvmpipe_texture_unmap()
297 * function. It's better to do it here to take advantage of
298 * threading/parallelism.
299 * This is a bin command which is stored in all bins.
300 */
301 static void
302 lp_rast_store_linear_color( struct lp_rasterizer_task *task )
303 {
304 const struct lp_scene *scene = task->scene;
305 unsigned buf;
306
307 for (buf = 0; buf < scene->fb.nr_cbufs; buf++) {
308 struct pipe_surface *cbuf = scene->fb.cbufs[buf];
309 const unsigned face_slice = cbuf->face + cbuf->zslice;
310 const unsigned level = cbuf->level;
311 struct llvmpipe_resource *lpt = llvmpipe_resource(cbuf->texture);
312
313 if (!task->color_tiles[buf])
314 continue;
315
316 llvmpipe_unswizzle_cbuf_tile(lpt,
317 face_slice,
318 level,
319 task->x, task->y,
320 task->color_tiles[buf]);
321 }
322 }
323
324
325
326 /**
327 * Run the shader on all blocks in a tile. This is used when a tile is
328 * completely contained inside a triangle.
329 * This is a bin command called during bin processing.
330 */
331 static void
332 lp_rast_shade_tile(struct lp_rasterizer_task *task,
333 const union lp_rast_cmd_arg arg)
334 {
335 const struct lp_scene *scene = task->scene;
336 const struct lp_rast_shader_inputs *inputs = arg.shade_tile;
337 const struct lp_rast_state *state = task->state;
338 struct lp_fragment_shader_variant *variant = state->variant;
339 const unsigned tile_x = task->x, tile_y = task->y;
340 unsigned x, y;
341
342 if (inputs->disable) {
343 /* This command was partially binned and has been disabled */
344 return;
345 }
346
347 LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
348
349 /* render the whole 64x64 tile in 4x4 chunks */
350 for (y = 0; y < TILE_SIZE; y += 4){
351 for (x = 0; x < TILE_SIZE; x += 4) {
352 uint8_t *color[PIPE_MAX_COLOR_BUFS];
353 uint32_t *depth;
354 unsigned i;
355
356 /* color buffer */
357 for (i = 0; i < scene->fb.nr_cbufs; i++)
358 color[i] = lp_rast_get_color_block_pointer(task, i,
359 tile_x + x, tile_y + y);
360
361 /* depth buffer */
362 depth = lp_rast_get_depth_block_pointer(task, tile_x + x, tile_y + y);
363
364 /* run shader on 4x4 block */
365 BEGIN_JIT_CALL(state);
366 variant->jit_function[RAST_WHOLE]( &state->jit_context,
367 tile_x + x, tile_y + y,
368 inputs->frontfacing,
369 inputs->a0,
370 inputs->dadx,
371 inputs->dady,
372 color,
373 depth,
374 0xffff,
375 &task->vis_counter);
376 END_JIT_CALL();
377 }
378 }
379 }
380
381
382 /**
383 * Run the shader on all blocks in a tile. This is used when a tile is
384 * completely contained inside a triangle, and the shader is opaque.
385 * This is a bin command called during bin processing.
386 */
387 static void
388 lp_rast_shade_tile_opaque(struct lp_rasterizer_task *task,
389 const union lp_rast_cmd_arg arg)
390 {
391 const struct lp_scene *scene = task->scene;
392 unsigned i;
393
394 LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
395
396 /* this will prevent converting the layout from tiled to linear */
397 for (i = 0; i < scene->fb.nr_cbufs; i++) {
398 (void)lp_rast_get_color_tile_pointer(task, i, LP_TEX_USAGE_WRITE_ALL);
399 }
400
401 lp_rast_shade_tile(task, arg);
402 }
403
404
405 /**
406 * Compute shading for a 4x4 block of pixels inside a triangle.
407 * This is a bin command called during bin processing.
408 * \param x X position of quad in window coords
409 * \param y Y position of quad in window coords
410 */
411 void
412 lp_rast_shade_quads_mask(struct lp_rasterizer_task *task,
413 const struct lp_rast_shader_inputs *inputs,
414 unsigned x, unsigned y,
415 unsigned mask)
416 {
417 const struct lp_rast_state *state = task->state;
418 struct lp_fragment_shader_variant *variant = state->variant;
419 const struct lp_scene *scene = task->scene;
420 uint8_t *color[PIPE_MAX_COLOR_BUFS];
421 void *depth;
422 unsigned i;
423
424 assert(state);
425
426 /* Sanity checks */
427 assert(x % TILE_VECTOR_WIDTH == 0);
428 assert(y % TILE_VECTOR_HEIGHT == 0);
429
430 assert((x % 4) == 0);
431 assert((y % 4) == 0);
432
433 /* color buffer */
434 for (i = 0; i < scene->fb.nr_cbufs; i++) {
435 color[i] = lp_rast_get_color_block_pointer(task, i, x, y);
436 assert(lp_check_alignment(color[i], 16));
437 }
438
439 /* depth buffer */
440 depth = lp_rast_get_depth_block_pointer(task, x, y);
441
442
443 assert(lp_check_alignment(state->jit_context.blend_color, 16));
444
445 /* run shader on 4x4 block */
446 BEGIN_JIT_CALL(state);
447 variant->jit_function[RAST_EDGE_TEST](&state->jit_context,
448 x, y,
449 inputs->frontfacing,
450 inputs->a0,
451 inputs->dadx,
452 inputs->dady,
453 color,
454 depth,
455 mask,
456 &task->vis_counter);
457 END_JIT_CALL();
458 }
459
460
461
462 /**
463 * Begin a new occlusion query.
464 * This is a bin command put in all bins.
465 * Called per thread.
466 */
467 static void
468 lp_rast_begin_query(struct lp_rasterizer_task *task,
469 const union lp_rast_cmd_arg arg)
470 {
471 struct llvmpipe_query *pq = arg.query_obj;
472
473 assert(task->query == NULL);
474 task->vis_counter = 0;
475 task->query = pq;
476 }
477
478
479 /**
480 * End the current occlusion query.
481 * This is a bin command put in all bins.
482 * Called per thread.
483 */
484 static void
485 lp_rast_end_query(struct lp_rasterizer_task *task,
486 const union lp_rast_cmd_arg arg)
487 {
488 task->query->count[task->thread_index] += task->vis_counter;
489 task->query = NULL;
490 }
491
492
493 void
494 lp_rast_set_state(struct lp_rasterizer_task *task,
495 const union lp_rast_cmd_arg arg)
496 {
497 task->state = arg.state;
498 }
499
500
501
502 /**
503 * Set top row and left column of the tile's pixels to white. For debugging.
504 */
505 static void
506 outline_tile(uint8_t *tile)
507 {
508 const uint8_t val = 0xff;
509 unsigned i;
510
511 for (i = 0; i < TILE_SIZE; i++) {
512 TILE_PIXEL(tile, i, 0, 0) = val;
513 TILE_PIXEL(tile, i, 0, 1) = val;
514 TILE_PIXEL(tile, i, 0, 2) = val;
515 TILE_PIXEL(tile, i, 0, 3) = val;
516
517 TILE_PIXEL(tile, 0, i, 0) = val;
518 TILE_PIXEL(tile, 0, i, 1) = val;
519 TILE_PIXEL(tile, 0, i, 2) = val;
520 TILE_PIXEL(tile, 0, i, 3) = val;
521 }
522 }
523
524
525 /**
526 * Draw grid of gray lines at 16-pixel intervals across the tile to
527 * show the sub-tile boundaries. For debugging.
528 */
529 static void
530 outline_subtiles(uint8_t *tile)
531 {
532 const uint8_t val = 0x80;
533 const unsigned step = 16;
534 unsigned i, j;
535
536 for (i = 0; i < TILE_SIZE; i += step) {
537 for (j = 0; j < TILE_SIZE; j++) {
538 TILE_PIXEL(tile, i, j, 0) = val;
539 TILE_PIXEL(tile, i, j, 1) = val;
540 TILE_PIXEL(tile, i, j, 2) = val;
541 TILE_PIXEL(tile, i, j, 3) = val;
542
543 TILE_PIXEL(tile, j, i, 0) = val;
544 TILE_PIXEL(tile, j, i, 1) = val;
545 TILE_PIXEL(tile, j, i, 2) = val;
546 TILE_PIXEL(tile, j, i, 3) = val;
547 }
548 }
549
550 outline_tile(tile);
551 }
552
553
554
555 /**
556 * Called when we're done writing to a color tile.
557 */
558 static void
559 lp_rast_tile_end(struct lp_rasterizer_task *task)
560 {
561 #ifdef DEBUG
562 if (LP_DEBUG & (DEBUG_SHOW_SUBTILES | DEBUG_SHOW_TILES)) {
563 const struct lp_scene *scene = task->scene;
564 unsigned buf;
565
566 for (buf = 0; buf < scene->fb.nr_cbufs; buf++) {
567 uint8_t *color = lp_rast_get_color_block_pointer(task, buf,
568 task->x, task->y);
569
570 if (LP_DEBUG & DEBUG_SHOW_SUBTILES)
571 outline_subtiles(color);
572 else if (LP_DEBUG & DEBUG_SHOW_TILES)
573 outline_tile(color);
574 }
575 }
576 #else
577 (void) outline_subtiles;
578 #endif
579
580 lp_rast_store_linear_color(task);
581
582 if (task->query) {
583 union lp_rast_cmd_arg dummy = {0};
584 lp_rast_end_query(task, dummy);
585 }
586
587 /* debug */
588 memset(task->color_tiles, 0, sizeof(task->color_tiles));
589 task->depth_tile = NULL;
590
591 task->bin = NULL;
592 }
593
594 static lp_rast_cmd_func dispatch[LP_RAST_OP_MAX] =
595 {
596 lp_rast_clear_color,
597 lp_rast_clear_zstencil,
598 lp_rast_triangle_1,
599 lp_rast_triangle_2,
600 lp_rast_triangle_3,
601 lp_rast_triangle_4,
602 lp_rast_triangle_5,
603 lp_rast_triangle_6,
604 lp_rast_triangle_7,
605 lp_rast_triangle_8,
606 lp_rast_triangle_3_4,
607 lp_rast_triangle_3_16,
608 lp_rast_triangle_4_16,
609 lp_rast_shade_tile,
610 lp_rast_shade_tile_opaque,
611 lp_rast_begin_query,
612 lp_rast_end_query,
613 lp_rast_set_state,
614 };
615
616
617 static void
618 do_rasterize_bin(struct lp_rasterizer_task *task,
619 const struct cmd_bin *bin)
620 {
621 const struct cmd_block *block;
622 unsigned k;
623
624 if (0)
625 lp_debug_bin(bin);
626
627 for (block = bin->head; block; block = block->next) {
628 for (k = 0; k < block->count; k++) {
629 dispatch[block->cmd[k]]( task, block->arg[k] );
630 }
631 }
632 }
633
634
635
636 /**
637 * Rasterize commands for a single bin.
638 * \param x, y position of the bin's tile in the framebuffer
639 * Must be called between lp_rast_begin() and lp_rast_end().
640 * Called per thread.
641 */
642 static void
643 rasterize_bin(struct lp_rasterizer_task *task,
644 const struct cmd_bin *bin )
645 {
646 lp_rast_tile_begin( task, bin );
647
648 do_rasterize_bin(task, bin);
649
650 lp_rast_tile_end(task);
651
652
653 /* Debug/Perf flags:
654 */
655 if (bin->head->count == 1) {
656 if (bin->head->cmd[0] == LP_RAST_OP_SHADE_TILE_OPAQUE)
657 LP_COUNT(nr_pure_shade_opaque_64);
658 else if (bin->head->cmd[0] == LP_RAST_OP_SHADE_TILE)
659 LP_COUNT(nr_pure_shade_64);
660 }
661 }
662
663
664 /* An empty bin is one that just loads the contents of the tile and
665 * stores them again unchanged. This typically happens when bins have
666 * been flushed for some reason in the middle of a frame, or when
667 * incremental updates are being made to a render target.
668 *
669 * Try to avoid doing pointless work in this case.
670 */
671 static boolean
672 is_empty_bin( const struct cmd_bin *bin )
673 {
674 return bin->head == NULL;
675 }
676
677
678 /**
679 * Rasterize/execute all bins within a scene.
680 * Called per thread.
681 */
682 static void
683 rasterize_scene(struct lp_rasterizer_task *task,
684 struct lp_scene *scene)
685 {
686 task->scene = scene;
687 /* loop over scene bins, rasterize each */
688 #if 0
689 {
690 unsigned i, j;
691 for (i = 0; i < scene->tiles_x; i++) {
692 for (j = 0; j < scene->tiles_y; j++) {
693 struct cmd_bin *bin = lp_scene_get_bin(scene, i, j);
694 rasterize_bin(task, bin, i, j);
695 }
696 }
697 }
698 #else
699 {
700 struct cmd_bin *bin;
701
702 assert(scene);
703 while ((bin = lp_scene_bin_iter_next(scene))) {
704 if (!is_empty_bin( bin ))
705 rasterize_bin(task, bin);
706 }
707 }
708 #endif
709
710 if (scene->fence) {
711 lp_fence_signal(scene->fence);
712 }
713
714 task->scene = NULL;
715 }
716
717
718 /**
719 * Called by setup module when it has something for us to render.
720 */
721 void
722 lp_rast_queue_scene( struct lp_rasterizer *rast,
723 struct lp_scene *scene)
724 {
725 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
726
727 if (rast->num_threads == 0) {
728 /* no threading */
729
730 lp_rast_begin( rast, scene );
731
732 rasterize_scene( &rast->tasks[0], scene );
733
734 lp_rast_end( rast );
735
736 rast->curr_scene = NULL;
737 }
738 else {
739 /* threaded rendering! */
740 unsigned i;
741
742 lp_scene_enqueue( rast->full_scenes, scene );
743
744 /* signal the threads that there's work to do */
745 for (i = 0; i < rast->num_threads; i++) {
746 pipe_semaphore_signal(&rast->tasks[i].work_ready);
747 }
748 }
749
750 LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
751 }
752
753
754 void
755 lp_rast_finish( struct lp_rasterizer *rast )
756 {
757 if (rast->num_threads == 0) {
758 /* nothing to do */
759 }
760 else {
761 int i;
762
763 /* wait for work to complete */
764 for (i = 0; i < rast->num_threads; i++) {
765 pipe_semaphore_wait(&rast->tasks[i].work_done);
766 }
767 }
768 }
769
770
771 /**
772 * This is the thread's main entrypoint.
773 * It's a simple loop:
774 * 1. wait for work
775 * 2. do work
776 * 3. signal that we're done
777 */
778 static PIPE_THREAD_ROUTINE( thread_func, init_data )
779 {
780 struct lp_rasterizer_task *task = (struct lp_rasterizer_task *) init_data;
781 struct lp_rasterizer *rast = task->rast;
782 boolean debug = false;
783
784 while (1) {
785 /* wait for work */
786 if (debug)
787 debug_printf("thread %d waiting for work\n", task->thread_index);
788 pipe_semaphore_wait(&task->work_ready);
789
790 if (rast->exit_flag)
791 break;
792
793 if (task->thread_index == 0) {
794 /* thread[0]:
795 * - get next scene to rasterize
796 * - map the framebuffer surfaces
797 */
798 lp_rast_begin( rast,
799 lp_scene_dequeue( rast->full_scenes, TRUE ) );
800 }
801
802 /* Wait for all threads to get here so that threads[1+] don't
803 * get a null rast->curr_scene pointer.
804 */
805 pipe_barrier_wait( &rast->barrier );
806
807 /* do work */
808 if (debug)
809 debug_printf("thread %d doing work\n", task->thread_index);
810
811 rasterize_scene(task,
812 rast->curr_scene);
813
814 /* wait for all threads to finish with this scene */
815 pipe_barrier_wait( &rast->barrier );
816
817 /* XXX: shouldn't be necessary:
818 */
819 if (task->thread_index == 0) {
820 lp_rast_end( rast );
821 }
822
823 /* signal done with work */
824 if (debug)
825 debug_printf("thread %d done working\n", task->thread_index);
826
827 pipe_semaphore_signal(&task->work_done);
828 }
829
830 return NULL;
831 }
832
833
834 /**
835 * Initialize semaphores and spawn the threads.
836 */
837 static void
838 create_rast_threads(struct lp_rasterizer *rast)
839 {
840 unsigned i;
841
842 /* NOTE: if num_threads is zero, we won't use any threads */
843 for (i = 0; i < rast->num_threads; i++) {
844 pipe_semaphore_init(&rast->tasks[i].work_ready, 0);
845 pipe_semaphore_init(&rast->tasks[i].work_done, 0);
846 rast->threads[i] = pipe_thread_create(thread_func,
847 (void *) &rast->tasks[i]);
848 }
849 }
850
851
852
853 /**
854 * Create new lp_rasterizer. If num_threads is zero, don't create any
855 * new threads, do rendering synchronously.
856 * \param num_threads number of rasterizer threads to create
857 */
858 struct lp_rasterizer *
859 lp_rast_create( unsigned num_threads )
860 {
861 struct lp_rasterizer *rast;
862 unsigned i;
863
864 rast = CALLOC_STRUCT(lp_rasterizer);
865 if(!rast)
866 return NULL;
867
868 rast->full_scenes = lp_scene_queue_create();
869
870 for (i = 0; i < Elements(rast->tasks); i++) {
871 struct lp_rasterizer_task *task = &rast->tasks[i];
872 task->rast = rast;
873 task->thread_index = i;
874 }
875
876 rast->num_threads = num_threads;
877
878 create_rast_threads(rast);
879
880 /* for synchronizing rasterization threads */
881 pipe_barrier_init( &rast->barrier, rast->num_threads );
882
883 memset(lp_swizzled_cbuf, 0, sizeof lp_swizzled_cbuf);
884
885 memset(lp_dummy_tile, 0, sizeof lp_dummy_tile);
886
887 return rast;
888 }
889
890
891 /* Shutdown:
892 */
893 void lp_rast_destroy( struct lp_rasterizer *rast )
894 {
895 unsigned i;
896
897 /* Set exit_flag and signal each thread's work_ready semaphore.
898 * Each thread will be woken up, notice that the exit_flag is set and
899 * break out of its main loop. The thread will then exit.
900 */
901 rast->exit_flag = TRUE;
902 for (i = 0; i < rast->num_threads; i++) {
903 pipe_semaphore_signal(&rast->tasks[i].work_ready);
904 }
905
906 /* Wait for threads to terminate before cleaning up per-thread data */
907 for (i = 0; i < rast->num_threads; i++) {
908 pipe_thread_wait(rast->threads[i]);
909 }
910
911 /* Clean up per-thread data */
912 for (i = 0; i < rast->num_threads; i++) {
913 pipe_semaphore_destroy(&rast->tasks[i].work_ready);
914 pipe_semaphore_destroy(&rast->tasks[i].work_done);
915 }
916
917 /* for synchronizing rasterization threads */
918 pipe_barrier_destroy( &rast->barrier );
919
920 lp_scene_queue_destroy(rast->full_scenes);
921
922 FREE(rast);
923 }
924
925
926 /** Return number of rasterization threads */
927 unsigned
928 lp_rast_get_num_threads( struct lp_rasterizer *rast )
929 {
930 return rast->num_threads;
931 }
932
933