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_rect.h"
32 #include "util/u_surface.h"
33 #include "util/u_pack_color.h"
35 #include "os/os_time.h"
37 #include "lp_scene_queue.h"
43 #include "lp_rast_priv.h"
44 #include "gallivm/lp_bld_debug.h"
46 #include "lp_tex_sample.h"
51 const struct lp_rast_state
*jit_state
= NULL
;
52 const struct lp_rasterizer_task
*jit_task
= NULL
;
57 * Begin rasterizing a scene.
58 * Called once per scene by one thread.
61 lp_rast_begin( struct lp_rasterizer
*rast
,
62 struct lp_scene
*scene
)
65 rast
->curr_scene
= scene
;
67 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
69 lp_scene_begin_rasterization( scene
);
70 lp_scene_bin_iter_begin( scene
);
75 lp_rast_end( struct lp_rasterizer
*rast
)
77 lp_scene_end_rasterization( rast
->curr_scene
);
79 rast
->curr_scene
= NULL
;
84 * Begining rasterization of a tile.
85 * \param x window X position of the tile, in pixels
86 * \param y window Y position of the tile, in pixels
89 lp_rast_tile_begin(struct lp_rasterizer_task
*task
,
90 const struct cmd_bin
*bin
)
92 LP_DBG(DEBUG_RAST
, "%s %d,%d\n", __FUNCTION__
, bin
->x
, bin
->y
);
95 task
->x
= bin
->x
* TILE_SIZE
;
96 task
->y
= bin
->y
* TILE_SIZE
;
98 /* reset pointers to color and depth tile(s) */
99 memset(task
->color_tiles
, 0, sizeof(task
->color_tiles
));
100 task
->depth_tile
= NULL
;
105 * Clear the rasterizer's current color tile.
106 * This is a bin command called during bin processing.
109 lp_rast_clear_color(struct lp_rasterizer_task
*task
,
110 const union lp_rast_cmd_arg arg
)
112 const struct lp_scene
*scene
= task
->scene
;
114 if (scene
->fb
.nr_cbufs
) {
118 if (util_format_is_pure_integer(scene
->fb
.cbufs
[0]->format
)) {
120 * We expect int/uint clear values here, though some APIs
121 * might disagree (but in any case util_pack_color()
122 * couldn't handle it)...
124 LP_DBG(DEBUG_RAST
, "%s pure int 0x%x,0x%x,0x%x,0x%x\n", __FUNCTION__
,
125 arg
.clear_color
.ui
[0],
126 arg
.clear_color
.ui
[1],
127 arg
.clear_color
.ui
[2],
128 arg
.clear_color
.ui
[3]);
130 for (i
= 0; i
< scene
->fb
.nr_cbufs
; i
++) {
131 enum pipe_format format
= scene
->fb
.cbufs
[i
]->format
;
133 if (util_format_is_pure_sint(format
)) {
134 util_format_write_4i(format
, arg
.clear_color
.i
, 0, &uc
, 0, 0, 0, 1, 1);
137 assert(util_format_is_pure_uint(format
));
138 util_format_write_4ui(format
, arg
.clear_color
.ui
, 0, &uc
, 0, 0, 0, 1, 1);
141 util_fill_rect(scene
->cbufs
[i
].map
,
142 scene
->fb
.cbufs
[i
]->format
,
143 scene
->cbufs
[i
].stride
,
152 uint8_t clear_color
[4];
154 for (i
= 0; i
< 4; ++i
) {
155 clear_color
[i
] = float_to_ubyte(arg
.clear_color
.f
[i
]);
158 LP_DBG(DEBUG_RAST
, "%s 0x%x,0x%x,0x%x,0x%x\n", __FUNCTION__
,
164 for (i
= 0; i
< scene
->fb
.nr_cbufs
; i
++) {
166 util_pack_color(arg
.clear_color
.f
,
167 scene
->fb
.cbufs
[i
]->format
, &uc
);
169 util_fill_rect(scene
->cbufs
[i
].map
,
170 scene
->fb
.cbufs
[i
]->format
,
171 scene
->cbufs
[i
].stride
,
181 LP_COUNT(nr_color_tile_clear
);
188 * Clear the rasterizer's current z/stencil tile.
189 * This is a bin command called during bin processing.
192 lp_rast_clear_zstencil(struct lp_rasterizer_task
*task
,
193 const union lp_rast_cmd_arg arg
)
195 const struct lp_scene
*scene
= task
->scene
;
196 uint64_t clear_value64
= arg
.clear_zstencil
.value
;
197 uint64_t clear_mask64
= arg
.clear_zstencil
.mask
;
198 uint32_t clear_value
= (uint32_t) clear_value64
;
199 uint32_t clear_mask
= (uint32_t) clear_mask64
;
200 const unsigned height
= TILE_SIZE
;
201 const unsigned width
= TILE_SIZE
;
202 const unsigned block_size
= scene
->zsbuf
.blocksize
;
203 const unsigned dst_stride
= scene
->zsbuf
.stride
;
207 LP_DBG(DEBUG_RAST
, "%s: value=0x%08x, mask=0x%08x\n",
208 __FUNCTION__
, clear_value
, clear_mask
);
211 * Clear the area of the depth/depth buffer matching this tile.
214 if (scene
->fb
.zsbuf
) {
216 dst
= lp_rast_get_unswizzled_depth_tile_pointer(task
, LP_TEX_USAGE_READ_WRITE
);
218 clear_value
&= clear_mask
;
220 switch (block_size
) {
222 assert(clear_mask
== 0xff);
223 memset(dst
, (uint8_t) clear_value
, height
* width
);
226 if (clear_mask
== 0xffff) {
227 for (i
= 0; i
< height
; i
++) {
228 uint16_t *row
= (uint16_t *)dst
;
229 for (j
= 0; j
< width
; j
++)
230 *row
++ = (uint16_t) clear_value
;
235 for (i
= 0; i
< height
; i
++) {
236 uint16_t *row
= (uint16_t *)dst
;
237 for (j
= 0; j
< width
; j
++) {
238 uint16_t tmp
= ~clear_mask
& *row
;
239 *row
++ = clear_value
| tmp
;
246 if (clear_mask
== 0xffffffff) {
247 for (i
= 0; i
< height
; i
++) {
248 uint32_t *row
= (uint32_t *)dst
;
249 for (j
= 0; j
< width
; j
++)
250 *row
++ = clear_value
;
255 for (i
= 0; i
< height
; i
++) {
256 uint32_t *row
= (uint32_t *)dst
;
257 for (j
= 0; j
< width
; j
++) {
258 uint32_t tmp
= ~clear_mask
& *row
;
259 *row
++ = clear_value
| tmp
;
266 clear_value64
&= clear_mask64
;
267 if (clear_mask64
== 0xffffffffffULL
) {
268 for (i
= 0; i
< height
; i
++) {
269 uint64_t *row
= (uint64_t *)dst
;
270 for (j
= 0; j
< width
; j
++)
271 *row
++ = clear_value64
;
276 for (i
= 0; i
< height
; i
++) {
277 uint64_t *row
= (uint64_t *)dst
;
278 for (j
= 0; j
< width
; j
++) {
279 uint64_t tmp
= ~clear_mask64
& *row
;
280 *row
++ = clear_value64
| tmp
;
297 * Run the shader on all blocks in a tile. This is used when a tile is
298 * completely contained inside a triangle.
299 * This is a bin command called during bin processing.
302 lp_rast_shade_tile(struct lp_rasterizer_task
*task
,
303 const union lp_rast_cmd_arg arg
)
305 const struct lp_scene
*scene
= task
->scene
;
306 const struct lp_rast_shader_inputs
*inputs
= arg
.shade_tile
;
307 const struct lp_rast_state
*state
;
308 struct lp_fragment_shader_variant
*variant
;
309 const unsigned tile_x
= task
->x
, tile_y
= task
->y
;
312 if (inputs
->disable
) {
313 /* This command was partially binned and has been disabled */
317 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
324 variant
= state
->variant
;
326 /* render the whole 64x64 tile in 4x4 chunks */
327 for (y
= 0; y
< TILE_SIZE
; y
+= 4){
328 for (x
= 0; x
< TILE_SIZE
; x
+= 4) {
329 uint8_t *color
[PIPE_MAX_COLOR_BUFS
];
330 unsigned stride
[PIPE_MAX_COLOR_BUFS
];
331 uint8_t *depth
= NULL
;
332 unsigned depth_stride
= 0;
336 for (i
= 0; i
< scene
->fb
.nr_cbufs
; i
++){
337 stride
[i
] = scene
->cbufs
[i
].stride
;
339 color
[i
] = lp_rast_get_unswizzled_color_block_pointer(task
, i
, tile_x
+ x
, tile_y
+ y
);
343 if (scene
->zsbuf
.map
) {
344 depth
= lp_rast_get_unswizzled_depth_block_pointer(task
, tile_x
+ x
, tile_y
+ y
);
345 depth_stride
= scene
->zsbuf
.stride
;
349 /* run shader on 4x4 block */
350 BEGIN_JIT_CALL(state
, task
);
351 variant
->jit_function
[RAST_WHOLE
]( &state
->jit_context
,
352 tile_x
+ x
, tile_y
+ y
,
370 * Run the shader on all blocks in a tile. This is used when a tile is
371 * completely contained inside a triangle, and the shader is opaque.
372 * This is a bin command called during bin processing.
375 lp_rast_shade_tile_opaque(struct lp_rasterizer_task
*task
,
376 const union lp_rast_cmd_arg arg
)
378 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
385 lp_rast_shade_tile(task
, arg
);
390 * Compute shading for a 4x4 block of pixels inside a triangle.
391 * This is a bin command called during bin processing.
392 * \param x X position of quad in window coords
393 * \param y Y position of quad in window coords
396 lp_rast_shade_quads_mask(struct lp_rasterizer_task
*task
,
397 const struct lp_rast_shader_inputs
*inputs
,
398 unsigned x
, unsigned y
,
401 const struct lp_rast_state
*state
= task
->state
;
402 struct lp_fragment_shader_variant
*variant
= state
->variant
;
403 const struct lp_scene
*scene
= task
->scene
;
404 uint8_t *color
[PIPE_MAX_COLOR_BUFS
];
405 unsigned stride
[PIPE_MAX_COLOR_BUFS
];
407 unsigned depth_stride
= 0;
413 assert(x
< scene
->tiles_x
* TILE_SIZE
);
414 assert(y
< scene
->tiles_y
* TILE_SIZE
);
415 assert(x
% TILE_VECTOR_WIDTH
== 0);
416 assert(y
% TILE_VECTOR_HEIGHT
== 0);
418 assert((x
% 4) == 0);
419 assert((y
% 4) == 0);
422 for (i
= 0; i
< scene
->fb
.nr_cbufs
; i
++) {
423 stride
[i
] = scene
->cbufs
[i
].stride
;
425 color
[i
] = lp_rast_get_unswizzled_color_block_pointer(task
, i
, x
, y
);
429 if (scene
->zsbuf
.map
) {
430 depth_stride
= scene
->zsbuf
.stride
;
431 depth
= lp_rast_get_unswizzled_depth_block_pointer(task
, x
, y
);
434 assert(lp_check_alignment(state
->jit_context
.u8_blend_color
, 16));
436 /* run shader on 4x4 block */
437 BEGIN_JIT_CALL(state
, task
);
438 variant
->jit_function
[RAST_EDGE_TEST
](&state
->jit_context
,
456 * Begin a new occlusion query.
457 * This is a bin command put in all bins.
461 lp_rast_begin_query(struct lp_rasterizer_task
*task
,
462 const union lp_rast_cmd_arg arg
)
464 struct llvmpipe_query
*pq
= arg
.query_obj
;
466 assert(task
->query
[pq
->type
] == NULL
);
469 case PIPE_QUERY_OCCLUSION_COUNTER
:
470 task
->thread_data
.vis_counter
= 0;
472 case PIPE_QUERY_PRIMITIVES_GENERATED
:
473 case PIPE_QUERY_PRIMITIVES_EMITTED
:
474 case PIPE_QUERY_SO_STATISTICS
:
475 case PIPE_QUERY_PIPELINE_STATISTICS
:
476 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
483 task
->query
[pq
->type
] = pq
;
488 * End the current occlusion query.
489 * This is a bin command put in all bins.
493 lp_rast_end_query(struct lp_rasterizer_task
*task
,
494 const union lp_rast_cmd_arg arg
)
496 struct llvmpipe_query
*pq
= arg
.query_obj
;
497 assert(task
->query
[pq
->type
] == pq
|| pq
->type
== PIPE_QUERY_TIMESTAMP
);
500 case PIPE_QUERY_OCCLUSION_COUNTER
:
501 pq
->count
[task
->thread_index
] += task
->thread_data
.vis_counter
;
503 case PIPE_QUERY_TIMESTAMP
:
504 pq
->count
[task
->thread_index
] = os_time_get_nano();
506 case PIPE_QUERY_PRIMITIVES_GENERATED
:
507 case PIPE_QUERY_PRIMITIVES_EMITTED
:
508 case PIPE_QUERY_SO_STATISTICS
:
509 case PIPE_QUERY_PIPELINE_STATISTICS
:
510 case PIPE_QUERY_SO_OVERFLOW_PREDICATE
:
517 if (task
->query
[pq
->type
] == pq
) {
518 task
->query
[pq
->type
] = NULL
;
524 lp_rast_set_state(struct lp_rasterizer_task
*task
,
525 const union lp_rast_cmd_arg arg
)
527 task
->state
= arg
.state
;
533 * Called when we're done writing to a color tile.
536 lp_rast_tile_end(struct lp_rasterizer_task
*task
)
540 for (i
= 0; i
< PIPE_QUERY_TYPES
; ++i
) {
541 if (task
->query
[i
]) {
542 lp_rast_end_query(task
, lp_rast_arg_query(task
->query
[i
]));
547 memset(task
->color_tiles
, 0, sizeof(task
->color_tiles
));
548 task
->depth_tile
= NULL
;
553 static lp_rast_cmd_func dispatch
[LP_RAST_OP_MAX
] =
556 lp_rast_clear_zstencil
,
565 lp_rast_triangle_3_4
,
566 lp_rast_triangle_3_16
,
567 lp_rast_triangle_4_16
,
569 lp_rast_shade_tile_opaque
,
577 do_rasterize_bin(struct lp_rasterizer_task
*task
,
578 const struct cmd_bin
*bin
)
580 const struct cmd_block
*block
;
586 for (block
= bin
->head
; block
; block
= block
->next
) {
587 for (k
= 0; k
< block
->count
; k
++) {
588 dispatch
[block
->cmd
[k
]]( task
, block
->arg
[k
] );
596 * Rasterize commands for a single bin.
597 * \param x, y position of the bin's tile in the framebuffer
598 * Must be called between lp_rast_begin() and lp_rast_end().
602 rasterize_bin(struct lp_rasterizer_task
*task
,
603 const struct cmd_bin
*bin
)
605 lp_rast_tile_begin( task
, bin
);
607 do_rasterize_bin(task
, bin
);
609 lp_rast_tile_end(task
);
614 if (bin
->head
->count
== 1) {
615 if (bin
->head
->cmd
[0] == LP_RAST_OP_SHADE_TILE_OPAQUE
)
616 LP_COUNT(nr_pure_shade_opaque_64
);
617 else if (bin
->head
->cmd
[0] == LP_RAST_OP_SHADE_TILE
)
618 LP_COUNT(nr_pure_shade_64
);
623 /* An empty bin is one that just loads the contents of the tile and
624 * stores them again unchanged. This typically happens when bins have
625 * been flushed for some reason in the middle of a frame, or when
626 * incremental updates are being made to a render target.
628 * Try to avoid doing pointless work in this case.
631 is_empty_bin( const struct cmd_bin
*bin
)
633 return bin
->head
== NULL
;
638 * Rasterize/execute all bins within a scene.
642 rasterize_scene(struct lp_rasterizer_task
*task
,
643 struct lp_scene
*scene
)
647 if (!task
->rast
->no_rast
&& !scene
->discard
) {
648 /* loop over scene bins, rasterize each */
652 for (i
= 0; i
< scene
->tiles_x
; i
++) {
653 for (j
= 0; j
< scene
->tiles_y
; j
++) {
654 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, i
, j
);
655 rasterize_bin(task
, bin
, i
, j
);
664 while ((bin
= lp_scene_bin_iter_next(scene
))) {
665 if (!is_empty_bin( bin
))
666 rasterize_bin(task
, bin
);
674 lp_fence_signal(scene
->fence
);
682 * Called by setup module when it has something for us to render.
685 lp_rast_queue_scene( struct lp_rasterizer
*rast
,
686 struct lp_scene
*scene
)
688 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
690 if (rast
->num_threads
== 0) {
693 lp_rast_begin( rast
, scene
);
695 rasterize_scene( &rast
->tasks
[0], scene
);
699 rast
->curr_scene
= NULL
;
702 /* threaded rendering! */
705 lp_scene_enqueue( rast
->full_scenes
, scene
);
707 /* signal the threads that there's work to do */
708 for (i
= 0; i
< rast
->num_threads
; i
++) {
709 pipe_semaphore_signal(&rast
->tasks
[i
].work_ready
);
713 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
718 lp_rast_finish( struct lp_rasterizer
*rast
)
720 if (rast
->num_threads
== 0) {
726 /* wait for work to complete */
727 for (i
= 0; i
< rast
->num_threads
; i
++) {
728 pipe_semaphore_wait(&rast
->tasks
[i
].work_done
);
735 * This is the thread's main entrypoint.
736 * It's a simple loop:
739 * 3. signal that we're done
741 static PIPE_THREAD_ROUTINE( thread_function
, init_data
)
743 struct lp_rasterizer_task
*task
= (struct lp_rasterizer_task
*) init_data
;
744 struct lp_rasterizer
*rast
= task
->rast
;
745 boolean debug
= false;
750 debug_printf("thread %d waiting for work\n", task
->thread_index
);
751 pipe_semaphore_wait(&task
->work_ready
);
756 if (task
->thread_index
== 0) {
758 * - get next scene to rasterize
759 * - map the framebuffer surfaces
762 lp_scene_dequeue( rast
->full_scenes
, TRUE
) );
765 /* Wait for all threads to get here so that threads[1+] don't
766 * get a null rast->curr_scene pointer.
768 pipe_barrier_wait( &rast
->barrier
);
772 debug_printf("thread %d doing work\n", task
->thread_index
);
774 rasterize_scene(task
,
777 /* wait for all threads to finish with this scene */
778 pipe_barrier_wait( &rast
->barrier
);
780 /* XXX: shouldn't be necessary:
782 if (task
->thread_index
== 0) {
786 /* signal done with work */
788 debug_printf("thread %d done working\n", task
->thread_index
);
790 pipe_semaphore_signal(&task
->work_done
);
798 * Initialize semaphores and spawn the threads.
801 create_rast_threads(struct lp_rasterizer
*rast
)
805 /* NOTE: if num_threads is zero, we won't use any threads */
806 for (i
= 0; i
< rast
->num_threads
; i
++) {
807 pipe_semaphore_init(&rast
->tasks
[i
].work_ready
, 0);
808 pipe_semaphore_init(&rast
->tasks
[i
].work_done
, 0);
809 rast
->threads
[i
] = pipe_thread_create(thread_function
,
810 (void *) &rast
->tasks
[i
]);
817 * Create new lp_rasterizer. If num_threads is zero, don't create any
818 * new threads, do rendering synchronously.
819 * \param num_threads number of rasterizer threads to create
821 struct lp_rasterizer
*
822 lp_rast_create( unsigned num_threads
)
824 struct lp_rasterizer
*rast
;
827 rast
= CALLOC_STRUCT(lp_rasterizer
);
832 rast
->full_scenes
= lp_scene_queue_create();
833 if (!rast
->full_scenes
) {
837 for (i
= 0; i
< Elements(rast
->tasks
); i
++) {
838 struct lp_rasterizer_task
*task
= &rast
->tasks
[i
];
840 task
->thread_index
= i
;
843 rast
->num_threads
= num_threads
;
845 rast
->no_rast
= debug_get_bool_option("LP_NO_RAST", FALSE
);
847 create_rast_threads(rast
);
849 /* for synchronizing rasterization threads */
850 pipe_barrier_init( &rast
->barrier
, rast
->num_threads
);
852 memset(lp_dummy_tile
, 0, sizeof lp_dummy_tile
);
865 void lp_rast_destroy( struct lp_rasterizer
*rast
)
869 /* Set exit_flag and signal each thread's work_ready semaphore.
870 * Each thread will be woken up, notice that the exit_flag is set and
871 * break out of its main loop. The thread will then exit.
873 rast
->exit_flag
= TRUE
;
874 for (i
= 0; i
< rast
->num_threads
; i
++) {
875 pipe_semaphore_signal(&rast
->tasks
[i
].work_ready
);
878 /* Wait for threads to terminate before cleaning up per-thread data */
879 for (i
= 0; i
< rast
->num_threads
; i
++) {
880 pipe_thread_wait(rast
->threads
[i
]);
883 /* Clean up per-thread data */
884 for (i
= 0; i
< rast
->num_threads
; i
++) {
885 pipe_semaphore_destroy(&rast
->tasks
[i
].work_ready
);
886 pipe_semaphore_destroy(&rast
->tasks
[i
].work_done
);
889 /* for synchronizing rasterization threads */
890 pipe_barrier_destroy( &rast
->barrier
);
892 lp_scene_queue_destroy(rast
->full_scenes
);
898 /** Return number of rasterization threads */
900 lp_rast_get_num_threads( struct lp_rasterizer
*rast
)
902 return rast
->num_threads
;