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 **************************************************************************/
28 #include "util/u_math.h"
29 #include "util/u_memory.h"
30 #include "util/u_inlines.h"
31 #include "util/u_simple_list.h"
32 #include "util/u_surface.h"
34 #include "lp_scene_queue.h"
39 lp_scene_create( struct pipe_context
*pipe
,
40 struct lp_scene_queue
*queue
)
43 struct lp_scene
*scene
= CALLOC_STRUCT(lp_scene
);
48 scene
->empty_queue
= queue
;
50 for (i
= 0; i
< TILES_X
; i
++) {
51 for (j
= 0; j
< TILES_Y
; j
++) {
52 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, i
, j
);
53 bin
->commands
.head
= bin
->commands
.tail
= CALLOC_STRUCT(cmd_block
);
58 scene
->data
.tail
= CALLOC_STRUCT(data_block
);
60 make_empty_list(&scene
->textures
);
62 pipe_mutex_init(scene
->mutex
);
69 * Free all data associated with the given scene, and free(scene).
72 lp_scene_destroy(struct lp_scene
*scene
)
76 lp_scene_reset(scene
);
78 for (i
= 0; i
< TILES_X
; i
++)
79 for (j
= 0; j
< TILES_Y
; j
++) {
80 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, i
, j
);
81 assert(bin
->commands
.head
== bin
->commands
.tail
);
82 FREE(bin
->commands
.head
);
83 bin
->commands
.head
= NULL
;
84 bin
->commands
.tail
= NULL
;
87 FREE(scene
->data
.head
);
88 scene
->data
.head
= NULL
;
90 pipe_mutex_destroy(scene
->mutex
);
97 * Check if the scene's bins are all empty.
98 * For debugging purposes.
101 lp_scene_is_empty(struct lp_scene
*scene
)
105 for (y
= 0; y
< TILES_Y
; y
++) {
106 for (x
= 0; x
< TILES_X
; x
++) {
107 const struct cmd_bin
*bin
= lp_scene_get_bin(scene
, x
, y
);
108 const struct cmd_block_list
*list
= &bin
->commands
;
109 if (list
->head
!= list
->tail
|| list
->head
->count
> 0) {
118 /* Free data for one particular bin. May be called from the
119 * rasterizer thread(s).
122 lp_scene_bin_reset(struct lp_scene
*scene
, unsigned x
, unsigned y
)
124 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, x
, y
);
125 struct cmd_block_list
*list
= &bin
->commands
;
126 struct cmd_block
*block
;
127 struct cmd_block
*tmp
;
132 for (block
= list
->head
; block
!= list
->tail
; block
= tmp
) {
137 assert(list
->tail
->next
== NULL
);
138 list
->head
= list
->tail
;
139 list
->head
->count
= 0;
144 * Free all the temporary data in a scene. May be called from the
145 * rasterizer thread(s).
148 lp_scene_reset(struct lp_scene
*scene
)
152 /* Free all but last binner command lists:
154 for (i
= 0; i
< scene
->tiles_x
; i
++) {
155 for (j
= 0; j
< scene
->tiles_y
; j
++) {
156 lp_scene_bin_reset(scene
, i
, j
);
160 assert(lp_scene_is_empty(scene
));
162 /* Free all but last binned data block:
165 struct data_block_list
*list
= &scene
->data
;
166 struct data_block
*block
, *tmp
;
168 for (block
= list
->head
; block
!= list
->tail
; block
= tmp
) {
173 assert(list
->tail
->next
== NULL
);
174 list
->head
= list
->tail
;
175 list
->head
->used
= 0;
178 /* Release texture refs
181 struct texture_ref
*ref
, *next
, *ref_list
= &scene
->textures
;
182 for (ref
= ref_list
->next
; ref
!= ref_list
; ref
= next
) {
183 next
= next_elem(ref
);
184 pipe_texture_reference(&ref
->texture
, NULL
);
187 make_empty_list(ref_list
);
197 lp_bin_new_cmd_block( struct cmd_block_list
*list
)
199 struct cmd_block
*block
= MALLOC_STRUCT(cmd_block
);
200 list
->tail
->next
= block
;
208 lp_bin_new_data_block( struct data_block_list
*list
)
210 struct data_block
*block
= MALLOC_STRUCT(data_block
);
211 list
->tail
->next
= block
;
218 /** Return number of bytes used for all bin data within a scene */
220 lp_scene_data_size( const struct lp_scene
*scene
)
223 const struct data_block
*block
;
224 for (block
= scene
->data
.head
; block
; block
= block
->next
) {
231 /** Return number of bytes used for a single bin */
233 lp_scene_bin_size( const struct lp_scene
*scene
, unsigned x
, unsigned y
)
235 struct cmd_bin
*bin
= lp_scene_get_bin((struct lp_scene
*) scene
, x
, y
);
236 const struct cmd_block
*cmd
;
238 for (cmd
= bin
->commands
.head
; cmd
; cmd
= cmd
->next
) {
239 size
+= (cmd
->count
*
240 (sizeof(lp_rast_cmd
) + sizeof(union lp_rast_cmd_arg
)));
247 * Add a reference to a texture by the scene.
250 lp_scene_texture_reference( struct lp_scene
*scene
,
251 struct pipe_texture
*texture
)
253 struct texture_ref
*ref
= CALLOC_STRUCT(texture_ref
);
255 struct texture_ref
*ref_list
= &scene
->textures
;
256 pipe_texture_reference(&ref
->texture
, texture
);
257 insert_at_tail(ref_list
, ref
);
263 * Does this scene have a reference to the given texture?
266 lp_scene_is_texture_referenced( const struct lp_scene
*scene
,
267 const struct pipe_texture
*texture
)
269 const struct texture_ref
*ref_list
= &scene
->textures
;
270 const struct texture_ref
*ref
;
271 foreach (ref
, ref_list
) {
272 if (ref
->texture
== texture
)
280 * Return last command in the bin
283 lp_get_last_command( const struct cmd_bin
*bin
)
285 const struct cmd_block
*tail
= bin
->commands
.tail
;
286 const unsigned i
= tail
->count
;
288 return tail
->cmd
[i
- 1];
295 * Replace the arg of the last command in the bin.
298 lp_replace_last_command_arg( struct cmd_bin
*bin
,
299 const union lp_rast_cmd_arg arg
)
301 struct cmd_block
*tail
= bin
->commands
.tail
;
302 const unsigned i
= tail
->count
;
304 tail
->arg
[i
- 1] = arg
;
310 * Put a state-change command into all bins.
311 * If we find that the last command in a bin was also a state-change
312 * command, we can simply replace that one with the new one.
315 lp_scene_bin_state_command( struct lp_scene
*scene
,
317 const union lp_rast_cmd_arg arg
)
320 for (i
= 0; i
< scene
->tiles_x
; i
++) {
321 for (j
= 0; j
< scene
->tiles_y
; j
++) {
322 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, i
, j
);
323 lp_rast_cmd last_cmd
= lp_get_last_command(bin
);
324 if (last_cmd
== cmd
) {
325 lp_replace_last_command_arg(bin
, arg
);
328 lp_scene_bin_command( scene
, i
, j
, cmd
, arg
);
335 /** advance curr_x,y to the next bin */
337 next_bin(struct lp_scene
*scene
)
340 if (scene
->curr_x
>= scene
->tiles_x
) {
344 if (scene
->curr_y
>= scene
->tiles_y
) {
353 lp_scene_bin_iter_begin( struct lp_scene
*scene
)
355 scene
->curr_x
= scene
->curr_y
= -1;
360 * Return pointer to next bin to be rendered.
361 * The lp_scene::curr_x and ::curr_y fields will be advanced.
362 * Multiple rendering threads will call this function to get a chunk
363 * of work (a bin) to work on.
366 lp_scene_bin_iter_next( struct lp_scene
*scene
, int *bin_x
, int *bin_y
)
368 struct cmd_bin
*bin
= NULL
;
370 pipe_mutex_lock(scene
->mutex
);
372 if (scene
->curr_x
< 0) {
377 else if (!next_bin(scene
)) {
378 /* no more bins left */
382 bin
= lp_scene_get_bin(scene
, scene
->curr_x
, scene
->curr_y
);
383 *bin_x
= scene
->curr_x
;
384 *bin_y
= scene
->curr_y
;
387 /*printf("return bin %p at %d, %d\n", (void *) bin, *bin_x, *bin_y);*/
388 pipe_mutex_unlock(scene
->mutex
);
394 * Prepare this scene for the rasterizer.
395 * Map the framebuffer surfaces. Initialize the 'rast' state.
398 lp_scene_map_buffers( struct lp_scene
*scene
)
400 struct pipe_surface
*cbuf
, *zsbuf
;
403 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
406 /* Map all color buffers
408 for (i
= 0; i
< scene
->fb
.nr_cbufs
; i
++) {
409 cbuf
= scene
->fb
.cbufs
[i
];
411 scene
->cbuf_map
[i
] = llvmpipe_texture_map(cbuf
->texture
,
415 if (!scene
->cbuf_map
[i
])
422 zsbuf
= scene
->fb
.zsbuf
;
424 scene
->zsbuf_map
= llvmpipe_texture_map(zsbuf
->texture
,
428 if (!scene
->zsbuf_map
)
435 /* Unmap and release transfers?
443 * Called after rasterizer as finished rasterizing a scene.
445 * We want to call this from the pipe_context's current thread to
446 * avoid having to have mutexes on the transfer functions.
449 lp_scene_unmap_buffers( struct lp_scene
*scene
)
453 for (i
= 0; i
< scene
->fb
.nr_cbufs
; i
++) {
454 if (scene
->cbuf_map
[i
]) {
455 struct pipe_surface
*cbuf
= scene
->fb
.cbufs
[i
];
456 llvmpipe_texture_unmap(cbuf
->texture
,
460 scene
->cbuf_map
[i
] = NULL
;
464 if (scene
->zsbuf_map
) {
465 struct pipe_surface
*zsbuf
= scene
->fb
.zsbuf
;
466 llvmpipe_texture_unmap(zsbuf
->texture
,
470 scene
->zsbuf_map
= NULL
;
473 util_unreference_framebuffer_state( &scene
->fb
);
477 void lp_scene_begin_binning( struct lp_scene
*scene
,
478 struct pipe_framebuffer_state
*fb
)
480 assert(lp_scene_is_empty(scene
));
482 util_copy_framebuffer_state(&scene
->fb
, fb
);
484 scene
->tiles_x
= align(fb
->width
, TILE_SIZE
) / TILE_SIZE
;
485 scene
->tiles_y
= align(fb
->height
, TILE_SIZE
) / TILE_SIZE
;
489 void lp_scene_rasterize( struct lp_scene
*scene
,
490 struct lp_rasterizer
*rast
,
491 boolean write_depth
)
495 debug_printf("rasterize scene:\n");
496 debug_printf(" data size: %u\n", lp_scene_data_size(scene
));
497 for (y
= 0; y
< scene
->tiles_y
; y
++) {
498 for (x
= 0; x
< scene
->tiles_x
; x
++) {
499 debug_printf(" bin %u, %u size: %u\n", x
, y
,
500 lp_scene_bin_size(scene
, x
, y
));
506 scene
->write_depth
= (scene
->fb
.zsbuf
!= NULL
&&
509 lp_scene_map_buffers( scene
);
511 /* Enqueue the scene for rasterization, then immediately wait for
514 lp_rast_queue_scene( rast
, scene
);
516 /* Currently just wait for the rasterizer to finish. Some
517 * threading interactions need to be worked out, particularly once
518 * transfers become per-context:
520 lp_rast_finish( rast
);
521 lp_scene_unmap_buffers( scene
);
522 lp_scene_enqueue( scene
->empty_queue
, scene
);