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_framebuffer.h"
29 #include "util/u_math.h"
30 #include "util/u_memory.h"
31 #include "util/u_inlines.h"
32 #include "util/u_simple_list.h"
34 #include "lp_scene_queue.h"
38 /** List of texture references */
40 struct pipe_resource
*texture
;
41 struct texture_ref
*prev
, *next
; /**< linked list w/ u_simple_list.h */
47 * Create a new scene object.
48 * \param queue the queue to put newly rendered/emptied scenes into
51 lp_scene_create( struct pipe_context
*pipe
,
52 struct lp_scene_queue
*queue
)
55 struct lp_scene
*scene
= CALLOC_STRUCT(lp_scene
);
60 scene
->empty_queue
= queue
;
62 for (i
= 0; i
< TILES_X
; i
++) {
63 for (j
= 0; j
< TILES_Y
; j
++) {
64 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, i
, j
);
65 bin
->commands
.head
= bin
->commands
.tail
= CALLOC_STRUCT(cmd_block
);
70 scene
->data
.tail
= CALLOC_STRUCT(data_block
);
72 make_empty_list(&scene
->resources
);
74 pipe_mutex_init(scene
->mutex
);
81 * Free all data associated with the given scene, and the scene itself.
84 lp_scene_destroy(struct lp_scene
*scene
)
88 lp_scene_reset(scene
);
90 for (i
= 0; i
< TILES_X
; i
++)
91 for (j
= 0; j
< TILES_Y
; j
++) {
92 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, i
, j
);
93 assert(bin
->commands
.head
== bin
->commands
.tail
);
94 FREE(bin
->commands
.head
);
95 bin
->commands
.head
= NULL
;
96 bin
->commands
.tail
= NULL
;
99 FREE(scene
->data
.head
);
100 scene
->data
.head
= NULL
;
102 pipe_mutex_destroy(scene
->mutex
);
109 * Check if the scene's bins are all empty.
110 * For debugging purposes.
113 lp_scene_is_empty(struct lp_scene
*scene
)
117 for (y
= 0; y
< TILES_Y
; y
++) {
118 for (x
= 0; x
< TILES_X
; x
++) {
119 const struct cmd_bin
*bin
= lp_scene_get_bin(scene
, x
, y
);
120 const struct cmd_block_list
*list
= &bin
->commands
;
121 if (list
->head
!= list
->tail
|| list
->head
->count
> 0) {
130 /* Free data for one particular bin. May be called from the
131 * rasterizer thread(s).
134 lp_scene_bin_reset(struct lp_scene
*scene
, unsigned x
, unsigned y
)
136 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, x
, y
);
137 struct cmd_block_list
*list
= &bin
->commands
;
138 struct cmd_block
*block
;
139 struct cmd_block
*tmp
;
144 for (block
= list
->head
; block
!= list
->tail
; block
= tmp
) {
149 assert(list
->tail
->next
== NULL
);
150 list
->head
= list
->tail
;
151 list
->head
->count
= 0;
156 * Free all the temporary data in a scene. May be called from the
157 * rasterizer thread(s).
160 lp_scene_reset(struct lp_scene
*scene
)
164 /* Free all but last binner command lists:
166 for (i
= 0; i
< scene
->tiles_x
; i
++) {
167 for (j
= 0; j
< scene
->tiles_y
; j
++) {
168 lp_scene_bin_reset(scene
, i
, j
);
172 /* If there are any bins which weren't cleared by the loop above,
173 * they will be caught (on debug builds at least) by this assert:
175 assert(lp_scene_is_empty(scene
));
177 /* Free all but last binned data block:
180 struct data_block_list
*list
= &scene
->data
;
181 struct data_block
*block
, *tmp
;
183 for (block
= list
->head
; block
!= list
->tail
; block
= tmp
) {
188 assert(list
->tail
->next
== NULL
);
189 list
->head
= list
->tail
;
190 list
->head
->used
= 0;
193 /* Release texture refs
196 struct resource_ref
*ref
, *next
, *ref_list
= &scene
->resources
;
197 for (ref
= ref_list
->next
; ref
!= ref_list
; ref
= next
) {
198 next
= next_elem(ref
);
199 pipe_resource_reference(&ref
->resource
, NULL
);
202 make_empty_list(ref_list
);
205 lp_fence_reference(&scene
->fence
, NULL
);
207 scene
->scene_size
= 0;
209 scene
->has_depthstencil_clear
= FALSE
;
218 lp_bin_new_cmd_block( struct cmd_block_list
*list
)
220 struct cmd_block
*block
= MALLOC_STRUCT(cmd_block
);
222 list
->tail
->next
= block
;
232 lp_bin_new_data_block( struct data_block_list
*list
)
234 struct data_block
*block
= MALLOC_STRUCT(data_block
);
236 list
->tail
->next
= block
;
246 * Return number of bytes used for all bin data within a scene.
247 * This does not include resources (textures) referenced by the scene.
250 lp_scene_data_size( const struct lp_scene
*scene
)
253 const struct data_block
*block
;
254 for (block
= scene
->data
.head
; block
; block
= block
->next
) {
261 /** Return number of bytes used for a single bin */
263 lp_scene_bin_size( const struct lp_scene
*scene
, unsigned x
, unsigned y
)
265 struct cmd_bin
*bin
= lp_scene_get_bin((struct lp_scene
*) scene
, x
, y
);
266 const struct cmd_block
*cmd
;
268 for (cmd
= bin
->commands
.head
; cmd
; cmd
= cmd
->next
) {
269 size
+= (cmd
->count
*
270 (sizeof(lp_rast_cmd
) + sizeof(union lp_rast_cmd_arg
)));
277 * Add a reference to a resource by the scene.
280 lp_scene_add_resource_reference(struct lp_scene
*scene
,
281 struct pipe_resource
*resource
)
283 struct resource_ref
*ref
= CALLOC_STRUCT(resource_ref
);
285 struct resource_ref
*ref_list
= &scene
->resources
;
286 pipe_resource_reference(&ref
->resource
, resource
);
287 insert_at_tail(ref_list
, ref
);
290 scene
->scene_size
+= llvmpipe_resource_size(resource
);
295 * Does this scene have a reference to the given resource?
298 lp_scene_is_resource_referenced(const struct lp_scene
*scene
,
299 const struct pipe_resource
*resource
)
301 const struct resource_ref
*ref_list
= &scene
->resources
;
302 const struct resource_ref
*ref
;
303 foreach (ref
, ref_list
) {
304 if (ref
->resource
== resource
)
313 /** advance curr_x,y to the next bin */
315 next_bin(struct lp_scene
*scene
)
318 if (scene
->curr_x
>= scene
->tiles_x
) {
322 if (scene
->curr_y
>= scene
->tiles_y
) {
331 lp_scene_bin_iter_begin( struct lp_scene
*scene
)
333 scene
->curr_x
= scene
->curr_y
= -1;
338 * Return pointer to next bin to be rendered.
339 * The lp_scene::curr_x and ::curr_y fields will be advanced.
340 * Multiple rendering threads will call this function to get a chunk
341 * of work (a bin) to work on.
344 lp_scene_bin_iter_next( struct lp_scene
*scene
, int *bin_x
, int *bin_y
)
346 struct cmd_bin
*bin
= NULL
;
348 pipe_mutex_lock(scene
->mutex
);
350 if (scene
->curr_x
< 0) {
355 else if (!next_bin(scene
)) {
356 /* no more bins left */
360 bin
= lp_scene_get_bin(scene
, scene
->curr_x
, scene
->curr_y
);
361 *bin_x
= scene
->curr_x
;
362 *bin_y
= scene
->curr_y
;
365 /*printf("return bin %p at %d, %d\n", (void *) bin, *bin_x, *bin_y);*/
366 pipe_mutex_unlock(scene
->mutex
);
371 void lp_scene_begin_binning( struct lp_scene
*scene
,
372 struct pipe_framebuffer_state
*fb
)
374 assert(lp_scene_is_empty(scene
));
376 util_copy_framebuffer_state(&scene
->fb
, fb
);
378 scene
->tiles_x
= align(fb
->width
, TILE_SIZE
) / TILE_SIZE
;
379 scene
->tiles_y
= align(fb
->height
, TILE_SIZE
) / TILE_SIZE
;
381 assert(scene
->tiles_x
<= TILES_X
);
382 assert(scene
->tiles_y
<= TILES_Y
);
386 void lp_scene_rasterize( struct lp_scene
*scene
,
387 struct lp_rasterizer
*rast
)
391 debug_printf("rasterize scene:\n");
392 debug_printf(" data size: %u\n", lp_scene_data_size(scene
));
393 for (y
= 0; y
< scene
->tiles_y
; y
++) {
394 for (x
= 0; x
< scene
->tiles_x
; x
++) {
395 debug_printf(" bin %u, %u size: %u\n", x
, y
,
396 lp_scene_bin_size(scene
, x
, y
));
401 /* Enqueue the scene for rasterization, then immediately wait for
404 lp_rast_queue_scene( rast
, scene
);
406 /* Currently just wait for the rasterizer to finish. Some
407 * threading interactions need to be worked out, particularly once
408 * transfers become per-context:
410 lp_rast_finish( rast
);
412 util_unreference_framebuffer_state( &scene
->fb
);
414 /* put scene into the empty list */
415 lp_scene_enqueue( scene
->empty_queue
, scene
);