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