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"
33 #include "util/u_format.h"
39 #define RESOURCE_REF_SZ 32
41 /** List of resource references */
43 struct pipe_resource
*resource
[RESOURCE_REF_SZ
];
45 struct resource_ref
*next
;
50 * Create a new scene object.
51 * \param queue the queue to put newly rendered/emptied scenes into
54 lp_scene_create( struct pipe_context
*pipe
)
56 struct lp_scene
*scene
= CALLOC_STRUCT(lp_scene
);
63 CALLOC_STRUCT(data_block
);
65 pipe_mutex_init(scene
->mutex
);
68 /* Do some scene limit sanity checks here */
70 size_t maxBins
= TILES_X
* TILES_Y
;
71 size_t maxCommandBytes
= sizeof(struct cmd_block
) * maxBins
;
72 size_t maxCommandPlusData
= maxCommandBytes
+ DATA_BLOCK_SIZE
;
73 /* We'll need at least one command block per bin. Make sure that's
74 * less than the max allowed scene size.
76 assert(maxCommandBytes
< LP_SCENE_MAX_SIZE
);
77 /* We'll also need space for at least one other data block */
78 assert(maxCommandPlusData
<= LP_SCENE_MAX_SIZE
);
87 * Free all data associated with the given scene, and the scene itself.
90 lp_scene_destroy(struct lp_scene
*scene
)
92 lp_fence_reference(&scene
->fence
, NULL
);
93 pipe_mutex_destroy(scene
->mutex
);
94 assert(scene
->data
.head
->next
== NULL
);
95 FREE(scene
->data
.head
);
101 * Check if the scene's bins are all empty.
102 * For debugging purposes.
105 lp_scene_is_empty(struct lp_scene
*scene
)
109 for (y
= 0; y
< TILES_Y
; y
++) {
110 for (x
= 0; x
< TILES_X
; x
++) {
111 const struct cmd_bin
*bin
= lp_scene_get_bin(scene
, x
, y
);
121 /* Returns true if there has ever been a failed allocation attempt in
122 * this scene. Used in triangle emit to avoid having to check success
126 lp_scene_is_oom(struct lp_scene
*scene
)
128 return scene
->alloc_failed
;
132 /* Remove all commands from a bin. Tries to reuse some of the memory
133 * allocated to the bin, however.
136 lp_scene_bin_reset(struct lp_scene
*scene
, unsigned x
, unsigned y
)
138 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, x
, y
);
140 bin
->last_state
= NULL
;
141 bin
->head
= bin
->tail
;
143 bin
->tail
->next
= NULL
;
144 bin
->tail
->count
= 0;
150 lp_scene_begin_rasterization(struct lp_scene
*scene
)
152 const struct pipe_framebuffer_state
*fb
= &scene
->fb
;
155 //LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
157 for (i
= 0; i
< scene
->fb
.nr_cbufs
; i
++) {
158 struct pipe_surface
*cbuf
= scene
->fb
.cbufs
[i
];
159 if (llvmpipe_resource_is_texture(cbuf
->texture
)) {
160 scene
->cbufs
[i
].stride
= llvmpipe_resource_stride(cbuf
->texture
,
163 scene
->cbufs
[i
].map
= llvmpipe_resource_map(cbuf
->texture
,
165 cbuf
->u
.tex
.first_layer
,
166 LP_TEX_USAGE_READ_WRITE
,
167 LP_TEX_LAYOUT_LINEAR
);
170 struct llvmpipe_resource
*lpr
= llvmpipe_resource(cbuf
->texture
);
171 unsigned pixstride
= util_format_get_blocksize(cbuf
->format
);
172 scene
->cbufs
[i
].stride
= cbuf
->texture
->width0
;
173 scene
->cbufs
[i
].map
= lpr
->data
;
174 scene
->cbufs
[i
].map
+= cbuf
->u
.buf
.first_element
* pixstride
;
179 struct pipe_surface
*zsbuf
= scene
->fb
.zsbuf
;
180 scene
->zsbuf
.stride
= llvmpipe_resource_stride(zsbuf
->texture
, zsbuf
->u
.tex
.level
);
181 scene
->zsbuf
.blocksize
=
182 util_format_get_blocksize(zsbuf
->texture
->format
);
184 scene
->zsbuf
.map
= llvmpipe_resource_map(zsbuf
->texture
,
186 zsbuf
->u
.tex
.first_layer
,
187 LP_TEX_USAGE_READ_WRITE
,
196 * Free all the temporary data in a scene.
199 lp_scene_end_rasterization(struct lp_scene
*scene
)
203 /* Unmap color buffers */
204 for (i
= 0; i
< scene
->fb
.nr_cbufs
; i
++) {
205 if (scene
->cbufs
[i
].map
) {
206 struct pipe_surface
*cbuf
= scene
->fb
.cbufs
[i
];
207 if (llvmpipe_resource_is_texture(cbuf
->texture
)) {
208 llvmpipe_resource_unmap(cbuf
->texture
,
210 cbuf
->u
.tex
.first_layer
);
212 scene
->cbufs
[i
].map
= NULL
;
216 /* Unmap z/stencil buffer */
217 if (scene
->zsbuf
.map
) {
218 struct pipe_surface
*zsbuf
= scene
->fb
.zsbuf
;
219 llvmpipe_resource_unmap(zsbuf
->texture
,
221 zsbuf
->u
.tex
.first_layer
);
222 scene
->zsbuf
.map
= NULL
;
225 /* Reset all command lists:
227 for (i
= 0; i
< scene
->tiles_x
; i
++) {
228 for (j
= 0; j
< scene
->tiles_y
; j
++) {
229 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, i
, j
);
232 bin
->last_state
= NULL
;
236 /* If there are any bins which weren't cleared by the loop above,
237 * they will be caught (on debug builds at least) by this assert:
239 assert(lp_scene_is_empty(scene
));
241 /* Decrement texture ref counts
244 struct resource_ref
*ref
;
247 for (ref
= scene
->resources
; ref
; ref
= ref
->next
) {
248 for (i
= 0; i
< ref
->count
; i
++) {
249 if (LP_DEBUG
& DEBUG_SETUP
)
250 debug_printf("resource %d: %p %dx%d sz %d\n",
252 (void *) ref
->resource
[i
],
253 ref
->resource
[i
]->width0
,
254 ref
->resource
[i
]->height0
,
255 llvmpipe_resource_size(ref
->resource
[i
]));
257 pipe_resource_reference(&ref
->resource
[i
], NULL
);
261 if (LP_DEBUG
& DEBUG_SETUP
)
262 debug_printf("scene %d resources, sz %d\n",
263 j
, scene
->resource_reference_size
);
266 /* Free all scene data blocks:
269 struct data_block_list
*list
= &scene
->data
;
270 struct data_block
*block
, *tmp
;
272 for (block
= list
->head
->next
; block
; block
= tmp
) {
277 list
->head
->next
= NULL
;
278 list
->head
->used
= 0;
281 lp_fence_reference(&scene
->fence
, NULL
);
283 scene
->resources
= NULL
;
284 scene
->scene_size
= 0;
285 scene
->resource_reference_size
= 0;
287 scene
->has_depthstencil_clear
= FALSE
;
288 scene
->alloc_failed
= FALSE
;
290 util_unreference_framebuffer_state( &scene
->fb
);
299 lp_scene_new_cmd_block( struct lp_scene
*scene
,
300 struct cmd_bin
*bin
)
302 struct cmd_block
*block
= lp_scene_alloc(scene
, sizeof(struct cmd_block
));
305 bin
->tail
->next
= block
;
312 //memset(block, 0, sizeof *block);
321 lp_scene_new_data_block( struct lp_scene
*scene
)
323 if (scene
->scene_size
+ DATA_BLOCK_SIZE
> LP_SCENE_MAX_SIZE
) {
324 if (0) debug_printf("%s: failed\n", __FUNCTION__
);
325 scene
->alloc_failed
= TRUE
;
329 struct data_block
*block
= MALLOC_STRUCT(data_block
);
333 scene
->scene_size
+= sizeof *block
;
336 block
->next
= scene
->data
.head
;
337 scene
->data
.head
= block
;
345 * Return number of bytes used for all bin data within a scene.
346 * This does not include resources (textures) referenced by the scene.
349 lp_scene_data_size( const struct lp_scene
*scene
)
352 const struct data_block
*block
;
353 for (block
= scene
->data
.head
; block
; block
= block
->next
) {
362 * Add a reference to a resource by the scene.
365 lp_scene_add_resource_reference(struct lp_scene
*scene
,
366 struct pipe_resource
*resource
,
367 boolean initializing_scene
)
369 struct resource_ref
*ref
, **last
= &scene
->resources
;
372 /* Look at existing resource blocks:
374 for (ref
= scene
->resources
; ref
; ref
= ref
->next
) {
377 /* Search for this resource:
379 for (i
= 0; i
< ref
->count
; i
++)
380 if (ref
->resource
[i
] == resource
)
383 if (ref
->count
< RESOURCE_REF_SZ
) {
384 /* If the block is half-empty, then append the reference here.
390 /* Create a new block if no half-empty block was found.
393 assert(*last
== NULL
);
394 *last
= lp_scene_alloc(scene
, sizeof *ref
);
399 memset(ref
, 0, sizeof *ref
);
402 /* Append the reference to the reference block.
404 pipe_resource_reference(&ref
->resource
[ref
->count
++], resource
);
405 scene
->resource_reference_size
+= llvmpipe_resource_size(resource
);
407 /* Heuristic to advise scene flushes. This isn't helpful in the
408 * initial setup of the scene, but after that point flush on the
409 * next resource added which exceeds 64MB in referenced texture
412 if (!initializing_scene
&&
413 scene
->resource_reference_size
>= LP_SCENE_MAX_RESOURCE_SIZE
)
421 * Does this scene have a reference to the given resource?
424 lp_scene_is_resource_referenced(const struct lp_scene
*scene
,
425 const struct pipe_resource
*resource
)
427 const struct resource_ref
*ref
;
430 for (ref
= scene
->resources
; ref
; ref
= ref
->next
) {
431 for (i
= 0; i
< ref
->count
; i
++)
432 if (ref
->resource
[i
] == resource
)
442 /** advance curr_x,y to the next bin */
444 next_bin(struct lp_scene
*scene
)
447 if (scene
->curr_x
>= scene
->tiles_x
) {
451 if (scene
->curr_y
>= scene
->tiles_y
) {
460 lp_scene_bin_iter_begin( struct lp_scene
*scene
)
462 scene
->curr_x
= scene
->curr_y
= -1;
467 * Return pointer to next bin to be rendered.
468 * The lp_scene::curr_x and ::curr_y fields will be advanced.
469 * Multiple rendering threads will call this function to get a chunk
470 * of work (a bin) to work on.
473 lp_scene_bin_iter_next( struct lp_scene
*scene
)
475 struct cmd_bin
*bin
= NULL
;
477 pipe_mutex_lock(scene
->mutex
);
479 if (scene
->curr_x
< 0) {
484 else if (!next_bin(scene
)) {
485 /* no more bins left */
489 bin
= lp_scene_get_bin(scene
, scene
->curr_x
, scene
->curr_y
);
492 /*printf("return bin %p at %d, %d\n", (void *) bin, *bin_x, *bin_y);*/
493 pipe_mutex_unlock(scene
->mutex
);
498 void lp_scene_begin_binning( struct lp_scene
*scene
,
499 struct pipe_framebuffer_state
*fb
, boolean discard
)
501 assert(lp_scene_is_empty(scene
));
503 scene
->discard
= discard
;
504 util_copy_framebuffer_state(&scene
->fb
, fb
);
506 scene
->tiles_x
= align(fb
->width
, TILE_SIZE
) / TILE_SIZE
;
507 scene
->tiles_y
= align(fb
->height
, TILE_SIZE
) / TILE_SIZE
;
509 assert(scene
->tiles_x
<= TILES_X
);
510 assert(scene
->tiles_y
<= TILES_Y
);
514 void lp_scene_end_binning( struct lp_scene
*scene
)
516 if (LP_DEBUG
& DEBUG_SCENE
) {
517 debug_printf("rasterize scene:\n");
518 debug_printf(" scene_size: %u\n",
520 debug_printf(" data size: %u\n",
521 lp_scene_data_size(scene
));
524 lp_debug_bins( scene
);