5e659a4b00438ac8990b7b43ea938f353813610a
[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 #ifdef DEBUG
573 if (LP_DEBUG & (DEBUG_SHOW_SUBTILES | DEBUG_SHOW_TILES)) {
574 struct lp_rasterizer *rast = task->rast;
575 unsigned buf;
576
577 for (buf = 0; buf < rast->state.nr_cbufs; buf++) {
578 uint8_t *color = lp_rast_get_color_block_pointer(task, buf,
579 task->x, task->y);
580
581 if (LP_DEBUG & DEBUG_SHOW_SUBTILES)
582 outline_subtiles(color);
583 else if (LP_DEBUG & DEBUG_SHOW_TILES)
584 outline_tile(color);
585 }
586 }
587 #else
588 (void) outline_subtiles;
589 #endif
590
591 /* debug */
592 memset(task->color_tiles, 0, sizeof(task->color_tiles));
593 task->depth_tile = NULL;
594 }
595
596
597
598 /**
599 * Signal on a fence. This is called during bin execution/rasterization.
600 * Called per thread.
601 */
602 void
603 lp_rast_fence(struct lp_rasterizer_task *task,
604 const union lp_rast_cmd_arg arg)
605 {
606 struct lp_fence *fence = arg.fence;
607 lp_fence_signal(fence);
608 }
609
610
611 /**
612 * Begin a new occlusion query.
613 * This is a bin command put in all bins.
614 * Called per thread.
615 */
616 void
617 lp_rast_begin_query(struct lp_rasterizer_task *task,
618 const union lp_rast_cmd_arg arg)
619 {
620 /* Reset the the per-task counter */
621 task->vis_counter = 0;
622 }
623
624
625 /**
626 * End the current occlusion query.
627 * This is a bin command put in all bins.
628 * Called per thread.
629 */
630 void
631 lp_rast_end_query(struct lp_rasterizer_task *task,
632 const union lp_rast_cmd_arg arg)
633 {
634 struct llvmpipe_query *pq = arg.query_obj;
635
636 pipe_mutex_lock(pq->mutex);
637 {
638 /* Accumulate the visible fragment counter from this tile in
639 * the query object.
640 */
641 pq->count[task->thread_index] += task->vis_counter;
642
643 /* check if this is the last tile in the scene */
644 pq->tile_count++;
645 if (pq->tile_count == pq->num_tiles) {
646 uint i;
647
648 /* sum the per-thread counters for the query */
649 pq->result = 0;
650 for (i = 0; i < LP_MAX_THREADS; i++) {
651 pq->result += pq->count[i];
652 }
653
654 /* reset counters (in case this query is re-used in the scene) */
655 memset(pq->count, 0, sizeof(pq->count));
656
657 pq->tile_count = 0;
658 pq->binned = FALSE;
659 pq->done = TRUE;
660 }
661 }
662 pipe_mutex_unlock(pq->mutex);
663 }
664
665
666
667 /**
668 * Rasterize commands for a single bin.
669 * \param x, y position of the bin's tile in the framebuffer
670 * Must be called between lp_rast_begin() and lp_rast_end().
671 * Called per thread.
672 */
673 static void
674 rasterize_bin(struct lp_rasterizer_task *task,
675 const struct cmd_bin *bin,
676 int x, int y)
677 {
678 const struct cmd_block_list *commands = &bin->commands;
679 struct cmd_block *block;
680 unsigned k;
681
682 lp_rast_tile_begin( task, x * TILE_SIZE, y * TILE_SIZE );
683
684 /* simply execute each of the commands in the block list */
685 for (block = commands->head; block; block = block->next) {
686 for (k = 0; k < block->count; k++) {
687 block->cmd[k]( task, block->arg[k] );
688 }
689 }
690
691 lp_rast_tile_end(task);
692
693 /* Free data for this bin.
694 */
695 lp_scene_bin_reset( task->rast->curr_scene, x, y);
696 }
697
698
699 #define RAST(x) { lp_rast_##x, #x }
700
701 static struct {
702 lp_rast_cmd cmd;
703 const char *name;
704 } cmd_names[] =
705 {
706 RAST(clear_color),
707 RAST(clear_zstencil),
708 RAST(triangle),
709 RAST(shade_tile),
710 RAST(set_state),
711 RAST(store_color),
712 RAST(fence),
713 RAST(begin_query),
714 RAST(end_query),
715 };
716
717 static void
718 debug_bin( const struct cmd_bin *bin )
719 {
720 const struct cmd_block *head = bin->commands.head;
721 int i, j;
722
723 for (i = 0; i < head->count; i++) {
724 debug_printf("%d: ", i);
725 for (j = 0; j < Elements(cmd_names); j++) {
726 if (head->cmd[i] == cmd_names[j].cmd) {
727 debug_printf("%s\n", cmd_names[j].name);
728 break;
729 }
730 }
731 if (j == Elements(cmd_names))
732 debug_printf("...other\n");
733 }
734
735 }
736
737 /* An empty bin is one that just loads the contents of the tile and
738 * stores them again unchanged. This typically happens when bins have
739 * been flushed for some reason in the middle of a frame, or when
740 * incremental updates are being made to a render target.
741 *
742 * Try to avoid doing pointless work in this case.
743 */
744 static boolean
745 is_empty_bin( const struct cmd_bin *bin )
746 {
747 const struct cmd_block *head = bin->commands.head;
748 int i;
749
750 if (0)
751 debug_bin(bin);
752
753 /* We emit at most two load-tile commands at the start of the first
754 * command block. In addition we seem to emit a couple of
755 * set-state commands even in empty bins.
756 *
757 * As a heuristic, if a bin has more than 4 commands, consider it
758 * non-empty.
759 */
760 if (head->next != NULL ||
761 head->count > 4) {
762 return FALSE;
763 }
764
765 for (i = 0; i < head->count; i++)
766 if (head->cmd[i] != lp_rast_set_state) {
767 return FALSE;
768 }
769
770 return TRUE;
771 }
772
773
774
775 /**
776 * Rasterize/execute all bins within a scene.
777 * Called per thread.
778 */
779 static void
780 rasterize_scene(struct lp_rasterizer_task *task,
781 struct lp_scene *scene)
782 {
783 /* loop over scene bins, rasterize each */
784 #if 0
785 {
786 unsigned i, j;
787 for (i = 0; i < scene->tiles_x; i++) {
788 for (j = 0; j < scene->tiles_y; j++) {
789 struct cmd_bin *bin = lp_scene_get_bin(scene, i, j);
790 rasterize_bin(task, bin, i, j);
791 }
792 }
793 }
794 #else
795 {
796 struct cmd_bin *bin;
797 int x, y;
798
799 assert(scene);
800 while ((bin = lp_scene_bin_iter_next(scene, &x, &y))) {
801 if (!is_empty_bin( bin ))
802 rasterize_bin(task, bin, x, y);
803 }
804 }
805 #endif
806 }
807
808
809 /**
810 * Called by setup module when it has something for us to render.
811 */
812 void
813 lp_rast_queue_scene( struct lp_rasterizer *rast,
814 struct lp_scene *scene)
815 {
816 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
817
818 if (rast->num_threads == 0) {
819 /* no threading */
820
821 lp_rast_begin( rast, scene );
822
823 rasterize_scene( &rast->tasks[0], scene );
824
825 lp_scene_reset( scene );
826
827 lp_rast_end( rast );
828
829 rast->curr_scene = NULL;
830 }
831 else {
832 /* threaded rendering! */
833 unsigned i;
834
835 lp_scene_enqueue( rast->full_scenes, scene );
836
837 /* signal the threads that there's work to do */
838 for (i = 0; i < rast->num_threads; i++) {
839 pipe_semaphore_signal(&rast->tasks[i].work_ready);
840 }
841 }
842
843 LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
844 }
845
846
847 void
848 lp_rast_finish( struct lp_rasterizer *rast )
849 {
850 if (rast->num_threads == 0) {
851 /* nothing to do */
852 }
853 else {
854 int i;
855
856 /* wait for work to complete */
857 for (i = 0; i < rast->num_threads; i++) {
858 pipe_semaphore_wait(&rast->tasks[i].work_done);
859 }
860 }
861 }
862
863
864 /**
865 * This is the thread's main entrypoint.
866 * It's a simple loop:
867 * 1. wait for work
868 * 2. do work
869 * 3. signal that we're done
870 */
871 static PIPE_THREAD_ROUTINE( thread_func, init_data )
872 {
873 struct lp_rasterizer_task *task = (struct lp_rasterizer_task *) init_data;
874 struct lp_rasterizer *rast = task->rast;
875 boolean debug = false;
876
877 while (1) {
878 /* wait for work */
879 if (debug)
880 debug_printf("thread %d waiting for work\n", task->thread_index);
881 pipe_semaphore_wait(&task->work_ready);
882
883 if (rast->exit_flag)
884 break;
885
886 if (task->thread_index == 0) {
887 /* thread[0]:
888 * - get next scene to rasterize
889 * - map the framebuffer surfaces
890 */
891 lp_rast_begin( rast,
892 lp_scene_dequeue( rast->full_scenes, TRUE ) );
893 }
894
895 /* Wait for all threads to get here so that threads[1+] don't
896 * get a null rast->curr_scene pointer.
897 */
898 pipe_barrier_wait( &rast->barrier );
899
900 /* do work */
901 if (debug)
902 debug_printf("thread %d doing work\n", task->thread_index);
903
904 rasterize_scene(task,
905 rast->curr_scene);
906
907 /* wait for all threads to finish with this scene */
908 pipe_barrier_wait( &rast->barrier );
909
910 /* XXX: shouldn't be necessary:
911 */
912 if (task->thread_index == 0) {
913 lp_rast_end( rast );
914 }
915
916 /* signal done with work */
917 if (debug)
918 debug_printf("thread %d done working\n", task->thread_index);
919
920 pipe_semaphore_signal(&task->work_done);
921 }
922
923 return NULL;
924 }
925
926
927 /**
928 * Initialize semaphores and spawn the threads.
929 */
930 static void
931 create_rast_threads(struct lp_rasterizer *rast)
932 {
933 unsigned i;
934
935 /* NOTE: if num_threads is zero, we won't use any threads */
936 for (i = 0; i < rast->num_threads; i++) {
937 pipe_semaphore_init(&rast->tasks[i].work_ready, 0);
938 pipe_semaphore_init(&rast->tasks[i].work_done, 0);
939 rast->threads[i] = pipe_thread_create(thread_func,
940 (void *) &rast->tasks[i]);
941 }
942 }
943
944
945
946 /**
947 * Create new lp_rasterizer. If num_threads is zero, don't create any
948 * new threads, do rendering synchronously.
949 * \param num_threads number of rasterizer threads to create
950 */
951 struct lp_rasterizer *
952 lp_rast_create( unsigned num_threads )
953 {
954 struct lp_rasterizer *rast;
955 unsigned i;
956
957 rast = CALLOC_STRUCT(lp_rasterizer);
958 if(!rast)
959 return NULL;
960
961 rast->full_scenes = lp_scene_queue_create();
962
963 for (i = 0; i < Elements(rast->tasks); i++) {
964 struct lp_rasterizer_task *task = &rast->tasks[i];
965 task->rast = rast;
966 task->thread_index = i;
967 }
968
969 rast->num_threads = num_threads;
970
971 create_rast_threads(rast);
972
973 /* for synchronizing rasterization threads */
974 pipe_barrier_init( &rast->barrier, rast->num_threads );
975
976 return rast;
977 }
978
979
980 /* Shutdown:
981 */
982 void lp_rast_destroy( struct lp_rasterizer *rast )
983 {
984 unsigned i;
985
986 /* Set exit_flag and signal each thread's work_ready semaphore.
987 * Each thread will be woken up, notice that the exit_flag is set and
988 * break out of its main loop. The thread will then exit.
989 */
990 rast->exit_flag = TRUE;
991 for (i = 0; i < rast->num_threads; i++) {
992 pipe_semaphore_signal(&rast->tasks[i].work_ready);
993 }
994
995 /* Wait for threads to terminate before cleaning up per-thread data */
996 for (i = 0; i < rast->num_threads; i++) {
997 pipe_thread_wait(rast->threads[i]);
998 }
999
1000 /* Clean up per-thread data */
1001 for (i = 0; i < rast->num_threads; i++) {
1002 pipe_semaphore_destroy(&rast->tasks[i].work_ready);
1003 pipe_semaphore_destroy(&rast->tasks[i].work_done);
1004 }
1005
1006 /* for synchronizing rasterization threads */
1007 pipe_barrier_destroy( &rast->barrier );
1008
1009 lp_scene_queue_destroy(rast->full_scenes);
1010
1011 FREE(rast);
1012 }
1013
1014
1015 /** Return number of rasterization threads */
1016 unsigned
1017 lp_rast_get_num_threads( struct lp_rasterizer *rast )
1018 {
1019 return rast->num_threads;
1020 }
1021
1022