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