anv/descriptor_set: Only vma_heap_finish if we have a descriptor buffer
[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 static const VkDescriptorBindingFlagBitsEXT flags_requiring_bindless =
212 VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT |
213 VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT |
214 VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT;
215
216 return (binding->flags & flags_requiring_bindless) != 0;
217 }
218
219 void anv_GetDescriptorSetLayoutSupport(
220 VkDevice _device,
221 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
222 VkDescriptorSetLayoutSupport* pSupport)
223 {
224 ANV_FROM_HANDLE(anv_device, device, _device);
225 const struct anv_physical_device *pdevice =
226 &device->instance->physicalDevice;
227
228 uint32_t surface_count[MESA_SHADER_STAGES] = { 0, };
229
230 for (uint32_t b = 0; b < pCreateInfo->bindingCount; b++) {
231 const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[b];
232
233 enum anv_descriptor_data desc_data =
234 anv_descriptor_data_for_type(pdevice, binding->descriptorType);
235
236 switch (binding->descriptorType) {
237 case VK_DESCRIPTOR_TYPE_SAMPLER:
238 /* There is no real limit on samplers */
239 break;
240
241 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
242 if (anv_descriptor_data_supports_bindless(pdevice, desc_data, false))
243 break;
244
245 if (binding->pImmutableSamplers) {
246 for (uint32_t i = 0; i < binding->descriptorCount; i++) {
247 ANV_FROM_HANDLE(anv_sampler, sampler,
248 binding->pImmutableSamplers[i]);
249 anv_foreach_stage(s, binding->stageFlags)
250 surface_count[s] += sampler->n_planes;
251 }
252 } else {
253 anv_foreach_stage(s, binding->stageFlags)
254 surface_count[s] += binding->descriptorCount;
255 }
256 break;
257
258 default:
259 if (anv_descriptor_data_supports_bindless(pdevice, desc_data, false))
260 break;
261
262 anv_foreach_stage(s, binding->stageFlags)
263 surface_count[s] += binding->descriptorCount;
264 break;
265 }
266 }
267
268 bool supported = true;
269 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
270 /* Our maximum binding table size is 240 and we need to reserve 8 for
271 * render targets.
272 */
273 if (surface_count[s] >= MAX_BINDING_TABLE_SIZE - MAX_RTS)
274 supported = false;
275 }
276
277 pSupport->supported = supported;
278 }
279
280 VkResult anv_CreateDescriptorSetLayout(
281 VkDevice _device,
282 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
283 const VkAllocationCallbacks* pAllocator,
284 VkDescriptorSetLayout* pSetLayout)
285 {
286 ANV_FROM_HANDLE(anv_device, device, _device);
287
288 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO);
289
290 uint32_t max_binding = 0;
291 uint32_t immutable_sampler_count = 0;
292 for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
293 max_binding = MAX2(max_binding, pCreateInfo->pBindings[j].binding);
294
295 /* From the Vulkan 1.1.97 spec for VkDescriptorSetLayoutBinding:
296 *
297 * "If descriptorType specifies a VK_DESCRIPTOR_TYPE_SAMPLER or
298 * VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER type descriptor, then
299 * pImmutableSamplers can be used to initialize a set of immutable
300 * samplers. [...] If descriptorType is not one of these descriptor
301 * types, then pImmutableSamplers is ignored.
302 *
303 * We need to be careful here and only parse pImmutableSamplers if we
304 * have one of the right descriptor types.
305 */
306 VkDescriptorType desc_type = pCreateInfo->pBindings[j].descriptorType;
307 if ((desc_type == VK_DESCRIPTOR_TYPE_SAMPLER ||
308 desc_type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) &&
309 pCreateInfo->pBindings[j].pImmutableSamplers)
310 immutable_sampler_count += pCreateInfo->pBindings[j].descriptorCount;
311 }
312
313 struct anv_descriptor_set_layout *set_layout;
314 struct anv_descriptor_set_binding_layout *bindings;
315 struct anv_sampler **samplers;
316
317 /* We need to allocate decriptor set layouts off the device allocator
318 * with DEVICE scope because they are reference counted and may not be
319 * destroyed when vkDestroyDescriptorSetLayout is called.
320 */
321 ANV_MULTIALLOC(ma);
322 anv_multialloc_add(&ma, &set_layout, 1);
323 anv_multialloc_add(&ma, &bindings, max_binding + 1);
324 anv_multialloc_add(&ma, &samplers, immutable_sampler_count);
325
326 if (!anv_multialloc_alloc(&ma, &device->alloc,
327 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE))
328 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
329
330 memset(set_layout, 0, sizeof(*set_layout));
331 set_layout->ref_cnt = 1;
332 set_layout->binding_count = max_binding + 1;
333
334 for (uint32_t b = 0; b <= max_binding; b++) {
335 /* Initialize all binding_layout entries to -1 */
336 memset(&set_layout->binding[b], -1, sizeof(set_layout->binding[b]));
337
338 set_layout->binding[b].flags = 0;
339 set_layout->binding[b].data = 0;
340 set_layout->binding[b].max_plane_count = 0;
341 set_layout->binding[b].array_size = 0;
342 set_layout->binding[b].immutable_samplers = NULL;
343 }
344
345 /* Initialize all samplers to 0 */
346 memset(samplers, 0, immutable_sampler_count * sizeof(*samplers));
347
348 uint32_t buffer_view_count = 0;
349 uint32_t dynamic_offset_count = 0;
350 uint32_t descriptor_buffer_size = 0;
351
352 for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
353 const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[j];
354 uint32_t b = binding->binding;
355 /* We temporarily store pCreateInfo->pBindings[] index (plus one) in the
356 * immutable_samplers pointer. This provides us with a quick-and-dirty
357 * way to sort the bindings by binding number.
358 */
359 set_layout->binding[b].immutable_samplers = (void *)(uintptr_t)(j + 1);
360 }
361
362 const VkDescriptorSetLayoutBindingFlagsCreateInfoEXT *binding_flags_info =
363 vk_find_struct_const(pCreateInfo->pNext,
364 DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO_EXT);
365
366 for (uint32_t b = 0; b <= max_binding; b++) {
367 /* We stashed the pCreateInfo->pBindings[] index (plus one) in the
368 * immutable_samplers pointer. Check for NULL (empty binding) and then
369 * reset it and compute the index.
370 */
371 if (set_layout->binding[b].immutable_samplers == NULL)
372 continue;
373 const uint32_t info_idx =
374 (uintptr_t)(void *)set_layout->binding[b].immutable_samplers - 1;
375 set_layout->binding[b].immutable_samplers = NULL;
376
377 const VkDescriptorSetLayoutBinding *binding =
378 &pCreateInfo->pBindings[info_idx];
379
380 if (binding->descriptorCount == 0)
381 continue;
382
383 #ifndef NDEBUG
384 set_layout->binding[b].type = binding->descriptorType;
385 #endif
386
387 if (binding_flags_info && binding_flags_info->bindingCount > 0) {
388 assert(binding_flags_info->bindingCount == pCreateInfo->bindingCount);
389 set_layout->binding[b].flags =
390 binding_flags_info->pBindingFlags[info_idx];
391 }
392
393 set_layout->binding[b].data =
394 anv_descriptor_data_for_type(&device->instance->physicalDevice,
395 binding->descriptorType);
396 set_layout->binding[b].array_size = binding->descriptorCount;
397 set_layout->binding[b].descriptor_index = set_layout->size;
398 set_layout->size += binding->descriptorCount;
399
400 if (set_layout->binding[b].data & ANV_DESCRIPTOR_BUFFER_VIEW) {
401 set_layout->binding[b].buffer_view_index = buffer_view_count;
402 buffer_view_count += binding->descriptorCount;
403 }
404
405 switch (binding->descriptorType) {
406 case VK_DESCRIPTOR_TYPE_SAMPLER:
407 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
408 set_layout->binding[b].max_plane_count = 1;
409 if (binding->pImmutableSamplers) {
410 set_layout->binding[b].immutable_samplers = samplers;
411 samplers += binding->descriptorCount;
412
413 for (uint32_t i = 0; i < binding->descriptorCount; i++) {
414 ANV_FROM_HANDLE(anv_sampler, sampler,
415 binding->pImmutableSamplers[i]);
416
417 set_layout->binding[b].immutable_samplers[i] = sampler;
418 if (set_layout->binding[b].max_plane_count < sampler->n_planes)
419 set_layout->binding[b].max_plane_count = sampler->n_planes;
420 }
421 }
422 break;
423
424 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
425 set_layout->binding[b].max_plane_count = 1;
426 break;
427
428 default:
429 break;
430 }
431
432 switch (binding->descriptorType) {
433 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
434 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
435 set_layout->binding[b].dynamic_offset_index = dynamic_offset_count;
436 dynamic_offset_count += binding->descriptorCount;
437 break;
438
439 default:
440 break;
441 }
442
443 if (binding->descriptorType ==
444 VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
445 /* Inline uniform blocks are specified to use the descriptor array
446 * size as the size in bytes of the block.
447 */
448 descriptor_buffer_size = align_u32(descriptor_buffer_size, 32);
449 set_layout->binding[b].descriptor_offset = descriptor_buffer_size;
450 descriptor_buffer_size += binding->descriptorCount;
451 } else {
452 set_layout->binding[b].descriptor_offset = descriptor_buffer_size;
453 descriptor_buffer_size += anv_descriptor_size(&set_layout->binding[b]) *
454 binding->descriptorCount;
455 }
456
457 set_layout->shader_stages |= binding->stageFlags;
458 }
459
460 set_layout->buffer_view_count = buffer_view_count;
461 set_layout->dynamic_offset_count = dynamic_offset_count;
462 set_layout->descriptor_buffer_size = descriptor_buffer_size;
463
464 *pSetLayout = anv_descriptor_set_layout_to_handle(set_layout);
465
466 return VK_SUCCESS;
467 }
468
469 void anv_DestroyDescriptorSetLayout(
470 VkDevice _device,
471 VkDescriptorSetLayout _set_layout,
472 const VkAllocationCallbacks* pAllocator)
473 {
474 ANV_FROM_HANDLE(anv_device, device, _device);
475 ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout, _set_layout);
476
477 if (!set_layout)
478 return;
479
480 anv_descriptor_set_layout_unref(device, set_layout);
481 }
482
483 #define SHA1_UPDATE_VALUE(ctx, x) _mesa_sha1_update(ctx, &(x), sizeof(x));
484
485 static void
486 sha1_update_immutable_sampler(struct mesa_sha1 *ctx,
487 const struct anv_sampler *sampler)
488 {
489 if (!sampler->conversion)
490 return;
491
492 /* The only thing that affects the shader is ycbcr conversion */
493 _mesa_sha1_update(ctx, sampler->conversion,
494 sizeof(*sampler->conversion));
495 }
496
497 static void
498 sha1_update_descriptor_set_binding_layout(struct mesa_sha1 *ctx,
499 const struct anv_descriptor_set_binding_layout *layout)
500 {
501 SHA1_UPDATE_VALUE(ctx, layout->flags);
502 SHA1_UPDATE_VALUE(ctx, layout->data);
503 SHA1_UPDATE_VALUE(ctx, layout->max_plane_count);
504 SHA1_UPDATE_VALUE(ctx, layout->array_size);
505 SHA1_UPDATE_VALUE(ctx, layout->descriptor_index);
506 SHA1_UPDATE_VALUE(ctx, layout->dynamic_offset_index);
507 SHA1_UPDATE_VALUE(ctx, layout->buffer_view_index);
508 SHA1_UPDATE_VALUE(ctx, layout->descriptor_offset);
509
510 if (layout->immutable_samplers) {
511 for (uint16_t i = 0; i < layout->array_size; i++)
512 sha1_update_immutable_sampler(ctx, layout->immutable_samplers[i]);
513 }
514 }
515
516 static void
517 sha1_update_descriptor_set_layout(struct mesa_sha1 *ctx,
518 const struct anv_descriptor_set_layout *layout)
519 {
520 SHA1_UPDATE_VALUE(ctx, layout->binding_count);
521 SHA1_UPDATE_VALUE(ctx, layout->size);
522 SHA1_UPDATE_VALUE(ctx, layout->shader_stages);
523 SHA1_UPDATE_VALUE(ctx, layout->buffer_view_count);
524 SHA1_UPDATE_VALUE(ctx, layout->dynamic_offset_count);
525 SHA1_UPDATE_VALUE(ctx, layout->descriptor_buffer_size);
526
527 for (uint16_t i = 0; i < layout->binding_count; i++)
528 sha1_update_descriptor_set_binding_layout(ctx, &layout->binding[i]);
529 }
530
531 /*
532 * Pipeline layouts. These have nothing to do with the pipeline. They are
533 * just multiple descriptor set layouts pasted together
534 */
535
536 VkResult anv_CreatePipelineLayout(
537 VkDevice _device,
538 const VkPipelineLayoutCreateInfo* pCreateInfo,
539 const VkAllocationCallbacks* pAllocator,
540 VkPipelineLayout* pPipelineLayout)
541 {
542 ANV_FROM_HANDLE(anv_device, device, _device);
543 struct anv_pipeline_layout *layout;
544
545 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
546
547 layout = vk_alloc2(&device->alloc, pAllocator, sizeof(*layout), 8,
548 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
549 if (layout == NULL)
550 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
551
552 layout->num_sets = pCreateInfo->setLayoutCount;
553
554 unsigned dynamic_offset_count = 0;
555
556 for (uint32_t set = 0; set < pCreateInfo->setLayoutCount; set++) {
557 ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout,
558 pCreateInfo->pSetLayouts[set]);
559 layout->set[set].layout = set_layout;
560 anv_descriptor_set_layout_ref(set_layout);
561
562 layout->set[set].dynamic_offset_start = dynamic_offset_count;
563 for (uint32_t b = 0; b < set_layout->binding_count; b++) {
564 if (set_layout->binding[b].dynamic_offset_index < 0)
565 continue;
566
567 dynamic_offset_count += set_layout->binding[b].array_size;
568 }
569 }
570
571 struct mesa_sha1 ctx;
572 _mesa_sha1_init(&ctx);
573 for (unsigned s = 0; s < layout->num_sets; s++) {
574 sha1_update_descriptor_set_layout(&ctx, layout->set[s].layout);
575 _mesa_sha1_update(&ctx, &layout->set[s].dynamic_offset_start,
576 sizeof(layout->set[s].dynamic_offset_start));
577 }
578 _mesa_sha1_update(&ctx, &layout->num_sets, sizeof(layout->num_sets));
579 _mesa_sha1_final(&ctx, layout->sha1);
580
581 *pPipelineLayout = anv_pipeline_layout_to_handle(layout);
582
583 return VK_SUCCESS;
584 }
585
586 void anv_DestroyPipelineLayout(
587 VkDevice _device,
588 VkPipelineLayout _pipelineLayout,
589 const VkAllocationCallbacks* pAllocator)
590 {
591 ANV_FROM_HANDLE(anv_device, device, _device);
592 ANV_FROM_HANDLE(anv_pipeline_layout, pipeline_layout, _pipelineLayout);
593
594 if (!pipeline_layout)
595 return;
596
597 for (uint32_t i = 0; i < pipeline_layout->num_sets; i++)
598 anv_descriptor_set_layout_unref(device, pipeline_layout->set[i].layout);
599
600 vk_free2(&device->alloc, pAllocator, pipeline_layout);
601 }
602
603 /*
604 * Descriptor pools.
605 *
606 * These are implemented using a big pool of memory and a free-list for the
607 * host memory allocations and a state_stream and a free list for the buffer
608 * view surface state. The spec allows us to fail to allocate due to
609 * fragmentation in all cases but two: 1) after pool reset, allocating up
610 * until the pool size with no freeing must succeed and 2) allocating and
611 * freeing only descriptor sets with the same layout. Case 1) is easy enogh,
612 * and the free lists lets us recycle blocks for case 2).
613 */
614
615 /* The vma heap reserves 0 to mean NULL; we have to offset by some ammount to
616 * ensure we can allocate the entire BO without hitting zero. The actual
617 * amount doesn't matter.
618 */
619 #define POOL_HEAP_OFFSET 64
620
621 #define EMPTY 1
622
623 VkResult anv_CreateDescriptorPool(
624 VkDevice _device,
625 const VkDescriptorPoolCreateInfo* pCreateInfo,
626 const VkAllocationCallbacks* pAllocator,
627 VkDescriptorPool* pDescriptorPool)
628 {
629 ANV_FROM_HANDLE(anv_device, device, _device);
630 struct anv_descriptor_pool *pool;
631
632 const VkDescriptorPoolInlineUniformBlockCreateInfoEXT *inline_info =
633 vk_find_struct_const(pCreateInfo->pNext,
634 DESCRIPTOR_POOL_INLINE_UNIFORM_BLOCK_CREATE_INFO_EXT);
635
636 uint32_t descriptor_count = 0;
637 uint32_t buffer_view_count = 0;
638 uint32_t descriptor_bo_size = 0;
639 for (uint32_t i = 0; i < pCreateInfo->poolSizeCount; i++) {
640 enum anv_descriptor_data desc_data =
641 anv_descriptor_data_for_type(&device->instance->physicalDevice,
642 pCreateInfo->pPoolSizes[i].type);
643
644 if (desc_data & ANV_DESCRIPTOR_BUFFER_VIEW)
645 buffer_view_count += pCreateInfo->pPoolSizes[i].descriptorCount;
646
647 unsigned desc_data_size = anv_descriptor_data_size(desc_data) *
648 pCreateInfo->pPoolSizes[i].descriptorCount;
649
650 /* Combined image sampler descriptors can take up to 3 slots if they
651 * hold a YCbCr image.
652 */
653 if (pCreateInfo->pPoolSizes[i].type ==
654 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
655 desc_data_size *= 3;
656
657 if (pCreateInfo->pPoolSizes[i].type ==
658 VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
659 /* Inline uniform blocks are specified to use the descriptor array
660 * size as the size in bytes of the block.
661 */
662 assert(inline_info);
663 desc_data_size += pCreateInfo->pPoolSizes[i].descriptorCount;
664 }
665
666 descriptor_bo_size += desc_data_size;
667
668 descriptor_count += pCreateInfo->pPoolSizes[i].descriptorCount;
669 }
670 /* We have to align descriptor buffer allocations to 32B so that we can
671 * push descriptor buffers. This means that each descriptor buffer
672 * allocated may burn up to 32B of extra space to get the right alignment.
673 * (Technically, it's at most 28B because we're always going to start at
674 * least 4B aligned but we're being conservative here.) Allocate enough
675 * extra space that we can chop it into maxSets pieces and align each one
676 * of them to 32B.
677 */
678 descriptor_bo_size += 32 * pCreateInfo->maxSets;
679 descriptor_bo_size = ALIGN(descriptor_bo_size, 4096);
680 /* We align inline uniform blocks to 32B */
681 if (inline_info)
682 descriptor_bo_size += 32 * inline_info->maxInlineUniformBlockBindings;
683
684 const size_t pool_size =
685 pCreateInfo->maxSets * sizeof(struct anv_descriptor_set) +
686 descriptor_count * sizeof(struct anv_descriptor) +
687 buffer_view_count * sizeof(struct anv_buffer_view);
688 const size_t total_size = sizeof(*pool) + pool_size;
689
690 pool = vk_alloc2(&device->alloc, pAllocator, total_size, 8,
691 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
692 if (!pool)
693 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
694
695 pool->size = pool_size;
696 pool->next = 0;
697 pool->free_list = EMPTY;
698
699 if (descriptor_bo_size > 0) {
700 VkResult result = anv_bo_init_new(&pool->bo, device, descriptor_bo_size);
701 if (result != VK_SUCCESS) {
702 vk_free2(&device->alloc, pAllocator, pool);
703 return result;
704 }
705
706 anv_gem_set_caching(device, pool->bo.gem_handle, I915_CACHING_CACHED);
707
708 pool->bo.map = anv_gem_mmap(device, pool->bo.gem_handle, 0,
709 descriptor_bo_size, 0);
710 if (pool->bo.map == NULL) {
711 anv_gem_close(device, pool->bo.gem_handle);
712 vk_free2(&device->alloc, pAllocator, pool);
713 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
714 }
715
716 if (device->instance->physicalDevice.use_softpin) {
717 pool->bo.flags |= EXEC_OBJECT_PINNED;
718 anv_vma_alloc(device, &pool->bo);
719 }
720
721 util_vma_heap_init(&pool->bo_heap, POOL_HEAP_OFFSET, descriptor_bo_size);
722 } else {
723 pool->bo.size = 0;
724 }
725
726 anv_state_stream_init(&pool->surface_state_stream,
727 &device->surface_state_pool, 4096);
728 pool->surface_state_free_list = NULL;
729
730 list_inithead(&pool->desc_sets);
731
732 *pDescriptorPool = anv_descriptor_pool_to_handle(pool);
733
734 return VK_SUCCESS;
735 }
736
737 void anv_DestroyDescriptorPool(
738 VkDevice _device,
739 VkDescriptorPool _pool,
740 const VkAllocationCallbacks* pAllocator)
741 {
742 ANV_FROM_HANDLE(anv_device, device, _device);
743 ANV_FROM_HANDLE(anv_descriptor_pool, pool, _pool);
744
745 if (!pool)
746 return;
747
748 list_for_each_entry_safe(struct anv_descriptor_set, set,
749 &pool->desc_sets, pool_link) {
750 anv_descriptor_set_destroy(device, pool, set);
751 }
752
753 if (pool->bo.size) {
754 anv_gem_munmap(pool->bo.map, pool->bo.size);
755 anv_vma_free(device, &pool->bo);
756 anv_gem_close(device, pool->bo.gem_handle);
757 util_vma_heap_finish(&pool->bo_heap);
758 }
759 anv_state_stream_finish(&pool->surface_state_stream);
760
761 vk_free2(&device->alloc, pAllocator, pool);
762 }
763
764 VkResult anv_ResetDescriptorPool(
765 VkDevice _device,
766 VkDescriptorPool descriptorPool,
767 VkDescriptorPoolResetFlags flags)
768 {
769 ANV_FROM_HANDLE(anv_device, device, _device);
770 ANV_FROM_HANDLE(anv_descriptor_pool, pool, descriptorPool);
771
772 list_for_each_entry_safe(struct anv_descriptor_set, set,
773 &pool->desc_sets, pool_link) {
774 anv_descriptor_set_destroy(device, pool, set);
775 }
776
777 pool->next = 0;
778 pool->free_list = EMPTY;
779
780 if (pool->bo.size) {
781 util_vma_heap_finish(&pool->bo_heap);
782 util_vma_heap_init(&pool->bo_heap, POOL_HEAP_OFFSET, pool->bo.size);
783 }
784
785 anv_state_stream_finish(&pool->surface_state_stream);
786 anv_state_stream_init(&pool->surface_state_stream,
787 &device->surface_state_pool, 4096);
788 pool->surface_state_free_list = NULL;
789
790 return VK_SUCCESS;
791 }
792
793 struct pool_free_list_entry {
794 uint32_t next;
795 uint32_t size;
796 };
797
798 static VkResult
799 anv_descriptor_pool_alloc_set(struct anv_descriptor_pool *pool,
800 uint32_t size,
801 struct anv_descriptor_set **set)
802 {
803 if (size <= pool->size - pool->next) {
804 *set = (struct anv_descriptor_set *) (pool->data + pool->next);
805 pool->next += size;
806 return VK_SUCCESS;
807 } else {
808 struct pool_free_list_entry *entry;
809 uint32_t *link = &pool->free_list;
810 for (uint32_t f = pool->free_list; f != EMPTY; f = entry->next) {
811 entry = (struct pool_free_list_entry *) (pool->data + f);
812 if (size <= entry->size) {
813 *link = entry->next;
814 *set = (struct anv_descriptor_set *) entry;
815 return VK_SUCCESS;
816 }
817 link = &entry->next;
818 }
819
820 if (pool->free_list != EMPTY) {
821 return vk_error(VK_ERROR_FRAGMENTED_POOL);
822 } else {
823 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY);
824 }
825 }
826 }
827
828 static void
829 anv_descriptor_pool_free_set(struct anv_descriptor_pool *pool,
830 struct anv_descriptor_set *set)
831 {
832 /* Put the descriptor set allocation back on the free list. */
833 const uint32_t index = (char *) set - pool->data;
834 if (index + set->size == pool->next) {
835 pool->next = index;
836 } else {
837 struct pool_free_list_entry *entry = (struct pool_free_list_entry *) set;
838 entry->next = pool->free_list;
839 entry->size = set->size;
840 pool->free_list = (char *) entry - pool->data;
841 }
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 list_addtail(&set->pool_link, &pool->desc_sets);
972
973 *out_set = set;
974
975 return VK_SUCCESS;
976 }
977
978 void
979 anv_descriptor_set_destroy(struct anv_device *device,
980 struct anv_descriptor_pool *pool,
981 struct anv_descriptor_set *set)
982 {
983 anv_descriptor_set_layout_unref(device, set->layout);
984
985 if (set->desc_mem.alloc_size) {
986 util_vma_heap_free(&pool->bo_heap,
987 (uint64_t)set->desc_mem.offset + POOL_HEAP_OFFSET,
988 set->desc_mem.alloc_size);
989 anv_descriptor_pool_free_state(pool, set->desc_surface_state);
990 }
991
992 for (uint32_t b = 0; b < set->buffer_view_count; b++)
993 anv_descriptor_pool_free_state(pool, set->buffer_views[b].surface_state);
994
995 list_del(&set->pool_link);
996
997 anv_descriptor_pool_free_set(pool, set);
998 }
999
1000 VkResult anv_AllocateDescriptorSets(
1001 VkDevice _device,
1002 const VkDescriptorSetAllocateInfo* pAllocateInfo,
1003 VkDescriptorSet* pDescriptorSets)
1004 {
1005 ANV_FROM_HANDLE(anv_device, device, _device);
1006 ANV_FROM_HANDLE(anv_descriptor_pool, pool, pAllocateInfo->descriptorPool);
1007
1008 VkResult result = VK_SUCCESS;
1009 struct anv_descriptor_set *set;
1010 uint32_t i;
1011
1012 for (i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
1013 ANV_FROM_HANDLE(anv_descriptor_set_layout, layout,
1014 pAllocateInfo->pSetLayouts[i]);
1015
1016 result = anv_descriptor_set_create(device, pool, layout, &set);
1017 if (result != VK_SUCCESS)
1018 break;
1019
1020 pDescriptorSets[i] = anv_descriptor_set_to_handle(set);
1021 }
1022
1023 if (result != VK_SUCCESS)
1024 anv_FreeDescriptorSets(_device, pAllocateInfo->descriptorPool,
1025 i, pDescriptorSets);
1026
1027 return result;
1028 }
1029
1030 VkResult anv_FreeDescriptorSets(
1031 VkDevice _device,
1032 VkDescriptorPool descriptorPool,
1033 uint32_t count,
1034 const VkDescriptorSet* pDescriptorSets)
1035 {
1036 ANV_FROM_HANDLE(anv_device, device, _device);
1037 ANV_FROM_HANDLE(anv_descriptor_pool, pool, descriptorPool);
1038
1039 for (uint32_t i = 0; i < count; i++) {
1040 ANV_FROM_HANDLE(anv_descriptor_set, set, pDescriptorSets[i]);
1041
1042 if (!set)
1043 continue;
1044
1045 anv_descriptor_set_destroy(device, pool, set);
1046 }
1047
1048 return VK_SUCCESS;
1049 }
1050
1051 static void
1052 anv_descriptor_set_write_image_param(uint32_t *param_desc_map,
1053 const struct brw_image_param *param)
1054 {
1055 #define WRITE_PARAM_FIELD(field, FIELD) \
1056 for (unsigned i = 0; i < ARRAY_SIZE(param->field); i++) \
1057 param_desc_map[BRW_IMAGE_PARAM_##FIELD##_OFFSET + i] = param->field[i]
1058
1059 WRITE_PARAM_FIELD(offset, OFFSET);
1060 WRITE_PARAM_FIELD(size, SIZE);
1061 WRITE_PARAM_FIELD(stride, STRIDE);
1062 WRITE_PARAM_FIELD(tiling, TILING);
1063 WRITE_PARAM_FIELD(swizzling, SWIZZLING);
1064 WRITE_PARAM_FIELD(size, SIZE);
1065
1066 #undef WRITE_PARAM_FIELD
1067 }
1068
1069 static uint32_t
1070 anv_surface_state_to_handle(struct anv_state state)
1071 {
1072 /* Bits 31:12 of the bindless surface offset in the extended message
1073 * descriptor is bits 25:6 of the byte-based address.
1074 */
1075 assert(state.offset >= 0);
1076 uint32_t offset = state.offset;
1077 assert((offset & 0x3f) == 0 && offset < (1 << 26));
1078 return offset << 6;
1079 }
1080
1081 void
1082 anv_descriptor_set_write_image_view(struct anv_device *device,
1083 struct anv_descriptor_set *set,
1084 const VkDescriptorImageInfo * const info,
1085 VkDescriptorType type,
1086 uint32_t binding,
1087 uint32_t element)
1088 {
1089 const struct anv_descriptor_set_binding_layout *bind_layout =
1090 &set->layout->binding[binding];
1091 struct anv_descriptor *desc =
1092 &set->descriptors[bind_layout->descriptor_index + element];
1093 struct anv_image_view *image_view = NULL;
1094 struct anv_sampler *sampler = NULL;
1095
1096 /* We get called with just VK_DESCRIPTOR_TYPE_SAMPLER as part of descriptor
1097 * set initialization to set the bindless samplers.
1098 */
1099 assert(type == bind_layout->type ||
1100 type == VK_DESCRIPTOR_TYPE_SAMPLER);
1101
1102 switch (type) {
1103 case VK_DESCRIPTOR_TYPE_SAMPLER:
1104 sampler = anv_sampler_from_handle(info->sampler);
1105 break;
1106
1107 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1108 image_view = anv_image_view_from_handle(info->imageView);
1109 sampler = anv_sampler_from_handle(info->sampler);
1110 break;
1111
1112 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1113 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1114 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1115 image_view = anv_image_view_from_handle(info->imageView);
1116 break;
1117
1118 default:
1119 unreachable("invalid descriptor type");
1120 }
1121
1122 /* If this descriptor has an immutable sampler, we don't want to stomp on
1123 * it.
1124 */
1125 sampler = bind_layout->immutable_samplers ?
1126 bind_layout->immutable_samplers[element] :
1127 sampler;
1128
1129 *desc = (struct anv_descriptor) {
1130 .type = type,
1131 .layout = info->imageLayout,
1132 .image_view = image_view,
1133 .sampler = sampler,
1134 };
1135
1136 void *desc_map = set->desc_mem.map + bind_layout->descriptor_offset +
1137 element * anv_descriptor_size(bind_layout);
1138
1139 if (bind_layout->data & ANV_DESCRIPTOR_SAMPLED_IMAGE) {
1140 struct anv_sampled_image_descriptor desc_data[3];
1141 memset(desc_data, 0, sizeof(desc_data));
1142
1143 if (image_view) {
1144 for (unsigned p = 0; p < image_view->n_planes; p++) {
1145 struct anv_surface_state sstate =
1146 (desc->layout == VK_IMAGE_LAYOUT_GENERAL) ?
1147 image_view->planes[p].general_sampler_surface_state :
1148 image_view->planes[p].optimal_sampler_surface_state;
1149 desc_data[p].image = anv_surface_state_to_handle(sstate.state);
1150 }
1151 }
1152
1153 if (sampler) {
1154 for (unsigned p = 0; p < sampler->n_planes; p++)
1155 desc_data[p].sampler = sampler->bindless_state.offset + p * 32;
1156 }
1157
1158 /* We may have max_plane_count < 0 if this isn't a sampled image but it
1159 * can be no more than the size of our array of handles.
1160 */
1161 assert(bind_layout->max_plane_count <= ARRAY_SIZE(desc_data));
1162 memcpy(desc_map, desc_data,
1163 MAX2(1, bind_layout->max_plane_count) * sizeof(desc_data[0]));
1164 }
1165
1166 if (bind_layout->data & ANV_DESCRIPTOR_STORAGE_IMAGE) {
1167 assert(!(bind_layout->data & ANV_DESCRIPTOR_IMAGE_PARAM));
1168 assert(image_view->n_planes == 1);
1169 struct anv_storage_image_descriptor desc_data = {
1170 .read_write = anv_surface_state_to_handle(
1171 image_view->planes[0].storage_surface_state.state),
1172 .write_only = anv_surface_state_to_handle(
1173 image_view->planes[0].writeonly_storage_surface_state.state),
1174 };
1175 memcpy(desc_map, &desc_data, sizeof(desc_data));
1176 }
1177
1178 if (bind_layout->data & ANV_DESCRIPTOR_IMAGE_PARAM) {
1179 /* Storage images can only ever have one plane */
1180 assert(image_view->n_planes == 1);
1181 const struct brw_image_param *image_param =
1182 &image_view->planes[0].storage_image_param;
1183
1184 anv_descriptor_set_write_image_param(desc_map, image_param);
1185 }
1186 }
1187
1188 void
1189 anv_descriptor_set_write_buffer_view(struct anv_device *device,
1190 struct anv_descriptor_set *set,
1191 VkDescriptorType type,
1192 struct anv_buffer_view *buffer_view,
1193 uint32_t binding,
1194 uint32_t element)
1195 {
1196 const struct anv_descriptor_set_binding_layout *bind_layout =
1197 &set->layout->binding[binding];
1198 struct anv_descriptor *desc =
1199 &set->descriptors[bind_layout->descriptor_index + element];
1200
1201 assert(type == bind_layout->type);
1202
1203 *desc = (struct anv_descriptor) {
1204 .type = type,
1205 .buffer_view = buffer_view,
1206 };
1207
1208 void *desc_map = set->desc_mem.map + bind_layout->descriptor_offset +
1209 element * anv_descriptor_size(bind_layout);
1210
1211 if (bind_layout->data & ANV_DESCRIPTOR_SAMPLED_IMAGE) {
1212 struct anv_sampled_image_descriptor desc_data = {
1213 .image = anv_surface_state_to_handle(buffer_view->surface_state),
1214 };
1215 memcpy(desc_map, &desc_data, sizeof(desc_data));
1216 }
1217
1218 if (bind_layout->data & ANV_DESCRIPTOR_STORAGE_IMAGE) {
1219 assert(!(bind_layout->data & ANV_DESCRIPTOR_IMAGE_PARAM));
1220 struct anv_storage_image_descriptor desc_data = {
1221 .read_write = anv_surface_state_to_handle(
1222 buffer_view->storage_surface_state),
1223 .write_only = anv_surface_state_to_handle(
1224 buffer_view->writeonly_storage_surface_state),
1225 };
1226 memcpy(desc_map, &desc_data, sizeof(desc_data));
1227 }
1228
1229 if (bind_layout->data & ANV_DESCRIPTOR_IMAGE_PARAM) {
1230 anv_descriptor_set_write_image_param(desc_map,
1231 &buffer_view->storage_image_param);
1232 }
1233 }
1234
1235 void
1236 anv_descriptor_set_write_buffer(struct anv_device *device,
1237 struct anv_descriptor_set *set,
1238 struct anv_state_stream *alloc_stream,
1239 VkDescriptorType type,
1240 struct anv_buffer *buffer,
1241 uint32_t binding,
1242 uint32_t element,
1243 VkDeviceSize offset,
1244 VkDeviceSize range)
1245 {
1246 const struct anv_descriptor_set_binding_layout *bind_layout =
1247 &set->layout->binding[binding];
1248 struct anv_descriptor *desc =
1249 &set->descriptors[bind_layout->descriptor_index + element];
1250
1251 assert(type == bind_layout->type);
1252
1253 struct anv_address bind_addr = anv_address_add(buffer->address, offset);
1254 uint64_t bind_range = anv_buffer_get_range(buffer, offset, range);
1255
1256 if (type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
1257 type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
1258 *desc = (struct anv_descriptor) {
1259 .type = type,
1260 .buffer = buffer,
1261 .offset = offset,
1262 .range = range,
1263 };
1264 } else {
1265 assert(bind_layout->data & ANV_DESCRIPTOR_BUFFER_VIEW);
1266 struct anv_buffer_view *bview =
1267 &set->buffer_views[bind_layout->buffer_view_index + element];
1268
1269 bview->format = anv_isl_format_for_descriptor_type(type);
1270 bview->range = bind_range;
1271 bview->address = bind_addr;
1272
1273 /* If we're writing descriptors through a push command, we need to
1274 * allocate the surface state from the command buffer. Otherwise it will
1275 * be allocated by the descriptor pool when calling
1276 * vkAllocateDescriptorSets. */
1277 if (alloc_stream)
1278 bview->surface_state = anv_state_stream_alloc(alloc_stream, 64, 64);
1279
1280 anv_fill_buffer_surface_state(device, bview->surface_state,
1281 bview->format, bind_addr, bind_range, 1);
1282
1283 *desc = (struct anv_descriptor) {
1284 .type = type,
1285 .buffer_view = bview,
1286 };
1287 }
1288
1289 void *desc_map = set->desc_mem.map + bind_layout->descriptor_offset +
1290 element * anv_descriptor_size(bind_layout);
1291
1292 if (bind_layout->data & ANV_DESCRIPTOR_ADDRESS_RANGE) {
1293 struct anv_address_range_descriptor desc = {
1294 .address = anv_address_physical(bind_addr),
1295 .range = bind_range,
1296 };
1297 memcpy(desc_map, &desc, sizeof(desc));
1298 }
1299 }
1300
1301 void
1302 anv_descriptor_set_write_inline_uniform_data(struct anv_device *device,
1303 struct anv_descriptor_set *set,
1304 uint32_t binding,
1305 const void *data,
1306 size_t offset,
1307 size_t size)
1308 {
1309 const struct anv_descriptor_set_binding_layout *bind_layout =
1310 &set->layout->binding[binding];
1311
1312 assert(bind_layout->data & ANV_DESCRIPTOR_INLINE_UNIFORM);
1313
1314 void *desc_map = set->desc_mem.map + bind_layout->descriptor_offset;
1315
1316 memcpy(desc_map + offset, data, size);
1317 }
1318
1319 void anv_UpdateDescriptorSets(
1320 VkDevice _device,
1321 uint32_t descriptorWriteCount,
1322 const VkWriteDescriptorSet* pDescriptorWrites,
1323 uint32_t descriptorCopyCount,
1324 const VkCopyDescriptorSet* pDescriptorCopies)
1325 {
1326 ANV_FROM_HANDLE(anv_device, device, _device);
1327
1328 for (uint32_t i = 0; i < descriptorWriteCount; i++) {
1329 const VkWriteDescriptorSet *write = &pDescriptorWrites[i];
1330 ANV_FROM_HANDLE(anv_descriptor_set, set, write->dstSet);
1331
1332 switch (write->descriptorType) {
1333 case VK_DESCRIPTOR_TYPE_SAMPLER:
1334 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1335 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1336 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1337 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1338 for (uint32_t j = 0; j < write->descriptorCount; j++) {
1339 anv_descriptor_set_write_image_view(device, set,
1340 write->pImageInfo + j,
1341 write->descriptorType,
1342 write->dstBinding,
1343 write->dstArrayElement + j);
1344 }
1345 break;
1346
1347 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1348 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1349 for (uint32_t j = 0; j < write->descriptorCount; j++) {
1350 ANV_FROM_HANDLE(anv_buffer_view, bview,
1351 write->pTexelBufferView[j]);
1352
1353 anv_descriptor_set_write_buffer_view(device, set,
1354 write->descriptorType,
1355 bview,
1356 write->dstBinding,
1357 write->dstArrayElement + j);
1358 }
1359 break;
1360
1361 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1362 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1363 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1364 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1365 for (uint32_t j = 0; j < write->descriptorCount; j++) {
1366 assert(write->pBufferInfo[j].buffer);
1367 ANV_FROM_HANDLE(anv_buffer, buffer, write->pBufferInfo[j].buffer);
1368 assert(buffer);
1369
1370 anv_descriptor_set_write_buffer(device, set,
1371 NULL,
1372 write->descriptorType,
1373 buffer,
1374 write->dstBinding,
1375 write->dstArrayElement + j,
1376 write->pBufferInfo[j].offset,
1377 write->pBufferInfo[j].range);
1378 }
1379 break;
1380
1381 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT: {
1382 const VkWriteDescriptorSetInlineUniformBlockEXT *inline_write =
1383 vk_find_struct_const(write->pNext,
1384 WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT);
1385 assert(inline_write->dataSize == write->descriptorCount);
1386 anv_descriptor_set_write_inline_uniform_data(device, set,
1387 write->dstBinding,
1388 inline_write->pData,
1389 write->dstArrayElement,
1390 inline_write->dataSize);
1391 break;
1392 }
1393
1394 default:
1395 break;
1396 }
1397 }
1398
1399 for (uint32_t i = 0; i < descriptorCopyCount; i++) {
1400 const VkCopyDescriptorSet *copy = &pDescriptorCopies[i];
1401 ANV_FROM_HANDLE(anv_descriptor_set, src, copy->srcSet);
1402 ANV_FROM_HANDLE(anv_descriptor_set, dst, copy->dstSet);
1403
1404 const struct anv_descriptor_set_binding_layout *src_layout =
1405 &src->layout->binding[copy->srcBinding];
1406 struct anv_descriptor *src_desc =
1407 &src->descriptors[src_layout->descriptor_index];
1408 src_desc += copy->srcArrayElement;
1409
1410 const struct anv_descriptor_set_binding_layout *dst_layout =
1411 &dst->layout->binding[copy->dstBinding];
1412 struct anv_descriptor *dst_desc =
1413 &dst->descriptors[dst_layout->descriptor_index];
1414 dst_desc += copy->dstArrayElement;
1415
1416 for (uint32_t j = 0; j < copy->descriptorCount; j++)
1417 dst_desc[j] = src_desc[j];
1418
1419 if (src_layout->data & ANV_DESCRIPTOR_INLINE_UNIFORM) {
1420 assert(src_layout->data == ANV_DESCRIPTOR_INLINE_UNIFORM);
1421 memcpy(dst->desc_mem.map + dst_layout->descriptor_offset +
1422 copy->dstArrayElement,
1423 src->desc_mem.map + src_layout->descriptor_offset +
1424 copy->srcArrayElement,
1425 copy->descriptorCount);
1426 } else {
1427 unsigned desc_size = anv_descriptor_size(src_layout);
1428 if (desc_size > 0) {
1429 assert(desc_size == anv_descriptor_size(dst_layout));
1430 memcpy(dst->desc_mem.map + dst_layout->descriptor_offset +
1431 copy->dstArrayElement * desc_size,
1432 src->desc_mem.map + src_layout->descriptor_offset +
1433 copy->srcArrayElement * desc_size,
1434 copy->descriptorCount * desc_size);
1435 }
1436 }
1437 }
1438 }
1439
1440 /*
1441 * Descriptor update templates.
1442 */
1443
1444 void
1445 anv_descriptor_set_write_template(struct anv_device *device,
1446 struct anv_descriptor_set *set,
1447 struct anv_state_stream *alloc_stream,
1448 const struct anv_descriptor_update_template *template,
1449 const void *data)
1450 {
1451 for (uint32_t i = 0; i < template->entry_count; i++) {
1452 const struct anv_descriptor_template_entry *entry =
1453 &template->entries[i];
1454
1455 switch (entry->type) {
1456 case VK_DESCRIPTOR_TYPE_SAMPLER:
1457 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1458 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1459 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1460 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1461 for (uint32_t j = 0; j < entry->array_count; j++) {
1462 const VkDescriptorImageInfo *info =
1463 data + entry->offset + j * entry->stride;
1464 anv_descriptor_set_write_image_view(device, set,
1465 info, entry->type,
1466 entry->binding,
1467 entry->array_element + j);
1468 }
1469 break;
1470
1471 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1472 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1473 for (uint32_t j = 0; j < entry->array_count; j++) {
1474 const VkBufferView *_bview =
1475 data + entry->offset + j * entry->stride;
1476 ANV_FROM_HANDLE(anv_buffer_view, bview, *_bview);
1477
1478 anv_descriptor_set_write_buffer_view(device, set,
1479 entry->type,
1480 bview,
1481 entry->binding,
1482 entry->array_element + j);
1483 }
1484 break;
1485
1486 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1487 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1488 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1489 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1490 for (uint32_t j = 0; j < entry->array_count; j++) {
1491 const VkDescriptorBufferInfo *info =
1492 data + entry->offset + j * entry->stride;
1493 ANV_FROM_HANDLE(anv_buffer, buffer, info->buffer);
1494
1495 anv_descriptor_set_write_buffer(device, set,
1496 alloc_stream,
1497 entry->type,
1498 buffer,
1499 entry->binding,
1500 entry->array_element + j,
1501 info->offset, info->range);
1502 }
1503 break;
1504
1505 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
1506 anv_descriptor_set_write_inline_uniform_data(device, set,
1507 entry->binding,
1508 data + entry->offset,
1509 entry->array_element,
1510 entry->array_count);
1511 break;
1512
1513 default:
1514 break;
1515 }
1516 }
1517 }
1518
1519 VkResult anv_CreateDescriptorUpdateTemplate(
1520 VkDevice _device,
1521 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
1522 const VkAllocationCallbacks* pAllocator,
1523 VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate)
1524 {
1525 ANV_FROM_HANDLE(anv_device, device, _device);
1526 struct anv_descriptor_update_template *template;
1527
1528 size_t size = sizeof(*template) +
1529 pCreateInfo->descriptorUpdateEntryCount * sizeof(template->entries[0]);
1530 template = vk_alloc2(&device->alloc, pAllocator, size, 8,
1531 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1532 if (template == NULL)
1533 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1534
1535 template->bind_point = pCreateInfo->pipelineBindPoint;
1536
1537 if (pCreateInfo->templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET)
1538 template->set = pCreateInfo->set;
1539
1540 template->entry_count = pCreateInfo->descriptorUpdateEntryCount;
1541 for (uint32_t i = 0; i < template->entry_count; i++) {
1542 const VkDescriptorUpdateTemplateEntry *pEntry =
1543 &pCreateInfo->pDescriptorUpdateEntries[i];
1544
1545 template->entries[i] = (struct anv_descriptor_template_entry) {
1546 .type = pEntry->descriptorType,
1547 .binding = pEntry->dstBinding,
1548 .array_element = pEntry->dstArrayElement,
1549 .array_count = pEntry->descriptorCount,
1550 .offset = pEntry->offset,
1551 .stride = pEntry->stride,
1552 };
1553 }
1554
1555 *pDescriptorUpdateTemplate =
1556 anv_descriptor_update_template_to_handle(template);
1557
1558 return VK_SUCCESS;
1559 }
1560
1561 void anv_DestroyDescriptorUpdateTemplate(
1562 VkDevice _device,
1563 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1564 const VkAllocationCallbacks* pAllocator)
1565 {
1566 ANV_FROM_HANDLE(anv_device, device, _device);
1567 ANV_FROM_HANDLE(anv_descriptor_update_template, template,
1568 descriptorUpdateTemplate);
1569
1570 vk_free2(&device->alloc, pAllocator, template);
1571 }
1572
1573 void anv_UpdateDescriptorSetWithTemplate(
1574 VkDevice _device,
1575 VkDescriptorSet descriptorSet,
1576 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1577 const void* pData)
1578 {
1579 ANV_FROM_HANDLE(anv_device, device, _device);
1580 ANV_FROM_HANDLE(anv_descriptor_set, set, descriptorSet);
1581 ANV_FROM_HANDLE(anv_descriptor_update_template, template,
1582 descriptorUpdateTemplate);
1583
1584 anv_descriptor_set_write_template(device, set, NULL, template, pData);
1585 }