anv/descriptor_set: Fix binding partly undefined descriptor sets
[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 /* By defining the descriptors to be zero now, we can later verify that
413 * a descriptor has not been populated with user data.
414 */
415 memset(set->descriptors, 0, sizeof(struct anv_descriptor) * layout->size);
416
417 /* Go through and fill out immutable samplers if we have any */
418 struct anv_descriptor *desc = set->descriptors;
419 for (uint32_t b = 0; b < layout->binding_count; b++) {
420 if (layout->binding[b].immutable_samplers) {
421 for (uint32_t i = 0; i < layout->binding[b].array_size; i++) {
422 /* The type will get changed to COMBINED_IMAGE_SAMPLER in
423 * UpdateDescriptorSets if needed. However, if the descriptor
424 * set has an immutable sampler, UpdateDescriptorSets may never
425 * touch it, so we need to make sure it's 100% valid now.
426 */
427 desc[i] = (struct anv_descriptor) {
428 .type = VK_DESCRIPTOR_TYPE_SAMPLER,
429 .sampler = layout->binding[b].immutable_samplers[i],
430 };
431 }
432 }
433 desc += layout->binding[b].array_size;
434 }
435
436 /* Allocate surface state for the buffer views. */
437 for (uint32_t b = 0; b < layout->buffer_count; b++) {
438 struct surface_state_free_list_entry *entry =
439 pool->surface_state_free_list;
440 struct anv_state state;
441
442 if (entry) {
443 state.map = entry;
444 state.offset = entry->offset;
445 state.alloc_size = 64;
446 pool->surface_state_free_list = entry->next;
447 } else {
448 state = anv_state_stream_alloc(&pool->surface_state_stream, 64, 64);
449 }
450
451 set->buffer_views[b].surface_state = state;
452 }
453
454 *out_set = set;
455
456 return VK_SUCCESS;
457 }
458
459 void
460 anv_descriptor_set_destroy(struct anv_device *device,
461 struct anv_descriptor_pool *pool,
462 struct anv_descriptor_set *set)
463 {
464 /* Put the buffer view surface state back on the free list. */
465 for (uint32_t b = 0; b < set->buffer_count; b++) {
466 struct surface_state_free_list_entry *entry =
467 set->buffer_views[b].surface_state.map;
468 entry->next = pool->surface_state_free_list;
469 pool->surface_state_free_list = entry;
470 }
471
472 /* Put the descriptor set allocation back on the free list. */
473 const uint32_t index = (char *) set - pool->data;
474 if (index + set->size == pool->next) {
475 pool->next = index;
476 } else {
477 struct pool_free_list_entry *entry = (struct pool_free_list_entry *) set;
478 entry->next = pool->free_list;
479 entry->size = set->size;
480 pool->free_list = (char *) entry - pool->data;
481 }
482 }
483
484 VkResult anv_AllocateDescriptorSets(
485 VkDevice _device,
486 const VkDescriptorSetAllocateInfo* pAllocateInfo,
487 VkDescriptorSet* pDescriptorSets)
488 {
489 ANV_FROM_HANDLE(anv_device, device, _device);
490 ANV_FROM_HANDLE(anv_descriptor_pool, pool, pAllocateInfo->descriptorPool);
491
492 VkResult result = VK_SUCCESS;
493 struct anv_descriptor_set *set;
494 uint32_t i;
495
496 for (i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
497 ANV_FROM_HANDLE(anv_descriptor_set_layout, layout,
498 pAllocateInfo->pSetLayouts[i]);
499
500 result = anv_descriptor_set_create(device, pool, layout, &set);
501 if (result != VK_SUCCESS)
502 break;
503
504 pDescriptorSets[i] = anv_descriptor_set_to_handle(set);
505 }
506
507 if (result != VK_SUCCESS)
508 anv_FreeDescriptorSets(_device, pAllocateInfo->descriptorPool,
509 i, pDescriptorSets);
510
511 return result;
512 }
513
514 VkResult anv_FreeDescriptorSets(
515 VkDevice _device,
516 VkDescriptorPool descriptorPool,
517 uint32_t count,
518 const VkDescriptorSet* pDescriptorSets)
519 {
520 ANV_FROM_HANDLE(anv_device, device, _device);
521 ANV_FROM_HANDLE(anv_descriptor_pool, pool, descriptorPool);
522
523 for (uint32_t i = 0; i < count; i++) {
524 ANV_FROM_HANDLE(anv_descriptor_set, set, pDescriptorSets[i]);
525
526 anv_descriptor_set_destroy(device, pool, set);
527 }
528
529 return VK_SUCCESS;
530 }
531
532 void anv_UpdateDescriptorSets(
533 VkDevice _device,
534 uint32_t descriptorWriteCount,
535 const VkWriteDescriptorSet* pDescriptorWrites,
536 uint32_t descriptorCopyCount,
537 const VkCopyDescriptorSet* pDescriptorCopies)
538 {
539 ANV_FROM_HANDLE(anv_device, device, _device);
540
541 for (uint32_t i = 0; i < descriptorWriteCount; i++) {
542 const VkWriteDescriptorSet *write = &pDescriptorWrites[i];
543 ANV_FROM_HANDLE(anv_descriptor_set, set, write->dstSet);
544 const struct anv_descriptor_set_binding_layout *bind_layout =
545 &set->layout->binding[write->dstBinding];
546 struct anv_descriptor *desc =
547 &set->descriptors[bind_layout->descriptor_index];
548 desc += write->dstArrayElement;
549
550 assert(write->descriptorType == bind_layout->type);
551
552 switch (write->descriptorType) {
553 case VK_DESCRIPTOR_TYPE_SAMPLER:
554 for (uint32_t j = 0; j < write->descriptorCount; j++) {
555 ANV_FROM_HANDLE(anv_sampler, sampler,
556 write->pImageInfo[j].sampler);
557
558 desc[j] = (struct anv_descriptor) {
559 .type = VK_DESCRIPTOR_TYPE_SAMPLER,
560 .sampler = sampler,
561 };
562 }
563 break;
564
565 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
566 for (uint32_t j = 0; j < write->descriptorCount; j++) {
567 ANV_FROM_HANDLE(anv_image_view, iview,
568 write->pImageInfo[j].imageView);
569 ANV_FROM_HANDLE(anv_sampler, sampler,
570 write->pImageInfo[j].sampler);
571
572 desc[j].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
573 desc[j].image_view = iview;
574
575 /* If this descriptor has an immutable sampler, we don't want
576 * to stomp on it.
577 */
578 if (sampler)
579 desc[j].sampler = sampler;
580 }
581 break;
582
583 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
584 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
585 for (uint32_t j = 0; j < write->descriptorCount; j++) {
586 ANV_FROM_HANDLE(anv_image_view, iview,
587 write->pImageInfo[j].imageView);
588
589 desc[j] = (struct anv_descriptor) {
590 .type = write->descriptorType,
591 .image_view = iview,
592 };
593 }
594 break;
595
596 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
597 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
598 for (uint32_t j = 0; j < write->descriptorCount; j++) {
599 ANV_FROM_HANDLE(anv_buffer_view, bview,
600 write->pTexelBufferView[j]);
601
602 desc[j] = (struct anv_descriptor) {
603 .type = write->descriptorType,
604 .buffer_view = bview,
605 };
606 }
607 break;
608
609 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
610 anv_finishme("input attachments not implemented");
611 break;
612
613 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
614 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
615 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
616 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
617 for (uint32_t j = 0; j < write->descriptorCount; j++) {
618 assert(write->pBufferInfo[j].buffer);
619 ANV_FROM_HANDLE(anv_buffer, buffer, write->pBufferInfo[j].buffer);
620 assert(buffer);
621
622 struct anv_buffer_view *view =
623 &set->buffer_views[bind_layout->buffer_index];
624 view += write->dstArrayElement + j;
625
626 view->format =
627 anv_isl_format_for_descriptor_type(write->descriptorType);
628 view->bo = buffer->bo;
629 view->offset = buffer->offset + write->pBufferInfo[j].offset;
630
631 /* For buffers with dynamic offsets, we use the full possible
632 * range in the surface state and do the actual range-checking
633 * in the shader.
634 */
635 if (bind_layout->dynamic_offset_index >= 0 ||
636 write->pBufferInfo[j].range == VK_WHOLE_SIZE)
637 view->range = buffer->size - write->pBufferInfo[j].offset;
638 else
639 view->range = write->pBufferInfo[j].range;
640
641 anv_fill_buffer_surface_state(device, view->surface_state,
642 view->format,
643 view->offset, view->range, 1);
644
645 desc[j] = (struct anv_descriptor) {
646 .type = write->descriptorType,
647 .buffer_view = view,
648 };
649
650 }
651
652 default:
653 break;
654 }
655 }
656
657 for (uint32_t i = 0; i < descriptorCopyCount; i++) {
658 const VkCopyDescriptorSet *copy = &pDescriptorCopies[i];
659 ANV_FROM_HANDLE(anv_descriptor_set, src, copy->dstSet);
660 ANV_FROM_HANDLE(anv_descriptor_set, dst, copy->dstSet);
661
662 const struct anv_descriptor_set_binding_layout *src_layout =
663 &src->layout->binding[copy->srcBinding];
664 struct anv_descriptor *src_desc =
665 &src->descriptors[src_layout->descriptor_index];
666 src_desc += copy->srcArrayElement;
667
668 const struct anv_descriptor_set_binding_layout *dst_layout =
669 &dst->layout->binding[copy->dstBinding];
670 struct anv_descriptor *dst_desc =
671 &dst->descriptors[dst_layout->descriptor_index];
672 dst_desc += copy->dstArrayElement;
673
674 for (uint32_t j = 0; j < copy->descriptorCount; j++)
675 dst_desc[j] = src_desc[j];
676 }
677 }