OSDN Git Service

anv: Add a concept of a descriptor buffer
[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 static enum anv_descriptor_data
39 anv_descriptor_data_for_type(const struct anv_physical_device *device,
40                              VkDescriptorType type)
41 {
42    enum anv_descriptor_data data = 0;
43
44    switch (type) {
45    case VK_DESCRIPTOR_TYPE_SAMPLER:
46       data = ANV_DESCRIPTOR_SAMPLER_STATE;
47       break;
48
49    case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
50       data = ANV_DESCRIPTOR_SURFACE_STATE |
51              ANV_DESCRIPTOR_SAMPLER_STATE;
52       break;
53
54    case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
55    case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
56    case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
57       data = ANV_DESCRIPTOR_SURFACE_STATE;
58       break;
59
60    case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
61    case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
62       data = ANV_DESCRIPTOR_SURFACE_STATE;
63       if (device->info.gen < 9)
64          data |= ANV_DESCRIPTOR_IMAGE_PARAM;
65       break;
66
67    case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
68    case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
69       data = ANV_DESCRIPTOR_SURFACE_STATE |
70              ANV_DESCRIPTOR_BUFFER_VIEW;
71       break;
72
73    case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
74    case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
75       data = ANV_DESCRIPTOR_SURFACE_STATE;
76       break;
77
78    default:
79       unreachable("Unsupported descriptor type");
80    }
81
82    return data;
83 }
84
85 static unsigned
86 anv_descriptor_data_size(enum anv_descriptor_data data)
87 {
88    return 0;
89 }
90
91 /** Returns the size in bytes of each descriptor with the given layout */
92 unsigned
93 anv_descriptor_size(const struct anv_descriptor_set_binding_layout *layout)
94 {
95    return anv_descriptor_data_size(layout->data);
96 }
97
98 /** Returns the size in bytes of each descriptor of the given type
99  *
100  * This version of the function does not have access to the entire layout so
101  * it may only work on certain descriptor types where the descriptor size is
102  * entirely determined by the descriptor type.  Whenever possible, code should
103  * use anv_descriptor_size() instead.
104  */
105 unsigned
106 anv_descriptor_type_size(const struct anv_physical_device *pdevice,
107                          VkDescriptorType type)
108 {
109    return anv_descriptor_data_size(anv_descriptor_data_for_type(pdevice, type));
110 }
111
112 void anv_GetDescriptorSetLayoutSupport(
113     VkDevice                                    device,
114     const VkDescriptorSetLayoutCreateInfo*      pCreateInfo,
115     VkDescriptorSetLayoutSupport*               pSupport)
116 {
117    uint32_t surface_count[MESA_SHADER_STAGES] = { 0, };
118
119    for (uint32_t b = 0; b < pCreateInfo->bindingCount; b++) {
120       const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[b];
121
122       switch (binding->descriptorType) {
123       case VK_DESCRIPTOR_TYPE_SAMPLER:
124          /* There is no real limit on samplers */
125          break;
126
127       case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
128          if (binding->pImmutableSamplers) {
129             for (uint32_t i = 0; i < binding->descriptorCount; i++) {
130                ANV_FROM_HANDLE(anv_sampler, sampler,
131                                binding->pImmutableSamplers[i]);
132                anv_foreach_stage(s, binding->stageFlags)
133                   surface_count[s] += sampler->n_planes;
134             }
135          } else {
136             anv_foreach_stage(s, binding->stageFlags)
137                surface_count[s] += binding->descriptorCount;
138          }
139          break;
140
141       default:
142          anv_foreach_stage(s, binding->stageFlags)
143             surface_count[s] += binding->descriptorCount;
144          break;
145       }
146    }
147
148    bool supported = true;
149    for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
150       /* Our maximum binding table size is 250 and we need to reserve 8 for
151        * render targets.  240 is a nice round number.
152        */
153       if (surface_count[s] >= 240)
154          supported = false;
155    }
156
157    pSupport->supported = supported;
158 }
159
160 VkResult anv_CreateDescriptorSetLayout(
161     VkDevice                                    _device,
162     const VkDescriptorSetLayoutCreateInfo*      pCreateInfo,
163     const VkAllocationCallbacks*                pAllocator,
164     VkDescriptorSetLayout*                      pSetLayout)
165 {
166    ANV_FROM_HANDLE(anv_device, device, _device);
167
168    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO);
169
170    uint32_t max_binding = 0;
171    uint32_t immutable_sampler_count = 0;
172    for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
173       max_binding = MAX2(max_binding, pCreateInfo->pBindings[j].binding);
174
175       /* From the Vulkan 1.1.97 spec for VkDescriptorSetLayoutBinding:
176        *
177        *    "If descriptorType specifies a VK_DESCRIPTOR_TYPE_SAMPLER or
178        *    VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER type descriptor, then
179        *    pImmutableSamplers can be used to initialize a set of immutable
180        *    samplers. [...]  If descriptorType is not one of these descriptor
181        *    types, then pImmutableSamplers is ignored.
182        *
183        * We need to be careful here and only parse pImmutableSamplers if we
184        * have one of the right descriptor types.
185        */
186       VkDescriptorType desc_type = pCreateInfo->pBindings[j].descriptorType;
187       if ((desc_type == VK_DESCRIPTOR_TYPE_SAMPLER ||
188            desc_type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) &&
189           pCreateInfo->pBindings[j].pImmutableSamplers)
190          immutable_sampler_count += pCreateInfo->pBindings[j].descriptorCount;
191    }
192
193    struct anv_descriptor_set_layout *set_layout;
194    struct anv_descriptor_set_binding_layout *bindings;
195    struct anv_sampler **samplers;
196
197    /* We need to allocate decriptor set layouts off the device allocator
198     * with DEVICE scope because they are reference counted and may not be
199     * destroyed when vkDestroyDescriptorSetLayout is called.
200     */
201    ANV_MULTIALLOC(ma);
202    anv_multialloc_add(&ma, &set_layout, 1);
203    anv_multialloc_add(&ma, &bindings, max_binding + 1);
204    anv_multialloc_add(&ma, &samplers, immutable_sampler_count);
205
206    if (!anv_multialloc_alloc(&ma, &device->alloc,
207                              VK_SYSTEM_ALLOCATION_SCOPE_DEVICE))
208       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
209
210    memset(set_layout, 0, sizeof(*set_layout));
211    set_layout->ref_cnt = 1;
212    set_layout->binding_count = max_binding + 1;
213
214    for (uint32_t b = 0; b <= max_binding; b++) {
215       /* Initialize all binding_layout entries to -1 */
216       memset(&set_layout->binding[b], -1, sizeof(set_layout->binding[b]));
217
218       set_layout->binding[b].data = 0;
219       set_layout->binding[b].array_size = 0;
220       set_layout->binding[b].immutable_samplers = NULL;
221    }
222
223    /* Initialize all samplers to 0 */
224    memset(samplers, 0, immutable_sampler_count * sizeof(*samplers));
225
226    uint32_t buffer_view_count = 0;
227    uint32_t dynamic_offset_count = 0;
228    uint32_t descriptor_buffer_size = 0;
229
230    for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
231       const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[j];
232       uint32_t b = binding->binding;
233       /* We temporarily store the pointer to the binding in the
234        * immutable_samplers pointer.  This provides us with a quick-and-dirty
235        * way to sort the bindings by binding number.
236        */
237       set_layout->binding[b].immutable_samplers = (void *)binding;
238    }
239
240    for (uint32_t b = 0; b <= max_binding; b++) {
241       const VkDescriptorSetLayoutBinding *binding =
242          (void *)set_layout->binding[b].immutable_samplers;
243
244       if (binding == NULL)
245          continue;
246
247       /* We temporarily stashed the pointer to the binding in the
248        * immutable_samplers pointer.  Now that we've pulled it back out
249        * again, we reset immutable_samplers to NULL.
250        */
251       set_layout->binding[b].immutable_samplers = NULL;
252
253       if (binding->descriptorCount == 0)
254          continue;
255
256 #ifndef NDEBUG
257       set_layout->binding[b].type = binding->descriptorType;
258 #endif
259       set_layout->binding[b].data =
260          anv_descriptor_data_for_type(&device->instance->physicalDevice,
261                                       binding->descriptorType);
262       set_layout->binding[b].array_size = binding->descriptorCount;
263       set_layout->binding[b].descriptor_index = set_layout->size;
264       set_layout->size += binding->descriptorCount;
265
266       if (set_layout->binding[b].data & ANV_DESCRIPTOR_BUFFER_VIEW) {
267          set_layout->binding[b].buffer_view_index = buffer_view_count;
268          buffer_view_count += binding->descriptorCount;
269       }
270
271       switch (binding->descriptorType) {
272       case VK_DESCRIPTOR_TYPE_SAMPLER:
273       case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
274          if (binding->pImmutableSamplers) {
275             set_layout->binding[b].immutable_samplers = samplers;
276             samplers += binding->descriptorCount;
277
278             for (uint32_t i = 0; i < binding->descriptorCount; i++)
279                set_layout->binding[b].immutable_samplers[i] =
280                   anv_sampler_from_handle(binding->pImmutableSamplers[i]);
281          }
282          break;
283       default:
284          break;
285       }
286
287       switch (binding->descriptorType) {
288       case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
289       case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
290          set_layout->binding[b].dynamic_offset_index = dynamic_offset_count;
291          dynamic_offset_count += binding->descriptorCount;
292          break;
293
294       default:
295          break;
296       }
297
298       set_layout->binding[b].descriptor_offset = descriptor_buffer_size;
299       descriptor_buffer_size += anv_descriptor_size(&set_layout->binding[b]) *
300                                 binding->descriptorCount;
301
302       set_layout->shader_stages |= binding->stageFlags;
303    }
304
305    set_layout->buffer_view_count = buffer_view_count;
306    set_layout->dynamic_offset_count = dynamic_offset_count;
307    set_layout->descriptor_buffer_size = descriptor_buffer_size;
308
309    *pSetLayout = anv_descriptor_set_layout_to_handle(set_layout);
310
311    return VK_SUCCESS;
312 }
313
314 void anv_DestroyDescriptorSetLayout(
315     VkDevice                                    _device,
316     VkDescriptorSetLayout                       _set_layout,
317     const VkAllocationCallbacks*                pAllocator)
318 {
319    ANV_FROM_HANDLE(anv_device, device, _device);
320    ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout, _set_layout);
321
322    if (!set_layout)
323       return;
324
325    anv_descriptor_set_layout_unref(device, set_layout);
326 }
327
328 #define SHA1_UPDATE_VALUE(ctx, x) _mesa_sha1_update(ctx, &(x), sizeof(x));
329
330 static void
331 sha1_update_immutable_sampler(struct mesa_sha1 *ctx,
332                               const struct anv_sampler *sampler)
333 {
334    if (!sampler->conversion)
335       return;
336
337    /* The only thing that affects the shader is ycbcr conversion */
338    _mesa_sha1_update(ctx, sampler->conversion,
339                      sizeof(*sampler->conversion));
340 }
341
342 static void
343 sha1_update_descriptor_set_binding_layout(struct mesa_sha1 *ctx,
344    const struct anv_descriptor_set_binding_layout *layout)
345 {
346    SHA1_UPDATE_VALUE(ctx, layout->data);
347    SHA1_UPDATE_VALUE(ctx, layout->array_size);
348    SHA1_UPDATE_VALUE(ctx, layout->descriptor_index);
349    SHA1_UPDATE_VALUE(ctx, layout->dynamic_offset_index);
350    SHA1_UPDATE_VALUE(ctx, layout->buffer_view_index);
351    SHA1_UPDATE_VALUE(ctx, layout->descriptor_offset);
352
353    if (layout->immutable_samplers) {
354       for (uint16_t i = 0; i < layout->array_size; i++)
355          sha1_update_immutable_sampler(ctx, layout->immutable_samplers[i]);
356    }
357 }
358
359 static void
360 sha1_update_descriptor_set_layout(struct mesa_sha1 *ctx,
361                                   const struct anv_descriptor_set_layout *layout)
362 {
363    SHA1_UPDATE_VALUE(ctx, layout->binding_count);
364    SHA1_UPDATE_VALUE(ctx, layout->size);
365    SHA1_UPDATE_VALUE(ctx, layout->shader_stages);
366    SHA1_UPDATE_VALUE(ctx, layout->buffer_view_count);
367    SHA1_UPDATE_VALUE(ctx, layout->dynamic_offset_count);
368    SHA1_UPDATE_VALUE(ctx, layout->descriptor_buffer_size);
369
370    for (uint16_t i = 0; i < layout->binding_count; i++)
371       sha1_update_descriptor_set_binding_layout(ctx, &layout->binding[i]);
372 }
373
374 /*
375  * Pipeline layouts.  These have nothing to do with the pipeline.  They are
376  * just multiple descriptor set layouts pasted together
377  */
378
379 VkResult anv_CreatePipelineLayout(
380     VkDevice                                    _device,
381     const VkPipelineLayoutCreateInfo*           pCreateInfo,
382     const VkAllocationCallbacks*                pAllocator,
383     VkPipelineLayout*                           pPipelineLayout)
384 {
385    ANV_FROM_HANDLE(anv_device, device, _device);
386    struct anv_pipeline_layout *layout;
387
388    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
389
390    layout = vk_alloc2(&device->alloc, pAllocator, sizeof(*layout), 8,
391                        VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
392    if (layout == NULL)
393       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
394
395    layout->num_sets = pCreateInfo->setLayoutCount;
396
397    unsigned dynamic_offset_count = 0;
398
399    for (uint32_t set = 0; set < pCreateInfo->setLayoutCount; set++) {
400       ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout,
401                       pCreateInfo->pSetLayouts[set]);
402       layout->set[set].layout = set_layout;
403       anv_descriptor_set_layout_ref(set_layout);
404
405       layout->set[set].dynamic_offset_start = dynamic_offset_count;
406       for (uint32_t b = 0; b < set_layout->binding_count; b++) {
407          if (set_layout->binding[b].dynamic_offset_index < 0)
408             continue;
409
410          dynamic_offset_count += set_layout->binding[b].array_size;
411       }
412    }
413
414    struct mesa_sha1 ctx;
415    _mesa_sha1_init(&ctx);
416    for (unsigned s = 0; s < layout->num_sets; s++) {
417       sha1_update_descriptor_set_layout(&ctx, layout->set[s].layout);
418       _mesa_sha1_update(&ctx, &layout->set[s].dynamic_offset_start,
419                         sizeof(layout->set[s].dynamic_offset_start));
420    }
421    _mesa_sha1_update(&ctx, &layout->num_sets, sizeof(layout->num_sets));
422    _mesa_sha1_final(&ctx, layout->sha1);
423
424    *pPipelineLayout = anv_pipeline_layout_to_handle(layout);
425
426    return VK_SUCCESS;
427 }
428
429 void anv_DestroyPipelineLayout(
430     VkDevice                                    _device,
431     VkPipelineLayout                            _pipelineLayout,
432     const VkAllocationCallbacks*                pAllocator)
433 {
434    ANV_FROM_HANDLE(anv_device, device, _device);
435    ANV_FROM_HANDLE(anv_pipeline_layout, pipeline_layout, _pipelineLayout);
436
437    if (!pipeline_layout)
438       return;
439
440    for (uint32_t i = 0; i < pipeline_layout->num_sets; i++)
441       anv_descriptor_set_layout_unref(device, pipeline_layout->set[i].layout);
442
443    vk_free2(&device->alloc, pAllocator, pipeline_layout);
444 }
445
446 /*
447  * Descriptor pools.
448  *
449  * These are implemented using a big pool of memory and a free-list for the
450  * host memory allocations and a state_stream and a free list for the buffer
451  * view surface state. The spec allows us to fail to allocate due to
452  * fragmentation in all cases but two: 1) after pool reset, allocating up
453  * until the pool size with no freeing must succeed and 2) allocating and
454  * freeing only descriptor sets with the same layout. Case 1) is easy enogh,
455  * and the free lists lets us recycle blocks for case 2).
456  */
457
458 /* The vma heap reserves 0 to mean NULL; we have to offset by some ammount to
459  * ensure we can allocate the entire BO without hitting zero.  The actual
460  * amount doesn't matter.
461  */
462 #define POOL_HEAP_OFFSET 64
463
464 #define EMPTY 1
465
466 VkResult anv_CreateDescriptorPool(
467     VkDevice                                    _device,
468     const VkDescriptorPoolCreateInfo*           pCreateInfo,
469     const VkAllocationCallbacks*                pAllocator,
470     VkDescriptorPool*                           pDescriptorPool)
471 {
472    ANV_FROM_HANDLE(anv_device, device, _device);
473    struct anv_descriptor_pool *pool;
474
475    uint32_t descriptor_count = 0;
476    uint32_t buffer_view_count = 0;
477    uint32_t descriptor_bo_size = 0;
478    for (uint32_t i = 0; i < pCreateInfo->poolSizeCount; i++) {
479       enum anv_descriptor_data desc_data =
480          anv_descriptor_data_for_type(&device->instance->physicalDevice,
481                                       pCreateInfo->pPoolSizes[i].type);
482
483       if (desc_data & ANV_DESCRIPTOR_BUFFER_VIEW)
484          buffer_view_count += pCreateInfo->pPoolSizes[i].descriptorCount;
485
486       unsigned desc_data_size = anv_descriptor_data_size(desc_data) *
487                                 pCreateInfo->pPoolSizes[i].descriptorCount;
488       descriptor_bo_size += desc_data_size;
489
490       descriptor_count += pCreateInfo->pPoolSizes[i].descriptorCount;
491    }
492    /* We have to align descriptor buffer allocations to 32B so that we can
493     * push descriptor buffers.  This means that each descriptor buffer
494     * allocated may burn up to 32B of extra space to get the right alignment.
495     * (Technically, it's at most 28B because we're always going to start at
496     * least 4B aligned but we're being conservative here.)  Allocate enough
497     * extra space that we can chop it into maxSets pieces and align each one
498     * of them to 32B.
499     */
500    descriptor_bo_size += 32 * pCreateInfo->maxSets;
501    descriptor_bo_size = ALIGN(descriptor_bo_size, 4096);
502
503    const size_t pool_size =
504       pCreateInfo->maxSets * sizeof(struct anv_descriptor_set) +
505       descriptor_count * sizeof(struct anv_descriptor) +
506       buffer_view_count * sizeof(struct anv_buffer_view);
507    const size_t total_size = sizeof(*pool) + pool_size;
508
509    pool = vk_alloc2(&device->alloc, pAllocator, total_size, 8,
510                      VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
511    if (!pool)
512       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
513
514    pool->size = pool_size;
515    pool->next = 0;
516    pool->free_list = EMPTY;
517
518    if (descriptor_bo_size > 0) {
519       VkResult result = anv_bo_init_new(&pool->bo, device, descriptor_bo_size);
520       if (result != VK_SUCCESS) {
521          vk_free2(&device->alloc, pAllocator, pool);
522          return result;
523       }
524
525       anv_gem_set_caching(device, pool->bo.gem_handle, I915_CACHING_CACHED);
526
527       pool->bo.map = anv_gem_mmap(device, pool->bo.gem_handle, 0,
528                                   descriptor_bo_size, 0);
529       if (pool->bo.map == NULL) {
530          anv_gem_close(device, pool->bo.gem_handle);
531          vk_free2(&device->alloc, pAllocator, pool);
532          return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
533       }
534
535       if (device->instance->physicalDevice.use_softpin) {
536          pool->bo.flags |= EXEC_OBJECT_PINNED;
537          anv_vma_alloc(device, &pool->bo);
538       }
539
540       util_vma_heap_init(&pool->bo_heap, POOL_HEAP_OFFSET, descriptor_bo_size);
541    } else {
542       pool->bo.size = 0;
543    }
544
545    anv_state_stream_init(&pool->surface_state_stream,
546                          &device->surface_state_pool, 4096);
547    pool->surface_state_free_list = NULL;
548
549    *pDescriptorPool = anv_descriptor_pool_to_handle(pool);
550
551    return VK_SUCCESS;
552 }
553
554 void anv_DestroyDescriptorPool(
555     VkDevice                                    _device,
556     VkDescriptorPool                            _pool,
557     const VkAllocationCallbacks*                pAllocator)
558 {
559    ANV_FROM_HANDLE(anv_device, device, _device);
560    ANV_FROM_HANDLE(anv_descriptor_pool, pool, _pool);
561
562    if (!pool)
563       return;
564
565    if (pool->bo.size) {
566       anv_gem_munmap(pool->bo.map, pool->bo.size);
567       anv_vma_free(device, &pool->bo);
568       anv_gem_close(device, pool->bo.gem_handle);
569    }
570    anv_state_stream_finish(&pool->surface_state_stream);
571    vk_free2(&device->alloc, pAllocator, pool);
572 }
573
574 VkResult anv_ResetDescriptorPool(
575     VkDevice                                    _device,
576     VkDescriptorPool                            descriptorPool,
577     VkDescriptorPoolResetFlags                  flags)
578 {
579    ANV_FROM_HANDLE(anv_device, device, _device);
580    ANV_FROM_HANDLE(anv_descriptor_pool, pool, descriptorPool);
581
582    pool->next = 0;
583    pool->free_list = EMPTY;
584
585    if (pool->bo.size) {
586       util_vma_heap_finish(&pool->bo_heap);
587       util_vma_heap_init(&pool->bo_heap, POOL_HEAP_OFFSET, pool->bo.size);
588    }
589
590    anv_state_stream_finish(&pool->surface_state_stream);
591    anv_state_stream_init(&pool->surface_state_stream,
592                          &device->surface_state_pool, 4096);
593    pool->surface_state_free_list = NULL;
594
595    return VK_SUCCESS;
596 }
597
598 struct pool_free_list_entry {
599    uint32_t next;
600    uint32_t size;
601 };
602
603 static VkResult
604 anv_descriptor_pool_alloc_set(struct anv_descriptor_pool *pool,
605                               uint32_t size,
606                               struct anv_descriptor_set **set)
607 {
608    if (size <= pool->size - pool->next) {
609       *set = (struct anv_descriptor_set *) (pool->data + pool->next);
610       pool->next += size;
611       return VK_SUCCESS;
612    } else {
613       struct pool_free_list_entry *entry;
614       uint32_t *link = &pool->free_list;
615       for (uint32_t f = pool->free_list; f != EMPTY; f = entry->next) {
616          entry = (struct pool_free_list_entry *) (pool->data + f);
617          if (size <= entry->size) {
618             *link = entry->next;
619             *set = (struct anv_descriptor_set *) entry;
620             return VK_SUCCESS;
621          }
622          link = &entry->next;
623       }
624
625       if (pool->free_list != EMPTY) {
626          return vk_error(VK_ERROR_FRAGMENTED_POOL);
627       } else {
628          return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY);
629       }
630    }
631 }
632
633 static void
634 anv_descriptor_pool_free_set(struct anv_descriptor_pool *pool,
635                              struct anv_descriptor_set *set)
636 {
637    /* Put the descriptor set allocation back on the free list. */
638    const uint32_t index = (char *) set - pool->data;
639    if (index + set->size == pool->next) {
640       pool->next = index;
641    } else {
642       struct pool_free_list_entry *entry = (struct pool_free_list_entry *) set;
643       entry->next = pool->free_list;
644       entry->size = set->size;
645       pool->free_list = (char *) entry - pool->data;
646    }
647 }
648
649 struct surface_state_free_list_entry {
650    void *next;
651    struct anv_state state;
652 };
653
654 static struct anv_state
655 anv_descriptor_pool_alloc_state(struct anv_descriptor_pool *pool)
656 {
657    struct surface_state_free_list_entry *entry =
658       pool->surface_state_free_list;
659
660    if (entry) {
661       struct anv_state state = entry->state;
662       pool->surface_state_free_list = entry->next;
663       assert(state.alloc_size == 64);
664       return state;
665    } else {
666       return anv_state_stream_alloc(&pool->surface_state_stream, 64, 64);
667    }
668 }
669
670 static void
671 anv_descriptor_pool_free_state(struct anv_descriptor_pool *pool,
672                                struct anv_state state)
673 {
674    /* Put the buffer view surface state back on the free list. */
675    struct surface_state_free_list_entry *entry = state.map;
676    entry->next = pool->surface_state_free_list;
677    entry->state = state;
678    pool->surface_state_free_list = entry;
679 }
680
681 size_t
682 anv_descriptor_set_layout_size(const struct anv_descriptor_set_layout *layout)
683 {
684    return
685       sizeof(struct anv_descriptor_set) +
686       layout->size * sizeof(struct anv_descriptor) +
687       layout->buffer_view_count * sizeof(struct anv_buffer_view);
688 }
689
690 VkResult
691 anv_descriptor_set_create(struct anv_device *device,
692                           struct anv_descriptor_pool *pool,
693                           struct anv_descriptor_set_layout *layout,
694                           struct anv_descriptor_set **out_set)
695 {
696    struct anv_descriptor_set *set;
697    const size_t size = anv_descriptor_set_layout_size(layout);
698
699    VkResult result = anv_descriptor_pool_alloc_set(pool, size, &set);
700    if (result != VK_SUCCESS)
701       return result;
702
703    if (layout->descriptor_buffer_size) {
704       /* Align the size to 32 so that alignment gaps don't cause extra holes
705        * in the heap which can lead to bad performance.
706        */
707       uint64_t pool_vma_offset =
708          util_vma_heap_alloc(&pool->bo_heap,
709                              ALIGN(layout->descriptor_buffer_size, 32), 32);
710       if (pool_vma_offset == 0) {
711          anv_descriptor_pool_free_set(pool, set);
712          return vk_error(VK_ERROR_FRAGMENTED_POOL);
713       }
714       assert(pool_vma_offset >= POOL_HEAP_OFFSET &&
715              pool_vma_offset - POOL_HEAP_OFFSET <= INT32_MAX);
716       set->desc_mem.offset = pool_vma_offset - POOL_HEAP_OFFSET;
717       set->desc_mem.alloc_size = layout->descriptor_buffer_size;
718       set->desc_mem.map = pool->bo.map + set->desc_mem.offset;
719
720       set->desc_surface_state = anv_descriptor_pool_alloc_state(pool);
721       anv_fill_buffer_surface_state(device, set->desc_surface_state,
722                                     ISL_FORMAT_R32G32B32A32_FLOAT,
723                                     (struct anv_address) {
724                                        .bo = &pool->bo,
725                                        .offset = set->desc_mem.offset,
726                                     },
727                                     layout->descriptor_buffer_size, 1);
728    } else {
729       set->desc_mem = ANV_STATE_NULL;
730       set->desc_surface_state = ANV_STATE_NULL;
731    }
732
733    set->pool = pool;
734    set->layout = layout;
735    anv_descriptor_set_layout_ref(layout);
736
737    set->size = size;
738    set->buffer_views =
739       (struct anv_buffer_view *) &set->descriptors[layout->size];
740    set->buffer_view_count = layout->buffer_view_count;
741
742    /* By defining the descriptors to be zero now, we can later verify that
743     * a descriptor has not been populated with user data.
744     */
745    memset(set->descriptors, 0, sizeof(struct anv_descriptor) * layout->size);
746
747    /* Go through and fill out immutable samplers if we have any */
748    struct anv_descriptor *desc = set->descriptors;
749    for (uint32_t b = 0; b < layout->binding_count; b++) {
750       if (layout->binding[b].immutable_samplers) {
751          for (uint32_t i = 0; i < layout->binding[b].array_size; i++) {
752             /* The type will get changed to COMBINED_IMAGE_SAMPLER in
753              * UpdateDescriptorSets if needed.  However, if the descriptor
754              * set has an immutable sampler, UpdateDescriptorSets may never
755              * touch it, so we need to make sure it's 100% valid now.
756              */
757             desc[i] = (struct anv_descriptor) {
758                .type = VK_DESCRIPTOR_TYPE_SAMPLER,
759                .sampler = layout->binding[b].immutable_samplers[i],
760             };
761          }
762       }
763       desc += layout->binding[b].array_size;
764    }
765
766    /* Allocate surface state for the buffer views. */
767    for (uint32_t b = 0; b < layout->buffer_view_count; b++) {
768       set->buffer_views[b].surface_state =
769          anv_descriptor_pool_alloc_state(pool);
770    }
771
772    *out_set = set;
773
774    return VK_SUCCESS;
775 }
776
777 void
778 anv_descriptor_set_destroy(struct anv_device *device,
779                            struct anv_descriptor_pool *pool,
780                            struct anv_descriptor_set *set)
781 {
782    anv_descriptor_set_layout_unref(device, set->layout);
783
784    if (set->desc_mem.alloc_size) {
785       util_vma_heap_free(&pool->bo_heap,
786                          (uint64_t)set->desc_mem.offset + POOL_HEAP_OFFSET,
787                          set->desc_mem.alloc_size);
788       anv_descriptor_pool_free_state(pool, set->desc_surface_state);
789    }
790
791    for (uint32_t b = 0; b < set->buffer_view_count; b++)
792       anv_descriptor_pool_free_state(pool, set->buffer_views[b].surface_state);
793
794    anv_descriptor_pool_free_set(pool, set);
795 }
796
797 VkResult anv_AllocateDescriptorSets(
798     VkDevice                                    _device,
799     const VkDescriptorSetAllocateInfo*          pAllocateInfo,
800     VkDescriptorSet*                            pDescriptorSets)
801 {
802    ANV_FROM_HANDLE(anv_device, device, _device);
803    ANV_FROM_HANDLE(anv_descriptor_pool, pool, pAllocateInfo->descriptorPool);
804
805    VkResult result = VK_SUCCESS;
806    struct anv_descriptor_set *set;
807    uint32_t i;
808
809    for (i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
810       ANV_FROM_HANDLE(anv_descriptor_set_layout, layout,
811                       pAllocateInfo->pSetLayouts[i]);
812
813       result = anv_descriptor_set_create(device, pool, layout, &set);
814       if (result != VK_SUCCESS)
815          break;
816
817       pDescriptorSets[i] = anv_descriptor_set_to_handle(set);
818    }
819
820    if (result != VK_SUCCESS)
821       anv_FreeDescriptorSets(_device, pAllocateInfo->descriptorPool,
822                              i, pDescriptorSets);
823
824    return result;
825 }
826
827 VkResult anv_FreeDescriptorSets(
828     VkDevice                                    _device,
829     VkDescriptorPool                            descriptorPool,
830     uint32_t                                    count,
831     const VkDescriptorSet*                      pDescriptorSets)
832 {
833    ANV_FROM_HANDLE(anv_device, device, _device);
834    ANV_FROM_HANDLE(anv_descriptor_pool, pool, descriptorPool);
835
836    for (uint32_t i = 0; i < count; i++) {
837       ANV_FROM_HANDLE(anv_descriptor_set, set, pDescriptorSets[i]);
838
839       if (!set)
840          continue;
841
842       anv_descriptor_set_destroy(device, pool, set);
843    }
844
845    return VK_SUCCESS;
846 }
847
848 void
849 anv_descriptor_set_write_image_view(struct anv_device *device,
850                                     struct anv_descriptor_set *set,
851                                     const VkDescriptorImageInfo * const info,
852                                     VkDescriptorType type,
853                                     uint32_t binding,
854                                     uint32_t element)
855 {
856    const struct anv_descriptor_set_binding_layout *bind_layout =
857       &set->layout->binding[binding];
858    struct anv_descriptor *desc =
859       &set->descriptors[bind_layout->descriptor_index + element];
860    struct anv_image_view *image_view = NULL;
861    struct anv_sampler *sampler = NULL;
862
863    assert(type == bind_layout->type);
864
865    switch (type) {
866    case VK_DESCRIPTOR_TYPE_SAMPLER:
867       sampler = anv_sampler_from_handle(info->sampler);
868       break;
869
870    case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
871       image_view = anv_image_view_from_handle(info->imageView);
872       sampler = anv_sampler_from_handle(info->sampler);
873       break;
874
875    case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
876    case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
877    case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
878       image_view = anv_image_view_from_handle(info->imageView);
879       break;
880
881    default:
882       unreachable("invalid descriptor type");
883    }
884
885    /* If this descriptor has an immutable sampler, we don't want to stomp on
886     * it.
887     */
888    sampler = bind_layout->immutable_samplers ?
889              bind_layout->immutable_samplers[element] :
890              sampler;
891
892    *desc = (struct anv_descriptor) {
893       .type = type,
894       .layout = info->imageLayout,
895       .image_view = image_view,
896       .sampler = sampler,
897    };
898 }
899
900 void
901 anv_descriptor_set_write_buffer_view(struct anv_device *device,
902                                      struct anv_descriptor_set *set,
903                                      VkDescriptorType type,
904                                      struct anv_buffer_view *buffer_view,
905                                      uint32_t binding,
906                                      uint32_t element)
907 {
908    const struct anv_descriptor_set_binding_layout *bind_layout =
909       &set->layout->binding[binding];
910    struct anv_descriptor *desc =
911       &set->descriptors[bind_layout->descriptor_index + element];
912
913    assert(type == bind_layout->type);
914
915    *desc = (struct anv_descriptor) {
916       .type = type,
917       .buffer_view = buffer_view,
918    };
919 }
920
921 void
922 anv_descriptor_set_write_buffer(struct anv_device *device,
923                                 struct anv_descriptor_set *set,
924                                 struct anv_state_stream *alloc_stream,
925                                 VkDescriptorType type,
926                                 struct anv_buffer *buffer,
927                                 uint32_t binding,
928                                 uint32_t element,
929                                 VkDeviceSize offset,
930                                 VkDeviceSize range)
931 {
932    const struct anv_descriptor_set_binding_layout *bind_layout =
933       &set->layout->binding[binding];
934    struct anv_descriptor *desc =
935       &set->descriptors[bind_layout->descriptor_index + element];
936
937    assert(type == bind_layout->type);
938
939    if (type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
940        type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
941       *desc = (struct anv_descriptor) {
942          .type = type,
943          .buffer = buffer,
944          .offset = offset,
945          .range = range,
946       };
947    } else {
948       assert(bind_layout->data & ANV_DESCRIPTOR_BUFFER_VIEW);
949       struct anv_buffer_view *bview =
950          &set->buffer_views[bind_layout->buffer_view_index + element];
951
952       bview->format = anv_isl_format_for_descriptor_type(type);
953       bview->range = anv_buffer_get_range(buffer, offset, range);
954       bview->address = anv_address_add(buffer->address, offset);
955
956       /* If we're writing descriptors through a push command, we need to
957        * allocate the surface state from the command buffer. Otherwise it will
958        * be allocated by the descriptor pool when calling
959        * vkAllocateDescriptorSets. */
960       if (alloc_stream)
961          bview->surface_state = anv_state_stream_alloc(alloc_stream, 64, 64);
962
963       anv_fill_buffer_surface_state(device, bview->surface_state,
964                                     bview->format,
965                                     bview->address, bview->range, 1);
966
967       *desc = (struct anv_descriptor) {
968          .type = type,
969          .buffer_view = bview,
970       };
971    }
972 }
973
974 void anv_UpdateDescriptorSets(
975     VkDevice                                    _device,
976     uint32_t                                    descriptorWriteCount,
977     const VkWriteDescriptorSet*                 pDescriptorWrites,
978     uint32_t                                    descriptorCopyCount,
979     const VkCopyDescriptorSet*                  pDescriptorCopies)
980 {
981    ANV_FROM_HANDLE(anv_device, device, _device);
982
983    for (uint32_t i = 0; i < descriptorWriteCount; i++) {
984       const VkWriteDescriptorSet *write = &pDescriptorWrites[i];
985       ANV_FROM_HANDLE(anv_descriptor_set, set, write->dstSet);
986
987       switch (write->descriptorType) {
988       case VK_DESCRIPTOR_TYPE_SAMPLER:
989       case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
990       case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
991       case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
992       case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
993          for (uint32_t j = 0; j < write->descriptorCount; j++) {
994             anv_descriptor_set_write_image_view(device, set,
995                                                 write->pImageInfo + j,
996                                                 write->descriptorType,
997                                                 write->dstBinding,
998                                                 write->dstArrayElement + j);
999          }
1000          break;
1001
1002       case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1003       case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1004          for (uint32_t j = 0; j < write->descriptorCount; j++) {
1005             ANV_FROM_HANDLE(anv_buffer_view, bview,
1006                             write->pTexelBufferView[j]);
1007
1008             anv_descriptor_set_write_buffer_view(device, set,
1009                                                  write->descriptorType,
1010                                                  bview,
1011                                                  write->dstBinding,
1012                                                  write->dstArrayElement + j);
1013          }
1014          break;
1015
1016       case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1017       case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1018       case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1019       case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1020          for (uint32_t j = 0; j < write->descriptorCount; j++) {
1021             assert(write->pBufferInfo[j].buffer);
1022             ANV_FROM_HANDLE(anv_buffer, buffer, write->pBufferInfo[j].buffer);
1023             assert(buffer);
1024
1025             anv_descriptor_set_write_buffer(device, set,
1026                                             NULL,
1027                                             write->descriptorType,
1028                                             buffer,
1029                                             write->dstBinding,
1030                                             write->dstArrayElement + j,
1031                                             write->pBufferInfo[j].offset,
1032                                             write->pBufferInfo[j].range);
1033          }
1034          break;
1035
1036       default:
1037          break;
1038       }
1039    }
1040
1041    for (uint32_t i = 0; i < descriptorCopyCount; i++) {
1042       const VkCopyDescriptorSet *copy = &pDescriptorCopies[i];
1043       ANV_FROM_HANDLE(anv_descriptor_set, src, copy->srcSet);
1044       ANV_FROM_HANDLE(anv_descriptor_set, dst, copy->dstSet);
1045
1046       const struct anv_descriptor_set_binding_layout *src_layout =
1047          &src->layout->binding[copy->srcBinding];
1048       struct anv_descriptor *src_desc =
1049          &src->descriptors[src_layout->descriptor_index];
1050       src_desc += copy->srcArrayElement;
1051
1052       const struct anv_descriptor_set_binding_layout *dst_layout =
1053          &dst->layout->binding[copy->dstBinding];
1054       struct anv_descriptor *dst_desc =
1055          &dst->descriptors[dst_layout->descriptor_index];
1056       dst_desc += copy->dstArrayElement;
1057
1058       for (uint32_t j = 0; j < copy->descriptorCount; j++)
1059          dst_desc[j] = src_desc[j];
1060
1061       unsigned desc_size = anv_descriptor_size(src_layout);
1062       if (desc_size > 0) {
1063          assert(desc_size == anv_descriptor_size(dst_layout));
1064          memcpy(dst->desc_mem.map + dst_layout->descriptor_offset +
1065                                     copy->dstArrayElement * desc_size,
1066                 src->desc_mem.map + src_layout->descriptor_offset +
1067                                     copy->srcArrayElement * desc_size,
1068                 copy->descriptorCount * desc_size);
1069       }
1070    }
1071 }
1072
1073 /*
1074  * Descriptor update templates.
1075  */
1076
1077 void
1078 anv_descriptor_set_write_template(struct anv_device *device,
1079                                   struct anv_descriptor_set *set,
1080                                   struct anv_state_stream *alloc_stream,
1081                                   const struct anv_descriptor_update_template *template,
1082                                   const void *data)
1083 {
1084    for (uint32_t i = 0; i < template->entry_count; i++) {
1085       const struct anv_descriptor_template_entry *entry =
1086          &template->entries[i];
1087
1088       switch (entry->type) {
1089       case VK_DESCRIPTOR_TYPE_SAMPLER:
1090       case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1091       case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1092       case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1093       case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1094          for (uint32_t j = 0; j < entry->array_count; j++) {
1095             const VkDescriptorImageInfo *info =
1096                data + entry->offset + j * entry->stride;
1097             anv_descriptor_set_write_image_view(device, set,
1098                                                 info, entry->type,
1099                                                 entry->binding,
1100                                                 entry->array_element + j);
1101          }
1102          break;
1103
1104       case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1105       case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1106          for (uint32_t j = 0; j < entry->array_count; j++) {
1107             const VkBufferView *_bview =
1108                data + entry->offset + j * entry->stride;
1109             ANV_FROM_HANDLE(anv_buffer_view, bview, *_bview);
1110
1111             anv_descriptor_set_write_buffer_view(device, set,
1112                                                  entry->type,
1113                                                  bview,
1114                                                  entry->binding,
1115                                                  entry->array_element + j);
1116          }
1117          break;
1118
1119       case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1120       case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1121       case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1122       case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1123          for (uint32_t j = 0; j < entry->array_count; j++) {
1124             const VkDescriptorBufferInfo *info =
1125                data + entry->offset + j * entry->stride;
1126             ANV_FROM_HANDLE(anv_buffer, buffer, info->buffer);
1127
1128             anv_descriptor_set_write_buffer(device, set,
1129                                             alloc_stream,
1130                                             entry->type,
1131                                             buffer,
1132                                             entry->binding,
1133                                             entry->array_element + j,
1134                                             info->offset, info->range);
1135          }
1136          break;
1137
1138       default:
1139          break;
1140       }
1141    }
1142 }
1143
1144 VkResult anv_CreateDescriptorUpdateTemplate(
1145     VkDevice                                    _device,
1146     const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
1147     const VkAllocationCallbacks*                pAllocator,
1148     VkDescriptorUpdateTemplate*                 pDescriptorUpdateTemplate)
1149 {
1150    ANV_FROM_HANDLE(anv_device, device, _device);
1151    struct anv_descriptor_update_template *template;
1152
1153    size_t size = sizeof(*template) +
1154       pCreateInfo->descriptorUpdateEntryCount * sizeof(template->entries[0]);
1155    template = vk_alloc2(&device->alloc, pAllocator, size, 8,
1156                         VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1157    if (template == NULL)
1158       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1159
1160    template->bind_point = pCreateInfo->pipelineBindPoint;
1161
1162    if (pCreateInfo->templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET)
1163       template->set = pCreateInfo->set;
1164
1165    template->entry_count = pCreateInfo->descriptorUpdateEntryCount;
1166    for (uint32_t i = 0; i < template->entry_count; i++) {
1167       const VkDescriptorUpdateTemplateEntry *pEntry =
1168          &pCreateInfo->pDescriptorUpdateEntries[i];
1169
1170       template->entries[i] = (struct anv_descriptor_template_entry) {
1171          .type = pEntry->descriptorType,
1172          .binding = pEntry->dstBinding,
1173          .array_element = pEntry->dstArrayElement,
1174          .array_count = pEntry->descriptorCount,
1175          .offset = pEntry->offset,
1176          .stride = pEntry->stride,
1177       };
1178    }
1179
1180    *pDescriptorUpdateTemplate =
1181       anv_descriptor_update_template_to_handle(template);
1182
1183    return VK_SUCCESS;
1184 }
1185
1186 void anv_DestroyDescriptorUpdateTemplate(
1187     VkDevice                                    _device,
1188     VkDescriptorUpdateTemplate                  descriptorUpdateTemplate,
1189     const VkAllocationCallbacks*                pAllocator)
1190 {
1191    ANV_FROM_HANDLE(anv_device, device, _device);
1192    ANV_FROM_HANDLE(anv_descriptor_update_template, template,
1193                    descriptorUpdateTemplate);
1194
1195    vk_free2(&device->alloc, pAllocator, template);
1196 }
1197
1198 void anv_UpdateDescriptorSetWithTemplate(
1199     VkDevice                                    _device,
1200     VkDescriptorSet                             descriptorSet,
1201     VkDescriptorUpdateTemplate                  descriptorUpdateTemplate,
1202     const void*                                 pData)
1203 {
1204    ANV_FROM_HANDLE(anv_device, device, _device);
1205    ANV_FROM_HANDLE(anv_descriptor_set, set, descriptorSet);
1206    ANV_FROM_HANDLE(anv_descriptor_update_template, template,
1207                    descriptorUpdateTemplate);
1208
1209    anv_descriptor_set_write_template(device, set, NULL, template, pData);
1210 }