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