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/reallocarray.h"
32#include "util/u_inlines.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#include "lp_setup_context.h"
41
42#define RESOURCE_REF_SZ 32
43
44/** List of resource references */
45struct resource_ref {
46   struct pipe_resource *resource[RESOURCE_REF_SZ];
47   int count;
48   struct resource_ref *next;
49};
50
51#define SHADER_REF_SZ 32
52/** List of shader variant references */
53struct shader_ref {
54   struct lp_fragment_shader_variant *variant[SHADER_REF_SZ];
55   int count;
56   struct shader_ref *next;
57};
58
59
60/**
61 * Create a new scene object.
62 * \param queue  the queue to put newly rendered/emptied scenes into
63 */
64struct lp_scene *
65lp_scene_create(struct lp_setup_context *setup)
66{
67   struct lp_scene *scene = slab_alloc_st(&setup->scene_slab);
68   if (!scene)
69      return NULL;
70
71   memset(scene, 0, sizeof(struct lp_scene));
72   scene->pipe = setup->pipe;
73   scene->setup = setup;
74   scene->data.head = &scene->data.first;
75
76   (void) mtx_init(&scene->mutex, mtx_plain);
77
78#ifdef DEBUG
79   /* Do some scene limit sanity checks here */
80   {
81      size_t maxBins = TILES_X * TILES_Y;
82      size_t maxCommandBytes = sizeof(struct cmd_block) * maxBins;
83      size_t maxCommandPlusData = maxCommandBytes + DATA_BLOCK_SIZE;
84      /* We'll need at least one command block per bin.  Make sure that's
85       * less than the max allowed scene size.
86       */
87      assert(maxCommandBytes < LP_SCENE_MAX_SIZE);
88      /* We'll also need space for at least one other data block */
89      assert(maxCommandPlusData <= LP_SCENE_MAX_SIZE);
90   }
91#endif
92
93   return scene;
94}
95
96
97/**
98 * Free all data associated with the given scene, and the scene itself.
99 */
100void
101lp_scene_destroy(struct lp_scene *scene)
102{
103   lp_scene_end_rasterization(scene);
104   mtx_destroy(&scene->mutex);
105   free(scene->tiles);
106   assert(scene->data.head == &scene->data.first);
107   slab_free_st(&scene->setup->scene_slab, scene);
108}
109
110
111/**
112 * Check if the scene's bins are all empty.
113 * For debugging purposes.
114 */
115boolean
116lp_scene_is_empty(struct lp_scene *scene )
117{
118   unsigned x, y;
119
120   for (y = 0; y < scene->tiles_y; y++) {
121      for (x = 0; x < scene->tiles_x; x++) {
122         const struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
123         if (bin->head) {
124            return FALSE;
125         }
126      }
127   }
128   return TRUE;
129}
130
131
132/* Returns true if there has ever been a failed allocation attempt in
133 * this scene.  Used in triangle/rectangle emit to avoid having to
134 * check success at each bin.
135 */
136boolean
137lp_scene_is_oom(struct lp_scene *scene)
138{
139   return scene->alloc_failed;
140}
141
142
143/* Remove all commands from a bin.  Tries to reuse some of the memory
144 * allocated to the bin, however.
145 */
146void
147lp_scene_bin_reset(struct lp_scene *scene, unsigned x, unsigned y)
148{
149   struct cmd_bin *bin = lp_scene_get_bin(scene, x, y);
150
151   bin->last_state = NULL;
152   bin->head = bin->tail;
153   if (bin->tail) {
154      bin->tail->next = NULL;
155      bin->tail->count = 0;
156   }
157}
158
159static void
160init_scene_texture(struct lp_scene_surface *ssurf, struct pipe_surface *psurf)
161{
162   if (!psurf) {
163      ssurf->stride = 0;
164      ssurf->layer_stride = 0;
165      ssurf->sample_stride = 0;
166      ssurf->nr_samples = 0;
167      ssurf->map = NULL;
168      return;
169   }
170
171   if (llvmpipe_resource_is_texture(psurf->texture)) {
172      ssurf->stride = llvmpipe_resource_stride(psurf->texture,
173                                               psurf->u.tex.level);
174      ssurf->layer_stride = llvmpipe_layer_stride(psurf->texture,
175                                                           psurf->u.tex.level);
176      ssurf->sample_stride = llvmpipe_sample_stride(psurf->texture);
177
178      ssurf->map = llvmpipe_resource_map(psurf->texture,
179                                         psurf->u.tex.level,
180                                         psurf->u.tex.first_layer,
181                                         LP_TEX_USAGE_READ_WRITE);
182      ssurf->format_bytes = util_format_get_blocksize(psurf->format);
183      ssurf->nr_samples = util_res_sample_count(psurf->texture);
184   }
185   else {
186      struct llvmpipe_resource *lpr = llvmpipe_resource(psurf->texture);
187      unsigned pixstride = util_format_get_blocksize(psurf->format);
188      ssurf->stride = psurf->texture->width0;
189      ssurf->layer_stride = 0;
190      ssurf->sample_stride = 0;
191      ssurf->nr_samples = 1;
192      ssurf->map = lpr->data;
193      ssurf->map += psurf->u.buf.first_element * pixstride;
194      ssurf->format_bytes = util_format_get_blocksize(psurf->format);
195   }
196}
197
198void
199lp_scene_begin_rasterization(struct lp_scene *scene)
200{
201   const struct pipe_framebuffer_state *fb = &scene->fb;
202   int i;
203
204   //LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);
205
206   for (i = 0; i < scene->fb.nr_cbufs; i++) {
207      struct pipe_surface *cbuf = scene->fb.cbufs[i];
208      init_scene_texture(&scene->cbufs[i], cbuf);
209   }
210
211   if (fb->zsbuf) {
212      struct pipe_surface *zsbuf = scene->fb.zsbuf;
213      init_scene_texture(&scene->zsbuf, zsbuf);
214   }
215}
216
217
218/**
219 * Free all the temporary data in a scene.
220 */
221void
222lp_scene_end_rasterization(struct lp_scene *scene )
223{
224   int i;
225
226   /* Unmap color buffers */
227   for (i = 0; i < scene->fb.nr_cbufs; i++) {
228      if (scene->cbufs[i].map) {
229         struct pipe_surface *cbuf = scene->fb.cbufs[i];
230         if (llvmpipe_resource_is_texture(cbuf->texture)) {
231            llvmpipe_resource_unmap(cbuf->texture,
232                                    cbuf->u.tex.level,
233                                    cbuf->u.tex.first_layer);
234         }
235         scene->cbufs[i].map = NULL;
236      }
237   }
238
239   /* Unmap z/stencil buffer */
240   if (scene->zsbuf.map) {
241      struct pipe_surface *zsbuf = scene->fb.zsbuf;
242      llvmpipe_resource_unmap(zsbuf->texture,
243                              zsbuf->u.tex.level,
244                              zsbuf->u.tex.first_layer);
245      scene->zsbuf.map = NULL;
246   }
247
248   /* Reset all command lists:
249    */
250   memset(scene->tiles, 0, sizeof(struct cmd_bin) * scene->num_alloced_tiles);
251
252   /* Decrement texture ref counts
253    */
254   int j = 0;
255   for (struct resource_ref *ref = scene->resources; ref; ref = ref->next) {
256      for (int i = 0; i < ref->count; i++) {
257         if (LP_DEBUG & DEBUG_SETUP)
258            debug_printf("resource %d: %p %dx%d sz %d\n",
259                         j,
260                         (void *) ref->resource[i],
261                         ref->resource[i]->width0,
262                         ref->resource[i]->height0,
263                         llvmpipe_resource_size(ref->resource[i]));
264         j++;
265         llvmpipe_resource_unmap(ref->resource[i], 0, 0);
266         pipe_resource_reference(&ref->resource[i], NULL);
267      }
268   }
269
270   for (struct resource_ref *ref = scene->writeable_resources; ref;
271        ref = ref->next) {
272      for (int i = 0; i < ref->count; i++) {
273         if (LP_DEBUG & DEBUG_SETUP)
274            debug_printf("resource %d: %p %dx%d sz %d\n",
275                         j,
276                         (void *) ref->resource[i],
277                         ref->resource[i]->width0,
278                            ref->resource[i]->height0,
279                         llvmpipe_resource_size(ref->resource[i]));
280         j++;
281         llvmpipe_resource_unmap(ref->resource[i], 0, 0);
282         pipe_resource_reference(&ref->resource[i], NULL);
283      }
284   }
285
286   if (LP_DEBUG & DEBUG_SETUP) {
287      debug_printf("scene %d resources, sz %d\n",
288                   j, scene->resource_reference_size);
289   }
290
291   /* Decrement shader variant ref counts
292    */
293   j = 0;
294   for (struct shader_ref *ref = scene->frag_shaders; ref; ref = ref->next) {
295      for (i = 0; i < ref->count; i++) {
296         if (LP_DEBUG & DEBUG_SETUP)
297            debug_printf("shader %d: %p\n", j, (void *) ref->variant[i]);
298         j++;
299         lp_fs_variant_reference(llvmpipe_context(scene->pipe),
300                                 &ref->variant[i], NULL);
301      }
302   }
303
304   /* Free all scene data blocks:
305    */
306   {
307      struct data_block_list *list = &scene->data;
308      struct data_block *block, *tmp;
309
310      for (block = list->head; block; block = tmp) {
311         tmp = block->next;
312         if (block != &list->first)
313            FREE(block);
314      }
315
316      list->head = &list->first;
317      list->head->next = NULL;
318   }
319
320   lp_fence_reference(&scene->fence, NULL);
321
322   scene->resources = NULL;
323   scene->writeable_resources = NULL;
324   scene->frag_shaders = NULL;
325   scene->scene_size = 0;
326   scene->resource_reference_size = 0;
327
328   scene->alloc_failed = FALSE;
329
330   util_unreference_framebuffer_state( &scene->fb );
331}
332
333
334
335
336
337
338struct cmd_block *
339lp_scene_new_cmd_block( struct lp_scene *scene,
340                        struct cmd_bin *bin )
341{
342   struct cmd_block *block = lp_scene_alloc(scene, sizeof(struct cmd_block));
343   if (block) {
344      if (bin->tail) {
345         bin->tail->next = block;
346         bin->tail = block;
347      }
348      else {
349         bin->head = block;
350         bin->tail = block;
351      }
352      //memset(block, 0, sizeof *block);
353      block->next = NULL;
354      block->count = 0;
355   }
356   return block;
357}
358
359
360struct data_block *
361lp_scene_new_data_block( struct lp_scene *scene )
362{
363   if (scene->scene_size + DATA_BLOCK_SIZE > LP_SCENE_MAX_SIZE) {
364      if (0) debug_printf("%s: failed\n", __FUNCTION__);
365      scene->alloc_failed = TRUE;
366      return NULL;
367   }
368   else {
369      struct data_block *block = MALLOC_STRUCT(data_block);
370      if (!block)
371         return NULL;
372
373      scene->scene_size += sizeof *block;
374
375      block->used = 0;
376      block->next = scene->data.head;
377      scene->data.head = block;
378
379      return block;
380   }
381}
382
383
384/**
385 * Return number of bytes used for all bin data within a scene.
386 * This does not include resources (textures) referenced by the scene.
387 */
388static unsigned
389lp_scene_data_size( const struct lp_scene *scene )
390{
391   unsigned size = 0;
392   const struct data_block *block;
393   for (block = scene->data.head; block; block = block->next) {
394      size += block->used;
395   }
396   return size;
397}
398
399
400
401/**
402 * Add a reference to a resource by the scene.
403 */
404boolean
405lp_scene_add_resource_reference(struct lp_scene *scene,
406                                struct pipe_resource *resource,
407                                boolean initializing_scene,
408                                boolean writeable)
409{
410   struct resource_ref *ref;
411   int i;
412   struct resource_ref **list = writeable ? &scene->writeable_resources : &scene->resources;
413   struct resource_ref **last = list;
414
415   /* Look at existing resource blocks:
416    */
417   for (ref = *list; ref; ref = ref->next) {
418      last = &ref->next;
419
420      /* Search for this resource:
421       */
422      for (i = 0; i < ref->count; i++)
423         if (ref->resource[i] == resource)
424            return TRUE;
425
426      if (ref->count < RESOURCE_REF_SZ) {
427         /* If the block is half-empty, then append the reference here.
428          */
429         break;
430      }
431   }
432
433   /* Create a new block if no half-empty block was found.
434    */
435   if (!ref) {
436      assert(*last == NULL);
437      *last = lp_scene_alloc(scene, sizeof *ref);
438      if (*last == NULL)
439          return FALSE;
440
441      ref = *last;
442      memset(ref, 0, sizeof *ref);
443   }
444
445   /* Map resource again to increment the map count. We likely use the
446    * already-mapped pointer in a texture of the jit context, and that pointer
447    * needs to stay mapped during rasterization. This map is unmap'ed when
448    * finalizing scene rasterization. */
449   llvmpipe_resource_map(resource, 0, 0, LP_TEX_USAGE_READ);
450
451   /* Append the reference to the reference block.
452    */
453   pipe_resource_reference(&ref->resource[ref->count++], resource);
454   scene->resource_reference_size += llvmpipe_resource_size(resource);
455
456   /* Heuristic to advise scene flushes.  This isn't helpful in the
457    * initial setup of the scene, but after that point flush on the
458    * next resource added which exceeds 64MB in referenced texture
459    * data.
460    */
461   if (!initializing_scene &&
462       scene->resource_reference_size >= LP_SCENE_MAX_RESOURCE_SIZE)
463      return FALSE;
464
465   return TRUE;
466}
467
468
469/**
470 * Add a reference to a fragment shader variant
471 * Return FALSE if out of memory, TRUE otherwise.
472 */
473boolean
474lp_scene_add_frag_shader_reference(struct lp_scene *scene,
475                                   struct lp_fragment_shader_variant *variant)
476{
477   struct shader_ref *ref, **last = &scene->frag_shaders;
478
479   /* Look at existing resource blocks:
480    */
481   for (ref = scene->frag_shaders; ref; ref = ref->next) {
482      last = &ref->next;
483
484      /* Search for this resource:
485       */
486      for (int i = 0; i < ref->count; i++)
487         if (ref->variant[i] == variant)
488            return TRUE;
489
490      if (ref->count < SHADER_REF_SZ) {
491         /* If the block is half-empty, then append the reference here.
492          */
493         break;
494      }
495   }
496
497   /* Create a new block if no half-empty block was found.
498    */
499   if (!ref) {
500      assert(*last == NULL);
501      *last = lp_scene_alloc(scene, sizeof *ref);
502      if (*last == NULL)
503          return FALSE;
504
505      ref = *last;
506      memset(ref, 0, sizeof *ref);
507   }
508
509   /* Append the reference to the reference block.
510    */
511   lp_fs_variant_reference(llvmpipe_context(scene->pipe), &ref->variant[ref->count++], variant);
512
513   return TRUE;
514}
515
516/**
517 * Does this scene have a reference to the given resource?
518 */
519unsigned
520lp_scene_is_resource_referenced(const struct lp_scene *scene,
521                                const struct pipe_resource *resource)
522{
523   const struct resource_ref *ref;
524   int i;
525
526   for (ref = scene->resources; ref; ref = ref->next) {
527      for (i = 0; i < ref->count; i++)
528         if (ref->resource[i] == resource)
529            return LP_REFERENCED_FOR_READ;
530   }
531
532   for (ref = scene->writeable_resources; ref; ref = ref->next) {
533      for (i = 0; i < ref->count; i++)
534         if (ref->resource[i] == resource)
535            return LP_REFERENCED_FOR_READ | LP_REFERENCED_FOR_WRITE;
536   }
537
538   return 0;
539}
540
541
542
543
544/** advance curr_x,y to the next bin */
545static boolean
546next_bin(struct lp_scene *scene)
547{
548   scene->curr_x++;
549   if (scene->curr_x >= scene->tiles_x) {
550      scene->curr_x = 0;
551      scene->curr_y++;
552   }
553   if (scene->curr_y >= scene->tiles_y) {
554      /* no more bins */
555      return FALSE;
556   }
557   return TRUE;
558}
559
560
561void
562lp_scene_bin_iter_begin( struct lp_scene *scene )
563{
564   scene->curr_x = scene->curr_y = -1;
565}
566
567
568/**
569 * Return pointer to next bin to be rendered.
570 * The lp_scene::curr_x and ::curr_y fields will be advanced.
571 * Multiple rendering threads will call this function to get a chunk
572 * of work (a bin) to work on.
573 */
574struct cmd_bin *
575lp_scene_bin_iter_next( struct lp_scene *scene , int *x, int *y)
576{
577   struct cmd_bin *bin = NULL;
578
579   mtx_lock(&scene->mutex);
580
581   if (scene->curr_x < 0) {
582      /* first bin */
583      scene->curr_x = 0;
584      scene->curr_y = 0;
585   }
586   else if (!next_bin(scene)) {
587      /* no more bins left */
588      goto end;
589   }
590
591   bin = lp_scene_get_bin(scene, scene->curr_x, scene->curr_y);
592   *x = scene->curr_x;
593   *y = scene->curr_y;
594
595end:
596   /*printf("return bin %p at %d, %d\n", (void *) bin, *bin_x, *bin_y);*/
597   mtx_unlock(&scene->mutex);
598   return bin;
599}
600
601
602void lp_scene_begin_binning(struct lp_scene *scene,
603                            struct pipe_framebuffer_state *fb)
604{
605   int i;
606   unsigned max_layer = ~0;
607
608   assert(lp_scene_is_empty(scene));
609
610   util_copy_framebuffer_state(&scene->fb, fb);
611
612   scene->tiles_x = align(fb->width, TILE_SIZE) / TILE_SIZE;
613   scene->tiles_y = align(fb->height, TILE_SIZE) / TILE_SIZE;
614   assert(scene->tiles_x <= TILES_X);
615   assert(scene->tiles_y <= TILES_Y);
616
617   unsigned num_required_tiles = scene->tiles_x * scene->tiles_y;
618   if (scene->num_alloced_tiles < num_required_tiles) {
619      scene->tiles = reallocarray(scene->tiles, num_required_tiles, sizeof(struct cmd_bin));
620      if (!scene->tiles)
621         return;
622      memset(scene->tiles, 0, sizeof(struct cmd_bin) * num_required_tiles);
623      scene->num_alloced_tiles = num_required_tiles;
624   }
625
626   /*
627    * Determine how many layers the fb has (used for clamping layer value).
628    * OpenGL (but not d3d10) permits different amount of layers per rt, however
629    * results are undefined if layer exceeds the amount of layers of ANY
630    * attachment hence don't need separate per cbuf and zsbuf max.
631    */
632   for (i = 0; i < scene->fb.nr_cbufs; i++) {
633      struct pipe_surface *cbuf = scene->fb.cbufs[i];
634      if (cbuf) {
635         if (llvmpipe_resource_is_texture(cbuf->texture)) {
636            max_layer = MIN2(max_layer,
637                             cbuf->u.tex.last_layer - cbuf->u.tex.first_layer);
638         }
639         else {
640            max_layer = 0;
641         }
642      }
643   }
644   if (fb->zsbuf) {
645      struct pipe_surface *zsbuf = scene->fb.zsbuf;
646      max_layer = MIN2(max_layer, zsbuf->u.tex.last_layer - zsbuf->u.tex.first_layer);
647   }
648   scene->fb_max_layer = max_layer;
649   scene->fb_max_samples = util_framebuffer_get_num_samples(fb);
650   if (scene->fb_max_samples == 4) {
651      for (unsigned i = 0; i < 4; i++) {
652         scene->fixed_sample_pos[i][0] = util_iround(lp_sample_pos_4x[i][0] * FIXED_ONE);
653         scene->fixed_sample_pos[i][1] = util_iround(lp_sample_pos_4x[i][1] * FIXED_ONE);
654      }
655   }
656}
657
658
659void lp_scene_end_binning( struct lp_scene *scene )
660{
661   if (LP_DEBUG & DEBUG_SCENE) {
662      debug_printf("rasterize scene:\n");
663      debug_printf("  scene_size: %u\n",
664                   scene->scene_size);
665      debug_printf("  data size: %u\n",
666                   lp_scene_data_size(scene));
667
668      if (0)
669         lp_debug_bins( scene );
670   }
671}
672