OSDN Git Service

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