f6c6941507177ddd567137fcf6f919f67a3cfa90
[mesa.git] / src / gallium / drivers / llvmpipe / lp_scene.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
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"
34 #include "lp_scene.h"
35 #include "lp_fence.h"
36 #include "lp_debug.h"
37
38
39 #define RESOURCE_REF_SZ 32
40
41 /** List of resource references */
42 struct resource_ref {
43 struct pipe_resource *resource[RESOURCE_REF_SZ];
44 int count;
45 struct resource_ref *next;
46 };
47
48
49 /**
50 * Create a new scene object.
51 * \param queue the queue to put newly rendered/emptied scenes into
52 */
53 struct lp_scene *
54 lp_scene_create( struct pipe_context *pipe )
55 {
56 struct lp_scene *scene = CALLOC_STRUCT(lp_scene);
57 if (!scene)
58 return NULL;
59
60 scene->pipe = pipe;
61 scene->data.head = &scene->data.first;
62
63 pipe_mutex_init(scene->mutex);
64
65 return scene;
66 }
67
68
69 /**
70 * Free all data associated with the given scene, and the scene itself.
71 */
72 void
73 lp_scene_destroy(struct lp_scene *scene)
74 {
75 pipe_mutex_destroy(scene->mutex);
76 assert(scene->data.head == &scene->data.first);
77 FREE(scene);
78 }
79
80
81 /**
82 * Check if the scene's bins are all empty.
83 * For debugging purposes.
84 */
85 boolean
86 lp_scene_is_empty(struct lp_scene *scene )
87 {
88 unsigned x, y;
89
90 for (y = 0; y < TILES_Y; y++) {
91 for (x = 0; x < TILES_X; x++) {
92 const struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
93 if (bin->head) {
94 return FALSE;
95 }
96 }
97 }
98 return TRUE;
99 }
100
101
102 /* Returns true if there has ever been a failed allocation attempt in
103 * this scene. Used in triangle emit to avoid having to check success
104 * at each bin.
105 */
106 boolean
107 lp_scene_is_oom(struct lp_scene *scene)
108 {
109 return scene->alloc_failed;
110 }
111
112
113 /* Remove all commands from a bin. Tries to reuse some of the memory
114 * allocated to the bin, however.
115 */
116 void
117 lp_scene_bin_reset(struct lp_scene *scene, unsigned x, unsigned y)
118 {
119 struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
120
121 bin->head = bin->tail;
122 if (bin->tail) {
123 bin->tail->next = NULL;
124 bin->tail->count = 0;
125 }
126 }
127
128
129 void
130 lp_scene_begin_rasterization(struct lp_scene *scene)
131 {
132 const struct pipe_framebuffer_state *fb = &scene->fb;
133 int i;
134
135 //LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
136
137 for (i = 0; i < scene->fb.nr_cbufs; i++) {
138 struct pipe_surface *cbuf = scene->fb.cbufs[i];
139 scene->cbufs[i].stride = llvmpipe_resource_stride(cbuf->texture,
140 cbuf->level);
141
142 scene->cbufs[i].map = llvmpipe_resource_map(cbuf->texture,
143 cbuf->face,
144 cbuf->level,
145 cbuf->zslice,
146 LP_TEX_USAGE_READ_WRITE,
147 LP_TEX_LAYOUT_LINEAR);
148 }
149
150 if (fb->zsbuf) {
151 struct pipe_surface *zsbuf = scene->fb.zsbuf;
152 scene->zsbuf.stride = llvmpipe_resource_stride(zsbuf->texture, zsbuf->level);
153 scene->zsbuf.blocksize =
154 util_format_get_blocksize(zsbuf->texture->format);
155
156 scene->zsbuf.map = llvmpipe_resource_map(zsbuf->texture,
157 zsbuf->face,
158 zsbuf->level,
159 zsbuf->zslice,
160 LP_TEX_USAGE_READ_WRITE,
161 LP_TEX_LAYOUT_NONE);
162 }
163 }
164
165
166
167
168 /**
169 * Free all the temporary data in a scene.
170 */
171 void
172 lp_scene_end_rasterization(struct lp_scene *scene )
173 {
174 int i, j;
175
176 /* Unmap color buffers */
177 for (i = 0; i < scene->fb.nr_cbufs; i++) {
178 if (scene->cbufs[i].map) {
179 struct pipe_surface *cbuf = scene->fb.cbufs[i];
180 llvmpipe_resource_unmap(cbuf->texture,
181 cbuf->face,
182 cbuf->level,
183 cbuf->zslice);
184 scene->cbufs[i].map = NULL;
185 }
186 }
187
188 /* Unmap z/stencil buffer */
189 if (scene->zsbuf.map) {
190 struct pipe_surface *zsbuf = scene->fb.zsbuf;
191 llvmpipe_resource_unmap(zsbuf->texture,
192 zsbuf->face,
193 zsbuf->level,
194 zsbuf->zslice);
195 scene->zsbuf.map = NULL;
196 }
197
198 /* Reset all command lists:
199 */
200 for (i = 0; i < scene->tiles_x; i++) {
201 for (j = 0; j < scene->tiles_y; j++) {
202 struct cmd_bin *bin = lp_scene_get_bin(scene, i, j);
203 bin->head = bin->tail = NULL;
204 }
205 }
206
207 /* If there are any bins which weren't cleared by the loop above,
208 * they will be caught (on debug builds at least) by this assert:
209 */
210 assert(lp_scene_is_empty(scene));
211
212 /* Decrement texture ref counts
213 */
214 {
215 struct resource_ref *ref;
216 int i, j = 0;
217
218 for (ref = scene->resources; ref; ref = ref->next) {
219 for (i = 0; i < ref->count; i++) {
220 if (LP_DEBUG & DEBUG_SETUP)
221 debug_printf("resource %d: %p %dx%d sz %d\n",
222 j,
223 ref->resource[i],
224 ref->resource[i]->width0,
225 ref->resource[i]->height0,
226 llvmpipe_resource_size(ref->resource[i]));
227 j++;
228 pipe_resource_reference(&ref->resource[i], NULL);
229 }
230 }
231
232 if (LP_DEBUG & DEBUG_SETUP)
233 debug_printf("scene %d resources, sz %d\n",
234 j, scene->resource_reference_size);
235 }
236
237 /* Free all scene data blocks:
238 */
239 {
240 struct data_block_list *list = &scene->data;
241 struct data_block *block, *tmp;
242
243 for (block = list->head; block; block = tmp) {
244 tmp = block->next;
245 if (block != &list->first)
246 FREE(block);
247 }
248
249 list->head = &list->first;
250 list->head->next = NULL;
251 }
252
253 lp_fence_reference(&scene->fence, NULL);
254
255 scene->resources = NULL;
256 scene->scene_size = 0;
257 scene->resource_reference_size = 0;
258
259 scene->has_depthstencil_clear = FALSE;
260 scene->alloc_failed = FALSE;
261
262 util_unreference_framebuffer_state( &scene->fb );
263 }
264
265
266
267
268
269
270 struct cmd_block *
271 lp_scene_new_cmd_block( struct lp_scene *scene,
272 struct cmd_bin *bin )
273 {
274 struct cmd_block *block = lp_scene_alloc(scene, sizeof(struct cmd_block));
275 if (block) {
276 if (bin->tail) {
277 bin->tail->next = block;
278 bin->tail = block;
279 }
280 else {
281 bin->head = block;
282 bin->tail = block;
283 }
284 //memset(block, 0, sizeof *block);
285 block->next = NULL;
286 block->count = 0;
287 }
288 return block;
289 }
290
291
292 struct data_block *
293 lp_scene_new_data_block( struct lp_scene *scene )
294 {
295 if (scene->scene_size + DATA_BLOCK_SIZE > LP_SCENE_MAX_SIZE) {
296 if (0) debug_printf("%s: failed\n", __FUNCTION__);
297 scene->alloc_failed = TRUE;
298 return NULL;
299 }
300 else {
301 struct data_block *block = MALLOC_STRUCT(data_block);
302 if (block == NULL)
303 return NULL;
304
305 scene->scene_size += sizeof *block;
306
307 block->used = 0;
308 block->next = scene->data.head;
309 scene->data.head = block;
310
311 return block;
312 }
313 }
314
315
316 /**
317 * Return number of bytes used for all bin data within a scene.
318 * This does not include resources (textures) referenced by the scene.
319 */
320 static unsigned
321 lp_scene_data_size( const struct lp_scene *scene )
322 {
323 unsigned size = 0;
324 const struct data_block *block;
325 for (block = scene->data.head; block; block = block->next) {
326 size += block->used;
327 }
328 return size;
329 }
330
331
332
333 /**
334 * Add a reference to a resource by the scene.
335 */
336 boolean
337 lp_scene_add_resource_reference(struct lp_scene *scene,
338 struct pipe_resource *resource,
339 boolean initializing_scene)
340 {
341 struct resource_ref *ref, **last = &scene->resources;
342 int i;
343
344 /* Look at existing resource blocks:
345 */
346 for (ref = scene->resources; ref; ref = ref->next) {
347 last = &ref->next;
348
349 /* Search for this resource:
350 */
351 for (i = 0; i < ref->count; i++)
352 if (ref->resource[i] == resource)
353 return TRUE;
354
355 if (ref->count < RESOURCE_REF_SZ) {
356 /* If the block is half-empty, then append the reference here.
357 */
358 break;
359 }
360 }
361
362 /* Create a new block if no half-empty block was found.
363 */
364 if (!ref) {
365 assert(*last == NULL);
366 *last = lp_scene_alloc(scene, sizeof *ref);
367 if (*last == NULL)
368 return FALSE;
369
370 ref = *last;
371 memset(ref, 0, sizeof *ref);
372 }
373
374 /* Append the reference to the reference block.
375 */
376 pipe_resource_reference(&ref->resource[ref->count++], resource);
377 scene->resource_reference_size += llvmpipe_resource_size(resource);
378
379 /* Heuristic to advise scene flushes. This isn't helpful in the
380 * initial setup of the scene, but after that point flush on the
381 * next resource added which exceeds 64MB in referenced texture
382 * data.
383 */
384 if (!initializing_scene &&
385 scene->resource_reference_size >= LP_SCENE_MAX_RESOURCE_SIZE)
386 return FALSE;
387
388 return TRUE;
389 }
390
391
392 /**
393 * Does this scene have a reference to the given resource?
394 */
395 boolean
396 lp_scene_is_resource_referenced(const struct lp_scene *scene,
397 const struct pipe_resource *resource)
398 {
399 const struct resource_ref *ref;
400 int i;
401
402 for (ref = scene->resources; ref; ref = ref->next) {
403 for (i = 0; i < ref->count; i++)
404 if (ref->resource[i] == resource)
405 return TRUE;
406 }
407
408 return FALSE;
409 }
410
411
412
413
414 /** advance curr_x,y to the next bin */
415 static boolean
416 next_bin(struct lp_scene *scene)
417 {
418 scene->curr_x++;
419 if (scene->curr_x >= scene->tiles_x) {
420 scene->curr_x = 0;
421 scene->curr_y++;
422 }
423 if (scene->curr_y >= scene->tiles_y) {
424 /* no more bins */
425 return FALSE;
426 }
427 return TRUE;
428 }
429
430
431 void
432 lp_scene_bin_iter_begin( struct lp_scene *scene )
433 {
434 scene->curr_x = scene->curr_y = -1;
435 }
436
437
438 /**
439 * Return pointer to next bin to be rendered.
440 * The lp_scene::curr_x and ::curr_y fields will be advanced.
441 * Multiple rendering threads will call this function to get a chunk
442 * of work (a bin) to work on.
443 */
444 struct cmd_bin *
445 lp_scene_bin_iter_next( struct lp_scene *scene )
446 {
447 struct cmd_bin *bin = NULL;
448
449 pipe_mutex_lock(scene->mutex);
450
451 if (scene->curr_x < 0) {
452 /* first bin */
453 scene->curr_x = 0;
454 scene->curr_y = 0;
455 }
456 else if (!next_bin(scene)) {
457 /* no more bins left */
458 goto end;
459 }
460
461 bin = lp_scene_get_bin(scene, scene->curr_x, scene->curr_y);
462
463 end:
464 /*printf("return bin %p at %d, %d\n", (void *) bin, *bin_x, *bin_y);*/
465 pipe_mutex_unlock(scene->mutex);
466 return bin;
467 }
468
469
470 void lp_scene_begin_binning( struct lp_scene *scene,
471 struct pipe_framebuffer_state *fb )
472 {
473 assert(lp_scene_is_empty(scene));
474
475 util_copy_framebuffer_state(&scene->fb, fb);
476
477 scene->tiles_x = align(fb->width, TILE_SIZE) / TILE_SIZE;
478 scene->tiles_y = align(fb->height, TILE_SIZE) / TILE_SIZE;
479
480 assert(scene->tiles_x <= TILES_X);
481 assert(scene->tiles_y <= TILES_Y);
482 }
483
484
485 void lp_scene_end_binning( struct lp_scene *scene )
486 {
487 if (LP_DEBUG & DEBUG_SCENE) {
488 debug_printf("rasterize scene:\n");
489 debug_printf(" scene_size: %u\n",
490 scene->scene_size);
491 debug_printf(" data size: %u\n",
492 lp_scene_data_size(scene));
493
494 if (0)
495 lp_debug_bins( scene );
496 }
497 }