• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/simple_list.h"
33 #include "util/format/u_format.h"
34 #include "lp_scene.h"
35 #include "lp_fence.h"
36 #include "lp_debug.h"
37 #include "lp_context.h"
38 #include "lp_state_fs.h"
39 
40 
41 #define RESOURCE_REF_SZ 32
42 
43 /** List of resource references */
44 struct resource_ref {
45    struct pipe_resource *resource[RESOURCE_REF_SZ];
46    int count;
47    struct resource_ref *next;
48 };
49 
50 #define SHADER_REF_SZ 32
51 /** List of shader variant references */
52 struct shader_ref {
53    struct lp_fragment_shader_variant *variant[SHADER_REF_SZ];
54    int count;
55    struct shader_ref *next;
56 };
57 
58 
59 /**
60  * Create a new scene object.
61  * \param queue  the queue to put newly rendered/emptied scenes into
62  */
63 struct lp_scene *
lp_scene_create(struct pipe_context * pipe)64 lp_scene_create( struct pipe_context *pipe )
65 {
66    struct lp_scene *scene = CALLOC_STRUCT(lp_scene);
67    if (!scene)
68       return NULL;
69 
70    scene->pipe = pipe;
71 
72    scene->data.head =
73       CALLOC_STRUCT(data_block);
74 
75    (void) mtx_init(&scene->mutex, mtx_plain);
76 
77 #ifdef DEBUG
78    /* Do some scene limit sanity checks here */
79    {
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.
85        */
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);
89    }
90 #endif
91 
92    return scene;
93 }
94 
95 
96 /**
97  * Free all data associated with the given scene, and the scene itself.
98  */
99 void
lp_scene_destroy(struct lp_scene * scene)100 lp_scene_destroy(struct lp_scene *scene)
101 {
102    lp_fence_reference(&scene->fence, NULL);
103    mtx_destroy(&scene->mutex);
104    assert(scene->data.head->next == NULL);
105    FREE(scene->data.head);
106    FREE(scene);
107 }
108 
109 
110 /**
111  * Check if the scene's bins are all empty.
112  * For debugging purposes.
113  */
114 boolean
lp_scene_is_empty(struct lp_scene * scene)115 lp_scene_is_empty(struct lp_scene *scene )
116 {
117    unsigned x, y;
118 
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);
122          if (bin->head) {
123             return FALSE;
124          }
125       }
126    }
127    return TRUE;
128 }
129 
130 
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
133  * at each bin.
134  */
135 boolean
lp_scene_is_oom(struct lp_scene * scene)136 lp_scene_is_oom(struct lp_scene *scene)
137 {
138    return scene->alloc_failed;
139 }
140 
141 
142 /* Remove all commands from a bin.  Tries to reuse some of the memory
143  * allocated to the bin, however.
144  */
145 void
lp_scene_bin_reset(struct lp_scene * scene,unsigned x,unsigned y)146 lp_scene_bin_reset(struct lp_scene *scene, unsigned x, unsigned y)
147 {
148    struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
149 
150    bin->last_state = NULL;
151    bin->head = bin->tail;
152    if (bin->tail) {
153       bin->tail->next = NULL;
154       bin->tail->count = 0;
155    }
156 }
157 
158 
159 void
lp_scene_begin_rasterization(struct lp_scene * scene)160 lp_scene_begin_rasterization(struct lp_scene *scene)
161 {
162    const struct pipe_framebuffer_state *fb = &scene->fb;
163    int i;
164 
165    //LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
166 
167    for (i = 0; i < scene->fb.nr_cbufs; i++) {
168       struct pipe_surface *cbuf = scene->fb.cbufs[i];
169 
170       if (!cbuf) {
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;
176          continue;
177       }
178 
179       if (llvmpipe_resource_is_texture(cbuf->texture)) {
180          scene->cbufs[i].stride = llvmpipe_resource_stride(cbuf->texture,
181                                                            cbuf->u.tex.level);
182          scene->cbufs[i].layer_stride = llvmpipe_layer_stride(cbuf->texture,
183                                                               cbuf->u.tex.level);
184          scene->cbufs[i].sample_stride = llvmpipe_sample_stride(cbuf->texture);
185 
186          scene->cbufs[i].map = llvmpipe_resource_map(cbuf->texture,
187                                                      cbuf->u.tex.level,
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);
192       }
193       else {
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);
203       }
204    }
205 
206    if (fb->zsbuf) {
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,
213                                                zsbuf->u.tex.level,
214                                                zsbuf->u.tex.first_layer,
215                                                LP_TEX_USAGE_READ_WRITE);
216       scene->zsbuf.format_bytes = util_format_get_blocksize(zsbuf->format);
217    }
218 }
219 
220 
221 
222 
223 /**
224  * Free all the temporary data in a scene.
225  */
226 void
lp_scene_end_rasterization(struct lp_scene * scene)227 lp_scene_end_rasterization(struct lp_scene *scene )
228 {
229    int i, j;
230 
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,
237                                     cbuf->u.tex.level,
238                                     cbuf->u.tex.first_layer);
239          }
240          scene->cbufs[i].map = NULL;
241       }
242    }
243 
244    /* Unmap z/stencil buffer */
245    if (scene->zsbuf.map) {
246       struct pipe_surface *zsbuf = scene->fb.zsbuf;
247       llvmpipe_resource_unmap(zsbuf->texture,
248                               zsbuf->u.tex.level,
249                               zsbuf->u.tex.first_layer);
250       scene->zsbuf.map = NULL;
251    }
252 
253    /* Reset all command lists:
254     */
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);
258          bin->head = NULL;
259          bin->tail = NULL;
260          bin->last_state = NULL;
261       }
262    }
263 
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:
266     */
267    assert(lp_scene_is_empty(scene));
268 
269    /* Decrement texture ref counts
270     */
271    {
272       struct resource_ref *ref;
273       int i, j = 0;
274 
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",
279                             j,
280                             (void *) ref->resource[i],
281                             ref->resource[i]->width0,
282                             ref->resource[i]->height0,
283                             llvmpipe_resource_size(ref->resource[i]));
284             j++;
285             pipe_resource_reference(&ref->resource[i], NULL);
286          }
287       }
288 
289       if (LP_DEBUG & DEBUG_SETUP)
290          debug_printf("scene %d resources, sz %d\n",
291                       j, scene->resource_reference_size);
292    }
293 
294    /* Decrement shader variant ref counts
295     */
296    {
297       struct shader_ref *ref;
298       int i, j = 0;
299 
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]);
304             j++;
305             lp_fs_variant_reference(llvmpipe_context(scene->pipe), &ref->variant[i], NULL);
306          }
307       }
308    }
309 
310    /* Free all scene data blocks:
311     */
312    {
313       struct data_block_list *list = &scene->data;
314       struct data_block *block, *tmp;
315 
316       for (block = list->head->next; block; block = tmp) {
317          tmp = block->next;
318 	 FREE(block);
319       }
320 
321       list->head->next = NULL;
322       list->head->used = 0;
323    }
324 
325    lp_fence_reference(&scene->fence, NULL);
326 
327    scene->resources = NULL;
328    scene->frag_shaders = NULL;
329    scene->scene_size = 0;
330    scene->resource_reference_size = 0;
331 
332    scene->alloc_failed = FALSE;
333 
334    util_unreference_framebuffer_state( &scene->fb );
335 }
336 
337 
338 
339 
340 
341 
342 struct cmd_block *
lp_scene_new_cmd_block(struct lp_scene * scene,struct cmd_bin * bin)343 lp_scene_new_cmd_block( struct lp_scene *scene,
344                         struct cmd_bin *bin )
345 {
346    struct cmd_block *block = lp_scene_alloc(scene, sizeof(struct cmd_block));
347    if (block) {
348       if (bin->tail) {
349          bin->tail->next = block;
350          bin->tail = block;
351       }
352       else {
353          bin->head = block;
354          bin->tail = block;
355       }
356       //memset(block, 0, sizeof *block);
357       block->next = NULL;
358       block->count = 0;
359    }
360    return block;
361 }
362 
363 
364 struct data_block *
lp_scene_new_data_block(struct lp_scene * scene)365 lp_scene_new_data_block( struct lp_scene *scene )
366 {
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;
370       return NULL;
371    }
372    else {
373       struct data_block *block = MALLOC_STRUCT(data_block);
374       if (!block)
375          return NULL;
376 
377       scene->scene_size += sizeof *block;
378 
379       block->used = 0;
380       block->next = scene->data.head;
381       scene->data.head = block;
382 
383       return block;
384    }
385 }
386 
387 
388 /**
389  * Return number of bytes used for all bin data within a scene.
390  * This does not include resources (textures) referenced by the scene.
391  */
392 static unsigned
lp_scene_data_size(const struct lp_scene * scene)393 lp_scene_data_size( const struct lp_scene *scene )
394 {
395    unsigned size = 0;
396    const struct data_block *block;
397    for (block = scene->data.head; block; block = block->next) {
398       size += block->used;
399    }
400    return size;
401 }
402 
403 
404 
405 /**
406  * Add a reference to a resource by the scene.
407  */
408 boolean
lp_scene_add_resource_reference(struct lp_scene * scene,struct pipe_resource * resource,boolean initializing_scene)409 lp_scene_add_resource_reference(struct lp_scene *scene,
410                                 struct pipe_resource *resource,
411                                 boolean initializing_scene)
412 {
413    struct resource_ref *ref, **last = &scene->resources;
414    int i;
415 
416    /* Look at existing resource blocks:
417     */
418    for (ref = scene->resources; ref; ref = ref->next) {
419       last = &ref->next;
420 
421       /* Search for this resource:
422        */
423       for (i = 0; i < ref->count; i++)
424          if (ref->resource[i] == resource)
425             return TRUE;
426 
427       if (ref->count < RESOURCE_REF_SZ) {
428          /* If the block is half-empty, then append the reference here.
429           */
430          break;
431       }
432    }
433 
434    /* Create a new block if no half-empty block was found.
435     */
436    if (!ref) {
437       assert(*last == NULL);
438       *last = lp_scene_alloc(scene, sizeof *ref);
439       if (*last == NULL)
440           return FALSE;
441 
442       ref = *last;
443       memset(ref, 0, sizeof *ref);
444    }
445 
446    /* Append the reference to the reference block.
447     */
448    pipe_resource_reference(&ref->resource[ref->count++], resource);
449    scene->resource_reference_size += llvmpipe_resource_size(resource);
450 
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
454     * data.
455     */
456    if (!initializing_scene &&
457        scene->resource_reference_size >= LP_SCENE_MAX_RESOURCE_SIZE)
458       return FALSE;
459 
460    return TRUE;
461 }
462 
463 
464 /**
465  * Add a reference to a fragment shader variant
466  */
467 boolean
lp_scene_add_frag_shader_reference(struct lp_scene * scene,struct lp_fragment_shader_variant * variant)468 lp_scene_add_frag_shader_reference(struct lp_scene *scene,
469                                    struct lp_fragment_shader_variant *variant)
470 {
471    struct shader_ref *ref, **last = &scene->frag_shaders;
472    int i;
473 
474    /* Look at existing resource blocks:
475     */
476    for (ref = scene->frag_shaders; ref; ref = ref->next) {
477       last = &ref->next;
478 
479       /* Search for this resource:
480        */
481       for (i = 0; i < ref->count; i++)
482          if (ref->variant[i] == variant)
483             return TRUE;
484 
485       if (ref->count < SHADER_REF_SZ) {
486          /* If the block is half-empty, then append the reference here.
487           */
488          break;
489       }
490    }
491 
492    /* Create a new block if no half-empty block was found.
493     */
494    if (!ref) {
495       assert(*last == NULL);
496       *last = lp_scene_alloc(scene, sizeof *ref);
497       if (*last == NULL)
498           return FALSE;
499 
500       ref = *last;
501       memset(ref, 0, sizeof *ref);
502    }
503 
504    /* Append the reference to the reference block.
505     */
506    lp_fs_variant_reference(llvmpipe_context(scene->pipe), &ref->variant[ref->count++], variant);
507 
508    return TRUE;
509 }
510 
511 /**
512  * Does this scene have a reference to the given resource?
513  */
514 boolean
lp_scene_is_resource_referenced(const struct lp_scene * scene,const struct pipe_resource * resource)515 lp_scene_is_resource_referenced(const struct lp_scene *scene,
516                                 const struct pipe_resource *resource)
517 {
518    const struct resource_ref *ref;
519    int i;
520 
521    for (ref = scene->resources; ref; ref = ref->next) {
522       for (i = 0; i < ref->count; i++)
523          if (ref->resource[i] == resource)
524             return TRUE;
525    }
526 
527    return FALSE;
528 }
529 
530 
531 
532 
533 /** advance curr_x,y to the next bin */
534 static boolean
next_bin(struct lp_scene * scene)535 next_bin(struct lp_scene *scene)
536 {
537    scene->curr_x++;
538    if (scene->curr_x >= scene->tiles_x) {
539       scene->curr_x = 0;
540       scene->curr_y++;
541    }
542    if (scene->curr_y >= scene->tiles_y) {
543       /* no more bins */
544       return FALSE;
545    }
546    return TRUE;
547 }
548 
549 
550 void
lp_scene_bin_iter_begin(struct lp_scene * scene)551 lp_scene_bin_iter_begin( struct lp_scene *scene )
552 {
553    scene->curr_x = scene->curr_y = -1;
554 }
555 
556 
557 /**
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.
562  */
563 struct cmd_bin *
lp_scene_bin_iter_next(struct lp_scene * scene,int * x,int * y)564 lp_scene_bin_iter_next( struct lp_scene *scene , int *x, int *y)
565 {
566    struct cmd_bin *bin = NULL;
567 
568    mtx_lock(&scene->mutex);
569 
570    if (scene->curr_x < 0) {
571       /* first bin */
572       scene->curr_x = 0;
573       scene->curr_y = 0;
574    }
575    else if (!next_bin(scene)) {
576       /* no more bins left */
577       goto end;
578    }
579 
580    bin = lp_scene_get_bin(scene, scene->curr_x, scene->curr_y);
581    *x = scene->curr_x;
582    *y = scene->curr_y;
583 
584 end:
585    /*printf("return bin %p at %d, %d\n", (void *) bin, *bin_x, *bin_y);*/
586    mtx_unlock(&scene->mutex);
587    return bin;
588 }
589 
590 
lp_scene_begin_binning(struct lp_scene * scene,struct pipe_framebuffer_state * fb)591 void lp_scene_begin_binning(struct lp_scene *scene,
592                             struct pipe_framebuffer_state *fb)
593 {
594    int i;
595    unsigned max_layer = ~0;
596 
597    assert(lp_scene_is_empty(scene));
598 
599    util_copy_framebuffer_state(&scene->fb, fb);
600 
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);
605 
606    /*
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.
611     */
612    for (i = 0; i < scene->fb.nr_cbufs; i++) {
613       struct pipe_surface *cbuf = scene->fb.cbufs[i];
614       if (cbuf) {
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);
618          }
619          else {
620             max_layer = 0;
621          }
622       }
623    }
624    if (fb->zsbuf) {
625       struct pipe_surface *zsbuf = scene->fb.zsbuf;
626       max_layer = MIN2(max_layer, zsbuf->u.tex.last_layer - zsbuf->u.tex.first_layer);
627    }
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);
634       }
635    }
636 }
637 
638 
lp_scene_end_binning(struct lp_scene * scene)639 void lp_scene_end_binning( struct lp_scene *scene )
640 {
641    if (LP_DEBUG & DEBUG_SCENE) {
642       debug_printf("rasterize scene:\n");
643       debug_printf("  scene_size: %u\n",
644                    scene->scene_size);
645       debug_printf("  data size: %u\n",
646                    lp_scene_data_size(scene));
647 
648       if (0)
649          lp_debug_bins( scene );
650    }
651 }
652