3215d0f65255b9ba9160934e94f4fc2adf5fc487
[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 * Load tile color from the framebuffer surface.
321 * This is a bin command called during bin processing.
322 */
323 #if 0
324 void
325 lp_rast_load_color(struct lp_rasterizer_task *task,
326 const union lp_rast_cmd_arg arg)
327 {
328 struct lp_rasterizer *rast = task->rast;
329 unsigned buf;
330 enum lp_texture_usage usage;
331
332 LP_DBG(DEBUG_RAST, "%s at %u, %u\n", __FUNCTION__, x, y);
333
334 if (scene->has_color_clear)
335 usage = LP_TEX_USAGE_WRITE_ALL;
336 else
337 usage = LP_TEX_USAGE_READ_WRITE;
338
339 /* Get pointers to color tile(s).
340 * This will convert linear data to tiled if needed.
341 */
342 for (buf = 0; buf < rast->state.nr_cbufs; buf++) {
343 struct pipe_surface *cbuf = rast->curr_scene->fb.cbufs[buf];
344 struct llvmpipe_texture *lpt;
345 assert(cbuf);
346 lpt = llvmpipe_texture(cbuf->texture);
347 task->color_tiles[buf] = llvmpipe_get_texture_tile(lpt,
348 cbuf->face + cbuf->zslice,
349 cbuf->level,
350 usage,
351 task->x, task->y);
352 assert(task->color_tiles[buf]);
353 }
354 }
355 #endif
356
357
358 /**
359 * Convert the color tile from tiled to linear layout.
360 * This is generally only done when we're flushing the scene just prior to
361 * SwapBuffers. If we didn't do this here, we'd have to convert the entire
362 * tiled color buffer to linear layout in the llvmpipe_texture_unmap()
363 * function. It's better to do it here to take advantage of
364 * threading/parallelism.
365 * This is a bin command which is stored in all bins.
366 */
367 void
368 lp_rast_store_linear_color( struct lp_rasterizer_task *task,
369 const union lp_rast_cmd_arg arg)
370 {
371 struct lp_rasterizer *rast = task->rast;
372 struct lp_scene *scene = rast->curr_scene;
373 unsigned buf;
374
375 for (buf = 0; buf < rast->state.nr_cbufs; buf++) {
376 struct pipe_surface *cbuf = scene->fb.cbufs[buf];
377 const unsigned face_slice = cbuf->face + cbuf->zslice;
378 const unsigned level = cbuf->level;
379 struct llvmpipe_resource *lpt = llvmpipe_resource(cbuf->texture);
380
381 if (!task->color_tiles[buf])
382 continue;
383
384 llvmpipe_unswizzle_cbuf_tile(lpt,
385 face_slice,
386 level,
387 task->x, task->y,
388 task->color_tiles[buf]);
389 }
390 }
391
392
393
394 /**
395 * Run the shader on all blocks in a tile. This is used when a tile is
396 * completely contained inside a triangle.
397 * This is a bin command called during bin processing.
398 */
399 void
400 lp_rast_shade_tile(struct lp_rasterizer_task *task,
401 const union lp_rast_cmd_arg arg)
402 {
403 struct lp_rasterizer *rast = task->rast;
404 const struct lp_rast_shader_inputs *inputs = arg.shade_tile;
405 const struct lp_rast_state *state = inputs->state;
406 struct lp_fragment_shader_variant *variant = state->variant;
407 const unsigned tile_x = task->x, tile_y = task->y;
408 unsigned x, y;
409
410 LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
411
412 /* render the whole 64x64 tile in 4x4 chunks */
413 for (y = 0; y < TILE_SIZE; y += 4){
414 for (x = 0; x < TILE_SIZE; x += 4) {
415 uint8_t *color[PIPE_MAX_COLOR_BUFS];
416 uint32_t *depth;
417 unsigned i;
418
419 /* color buffer */
420 for (i = 0; i < rast->state.nr_cbufs; i++)
421 color[i] = lp_rast_get_color_block_pointer(task, i,
422 tile_x + x, tile_y + y);
423
424 /* depth buffer */
425 depth = lp_rast_get_depth_block_pointer(task, tile_x + x, tile_y + y);
426
427 /* run shader on 4x4 block */
428 BEGIN_JIT_CALL(state);
429 variant->jit_function[RAST_WHOLE]( &state->jit_context,
430 tile_x + x, tile_y + y,
431 inputs->facing,
432 inputs->a0,
433 inputs->dadx,
434 inputs->dady,
435 color,
436 depth,
437 0xffff,
438 &task->vis_counter);
439 END_JIT_CALL();
440 }
441 }
442 }
443
444
445 /**
446 * Run the shader on all blocks in a tile. This is used when a tile is
447 * completely contained inside a triangle, and the shader is opaque.
448 * This is a bin command called during bin processing.
449 */
450 void
451 lp_rast_shade_tile_opaque(struct lp_rasterizer_task *task,
452 const union lp_rast_cmd_arg arg)
453 {
454 struct lp_rasterizer *rast = task->rast;
455 unsigned i;
456
457 LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
458
459 /* this will prevent converting the layout from tiled to linear */
460 for (i = 0; i < rast->state.nr_cbufs; i++) {
461 (void)lp_rast_get_color_tile_pointer(task, i, LP_TEX_USAGE_WRITE_ALL);
462 }
463
464 lp_rast_shade_tile(task, arg);
465 }
466
467
468 /**
469 * Compute shading for a 4x4 block of pixels inside a triangle.
470 * This is a bin command called during bin processing.
471 * \param x X position of quad in window coords
472 * \param y Y position of quad in window coords
473 */
474 void
475 lp_rast_shade_quads_mask(struct lp_rasterizer_task *task,
476 const struct lp_rast_shader_inputs *inputs,
477 unsigned x, unsigned y,
478 unsigned mask)
479 {
480 const struct lp_rast_state *state = inputs->state;
481 struct lp_fragment_shader_variant *variant = state->variant;
482 struct lp_rasterizer *rast = task->rast;
483 uint8_t *color[PIPE_MAX_COLOR_BUFS];
484 void *depth;
485 unsigned i;
486
487 assert(state);
488
489 /* Sanity checks */
490 assert(x % TILE_VECTOR_WIDTH == 0);
491 assert(y % TILE_VECTOR_HEIGHT == 0);
492
493 assert((x % 4) == 0);
494 assert((y % 4) == 0);
495
496 /* color buffer */
497 for (i = 0; i < rast->state.nr_cbufs; i++) {
498 color[i] = lp_rast_get_color_block_pointer(task, i, x, y);
499 assert(lp_check_alignment(color[i], 16));
500 }
501
502 /* depth buffer */
503 depth = lp_rast_get_depth_block_pointer(task, x, y);
504
505
506 assert(lp_check_alignment(state->jit_context.blend_color, 16));
507
508 /* run shader on 4x4 block */
509 BEGIN_JIT_CALL(state);
510 variant->jit_function[RAST_EDGE_TEST](&state->jit_context,
511 x, y,
512 inputs->facing,
513 inputs->a0,
514 inputs->dadx,
515 inputs->dady,
516 color,
517 depth,
518 mask,
519 &task->vis_counter);
520 END_JIT_CALL();
521 }
522
523
524
525 /**
526 * Set top row and left column of the tile's pixels to white. For debugging.
527 */
528 static void
529 outline_tile(uint8_t *tile)
530 {
531 const uint8_t val = 0xff;
532 unsigned i;
533
534 for (i = 0; i < TILE_SIZE; i++) {
535 TILE_PIXEL(tile, i, 0, 0) = val;
536 TILE_PIXEL(tile, i, 0, 1) = val;
537 TILE_PIXEL(tile, i, 0, 2) = val;
538 TILE_PIXEL(tile, i, 0, 3) = val;
539
540 TILE_PIXEL(tile, 0, i, 0) = val;
541 TILE_PIXEL(tile, 0, i, 1) = val;
542 TILE_PIXEL(tile, 0, i, 2) = val;
543 TILE_PIXEL(tile, 0, i, 3) = val;
544 }
545 }
546
547
548 /**
549 * Draw grid of gray lines at 16-pixel intervals across the tile to
550 * show the sub-tile boundaries. For debugging.
551 */
552 static void
553 outline_subtiles(uint8_t *tile)
554 {
555 const uint8_t val = 0x80;
556 const unsigned step = 16;
557 unsigned i, j;
558
559 for (i = 0; i < TILE_SIZE; i += step) {
560 for (j = 0; j < TILE_SIZE; j++) {
561 TILE_PIXEL(tile, i, j, 0) = val;
562 TILE_PIXEL(tile, i, j, 1) = val;
563 TILE_PIXEL(tile, i, j, 2) = val;
564 TILE_PIXEL(tile, i, j, 3) = val;
565
566 TILE_PIXEL(tile, j, i, 0) = val;
567 TILE_PIXEL(tile, j, i, 1) = val;
568 TILE_PIXEL(tile, j, i, 2) = val;
569 TILE_PIXEL(tile, j, i, 3) = val;
570 }
571 }
572
573 outline_tile(tile);
574 }
575
576
577
578 /**
579 * Called when we're done writing to a color tile.
580 */
581 static void
582 lp_rast_tile_end(struct lp_rasterizer_task *task)
583 {
584 #ifdef DEBUG
585 if (LP_DEBUG & (DEBUG_SHOW_SUBTILES | DEBUG_SHOW_TILES)) {
586 struct lp_rasterizer *rast = task->rast;
587 unsigned buf;
588
589 for (buf = 0; buf < rast->state.nr_cbufs; buf++) {
590 uint8_t *color = lp_rast_get_color_block_pointer(task, buf,
591 task->x, task->y);
592
593 if (LP_DEBUG & DEBUG_SHOW_SUBTILES)
594 outline_subtiles(color);
595 else if (LP_DEBUG & DEBUG_SHOW_TILES)
596 outline_tile(color);
597 }
598 }
599 #else
600 (void) outline_subtiles;
601 #endif
602
603 {
604 union lp_rast_cmd_arg dummy = {0};
605 lp_rast_store_linear_color(task, dummy);
606 }
607
608 /* debug */
609 memset(task->color_tiles, 0, sizeof(task->color_tiles));
610 task->depth_tile = NULL;
611 }
612
613
614
615 /**
616 * Signal on a fence. This is called during bin execution/rasterization.
617 * Called per thread.
618 */
619 void
620 lp_rast_fence(struct lp_rasterizer_task *task,
621 const union lp_rast_cmd_arg arg)
622 {
623 struct lp_fence *fence = arg.fence;
624 lp_fence_signal(fence);
625 }
626
627
628 /**
629 * Begin a new occlusion query.
630 * This is a bin command put in all bins.
631 * Called per thread.
632 */
633 void
634 lp_rast_begin_query(struct lp_rasterizer_task *task,
635 const union lp_rast_cmd_arg arg)
636 {
637 /* Reset the per-task counter */
638 task->vis_counter = 0;
639 }
640
641
642 /**
643 * End the current occlusion query.
644 * This is a bin command put in all bins.
645 * Called per thread.
646 */
647 void
648 lp_rast_end_query(struct lp_rasterizer_task *task,
649 const union lp_rast_cmd_arg arg)
650 {
651 struct llvmpipe_query *pq = arg.query_obj;
652
653 pipe_mutex_lock(pq->mutex);
654 {
655 /* Accumulate the visible fragment counter from this tile in
656 * the query object.
657 */
658 pq->count[task->thread_index] += task->vis_counter;
659
660 /* check if this is the last tile in the scene */
661 pq->tile_count++;
662 if (pq->tile_count == pq->num_tiles) {
663 uint i;
664
665 /* sum the per-thread counters for the query */
666 pq->result = 0;
667 for (i = 0; i < LP_MAX_THREADS; i++) {
668 pq->result += pq->count[i];
669 }
670
671 /* reset counters (in case this query is re-used in the scene) */
672 memset(pq->count, 0, sizeof(pq->count));
673
674 pq->tile_count = 0;
675 pq->binned = FALSE;
676 pq->done = TRUE;
677 }
678 }
679 pipe_mutex_unlock(pq->mutex);
680 }
681
682
683
684 /**
685 * Rasterize commands for a single bin.
686 * \param x, y position of the bin's tile in the framebuffer
687 * Must be called between lp_rast_begin() and lp_rast_end().
688 * Called per thread.
689 */
690 static void
691 rasterize_bin(struct lp_rasterizer_task *task,
692 const struct cmd_bin *bin,
693 int x, int y)
694 {
695 const struct cmd_block_list *commands = &bin->commands;
696 struct cmd_block *block;
697 unsigned k;
698
699 lp_rast_tile_begin( task, x * TILE_SIZE, y * TILE_SIZE );
700
701 /* simply execute each of the commands in the block list */
702 for (block = commands->head; block; block = block->next) {
703 for (k = 0; k < block->count; k++) {
704 block->cmd[k]( task, block->arg[k] );
705 }
706 }
707
708 lp_rast_tile_end(task);
709
710 /* Free data for this bin.
711 */
712 lp_scene_bin_reset( task->rast->curr_scene, x, y);
713 }
714
715
716 #define RAST(x) { lp_rast_##x, #x }
717
718 static struct {
719 lp_rast_cmd cmd;
720 const char *name;
721 } cmd_names[] =
722 {
723 RAST(clear_color),
724 RAST(clear_zstencil),
725 RAST(triangle_1),
726 RAST(triangle_2),
727 RAST(triangle_3),
728 RAST(triangle_4),
729 RAST(triangle_5),
730 RAST(triangle_6),
731 RAST(triangle_7),
732 RAST(shade_tile),
733 RAST(shade_tile_opaque),
734 RAST(store_linear_color),
735 RAST(fence),
736 RAST(begin_query),
737 RAST(end_query),
738 };
739
740 static void
741 debug_bin( const struct cmd_bin *bin )
742 {
743 const struct cmd_block *head = bin->commands.head;
744 int i, j;
745
746 for (i = 0; i < head->count; i++) {
747 debug_printf("%d: ", i);
748 for (j = 0; j < Elements(cmd_names); j++) {
749 if (head->cmd[i] == cmd_names[j].cmd) {
750 debug_printf("%s\n", cmd_names[j].name);
751 break;
752 }
753 }
754 if (j == Elements(cmd_names))
755 debug_printf("...other\n");
756 }
757
758 }
759
760 /* An empty bin is one that just loads the contents of the tile and
761 * stores them again unchanged. This typically happens when bins have
762 * been flushed for some reason in the middle of a frame, or when
763 * incremental updates are being made to a render target.
764 *
765 * Try to avoid doing pointless work in this case.
766 */
767 static boolean
768 is_empty_bin( const struct cmd_bin *bin )
769 {
770 if (0) debug_bin(bin);
771 return bin->commands.head->count == 0;
772 }
773
774
775
776 /**
777 * Rasterize/execute all bins within a scene.
778 * Called per thread.
779 */
780 static void
781 rasterize_scene(struct lp_rasterizer_task *task,
782 struct lp_scene *scene)
783 {
784 /* loop over scene bins, rasterize each */
785 #if 0
786 {
787 unsigned i, j;
788 for (i = 0; i < scene->tiles_x; i++) {
789 for (j = 0; j < scene->tiles_y; j++) {
790 struct cmd_bin *bin = lp_scene_get_bin(scene, i, j);
791 rasterize_bin(task, bin, i, j);
792 }
793 }
794 }
795 #else
796 {
797 struct cmd_bin *bin;
798 int x, y;
799
800 assert(scene);
801 while ((bin = lp_scene_bin_iter_next(scene, &x, &y))) {
802 if (!is_empty_bin( bin ))
803 rasterize_bin(task, bin, x, y);
804 }
805 }
806 #endif
807
808 if (scene->fence) {
809 lp_rast_fence(task, lp_rast_arg_fence(scene->fence));
810 }
811 }
812
813
814 /**
815 * Called by setup module when it has something for us to render.
816 */
817 void
818 lp_rast_queue_scene( struct lp_rasterizer *rast,
819 struct lp_scene *scene)
820 {
821 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
822
823 if (rast->num_threads == 0) {
824 /* no threading */
825
826 lp_rast_begin( rast, scene );
827
828 rasterize_scene( &rast->tasks[0], scene );
829
830 lp_scene_reset( scene );
831
832 lp_rast_end( rast );
833
834 rast->curr_scene = NULL;
835 }
836 else {
837 /* threaded rendering! */
838 unsigned i;
839
840 lp_scene_enqueue( rast->full_scenes, scene );
841
842 /* signal the threads that there's work to do */
843 for (i = 0; i < rast->num_threads; i++) {
844 pipe_semaphore_signal(&rast->tasks[i].work_ready);
845 }
846 }
847
848 LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
849 }
850
851
852 void
853 lp_rast_finish( struct lp_rasterizer *rast )
854 {
855 if (rast->num_threads == 0) {
856 /* nothing to do */
857 }
858 else {
859 int i;
860
861 /* wait for work to complete */
862 for (i = 0; i < rast->num_threads; i++) {
863 pipe_semaphore_wait(&rast->tasks[i].work_done);
864 }
865 }
866 }
867
868
869 /**
870 * This is the thread's main entrypoint.
871 * It's a simple loop:
872 * 1. wait for work
873 * 2. do work
874 * 3. signal that we're done
875 */
876 static PIPE_THREAD_ROUTINE( thread_func, init_data )
877 {
878 struct lp_rasterizer_task *task = (struct lp_rasterizer_task *) init_data;
879 struct lp_rasterizer *rast = task->rast;
880 boolean debug = false;
881
882 while (1) {
883 /* wait for work */
884 if (debug)
885 debug_printf("thread %d waiting for work\n", task->thread_index);
886 pipe_semaphore_wait(&task->work_ready);
887
888 if (rast->exit_flag)
889 break;
890
891 if (task->thread_index == 0) {
892 /* thread[0]:
893 * - get next scene to rasterize
894 * - map the framebuffer surfaces
895 */
896 lp_rast_begin( rast,
897 lp_scene_dequeue( rast->full_scenes, TRUE ) );
898 }
899
900 /* Wait for all threads to get here so that threads[1+] don't
901 * get a null rast->curr_scene pointer.
902 */
903 pipe_barrier_wait( &rast->barrier );
904
905 /* do work */
906 if (debug)
907 debug_printf("thread %d doing work\n", task->thread_index);
908
909 rasterize_scene(task,
910 rast->curr_scene);
911
912 /* wait for all threads to finish with this scene */
913 pipe_barrier_wait( &rast->barrier );
914
915 /* XXX: shouldn't be necessary:
916 */
917 if (task->thread_index == 0) {
918 lp_rast_end( rast );
919 }
920
921 /* signal done with work */
922 if (debug)
923 debug_printf("thread %d done working\n", task->thread_index);
924
925 pipe_semaphore_signal(&task->work_done);
926 }
927
928 return NULL;
929 }
930
931
932 /**
933 * Initialize semaphores and spawn the threads.
934 */
935 static void
936 create_rast_threads(struct lp_rasterizer *rast)
937 {
938 unsigned i;
939
940 /* NOTE: if num_threads is zero, we won't use any threads */
941 for (i = 0; i < rast->num_threads; i++) {
942 pipe_semaphore_init(&rast->tasks[i].work_ready, 0);
943 pipe_semaphore_init(&rast->tasks[i].work_done, 0);
944 rast->threads[i] = pipe_thread_create(thread_func,
945 (void *) &rast->tasks[i]);
946 }
947 }
948
949
950
951 /**
952 * Create new lp_rasterizer. If num_threads is zero, don't create any
953 * new threads, do rendering synchronously.
954 * \param num_threads number of rasterizer threads to create
955 */
956 struct lp_rasterizer *
957 lp_rast_create( unsigned num_threads )
958 {
959 struct lp_rasterizer *rast;
960 unsigned i;
961
962 rast = CALLOC_STRUCT(lp_rasterizer);
963 if(!rast)
964 return NULL;
965
966 rast->full_scenes = lp_scene_queue_create();
967
968 for (i = 0; i < Elements(rast->tasks); i++) {
969 struct lp_rasterizer_task *task = &rast->tasks[i];
970 task->rast = rast;
971 task->thread_index = i;
972 }
973
974 rast->num_threads = num_threads;
975
976 create_rast_threads(rast);
977
978 /* for synchronizing rasterization threads */
979 pipe_barrier_init( &rast->barrier, rast->num_threads );
980
981 memset(lp_swizzled_cbuf, 0, sizeof lp_swizzled_cbuf);
982
983 memset(lp_dummy_tile, 0, sizeof lp_dummy_tile);
984
985 return rast;
986 }
987
988
989 /* Shutdown:
990 */
991 void lp_rast_destroy( struct lp_rasterizer *rast )
992 {
993 unsigned i;
994
995 /* Set exit_flag and signal each thread's work_ready semaphore.
996 * Each thread will be woken up, notice that the exit_flag is set and
997 * break out of its main loop. The thread will then exit.
998 */
999 rast->exit_flag = TRUE;
1000 for (i = 0; i < rast->num_threads; i++) {
1001 pipe_semaphore_signal(&rast->tasks[i].work_ready);
1002 }
1003
1004 /* Wait for threads to terminate before cleaning up per-thread data */
1005 for (i = 0; i < rast->num_threads; i++) {
1006 pipe_thread_wait(rast->threads[i]);
1007 }
1008
1009 /* Clean up per-thread data */
1010 for (i = 0; i < rast->num_threads; i++) {
1011 pipe_semaphore_destroy(&rast->tasks[i].work_ready);
1012 pipe_semaphore_destroy(&rast->tasks[i].work_done);
1013 }
1014
1015 /* for synchronizing rasterization threads */
1016 pipe_barrier_destroy( &rast->barrier );
1017
1018 lp_scene_queue_destroy(rast->full_scenes);
1019
1020 FREE(rast);
1021 }
1022
1023
1024 /** Return number of rasterization threads */
1025 unsigned
1026 lp_rast_get_num_threads( struct lp_rasterizer *rast )
1027 {
1028 return rast->num_threads;
1029 }
1030
1031