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