OSDN Git Service

Merge remote-tracking branch 'origin/master' into vulkan
[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 "anv_private.h"
31
32 /*
33  * Descriptor set layouts.
34  */
35
36 VkResult anv_CreateDescriptorSetLayout(
37     VkDevice                                    _device,
38     const VkDescriptorSetLayoutCreateInfo*      pCreateInfo,
39     const VkAllocationCallbacks*                pAllocator,
40     VkDescriptorSetLayout*                      pSetLayout)
41 {
42    ANV_FROM_HANDLE(anv_device, device, _device);
43    struct anv_descriptor_set_layout *set_layout;
44
45    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO);
46
47    uint32_t max_binding = 0;
48    uint32_t immutable_sampler_count = 0;
49    for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
50       max_binding = MAX2(max_binding, pCreateInfo->pBindings[j].binding);
51       if (pCreateInfo->pBindings[j].pImmutableSamplers)
52          immutable_sampler_count += pCreateInfo->pBindings[j].descriptorCount;
53    }
54
55    size_t size = sizeof(struct anv_descriptor_set_layout) +
56                  (max_binding + 1) * sizeof(set_layout->binding[0]) +
57                  immutable_sampler_count * sizeof(struct anv_sampler *);
58
59    set_layout = anv_alloc2(&device->alloc, pAllocator, size, 8,
60                            VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
61    if (!set_layout)
62       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
63
64    /* We just allocate all the samplers at the end of the struct */
65    struct anv_sampler **samplers =
66       (struct anv_sampler **)&set_layout->binding[max_binding + 1];
67
68    set_layout->binding_count = max_binding + 1;
69    set_layout->shader_stages = 0;
70    set_layout->size = 0;
71
72    for (uint32_t b = 0; b <= max_binding; b++) {
73       /* Initialize all binding_layout entries to -1 */
74       memset(&set_layout->binding[b], -1, sizeof(set_layout->binding[b]));
75
76       set_layout->binding[b].immutable_samplers = NULL;
77    }
78
79    /* Initialize all samplers to 0 */
80    memset(samplers, 0, immutable_sampler_count * sizeof(*samplers));
81
82    uint32_t sampler_count[MESA_SHADER_STAGES] = { 0, };
83    uint32_t surface_count[MESA_SHADER_STAGES] = { 0, };
84    uint32_t image_count[MESA_SHADER_STAGES] = { 0, };
85    uint32_t buffer_count = 0;
86    uint32_t dynamic_offset_count = 0;
87
88    for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
89       const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[j];
90       uint32_t b = binding->binding;
91
92       assert(binding->descriptorCount > 0);
93       set_layout->binding[b].array_size = binding->descriptorCount;
94       set_layout->binding[b].descriptor_index = set_layout->size;
95       set_layout->size += binding->descriptorCount;
96
97       switch (binding->descriptorType) {
98       case VK_DESCRIPTOR_TYPE_SAMPLER:
99       case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
100          anv_foreach_stage(s, binding->stageFlags) {
101             set_layout->binding[b].stage[s].sampler_index = sampler_count[s];
102             sampler_count[s] += binding->descriptorCount;
103          }
104          break;
105       default:
106          break;
107       }
108
109       switch (binding->descriptorType) {
110       case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
111       case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
112       case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
113       case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
114          set_layout->binding[b].buffer_index = buffer_count;
115          buffer_count += binding->descriptorCount;
116          /* fall through */
117
118       case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
119       case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
120       case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
121       case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
122       case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
123       case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
124          anv_foreach_stage(s, binding->stageFlags) {
125             set_layout->binding[b].stage[s].surface_index = surface_count[s];
126             surface_count[s] += binding->descriptorCount;
127          }
128          break;
129       default:
130          break;
131       }
132
133       switch (binding->descriptorType) {
134       case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
135       case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
136          set_layout->binding[b].dynamic_offset_index = dynamic_offset_count;
137          dynamic_offset_count += binding->descriptorCount;
138          break;
139       default:
140          break;
141       }
142
143       switch (binding->descriptorType) {
144       case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
145       case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
146          anv_foreach_stage(s, binding->stageFlags) {
147             set_layout->binding[b].stage[s].image_index = image_count[s];
148             image_count[s] += binding->descriptorCount;
149          }
150          break;
151       default:
152          break;
153       }
154
155       if (binding->pImmutableSamplers) {
156          set_layout->binding[b].immutable_samplers = samplers;
157          samplers += binding->descriptorCount;
158
159          for (uint32_t i = 0; i < binding->descriptorCount; i++)
160             set_layout->binding[b].immutable_samplers[i] =
161                anv_sampler_from_handle(binding->pImmutableSamplers[i]);
162       } else {
163          set_layout->binding[b].immutable_samplers = NULL;
164       }
165
166       set_layout->shader_stages |= binding->stageFlags;
167    }
168
169    set_layout->buffer_count = buffer_count;
170    set_layout->dynamic_offset_count = dynamic_offset_count;
171
172    *pSetLayout = anv_descriptor_set_layout_to_handle(set_layout);
173
174    return VK_SUCCESS;
175 }
176
177 void anv_DestroyDescriptorSetLayout(
178     VkDevice                                    _device,
179     VkDescriptorSetLayout                       _set_layout,
180     const VkAllocationCallbacks*                pAllocator)
181 {
182    ANV_FROM_HANDLE(anv_device, device, _device);
183    ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout, _set_layout);
184
185    anv_free2(&device->alloc, pAllocator, set_layout);
186 }
187
188 /*
189  * Pipeline layouts.  These have nothing to do with the pipeline.  They are
190  * just muttiple descriptor set layouts pasted together
191  */
192
193 VkResult anv_CreatePipelineLayout(
194     VkDevice                                    _device,
195     const VkPipelineLayoutCreateInfo*           pCreateInfo,
196     const VkAllocationCallbacks*                pAllocator,
197     VkPipelineLayout*                           pPipelineLayout)
198 {
199    ANV_FROM_HANDLE(anv_device, device, _device);
200    struct anv_pipeline_layout *layout;
201
202    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
203
204    layout = anv_alloc2(&device->alloc, pAllocator, sizeof(*layout), 8,
205                        VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
206    if (layout == NULL)
207       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
208
209    layout->num_sets = pCreateInfo->setLayoutCount;
210
211    unsigned dynamic_offset_count = 0;
212
213    memset(layout->stage, 0, sizeof(layout->stage));
214    for (uint32_t set = 0; set < pCreateInfo->setLayoutCount; set++) {
215       ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout,
216                       pCreateInfo->pSetLayouts[set]);
217       layout->set[set].layout = set_layout;
218
219       layout->set[set].dynamic_offset_start = dynamic_offset_count;
220       for (uint32_t b = 0; b < set_layout->binding_count; b++) {
221          if (set_layout->binding[b].dynamic_offset_index < 0)
222             continue;
223
224          dynamic_offset_count += set_layout->binding[b].array_size;
225          for (gl_shader_stage s = 0; s < MESA_SHADER_STAGES; s++) {
226             if (set_layout->binding[b].stage[s].surface_index >= 0)
227                layout->stage[s].has_dynamic_offsets = true;
228          }
229       }
230    }
231
232    *pPipelineLayout = anv_pipeline_layout_to_handle(layout);
233
234    return VK_SUCCESS;
235 }
236
237 void anv_DestroyPipelineLayout(
238     VkDevice                                    _device,
239     VkPipelineLayout                            _pipelineLayout,
240     const VkAllocationCallbacks*                pAllocator)
241 {
242    ANV_FROM_HANDLE(anv_device, device, _device);
243    ANV_FROM_HANDLE(anv_pipeline_layout, pipeline_layout, _pipelineLayout);
244
245    anv_free2(&device->alloc, pAllocator, pipeline_layout);
246 }
247
248 /*
249  * Descriptor pools.
250  *
251  * These are implemented using a big pool of memory and a free-list for the
252  * host memory allocations and a state_stream and a free list for the buffer
253  * view surface state. The spec allows us to fail to allocate due to
254  * fragmentation in all cases but two: 1) after pool reset, allocating up
255  * until the pool size with no freeing must succeed and 2) allocating and
256  * freeing only descriptor sets with the same layout. Case 1) is easy enogh,
257  * and the free lists lets us recycle blocks for case 2).
258  */
259
260 #define EMPTY 1
261
262 VkResult anv_CreateDescriptorPool(
263     VkDevice                                    _device,
264     const VkDescriptorPoolCreateInfo*           pCreateInfo,
265     const VkAllocationCallbacks*                pAllocator,
266     VkDescriptorPool*                           pDescriptorPool)
267 {
268    ANV_FROM_HANDLE(anv_device, device, _device);
269    struct anv_descriptor_pool *pool;
270
271    uint32_t descriptor_count = 0;
272    uint32_t buffer_count = 0;
273    for (uint32_t i = 0; i < pCreateInfo->poolSizeCount; i++) {
274       switch (pCreateInfo->pPoolSizes[i].type) {
275       case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
276       case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
277       case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
278       case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
279          buffer_count += pCreateInfo->pPoolSizes[i].descriptorCount;
280       default:
281          descriptor_count += pCreateInfo->pPoolSizes[i].descriptorCount;
282          break;
283       }
284    }
285
286    const size_t size =
287       sizeof(*pool) +
288       pCreateInfo->maxSets * sizeof(struct anv_descriptor_set) +
289       descriptor_count * sizeof(struct anv_descriptor) +
290       buffer_count * sizeof(struct anv_buffer_view);
291
292    pool = anv_alloc2(&device->alloc, pAllocator, size, 8,
293                      VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
294    if (!pool)
295       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
296
297    pool->size = size;
298    pool->next = 0;
299    pool->free_list = EMPTY;
300
301    anv_state_stream_init(&pool->surface_state_stream,
302                          &device->surface_state_block_pool);
303    pool->surface_state_free_list = NULL;
304
305    *pDescriptorPool = anv_descriptor_pool_to_handle(pool);
306
307    return VK_SUCCESS;
308 }
309
310 void anv_DestroyDescriptorPool(
311     VkDevice                                    _device,
312     VkDescriptorPool                            _pool,
313     const VkAllocationCallbacks*                pAllocator)
314 {
315    ANV_FROM_HANDLE(anv_device, device, _device);
316    ANV_FROM_HANDLE(anv_descriptor_pool, pool, _pool);
317
318    anv_state_stream_finish(&pool->surface_state_stream);
319    anv_free2(&device->alloc, pAllocator, pool);
320 }
321
322 VkResult anv_ResetDescriptorPool(
323     VkDevice                                    _device,
324     VkDescriptorPool                            descriptorPool,
325     VkDescriptorPoolResetFlags                  flags)
326 {
327    ANV_FROM_HANDLE(anv_device, device, _device);
328    ANV_FROM_HANDLE(anv_descriptor_pool, pool, descriptorPool);
329
330    pool->next = 0;
331    pool->free_list = EMPTY;
332    anv_state_stream_finish(&pool->surface_state_stream);
333    anv_state_stream_init(&pool->surface_state_stream,
334                          &device->surface_state_block_pool);
335    pool->surface_state_free_list = NULL;
336
337    return VK_SUCCESS;
338 }
339
340 struct pool_free_list_entry {
341    uint32_t next;
342    uint32_t size;
343 };
344
345 static size_t
346 layout_size(const struct anv_descriptor_set_layout *layout)
347 {
348    return
349       sizeof(struct anv_descriptor_set) +
350       layout->size * sizeof(struct anv_descriptor) +
351       layout->buffer_count * sizeof(struct anv_buffer_view);
352 }
353
354 struct surface_state_free_list_entry {
355    void *next;
356    uint32_t offset;
357 };
358
359 VkResult
360 anv_descriptor_set_create(struct anv_device *device,
361                           struct anv_descriptor_pool *pool,
362                           const struct anv_descriptor_set_layout *layout,
363                           struct anv_descriptor_set **out_set)
364 {
365    struct anv_descriptor_set *set;
366    const size_t size = layout_size(layout);
367
368    set = NULL;
369    if (size <= pool->size - pool->next) {
370       set = (struct anv_descriptor_set *) (pool->data + pool->next);
371       pool->next += size;
372    } else {
373       struct pool_free_list_entry *entry;
374       uint32_t *link = &pool->free_list;
375       for (uint32_t f = pool->free_list; f != EMPTY; f = entry->next) {
376          entry = (struct pool_free_list_entry *) (pool->data + f);
377          if (size <= entry->size) {
378             *link = entry->next;
379             set = (struct anv_descriptor_set *) entry;
380             break;
381          }
382          link = &entry->next;
383       }
384    }
385
386    if (set == NULL)
387       return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
388
389    set->size = size;
390    set->layout = layout;
391    set->buffer_views =
392       (struct anv_buffer_view *) &set->descriptors[layout->size];
393    set->buffer_count = layout->buffer_count;
394
395    /* Go through and fill out immutable samplers if we have any */
396    struct anv_descriptor *desc = set->descriptors;
397    for (uint32_t b = 0; b < layout->binding_count; b++) {
398       if (layout->binding[b].immutable_samplers) {
399          for (uint32_t i = 0; i < layout->binding[b].array_size; i++) {
400             /* The type will get changed to COMBINED_IMAGE_SAMPLER in
401              * UpdateDescriptorSets if needed.  However, if the descriptor
402              * set has an immutable sampler, UpdateDescriptorSets may never
403              * touch it, so we need to make sure it's 100% valid now.
404              */
405             desc[i] = (struct anv_descriptor) {
406                .type = VK_DESCRIPTOR_TYPE_SAMPLER,
407                .sampler = layout->binding[b].immutable_samplers[i],
408             };
409          }
410       }
411       desc += layout->binding[b].array_size;
412    }
413
414    /* Allocate surface state for the buffer views. */
415    for (uint32_t b = 0; b < layout->buffer_count; b++) {
416       struct surface_state_free_list_entry *entry =
417          pool->surface_state_free_list;
418       struct anv_state state;
419
420       if (entry) {
421          state.map = entry;
422          state.offset = entry->offset;
423          state.alloc_size = 64;
424          pool->surface_state_free_list = entry->next;
425       } else {
426          state = anv_state_stream_alloc(&pool->surface_state_stream, 64, 64);
427       }
428
429       set->buffer_views[b].surface_state = state;
430    }
431
432    *out_set = set;
433
434    return VK_SUCCESS;
435 }
436
437 void
438 anv_descriptor_set_destroy(struct anv_device *device,
439                            struct anv_descriptor_pool *pool,
440                            struct anv_descriptor_set *set)
441 {
442    /* Put the buffer view surface state back on the free list. */
443    for (uint32_t b = 0; b < set->buffer_count; b++) {
444       struct surface_state_free_list_entry *entry =
445          set->buffer_views[b].surface_state.map;
446       entry->next = pool->surface_state_free_list;
447       pool->surface_state_free_list = entry;
448    }
449
450    /* Put the descriptor set allocation back on the free list. */
451    const uint32_t index = (char *) set - pool->data;
452    if (index + set->size == pool->next) {
453       pool->next = index;
454    } else {
455       struct pool_free_list_entry *entry = (struct pool_free_list_entry *) set;
456       entry->next = pool->free_list;
457       entry->size = set->size;
458       pool->free_list = (char *) entry - pool->data;
459    }
460 }
461
462 VkResult anv_AllocateDescriptorSets(
463     VkDevice                                    _device,
464     const VkDescriptorSetAllocateInfo*          pAllocateInfo,
465     VkDescriptorSet*                            pDescriptorSets)
466 {
467    ANV_FROM_HANDLE(anv_device, device, _device);
468    ANV_FROM_HANDLE(anv_descriptor_pool, pool, pAllocateInfo->descriptorPool);
469
470    VkResult result = VK_SUCCESS;
471    struct anv_descriptor_set *set;
472    uint32_t i;
473
474    for (i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
475       ANV_FROM_HANDLE(anv_descriptor_set_layout, layout,
476                       pAllocateInfo->pSetLayouts[i]);
477
478       result = anv_descriptor_set_create(device, pool, layout, &set);
479       if (result != VK_SUCCESS)
480          break;
481
482       pDescriptorSets[i] = anv_descriptor_set_to_handle(set);
483    }
484
485    if (result != VK_SUCCESS)
486       anv_FreeDescriptorSets(_device, pAllocateInfo->descriptorPool,
487                              i, pDescriptorSets);
488
489    return result;
490 }
491
492 VkResult anv_FreeDescriptorSets(
493     VkDevice                                    _device,
494     VkDescriptorPool                            descriptorPool,
495     uint32_t                                    count,
496     const VkDescriptorSet*                      pDescriptorSets)
497 {
498    ANV_FROM_HANDLE(anv_device, device, _device);
499    ANV_FROM_HANDLE(anv_descriptor_pool, pool, descriptorPool);
500
501    for (uint32_t i = 0; i < count; i++) {
502       ANV_FROM_HANDLE(anv_descriptor_set, set, pDescriptorSets[i]);
503
504       anv_descriptor_set_destroy(device, pool, set);
505    }
506
507    return VK_SUCCESS;
508 }
509
510 void anv_UpdateDescriptorSets(
511     VkDevice                                    _device,
512     uint32_t                                    descriptorWriteCount,
513     const VkWriteDescriptorSet*                 pDescriptorWrites,
514     uint32_t                                    descriptorCopyCount,
515     const VkCopyDescriptorSet*                  pDescriptorCopies)
516 {
517    ANV_FROM_HANDLE(anv_device, device, _device);
518
519    for (uint32_t i = 0; i < descriptorWriteCount; i++) {
520       const VkWriteDescriptorSet *write = &pDescriptorWrites[i];
521       ANV_FROM_HANDLE(anv_descriptor_set, set, write->dstSet);
522       const struct anv_descriptor_set_binding_layout *bind_layout =
523          &set->layout->binding[write->dstBinding];
524       struct anv_descriptor *desc =
525          &set->descriptors[bind_layout->descriptor_index];
526       desc += write->dstArrayElement;
527
528       switch (write->descriptorType) {
529       case VK_DESCRIPTOR_TYPE_SAMPLER:
530          for (uint32_t j = 0; j < write->descriptorCount; j++) {
531             ANV_FROM_HANDLE(anv_sampler, sampler,
532                             write->pImageInfo[j].sampler);
533
534             desc[j] = (struct anv_descriptor) {
535                .type = VK_DESCRIPTOR_TYPE_SAMPLER,
536                .sampler = sampler,
537             };
538          }
539          break;
540
541       case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
542          for (uint32_t j = 0; j < write->descriptorCount; j++) {
543             ANV_FROM_HANDLE(anv_image_view, iview,
544                             write->pImageInfo[j].imageView);
545             ANV_FROM_HANDLE(anv_sampler, sampler,
546                             write->pImageInfo[j].sampler);
547
548             desc[j].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
549             desc[j].image_view = iview;
550
551             /* If this descriptor has an immutable sampler, we don't want
552              * to stomp on it.
553              */
554             if (sampler)
555                desc[j].sampler = sampler;
556          }
557          break;
558
559       case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
560       case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
561          for (uint32_t j = 0; j < write->descriptorCount; j++) {
562             ANV_FROM_HANDLE(anv_image_view, iview,
563                             write->pImageInfo[j].imageView);
564
565             desc[j] = (struct anv_descriptor) {
566                .type = write->descriptorType,
567                .image_view = iview,
568             };
569          }
570          break;
571
572       case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
573       case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
574          for (uint32_t j = 0; j < write->descriptorCount; j++) {
575             ANV_FROM_HANDLE(anv_buffer_view, bview,
576                             write->pTexelBufferView[j]);
577
578             desc[j] = (struct anv_descriptor) {
579                .type = write->descriptorType,
580                .buffer_view = bview,
581             };
582          }
583          break;
584
585       case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
586          anv_finishme("input attachments not implemented");
587          break;
588
589       case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
590       case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
591       case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
592       case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
593          for (uint32_t j = 0; j < write->descriptorCount; j++) {
594             assert(write->pBufferInfo[j].buffer);
595             ANV_FROM_HANDLE(anv_buffer, buffer, write->pBufferInfo[j].buffer);
596             assert(buffer);
597
598             struct anv_buffer_view *view =
599                &set->buffer_views[bind_layout->buffer_index];
600             view += write->dstArrayElement + j;
601
602             const struct anv_format *format =
603                anv_format_for_descriptor_type(write->descriptorType);
604
605             view->format = format->isl_format;
606             view->bo = buffer->bo;
607             view->offset = buffer->offset + write->pBufferInfo[j].offset;
608
609             /* For buffers with dynamic offsets, we use the full possible
610              * range in the surface state and do the actual range-checking
611              * in the shader.
612              */
613             if (bind_layout->dynamic_offset_index >= 0 ||
614                 write->pBufferInfo[j].range == VK_WHOLE_SIZE)
615                view->range = buffer->size - write->pBufferInfo[j].offset;
616             else
617                view->range = write->pBufferInfo[j].range;
618
619             anv_fill_buffer_surface_state(device, view->surface_state,
620                                           view->format,
621                                           view->offset, view->range, 1);
622
623             desc[j] = (struct anv_descriptor) {
624                .type = write->descriptorType,
625                .buffer_view = view,
626             };
627
628          }
629
630       default:
631          break;
632       }
633    }
634
635    for (uint32_t i = 0; i < descriptorCopyCount; i++) {
636       const VkCopyDescriptorSet *copy = &pDescriptorCopies[i];
637       ANV_FROM_HANDLE(anv_descriptor_set, src, copy->dstSet);
638       ANV_FROM_HANDLE(anv_descriptor_set, dst, copy->dstSet);
639
640       const struct anv_descriptor_set_binding_layout *src_layout =
641          &src->layout->binding[copy->srcBinding];
642       struct anv_descriptor *src_desc =
643          &src->descriptors[src_layout->descriptor_index];
644       src_desc += copy->srcArrayElement;
645
646       const struct anv_descriptor_set_binding_layout *dst_layout =
647          &dst->layout->binding[copy->dstBinding];
648       struct anv_descriptor *dst_desc =
649          &dst->descriptors[dst_layout->descriptor_index];
650       dst_desc += copy->dstArrayElement;
651
652       for (uint32_t j = 0; j < copy->descriptorCount; j++)
653          dst_desc[j] = src_desc[j];
654    }
655 }