e8d36bbdc5038305b794da9c77133ef7228d933e
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"
37 /** List of texture references */
39 struct pipe_resource
*texture
;
40 struct texture_ref
*prev
, *next
; /**< linked list w/ u_simple_list.h */
46 * Create a new scene object.
47 * \param queue the queue to put newly rendered/emptied scenes into
50 lp_scene_create( struct pipe_context
*pipe
,
51 struct lp_scene_queue
*queue
)
54 struct lp_scene
*scene
= CALLOC_STRUCT(lp_scene
);
59 scene
->empty_queue
= queue
;
61 for (i
= 0; i
< TILES_X
; i
++) {
62 for (j
= 0; j
< TILES_Y
; j
++) {
63 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, i
, j
);
64 bin
->commands
.head
= bin
->commands
.tail
= CALLOC_STRUCT(cmd_block
);
69 scene
->data
.tail
= CALLOC_STRUCT(data_block
);
71 make_empty_list(&scene
->resources
);
73 pipe_mutex_init(scene
->mutex
);
80 * Free all data associated with the given scene, and the scene itself.
83 lp_scene_destroy(struct lp_scene
*scene
)
87 lp_scene_reset(scene
);
89 for (i
= 0; i
< TILES_X
; i
++)
90 for (j
= 0; j
< TILES_Y
; j
++) {
91 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, i
, j
);
92 assert(bin
->commands
.head
== bin
->commands
.tail
);
93 FREE(bin
->commands
.head
);
94 bin
->commands
.head
= NULL
;
95 bin
->commands
.tail
= NULL
;
98 FREE(scene
->data
.head
);
99 scene
->data
.head
= NULL
;
101 pipe_mutex_destroy(scene
->mutex
);
108 * Check if the scene's bins are all empty.
109 * For debugging purposes.
112 lp_scene_is_empty(struct lp_scene
*scene
)
116 for (y
= 0; y
< TILES_Y
; y
++) {
117 for (x
= 0; x
< TILES_X
; x
++) {
118 const struct cmd_bin
*bin
= lp_scene_get_bin(scene
, x
, y
);
119 const struct cmd_block_list
*list
= &bin
->commands
;
120 if (list
->head
!= list
->tail
|| list
->head
->count
> 0) {
129 /* Free data for one particular bin. May be called from the
130 * rasterizer thread(s).
133 lp_scene_bin_reset(struct lp_scene
*scene
, unsigned x
, unsigned y
)
135 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, x
, y
);
136 struct cmd_block_list
*list
= &bin
->commands
;
137 struct cmd_block
*block
;
138 struct cmd_block
*tmp
;
143 for (block
= list
->head
; block
!= list
->tail
; block
= tmp
) {
148 assert(list
->tail
->next
== NULL
);
149 list
->head
= list
->tail
;
150 list
->head
->count
= 0;
155 * Free all the temporary data in a scene. May be called from the
156 * rasterizer thread(s).
159 lp_scene_reset(struct lp_scene
*scene
)
163 /* Free all but last binner command lists:
165 for (i
= 0; i
< TILES_X
; i
++) {
166 for (j
= 0; j
< TILES_Y
; j
++) {
167 lp_scene_bin_reset(scene
, i
, j
);
171 assert(lp_scene_is_empty(scene
));
173 /* Free all but last binned data block:
176 struct data_block_list
*list
= &scene
->data
;
177 struct data_block
*block
, *tmp
;
179 for (block
= list
->head
; block
!= list
->tail
; block
= tmp
) {
184 assert(list
->tail
->next
== NULL
);
185 list
->head
= list
->tail
;
186 list
->head
->used
= 0;
189 /* Release texture refs
192 struct resource_ref
*ref
, *next
, *ref_list
= &scene
->resources
;
193 for (ref
= ref_list
->next
; ref
!= ref_list
; ref
= next
) {
194 next
= next_elem(ref
);
195 pipe_resource_reference(&ref
->resource
, NULL
);
198 make_empty_list(ref_list
);
201 scene
->scene_size
= 0;
203 scene
->has_color_clear
= FALSE
;
204 scene
->has_depthstencil_clear
= FALSE
;
213 lp_bin_new_cmd_block( struct cmd_block_list
*list
)
215 struct cmd_block
*block
= MALLOC_STRUCT(cmd_block
);
217 list
->tail
->next
= block
;
227 lp_bin_new_data_block( struct data_block_list
*list
)
229 struct data_block
*block
= MALLOC_STRUCT(data_block
);
231 list
->tail
->next
= block
;
241 * Return number of bytes used for all bin data within a scene.
242 * This does not include resources (textures) referenced by the scene.
245 lp_scene_data_size( const struct lp_scene
*scene
)
248 const struct data_block
*block
;
249 for (block
= scene
->data
.head
; block
; block
= block
->next
) {
256 /** Return number of bytes used for a single bin */
258 lp_scene_bin_size( const struct lp_scene
*scene
, unsigned x
, unsigned y
)
260 struct cmd_bin
*bin
= lp_scene_get_bin((struct lp_scene
*) scene
, x
, y
);
261 const struct cmd_block
*cmd
;
263 for (cmd
= bin
->commands
.head
; cmd
; cmd
= cmd
->next
) {
264 size
+= (cmd
->count
*
265 (sizeof(lp_rast_cmd
) + sizeof(union lp_rast_cmd_arg
)));
272 * Add a reference to a resource by the scene.
275 lp_scene_add_resource_reference(struct lp_scene
*scene
,
276 struct pipe_resource
*resource
)
278 struct resource_ref
*ref
= CALLOC_STRUCT(resource_ref
);
280 struct resource_ref
*ref_list
= &scene
->resources
;
281 pipe_resource_reference(&ref
->resource
, resource
);
282 insert_at_tail(ref_list
, ref
);
285 scene
->scene_size
+= llvmpipe_resource_size(resource
);
290 * Does this scene have a reference to the given resource?
293 lp_scene_is_resource_referenced(const struct lp_scene
*scene
,
294 const struct pipe_resource
*resource
)
296 const struct resource_ref
*ref_list
= &scene
->resources
;
297 const struct resource_ref
*ref
;
298 foreach (ref
, ref_list
) {
299 if (ref
->resource
== resource
)
307 * Return last command in the bin
310 lp_get_last_command( const struct cmd_bin
*bin
)
312 const struct cmd_block
*tail
= bin
->commands
.tail
;
313 const unsigned i
= tail
->count
;
315 return tail
->cmd
[i
- 1];
322 * Replace the arg of the last command in the bin.
325 lp_replace_last_command_arg( struct cmd_bin
*bin
,
326 const union lp_rast_cmd_arg arg
)
328 struct cmd_block
*tail
= bin
->commands
.tail
;
329 const unsigned i
= tail
->count
;
331 tail
->arg
[i
- 1] = arg
;
337 * Put a state-change command into all bins.
338 * If we find that the last command in a bin was also a state-change
339 * command, we can simply replace that one with the new one.
342 lp_scene_bin_state_command( struct lp_scene
*scene
,
344 const union lp_rast_cmd_arg arg
)
347 for (i
= 0; i
< scene
->tiles_x
; i
++) {
348 for (j
= 0; j
< scene
->tiles_y
; j
++) {
349 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, i
, j
);
350 lp_rast_cmd last_cmd
= lp_get_last_command(bin
);
351 if (last_cmd
== cmd
) {
352 lp_replace_last_command_arg(bin
, arg
);
355 lp_scene_bin_command( scene
, i
, j
, cmd
, arg
);
362 /** advance curr_x,y to the next bin */
364 next_bin(struct lp_scene
*scene
)
367 if (scene
->curr_x
>= scene
->tiles_x
) {
371 if (scene
->curr_y
>= scene
->tiles_y
) {
380 lp_scene_bin_iter_begin( struct lp_scene
*scene
)
382 scene
->curr_x
= scene
->curr_y
= -1;
387 * Return pointer to next bin to be rendered.
388 * The lp_scene::curr_x and ::curr_y fields will be advanced.
389 * Multiple rendering threads will call this function to get a chunk
390 * of work (a bin) to work on.
393 lp_scene_bin_iter_next( struct lp_scene
*scene
, int *bin_x
, int *bin_y
)
395 struct cmd_bin
*bin
= NULL
;
397 pipe_mutex_lock(scene
->mutex
);
399 if (scene
->curr_x
< 0) {
404 else if (!next_bin(scene
)) {
405 /* no more bins left */
409 bin
= lp_scene_get_bin(scene
, scene
->curr_x
, scene
->curr_y
);
410 *bin_x
= scene
->curr_x
;
411 *bin_y
= scene
->curr_y
;
414 /*printf("return bin %p at %d, %d\n", (void *) bin, *bin_x, *bin_y);*/
415 pipe_mutex_unlock(scene
->mutex
);
420 void lp_scene_begin_binning( struct lp_scene
*scene
,
421 struct pipe_framebuffer_state
*fb
)
423 assert(lp_scene_is_empty(scene
));
425 util_copy_framebuffer_state(&scene
->fb
, fb
);
427 scene
->tiles_x
= align(fb
->width
, TILE_SIZE
) / TILE_SIZE
;
428 scene
->tiles_y
= align(fb
->height
, TILE_SIZE
) / TILE_SIZE
;
430 assert(scene
->tiles_x
<= TILES_X
);
431 assert(scene
->tiles_y
<= TILES_Y
);
435 void lp_scene_rasterize( struct lp_scene
*scene
,
436 struct lp_rasterizer
*rast
)
440 debug_printf("rasterize scene:\n");
441 debug_printf(" data size: %u\n", lp_scene_data_size(scene
));
442 for (y
= 0; y
< scene
->tiles_y
; y
++) {
443 for (x
= 0; x
< scene
->tiles_x
; x
++) {
444 debug_printf(" bin %u, %u size: %u\n", x
, y
,
445 lp_scene_bin_size(scene
, x
, y
));
450 /* Enqueue the scene for rasterization, then immediately wait for
453 lp_rast_queue_scene( rast
, scene
);
455 /* Currently just wait for the rasterizer to finish. Some
456 * threading interactions need to be worked out, particularly once
457 * transfers become per-context:
459 lp_rast_finish( rast
);
461 util_unreference_framebuffer_state( &scene
->fb
);
463 /* put scene into the empty list */
464 lp_scene_enqueue( scene
->empty_queue
, scene
);