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