cba0e2129851f08781c4523492ecccd997de126f
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
;
106 for (block
= list
->head
; block
!= list
->tail
; block
= tmp
) {
111 assert(list
->tail
->next
== NULL
);
112 list
->head
= list
->tail
;
113 list
->head
->count
= 0;
118 * Set scene to empty state.
121 lp_scene_reset(struct lp_scene
*scene
)
125 /* Free all but last binner command lists:
127 for (i
= 0; i
< scene
->tiles_x
; i
++) {
128 for (j
= 0; j
< scene
->tiles_y
; j
++) {
129 lp_scene_bin_reset(scene
, i
, j
);
133 assert(lp_scene_is_empty(scene
));
135 /* Free all but last binned data block:
138 struct data_block_list
*list
= &scene
->data
;
139 struct data_block
*block
, *tmp
;
141 for (block
= list
->head
; block
!= list
->tail
; block
= tmp
) {
146 assert(list
->tail
->next
== NULL
);
147 list
->head
= list
->tail
;
148 list
->head
->used
= 0;
151 /* Release texture refs
154 struct texture_ref
*ref
, *next
, *ref_list
= &scene
->textures
;
155 for (ref
= ref_list
->next
; ref
!= ref_list
; ref
= next
) {
156 next
= next_elem(ref
);
157 pipe_texture_reference(&ref
->texture
, NULL
);
160 make_empty_list(ref_list
);
166 * Free all data associated with the given bin, but don't free(scene).
169 lp_scene_free_bin_data(struct lp_scene
*scene
)
173 for (i
= 0; i
< TILES_X
; i
++)
174 for (j
= 0; j
< TILES_Y
; j
++) {
175 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, i
, j
);
176 /* lp_reset_scene() should have been already called */
177 assert(bin
->commands
.head
== bin
->commands
.tail
);
178 FREE(bin
->commands
.head
);
179 bin
->commands
.head
= NULL
;
180 bin
->commands
.tail
= NULL
;
183 FREE(scene
->data
.head
);
184 scene
->data
.head
= NULL
;
186 pipe_mutex_destroy(scene
->mutex
);
191 lp_scene_set_framebuffer_size( struct lp_scene
*scene
,
192 unsigned width
, unsigned height
)
194 assert(lp_scene_is_empty(scene
));
196 scene
->tiles_x
= align(width
, TILE_SIZE
) / TILE_SIZE
;
197 scene
->tiles_y
= align(height
, TILE_SIZE
) / TILE_SIZE
;
202 lp_bin_new_cmd_block( struct cmd_block_list
*list
)
204 struct cmd_block
*block
= MALLOC_STRUCT(cmd_block
);
205 list
->tail
->next
= block
;
213 lp_bin_new_data_block( struct data_block_list
*list
)
215 struct data_block
*block
= MALLOC_STRUCT(data_block
);
216 list
->tail
->next
= block
;
223 /** Return number of bytes used for all bin data within a scene */
225 lp_scene_data_size( const struct lp_scene
*scene
)
228 const struct data_block
*block
;
229 for (block
= scene
->data
.head
; block
; block
= block
->next
) {
236 /** Return number of bytes used for a single bin */
238 lp_scene_bin_size( const struct lp_scene
*scene
, unsigned x
, unsigned y
)
240 struct cmd_bin
*bin
= lp_scene_get_bin((struct lp_scene
*) scene
, x
, y
);
241 const struct cmd_block
*cmd
;
243 for (cmd
= bin
->commands
.head
; cmd
; cmd
= cmd
->next
) {
244 size
+= (cmd
->count
*
245 (sizeof(lp_rast_cmd
) + sizeof(union lp_rast_cmd_arg
)));
252 * Add a reference to a texture by the scene.
255 lp_scene_texture_reference( struct lp_scene
*scene
,
256 struct pipe_texture
*texture
)
258 struct texture_ref
*ref
= CALLOC_STRUCT(texture_ref
);
260 struct texture_ref
*ref_list
= &scene
->textures
;
261 pipe_texture_reference(&ref
->texture
, texture
);
262 insert_at_tail(ref_list
, ref
);
268 * Does this scene have a reference to the given texture?
271 lp_scene_is_texture_referenced( const struct lp_scene
*scene
,
272 const struct pipe_texture
*texture
)
274 const struct texture_ref
*ref_list
= &scene
->textures
;
275 const struct texture_ref
*ref
;
276 foreach (ref
, ref_list
) {
277 if (ref
->texture
== texture
)
285 * Return last command in the bin
288 lp_get_last_command( const struct cmd_bin
*bin
)
290 const struct cmd_block
*tail
= bin
->commands
.tail
;
291 const unsigned i
= tail
->count
;
293 return tail
->cmd
[i
- 1];
300 * Replace the arg of the last command in the bin.
303 lp_replace_last_command_arg( struct cmd_bin
*bin
,
304 const union lp_rast_cmd_arg arg
)
306 struct cmd_block
*tail
= bin
->commands
.tail
;
307 const unsigned i
= tail
->count
;
309 tail
->arg
[i
- 1] = arg
;
315 * Put a state-change command into all bins.
316 * If we find that the last command in a bin was also a state-change
317 * command, we can simply replace that one with the new one.
320 lp_scene_bin_state_command( struct lp_scene
*scene
,
322 const union lp_rast_cmd_arg arg
)
325 for (i
= 0; i
< scene
->tiles_x
; i
++) {
326 for (j
= 0; j
< scene
->tiles_y
; j
++) {
327 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, i
, j
);
328 lp_rast_cmd last_cmd
= lp_get_last_command(bin
);
329 if (last_cmd
== cmd
) {
330 lp_replace_last_command_arg(bin
, arg
);
333 lp_scene_bin_command( scene
, i
, j
, cmd
, arg
);
340 /** advance curr_x,y to the next bin */
342 next_bin(struct lp_scene
*scene
)
345 if (scene
->curr_x
>= scene
->tiles_x
) {
349 if (scene
->curr_y
>= scene
->tiles_y
) {
358 lp_scene_bin_iter_begin( struct lp_scene
*scene
)
360 scene
->curr_x
= scene
->curr_y
= -1;
365 * Return pointer to next bin to be rendered.
366 * The lp_scene::curr_x and ::curr_y fields will be advanced.
367 * Multiple rendering threads will call this function to get a chunk
368 * of work (a bin) to work on.
371 lp_scene_bin_iter_next( struct lp_scene
*scene
, int *bin_x
, int *bin_y
)
373 struct cmd_bin
*bin
= NULL
;
375 pipe_mutex_lock(scene
->mutex
);
377 if (scene
->curr_x
< 0) {
382 else if (!next_bin(scene
)) {
383 /* no more bins left */
387 bin
= lp_scene_get_bin(scene
, scene
->curr_x
, scene
->curr_y
);
388 *bin_x
= scene
->curr_x
;
389 *bin_y
= scene
->curr_y
;
392 /*printf("return bin %p at %d, %d\n", (void *) bin, *bin_x, *bin_y);*/
393 pipe_mutex_unlock(scene
->mutex
);