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/simple_list.h"
33 #include "util/format/u_format.h"
37 #include "lp_context.h"
38 #include "lp_state_fs.h"
41 #define RESOURCE_REF_SZ 32
43 /** List of resource references */
45 struct pipe_resource
*resource
[RESOURCE_REF_SZ
];
47 struct resource_ref
*next
;
50 #define SHADER_REF_SZ 32
51 /** List of shader variant references */
53 struct lp_fragment_shader_variant
*variant
[SHADER_REF_SZ
];
55 struct shader_ref
*next
;
60 * Create a new scene object.
61 * \param queue the queue to put newly rendered/emptied scenes into
64 lp_scene_create( struct pipe_context
*pipe
)
66 struct lp_scene
*scene
= CALLOC_STRUCT(lp_scene
);
73 CALLOC_STRUCT(data_block
);
75 (void) mtx_init(&scene
->mutex
, mtx_plain
);
78 /* Do some scene limit sanity checks here */
80 size_t maxBins
= TILES_X
* TILES_Y
;
81 size_t maxCommandBytes
= sizeof(struct cmd_block
) * maxBins
;
82 size_t maxCommandPlusData
= maxCommandBytes
+ DATA_BLOCK_SIZE
;
83 /* We'll need at least one command block per bin. Make sure that's
84 * less than the max allowed scene size.
86 assert(maxCommandBytes
< LP_SCENE_MAX_SIZE
);
87 /* We'll also need space for at least one other data block */
88 assert(maxCommandPlusData
<= LP_SCENE_MAX_SIZE
);
97 * Free all data associated with the given scene, and the scene itself.
100 lp_scene_destroy(struct lp_scene
*scene
)
102 lp_fence_reference(&scene
->fence
, NULL
);
103 mtx_destroy(&scene
->mutex
);
104 assert(scene
->data
.head
->next
== NULL
);
105 FREE(scene
->data
.head
);
111 * Check if the scene's bins are all empty.
112 * For debugging purposes.
115 lp_scene_is_empty(struct lp_scene
*scene
)
119 for (y
= 0; y
< scene
->tiles_y
; y
++) {
120 for (x
= 0; x
< scene
->tiles_x
; x
++) {
121 const struct cmd_bin
*bin
= lp_scene_get_bin(scene
, x
, y
);
131 /* Returns true if there has ever been a failed allocation attempt in
132 * this scene. Used in triangle emit to avoid having to check success
136 lp_scene_is_oom(struct lp_scene
*scene
)
138 return scene
->alloc_failed
;
142 /* Remove all commands from a bin. Tries to reuse some of the memory
143 * allocated to the bin, however.
146 lp_scene_bin_reset(struct lp_scene
*scene
, unsigned x
, unsigned y
)
148 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, x
, y
);
150 bin
->last_state
= NULL
;
151 bin
->head
= bin
->tail
;
153 bin
->tail
->next
= NULL
;
154 bin
->tail
->count
= 0;
160 lp_scene_begin_rasterization(struct lp_scene
*scene
)
162 const struct pipe_framebuffer_state
*fb
= &scene
->fb
;
165 //LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
167 for (i
= 0; i
< scene
->fb
.nr_cbufs
; i
++) {
168 struct pipe_surface
*cbuf
= scene
->fb
.cbufs
[i
];
171 scene
->cbufs
[i
].stride
= 0;
172 scene
->cbufs
[i
].layer_stride
= 0;
173 scene
->cbufs
[i
].sample_stride
= 0;
174 scene
->cbufs
[i
].nr_samples
= 0;
175 scene
->cbufs
[i
].map
= NULL
;
179 if (llvmpipe_resource_is_texture(cbuf
->texture
)) {
180 scene
->cbufs
[i
].stride
= llvmpipe_resource_stride(cbuf
->texture
,
182 scene
->cbufs
[i
].layer_stride
= llvmpipe_layer_stride(cbuf
->texture
,
184 scene
->cbufs
[i
].sample_stride
= llvmpipe_sample_stride(cbuf
->texture
);
186 scene
->cbufs
[i
].map
= llvmpipe_resource_map(cbuf
->texture
,
188 cbuf
->u
.tex
.first_layer
,
189 LP_TEX_USAGE_READ_WRITE
);
190 scene
->cbufs
[i
].format_bytes
= util_format_get_blocksize(cbuf
->format
);
191 scene
->cbufs
[i
].nr_samples
= util_res_sample_count(cbuf
->texture
);
194 struct llvmpipe_resource
*lpr
= llvmpipe_resource(cbuf
->texture
);
195 unsigned pixstride
= util_format_get_blocksize(cbuf
->format
);
196 scene
->cbufs
[i
].stride
= cbuf
->texture
->width0
;
197 scene
->cbufs
[i
].layer_stride
= 0;
198 scene
->cbufs
[i
].sample_stride
= 0;
199 scene
->cbufs
[i
].nr_samples
= 1;
200 scene
->cbufs
[i
].map
= lpr
->data
;
201 scene
->cbufs
[i
].map
+= cbuf
->u
.buf
.first_element
* pixstride
;
202 scene
->cbufs
[i
].format_bytes
= util_format_get_blocksize(cbuf
->format
);
207 struct pipe_surface
*zsbuf
= scene
->fb
.zsbuf
;
208 scene
->zsbuf
.stride
= llvmpipe_resource_stride(zsbuf
->texture
, zsbuf
->u
.tex
.level
);
209 scene
->zsbuf
.layer_stride
= llvmpipe_layer_stride(zsbuf
->texture
, zsbuf
->u
.tex
.level
);
210 scene
->zsbuf
.sample_stride
= llvmpipe_sample_stride(zsbuf
->texture
);
211 scene
->zsbuf
.nr_samples
= util_res_sample_count(zsbuf
->texture
);
212 scene
->zsbuf
.map
= llvmpipe_resource_map(zsbuf
->texture
,
214 zsbuf
->u
.tex
.first_layer
,
215 LP_TEX_USAGE_READ_WRITE
);
216 scene
->zsbuf
.format_bytes
= util_format_get_blocksize(zsbuf
->format
);
224 * Free all the temporary data in a scene.
227 lp_scene_end_rasterization(struct lp_scene
*scene
)
231 /* Unmap color buffers */
232 for (i
= 0; i
< scene
->fb
.nr_cbufs
; i
++) {
233 if (scene
->cbufs
[i
].map
) {
234 struct pipe_surface
*cbuf
= scene
->fb
.cbufs
[i
];
235 if (llvmpipe_resource_is_texture(cbuf
->texture
)) {
236 llvmpipe_resource_unmap(cbuf
->texture
,
238 cbuf
->u
.tex
.first_layer
);
240 scene
->cbufs
[i
].map
= NULL
;
244 /* Unmap z/stencil buffer */
245 if (scene
->zsbuf
.map
) {
246 struct pipe_surface
*zsbuf
= scene
->fb
.zsbuf
;
247 llvmpipe_resource_unmap(zsbuf
->texture
,
249 zsbuf
->u
.tex
.first_layer
);
250 scene
->zsbuf
.map
= NULL
;
253 /* Reset all command lists:
255 for (i
= 0; i
< scene
->tiles_x
; i
++) {
256 for (j
= 0; j
< scene
->tiles_y
; j
++) {
257 struct cmd_bin
*bin
= lp_scene_get_bin(scene
, i
, j
);
260 bin
->last_state
= NULL
;
264 /* If there are any bins which weren't cleared by the loop above,
265 * they will be caught (on debug builds at least) by this assert:
267 assert(lp_scene_is_empty(scene
));
269 /* Decrement texture ref counts
272 struct resource_ref
*ref
;
275 for (ref
= scene
->resources
; ref
; ref
= ref
->next
) {
276 for (i
= 0; i
< ref
->count
; i
++) {
277 if (LP_DEBUG
& DEBUG_SETUP
)
278 debug_printf("resource %d: %p %dx%d sz %d\n",
280 (void *) ref
->resource
[i
],
281 ref
->resource
[i
]->width0
,
282 ref
->resource
[i
]->height0
,
283 llvmpipe_resource_size(ref
->resource
[i
]));
285 pipe_resource_reference(&ref
->resource
[i
], NULL
);
289 if (LP_DEBUG
& DEBUG_SETUP
)
290 debug_printf("scene %d resources, sz %d\n",
291 j
, scene
->resource_reference_size
);
294 /* Decrement shader variant ref counts
297 struct shader_ref
*ref
;
300 for (ref
= scene
->frag_shaders
; ref
; ref
= ref
->next
) {
301 for (i
= 0; i
< ref
->count
; i
++) {
302 if (LP_DEBUG
& DEBUG_SETUP
)
303 debug_printf("shader %d: %p\n", j
, (void *) ref
->variant
[i
]);
305 lp_fs_variant_reference(llvmpipe_context(scene
->pipe
), &ref
->variant
[i
], NULL
);
310 /* Free all scene data blocks:
313 struct data_block_list
*list
= &scene
->data
;
314 struct data_block
*block
, *tmp
;
316 for (block
= list
->head
->next
; block
; block
= tmp
) {
321 list
->head
->next
= NULL
;
322 list
->head
->used
= 0;
325 lp_fence_reference(&scene
->fence
, NULL
);
327 scene
->resources
= NULL
;
328 scene
->frag_shaders
= NULL
;
329 scene
->scene_size
= 0;
330 scene
->resource_reference_size
= 0;
332 scene
->alloc_failed
= FALSE
;
334 util_unreference_framebuffer_state( &scene
->fb
);
343 lp_scene_new_cmd_block( struct lp_scene
*scene
,
344 struct cmd_bin
*bin
)
346 struct cmd_block
*block
= lp_scene_alloc(scene
, sizeof(struct cmd_block
));
349 bin
->tail
->next
= block
;
356 //memset(block, 0, sizeof *block);
365 lp_scene_new_data_block( struct lp_scene
*scene
)
367 if (scene
->scene_size
+ DATA_BLOCK_SIZE
> LP_SCENE_MAX_SIZE
) {
368 if (0) debug_printf("%s: failed\n", __FUNCTION__
);
369 scene
->alloc_failed
= TRUE
;
373 struct data_block
*block
= MALLOC_STRUCT(data_block
);
377 scene
->scene_size
+= sizeof *block
;
380 block
->next
= scene
->data
.head
;
381 scene
->data
.head
= block
;
389 * Return number of bytes used for all bin data within a scene.
390 * This does not include resources (textures) referenced by the scene.
393 lp_scene_data_size( const struct lp_scene
*scene
)
396 const struct data_block
*block
;
397 for (block
= scene
->data
.head
; block
; block
= block
->next
) {
406 * Add a reference to a resource by the scene.
409 lp_scene_add_resource_reference(struct lp_scene
*scene
,
410 struct pipe_resource
*resource
,
411 boolean initializing_scene
)
413 struct resource_ref
*ref
, **last
= &scene
->resources
;
416 /* Look at existing resource blocks:
418 for (ref
= scene
->resources
; ref
; ref
= ref
->next
) {
421 /* Search for this resource:
423 for (i
= 0; i
< ref
->count
; i
++)
424 if (ref
->resource
[i
] == resource
)
427 if (ref
->count
< RESOURCE_REF_SZ
) {
428 /* If the block is half-empty, then append the reference here.
434 /* Create a new block if no half-empty block was found.
437 assert(*last
== NULL
);
438 *last
= lp_scene_alloc(scene
, sizeof *ref
);
443 memset(ref
, 0, sizeof *ref
);
446 /* Append the reference to the reference block.
448 pipe_resource_reference(&ref
->resource
[ref
->count
++], resource
);
449 scene
->resource_reference_size
+= llvmpipe_resource_size(resource
);
451 /* Heuristic to advise scene flushes. This isn't helpful in the
452 * initial setup of the scene, but after that point flush on the
453 * next resource added which exceeds 64MB in referenced texture
456 if (!initializing_scene
&&
457 scene
->resource_reference_size
>= LP_SCENE_MAX_RESOURCE_SIZE
)
465 * Add a reference to a fragment shader variant
468 lp_scene_add_frag_shader_reference(struct lp_scene
*scene
,
469 struct lp_fragment_shader_variant
*variant
)
471 struct shader_ref
*ref
, **last
= &scene
->frag_shaders
;
474 /* Look at existing resource blocks:
476 for (ref
= scene
->frag_shaders
; ref
; ref
= ref
->next
) {
479 /* Search for this resource:
481 for (i
= 0; i
< ref
->count
; i
++)
482 if (ref
->variant
[i
] == variant
)
485 if (ref
->count
< SHADER_REF_SZ
) {
486 /* If the block is half-empty, then append the reference here.
492 /* Create a new block if no half-empty block was found.
495 assert(*last
== NULL
);
496 *last
= lp_scene_alloc(scene
, sizeof *ref
);
501 memset(ref
, 0, sizeof *ref
);
504 /* Append the reference to the reference block.
506 lp_fs_variant_reference(llvmpipe_context(scene
->pipe
), &ref
->variant
[ref
->count
++], variant
);
512 * Does this scene have a reference to the given resource?
515 lp_scene_is_resource_referenced(const struct lp_scene
*scene
,
516 const struct pipe_resource
*resource
)
518 const struct resource_ref
*ref
;
521 for (ref
= scene
->resources
; ref
; ref
= ref
->next
) {
522 for (i
= 0; i
< ref
->count
; i
++)
523 if (ref
->resource
[i
] == resource
)
533 /** advance curr_x,y to the next bin */
535 next_bin(struct lp_scene
*scene
)
538 if (scene
->curr_x
>= scene
->tiles_x
) {
542 if (scene
->curr_y
>= scene
->tiles_y
) {
551 lp_scene_bin_iter_begin( struct lp_scene
*scene
)
553 scene
->curr_x
= scene
->curr_y
= -1;
558 * Return pointer to next bin to be rendered.
559 * The lp_scene::curr_x and ::curr_y fields will be advanced.
560 * Multiple rendering threads will call this function to get a chunk
561 * of work (a bin) to work on.
564 lp_scene_bin_iter_next( struct lp_scene
*scene
, int *x
, int *y
)
566 struct cmd_bin
*bin
= NULL
;
568 mtx_lock(&scene
->mutex
);
570 if (scene
->curr_x
< 0) {
575 else if (!next_bin(scene
)) {
576 /* no more bins left */
580 bin
= lp_scene_get_bin(scene
, scene
->curr_x
, scene
->curr_y
);
585 /*printf("return bin %p at %d, %d\n", (void *) bin, *bin_x, *bin_y);*/
586 mtx_unlock(&scene
->mutex
);
591 void lp_scene_begin_binning(struct lp_scene
*scene
,
592 struct pipe_framebuffer_state
*fb
)
595 unsigned max_layer
= ~0;
597 assert(lp_scene_is_empty(scene
));
599 util_copy_framebuffer_state(&scene
->fb
, fb
);
601 scene
->tiles_x
= align(fb
->width
, TILE_SIZE
) / TILE_SIZE
;
602 scene
->tiles_y
= align(fb
->height
, TILE_SIZE
) / TILE_SIZE
;
603 assert(scene
->tiles_x
<= TILES_X
);
604 assert(scene
->tiles_y
<= TILES_Y
);
607 * Determine how many layers the fb has (used for clamping layer value).
608 * OpenGL (but not d3d10) permits different amount of layers per rt, however
609 * results are undefined if layer exceeds the amount of layers of ANY
610 * attachment hence don't need separate per cbuf and zsbuf max.
612 for (i
= 0; i
< scene
->fb
.nr_cbufs
; i
++) {
613 struct pipe_surface
*cbuf
= scene
->fb
.cbufs
[i
];
615 if (llvmpipe_resource_is_texture(cbuf
->texture
)) {
616 max_layer
= MIN2(max_layer
,
617 cbuf
->u
.tex
.last_layer
- cbuf
->u
.tex
.first_layer
);
625 struct pipe_surface
*zsbuf
= scene
->fb
.zsbuf
;
626 max_layer
= MIN2(max_layer
, zsbuf
->u
.tex
.last_layer
- zsbuf
->u
.tex
.first_layer
);
628 scene
->fb_max_layer
= max_layer
;
629 scene
->fb_max_samples
= util_framebuffer_get_num_samples(fb
);
630 if (scene
->fb_max_samples
== 4) {
631 for (unsigned i
= 0; i
< 4; i
++) {
632 scene
->fixed_sample_pos
[i
][0] = util_iround(lp_sample_pos_4x
[i
][0] * FIXED_ONE
);
633 scene
->fixed_sample_pos
[i
][1] = util_iround(lp_sample_pos_4x
[i
][1] * FIXED_ONE
);
639 void lp_scene_end_binning( struct lp_scene
*scene
)
641 if (LP_DEBUG
& DEBUG_SCENE
) {
642 debug_printf("rasterize scene:\n");
643 debug_printf(" scene_size: %u\n",
645 debug_printf(" data size: %u\n",
646 lp_scene_data_size(scene
));
649 lp_debug_bins( scene
);