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