71d9529230be70e4f9ec86522cea5274b00145fe
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"
38 /** List of texture references */
40 struct pipe_resource
*texture
;
41 struct texture_ref
*prev
, *next
; /**< linked list w/ u_simple_list.h */
47 lp_scene_create( struct pipe_context
*pipe
,
48 struct lp_scene_queue
*queue
)
51 struct lp_scene
*scene
= CALLOC_STRUCT(lp_scene
);
56 scene
->empty_queue
= queue
;
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
);
66 scene
->data
.tail
= CALLOC_STRUCT(data_block
);
68 make_empty_list(&scene
->resources
);
70 pipe_mutex_init(scene
->mutex
);
77 * Free all data associated with the given scene, and the scene itself.
80 lp_scene_destroy(struct lp_scene
*scene
)
84 lp_scene_reset(scene
);
86 for (i
= 0; i
< TILES_X
; i
++)
87 for (j
= 0; j
< TILES_Y
; j
++) {
88 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, i
, j
);
89 assert(bin
->commands
.head
== bin
->commands
.tail
);
90 FREE(bin
->commands
.head
);
91 bin
->commands
.head
= NULL
;
92 bin
->commands
.tail
= NULL
;
95 FREE(scene
->data
.head
);
96 scene
->data
.head
= NULL
;
98 pipe_mutex_destroy(scene
->mutex
);
105 * Check if the scene's bins are all empty.
106 * For debugging purposes.
109 lp_scene_is_empty(struct lp_scene
*scene
)
113 for (y
= 0; y
< TILES_Y
; y
++) {
114 for (x
= 0; x
< TILES_X
; x
++) {
115 const struct cmd_bin
*bin
= lp_scene_get_bin(scene
, x
, y
);
116 const struct cmd_block_list
*list
= &bin
->commands
;
117 if (list
->head
!= list
->tail
|| list
->head
->count
> 0) {
126 /* Free data for one particular bin. May be called from the
127 * rasterizer thread(s).
130 lp_scene_bin_reset(struct lp_scene
*scene
, unsigned x
, unsigned y
)
132 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, x
, y
);
133 struct cmd_block_list
*list
= &bin
->commands
;
134 struct cmd_block
*block
;
135 struct cmd_block
*tmp
;
140 for (block
= list
->head
; block
!= list
->tail
; block
= tmp
) {
145 assert(list
->tail
->next
== NULL
);
146 list
->head
= list
->tail
;
147 list
->head
->count
= 0;
152 * Free all the temporary data in a scene. May be called from the
153 * rasterizer thread(s).
156 lp_scene_reset(struct lp_scene
*scene
)
160 /* Free all but last binner command lists:
162 for (i
= 0; i
< scene
->tiles_x
; i
++) {
163 for (j
= 0; j
< scene
->tiles_y
; j
++) {
164 lp_scene_bin_reset(scene
, i
, j
);
168 assert(lp_scene_is_empty(scene
));
170 /* Free all but last binned data block:
173 struct data_block_list
*list
= &scene
->data
;
174 struct data_block
*block
, *tmp
;
176 for (block
= list
->head
; block
!= list
->tail
; block
= tmp
) {
181 assert(list
->tail
->next
== NULL
);
182 list
->head
= list
->tail
;
183 list
->head
->used
= 0;
186 /* Release texture refs
189 struct resource_ref
*ref
, *next
, *ref_list
= &scene
->resources
;
190 for (ref
= ref_list
->next
; ref
!= ref_list
; ref
= next
) {
191 next
= next_elem(ref
);
192 pipe_resource_reference(&ref
->resource
, NULL
);
195 make_empty_list(ref_list
);
198 scene
->has_color_clear
= FALSE
;
199 scene
->has_depth_clear
= FALSE
;
208 lp_bin_new_cmd_block( struct cmd_block_list
*list
)
210 struct cmd_block
*block
= MALLOC_STRUCT(cmd_block
);
211 list
->tail
->next
= block
;
219 lp_bin_new_data_block( struct data_block_list
*list
)
221 struct data_block
*block
= MALLOC_STRUCT(data_block
);
222 list
->tail
->next
= block
;
229 /** Return number of bytes used for all bin data within a scene */
231 lp_scene_data_size( const struct lp_scene
*scene
)
234 const struct data_block
*block
;
235 for (block
= scene
->data
.head
; block
; block
= block
->next
) {
242 /** Return number of bytes used for a single bin */
244 lp_scene_bin_size( const struct lp_scene
*scene
, unsigned x
, unsigned y
)
246 struct cmd_bin
*bin
= lp_scene_get_bin((struct lp_scene
*) scene
, x
, y
);
247 const struct cmd_block
*cmd
;
249 for (cmd
= bin
->commands
.head
; cmd
; cmd
= cmd
->next
) {
250 size
+= (cmd
->count
*
251 (sizeof(lp_rast_cmd
) + sizeof(union lp_rast_cmd_arg
)));
258 * Add a reference to a resource by the scene.
261 lp_scene_add_resource_reference(struct lp_scene
*scene
,
262 struct pipe_resource
*resource
)
264 struct resource_ref
*ref
= CALLOC_STRUCT(resource_ref
);
266 struct resource_ref
*ref_list
= &scene
->resources
;
267 pipe_resource_reference(&ref
->resource
, resource
);
268 insert_at_tail(ref_list
, ref
);
274 * Does this scene have a reference to the given resource?
277 lp_scene_is_resource_referenced(const struct lp_scene
*scene
,
278 const struct pipe_resource
*resource
)
280 const struct resource_ref
*ref_list
= &scene
->resources
;
281 const struct resource_ref
*ref
;
282 foreach (ref
, ref_list
) {
283 if (ref
->resource
== resource
)
291 * Return last command in the bin
294 lp_get_last_command( const struct cmd_bin
*bin
)
296 const struct cmd_block
*tail
= bin
->commands
.tail
;
297 const unsigned i
= tail
->count
;
299 return tail
->cmd
[i
- 1];
306 * Replace the arg of the last command in the bin.
309 lp_replace_last_command_arg( struct cmd_bin
*bin
,
310 const union lp_rast_cmd_arg arg
)
312 struct cmd_block
*tail
= bin
->commands
.tail
;
313 const unsigned i
= tail
->count
;
315 tail
->arg
[i
- 1] = arg
;
321 * Put a state-change command into all bins.
322 * If we find that the last command in a bin was also a state-change
323 * command, we can simply replace that one with the new one.
326 lp_scene_bin_state_command( struct lp_scene
*scene
,
328 const union lp_rast_cmd_arg arg
)
331 for (i
= 0; i
< scene
->tiles_x
; i
++) {
332 for (j
= 0; j
< scene
->tiles_y
; j
++) {
333 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, i
, j
);
334 lp_rast_cmd last_cmd
= lp_get_last_command(bin
);
335 if (last_cmd
== cmd
) {
336 lp_replace_last_command_arg(bin
, arg
);
339 lp_scene_bin_command( scene
, i
, j
, cmd
, arg
);
346 /** advance curr_x,y to the next bin */
348 next_bin(struct lp_scene
*scene
)
351 if (scene
->curr_x
>= scene
->tiles_x
) {
355 if (scene
->curr_y
>= scene
->tiles_y
) {
364 lp_scene_bin_iter_begin( struct lp_scene
*scene
)
366 scene
->curr_x
= scene
->curr_y
= -1;
371 * Return pointer to next bin to be rendered.
372 * The lp_scene::curr_x and ::curr_y fields will be advanced.
373 * Multiple rendering threads will call this function to get a chunk
374 * of work (a bin) to work on.
377 lp_scene_bin_iter_next( struct lp_scene
*scene
, int *bin_x
, int *bin_y
)
379 struct cmd_bin
*bin
= NULL
;
381 pipe_mutex_lock(scene
->mutex
);
383 if (scene
->curr_x
< 0) {
388 else if (!next_bin(scene
)) {
389 /* no more bins left */
393 bin
= lp_scene_get_bin(scene
, scene
->curr_x
, scene
->curr_y
);
394 *bin_x
= scene
->curr_x
;
395 *bin_y
= scene
->curr_y
;
398 /*printf("return bin %p at %d, %d\n", (void *) bin, *bin_x, *bin_y);*/
399 pipe_mutex_unlock(scene
->mutex
);
406 * Prepare this scene for the rasterizer.
407 * Map the framebuffer surfaces. Initialize the 'rast' state.
410 lp_scene_map_buffers( struct lp_scene
*scene
)
412 LP_DBG(DEBUG_RAST
, "%s\n", __FUNCTION__
);
414 /* XXX framebuffer surfaces are no longer mapped here */
415 /* XXX move all map/unmap stuff into rast module... */
423 * Called after rasterizer as finished rasterizing a scene.
425 * We want to call this from the pipe_context's current thread to
426 * avoid having to have mutexes on the transfer functions.
429 lp_scene_unmap_buffers( struct lp_scene
*scene
)
434 for (i
= 0; i
< scene
->fb
.nr_cbufs
; i
++) {
435 if (scene
->cbuf_map
[i
]) {
436 struct pipe_surface
*cbuf
= scene
->fb
.cbufs
[i
];
437 llvmpipe_resource_unmap(cbuf
->texture
,
441 scene
->cbuf_map
[i
] = NULL
;
445 if (scene
->zsbuf_map
) {
446 struct pipe_surface
*zsbuf
= scene
->fb
.zsbuf
;
447 llvmpipe_resource_unmap(zsbuf
->texture
,
451 scene
->zsbuf_map
= NULL
;
455 util_unreference_framebuffer_state( &scene
->fb
);
459 void lp_scene_begin_binning( struct lp_scene
*scene
,
460 struct pipe_framebuffer_state
*fb
)
462 assert(lp_scene_is_empty(scene
));
464 util_copy_framebuffer_state(&scene
->fb
, fb
);
466 scene
->tiles_x
= align(fb
->width
, TILE_SIZE
) / TILE_SIZE
;
467 scene
->tiles_y
= align(fb
->height
, TILE_SIZE
) / TILE_SIZE
;
469 assert(scene
->tiles_x
<= TILES_X
);
470 assert(scene
->tiles_y
<= TILES_Y
);
474 void lp_scene_rasterize( struct lp_scene
*scene
,
475 struct lp_rasterizer
*rast
,
476 boolean write_depth
)
480 debug_printf("rasterize scene:\n");
481 debug_printf(" data size: %u\n", lp_scene_data_size(scene
));
482 for (y
= 0; y
< scene
->tiles_y
; y
++) {
483 for (x
= 0; x
< scene
->tiles_x
; x
++) {
484 debug_printf(" bin %u, %u size: %u\n", x
, y
,
485 lp_scene_bin_size(scene
, x
, y
));
490 scene
->write_depth
= (scene
->fb
.zsbuf
!= NULL
&&
493 lp_scene_map_buffers( scene
);
495 /* Enqueue the scene for rasterization, then immediately wait for
498 lp_rast_queue_scene( rast
, scene
);
500 /* Currently just wait for the rasterizer to finish. Some
501 * threading interactions need to be worked out, particularly once
502 * transfers become per-context:
504 lp_rast_finish( rast
);
505 lp_scene_unmap_buffers( scene
);
506 lp_scene_enqueue( scene
->empty_queue
, scene
);