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