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"
38 struct lp_scene
*scene
= CALLOC_STRUCT(lp_scene
);
46 lp_scene_destroy(struct lp_scene
*scene
)
48 lp_scene_reset(scene
);
49 lp_scene_free_bin_data(scene
);
55 lp_scene_init(struct lp_scene
*scene
)
58 for (i
= 0; i
< TILES_X
; i
++)
59 for (j
= 0; j
< TILES_Y
; j
++) {
60 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, i
, j
);
61 bin
->commands
.head
= bin
->commands
.tail
= CALLOC_STRUCT(cmd_block
);
65 scene
->data
.tail
= CALLOC_STRUCT(data_block
);
67 make_empty_list(&scene
->textures
);
69 pipe_mutex_init(scene
->mutex
);
74 * Check if the scene's bins are all empty.
75 * For debugging purposes.
78 lp_scene_is_empty(struct lp_scene
*scene
)
82 for (y
= 0; y
< TILES_Y
; y
++) {
83 for (x
= 0; x
< TILES_X
; x
++) {
84 const struct cmd_bin
*bin
= lp_scene_get_bin(scene
, x
, y
);
85 const struct cmd_block_list
*list
= &bin
->commands
;
86 if (list
->head
!= list
->tail
|| list
->head
->count
> 0) {
96 lp_scene_bin_reset(struct lp_scene
*scene
, unsigned x
, unsigned y
)
98 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, x
, y
);
99 struct cmd_block_list
*list
= &bin
->commands
;
100 struct cmd_block
*block
;
101 struct cmd_block
*tmp
;
103 for (block
= list
->head
; block
!= list
->tail
; block
= tmp
) {
108 assert(list
->tail
->next
== NULL
);
109 list
->head
= list
->tail
;
110 list
->head
->count
= 0;
115 * Set scene to empty state.
118 lp_scene_reset(struct lp_scene
*scene
)
122 /* Free all but last binner command lists:
124 for (i
= 0; i
< scene
->tiles_x
; i
++) {
125 for (j
= 0; j
< scene
->tiles_y
; j
++) {
126 lp_scene_bin_reset(scene
, i
, j
);
130 assert(lp_scene_is_empty(scene
));
132 /* Free all but last binned data block:
135 struct data_block_list
*list
= &scene
->data
;
136 struct data_block
*block
, *tmp
;
138 for (block
= list
->head
; block
!= list
->tail
; block
= tmp
) {
143 assert(list
->tail
->next
== NULL
);
144 list
->head
= list
->tail
;
145 list
->head
->used
= 0;
148 /* Release texture refs
151 struct texture_ref
*ref
, *next
, *ref_list
= &scene
->textures
;
152 for (ref
= ref_list
->next
; ref
!= ref_list
; ref
= next
) {
153 next
= next_elem(ref
);
154 pipe_texture_reference(&ref
->texture
, NULL
);
157 make_empty_list(ref_list
);
163 * Free all data associated with the given bin, but don't free(scene).
166 lp_scene_free_bin_data(struct lp_scene
*scene
)
170 for (i
= 0; i
< TILES_X
; i
++)
171 for (j
= 0; j
< TILES_Y
; j
++) {
172 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, i
, j
);
173 /* lp_reset_scene() should have been already called */
174 assert(bin
->commands
.head
== bin
->commands
.tail
);
175 FREE(bin
->commands
.head
);
176 bin
->commands
.head
= NULL
;
177 bin
->commands
.tail
= NULL
;
180 FREE(scene
->data
.head
);
181 scene
->data
.head
= NULL
;
183 pipe_mutex_destroy(scene
->mutex
);
188 lp_scene_set_framebuffer_size( struct lp_scene
*scene
,
189 unsigned width
, unsigned height
)
191 assert(lp_scene_is_empty(scene
));
193 scene
->tiles_x
= align(width
, TILE_SIZE
) / TILE_SIZE
;
194 scene
->tiles_y
= align(height
, TILE_SIZE
) / TILE_SIZE
;
199 lp_bin_new_cmd_block( struct cmd_block_list
*list
)
201 struct cmd_block
*block
= MALLOC_STRUCT(cmd_block
);
202 list
->tail
->next
= block
;
210 lp_bin_new_data_block( struct data_block_list
*list
)
212 struct data_block
*block
= MALLOC_STRUCT(data_block
);
213 list
->tail
->next
= block
;
220 /** Return number of bytes used for all bin data within a scene */
222 lp_scene_data_size( const struct lp_scene
*scene
)
225 const struct data_block
*block
;
226 for (block
= scene
->data
.head
; block
; block
= block
->next
) {
233 /** Return number of bytes used for a single bin */
235 lp_scene_bin_size( const struct lp_scene
*scene
, unsigned x
, unsigned y
)
237 struct cmd_bin
*bin
= lp_scene_get_bin((struct lp_scene
*) scene
, x
, y
);
238 const struct cmd_block
*cmd
;
240 for (cmd
= bin
->commands
.head
; cmd
; cmd
= cmd
->next
) {
241 size
+= (cmd
->count
*
242 (sizeof(lp_rast_cmd
) + sizeof(union lp_rast_cmd_arg
)));
249 * Add a reference to a texture by the scene.
252 lp_scene_texture_reference( struct lp_scene
*scene
,
253 struct pipe_texture
*texture
)
255 struct texture_ref
*ref
= CALLOC_STRUCT(texture_ref
);
257 struct texture_ref
*ref_list
= &scene
->textures
;
258 pipe_texture_reference(&ref
->texture
, texture
);
259 insert_at_tail(ref_list
, ref
);
265 * Does this scene have a reference to the given texture?
268 lp_scene_is_textured_referenced( const struct lp_scene
*scene
,
269 const struct pipe_texture
*texture
)
271 const struct texture_ref
*ref_list
= &scene
->textures
;
272 const struct texture_ref
*ref
;
273 foreach (ref
, ref_list
) {
274 if (ref
->texture
== texture
)
282 * Return last command in the bin
285 lp_get_last_command( const struct cmd_bin
*bin
)
287 const struct cmd_block
*tail
= bin
->commands
.tail
;
288 const unsigned i
= tail
->count
;
290 return tail
->cmd
[i
- 1];
297 * Replace the arg of the last command in the bin.
300 lp_replace_last_command_arg( struct cmd_bin
*bin
,
301 const union lp_rast_cmd_arg arg
)
303 struct cmd_block
*tail
= bin
->commands
.tail
;
304 const unsigned i
= tail
->count
;
306 tail
->arg
[i
- 1] = arg
;
312 * Put a state-change command into all bins.
313 * If we find that the last command in a bin was also a state-change
314 * command, we can simply replace that one with the new one.
317 lp_scene_bin_state_command( struct lp_scene
*scene
,
319 const union lp_rast_cmd_arg arg
)
322 for (i
= 0; i
< scene
->tiles_x
; i
++) {
323 for (j
= 0; j
< scene
->tiles_y
; j
++) {
324 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, i
, j
);
325 lp_rast_cmd last_cmd
= lp_get_last_command(bin
);
326 if (last_cmd
== cmd
) {
327 lp_replace_last_command_arg(bin
, arg
);
330 lp_scene_bin_command( scene
, i
, j
, cmd
, arg
);
337 /** advance curr_x,y to the next bin */
339 next_bin(struct lp_scene
*scene
)
342 if (scene
->curr_x
>= scene
->tiles_x
) {
346 if (scene
->curr_y
>= scene
->tiles_y
) {
355 lp_scene_bin_iter_begin( struct lp_scene
*scene
)
357 scene
->curr_x
= scene
->curr_y
= -1;
362 * Return pointer to next bin to be rendered.
363 * The lp_scene::curr_x and ::curr_y fields will be advanced.
364 * Multiple rendering threads will call this function to get a chunk
365 * of work (a bin) to work on.
368 lp_scene_bin_iter_next( struct lp_scene
*scene
, int *bin_x
, int *bin_y
)
370 struct cmd_bin
*bin
= NULL
;
372 pipe_mutex_lock(scene
->mutex
);
374 if (scene
->curr_x
< 0) {
379 else if (!next_bin(scene
)) {
380 /* no more bins left */
384 bin
= lp_scene_get_bin(scene
, scene
->curr_x
, scene
->curr_y
);
385 *bin_x
= scene
->curr_x
;
386 *bin_y
= scene
->curr_y
;
389 /*printf("return bin %p at %d, %d\n", (void *) bin, *bin_x, *bin_y);*/
390 pipe_mutex_unlock(scene
->mutex
);