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