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