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