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"
38 #include "lp_rast_priv.h"
39 #include "lp_tile_soa.h"
40 #include "lp_bld_debug.h"
45 * Begin the rasterization phase.
46 * Map the framebuffer surfaces. Initialize the 'rast' state.
49 lp_rast_begin( struct lp_rasterizer
*rast
,
50 const struct pipe_framebuffer_state
*fb
,
52 boolean write_zstencil
)
54 struct pipe_screen
*screen
= rast
->screen
;
55 struct pipe_surface
*cbuf
, *zsbuf
;
58 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
60 util_copy_framebuffer_state(&rast
->state
.fb
, fb
);
62 rast
->state
.write_zstencil
= write_zstencil
;
63 rast
->state
.write_color
= write_color
;
65 rast
->check_for_clipped_tiles
= (fb
->width
% TILE_SIZE
!= 0 ||
66 fb
->height
% TILE_SIZE
!= 0);
69 for (i
= 0; i
< rast
->state
.fb
.nr_cbufs
; i
++) {
70 cbuf
= rast
->state
.fb
.cbufs
[i
];
72 rast
->cbuf_transfer
[i
] = screen
->get_tex_transfer(rast
->screen
,
77 PIPE_TRANSFER_READ_WRITE
,
81 if (!rast
->cbuf_transfer
[i
])
84 rast
->cbuf_map
[i
] = screen
->transfer_map(rast
->screen
,
85 rast
->cbuf_transfer
[i
]);
86 if (!rast
->cbuf_map
[i
])
91 zsbuf
= rast
->state
.fb
.zsbuf
;
93 rast
->zsbuf_transfer
= screen
->get_tex_transfer(rast
->screen
,
98 PIPE_TRANSFER_READ_WRITE
,
102 if (!rast
->zsbuf_transfer
)
105 rast
->zsbuf_map
= screen
->transfer_map(rast
->screen
,
106 rast
->zsbuf_transfer
);
107 if (!rast
->zsbuf_map
)
114 /* Unmap and release transfers?
121 * Finish the rasterization phase.
122 * Unmap framebuffer surfaces.
125 lp_rast_end( struct lp_rasterizer
*rast
)
127 struct pipe_screen
*screen
= rast
->screen
;
130 for (i
= 0; i
< rast
->state
.fb
.nr_cbufs
; i
++) {
131 if (rast
->cbuf_map
[i
])
132 screen
->transfer_unmap(screen
, rast
->cbuf_transfer
[i
]);
134 if (rast
->cbuf_transfer
[i
])
135 screen
->tex_transfer_destroy(rast
->cbuf_transfer
[i
]);
137 rast
->cbuf_transfer
[i
] = NULL
;
138 rast
->cbuf_map
[i
] = NULL
;
142 screen
->transfer_unmap(screen
, rast
->zsbuf_transfer
);
144 if (rast
->zsbuf_transfer
)
145 screen
->tex_transfer_destroy(rast
->zsbuf_transfer
);
147 rast
->zsbuf_transfer
= NULL
;
148 rast
->zsbuf_map
= NULL
;
153 * Begining rasterization of a tile.
154 * \param x window X position of the tile, in pixels
155 * \param y window Y position of the tile, in pixels
158 lp_rast_start_tile( struct lp_rasterizer
*rast
,
159 unsigned thread_index
,
160 unsigned x
, unsigned y
)
162 LP_DBG(DEBUG_RAST
, "%s %d,%d\n", __FUNCTION__
, x
, y
);
164 rast
->tasks
[thread_index
].x
= x
;
165 rast
->tasks
[thread_index
].y
= y
;
170 * Clear the rasterizer's current color tile.
171 * This is a bin command called during bin processing.
173 void lp_rast_clear_color( struct lp_rasterizer
*rast
,
174 unsigned thread_index
,
175 const union lp_rast_cmd_arg arg
)
177 const uint8_t *clear_color
= arg
.clear_color
;
178 uint8_t **color_tile
= rast
->tasks
[thread_index
].tile
.color
;
181 LP_DBG(DEBUG_RAST
, "%s 0x%x,0x%x,0x%x,0x%x\n", __FUNCTION__
,
187 if (clear_color
[0] == clear_color
[1] &&
188 clear_color
[1] == clear_color
[2] &&
189 clear_color
[2] == clear_color
[3]) {
190 for (i
= 0; i
< rast
->state
.fb
.nr_cbufs
; i
++) {
191 memset(color_tile
[i
], clear_color
[0], TILE_SIZE
* TILE_SIZE
* 4);
196 for (i
= 0; i
< rast
->state
.fb
.nr_cbufs
; i
++)
197 for (y
= 0; y
< TILE_SIZE
; y
++)
198 for (x
= 0; x
< TILE_SIZE
; x
++)
199 for (chan
= 0; chan
< 4; ++chan
)
200 TILE_PIXEL(color_tile
[i
], x
, y
, chan
) = clear_color
[chan
];
206 * Clear the rasterizer's current z/stencil tile.
207 * This is a bin command called during bin processing.
209 void lp_rast_clear_zstencil( struct lp_rasterizer
*rast
,
210 unsigned thread_index
,
211 const union lp_rast_cmd_arg arg
)
214 uint32_t *depth_tile
= rast
->tasks
[thread_index
].tile
.depth
;
216 LP_DBG(DEBUG_RAST
, "%s 0x%x\n", __FUNCTION__
, arg
.clear_zstencil
);
218 for (i
= 0; i
< TILE_SIZE
; i
++)
219 for (j
= 0; j
< TILE_SIZE
; j
++)
220 depth_tile
[i
*TILE_SIZE
+ j
] = arg
.clear_zstencil
;
225 * Load tile color from the framebuffer surface.
226 * This is a bin command called during bin processing.
228 void lp_rast_load_color( struct lp_rasterizer
*rast
,
229 unsigned thread_index
,
230 const union lp_rast_cmd_arg arg
)
232 struct lp_rasterizer_task
*task
= &rast
->tasks
[thread_index
];
233 const unsigned x
= task
->x
;
234 const unsigned y
= task
->y
;
237 LP_DBG(DEBUG_RAST
, "%s at %u, %u\n", __FUNCTION__
, x
, y
);
239 for (i
= 0; i
< rast
->state
.fb
.nr_cbufs
; i
++) {
240 struct pipe_transfer
*transfer
= rast
->cbuf_transfer
[i
];
244 if (x
>= transfer
->width
)
247 if (y
>= transfer
->height
)
249 /* XXX: require tile-size aligned render target dimensions:
251 if (x
+ w
> transfer
->width
)
252 w
-= x
+ w
- transfer
->width
;
254 if (y
+ h
> transfer
->height
)
255 h
-= y
+ h
- transfer
->height
;
259 assert(w
<= TILE_SIZE
);
260 assert(h
<= TILE_SIZE
);
262 lp_tile_read_4ub(transfer
->texture
->format
,
263 rast
->tasks
[thread_index
].tile
.color
[i
],
273 * Load tile z/stencil from the framebuffer surface.
274 * This is a bin command called during bin processing.
276 void lp_rast_load_zstencil( struct lp_rasterizer
*rast
,
277 unsigned thread_index
,
278 const union lp_rast_cmd_arg arg
)
280 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
282 /* call u_tile func to load depth (and stencil?) from surface */
286 void lp_rast_set_state( struct lp_rasterizer
*rast
,
287 unsigned thread_index
,
288 const union lp_rast_cmd_arg arg
)
290 const struct lp_rast_state
*state
= arg
.set_state
;
292 LP_DBG(DEBUG_RAST
, "%s %p\n", __FUNCTION__
, (void *) state
);
294 /* just set the current state pointer for this rasterizer */
295 rast
->tasks
[thread_index
].current_state
= state
;
304 * Run the shader on all blocks in a tile. This is used when a tile is
305 * completely contained inside a triangle.
306 * This is a bin command called during bin processing.
308 void lp_rast_shade_tile( struct lp_rasterizer
*rast
,
309 unsigned thread_index
,
310 const union lp_rast_cmd_arg arg
)
312 /* Set c1,c2,c3 to large values so the in/out test always passes */
313 const int32_t c1
= INT_MIN
, c2
= INT_MIN
, c3
= INT_MIN
;
314 const struct lp_rast_shader_inputs
*inputs
= arg
.shade_tile
;
315 const unsigned tile_x
= rast
->tasks
[thread_index
].x
;
316 const unsigned tile_y
= rast
->tasks
[thread_index
].y
;
319 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
321 /* Use the existing preference for 4x4 (four quads) shading:
323 for (y
= 0; y
< TILE_SIZE
; y
+= 4)
324 for (x
= 0; x
< TILE_SIZE
; x
+= 4)
325 lp_rast_shade_quads( rast
,
335 * Compute shading for a 4x4 block of pixels.
336 * This is a bin command called during bin processing.
338 void lp_rast_shade_quads( struct lp_rasterizer
*rast
,
339 unsigned thread_index
,
340 const struct lp_rast_shader_inputs
*inputs
,
341 unsigned x
, unsigned y
,
342 int32_t c1
, int32_t c2
, int32_t c3
)
344 const struct lp_rast_state
*state
= rast
->tasks
[thread_index
].current_state
;
345 struct lp_rast_tile
*tile
= &rast
->tasks
[thread_index
].tile
;
346 uint8_t *color
[PIPE_MAX_COLOR_BUFS
];
356 assert(x
% TILE_VECTOR_WIDTH
== 0);
357 assert(y
% TILE_VECTOR_HEIGHT
== 0);
359 assert((x
% 4) == 0);
360 assert((y
% 4) == 0);
366 /* offset of the 16x16 pixel block within the tile */
367 block_offset
= ((iy
/4)*(16*16) + (ix
/4)*16);
370 for (i
= 0; i
< rast
->state
.fb
.nr_cbufs
; i
++)
371 color
[i
] = tile
->color
[i
] + 4 * block_offset
;
374 depth
= tile
->depth
+ block_offset
;
379 assert(lp_check_alignment(tile
->depth
, 16));
380 assert(lp_check_alignment(tile
->color
[0], 16));
381 assert(lp_check_alignment(state
->jit_context
.blend_color
, 16));
383 assert(lp_check_alignment(inputs
->step
[0], 16));
384 assert(lp_check_alignment(inputs
->step
[1], 16));
385 assert(lp_check_alignment(inputs
->step
[2], 16));
389 state
->jit_function( &state
->jit_context
,
397 inputs
->step
[0], inputs
->step
[1], inputs
->step
[2]);
406 * Write the rasterizer's color tile to the framebuffer.
408 static void lp_rast_store_color( struct lp_rasterizer
*rast
,
409 unsigned thread_index
)
411 const unsigned x
= rast
->tasks
[thread_index
].x
;
412 const unsigned y
= rast
->tasks
[thread_index
].y
;
415 for (i
= 0; i
< rast
->state
.fb
.nr_cbufs
; i
++) {
416 struct pipe_transfer
*transfer
= rast
->cbuf_transfer
[i
];
420 if (x
>= transfer
->width
)
423 if (y
>= transfer
->height
)
426 /* XXX: require tile-size aligned render target dimensions:
428 if (x
+ w
> transfer
->width
)
429 w
-= x
+ w
- transfer
->width
;
431 if (y
+ h
> transfer
->height
)
432 h
-= y
+ h
- transfer
->height
;
436 assert(w
<= TILE_SIZE
);
437 assert(h
<= TILE_SIZE
);
439 LP_DBG(DEBUG_RAST
, "%s [%u] %d,%d %dx%d\n", __FUNCTION__
,
440 thread_index
, x
, y
, w
, h
);
442 lp_tile_write_4ub(transfer
->texture
->format
,
443 rast
->tasks
[thread_index
].tile
.color
[i
],
453 lp_tile_write_z32(const uint32_t *src
, uint8_t *dst
, unsigned dst_stride
,
454 unsigned x0
, unsigned y0
, unsigned w
, unsigned h
)
457 uint8_t *dst_row
= dst
+ y0
*dst_stride
;
458 for (y
= 0; y
< h
; ++y
) {
459 uint32_t *dst_pixel
= (uint32_t *)(dst_row
+ x0
*4);
460 for (x
= 0; x
< w
; ++x
) {
461 *dst_pixel
++ = *src
++;
463 dst_row
+= dst_stride
;
468 * Write the rasterizer's z/stencil tile to the framebuffer.
470 static void lp_rast_store_zstencil( struct lp_rasterizer
*rast
,
471 unsigned thread_index
)
473 const unsigned x
= rast
->tasks
[thread_index
].x
;
474 const unsigned y
= rast
->tasks
[thread_index
].y
;
475 unsigned w
= TILE_SIZE
;
476 unsigned h
= TILE_SIZE
;
478 if (x
+ w
> rast
->state
.fb
.width
)
479 w
-= x
+ w
- rast
->state
.fb
.width
;
481 if (y
+ h
> rast
->state
.fb
.height
)
482 h
-= y
+ h
- rast
->state
.fb
.height
;
484 LP_DBG(DEBUG_RAST
, "%s %d,%d %dx%d\n", __FUNCTION__
, x
, y
, w
, h
);
486 assert(rast
->zsbuf_transfer
->texture
->format
== PIPE_FORMAT_Z32_UNORM
);
487 lp_tile_write_z32(rast
->tasks
[thread_index
].tile
.depth
,
489 rast
->zsbuf_transfer
->stride
,
495 * Write the rasterizer's tiles to the framebuffer.
498 lp_rast_end_tile( struct lp_rasterizer
*rast
,
499 unsigned thread_index
)
501 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
503 if (rast
->state
.write_color
)
504 lp_rast_store_color(rast
, thread_index
);
506 if (rast
->state
.write_zstencil
)
507 lp_rast_store_zstencil(rast
, thread_index
);
512 * Signal on a fence. This is called during bin execution/rasterization.
515 void lp_rast_fence( struct lp_rasterizer
*rast
,
516 unsigned thread_index
,
517 const union lp_rast_cmd_arg arg
)
519 struct lp_fence
*fence
= arg
.fence
;
521 pipe_mutex_lock( fence
->mutex
);
524 assert(fence
->count
<= fence
->rank
);
526 LP_DBG(DEBUG_RAST
, "%s count=%u rank=%u\n", __FUNCTION__
,
527 fence
->count
, fence
->rank
);
529 pipe_condvar_signal( fence
->signalled
);
531 pipe_mutex_unlock( fence
->mutex
);
536 * When all the threads are done rasterizing a scene, one thread will
537 * call this function to reset the scene and put it onto the empty queue.
540 release_scene( struct lp_rasterizer
*rast
,
541 struct lp_scene
*scene
)
543 util_unreference_framebuffer_state( &scene
->fb
);
545 lp_scene_reset( scene
);
546 lp_scene_enqueue( rast
->empty_scenes
, scene
);
547 rast
->curr_scene
= NULL
;
552 * Rasterize commands for a single bin.
553 * \param x, y position of the bin's tile in the framebuffer
554 * Must be called between lp_rast_begin() and lp_rast_end().
558 rasterize_bin( struct lp_rasterizer
*rast
,
559 unsigned thread_index
,
560 const struct cmd_bin
*bin
,
563 const struct cmd_block_list
*commands
= &bin
->commands
;
564 struct cmd_block
*block
;
567 lp_rast_start_tile( rast
, thread_index
, x
, y
);
569 /* simply execute each of the commands in the block list */
570 for (block
= commands
->head
; block
; block
= block
->next
) {
571 for (k
= 0; k
< block
->count
; k
++) {
572 block
->cmd
[k
]( rast
, thread_index
, block
->arg
[k
] );
576 lp_rast_end_tile( rast
, thread_index
);
581 * Rasterize/execute all bins within a scene.
585 rasterize_scene( struct lp_rasterizer
*rast
,
586 unsigned thread_index
,
587 struct lp_scene
*scene
,
590 /* loop over scene bins, rasterize each */
594 for (i
= 0; i
< scene
->tiles_x
; i
++) {
595 for (j
= 0; j
< scene
->tiles_y
; j
++) {
596 struct cmd_bin
*bin
= lp_get_bin(scene
, i
, j
);
597 rasterize_bin( rast
, thread_index
,
598 bin
, i
* TILE_SIZE
, j
* TILE_SIZE
);
608 while ((bin
= lp_scene_bin_iter_next(scene
, &x
, &y
))) {
609 rasterize_bin( rast
, thread_index
, bin
, x
* TILE_SIZE
, y
* TILE_SIZE
);
617 * Called by setup module when it has something for us to render.
620 lp_rasterize_scene( struct lp_rasterizer
*rast
,
621 struct lp_scene
*scene
,
622 const struct pipe_framebuffer_state
*fb
,
625 boolean debug
= false;
627 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
631 printf("rasterize scene:\n");
632 printf(" data size: %u\n", lp_scene_data_size(scene
));
633 for (y
= 0; y
< scene
->tiles_y
; y
++) {
634 for (x
= 0; x
< scene
->tiles_x
; x
++) {
635 printf(" bin %u, %u size: %u\n", x
, y
,
636 lp_scene_bin_size(scene
, x
, y
));
641 /* save framebuffer state in the bin */
642 util_copy_framebuffer_state(&scene
->fb
, fb
);
643 scene
->write_depth
= write_depth
;
645 if (rast
->num_threads
== 0) {
648 lp_rast_begin( rast
, fb
,
649 fb
->nr_cbufs
!= 0, /* always write color if cbufs present */
650 fb
->zsbuf
!= NULL
&& write_depth
);
652 lp_scene_bin_iter_begin( scene
);
653 rasterize_scene( rast
, 0, scene
, write_depth
);
655 release_scene( rast
, scene
);
660 /* threaded rendering! */
663 lp_scene_enqueue( rast
->full_scenes
, scene
);
665 /* signal the threads that there's work to do */
666 for (i
= 0; i
< rast
->num_threads
; i
++) {
667 pipe_semaphore_signal(&rast
->tasks
[i
].work_ready
);
670 /* wait for work to complete */
671 for (i
= 0; i
< rast
->num_threads
; i
++) {
672 pipe_semaphore_wait(&rast
->tasks
[i
].work_done
);
676 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
681 * This is the thread's main entrypoint.
682 * It's a simple loop:
685 * 3. signal that we're done
688 thread_func( void *init_data
)
690 struct lp_rasterizer_task
*task
= (struct lp_rasterizer_task
*) init_data
;
691 struct lp_rasterizer
*rast
= task
->rast
;
692 boolean debug
= false;
697 debug_printf("thread %d waiting for work\n", task
->thread_index
);
698 pipe_semaphore_wait(&task
->work_ready
);
700 if (task
->thread_index
== 0) {
702 * - get next scene to rasterize
703 * - map the framebuffer surfaces
705 const struct pipe_framebuffer_state
*fb
;
708 rast
->curr_scene
= lp_scene_dequeue( rast
->full_scenes
);
710 lp_scene_bin_iter_begin( rast
->curr_scene
);
712 fb
= &rast
->curr_scene
->fb
;
713 write_depth
= rast
->curr_scene
->write_depth
;
715 lp_rast_begin( rast
, fb
,
717 fb
->zsbuf
!= NULL
&& write_depth
);
720 /* Wait for all threads to get here so that threads[1+] don't
721 * get a null rast->curr_scene pointer.
723 pipe_barrier_wait( &rast
->barrier
);
727 debug_printf("thread %d doing work\n", task
->thread_index
);
728 rasterize_scene(rast
,
731 rast
->curr_scene
->write_depth
);
733 /* wait for all threads to finish with this scene */
734 pipe_barrier_wait( &rast
->barrier
);
736 if (task
->thread_index
== 0) {
738 * - release the scene object
739 * - unmap the framebuffer surfaces
741 release_scene( rast
, rast
->curr_scene
);
745 /* signal done with work */
747 debug_printf("thread %d done working\n", task
->thread_index
);
748 pipe_semaphore_signal(&task
->work_done
);
756 * Initialize semaphores and spawn the threads.
759 create_rast_threads(struct lp_rasterizer
*rast
)
763 rast
->num_threads
= util_cpu_caps
.nr_cpus
;
764 rast
->num_threads
= debug_get_num_option("LP_NUM_THREADS", rast
->num_threads
);
765 rast
->num_threads
= MIN2(rast
->num_threads
, MAX_THREADS
);
767 /* NOTE: if num_threads is zero, we won't use any threads */
768 for (i
= 0; i
< rast
->num_threads
; i
++) {
769 pipe_semaphore_init(&rast
->tasks
[i
].work_ready
, 0);
770 pipe_semaphore_init(&rast
->tasks
[i
].work_done
, 0);
771 rast
->threads
[i
] = pipe_thread_create(thread_func
,
772 (void *) &rast
->tasks
[i
]);
779 * Create new lp_rasterizer.
780 * \param empty the queue to put empty scenes on after we've finished
783 struct lp_rasterizer
*
784 lp_rast_create( struct pipe_screen
*screen
, struct lp_scene_queue
*empty
)
786 struct lp_rasterizer
*rast
;
789 rast
= CALLOC_STRUCT(lp_rasterizer
);
793 rast
->screen
= screen
;
795 rast
->empty_scenes
= empty
;
796 rast
->full_scenes
= lp_scene_queue_create();
798 for (i
= 0; i
< Elements(rast
->tasks
); i
++) {
799 for (cbuf
= 0; cbuf
< PIPE_MAX_COLOR_BUFS
; cbuf
++ )
800 rast
->tasks
[i
].tile
.color
[cbuf
] = align_malloc( TILE_SIZE
*TILE_SIZE
*4, 16 );
802 rast
->tasks
[i
].tile
.depth
= align_malloc( TILE_SIZE
*TILE_SIZE
*4, 16 );
803 rast
->tasks
[i
].rast
= rast
;
804 rast
->tasks
[i
].thread_index
= i
;
807 create_rast_threads(rast
);
809 /* for synchronizing rasterization threads */
810 pipe_barrier_init( &rast
->barrier
, rast
->num_threads
);
818 void lp_rast_destroy( struct lp_rasterizer
*rast
)
822 util_unreference_framebuffer_state(&rast
->state
.fb
);
824 for (i
= 0; i
< Elements(rast
->tasks
); i
++) {
825 align_free(rast
->tasks
[i
].tile
.depth
);
826 for (cbuf
= 0; cbuf
< PIPE_MAX_COLOR_BUFS
; cbuf
++ )
827 align_free(rast
->tasks
[i
].tile
.color
[cbuf
]);
830 /* for synchronizing rasterization threads */
831 pipe_barrier_destroy( &rast
->barrier
);
837 /** Return number of rasterization threads */
839 lp_rast_get_num_threads( struct lp_rasterizer
*rast
)
841 return rast
->num_threads
;