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