1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
29 #include "util/u_memory.h"
30 #include "util/u_math.h"
31 #include "util/u_cpu_detect.h"
32 #include "util/u_surface.h"
34 #include "lp_scene_queue.h"
39 #include "lp_rast_priv.h"
40 #include "lp_tile_soa.h"
41 #include "gallivm/lp_bld_debug.h"
45 /* Begin rasterizing a scene:
48 lp_rast_begin( struct lp_rasterizer *rast,
49 struct lp_scene *scene )
51 const struct pipe_framebuffer_state *fb = &scene->fb;
52 boolean write_color = fb->nr_cbufs != 0;
53 boolean write_zstencil = fb->zsbuf != NULL;
56 rast->curr_scene = scene;
58 LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
60 rast->state.nr_cbufs = scene->fb.nr_cbufs;
61 rast->state.write_zstencil = write_zstencil;
62 rast->state.write_color = write_color;
64 for (i = 0; i < rast->state.nr_cbufs; i++) {
65 struct pipe_surface *cbuf = scene->fb.cbufs[i];
66 rast->cbuf[i].map = scene->cbuf_map[i];
67 rast->cbuf[i].format = cbuf->texture->format;
68 rast->cbuf[i].width = cbuf->width;
69 rast->cbuf[i].height = cbuf->height;
70 rast->cbuf[i].stride = llvmpipe_texture_stride(cbuf->texture, cbuf->level);
74 struct pipe_surface *zsbuf = scene->fb.zsbuf;
75 rast->zsbuf.map = scene->zsbuf_map;
76 rast->zsbuf.stride = llvmpipe_texture_stride(zsbuf->texture, zsbuf->level);
77 rast->zsbuf.blocksize =
78 util_format_get_blocksize(zsbuf->texture->format);
81 lp_scene_bin_iter_begin( scene );
88 lp_rast_end( struct lp_rasterizer *rast )
92 lp_scene_reset( rast->curr_scene );
94 for (i = 0; i < rast->state.nr_cbufs; i++)
95 rast->cbuf[i].map = NULL;
97 rast->zsbuf.map = NULL;
98 rast->curr_scene = NULL;
102 * Begining rasterization of a tile.
103 * \param x window X position of the tile, in pixels
104 * \param y window Y position of the tile, in pixels
107 lp_rast_start_tile(struct lp_rasterizer_task *task,
108 unsigned x, unsigned y)
110 LP_DBG(DEBUG_RAST, "%s %d,%d\n", __FUNCTION__, x, y);
118 * Clear the rasterizer's current color tile.
119 * This is a bin command called during bin processing.
122 lp_rast_clear_color(struct lp_rasterizer_task *task,
123 const union lp_rast_cmd_arg arg)
125 struct lp_rasterizer *rast = task->rast;
126 const uint8_t *clear_color = arg.clear_color;
127 uint8_t **color_tile = task->tile.color;
130 LP_DBG(DEBUG_RAST, "%s 0x%x,0x%x,0x%x,0x%x\n", __FUNCTION__,
136 if (clear_color[0] == clear_color[1] &&
137 clear_color[1] == clear_color[2] &&
138 clear_color[2] == clear_color[3]) {
139 /* clear to grayscale value {x, x, x, x} */
140 for (i = 0; i < rast->state.nr_cbufs; i++) {
141 memset(color_tile[i], clear_color[0], TILE_SIZE * TILE_SIZE * 4);
146 * Note: if the swizzled tile layout changes (see TILE_PIXEL) this code
147 * will need to change. It'll be pretty obvious when clearing no longer
150 const unsigned chunk = TILE_SIZE / 4;
151 for (i = 0; i < rast->state.nr_cbufs; i++) {
152 uint8_t *c = color_tile[i];
154 for (j = 0; j < 4 * TILE_SIZE; j++) {
155 memset(c, clear_color[0], chunk);
157 memset(c, clear_color[1], chunk);
159 memset(c, clear_color[2], chunk);
161 memset(c, clear_color[3], chunk);
164 assert(c - color_tile[i] == TILE_SIZE * TILE_SIZE * 4);
168 LP_COUNT(nr_color_tile_clear);
173 * Clear the rasterizer's current z/stencil tile.
174 * This is a bin command called during bin processing.
177 lp_rast_clear_zstencil(struct lp_rasterizer_task *task,
178 const union lp_rast_cmd_arg arg)
180 struct lp_rasterizer *rast = task->rast;
181 const unsigned tile_x = task->x;
182 const unsigned tile_y = task->y;
183 const unsigned height = TILE_SIZE / TILE_VECTOR_HEIGHT;
184 const unsigned width = TILE_SIZE * TILE_VECTOR_HEIGHT;
185 unsigned block_size = rast->zsbuf.blocksize;
187 unsigned dst_stride = rast->zsbuf.stride * TILE_VECTOR_HEIGHT;
190 LP_DBG(DEBUG_RAST, "%s 0x%x\n", __FUNCTION__, arg.clear_zstencil);
192 /*assert(rast->zsbuf.map);*/
193 if (!rast->zsbuf.map)
196 LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
199 * Clear the aera of the swizzled depth/depth buffer matching this tile, in
200 * stripes of TILE_VECTOR_HEIGHT x TILE_SIZE at a time.
202 * The swizzled depth format is such that the depths for
203 * TILE_VECTOR_HEIGHT x TILE_VECTOR_WIDTH pixels have consecutive offsets.
206 dst = lp_rast_depth_pointer(rast, tile_x, tile_y);
208 switch (block_size) {
210 memset(dst, (uint8_t) arg.clear_zstencil, height * width);
213 for (i = 0; i < height; i++) {
214 uint16_t *row = (uint16_t *)dst;
215 for (j = 0; j < width; j++)
216 *row++ = (uint16_t) arg.clear_zstencil;
221 for (i = 0; i < height; i++) {
222 uint32_t *row = (uint32_t *)dst;
223 for (j = 0; j < width; j++)
224 *row++ = arg.clear_zstencil;
236 * Load tile color from the framebuffer surface.
237 * This is a bin command called during bin processing.
240 lp_rast_load_color(struct lp_rasterizer_task *task,
241 const union lp_rast_cmd_arg arg)
243 struct lp_rasterizer *rast = task->rast;
244 const unsigned x = task->x, y = task->y;
247 LP_DBG(DEBUG_RAST, "%s at %u, %u\n", __FUNCTION__, x, y);
249 for (i = 0; i < rast->state.nr_cbufs; i++) {
250 if (x >= rast->cbuf[i].width || y >= rast->cbuf[i].height)
253 lp_tile_read_4ub(rast->cbuf[i].format,
256 rast->cbuf[i].stride,
258 TILE_SIZE, TILE_SIZE);
260 LP_COUNT(nr_color_tile_load);
266 lp_rast_set_state(struct lp_rasterizer_task *task,
267 const union lp_rast_cmd_arg arg)
269 const struct lp_rast_state *state = arg.set_state;
271 LP_DBG(DEBUG_RAST, "%s %p\n", __FUNCTION__, (void *) state);
273 /* just set the current state pointer for this rasterizer */
274 task->current_state = state;
280 * Run the shader on all blocks in a tile. This is used when a tile is
281 * completely contained inside a triangle.
282 * This is a bin command called during bin processing.
285 lp_rast_shade_tile(struct lp_rasterizer_task *task,
286 const union lp_rast_cmd_arg arg)
288 struct lp_rasterizer *rast = task->rast;
289 const struct lp_rast_state *state = task->current_state;
290 struct lp_rast_tile *tile = &task->tile;
291 const struct lp_rast_shader_inputs *inputs = arg.shade_tile;
292 const unsigned tile_x = task->x, tile_y = task->y;
295 LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
297 /* render the whole 64x64 tile in 4x4 chunks */
298 for (y = 0; y < TILE_SIZE; y += 4){
299 for (x = 0; x < TILE_SIZE; x += 4) {
300 uint8_t *color[PIPE_MAX_COLOR_BUFS];
302 unsigned block_offset, i;
304 /* offset of the 16x16 pixel block within the tile */
305 block_offset = ((y / 4) * (16 * 16) + (x / 4) * 16);
308 for (i = 0; i < rast->state.nr_cbufs; i++)
309 color[i] = tile->color[i] + 4 * block_offset;
312 depth = lp_rast_depth_pointer(rast, tile_x + x, tile_y + y);
315 state->jit_function[RAST_WHOLE]( &state->jit_context,
316 tile_x + x, tile_y + y,
323 INT_MIN, INT_MIN, INT_MIN,
331 * Compute shading for a 4x4 block of pixels.
332 * This is a bin command called during bin processing.
334 void lp_rast_shade_quads( struct lp_rasterizer_task *task,
335 const struct lp_rast_shader_inputs *inputs,
336 unsigned x, unsigned y,
337 int32_t c1, int32_t c2, int32_t c3)
339 const struct lp_rast_state *state = task->current_state;
340 struct lp_rasterizer *rast = task->rast;
341 struct lp_rast_tile *tile = &task->tile;
342 uint8_t *color[PIPE_MAX_COLOR_BUFS];
351 assert(x % TILE_VECTOR_WIDTH == 0);
352 assert(y % TILE_VECTOR_HEIGHT == 0);
354 assert((x % 4) == 0);
355 assert((y % 4) == 0);
360 /* offset of the 16x16 pixel block within the tile */
361 block_offset = ((iy / 4) * (16 * 16) + (ix / 4) * 16);
364 for (i = 0; i < rast->state.nr_cbufs; i++)
365 color[i] = tile->color[i] + 4 * block_offset;
368 depth = lp_rast_depth_pointer(rast, x, y);
371 assert(lp_check_alignment(tile->color[0], 16));
372 assert(lp_check_alignment(state->jit_context.blend_color, 16));
374 assert(lp_check_alignment(inputs->step[0], 16));
375 assert(lp_check_alignment(inputs->step[1], 16));
376 assert(lp_check_alignment(inputs->step[2], 16));
379 state->jit_function[RAST_EDGE_TEST]( &state->jit_context,
395 * Set top row and left column of the tile's pixels to white. For debugging.
398 outline_tile(uint8_t *tile)
400 const uint8_t val = 0xff;
403 for (i = 0; i < TILE_SIZE; i++) {
404 TILE_PIXEL(tile, i, 0, 0) = val;
405 TILE_PIXEL(tile, i, 0, 1) = val;
406 TILE_PIXEL(tile, i, 0, 2) = val;
407 TILE_PIXEL(tile, i, 0, 3) = val;
409 TILE_PIXEL(tile, 0, i, 0) = val;
410 TILE_PIXEL(tile, 0, i, 1) = val;
411 TILE_PIXEL(tile, 0, i, 2) = val;
412 TILE_PIXEL(tile, 0, i, 3) = val;
418 * Draw grid of gray lines at 16-pixel intervals across the tile to
419 * show the sub-tile boundaries. For debugging.
422 outline_subtiles(uint8_t *tile)
424 const uint8_t val = 0x80;
425 const unsigned step = 16;
428 for (i = 0; i < TILE_SIZE; i += step) {
429 for (j = 0; j < TILE_SIZE; j++) {
430 TILE_PIXEL(tile, i, j, 0) = val;
431 TILE_PIXEL(tile, i, j, 1) = val;
432 TILE_PIXEL(tile, i, j, 2) = val;
433 TILE_PIXEL(tile, i, j, 3) = val;
435 TILE_PIXEL(tile, j, i, 0) = val;
436 TILE_PIXEL(tile, j, i, 1) = val;
437 TILE_PIXEL(tile, j, i, 2) = val;
438 TILE_PIXEL(tile, j, i, 3) = val;
448 * Write the rasterizer's color tile to the framebuffer.
451 lp_rast_store_color(struct lp_rasterizer_task *task)
453 struct lp_rasterizer *rast = task->rast;
454 const unsigned x = task->x, y = task->y;
457 for (i = 0; i < rast->state.nr_cbufs; i++) {
458 if (x >= rast->cbuf[i].width)
461 if (y >= rast->cbuf[i].height)
464 LP_DBG(DEBUG_RAST, "%s [%u] %d,%d\n", __FUNCTION__,
465 task->thread_index, x, y);
467 if (LP_DEBUG & DEBUG_SHOW_SUBTILES)
468 outline_subtiles(task->tile.color[i]);
469 else if (LP_DEBUG & DEBUG_SHOW_TILES)
470 outline_tile(task->tile.color[i]);
472 lp_tile_write_4ub(rast->cbuf[i].format,
475 rast->cbuf[i].stride,
477 TILE_SIZE, TILE_SIZE);
479 LP_COUNT(nr_color_tile_store);
486 * Signal on a fence. This is called during bin execution/rasterization.
490 lp_rast_fence(struct lp_rasterizer_task *task,
491 const union lp_rast_cmd_arg arg)
493 struct lp_fence *fence = arg.fence;
494 lp_fence_signal(fence);
501 * Rasterize commands for a single bin.
502 * \param x, y position of the bin's tile in the framebuffer
503 * Must be called between lp_rast_begin() and lp_rast_end().
507 rasterize_bin(struct lp_rasterizer_task *task,
508 const struct cmd_bin *bin,
511 const struct cmd_block_list *commands = &bin->commands;
512 struct cmd_block *block;
515 lp_rast_start_tile( task, x * TILE_SIZE, y * TILE_SIZE );
517 /* simply execute each of the commands in the block list */
518 for (block = commands->head; block; block = block->next) {
519 for (k = 0; k < block->count; k++) {
520 block->cmd[k]( task, block->arg[k] );
524 /* Write the rasterizer's tiles to the framebuffer.
526 if (task->rast->state.write_color)
527 lp_rast_store_color(task);
529 /* Free data for this bin.
531 lp_scene_bin_reset( task->rast->curr_scene, x, y);
535 #define RAST(x) { lp_rast_##x, #x }
544 RAST(clear_zstencil),
552 debug_bin( const struct cmd_bin *bin )
554 const struct cmd_block *head = bin->commands.head;
557 for (i = 0; i < head->count; i++) {
558 debug_printf("%d: ", i);
559 for (j = 0; j < Elements(cmd_names); j++) {
560 if (head->cmd[i] == cmd_names[j].cmd) {
561 debug_printf("%s\n", cmd_names[j].name);
565 if (j == Elements(cmd_names))
566 debug_printf("...other\n");
571 /* An empty bin is one that just loads the contents of the tile and
572 * stores them again unchanged. This typically happens when bins have
573 * been flushed for some reason in the middle of a frame, or when
574 * incremental updates are being made to a render target.
576 * Try to avoid doing pointless work in this case.
579 is_empty_bin( const struct cmd_bin *bin )
581 const struct cmd_block *head = bin->commands.head;
587 /* We emit at most two load-tile commands at the start of the first
588 * command block. In addition we seem to emit a couple of
589 * set-state commands even in empty bins.
591 * As a heuristic, if a bin has more than 4 commands, consider it
594 if (head->next != NULL ||
599 for (i = 0; i < head->count; i++)
600 if (head->cmd[i] != lp_rast_load_color &&
601 head->cmd[i] != lp_rast_set_state) {
611 * Rasterize/execute all bins within a scene.
615 rasterize_scene(struct lp_rasterizer_task *task,
616 struct lp_scene *scene)
618 /* loop over scene bins, rasterize each */
622 for (i = 0; i < scene->tiles_x; i++) {
623 for (j = 0; j < scene->tiles_y; j++) {
624 struct cmd_bin *bin = lp_scene_get_bin(scene, i, j);
625 rasterize_bin(task, bin, i, j);
635 while ((bin = lp_scene_bin_iter_next(scene, &x, &y))) {
636 if (!is_empty_bin( bin ))
637 rasterize_bin(task, bin, x, y);
645 * Called by setup module when it has something for us to render.
648 lp_rast_queue_scene( struct lp_rasterizer *rast,
649 struct lp_scene *scene)
651 LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);
653 if (rast->num_threads == 0) {
656 lp_rast_begin( rast, scene );
658 rasterize_scene( &rast->tasks[0], scene );
660 lp_scene_reset( scene );
661 rast->curr_scene = NULL;
664 /* threaded rendering! */
667 lp_scene_enqueue( rast->full_scenes, scene );
669 /* signal the threads that there's work to do */
670 for (i = 0; i < rast->num_threads; i++) {
671 pipe_semaphore_signal(&rast->tasks[i].work_ready);
675 LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);
680 lp_rast_finish( struct lp_rasterizer *rast )
682 if (rast->num_threads == 0) {
688 /* wait for work to complete */
689 for (i = 0; i < rast->num_threads; i++) {
690 pipe_semaphore_wait(&rast->tasks[i].work_done);
697 * This is the thread's main entrypoint.
698 * It's a simple loop:
701 * 3. signal that we're done
703 static PIPE_THREAD_ROUTINE( thread_func, init_data )
705 struct lp_rasterizer_task *task = (struct lp_rasterizer_task *) init_data;
706 struct lp_rasterizer *rast = task->rast;
707 boolean debug = false;
712 debug_printf("thread %d waiting for work\n", task->thread_index);
713 pipe_semaphore_wait(&task->work_ready);
718 if (task->thread_index == 0) {
720 * - get next scene to rasterize
721 * - map the framebuffer surfaces
724 lp_scene_dequeue( rast->full_scenes, TRUE ) );
727 /* Wait for all threads to get here so that threads[1+] don't
728 * get a null rast->curr_scene pointer.
730 pipe_barrier_wait( &rast->barrier );
734 debug_printf("thread %d doing work\n", task->thread_index);
736 rasterize_scene(task,
739 /* wait for all threads to finish with this scene */
740 pipe_barrier_wait( &rast->barrier );
742 /* XXX: shouldn't be necessary:
744 if (task->thread_index == 0) {
748 /* signal done with work */
750 debug_printf("thread %d done working\n", task->thread_index);
752 pipe_semaphore_signal(&task->work_done);
760 * Initialize semaphores and spawn the threads.
763 create_rast_threads(struct lp_rasterizer *rast)
767 #ifdef PIPE_OS_WINDOWS
768 /* Multithreading not supported on windows until conditions and barriers are
769 * properly implemented. */
770 rast->num_threads = 0;
772 #ifdef PIPE_OS_EMBEDDED
773 rast->num_threads = 0;
775 rast->num_threads = util_cpu_caps.nr_cpus;
777 rast->num_threads = debug_get_num_option("LP_NUM_THREADS", rast->num_threads);
778 rast->num_threads = MIN2(rast->num_threads, MAX_THREADS);
781 /* NOTE: if num_threads is zero, we won't use any threads */
782 for (i = 0; i < rast->num_threads; i++) {
783 pipe_semaphore_init(&rast->tasks[i].work_ready, 0);
784 pipe_semaphore_init(&rast->tasks[i].work_done, 0);
785 rast->threads[i] = pipe_thread_create(thread_func,
786 (void *) &rast->tasks[i]);
793 * Create new lp_rasterizer.
794 * \param empty the queue to put empty scenes on after we've finished
797 struct lp_rasterizer *
798 lp_rast_create( void )
800 struct lp_rasterizer *rast;
803 rast = CALLOC_STRUCT(lp_rasterizer);
807 rast->full_scenes = lp_scene_queue_create();
809 for (i = 0; i < Elements(rast->tasks); i++) {
810 struct lp_rasterizer_task *task = &rast->tasks[i];
812 for (cbuf = 0; cbuf < PIPE_MAX_COLOR_BUFS; cbuf++ )
813 task->tile.color[cbuf] = align_malloc(TILE_SIZE * TILE_SIZE * 4, 16);
816 task->thread_index = i;
819 create_rast_threads(rast);
821 /* for synchronizing rasterization threads */
822 pipe_barrier_init( &rast->barrier, rast->num_threads );
830 void lp_rast_destroy( struct lp_rasterizer *rast )
834 for (i = 0; i < Elements(rast->tasks); i++) {
835 for (cbuf = 0; cbuf < PIPE_MAX_COLOR_BUFS; cbuf++ )
836 align_free(rast->tasks[i].tile.color[cbuf]);
839 /* Set exit_flag and signal each thread's work_ready semaphore.
840 * Each thread will be woken up, notice that the exit_flag is set and
841 * break out of its main loop. The thread will then exit.
843 rast->exit_flag = TRUE;
844 for (i = 0; i < rast->num_threads; i++) {
845 pipe_semaphore_signal(&rast->tasks[i].work_ready);
848 /* Wait for threads to terminate before cleaning up per-thread data */
849 for (i = 0; i < rast->num_threads; i++) {
850 pipe_thread_wait(rast->threads[i]);
853 /* Clean up per-thread data */
854 for (i = 0; i < rast->num_threads; i++) {
855 pipe_semaphore_destroy(&rast->tasks[i].work_ready);
856 pipe_semaphore_destroy(&rast->tasks[i].work_done);
859 /* for synchronizing rasterization threads */
860 pipe_barrier_destroy( &rast->barrier );
866 /** Return number of rasterization threads */
868 lp_rast_get_num_threads( struct lp_rasterizer *rast )
870 return rast->num_threads;