OSDN Git Service

anv: Count surfaces for non-YCbCr images in GetDescriptorSetLayoutSupport
[android-x86/external-mesa.git] / src / intel / vulkan / anv_descriptor_set.c
1 /*
2  * Copyright © 2015 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23
24 #include <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "util/mesa-sha1.h"
31
32 #include "anv_private.h"
33
34 /*
35  * Descriptor set layouts.
36  */
37
38 void anv_GetDescriptorSetLayoutSupport(
39     VkDevice                                    device,
40     const VkDescriptorSetLayoutCreateInfo*      pCreateInfo,
41     VkDescriptorSetLayoutSupport*               pSupport)
42 {
43    uint32_t surface_count[MESA_SHADER_STAGES] = { 0, };
44
45    for (uint32_t b = 0; b < pCreateInfo->bindingCount; b++) {
46       const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[b];
47
48       switch (binding->descriptorType) {
49       case VK_DESCRIPTOR_TYPE_SAMPLER:
50          /* There is no real limit on samplers */
51          break;
52
53       case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
54          if (binding->pImmutableSamplers) {
55             for (uint32_t i = 0; i < binding->descriptorCount; i++) {
56                ANV_FROM_HANDLE(anv_sampler, sampler,
57                                binding->pImmutableSamplers[i]);
58                anv_foreach_stage(s, binding->stageFlags)
59                   surface_count[s] += sampler->n_planes;
60             }
61          } else {
62             anv_foreach_stage(s, binding->stageFlags)
63                surface_count[s] += binding->descriptorCount;
64          }
65          break;
66
67       default:
68          anv_foreach_stage(s, binding->stageFlags)
69             surface_count[s] += binding->descriptorCount;
70          break;
71       }
72    }
73
74    bool supported = true;
75    for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
76       /* Our maximum binding table size is 250 and we need to reserve 8 for
77        * render targets.  240 is a nice round number.
78        */
79       if (surface_count[s] >= 240)
80          supported = false;
81    }
82
83    pSupport->supported = supported;
84 }
85
86 VkResult anv_CreateDescriptorSetLayout(
87     VkDevice                                    _device,
88     const VkDescriptorSetLayoutCreateInfo*      pCreateInfo,
89     const VkAllocationCallbacks*                pAllocator,
90     VkDescriptorSetLayout*                      pSetLayout)
91 {
92    ANV_FROM_HANDLE(anv_device, device, _device);
93
94    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO);
95
96    uint32_t max_binding = 0;
97    uint32_t immutable_sampler_count = 0;
98    for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
99       max_binding = MAX2(max_binding, pCreateInfo->pBindings[j].binding);
100
101       /* From the Vulkan 1.1.97 spec for VkDescriptorSetLayoutBinding:
102        *
103        *    "If descriptorType specifies a VK_DESCRIPTOR_TYPE_SAMPLER or
104        *    VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER type descriptor, then
105        *    pImmutableSamplers can be used to initialize a set of immutable
106        *    samplers. [...]  If descriptorType is not one of these descriptor
107        *    types, then pImmutableSamplers is ignored.
108        *
109        * We need to be careful here and only parse pImmutableSamplers if we
110        * have one of the right descriptor types.
111        */
112       VkDescriptorType desc_type = pCreateInfo->pBindings[j].descriptorType;
113       if ((desc_type == VK_DESCRIPTOR_TYPE_SAMPLER ||
114            desc_type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) &&
115           pCreateInfo->pBindings[j].pImmutableSamplers)
116          immutable_sampler_count += pCreateInfo->pBindings[j].descriptorCount;
117    }
118
119    struct anv_descriptor_set_layout *set_layout;
120    struct anv_descriptor_set_binding_layout *bindings;
121    struct anv_sampler **samplers;
122
123    /* We need to allocate decriptor set layouts off the device allocator
124     * with DEVICE scope because they are reference counted and may not be
125     * destroyed when vkDestroyDescriptorSetLayout is called.
126     */
127    ANV_MULTIALLOC(ma);
128    anv_multialloc_add(&ma, &set_layout, 1);
129    anv_multialloc_add(&ma, &bindings, max_binding + 1);
130    anv_multialloc_add(&ma, &samplers, immutable_sampler_count);
131
132    if (!anv_multialloc_alloc(&ma, &device->alloc,
133                              VK_SYSTEM_ALLOCATION_SCOPE_DEVICE))
134       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
135
136    memset(set_layout, 0, sizeof(*set_layout));
137    set_layout->ref_cnt = 1;
138    set_layout->binding_count = max_binding + 1;
139
140    for (uint32_t b = 0; b <= max_binding; b++) {
141       /* Initialize all binding_layout entries to -1 */
142       memset(&set_layout->binding[b], -1, sizeof(set_layout->binding[b]));
143
144       set_layout->binding[b].array_size = 0;
145       set_layout->binding[b].immutable_samplers = NULL;
146    }
147
148    /* Initialize all samplers to 0 */
149    memset(samplers, 0, immutable_sampler_count * sizeof(*samplers));
150
151    uint32_t sampler_count[MESA_SHADER_STAGES] = { 0, };
152    uint32_t surface_count[MESA_SHADER_STAGES] = { 0, };
153    uint32_t image_count[MESA_SHADER_STAGES] = { 0, };
154    uint32_t buffer_count = 0;
155    uint32_t dynamic_offset_count = 0;
156
157    for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
158       const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[j];
159       uint32_t b = binding->binding;
160       /* We temporarily store the pointer to the binding in the
161        * immutable_samplers pointer.  This provides us with a quick-and-dirty
162        * way to sort the bindings by binding number.
163        */
164       set_layout->binding[b].immutable_samplers = (void *)binding;
165    }
166
167    for (uint32_t b = 0; b <= max_binding; b++) {
168       const VkDescriptorSetLayoutBinding *binding =
169          (void *)set_layout->binding[b].immutable_samplers;
170
171       if (binding == NULL)
172          continue;
173
174       /* We temporarily stashed the pointer to the binding in the
175        * immutable_samplers pointer.  Now that we've pulled it back out
176        * again, we reset immutable_samplers to NULL.
177        */
178       set_layout->binding[b].immutable_samplers = NULL;
179
180       if (binding->descriptorCount == 0)
181          continue;
182
183 #ifndef NDEBUG
184       set_layout->binding[b].type = binding->descriptorType;
185 #endif
186       set_layout->binding[b].array_size = binding->descriptorCount;
187       set_layout->binding[b].descriptor_index = set_layout->size;
188       set_layout->size += binding->descriptorCount;
189
190       switch (binding->descriptorType) {
191       case VK_DESCRIPTOR_TYPE_SAMPLER:
192       case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
193          anv_foreach_stage(s, binding->stageFlags) {
194             set_layout->binding[b].stage[s].sampler_index = sampler_count[s];
195             sampler_count[s] += binding->descriptorCount;
196          }
197
198          if (binding->pImmutableSamplers) {
199             set_layout->binding[b].immutable_samplers = samplers;
200             samplers += binding->descriptorCount;
201
202             for (uint32_t i = 0; i < binding->descriptorCount; i++)
203                set_layout->binding[b].immutable_samplers[i] =
204                   anv_sampler_from_handle(binding->pImmutableSamplers[i]);
205          }
206          break;
207       default:
208          break;
209       }
210
211       switch (binding->descriptorType) {
212       case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
213       case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
214       case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
215       case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
216          set_layout->binding[b].buffer_index = buffer_count;
217          buffer_count += binding->descriptorCount;
218          /* fall through */
219
220       case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
221       case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
222       case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
223       case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
224       case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
225       case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
226          anv_foreach_stage(s, binding->stageFlags) {
227             set_layout->binding[b].stage[s].surface_index = surface_count[s];
228             surface_count[s] += binding->descriptorCount;
229          }
230          break;
231       default:
232          break;
233       }
234
235       switch (binding->descriptorType) {
236       case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
237       case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
238          set_layout->binding[b].dynamic_offset_index = dynamic_offset_count;
239          dynamic_offset_count += binding->descriptorCount;
240          break;
241       default:
242          break;
243       }
244
245       switch (binding->descriptorType) {
246       case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
247       case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
248          anv_foreach_stage(s, binding->stageFlags) {
249             set_layout->binding[b].stage[s].image_index = image_count[s];
250             image_count[s] += binding->descriptorCount;
251          }
252          break;
253       default:
254          break;
255       }
256
257       set_layout->shader_stages |= binding->stageFlags;
258    }
259
260    set_layout->buffer_count = buffer_count;
261    set_layout->dynamic_offset_count = dynamic_offset_count;
262
263    *pSetLayout = anv_descriptor_set_layout_to_handle(set_layout);
264
265    return VK_SUCCESS;
266 }
267
268 void anv_DestroyDescriptorSetLayout(
269     VkDevice                                    _device,
270     VkDescriptorSetLayout                       _set_layout,
271     const VkAllocationCallbacks*                pAllocator)
272 {
273    ANV_FROM_HANDLE(anv_device, device, _device);
274    ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout, _set_layout);
275
276    if (!set_layout)
277       return;
278
279    anv_descriptor_set_layout_unref(device, set_layout);
280 }
281
282 #define SHA1_UPDATE_VALUE(ctx, x) _mesa_sha1_update(ctx, &(x), sizeof(x));
283
284 static void
285 sha1_update_immutable_sampler(struct mesa_sha1 *ctx,
286                               const struct anv_sampler *sampler)
287 {
288    if (!sampler->conversion)
289       return;
290
291    /* The only thing that affects the shader is ycbcr conversion */
292    _mesa_sha1_update(ctx, sampler->conversion,
293                      sizeof(*sampler->conversion));
294 }
295
296 static void
297 sha1_update_descriptor_set_binding_layout(struct mesa_sha1 *ctx,
298    const struct anv_descriptor_set_binding_layout *layout)
299 {
300    SHA1_UPDATE_VALUE(ctx, layout->array_size);
301    SHA1_UPDATE_VALUE(ctx, layout->descriptor_index);
302    SHA1_UPDATE_VALUE(ctx, layout->dynamic_offset_index);
303    SHA1_UPDATE_VALUE(ctx, layout->buffer_index);
304    _mesa_sha1_update(ctx, layout->stage, sizeof(layout->stage));
305
306    if (layout->immutable_samplers) {
307       for (uint16_t i = 0; i < layout->array_size; i++)
308          sha1_update_immutable_sampler(ctx, layout->immutable_samplers[i]);
309    }
310 }
311
312 static void
313 sha1_update_descriptor_set_layout(struct mesa_sha1 *ctx,
314                                   const struct anv_descriptor_set_layout *layout)
315 {
316    SHA1_UPDATE_VALUE(ctx, layout->binding_count);
317    SHA1_UPDATE_VALUE(ctx, layout->size);
318    SHA1_UPDATE_VALUE(ctx, layout->shader_stages);
319    SHA1_UPDATE_VALUE(ctx, layout->buffer_count);
320    SHA1_UPDATE_VALUE(ctx, layout->dynamic_offset_count);
321
322    for (uint16_t i = 0; i < layout->binding_count; i++)
323       sha1_update_descriptor_set_binding_layout(ctx, &layout->binding[i]);
324 }
325
326 /*
327  * Pipeline layouts.  These have nothing to do with the pipeline.  They are
328  * just multiple descriptor set layouts pasted together
329  */
330
331 VkResult anv_CreatePipelineLayout(
332     VkDevice                                    _device,
333     const VkPipelineLayoutCreateInfo*           pCreateInfo,
334     const VkAllocationCallbacks*                pAllocator,
335     VkPipelineLayout*                           pPipelineLayout)
336 {
337    ANV_FROM_HANDLE(anv_device, device, _device);
338    struct anv_pipeline_layout *layout;
339
340    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
341
342    layout = vk_alloc2(&device->alloc, pAllocator, sizeof(*layout), 8,
343                        VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
344    if (layout == NULL)
345       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
346
347    layout->num_sets = pCreateInfo->setLayoutCount;
348
349    unsigned dynamic_offset_count = 0;
350
351    memset(layout->stage, 0, sizeof(layout->stage));
352    for (uint32_t set = 0; set < pCreateInfo->setLayoutCount; set++) {
353       ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout,
354                       pCreateInfo->pSetLayouts[set]);
355       layout->set[set].layout = set_layout;
356       anv_descriptor_set_layout_ref(set_layout);
357
358       layout->set[set].dynamic_offset_start = dynamic_offset_count;
359       for (uint32_t b = 0; b < set_layout->binding_count; b++) {
360          if (set_layout->binding[b].dynamic_offset_index < 0)
361             continue;
362
363          dynamic_offset_count += set_layout->binding[b].array_size;
364          for (gl_shader_stage s = 0; s < MESA_SHADER_STAGES; s++) {
365             if (set_layout->binding[b].stage[s].surface_index >= 0)
366                layout->stage[s].has_dynamic_offsets = true;
367          }
368       }
369    }
370
371    struct mesa_sha1 ctx;
372    _mesa_sha1_init(&ctx);
373    for (unsigned s = 0; s < layout->num_sets; s++) {
374       sha1_update_descriptor_set_layout(&ctx, layout->set[s].layout);
375       _mesa_sha1_update(&ctx, &layout->set[s].dynamic_offset_start,
376                         sizeof(layout->set[s].dynamic_offset_start));
377    }
378    _mesa_sha1_update(&ctx, &layout->num_sets, sizeof(layout->num_sets));
379    for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
380       _mesa_sha1_update(&ctx, &layout->stage[s].has_dynamic_offsets,
381                         sizeof(layout->stage[s].has_dynamic_offsets));
382    }
383    _mesa_sha1_final(&ctx, layout->sha1);
384
385    *pPipelineLayout = anv_pipeline_layout_to_handle(layout);
386
387    return VK_SUCCESS;
388 }
389
390 void anv_DestroyPipelineLayout(
391     VkDevice                                    _device,
392     VkPipelineLayout                            _pipelineLayout,
393     const VkAllocationCallbacks*                pAllocator)
394 {
395    ANV_FROM_HANDLE(anv_device, device, _device);
396    ANV_FROM_HANDLE(anv_pipeline_layout, pipeline_layout, _pipelineLayout);
397
398    if (!pipeline_layout)
399       return;
400
401    for (uint32_t i = 0; i < pipeline_layout->num_sets; i++)
402       anv_descriptor_set_layout_unref(device, pipeline_layout->set[i].layout);
403
404    vk_free2(&device->alloc, pAllocator, pipeline_layout);
405 }
406
407 /*
408  * Descriptor pools.
409  *
410  * These are implemented using a big pool of memory and a free-list for the
411  * host memory allocations and a state_stream and a free list for the buffer
412  * view surface state. The spec allows us to fail to allocate due to
413  * fragmentation in all cases but two: 1) after pool reset, allocating up
414  * until the pool size with no freeing must succeed and 2) allocating and
415  * freeing only descriptor sets with the same layout. Case 1) is easy enogh,
416  * and the free lists lets us recycle blocks for case 2).
417  */
418
419 #define EMPTY 1
420
421 VkResult anv_CreateDescriptorPool(
422     VkDevice                                    _device,
423     const VkDescriptorPoolCreateInfo*           pCreateInfo,
424     const VkAllocationCallbacks*                pAllocator,
425     VkDescriptorPool*                           pDescriptorPool)
426 {
427    ANV_FROM_HANDLE(anv_device, device, _device);
428    struct anv_descriptor_pool *pool;
429
430    uint32_t descriptor_count = 0;
431    uint32_t buffer_count = 0;
432    for (uint32_t i = 0; i < pCreateInfo->poolSizeCount; i++) {
433       switch (pCreateInfo->pPoolSizes[i].type) {
434       case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
435       case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
436       case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
437       case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
438          buffer_count += pCreateInfo->pPoolSizes[i].descriptorCount;
439       default:
440          descriptor_count += pCreateInfo->pPoolSizes[i].descriptorCount;
441          break;
442       }
443    }
444
445    const size_t pool_size =
446       pCreateInfo->maxSets * sizeof(struct anv_descriptor_set) +
447       descriptor_count * sizeof(struct anv_descriptor) +
448       buffer_count * sizeof(struct anv_buffer_view);
449    const size_t total_size = sizeof(*pool) + pool_size;
450
451    pool = vk_alloc2(&device->alloc, pAllocator, total_size, 8,
452                      VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
453    if (!pool)
454       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
455
456    pool->size = pool_size;
457    pool->next = 0;
458    pool->free_list = EMPTY;
459
460    anv_state_stream_init(&pool->surface_state_stream,
461                          &device->surface_state_pool, 4096);
462    pool->surface_state_free_list = NULL;
463
464    *pDescriptorPool = anv_descriptor_pool_to_handle(pool);
465
466    return VK_SUCCESS;
467 }
468
469 void anv_DestroyDescriptorPool(
470     VkDevice                                    _device,
471     VkDescriptorPool                            _pool,
472     const VkAllocationCallbacks*                pAllocator)
473 {
474    ANV_FROM_HANDLE(anv_device, device, _device);
475    ANV_FROM_HANDLE(anv_descriptor_pool, pool, _pool);
476
477    if (!pool)
478       return;
479
480    anv_state_stream_finish(&pool->surface_state_stream);
481    vk_free2(&device->alloc, pAllocator, pool);
482 }
483
484 VkResult anv_ResetDescriptorPool(
485     VkDevice                                    _device,
486     VkDescriptorPool                            descriptorPool,
487     VkDescriptorPoolResetFlags                  flags)
488 {
489    ANV_FROM_HANDLE(anv_device, device, _device);
490    ANV_FROM_HANDLE(anv_descriptor_pool, pool, descriptorPool);
491
492    pool->next = 0;
493    pool->free_list = EMPTY;
494    anv_state_stream_finish(&pool->surface_state_stream);
495    anv_state_stream_init(&pool->surface_state_stream,
496                          &device->surface_state_pool, 4096);
497    pool->surface_state_free_list = NULL;
498
499    return VK_SUCCESS;
500 }
501
502 struct pool_free_list_entry {
503    uint32_t next;
504    uint32_t size;
505 };
506
507 size_t
508 anv_descriptor_set_layout_size(const struct anv_descriptor_set_layout *layout)
509 {
510    return
511       sizeof(struct anv_descriptor_set) +
512       layout->size * sizeof(struct anv_descriptor) +
513       layout->buffer_count * sizeof(struct anv_buffer_view);
514 }
515
516 struct surface_state_free_list_entry {
517    void *next;
518    struct anv_state state;
519 };
520
521 VkResult
522 anv_descriptor_set_create(struct anv_device *device,
523                           struct anv_descriptor_pool *pool,
524                           struct anv_descriptor_set_layout *layout,
525                           struct anv_descriptor_set **out_set)
526 {
527    struct anv_descriptor_set *set;
528    const size_t size = anv_descriptor_set_layout_size(layout);
529
530    set = NULL;
531    if (size <= pool->size - pool->next) {
532       set = (struct anv_descriptor_set *) (pool->data + pool->next);
533       pool->next += size;
534    } else {
535       struct pool_free_list_entry *entry;
536       uint32_t *link = &pool->free_list;
537       for (uint32_t f = pool->free_list; f != EMPTY; f = entry->next) {
538          entry = (struct pool_free_list_entry *) (pool->data + f);
539          if (size <= entry->size) {
540             *link = entry->next;
541             set = (struct anv_descriptor_set *) entry;
542             break;
543          }
544          link = &entry->next;
545       }
546    }
547
548    if (set == NULL) {
549       if (pool->free_list != EMPTY) {
550          return vk_error(VK_ERROR_FRAGMENTED_POOL);
551       } else {
552          return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY);
553       }
554    }
555
556    set->layout = layout;
557    anv_descriptor_set_layout_ref(layout);
558
559    set->size = size;
560    set->buffer_views =
561       (struct anv_buffer_view *) &set->descriptors[layout->size];
562    set->buffer_count = layout->buffer_count;
563
564    /* By defining the descriptors to be zero now, we can later verify that
565     * a descriptor has not been populated with user data.
566     */
567    memset(set->descriptors, 0, sizeof(struct anv_descriptor) * layout->size);
568
569    /* Go through and fill out immutable samplers if we have any */
570    struct anv_descriptor *desc = set->descriptors;
571    for (uint32_t b = 0; b < layout->binding_count; b++) {
572       if (layout->binding[b].immutable_samplers) {
573          for (uint32_t i = 0; i < layout->binding[b].array_size; i++) {
574             /* The type will get changed to COMBINED_IMAGE_SAMPLER in
575              * UpdateDescriptorSets if needed.  However, if the descriptor
576              * set has an immutable sampler, UpdateDescriptorSets may never
577              * touch it, so we need to make sure it's 100% valid now.
578              */
579             desc[i] = (struct anv_descriptor) {
580                .type = VK_DESCRIPTOR_TYPE_SAMPLER,
581                .sampler = layout->binding[b].immutable_samplers[i],
582             };
583          }
584       }
585       desc += layout->binding[b].array_size;
586    }
587
588    /* Allocate surface state for the buffer views. */
589    for (uint32_t b = 0; b < layout->buffer_count; b++) {
590       struct surface_state_free_list_entry *entry =
591          pool->surface_state_free_list;
592       struct anv_state state;
593
594       if (entry) {
595          state = entry->state;
596          pool->surface_state_free_list = entry->next;
597          assert(state.alloc_size == 64);
598       } else {
599          state = anv_state_stream_alloc(&pool->surface_state_stream, 64, 64);
600       }
601
602       set->buffer_views[b].surface_state = state;
603    }
604
605    *out_set = set;
606
607    return VK_SUCCESS;
608 }
609
610 void
611 anv_descriptor_set_destroy(struct anv_device *device,
612                            struct anv_descriptor_pool *pool,
613                            struct anv_descriptor_set *set)
614 {
615    anv_descriptor_set_layout_unref(device, set->layout);
616
617    /* Put the buffer view surface state back on the free list. */
618    for (uint32_t b = 0; b < set->buffer_count; b++) {
619       struct surface_state_free_list_entry *entry =
620          set->buffer_views[b].surface_state.map;
621       entry->next = pool->surface_state_free_list;
622       entry->state = set->buffer_views[b].surface_state;
623       pool->surface_state_free_list = entry;
624    }
625
626    /* Put the descriptor set allocation back on the free list. */
627    const uint32_t index = (char *) set - pool->data;
628    if (index + set->size == pool->next) {
629       pool->next = index;
630    } else {
631       struct pool_free_list_entry *entry = (struct pool_free_list_entry *) set;
632       entry->next = pool->free_list;
633       entry->size = set->size;
634       pool->free_list = (char *) entry - pool->data;
635    }
636 }
637
638 VkResult anv_AllocateDescriptorSets(
639     VkDevice                                    _device,
640     const VkDescriptorSetAllocateInfo*          pAllocateInfo,
641     VkDescriptorSet*                            pDescriptorSets)
642 {
643    ANV_FROM_HANDLE(anv_device, device, _device);
644    ANV_FROM_HANDLE(anv_descriptor_pool, pool, pAllocateInfo->descriptorPool);
645
646    VkResult result = VK_SUCCESS;
647    struct anv_descriptor_set *set;
648    uint32_t i;
649
650    for (i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
651       ANV_FROM_HANDLE(anv_descriptor_set_layout, layout,
652                       pAllocateInfo->pSetLayouts[i]);
653
654       result = anv_descriptor_set_create(device, pool, layout, &set);
655       if (result != VK_SUCCESS)
656          break;
657
658       pDescriptorSets[i] = anv_descriptor_set_to_handle(set);
659    }
660
661    if (result != VK_SUCCESS)
662       anv_FreeDescriptorSets(_device, pAllocateInfo->descriptorPool,
663                              i, pDescriptorSets);
664
665    return result;
666 }
667
668 VkResult anv_FreeDescriptorSets(
669     VkDevice                                    _device,
670     VkDescriptorPool                            descriptorPool,
671     uint32_t                                    count,
672     const VkDescriptorSet*                      pDescriptorSets)
673 {
674    ANV_FROM_HANDLE(anv_device, device, _device);
675    ANV_FROM_HANDLE(anv_descriptor_pool, pool, descriptorPool);
676
677    for (uint32_t i = 0; i < count; i++) {
678       ANV_FROM_HANDLE(anv_descriptor_set, set, pDescriptorSets[i]);
679
680       if (!set)
681          continue;
682
683       anv_descriptor_set_destroy(device, pool, set);
684    }
685
686    return VK_SUCCESS;
687 }
688
689 void
690 anv_descriptor_set_write_image_view(struct anv_descriptor_set *set,
691                                     const struct gen_device_info * const devinfo,
692                                     const VkDescriptorImageInfo * const info,
693                                     VkDescriptorType type,
694                                     uint32_t binding,
695                                     uint32_t element)
696 {
697    const struct anv_descriptor_set_binding_layout *bind_layout =
698       &set->layout->binding[binding];
699    struct anv_descriptor *desc =
700       &set->descriptors[bind_layout->descriptor_index + element];
701    struct anv_image_view *image_view = NULL;
702    struct anv_sampler *sampler = NULL;
703
704    assert(type == bind_layout->type);
705
706    switch (type) {
707    case VK_DESCRIPTOR_TYPE_SAMPLER:
708       sampler = anv_sampler_from_handle(info->sampler);
709       break;
710
711    case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
712       image_view = anv_image_view_from_handle(info->imageView);
713       sampler = anv_sampler_from_handle(info->sampler);
714       break;
715
716    case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
717    case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
718    case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
719       image_view = anv_image_view_from_handle(info->imageView);
720       break;
721
722    default:
723       unreachable("invalid descriptor type");
724    }
725
726    /* If this descriptor has an immutable sampler, we don't want to stomp on
727     * it.
728     */
729    sampler = bind_layout->immutable_samplers ?
730              bind_layout->immutable_samplers[element] :
731              sampler;
732
733    *desc = (struct anv_descriptor) {
734       .type = type,
735       .layout = info->imageLayout,
736       .image_view = image_view,
737       .sampler = sampler,
738    };
739 }
740
741 void
742 anv_descriptor_set_write_buffer_view(struct anv_descriptor_set *set,
743                                      VkDescriptorType type,
744                                      struct anv_buffer_view *buffer_view,
745                                      uint32_t binding,
746                                      uint32_t element)
747 {
748    const struct anv_descriptor_set_binding_layout *bind_layout =
749       &set->layout->binding[binding];
750    struct anv_descriptor *desc =
751       &set->descriptors[bind_layout->descriptor_index + element];
752
753    assert(type == bind_layout->type);
754
755    *desc = (struct anv_descriptor) {
756       .type = type,
757       .buffer_view = buffer_view,
758    };
759 }
760
761 void
762 anv_descriptor_set_write_buffer(struct anv_descriptor_set *set,
763                                 struct anv_device *device,
764                                 struct anv_state_stream *alloc_stream,
765                                 VkDescriptorType type,
766                                 struct anv_buffer *buffer,
767                                 uint32_t binding,
768                                 uint32_t element,
769                                 VkDeviceSize offset,
770                                 VkDeviceSize range)
771 {
772    const struct anv_descriptor_set_binding_layout *bind_layout =
773       &set->layout->binding[binding];
774    struct anv_descriptor *desc =
775       &set->descriptors[bind_layout->descriptor_index + element];
776
777    assert(type == bind_layout->type);
778
779    if (type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
780        type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
781       *desc = (struct anv_descriptor) {
782          .type = type,
783          .buffer = buffer,
784          .offset = offset,
785          .range = range,
786       };
787    } else {
788       struct anv_buffer_view *bview =
789          &set->buffer_views[bind_layout->buffer_index + element];
790
791       bview->format = anv_isl_format_for_descriptor_type(type);
792       bview->range = anv_buffer_get_range(buffer, offset, range);
793       bview->address = anv_address_add(buffer->address, offset);
794
795       /* If we're writing descriptors through a push command, we need to
796        * allocate the surface state from the command buffer. Otherwise it will
797        * be allocated by the descriptor pool when calling
798        * vkAllocateDescriptorSets. */
799       if (alloc_stream)
800          bview->surface_state = anv_state_stream_alloc(alloc_stream, 64, 64);
801
802       anv_fill_buffer_surface_state(device, bview->surface_state,
803                                     bview->format,
804                                     bview->address, bview->range, 1);
805
806       *desc = (struct anv_descriptor) {
807          .type = type,
808          .buffer_view = bview,
809       };
810    }
811 }
812
813 void anv_UpdateDescriptorSets(
814     VkDevice                                    _device,
815     uint32_t                                    descriptorWriteCount,
816     const VkWriteDescriptorSet*                 pDescriptorWrites,
817     uint32_t                                    descriptorCopyCount,
818     const VkCopyDescriptorSet*                  pDescriptorCopies)
819 {
820    ANV_FROM_HANDLE(anv_device, device, _device);
821
822    for (uint32_t i = 0; i < descriptorWriteCount; i++) {
823       const VkWriteDescriptorSet *write = &pDescriptorWrites[i];
824       ANV_FROM_HANDLE(anv_descriptor_set, set, write->dstSet);
825
826       switch (write->descriptorType) {
827       case VK_DESCRIPTOR_TYPE_SAMPLER:
828       case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
829       case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
830       case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
831       case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
832          for (uint32_t j = 0; j < write->descriptorCount; j++) {
833             anv_descriptor_set_write_image_view(set, &device->info,
834                                                 write->pImageInfo + j,
835                                                 write->descriptorType,
836                                                 write->dstBinding,
837                                                 write->dstArrayElement + j);
838          }
839          break;
840
841       case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
842       case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
843          for (uint32_t j = 0; j < write->descriptorCount; j++) {
844             ANV_FROM_HANDLE(anv_buffer_view, bview,
845                             write->pTexelBufferView[j]);
846
847             anv_descriptor_set_write_buffer_view(set,
848                                                  write->descriptorType,
849                                                  bview,
850                                                  write->dstBinding,
851                                                  write->dstArrayElement + j);
852          }
853          break;
854
855       case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
856       case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
857       case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
858       case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
859          for (uint32_t j = 0; j < write->descriptorCount; j++) {
860             assert(write->pBufferInfo[j].buffer);
861             ANV_FROM_HANDLE(anv_buffer, buffer, write->pBufferInfo[j].buffer);
862             assert(buffer);
863
864             anv_descriptor_set_write_buffer(set,
865                                             device,
866                                             NULL,
867                                             write->descriptorType,
868                                             buffer,
869                                             write->dstBinding,
870                                             write->dstArrayElement + j,
871                                             write->pBufferInfo[j].offset,
872                                             write->pBufferInfo[j].range);
873          }
874          break;
875
876       default:
877          break;
878       }
879    }
880
881    for (uint32_t i = 0; i < descriptorCopyCount; i++) {
882       const VkCopyDescriptorSet *copy = &pDescriptorCopies[i];
883       ANV_FROM_HANDLE(anv_descriptor_set, src, copy->srcSet);
884       ANV_FROM_HANDLE(anv_descriptor_set, dst, copy->dstSet);
885
886       const struct anv_descriptor_set_binding_layout *src_layout =
887          &src->layout->binding[copy->srcBinding];
888       struct anv_descriptor *src_desc =
889          &src->descriptors[src_layout->descriptor_index];
890       src_desc += copy->srcArrayElement;
891
892       const struct anv_descriptor_set_binding_layout *dst_layout =
893          &dst->layout->binding[copy->dstBinding];
894       struct anv_descriptor *dst_desc =
895          &dst->descriptors[dst_layout->descriptor_index];
896       dst_desc += copy->dstArrayElement;
897
898       for (uint32_t j = 0; j < copy->descriptorCount; j++)
899          dst_desc[j] = src_desc[j];
900    }
901 }
902
903 /*
904  * Descriptor update templates.
905  */
906
907 void
908 anv_descriptor_set_write_template(struct anv_descriptor_set *set,
909                                   struct anv_device *device,
910                                   struct anv_state_stream *alloc_stream,
911                                   const struct anv_descriptor_update_template *template,
912                                   const void *data)
913 {
914    for (uint32_t i = 0; i < template->entry_count; i++) {
915       const struct anv_descriptor_template_entry *entry =
916          &template->entries[i];
917
918       switch (entry->type) {
919       case VK_DESCRIPTOR_TYPE_SAMPLER:
920       case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
921       case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
922       case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
923       case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
924          for (uint32_t j = 0; j < entry->array_count; j++) {
925             const VkDescriptorImageInfo *info =
926                data + entry->offset + j * entry->stride;
927             anv_descriptor_set_write_image_view(set, &device->info,
928                                                 info, entry->type,
929                                                 entry->binding,
930                                                 entry->array_element + j);
931          }
932          break;
933
934       case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
935       case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
936          for (uint32_t j = 0; j < entry->array_count; j++) {
937             const VkBufferView *_bview =
938                data + entry->offset + j * entry->stride;
939             ANV_FROM_HANDLE(anv_buffer_view, bview, *_bview);
940
941             anv_descriptor_set_write_buffer_view(set,
942                                                  entry->type,
943                                                  bview,
944                                                  entry->binding,
945                                                  entry->array_element + j);
946          }
947          break;
948
949       case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
950       case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
951       case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
952       case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
953          for (uint32_t j = 0; j < entry->array_count; j++) {
954             const VkDescriptorBufferInfo *info =
955                data + entry->offset + j * entry->stride;
956             ANV_FROM_HANDLE(anv_buffer, buffer, info->buffer);
957
958             anv_descriptor_set_write_buffer(set,
959                                             device,
960                                             alloc_stream,
961                                             entry->type,
962                                             buffer,
963                                             entry->binding,
964                                             entry->array_element + j,
965                                             info->offset, info->range);
966          }
967          break;
968
969       default:
970          break;
971       }
972    }
973 }
974
975 VkResult anv_CreateDescriptorUpdateTemplate(
976     VkDevice                                    _device,
977     const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
978     const VkAllocationCallbacks*                pAllocator,
979     VkDescriptorUpdateTemplate*                 pDescriptorUpdateTemplate)
980 {
981    ANV_FROM_HANDLE(anv_device, device, _device);
982    struct anv_descriptor_update_template *template;
983
984    size_t size = sizeof(*template) +
985       pCreateInfo->descriptorUpdateEntryCount * sizeof(template->entries[0]);
986    template = vk_alloc2(&device->alloc, pAllocator, size, 8,
987                         VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
988    if (template == NULL)
989       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
990
991    template->bind_point = pCreateInfo->pipelineBindPoint;
992
993    if (pCreateInfo->templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET)
994       template->set = pCreateInfo->set;
995
996    template->entry_count = pCreateInfo->descriptorUpdateEntryCount;
997    for (uint32_t i = 0; i < template->entry_count; i++) {
998       const VkDescriptorUpdateTemplateEntryKHR *pEntry =
999          &pCreateInfo->pDescriptorUpdateEntries[i];
1000
1001       template->entries[i] = (struct anv_descriptor_template_entry) {
1002          .type = pEntry->descriptorType,
1003          .binding = pEntry->dstBinding,
1004          .array_element = pEntry->dstArrayElement,
1005          .array_count = pEntry->descriptorCount,
1006          .offset = pEntry->offset,
1007          .stride = pEntry->stride,
1008       };
1009    }
1010
1011    *pDescriptorUpdateTemplate =
1012       anv_descriptor_update_template_to_handle(template);
1013
1014    return VK_SUCCESS;
1015 }
1016
1017 void anv_DestroyDescriptorUpdateTemplate(
1018     VkDevice                                    _device,
1019     VkDescriptorUpdateTemplate                  descriptorUpdateTemplate,
1020     const VkAllocationCallbacks*                pAllocator)
1021 {
1022    ANV_FROM_HANDLE(anv_device, device, _device);
1023    ANV_FROM_HANDLE(anv_descriptor_update_template, template,
1024                    descriptorUpdateTemplate);
1025
1026    vk_free2(&device->alloc, pAllocator, template);
1027 }
1028
1029 void anv_UpdateDescriptorSetWithTemplate(
1030     VkDevice                                    _device,
1031     VkDescriptorSet                             descriptorSet,
1032     VkDescriptorUpdateTemplate                  descriptorUpdateTemplate,
1033     const void*                                 pData)
1034 {
1035    ANV_FROM_HANDLE(anv_device, device, _device);
1036    ANV_FROM_HANDLE(anv_descriptor_set, set, descriptorSet);
1037    ANV_FROM_HANDLE(anv_descriptor_update_template, template,
1038                    descriptorUpdateTemplate);
1039
1040    anv_descriptor_set_write_template(set, device, NULL, template, pData);
1041 }