c7a901275114a367a8e9456bbc7544b388dd9ab4
[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 #include "vk_util.h"
32
33 #include "anv_private.h"
34
35 /*
36 * Descriptor set layouts.
37 */
38
39 static enum anv_descriptor_data
40 anv_descriptor_data_for_type(const struct anv_physical_device *device,
41 VkDescriptorType type)
42 {
43 enum anv_descriptor_data data = 0;
44
45 switch (type) {
46 case VK_DESCRIPTOR_TYPE_SAMPLER:
47 data = ANV_DESCRIPTOR_SAMPLER_STATE;
48 break;
49
50 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
51 data = ANV_DESCRIPTOR_SURFACE_STATE |
52 ANV_DESCRIPTOR_SAMPLER_STATE;
53 break;
54
55 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
56 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
57 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
58 data = ANV_DESCRIPTOR_SURFACE_STATE;
59 break;
60
61 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
62 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
63 data = ANV_DESCRIPTOR_SURFACE_STATE;
64 if (device->info.gen < 9)
65 data |= ANV_DESCRIPTOR_IMAGE_PARAM;
66 break;
67
68 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
69 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
70 data = ANV_DESCRIPTOR_SURFACE_STATE |
71 ANV_DESCRIPTOR_BUFFER_VIEW;
72 break;
73
74 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
75 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
76 data = ANV_DESCRIPTOR_SURFACE_STATE;
77 break;
78
79 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
80 data = ANV_DESCRIPTOR_INLINE_UNIFORM;
81 break;
82
83 default:
84 unreachable("Unsupported descriptor type");
85 }
86
87 /* On gen8 and above when we have softpin enabled, we also need to push
88 * SSBO address ranges so that we can use A64 messages in the shader.
89 */
90 if (device->has_a64_buffer_access &&
91 (type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER ||
92 type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC))
93 data |= ANV_DESCRIPTOR_ADDRESS_RANGE;
94
95 return data;
96 }
97
98 static unsigned
99 anv_descriptor_data_size(enum anv_descriptor_data data)
100 {
101 unsigned size = 0;
102
103 if (data & ANV_DESCRIPTOR_IMAGE_PARAM)
104 size += BRW_IMAGE_PARAM_SIZE * 4;
105
106 if (data & ANV_DESCRIPTOR_ADDRESS_RANGE)
107 size += sizeof(struct anv_address_range_descriptor);
108
109 return size;
110 }
111
112 /** Returns the size in bytes of each descriptor with the given layout */
113 unsigned
114 anv_descriptor_size(const struct anv_descriptor_set_binding_layout *layout)
115 {
116 if (layout->data & ANV_DESCRIPTOR_INLINE_UNIFORM) {
117 assert(layout->data == ANV_DESCRIPTOR_INLINE_UNIFORM);
118 return layout->array_size;
119 }
120
121 return anv_descriptor_data_size(layout->data);
122 }
123
124 /** Returns the size in bytes of each descriptor of the given type
125 *
126 * This version of the function does not have access to the entire layout so
127 * it may only work on certain descriptor types where the descriptor size is
128 * entirely determined by the descriptor type. Whenever possible, code should
129 * use anv_descriptor_size() instead.
130 */
131 unsigned
132 anv_descriptor_type_size(const struct anv_physical_device *pdevice,
133 VkDescriptorType type)
134 {
135 assert(type != VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT);
136 return anv_descriptor_data_size(anv_descriptor_data_for_type(pdevice, type));
137 }
138
139 static bool
140 anv_descriptor_data_supports_bindless(const struct anv_physical_device *pdevice,
141 enum anv_descriptor_data data,
142 bool sampler)
143 {
144 if (data & ANV_DESCRIPTOR_ADDRESS_RANGE) {
145 assert(pdevice->has_a64_buffer_access);
146 return true;
147 }
148
149 return false;
150 }
151
152 bool
153 anv_descriptor_supports_bindless(const struct anv_physical_device *pdevice,
154 const struct anv_descriptor_set_binding_layout *binding,
155 bool sampler)
156 {
157 return anv_descriptor_data_supports_bindless(pdevice, binding->data,
158 sampler);
159 }
160
161 bool
162 anv_descriptor_requires_bindless(const struct anv_physical_device *pdevice,
163 const struct anv_descriptor_set_binding_layout *binding,
164 bool sampler)
165 {
166 if (pdevice->always_use_bindless)
167 return anv_descriptor_supports_bindless(pdevice, binding, sampler);
168
169 return false;
170 }
171
172 void anv_GetDescriptorSetLayoutSupport(
173 VkDevice _device,
174 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
175 VkDescriptorSetLayoutSupport* pSupport)
176 {
177 ANV_FROM_HANDLE(anv_device, device, _device);
178 const struct anv_physical_device *pdevice =
179 &device->instance->physicalDevice;
180
181 uint32_t surface_count[MESA_SHADER_STAGES] = { 0, };
182
183 for (uint32_t b = 0; b < pCreateInfo->bindingCount; b++) {
184 const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[b];
185
186 enum anv_descriptor_data desc_data =
187 anv_descriptor_data_for_type(pdevice, binding->descriptorType);
188
189 switch (binding->descriptorType) {
190 case VK_DESCRIPTOR_TYPE_SAMPLER:
191 /* There is no real limit on samplers */
192 break;
193
194 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
195 if (anv_descriptor_data_supports_bindless(pdevice, desc_data, false))
196 break;
197
198 if (binding->pImmutableSamplers) {
199 for (uint32_t i = 0; i < binding->descriptorCount; i++) {
200 ANV_FROM_HANDLE(anv_sampler, sampler,
201 binding->pImmutableSamplers[i]);
202 anv_foreach_stage(s, binding->stageFlags)
203 surface_count[s] += sampler->n_planes;
204 }
205 } else {
206 anv_foreach_stage(s, binding->stageFlags)
207 surface_count[s] += binding->descriptorCount;
208 }
209 break;
210
211 default:
212 if (anv_descriptor_data_supports_bindless(pdevice, desc_data, false))
213 break;
214
215 anv_foreach_stage(s, binding->stageFlags)
216 surface_count[s] += binding->descriptorCount;
217 break;
218 }
219 }
220
221 bool supported = true;
222 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
223 /* Our maximum binding table size is 240 and we need to reserve 8 for
224 * render targets.
225 */
226 if (surface_count[s] >= MAX_BINDING_TABLE_SIZE - MAX_RTS)
227 supported = false;
228 }
229
230 pSupport->supported = supported;
231 }
232
233 VkResult anv_CreateDescriptorSetLayout(
234 VkDevice _device,
235 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
236 const VkAllocationCallbacks* pAllocator,
237 VkDescriptorSetLayout* pSetLayout)
238 {
239 ANV_FROM_HANDLE(anv_device, device, _device);
240
241 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO);
242
243 uint32_t max_binding = 0;
244 uint32_t immutable_sampler_count = 0;
245 for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
246 max_binding = MAX2(max_binding, pCreateInfo->pBindings[j].binding);
247
248 /* From the Vulkan 1.1.97 spec for VkDescriptorSetLayoutBinding:
249 *
250 * "If descriptorType specifies a VK_DESCRIPTOR_TYPE_SAMPLER or
251 * VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER type descriptor, then
252 * pImmutableSamplers can be used to initialize a set of immutable
253 * samplers. [...] If descriptorType is not one of these descriptor
254 * types, then pImmutableSamplers is ignored.
255 *
256 * We need to be careful here and only parse pImmutableSamplers if we
257 * have one of the right descriptor types.
258 */
259 VkDescriptorType desc_type = pCreateInfo->pBindings[j].descriptorType;
260 if ((desc_type == VK_DESCRIPTOR_TYPE_SAMPLER ||
261 desc_type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) &&
262 pCreateInfo->pBindings[j].pImmutableSamplers)
263 immutable_sampler_count += pCreateInfo->pBindings[j].descriptorCount;
264 }
265
266 struct anv_descriptor_set_layout *set_layout;
267 struct anv_descriptor_set_binding_layout *bindings;
268 struct anv_sampler **samplers;
269
270 /* We need to allocate decriptor set layouts off the device allocator
271 * with DEVICE scope because they are reference counted and may not be
272 * destroyed when vkDestroyDescriptorSetLayout is called.
273 */
274 ANV_MULTIALLOC(ma);
275 anv_multialloc_add(&ma, &set_layout, 1);
276 anv_multialloc_add(&ma, &bindings, max_binding + 1);
277 anv_multialloc_add(&ma, &samplers, immutable_sampler_count);
278
279 if (!anv_multialloc_alloc(&ma, &device->alloc,
280 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE))
281 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
282
283 memset(set_layout, 0, sizeof(*set_layout));
284 set_layout->ref_cnt = 1;
285 set_layout->binding_count = max_binding + 1;
286
287 for (uint32_t b = 0; b <= max_binding; b++) {
288 /* Initialize all binding_layout entries to -1 */
289 memset(&set_layout->binding[b], -1, sizeof(set_layout->binding[b]));
290
291 set_layout->binding[b].data = 0;
292 set_layout->binding[b].max_plane_count = 0;
293 set_layout->binding[b].array_size = 0;
294 set_layout->binding[b].immutable_samplers = NULL;
295 }
296
297 /* Initialize all samplers to 0 */
298 memset(samplers, 0, immutable_sampler_count * sizeof(*samplers));
299
300 uint32_t buffer_view_count = 0;
301 uint32_t dynamic_offset_count = 0;
302 uint32_t descriptor_buffer_size = 0;
303
304 for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
305 const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[j];
306 uint32_t b = binding->binding;
307 /* We temporarily store the pointer to the binding in the
308 * immutable_samplers pointer. This provides us with a quick-and-dirty
309 * way to sort the bindings by binding number.
310 */
311 set_layout->binding[b].immutable_samplers = (void *)binding;
312 }
313
314 for (uint32_t b = 0; b <= max_binding; b++) {
315 const VkDescriptorSetLayoutBinding *binding =
316 (void *)set_layout->binding[b].immutable_samplers;
317
318 if (binding == NULL)
319 continue;
320
321 /* We temporarily stashed the pointer to the binding in the
322 * immutable_samplers pointer. Now that we've pulled it back out
323 * again, we reset immutable_samplers to NULL.
324 */
325 set_layout->binding[b].immutable_samplers = NULL;
326
327 if (binding->descriptorCount == 0)
328 continue;
329
330 #ifndef NDEBUG
331 set_layout->binding[b].type = binding->descriptorType;
332 #endif
333 set_layout->binding[b].data =
334 anv_descriptor_data_for_type(&device->instance->physicalDevice,
335 binding->descriptorType);
336 set_layout->binding[b].array_size = binding->descriptorCount;
337 set_layout->binding[b].descriptor_index = set_layout->size;
338 set_layout->size += binding->descriptorCount;
339
340 if (set_layout->binding[b].data & ANV_DESCRIPTOR_BUFFER_VIEW) {
341 set_layout->binding[b].buffer_view_index = buffer_view_count;
342 buffer_view_count += binding->descriptorCount;
343 }
344
345 switch (binding->descriptorType) {
346 case VK_DESCRIPTOR_TYPE_SAMPLER:
347 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
348 set_layout->binding[b].max_plane_count = 1;
349 if (binding->pImmutableSamplers) {
350 set_layout->binding[b].immutable_samplers = samplers;
351 samplers += binding->descriptorCount;
352
353 for (uint32_t i = 0; i < binding->descriptorCount; i++) {
354 ANV_FROM_HANDLE(anv_sampler, sampler,
355 binding->pImmutableSamplers[i]);
356
357 set_layout->binding[b].immutable_samplers[i] = sampler;
358 if (set_layout->binding[b].max_plane_count < sampler->n_planes)
359 set_layout->binding[b].max_plane_count = sampler->n_planes;
360 }
361 }
362 break;
363
364 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
365 set_layout->binding[b].max_plane_count = 1;
366 break;
367
368 default:
369 break;
370 }
371
372 switch (binding->descriptorType) {
373 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
374 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
375 set_layout->binding[b].dynamic_offset_index = dynamic_offset_count;
376 dynamic_offset_count += binding->descriptorCount;
377 break;
378
379 default:
380 break;
381 }
382
383 if (binding->descriptorType ==
384 VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
385 /* Inline uniform blocks are specified to use the descriptor array
386 * size as the size in bytes of the block.
387 */
388 descriptor_buffer_size = align_u32(descriptor_buffer_size, 32);
389 set_layout->binding[b].descriptor_offset = descriptor_buffer_size;
390 descriptor_buffer_size += binding->descriptorCount;
391 } else {
392 set_layout->binding[b].descriptor_offset = descriptor_buffer_size;
393 descriptor_buffer_size += anv_descriptor_size(&set_layout->binding[b]) *
394 binding->descriptorCount;
395 }
396
397 set_layout->shader_stages |= binding->stageFlags;
398 }
399
400 set_layout->buffer_view_count = buffer_view_count;
401 set_layout->dynamic_offset_count = dynamic_offset_count;
402 set_layout->descriptor_buffer_size = descriptor_buffer_size;
403
404 *pSetLayout = anv_descriptor_set_layout_to_handle(set_layout);
405
406 return VK_SUCCESS;
407 }
408
409 void anv_DestroyDescriptorSetLayout(
410 VkDevice _device,
411 VkDescriptorSetLayout _set_layout,
412 const VkAllocationCallbacks* pAllocator)
413 {
414 ANV_FROM_HANDLE(anv_device, device, _device);
415 ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout, _set_layout);
416
417 if (!set_layout)
418 return;
419
420 anv_descriptor_set_layout_unref(device, set_layout);
421 }
422
423 #define SHA1_UPDATE_VALUE(ctx, x) _mesa_sha1_update(ctx, &(x), sizeof(x));
424
425 static void
426 sha1_update_immutable_sampler(struct mesa_sha1 *ctx,
427 const struct anv_sampler *sampler)
428 {
429 if (!sampler->conversion)
430 return;
431
432 /* The only thing that affects the shader is ycbcr conversion */
433 _mesa_sha1_update(ctx, sampler->conversion,
434 sizeof(*sampler->conversion));
435 }
436
437 static void
438 sha1_update_descriptor_set_binding_layout(struct mesa_sha1 *ctx,
439 const struct anv_descriptor_set_binding_layout *layout)
440 {
441 SHA1_UPDATE_VALUE(ctx, layout->data);
442 SHA1_UPDATE_VALUE(ctx, layout->max_plane_count);
443 SHA1_UPDATE_VALUE(ctx, layout->array_size);
444 SHA1_UPDATE_VALUE(ctx, layout->descriptor_index);
445 SHA1_UPDATE_VALUE(ctx, layout->dynamic_offset_index);
446 SHA1_UPDATE_VALUE(ctx, layout->buffer_view_index);
447 SHA1_UPDATE_VALUE(ctx, layout->descriptor_offset);
448
449 if (layout->immutable_samplers) {
450 for (uint16_t i = 0; i < layout->array_size; i++)
451 sha1_update_immutable_sampler(ctx, layout->immutable_samplers[i]);
452 }
453 }
454
455 static void
456 sha1_update_descriptor_set_layout(struct mesa_sha1 *ctx,
457 const struct anv_descriptor_set_layout *layout)
458 {
459 SHA1_UPDATE_VALUE(ctx, layout->binding_count);
460 SHA1_UPDATE_VALUE(ctx, layout->size);
461 SHA1_UPDATE_VALUE(ctx, layout->shader_stages);
462 SHA1_UPDATE_VALUE(ctx, layout->buffer_view_count);
463 SHA1_UPDATE_VALUE(ctx, layout->dynamic_offset_count);
464 SHA1_UPDATE_VALUE(ctx, layout->descriptor_buffer_size);
465
466 for (uint16_t i = 0; i < layout->binding_count; i++)
467 sha1_update_descriptor_set_binding_layout(ctx, &layout->binding[i]);
468 }
469
470 /*
471 * Pipeline layouts. These have nothing to do with the pipeline. They are
472 * just multiple descriptor set layouts pasted together
473 */
474
475 VkResult anv_CreatePipelineLayout(
476 VkDevice _device,
477 const VkPipelineLayoutCreateInfo* pCreateInfo,
478 const VkAllocationCallbacks* pAllocator,
479 VkPipelineLayout* pPipelineLayout)
480 {
481 ANV_FROM_HANDLE(anv_device, device, _device);
482 struct anv_pipeline_layout *layout;
483
484 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
485
486 layout = vk_alloc2(&device->alloc, pAllocator, sizeof(*layout), 8,
487 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
488 if (layout == NULL)
489 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
490
491 layout->num_sets = pCreateInfo->setLayoutCount;
492
493 unsigned dynamic_offset_count = 0;
494
495 for (uint32_t set = 0; set < pCreateInfo->setLayoutCount; set++) {
496 ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout,
497 pCreateInfo->pSetLayouts[set]);
498 layout->set[set].layout = set_layout;
499 anv_descriptor_set_layout_ref(set_layout);
500
501 layout->set[set].dynamic_offset_start = dynamic_offset_count;
502 for (uint32_t b = 0; b < set_layout->binding_count; b++) {
503 if (set_layout->binding[b].dynamic_offset_index < 0)
504 continue;
505
506 dynamic_offset_count += set_layout->binding[b].array_size;
507 }
508 }
509
510 struct mesa_sha1 ctx;
511 _mesa_sha1_init(&ctx);
512 for (unsigned s = 0; s < layout->num_sets; s++) {
513 sha1_update_descriptor_set_layout(&ctx, layout->set[s].layout);
514 _mesa_sha1_update(&ctx, &layout->set[s].dynamic_offset_start,
515 sizeof(layout->set[s].dynamic_offset_start));
516 }
517 _mesa_sha1_update(&ctx, &layout->num_sets, sizeof(layout->num_sets));
518 _mesa_sha1_final(&ctx, layout->sha1);
519
520 *pPipelineLayout = anv_pipeline_layout_to_handle(layout);
521
522 return VK_SUCCESS;
523 }
524
525 void anv_DestroyPipelineLayout(
526 VkDevice _device,
527 VkPipelineLayout _pipelineLayout,
528 const VkAllocationCallbacks* pAllocator)
529 {
530 ANV_FROM_HANDLE(anv_device, device, _device);
531 ANV_FROM_HANDLE(anv_pipeline_layout, pipeline_layout, _pipelineLayout);
532
533 if (!pipeline_layout)
534 return;
535
536 for (uint32_t i = 0; i < pipeline_layout->num_sets; i++)
537 anv_descriptor_set_layout_unref(device, pipeline_layout->set[i].layout);
538
539 vk_free2(&device->alloc, pAllocator, pipeline_layout);
540 }
541
542 /*
543 * Descriptor pools.
544 *
545 * These are implemented using a big pool of memory and a free-list for the
546 * host memory allocations and a state_stream and a free list for the buffer
547 * view surface state. The spec allows us to fail to allocate due to
548 * fragmentation in all cases but two: 1) after pool reset, allocating up
549 * until the pool size with no freeing must succeed and 2) allocating and
550 * freeing only descriptor sets with the same layout. Case 1) is easy enogh,
551 * and the free lists lets us recycle blocks for case 2).
552 */
553
554 /* The vma heap reserves 0 to mean NULL; we have to offset by some ammount to
555 * ensure we can allocate the entire BO without hitting zero. The actual
556 * amount doesn't matter.
557 */
558 #define POOL_HEAP_OFFSET 64
559
560 #define EMPTY 1
561
562 VkResult anv_CreateDescriptorPool(
563 VkDevice _device,
564 const VkDescriptorPoolCreateInfo* pCreateInfo,
565 const VkAllocationCallbacks* pAllocator,
566 VkDescriptorPool* pDescriptorPool)
567 {
568 ANV_FROM_HANDLE(anv_device, device, _device);
569 struct anv_descriptor_pool *pool;
570
571 const VkDescriptorPoolInlineUniformBlockCreateInfoEXT *inline_info =
572 vk_find_struct_const(pCreateInfo->pNext,
573 DESCRIPTOR_POOL_INLINE_UNIFORM_BLOCK_CREATE_INFO_EXT);
574
575 uint32_t descriptor_count = 0;
576 uint32_t buffer_view_count = 0;
577 uint32_t descriptor_bo_size = 0;
578 for (uint32_t i = 0; i < pCreateInfo->poolSizeCount; i++) {
579 enum anv_descriptor_data desc_data =
580 anv_descriptor_data_for_type(&device->instance->physicalDevice,
581 pCreateInfo->pPoolSizes[i].type);
582
583 if (desc_data & ANV_DESCRIPTOR_BUFFER_VIEW)
584 buffer_view_count += pCreateInfo->pPoolSizes[i].descriptorCount;
585
586 unsigned desc_data_size = anv_descriptor_data_size(desc_data) *
587 pCreateInfo->pPoolSizes[i].descriptorCount;
588
589 if (pCreateInfo->pPoolSizes[i].type ==
590 VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
591 /* Inline uniform blocks are specified to use the descriptor array
592 * size as the size in bytes of the block.
593 */
594 assert(inline_info);
595 desc_data_size += pCreateInfo->pPoolSizes[i].descriptorCount;
596 }
597
598 descriptor_bo_size += desc_data_size;
599
600 descriptor_count += pCreateInfo->pPoolSizes[i].descriptorCount;
601 }
602 /* We have to align descriptor buffer allocations to 32B so that we can
603 * push descriptor buffers. This means that each descriptor buffer
604 * allocated may burn up to 32B of extra space to get the right alignment.
605 * (Technically, it's at most 28B because we're always going to start at
606 * least 4B aligned but we're being conservative here.) Allocate enough
607 * extra space that we can chop it into maxSets pieces and align each one
608 * of them to 32B.
609 */
610 descriptor_bo_size += 32 * pCreateInfo->maxSets;
611 descriptor_bo_size = ALIGN(descriptor_bo_size, 4096);
612 /* We align inline uniform blocks to 32B */
613 if (inline_info)
614 descriptor_bo_size += 32 * inline_info->maxInlineUniformBlockBindings;
615
616 const size_t pool_size =
617 pCreateInfo->maxSets * sizeof(struct anv_descriptor_set) +
618 descriptor_count * sizeof(struct anv_descriptor) +
619 buffer_view_count * sizeof(struct anv_buffer_view);
620 const size_t total_size = sizeof(*pool) + pool_size;
621
622 pool = vk_alloc2(&device->alloc, pAllocator, total_size, 8,
623 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
624 if (!pool)
625 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
626
627 pool->size = pool_size;
628 pool->next = 0;
629 pool->free_list = EMPTY;
630
631 if (descriptor_bo_size > 0) {
632 VkResult result = anv_bo_init_new(&pool->bo, device, descriptor_bo_size);
633 if (result != VK_SUCCESS) {
634 vk_free2(&device->alloc, pAllocator, pool);
635 return result;
636 }
637
638 anv_gem_set_caching(device, pool->bo.gem_handle, I915_CACHING_CACHED);
639
640 pool->bo.map = anv_gem_mmap(device, pool->bo.gem_handle, 0,
641 descriptor_bo_size, 0);
642 if (pool->bo.map == NULL) {
643 anv_gem_close(device, pool->bo.gem_handle);
644 vk_free2(&device->alloc, pAllocator, pool);
645 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
646 }
647
648 if (device->instance->physicalDevice.use_softpin) {
649 pool->bo.flags |= EXEC_OBJECT_PINNED;
650 anv_vma_alloc(device, &pool->bo);
651 }
652
653 util_vma_heap_init(&pool->bo_heap, POOL_HEAP_OFFSET, descriptor_bo_size);
654 } else {
655 pool->bo.size = 0;
656 }
657
658 anv_state_stream_init(&pool->surface_state_stream,
659 &device->surface_state_pool, 4096);
660 pool->surface_state_free_list = NULL;
661
662 list_inithead(&pool->desc_sets);
663
664 *pDescriptorPool = anv_descriptor_pool_to_handle(pool);
665
666 return VK_SUCCESS;
667 }
668
669 void anv_DestroyDescriptorPool(
670 VkDevice _device,
671 VkDescriptorPool _pool,
672 const VkAllocationCallbacks* pAllocator)
673 {
674 ANV_FROM_HANDLE(anv_device, device, _device);
675 ANV_FROM_HANDLE(anv_descriptor_pool, pool, _pool);
676
677 if (!pool)
678 return;
679
680 if (pool->bo.size) {
681 anv_gem_munmap(pool->bo.map, pool->bo.size);
682 anv_vma_free(device, &pool->bo);
683 anv_gem_close(device, pool->bo.gem_handle);
684 }
685 anv_state_stream_finish(&pool->surface_state_stream);
686
687 list_for_each_entry_safe(struct anv_descriptor_set, set,
688 &pool->desc_sets, pool_link) {
689 anv_descriptor_set_destroy(device, pool, set);
690 }
691
692 util_vma_heap_finish(&pool->bo_heap);
693
694 vk_free2(&device->alloc, pAllocator, pool);
695 }
696
697 VkResult anv_ResetDescriptorPool(
698 VkDevice _device,
699 VkDescriptorPool descriptorPool,
700 VkDescriptorPoolResetFlags flags)
701 {
702 ANV_FROM_HANDLE(anv_device, device, _device);
703 ANV_FROM_HANDLE(anv_descriptor_pool, pool, descriptorPool);
704
705 list_for_each_entry_safe(struct anv_descriptor_set, set,
706 &pool->desc_sets, pool_link) {
707 anv_descriptor_set_destroy(device, pool, set);
708 }
709
710 pool->next = 0;
711 pool->free_list = EMPTY;
712
713 if (pool->bo.size) {
714 util_vma_heap_finish(&pool->bo_heap);
715 util_vma_heap_init(&pool->bo_heap, POOL_HEAP_OFFSET, pool->bo.size);
716 }
717
718 anv_state_stream_finish(&pool->surface_state_stream);
719 anv_state_stream_init(&pool->surface_state_stream,
720 &device->surface_state_pool, 4096);
721 pool->surface_state_free_list = NULL;
722
723 return VK_SUCCESS;
724 }
725
726 struct pool_free_list_entry {
727 uint32_t next;
728 uint32_t size;
729 };
730
731 static VkResult
732 anv_descriptor_pool_alloc_set(struct anv_descriptor_pool *pool,
733 uint32_t size,
734 struct anv_descriptor_set **set)
735 {
736 if (size <= pool->size - pool->next) {
737 *set = (struct anv_descriptor_set *) (pool->data + pool->next);
738 pool->next += size;
739 return VK_SUCCESS;
740 } else {
741 struct pool_free_list_entry *entry;
742 uint32_t *link = &pool->free_list;
743 for (uint32_t f = pool->free_list; f != EMPTY; f = entry->next) {
744 entry = (struct pool_free_list_entry *) (pool->data + f);
745 if (size <= entry->size) {
746 *link = entry->next;
747 *set = (struct anv_descriptor_set *) entry;
748 return VK_SUCCESS;
749 }
750 link = &entry->next;
751 }
752
753 if (pool->free_list != EMPTY) {
754 return vk_error(VK_ERROR_FRAGMENTED_POOL);
755 } else {
756 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY);
757 }
758 }
759 }
760
761 static void
762 anv_descriptor_pool_free_set(struct anv_descriptor_pool *pool,
763 struct anv_descriptor_set *set)
764 {
765 /* Put the descriptor set allocation back on the free list. */
766 const uint32_t index = (char *) set - pool->data;
767 if (index + set->size == pool->next) {
768 pool->next = index;
769 } else {
770 struct pool_free_list_entry *entry = (struct pool_free_list_entry *) set;
771 entry->next = pool->free_list;
772 entry->size = set->size;
773 pool->free_list = (char *) entry - pool->data;
774 }
775
776 list_del(&set->pool_link);
777 }
778
779 struct surface_state_free_list_entry {
780 void *next;
781 struct anv_state state;
782 };
783
784 static struct anv_state
785 anv_descriptor_pool_alloc_state(struct anv_descriptor_pool *pool)
786 {
787 struct surface_state_free_list_entry *entry =
788 pool->surface_state_free_list;
789
790 if (entry) {
791 struct anv_state state = entry->state;
792 pool->surface_state_free_list = entry->next;
793 assert(state.alloc_size == 64);
794 return state;
795 } else {
796 return anv_state_stream_alloc(&pool->surface_state_stream, 64, 64);
797 }
798 }
799
800 static void
801 anv_descriptor_pool_free_state(struct anv_descriptor_pool *pool,
802 struct anv_state state)
803 {
804 /* Put the buffer view surface state back on the free list. */
805 struct surface_state_free_list_entry *entry = state.map;
806 entry->next = pool->surface_state_free_list;
807 entry->state = state;
808 pool->surface_state_free_list = entry;
809 }
810
811 size_t
812 anv_descriptor_set_layout_size(const struct anv_descriptor_set_layout *layout)
813 {
814 return
815 sizeof(struct anv_descriptor_set) +
816 layout->size * sizeof(struct anv_descriptor) +
817 layout->buffer_view_count * sizeof(struct anv_buffer_view);
818 }
819
820 VkResult
821 anv_descriptor_set_create(struct anv_device *device,
822 struct anv_descriptor_pool *pool,
823 struct anv_descriptor_set_layout *layout,
824 struct anv_descriptor_set **out_set)
825 {
826 struct anv_descriptor_set *set;
827 const size_t size = anv_descriptor_set_layout_size(layout);
828
829 VkResult result = anv_descriptor_pool_alloc_set(pool, size, &set);
830 if (result != VK_SUCCESS)
831 return result;
832
833 if (layout->descriptor_buffer_size) {
834 /* Align the size to 32 so that alignment gaps don't cause extra holes
835 * in the heap which can lead to bad performance.
836 */
837 uint64_t pool_vma_offset =
838 util_vma_heap_alloc(&pool->bo_heap,
839 ALIGN(layout->descriptor_buffer_size, 32), 32);
840 if (pool_vma_offset == 0) {
841 anv_descriptor_pool_free_set(pool, set);
842 return vk_error(VK_ERROR_FRAGMENTED_POOL);
843 }
844 assert(pool_vma_offset >= POOL_HEAP_OFFSET &&
845 pool_vma_offset - POOL_HEAP_OFFSET <= INT32_MAX);
846 set->desc_mem.offset = pool_vma_offset - POOL_HEAP_OFFSET;
847 set->desc_mem.alloc_size = layout->descriptor_buffer_size;
848 set->desc_mem.map = pool->bo.map + set->desc_mem.offset;
849
850 set->desc_surface_state = anv_descriptor_pool_alloc_state(pool);
851 anv_fill_buffer_surface_state(device, set->desc_surface_state,
852 ISL_FORMAT_R32G32B32A32_FLOAT,
853 (struct anv_address) {
854 .bo = &pool->bo,
855 .offset = set->desc_mem.offset,
856 },
857 layout->descriptor_buffer_size, 1);
858 } else {
859 set->desc_mem = ANV_STATE_NULL;
860 set->desc_surface_state = ANV_STATE_NULL;
861 }
862
863 set->pool = pool;
864 set->layout = layout;
865 anv_descriptor_set_layout_ref(layout);
866
867 set->size = size;
868 set->buffer_views =
869 (struct anv_buffer_view *) &set->descriptors[layout->size];
870 set->buffer_view_count = layout->buffer_view_count;
871
872 /* By defining the descriptors to be zero now, we can later verify that
873 * a descriptor has not been populated with user data.
874 */
875 memset(set->descriptors, 0, sizeof(struct anv_descriptor) * layout->size);
876
877 /* Go through and fill out immutable samplers if we have any */
878 struct anv_descriptor *desc = set->descriptors;
879 for (uint32_t b = 0; b < layout->binding_count; b++) {
880 if (layout->binding[b].immutable_samplers) {
881 for (uint32_t i = 0; i < layout->binding[b].array_size; i++) {
882 /* The type will get changed to COMBINED_IMAGE_SAMPLER in
883 * UpdateDescriptorSets if needed. However, if the descriptor
884 * set has an immutable sampler, UpdateDescriptorSets may never
885 * touch it, so we need to make sure it's 100% valid now.
886 *
887 * We don't need to actually provide a sampler because the helper
888 * will always write in the immutable sampler regardless of what
889 * is in the sampler parameter.
890 */
891 struct VkDescriptorImageInfo info = { };
892 anv_descriptor_set_write_image_view(device, set, &info,
893 VK_DESCRIPTOR_TYPE_SAMPLER,
894 b, i);
895 }
896 }
897 desc += layout->binding[b].array_size;
898 }
899
900 /* Allocate surface state for the buffer views. */
901 for (uint32_t b = 0; b < layout->buffer_view_count; b++) {
902 set->buffer_views[b].surface_state =
903 anv_descriptor_pool_alloc_state(pool);
904 }
905
906 *out_set = set;
907
908 return VK_SUCCESS;
909 }
910
911 void
912 anv_descriptor_set_destroy(struct anv_device *device,
913 struct anv_descriptor_pool *pool,
914 struct anv_descriptor_set *set)
915 {
916 anv_descriptor_set_layout_unref(device, set->layout);
917
918 if (set->desc_mem.alloc_size) {
919 util_vma_heap_free(&pool->bo_heap,
920 (uint64_t)set->desc_mem.offset + POOL_HEAP_OFFSET,
921 set->desc_mem.alloc_size);
922 anv_descriptor_pool_free_state(pool, set->desc_surface_state);
923 }
924
925 for (uint32_t b = 0; b < set->buffer_view_count; b++)
926 anv_descriptor_pool_free_state(pool, set->buffer_views[b].surface_state);
927
928 anv_descriptor_pool_free_set(pool, set);
929 }
930
931 VkResult anv_AllocateDescriptorSets(
932 VkDevice _device,
933 const VkDescriptorSetAllocateInfo* pAllocateInfo,
934 VkDescriptorSet* pDescriptorSets)
935 {
936 ANV_FROM_HANDLE(anv_device, device, _device);
937 ANV_FROM_HANDLE(anv_descriptor_pool, pool, pAllocateInfo->descriptorPool);
938
939 VkResult result = VK_SUCCESS;
940 struct anv_descriptor_set *set;
941 uint32_t i;
942
943 for (i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
944 ANV_FROM_HANDLE(anv_descriptor_set_layout, layout,
945 pAllocateInfo->pSetLayouts[i]);
946
947 result = anv_descriptor_set_create(device, pool, layout, &set);
948 if (result != VK_SUCCESS)
949 break;
950
951 list_addtail(&set->pool_link, &pool->desc_sets);
952
953 pDescriptorSets[i] = anv_descriptor_set_to_handle(set);
954 }
955
956 if (result != VK_SUCCESS)
957 anv_FreeDescriptorSets(_device, pAllocateInfo->descriptorPool,
958 i, pDescriptorSets);
959
960 return result;
961 }
962
963 VkResult anv_FreeDescriptorSets(
964 VkDevice _device,
965 VkDescriptorPool descriptorPool,
966 uint32_t count,
967 const VkDescriptorSet* pDescriptorSets)
968 {
969 ANV_FROM_HANDLE(anv_device, device, _device);
970 ANV_FROM_HANDLE(anv_descriptor_pool, pool, descriptorPool);
971
972 for (uint32_t i = 0; i < count; i++) {
973 ANV_FROM_HANDLE(anv_descriptor_set, set, pDescriptorSets[i]);
974
975 if (!set)
976 continue;
977
978 anv_descriptor_set_destroy(device, pool, set);
979 }
980
981 return VK_SUCCESS;
982 }
983
984 static void
985 anv_descriptor_set_write_image_param(uint32_t *param_desc_map,
986 const struct brw_image_param *param)
987 {
988 #define WRITE_PARAM_FIELD(field, FIELD) \
989 for (unsigned i = 0; i < ARRAY_SIZE(param->field); i++) \
990 param_desc_map[BRW_IMAGE_PARAM_##FIELD##_OFFSET + i] = param->field[i]
991
992 WRITE_PARAM_FIELD(offset, OFFSET);
993 WRITE_PARAM_FIELD(size, SIZE);
994 WRITE_PARAM_FIELD(stride, STRIDE);
995 WRITE_PARAM_FIELD(tiling, TILING);
996 WRITE_PARAM_FIELD(swizzling, SWIZZLING);
997 WRITE_PARAM_FIELD(size, SIZE);
998
999 #undef WRITE_PARAM_FIELD
1000 }
1001
1002 void
1003 anv_descriptor_set_write_image_view(struct anv_device *device,
1004 struct anv_descriptor_set *set,
1005 const VkDescriptorImageInfo * const info,
1006 VkDescriptorType type,
1007 uint32_t binding,
1008 uint32_t element)
1009 {
1010 const struct anv_descriptor_set_binding_layout *bind_layout =
1011 &set->layout->binding[binding];
1012 struct anv_descriptor *desc =
1013 &set->descriptors[bind_layout->descriptor_index + element];
1014 struct anv_image_view *image_view = NULL;
1015 struct anv_sampler *sampler = NULL;
1016
1017 /* We get called with just VK_DESCRIPTOR_TYPE_SAMPLER as part of descriptor
1018 * set initialization to set the bindless samplers.
1019 */
1020 assert(type == bind_layout->type ||
1021 type == VK_DESCRIPTOR_TYPE_SAMPLER);
1022
1023 switch (type) {
1024 case VK_DESCRIPTOR_TYPE_SAMPLER:
1025 sampler = anv_sampler_from_handle(info->sampler);
1026 break;
1027
1028 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1029 image_view = anv_image_view_from_handle(info->imageView);
1030 sampler = anv_sampler_from_handle(info->sampler);
1031 break;
1032
1033 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1034 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1035 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1036 image_view = anv_image_view_from_handle(info->imageView);
1037 break;
1038
1039 default:
1040 unreachable("invalid descriptor type");
1041 }
1042
1043 /* If this descriptor has an immutable sampler, we don't want to stomp on
1044 * it.
1045 */
1046 sampler = bind_layout->immutable_samplers ?
1047 bind_layout->immutable_samplers[element] :
1048 sampler;
1049
1050 *desc = (struct anv_descriptor) {
1051 .type = type,
1052 .layout = info->imageLayout,
1053 .image_view = image_view,
1054 .sampler = sampler,
1055 };
1056
1057 void *desc_map = set->desc_mem.map + bind_layout->descriptor_offset +
1058 element * anv_descriptor_size(bind_layout);
1059
1060 if (bind_layout->data & ANV_DESCRIPTOR_IMAGE_PARAM) {
1061 /* Storage images can only ever have one plane */
1062 assert(image_view->n_planes == 1);
1063 const struct brw_image_param *image_param =
1064 &image_view->planes[0].storage_image_param;
1065
1066 anv_descriptor_set_write_image_param(desc_map, image_param);
1067 }
1068 }
1069
1070 void
1071 anv_descriptor_set_write_buffer_view(struct anv_device *device,
1072 struct anv_descriptor_set *set,
1073 VkDescriptorType type,
1074 struct anv_buffer_view *buffer_view,
1075 uint32_t binding,
1076 uint32_t element)
1077 {
1078 const struct anv_descriptor_set_binding_layout *bind_layout =
1079 &set->layout->binding[binding];
1080 struct anv_descriptor *desc =
1081 &set->descriptors[bind_layout->descriptor_index + element];
1082
1083 assert(type == bind_layout->type);
1084
1085 *desc = (struct anv_descriptor) {
1086 .type = type,
1087 .buffer_view = buffer_view,
1088 };
1089
1090 void *desc_map = set->desc_mem.map + bind_layout->descriptor_offset +
1091 element * anv_descriptor_size(bind_layout);
1092
1093 if (bind_layout->data & ANV_DESCRIPTOR_IMAGE_PARAM) {
1094 anv_descriptor_set_write_image_param(desc_map,
1095 &buffer_view->storage_image_param);
1096 }
1097 }
1098
1099 void
1100 anv_descriptor_set_write_buffer(struct anv_device *device,
1101 struct anv_descriptor_set *set,
1102 struct anv_state_stream *alloc_stream,
1103 VkDescriptorType type,
1104 struct anv_buffer *buffer,
1105 uint32_t binding,
1106 uint32_t element,
1107 VkDeviceSize offset,
1108 VkDeviceSize range)
1109 {
1110 const struct anv_descriptor_set_binding_layout *bind_layout =
1111 &set->layout->binding[binding];
1112 struct anv_descriptor *desc =
1113 &set->descriptors[bind_layout->descriptor_index + element];
1114
1115 assert(type == bind_layout->type);
1116
1117 struct anv_address bind_addr = anv_address_add(buffer->address, offset);
1118 uint64_t bind_range = anv_buffer_get_range(buffer, offset, range);
1119
1120 if (type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
1121 type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
1122 *desc = (struct anv_descriptor) {
1123 .type = type,
1124 .buffer = buffer,
1125 .offset = offset,
1126 .range = range,
1127 };
1128 } else {
1129 assert(bind_layout->data & ANV_DESCRIPTOR_BUFFER_VIEW);
1130 struct anv_buffer_view *bview =
1131 &set->buffer_views[bind_layout->buffer_view_index + element];
1132
1133 bview->format = anv_isl_format_for_descriptor_type(type);
1134 bview->range = bind_range;
1135 bview->address = bind_addr;
1136
1137 /* If we're writing descriptors through a push command, we need to
1138 * allocate the surface state from the command buffer. Otherwise it will
1139 * be allocated by the descriptor pool when calling
1140 * vkAllocateDescriptorSets. */
1141 if (alloc_stream)
1142 bview->surface_state = anv_state_stream_alloc(alloc_stream, 64, 64);
1143
1144 anv_fill_buffer_surface_state(device, bview->surface_state,
1145 bview->format, bind_addr, bind_range, 1);
1146
1147 *desc = (struct anv_descriptor) {
1148 .type = type,
1149 .buffer_view = bview,
1150 };
1151 }
1152
1153 void *desc_map = set->desc_mem.map + bind_layout->descriptor_offset +
1154 element * anv_descriptor_size(bind_layout);
1155
1156 if (bind_layout->data & ANV_DESCRIPTOR_ADDRESS_RANGE) {
1157 struct anv_address_range_descriptor desc = {
1158 .address = anv_address_physical(bind_addr),
1159 .range = bind_range,
1160 };
1161 memcpy(desc_map, &desc, sizeof(desc));
1162 }
1163 }
1164
1165 void
1166 anv_descriptor_set_write_inline_uniform_data(struct anv_device *device,
1167 struct anv_descriptor_set *set,
1168 uint32_t binding,
1169 const void *data,
1170 size_t offset,
1171 size_t size)
1172 {
1173 const struct anv_descriptor_set_binding_layout *bind_layout =
1174 &set->layout->binding[binding];
1175
1176 assert(bind_layout->data & ANV_DESCRIPTOR_INLINE_UNIFORM);
1177
1178 void *desc_map = set->desc_mem.map + bind_layout->descriptor_offset;
1179
1180 memcpy(desc_map + offset, data, size);
1181 }
1182
1183 void anv_UpdateDescriptorSets(
1184 VkDevice _device,
1185 uint32_t descriptorWriteCount,
1186 const VkWriteDescriptorSet* pDescriptorWrites,
1187 uint32_t descriptorCopyCount,
1188 const VkCopyDescriptorSet* pDescriptorCopies)
1189 {
1190 ANV_FROM_HANDLE(anv_device, device, _device);
1191
1192 for (uint32_t i = 0; i < descriptorWriteCount; i++) {
1193 const VkWriteDescriptorSet *write = &pDescriptorWrites[i];
1194 ANV_FROM_HANDLE(anv_descriptor_set, set, write->dstSet);
1195
1196 switch (write->descriptorType) {
1197 case VK_DESCRIPTOR_TYPE_SAMPLER:
1198 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1199 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1200 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1201 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1202 for (uint32_t j = 0; j < write->descriptorCount; j++) {
1203 anv_descriptor_set_write_image_view(device, set,
1204 write->pImageInfo + j,
1205 write->descriptorType,
1206 write->dstBinding,
1207 write->dstArrayElement + j);
1208 }
1209 break;
1210
1211 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1212 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1213 for (uint32_t j = 0; j < write->descriptorCount; j++) {
1214 ANV_FROM_HANDLE(anv_buffer_view, bview,
1215 write->pTexelBufferView[j]);
1216
1217 anv_descriptor_set_write_buffer_view(device, set,
1218 write->descriptorType,
1219 bview,
1220 write->dstBinding,
1221 write->dstArrayElement + j);
1222 }
1223 break;
1224
1225 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1226 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1227 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1228 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1229 for (uint32_t j = 0; j < write->descriptorCount; j++) {
1230 assert(write->pBufferInfo[j].buffer);
1231 ANV_FROM_HANDLE(anv_buffer, buffer, write->pBufferInfo[j].buffer);
1232 assert(buffer);
1233
1234 anv_descriptor_set_write_buffer(device, set,
1235 NULL,
1236 write->descriptorType,
1237 buffer,
1238 write->dstBinding,
1239 write->dstArrayElement + j,
1240 write->pBufferInfo[j].offset,
1241 write->pBufferInfo[j].range);
1242 }
1243 break;
1244
1245 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT: {
1246 const VkWriteDescriptorSetInlineUniformBlockEXT *inline_write =
1247 vk_find_struct_const(write->pNext,
1248 WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT);
1249 assert(inline_write->dataSize == write->descriptorCount);
1250 anv_descriptor_set_write_inline_uniform_data(device, set,
1251 write->dstBinding,
1252 inline_write->pData,
1253 write->dstArrayElement,
1254 inline_write->dataSize);
1255 break;
1256 }
1257
1258 default:
1259 break;
1260 }
1261 }
1262
1263 for (uint32_t i = 0; i < descriptorCopyCount; i++) {
1264 const VkCopyDescriptorSet *copy = &pDescriptorCopies[i];
1265 ANV_FROM_HANDLE(anv_descriptor_set, src, copy->srcSet);
1266 ANV_FROM_HANDLE(anv_descriptor_set, dst, copy->dstSet);
1267
1268 const struct anv_descriptor_set_binding_layout *src_layout =
1269 &src->layout->binding[copy->srcBinding];
1270 struct anv_descriptor *src_desc =
1271 &src->descriptors[src_layout->descriptor_index];
1272 src_desc += copy->srcArrayElement;
1273
1274 const struct anv_descriptor_set_binding_layout *dst_layout =
1275 &dst->layout->binding[copy->dstBinding];
1276 struct anv_descriptor *dst_desc =
1277 &dst->descriptors[dst_layout->descriptor_index];
1278 dst_desc += copy->dstArrayElement;
1279
1280 for (uint32_t j = 0; j < copy->descriptorCount; j++)
1281 dst_desc[j] = src_desc[j];
1282
1283 if (src_layout->data & ANV_DESCRIPTOR_INLINE_UNIFORM) {
1284 assert(src_layout->data == ANV_DESCRIPTOR_INLINE_UNIFORM);
1285 memcpy(dst->desc_mem.map + dst_layout->descriptor_offset +
1286 copy->dstArrayElement,
1287 src->desc_mem.map + src_layout->descriptor_offset +
1288 copy->srcArrayElement,
1289 copy->descriptorCount);
1290 } else {
1291 unsigned desc_size = anv_descriptor_size(src_layout);
1292 if (desc_size > 0) {
1293 assert(desc_size == anv_descriptor_size(dst_layout));
1294 memcpy(dst->desc_mem.map + dst_layout->descriptor_offset +
1295 copy->dstArrayElement * desc_size,
1296 src->desc_mem.map + src_layout->descriptor_offset +
1297 copy->srcArrayElement * desc_size,
1298 copy->descriptorCount * desc_size);
1299 }
1300 }
1301 }
1302 }
1303
1304 /*
1305 * Descriptor update templates.
1306 */
1307
1308 void
1309 anv_descriptor_set_write_template(struct anv_device *device,
1310 struct anv_descriptor_set *set,
1311 struct anv_state_stream *alloc_stream,
1312 const struct anv_descriptor_update_template *template,
1313 const void *data)
1314 {
1315 for (uint32_t i = 0; i < template->entry_count; i++) {
1316 const struct anv_descriptor_template_entry *entry =
1317 &template->entries[i];
1318
1319 switch (entry->type) {
1320 case VK_DESCRIPTOR_TYPE_SAMPLER:
1321 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1322 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1323 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1324 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1325 for (uint32_t j = 0; j < entry->array_count; j++) {
1326 const VkDescriptorImageInfo *info =
1327 data + entry->offset + j * entry->stride;
1328 anv_descriptor_set_write_image_view(device, set,
1329 info, entry->type,
1330 entry->binding,
1331 entry->array_element + j);
1332 }
1333 break;
1334
1335 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1336 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1337 for (uint32_t j = 0; j < entry->array_count; j++) {
1338 const VkBufferView *_bview =
1339 data + entry->offset + j * entry->stride;
1340 ANV_FROM_HANDLE(anv_buffer_view, bview, *_bview);
1341
1342 anv_descriptor_set_write_buffer_view(device, set,
1343 entry->type,
1344 bview,
1345 entry->binding,
1346 entry->array_element + j);
1347 }
1348 break;
1349
1350 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1351 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1352 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1353 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1354 for (uint32_t j = 0; j < entry->array_count; j++) {
1355 const VkDescriptorBufferInfo *info =
1356 data + entry->offset + j * entry->stride;
1357 ANV_FROM_HANDLE(anv_buffer, buffer, info->buffer);
1358
1359 anv_descriptor_set_write_buffer(device, set,
1360 alloc_stream,
1361 entry->type,
1362 buffer,
1363 entry->binding,
1364 entry->array_element + j,
1365 info->offset, info->range);
1366 }
1367 break;
1368
1369 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
1370 anv_descriptor_set_write_inline_uniform_data(device, set,
1371 entry->binding,
1372 data + entry->offset,
1373 entry->array_element,
1374 entry->array_count);
1375 break;
1376
1377 default:
1378 break;
1379 }
1380 }
1381 }
1382
1383 VkResult anv_CreateDescriptorUpdateTemplate(
1384 VkDevice _device,
1385 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
1386 const VkAllocationCallbacks* pAllocator,
1387 VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate)
1388 {
1389 ANV_FROM_HANDLE(anv_device, device, _device);
1390 struct anv_descriptor_update_template *template;
1391
1392 size_t size = sizeof(*template) +
1393 pCreateInfo->descriptorUpdateEntryCount * sizeof(template->entries[0]);
1394 template = vk_alloc2(&device->alloc, pAllocator, size, 8,
1395 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1396 if (template == NULL)
1397 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1398
1399 template->bind_point = pCreateInfo->pipelineBindPoint;
1400
1401 if (pCreateInfo->templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET)
1402 template->set = pCreateInfo->set;
1403
1404 template->entry_count = pCreateInfo->descriptorUpdateEntryCount;
1405 for (uint32_t i = 0; i < template->entry_count; i++) {
1406 const VkDescriptorUpdateTemplateEntry *pEntry =
1407 &pCreateInfo->pDescriptorUpdateEntries[i];
1408
1409 template->entries[i] = (struct anv_descriptor_template_entry) {
1410 .type = pEntry->descriptorType,
1411 .binding = pEntry->dstBinding,
1412 .array_element = pEntry->dstArrayElement,
1413 .array_count = pEntry->descriptorCount,
1414 .offset = pEntry->offset,
1415 .stride = pEntry->stride,
1416 };
1417 }
1418
1419 *pDescriptorUpdateTemplate =
1420 anv_descriptor_update_template_to_handle(template);
1421
1422 return VK_SUCCESS;
1423 }
1424
1425 void anv_DestroyDescriptorUpdateTemplate(
1426 VkDevice _device,
1427 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1428 const VkAllocationCallbacks* pAllocator)
1429 {
1430 ANV_FROM_HANDLE(anv_device, device, _device);
1431 ANV_FROM_HANDLE(anv_descriptor_update_template, template,
1432 descriptorUpdateTemplate);
1433
1434 vk_free2(&device->alloc, pAllocator, template);
1435 }
1436
1437 void anv_UpdateDescriptorSetWithTemplate(
1438 VkDevice _device,
1439 VkDescriptorSet descriptorSet,
1440 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1441 const void* pData)
1442 {
1443 ANV_FROM_HANDLE(anv_device, device, _device);
1444 ANV_FROM_HANDLE(anv_descriptor_set, set, descriptorSet);
1445 ANV_FROM_HANDLE(anv_descriptor_update_template, template,
1446 descriptorUpdateTemplate);
1447
1448 anv_descriptor_set_write_template(device, set, NULL, template, pData);
1449 }