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"
38 #include "lp_context.h"
44 #include "lp_rast_priv.h"
45 #include "gallivm/lp_bld_debug.h"
47 #include "lp_tex_sample.h"
52 const struct lp_rast_state
*jit_state
= NULL
;
53 const struct lp_rasterizer_task
*jit_task
= NULL
;
58 * Begin rasterizing a scene.
59 * Called once per scene by one thread.
62 lp_rast_begin( struct lp_rasterizer
*rast
,
63 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
,
93 LP_DBG(DEBUG_RAST
, "%s %d,%d\n", __FUNCTION__
, x
, y
);
96 task
->x
= x
* TILE_SIZE
;
97 task
->y
= y
* TILE_SIZE
;
98 task
->width
= TILE_SIZE
+ x
* TILE_SIZE
> task
->scene
->fb
.width
?
99 task
->scene
->fb
.width
- x
* TILE_SIZE
: TILE_SIZE
;
100 task
->height
= TILE_SIZE
+ y
* TILE_SIZE
> task
->scene
->fb
.height
?
101 task
->scene
->fb
.height
- y
* TILE_SIZE
: TILE_SIZE
;
103 task
->thread_data
.vis_counter
= 0;
104 task
->ps_invocations
= 0;
106 /* reset pointers to color and depth tile(s) */
107 memset(task
->color_tiles
, 0, sizeof(task
->color_tiles
));
108 task
->depth_tile
= NULL
;
113 * Clear the rasterizer's current color tile.
114 * This is a bin command called during bin processing.
115 * Clear commands always clear all bound layers.
118 lp_rast_clear_color(struct lp_rasterizer_task
*task
,
119 const union lp_rast_cmd_arg arg
)
121 const struct lp_scene
*scene
= task
->scene
;
122 unsigned cbuf
= arg
.clear_rb
->cbuf
;
124 enum pipe_format format
;
126 /* we never bin clear commands for non-existing buffers */
127 assert(cbuf
< scene
->fb
.nr_cbufs
);
128 assert(scene
->fb
.cbufs
[cbuf
]);
130 format
= scene
->fb
.cbufs
[cbuf
]->format
;
131 uc
= arg
.clear_rb
->color_val
;
134 * this is pretty rough since we have target format (bunch of bytes...) here.
135 * dump it as raw 4 dwords.
137 LP_DBG(DEBUG_RAST
, "%s clear value (target format %d) raw 0x%x,0x%x,0x%x,0x%x\n",
138 __FUNCTION__
, format
, uc
.ui
[0], uc
.ui
[1], uc
.ui
[2], uc
.ui
[3]);
141 util_fill_box(scene
->cbufs
[cbuf
].map
,
143 scene
->cbufs
[cbuf
].stride
,
144 scene
->cbufs
[cbuf
].layer_stride
,
150 scene
->fb_max_layer
+ 1,
153 /* this will increase for each rb which probably doesn't mean much */
154 LP_COUNT(nr_color_tile_clear
);
159 * Clear the rasterizer's current z/stencil tile.
160 * This is a bin command called during bin processing.
161 * Clear commands always clear all bound layers.
164 lp_rast_clear_zstencil(struct lp_rasterizer_task
*task
,
165 const union lp_rast_cmd_arg arg
)
167 const struct lp_scene
*scene
= task
->scene
;
168 uint64_t clear_value64
= arg
.clear_zstencil
.value
;
169 uint64_t clear_mask64
= arg
.clear_zstencil
.mask
;
170 uint32_t clear_value
= (uint32_t) clear_value64
;
171 uint32_t clear_mask
= (uint32_t) clear_mask64
;
172 const unsigned height
= task
->height
;
173 const unsigned width
= task
->width
;
174 const unsigned dst_stride
= scene
->zsbuf
.stride
;
179 LP_DBG(DEBUG_RAST
, "%s: value=0x%08x, mask=0x%08x\n",
180 __FUNCTION__
, clear_value
, clear_mask
);
183 * Clear the area of the depth/depth buffer matching this tile.
186 if (scene
->fb
.zsbuf
) {
188 uint8_t *dst_layer
= lp_rast_get_depth_tile_pointer(task
, LP_TEX_USAGE_READ_WRITE
);
189 block_size
= util_format_get_blocksize(scene
->fb
.zsbuf
->format
);
191 clear_value
&= clear_mask
;
193 for (layer
= 0; layer
<= scene
->fb_max_layer
; layer
++) {
196 switch (block_size
) {
198 assert(clear_mask
== 0xff);
199 memset(dst
, (uint8_t) clear_value
, height
* width
);
202 if (clear_mask
== 0xffff) {
203 for (i
= 0; i
< height
; i
++) {
204 uint16_t *row
= (uint16_t *)dst
;
205 for (j
= 0; j
< width
; j
++)
206 *row
++ = (uint16_t) clear_value
;
211 for (i
= 0; i
< height
; i
++) {
212 uint16_t *row
= (uint16_t *)dst
;
213 for (j
= 0; j
< width
; j
++) {
214 uint16_t tmp
= ~clear_mask
& *row
;
215 *row
++ = clear_value
| tmp
;
222 if (clear_mask
== 0xffffffff) {
223 for (i
= 0; i
< height
; i
++) {
224 uint32_t *row
= (uint32_t *)dst
;
225 for (j
= 0; j
< width
; j
++)
226 *row
++ = clear_value
;
231 for (i
= 0; i
< height
; i
++) {
232 uint32_t *row
= (uint32_t *)dst
;
233 for (j
= 0; j
< width
; j
++) {
234 uint32_t tmp
= ~clear_mask
& *row
;
235 *row
++ = clear_value
| tmp
;
242 clear_value64
&= clear_mask64
;
243 if (clear_mask64
== 0xffffffffffULL
) {
244 for (i
= 0; i
< height
; i
++) {
245 uint64_t *row
= (uint64_t *)dst
;
246 for (j
= 0; j
< width
; j
++)
247 *row
++ = clear_value64
;
252 for (i
= 0; i
< height
; i
++) {
253 uint64_t *row
= (uint64_t *)dst
;
254 for (j
= 0; j
< width
; j
++) {
255 uint64_t tmp
= ~clear_mask64
& *row
;
256 *row
++ = clear_value64
| tmp
;
267 dst_layer
+= scene
->zsbuf
.layer_stride
;
275 * Run the shader on all blocks in a tile. This is used when a tile is
276 * completely contained inside a triangle.
277 * This is a bin command called during bin processing.
280 lp_rast_shade_tile(struct lp_rasterizer_task
*task
,
281 const union lp_rast_cmd_arg arg
)
283 const struct lp_scene
*scene
= task
->scene
;
284 const struct lp_rast_shader_inputs
*inputs
= arg
.shade_tile
;
285 const struct lp_rast_state
*state
;
286 struct lp_fragment_shader_variant
*variant
;
287 const unsigned tile_x
= task
->x
, tile_y
= task
->y
;
290 if (inputs
->disable
) {
291 /* This command was partially binned and has been disabled */
295 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
302 variant
= state
->variant
;
304 /* render the whole 64x64 tile in 4x4 chunks */
305 for (y
= 0; y
< task
->height
; y
+= 4){
306 for (x
= 0; x
< task
->width
; x
+= 4) {
307 uint8_t *color
[PIPE_MAX_COLOR_BUFS
];
308 unsigned stride
[PIPE_MAX_COLOR_BUFS
];
309 uint8_t *depth
= NULL
;
310 unsigned depth_stride
= 0;
314 for (i
= 0; i
< scene
->fb
.nr_cbufs
; i
++){
315 if (scene
->fb
.cbufs
[i
]) {
316 stride
[i
] = scene
->cbufs
[i
].stride
;
317 color
[i
] = lp_rast_get_color_block_pointer(task
, i
, tile_x
+ x
,
318 tile_y
+ y
, inputs
->layer
);
327 if (scene
->zsbuf
.map
) {
328 depth
= lp_rast_get_depth_block_pointer(task
, tile_x
+ x
,
329 tile_y
+ y
, inputs
->layer
);
330 depth_stride
= scene
->zsbuf
.stride
;
333 /* Propagate non-interpolated raster state. */
334 task
->thread_data
.raster_state
.viewport_index
= inputs
->viewport_index
;
336 /* run shader on 4x4 block */
337 BEGIN_JIT_CALL(state
, task
);
338 variant
->jit_function
[RAST_WHOLE
]( &state
->jit_context
,
339 tile_x
+ x
, tile_y
+ y
,
357 * Run the shader on all blocks in a tile. This is used when a tile is
358 * completely contained inside a triangle, and the shader is opaque.
359 * This is a bin command called during bin processing.
362 lp_rast_shade_tile_opaque(struct lp_rasterizer_task
*task
,
363 const union lp_rast_cmd_arg arg
)
365 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
372 lp_rast_shade_tile(task
, arg
);
377 * Compute shading for a 4x4 block of pixels inside a triangle.
378 * This is a bin command called during bin processing.
379 * \param x X position of quad in window coords
380 * \param y Y position of quad in window coords
383 lp_rast_shade_quads_mask(struct lp_rasterizer_task
*task
,
384 const struct lp_rast_shader_inputs
*inputs
,
385 unsigned x
, unsigned y
,
388 const struct lp_rast_state
*state
= task
->state
;
389 struct lp_fragment_shader_variant
*variant
= state
->variant
;
390 const struct lp_scene
*scene
= task
->scene
;
391 uint8_t *color
[PIPE_MAX_COLOR_BUFS
];
392 unsigned stride
[PIPE_MAX_COLOR_BUFS
];
393 uint8_t *depth
= NULL
;
394 unsigned depth_stride
= 0;
400 assert(x
< scene
->tiles_x
* TILE_SIZE
);
401 assert(y
< scene
->tiles_y
* TILE_SIZE
);
402 assert(x
% TILE_VECTOR_WIDTH
== 0);
403 assert(y
% TILE_VECTOR_HEIGHT
== 0);
405 assert((x
% 4) == 0);
406 assert((y
% 4) == 0);
409 for (i
= 0; i
< scene
->fb
.nr_cbufs
; i
++) {
410 if (scene
->fb
.cbufs
[i
]) {
411 stride
[i
] = scene
->cbufs
[i
].stride
;
412 color
[i
] = lp_rast_get_color_block_pointer(task
, i
, x
, y
,
422 if (scene
->zsbuf
.map
) {
423 depth_stride
= scene
->zsbuf
.stride
;
424 depth
= lp_rast_get_depth_block_pointer(task
, x
, y
, inputs
->layer
);
427 assert(lp_check_alignment(state
->jit_context
.u8_blend_color
, 16));
430 * The rasterizer may produce fragments outside our
431 * allocated 4x4 blocks hence need to filter them out here.
433 if ((x
% TILE_SIZE
) < task
->width
&& (y
% TILE_SIZE
) < task
->height
) {
434 /* not very accurate would need a popcount on the mask */
435 /* always count this not worth bothering? */
436 task
->ps_invocations
+= 1 * variant
->ps_inv_multiplier
;
438 /* Propagate non-interpolated raster state. */
439 task
->thread_data
.raster_state
.viewport_index
= inputs
->viewport_index
;
441 /* run shader on 4x4 block */
442 BEGIN_JIT_CALL(state
, task
);
443 variant
->jit_function
[RAST_EDGE_TEST
](&state
->jit_context
,
462 * Begin a new occlusion query.
463 * This is a bin command put in all bins.
467 lp_rast_begin_query(struct lp_rasterizer_task
*task
,
468 const union lp_rast_cmd_arg arg
)
470 struct llvmpipe_query
*pq
= arg
.query_obj
;
473 case PIPE_QUERY_OCCLUSION_COUNTER
:
474 case PIPE_QUERY_OCCLUSION_PREDICATE
:
475 pq
->start
[task
->thread_index
] = task
->thread_data
.vis_counter
;
477 case PIPE_QUERY_PIPELINE_STATISTICS
:
478 pq
->start
[task
->thread_index
] = task
->ps_invocations
;
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
;
499 case PIPE_QUERY_OCCLUSION_COUNTER
:
500 case PIPE_QUERY_OCCLUSION_PREDICATE
:
501 pq
->end
[task
->thread_index
] +=
502 task
->thread_data
.vis_counter
- pq
->start
[task
->thread_index
];
503 pq
->start
[task
->thread_index
] = 0;
505 case PIPE_QUERY_TIMESTAMP
:
506 pq
->end
[task
->thread_index
] = os_time_get_nano();
508 case PIPE_QUERY_PIPELINE_STATISTICS
:
509 pq
->end
[task
->thread_index
] +=
510 task
->ps_invocations
- pq
->start
[task
->thread_index
];
511 pq
->start
[task
->thread_index
] = 0;
521 lp_rast_set_state(struct lp_rasterizer_task
*task
,
522 const union lp_rast_cmd_arg arg
)
524 task
->state
= arg
.state
;
530 * Called when we're done writing to a color tile.
533 lp_rast_tile_end(struct lp_rasterizer_task
*task
)
537 for (i
= 0; i
< task
->scene
->num_active_queries
; ++i
) {
538 lp_rast_end_query(task
, lp_rast_arg_query(task
->scene
->active_queries
[i
]));
542 memset(task
->color_tiles
, 0, sizeof(task
->color_tiles
));
543 task
->depth_tile
= NULL
;
548 static lp_rast_cmd_func dispatch
[LP_RAST_OP_MAX
] =
551 lp_rast_clear_zstencil
,
560 lp_rast_triangle_3_4
,
561 lp_rast_triangle_3_16
,
562 lp_rast_triangle_4_16
,
564 lp_rast_shade_tile_opaque
,
568 lp_rast_triangle_32_1
,
569 lp_rast_triangle_32_2
,
570 lp_rast_triangle_32_3
,
571 lp_rast_triangle_32_4
,
572 lp_rast_triangle_32_5
,
573 lp_rast_triangle_32_6
,
574 lp_rast_triangle_32_7
,
575 lp_rast_triangle_32_8
,
576 lp_rast_triangle_32_3_4
,
577 lp_rast_triangle_32_3_16
,
578 lp_rast_triangle_32_4_16
583 do_rasterize_bin(struct lp_rasterizer_task
*task
,
584 const struct cmd_bin
*bin
,
587 const struct cmd_block
*block
;
591 lp_debug_bin(bin
, x
, y
);
593 for (block
= bin
->head
; block
; block
= block
->next
) {
594 for (k
= 0; k
< block
->count
; k
++) {
595 dispatch
[block
->cmd
[k
]]( task
, block
->arg
[k
] );
603 * Rasterize commands for a single bin.
604 * \param x, y position of the bin's tile in the framebuffer
605 * Must be called between lp_rast_begin() and lp_rast_end().
609 rasterize_bin(struct lp_rasterizer_task
*task
,
610 const struct cmd_bin
*bin
, int x
, int y
)
612 lp_rast_tile_begin( task
, bin
, x
, y
);
614 do_rasterize_bin(task
, bin
, x
, y
);
616 lp_rast_tile_end(task
);
621 if (bin
->head
->count
== 1) {
622 if (bin
->head
->cmd
[0] == LP_RAST_OP_SHADE_TILE_OPAQUE
)
623 LP_COUNT(nr_pure_shade_opaque_64
);
624 else if (bin
->head
->cmd
[0] == LP_RAST_OP_SHADE_TILE
)
625 LP_COUNT(nr_pure_shade_64
);
630 /* An empty bin is one that just loads the contents of the tile and
631 * stores them again unchanged. This typically happens when bins have
632 * been flushed for some reason in the middle of a frame, or when
633 * incremental updates are being made to a render target.
635 * Try to avoid doing pointless work in this case.
638 is_empty_bin( const struct cmd_bin
*bin
)
640 return bin
->head
== NULL
;
645 * Rasterize/execute all bins within a scene.
649 rasterize_scene(struct lp_rasterizer_task
*task
,
650 struct lp_scene
*scene
)
654 if (!task
->rast
->no_rast
&& !scene
->discard
) {
655 /* loop over scene bins, rasterize each */
661 while ((bin
= lp_scene_bin_iter_next(scene
, &i
, &j
))) {
662 if (!is_empty_bin( bin
))
663 rasterize_bin(task
, bin
, i
, j
);
670 lp_fence_signal(scene
->fence
);
678 * Called by setup module when it has something for us to render.
681 lp_rast_queue_scene( struct lp_rasterizer
*rast
,
682 struct lp_scene
*scene
)
684 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
686 if (rast
->num_threads
== 0) {
688 unsigned fpstate
= util_fpstate_get();
690 /* Make sure that denorms are treated like zeros. This is
691 * the behavior required by D3D10. OpenGL doesn't care.
693 util_fpstate_set_denorms_to_zero(fpstate
);
695 lp_rast_begin( rast
, scene
);
697 rasterize_scene( &rast
->tasks
[0], scene
);
701 util_fpstate_set(fpstate
);
703 rast
->curr_scene
= NULL
;
706 /* threaded rendering! */
709 lp_scene_enqueue( rast
->full_scenes
, scene
);
711 /* signal the threads that there's work to do */
712 for (i
= 0; i
< rast
->num_threads
; i
++) {
713 pipe_semaphore_signal(&rast
->tasks
[i
].work_ready
);
717 LP_DBG(DEBUG_SETUP
, "%s done \n", __FUNCTION__
);
722 lp_rast_finish( struct lp_rasterizer
*rast
)
724 if (rast
->num_threads
== 0) {
730 /* wait for work to complete */
731 for (i
= 0; i
< rast
->num_threads
; i
++) {
732 pipe_semaphore_wait(&rast
->tasks
[i
].work_done
);
739 * This is the thread's main entrypoint.
740 * It's a simple loop:
743 * 3. signal that we're done
745 static PIPE_THREAD_ROUTINE( thread_function
, init_data
)
747 struct lp_rasterizer_task
*task
= (struct lp_rasterizer_task
*) init_data
;
748 struct lp_rasterizer
*rast
= task
->rast
;
749 boolean debug
= false;
750 unsigned fpstate
= util_fpstate_get();
752 /* Make sure that denorms are treated like zeros. This is
753 * the behavior required by D3D10. OpenGL doesn't care.
755 util_fpstate_set_denorms_to_zero(fpstate
);
760 debug_printf("thread %d waiting for work\n", task
->thread_index
);
761 pipe_semaphore_wait(&task
->work_ready
);
766 if (task
->thread_index
== 0) {
768 * - get next scene to rasterize
769 * - map the framebuffer surfaces
772 lp_scene_dequeue( rast
->full_scenes
, TRUE
) );
775 /* Wait for all threads to get here so that threads[1+] don't
776 * get a null rast->curr_scene pointer.
778 pipe_barrier_wait( &rast
->barrier
);
782 debug_printf("thread %d doing work\n", task
->thread_index
);
784 rasterize_scene(task
,
787 /* wait for all threads to finish with this scene */
788 pipe_barrier_wait( &rast
->barrier
);
790 /* XXX: shouldn't be necessary:
792 if (task
->thread_index
== 0) {
796 /* signal done with work */
798 debug_printf("thread %d done working\n", task
->thread_index
);
800 pipe_semaphore_signal(&task
->work_done
);
808 * Initialize semaphores and spawn the threads.
811 create_rast_threads(struct lp_rasterizer
*rast
)
815 /* NOTE: if num_threads is zero, we won't use any threads */
816 for (i
= 0; i
< rast
->num_threads
; i
++) {
817 pipe_semaphore_init(&rast
->tasks
[i
].work_ready
, 0);
818 pipe_semaphore_init(&rast
->tasks
[i
].work_done
, 0);
819 rast
->threads
[i
] = pipe_thread_create(thread_function
,
820 (void *) &rast
->tasks
[i
]);
827 * Create new lp_rasterizer. If num_threads is zero, don't create any
828 * new threads, do rendering synchronously.
829 * \param num_threads number of rasterizer threads to create
831 struct lp_rasterizer
*
832 lp_rast_create( unsigned num_threads
)
834 struct lp_rasterizer
*rast
;
837 rast
= CALLOC_STRUCT(lp_rasterizer
);
842 rast
->full_scenes
= lp_scene_queue_create();
843 if (!rast
->full_scenes
) {
847 for (i
= 0; i
< Elements(rast
->tasks
); i
++) {
848 struct lp_rasterizer_task
*task
= &rast
->tasks
[i
];
850 task
->thread_index
= i
;
853 rast
->num_threads
= num_threads
;
855 rast
->no_rast
= debug_get_bool_option("LP_NO_RAST", FALSE
);
857 create_rast_threads(rast
);
859 /* for synchronizing rasterization threads */
860 pipe_barrier_init( &rast
->barrier
, rast
->num_threads
);
862 memset(lp_dummy_tile
, 0, sizeof lp_dummy_tile
);
875 void lp_rast_destroy( struct lp_rasterizer
*rast
)
879 /* Set exit_flag and signal each thread's work_ready semaphore.
880 * Each thread will be woken up, notice that the exit_flag is set and
881 * break out of its main loop. The thread will then exit.
883 rast
->exit_flag
= TRUE
;
884 for (i
= 0; i
< rast
->num_threads
; i
++) {
885 pipe_semaphore_signal(&rast
->tasks
[i
].work_ready
);
888 /* Wait for threads to terminate before cleaning up per-thread data */
889 for (i
= 0; i
< rast
->num_threads
; i
++) {
890 pipe_thread_wait(rast
->threads
[i
]);
893 /* Clean up per-thread data */
894 for (i
= 0; i
< rast
->num_threads
; i
++) {
895 pipe_semaphore_destroy(&rast
->tasks
[i
].work_ready
);
896 pipe_semaphore_destroy(&rast
->tasks
[i
].work_done
);
899 /* for synchronizing rasterization threads */
900 pipe_barrier_destroy( &rast
->barrier
);
902 lp_scene_queue_destroy(rast
->full_scenes
);