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