anv: Ignore descriptor binding flags if bindingCount == 0
[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 the pointer to the binding 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 *)binding;
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 const VkDescriptorSetLayoutBinding *binding =
368 (void *)set_layout->binding[b].immutable_samplers;
369
370 if (binding == NULL)
371 continue;
372
373 /* We temporarily stashed the pointer to the binding in the
374 * immutable_samplers pointer. Now that we've pulled it back out
375 * again, we reset immutable_samplers to NULL.
376 */
377 set_layout->binding[b].immutable_samplers = NULL;
378
379 if (binding->descriptorCount == 0)
380 continue;
381
382 #ifndef NDEBUG
383 set_layout->binding[b].type = binding->descriptorType;
384 #endif
385
386 if (binding_flags_info && binding_flags_info->bindingCount > 0) {
387 assert(binding_flags_info->bindingCount == pCreateInfo->bindingCount);
388 uint32_t binding_strct_idx = binding - pCreateInfo->pBindings;
389 assert(binding_strct_idx < binding_flags_info->bindingCount);
390 set_layout->binding[b].flags =
391 binding_flags_info->pBindingFlags[binding_strct_idx];
392 }
393
394 set_layout->binding[b].data =
395 anv_descriptor_data_for_type(&device->instance->physicalDevice,
396 binding->descriptorType);
397 set_layout->binding[b].array_size = binding->descriptorCount;
398 set_layout->binding[b].descriptor_index = set_layout->size;
399 set_layout->size += binding->descriptorCount;
400
401 if (set_layout->binding[b].data & ANV_DESCRIPTOR_BUFFER_VIEW) {
402 set_layout->binding[b].buffer_view_index = buffer_view_count;
403 buffer_view_count += binding->descriptorCount;
404 }
405
406 switch (binding->descriptorType) {
407 case VK_DESCRIPTOR_TYPE_SAMPLER:
408 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
409 set_layout->binding[b].max_plane_count = 1;
410 if (binding->pImmutableSamplers) {
411 set_layout->binding[b].immutable_samplers = samplers;
412 samplers += binding->descriptorCount;
413
414 for (uint32_t i = 0; i < binding->descriptorCount; i++) {
415 ANV_FROM_HANDLE(anv_sampler, sampler,
416 binding->pImmutableSamplers[i]);
417
418 set_layout->binding[b].immutable_samplers[i] = sampler;
419 if (set_layout->binding[b].max_plane_count < sampler->n_planes)
420 set_layout->binding[b].max_plane_count = sampler->n_planes;
421 }
422 }
423 break;
424
425 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
426 set_layout->binding[b].max_plane_count = 1;
427 break;
428
429 default:
430 break;
431 }
432
433 switch (binding->descriptorType) {
434 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
435 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
436 set_layout->binding[b].dynamic_offset_index = dynamic_offset_count;
437 dynamic_offset_count += binding->descriptorCount;
438 break;
439
440 default:
441 break;
442 }
443
444 if (binding->descriptorType ==
445 VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
446 /* Inline uniform blocks are specified to use the descriptor array
447 * size as the size in bytes of the block.
448 */
449 descriptor_buffer_size = align_u32(descriptor_buffer_size, 32);
450 set_layout->binding[b].descriptor_offset = descriptor_buffer_size;
451 descriptor_buffer_size += binding->descriptorCount;
452 } else {
453 set_layout->binding[b].descriptor_offset = descriptor_buffer_size;
454 descriptor_buffer_size += anv_descriptor_size(&set_layout->binding[b]) *
455 binding->descriptorCount;
456 }
457
458 set_layout->shader_stages |= binding->stageFlags;
459 }
460
461 set_layout->buffer_view_count = buffer_view_count;
462 set_layout->dynamic_offset_count = dynamic_offset_count;
463 set_layout->descriptor_buffer_size = descriptor_buffer_size;
464
465 *pSetLayout = anv_descriptor_set_layout_to_handle(set_layout);
466
467 return VK_SUCCESS;
468 }
469
470 void anv_DestroyDescriptorSetLayout(
471 VkDevice _device,
472 VkDescriptorSetLayout _set_layout,
473 const VkAllocationCallbacks* pAllocator)
474 {
475 ANV_FROM_HANDLE(anv_device, device, _device);
476 ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout, _set_layout);
477
478 if (!set_layout)
479 return;
480
481 anv_descriptor_set_layout_unref(device, set_layout);
482 }
483
484 #define SHA1_UPDATE_VALUE(ctx, x) _mesa_sha1_update(ctx, &(x), sizeof(x));
485
486 static void
487 sha1_update_immutable_sampler(struct mesa_sha1 *ctx,
488 const struct anv_sampler *sampler)
489 {
490 if (!sampler->conversion)
491 return;
492
493 /* The only thing that affects the shader is ycbcr conversion */
494 _mesa_sha1_update(ctx, sampler->conversion,
495 sizeof(*sampler->conversion));
496 }
497
498 static void
499 sha1_update_descriptor_set_binding_layout(struct mesa_sha1 *ctx,
500 const struct anv_descriptor_set_binding_layout *layout)
501 {
502 SHA1_UPDATE_VALUE(ctx, layout->flags);
503 SHA1_UPDATE_VALUE(ctx, layout->data);
504 SHA1_UPDATE_VALUE(ctx, layout->max_plane_count);
505 SHA1_UPDATE_VALUE(ctx, layout->array_size);
506 SHA1_UPDATE_VALUE(ctx, layout->descriptor_index);
507 SHA1_UPDATE_VALUE(ctx, layout->dynamic_offset_index);
508 SHA1_UPDATE_VALUE(ctx, layout->buffer_view_index);
509 SHA1_UPDATE_VALUE(ctx, layout->descriptor_offset);
510
511 if (layout->immutable_samplers) {
512 for (uint16_t i = 0; i < layout->array_size; i++)
513 sha1_update_immutable_sampler(ctx, layout->immutable_samplers[i]);
514 }
515 }
516
517 static void
518 sha1_update_descriptor_set_layout(struct mesa_sha1 *ctx,
519 const struct anv_descriptor_set_layout *layout)
520 {
521 SHA1_UPDATE_VALUE(ctx, layout->binding_count);
522 SHA1_UPDATE_VALUE(ctx, layout->size);
523 SHA1_UPDATE_VALUE(ctx, layout->shader_stages);
524 SHA1_UPDATE_VALUE(ctx, layout->buffer_view_count);
525 SHA1_UPDATE_VALUE(ctx, layout->dynamic_offset_count);
526 SHA1_UPDATE_VALUE(ctx, layout->descriptor_buffer_size);
527
528 for (uint16_t i = 0; i < layout->binding_count; i++)
529 sha1_update_descriptor_set_binding_layout(ctx, &layout->binding[i]);
530 }
531
532 /*
533 * Pipeline layouts. These have nothing to do with the pipeline. They are
534 * just multiple descriptor set layouts pasted together
535 */
536
537 VkResult anv_CreatePipelineLayout(
538 VkDevice _device,
539 const VkPipelineLayoutCreateInfo* pCreateInfo,
540 const VkAllocationCallbacks* pAllocator,
541 VkPipelineLayout* pPipelineLayout)
542 {
543 ANV_FROM_HANDLE(anv_device, device, _device);
544 struct anv_pipeline_layout *layout;
545
546 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
547
548 layout = vk_alloc2(&device->alloc, pAllocator, sizeof(*layout), 8,
549 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
550 if (layout == NULL)
551 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
552
553 layout->num_sets = pCreateInfo->setLayoutCount;
554
555 unsigned dynamic_offset_count = 0;
556
557 for (uint32_t set = 0; set < pCreateInfo->setLayoutCount; set++) {
558 ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout,
559 pCreateInfo->pSetLayouts[set]);
560 layout->set[set].layout = set_layout;
561 anv_descriptor_set_layout_ref(set_layout);
562
563 layout->set[set].dynamic_offset_start = dynamic_offset_count;
564 for (uint32_t b = 0; b < set_layout->binding_count; b++) {
565 if (set_layout->binding[b].dynamic_offset_index < 0)
566 continue;
567
568 dynamic_offset_count += set_layout->binding[b].array_size;
569 }
570 }
571
572 struct mesa_sha1 ctx;
573 _mesa_sha1_init(&ctx);
574 for (unsigned s = 0; s < layout->num_sets; s++) {
575 sha1_update_descriptor_set_layout(&ctx, layout->set[s].layout);
576 _mesa_sha1_update(&ctx, &layout->set[s].dynamic_offset_start,
577 sizeof(layout->set[s].dynamic_offset_start));
578 }
579 _mesa_sha1_update(&ctx, &layout->num_sets, sizeof(layout->num_sets));
580 _mesa_sha1_final(&ctx, layout->sha1);
581
582 *pPipelineLayout = anv_pipeline_layout_to_handle(layout);
583
584 return VK_SUCCESS;
585 }
586
587 void anv_DestroyPipelineLayout(
588 VkDevice _device,
589 VkPipelineLayout _pipelineLayout,
590 const VkAllocationCallbacks* pAllocator)
591 {
592 ANV_FROM_HANDLE(anv_device, device, _device);
593 ANV_FROM_HANDLE(anv_pipeline_layout, pipeline_layout, _pipelineLayout);
594
595 if (!pipeline_layout)
596 return;
597
598 for (uint32_t i = 0; i < pipeline_layout->num_sets; i++)
599 anv_descriptor_set_layout_unref(device, pipeline_layout->set[i].layout);
600
601 vk_free2(&device->alloc, pAllocator, pipeline_layout);
602 }
603
604 /*
605 * Descriptor pools.
606 *
607 * These are implemented using a big pool of memory and a free-list for the
608 * host memory allocations and a state_stream and a free list for the buffer
609 * view surface state. The spec allows us to fail to allocate due to
610 * fragmentation in all cases but two: 1) after pool reset, allocating up
611 * until the pool size with no freeing must succeed and 2) allocating and
612 * freeing only descriptor sets with the same layout. Case 1) is easy enogh,
613 * and the free lists lets us recycle blocks for case 2).
614 */
615
616 /* The vma heap reserves 0 to mean NULL; we have to offset by some ammount to
617 * ensure we can allocate the entire BO without hitting zero. The actual
618 * amount doesn't matter.
619 */
620 #define POOL_HEAP_OFFSET 64
621
622 #define EMPTY 1
623
624 VkResult anv_CreateDescriptorPool(
625 VkDevice _device,
626 const VkDescriptorPoolCreateInfo* pCreateInfo,
627 const VkAllocationCallbacks* pAllocator,
628 VkDescriptorPool* pDescriptorPool)
629 {
630 ANV_FROM_HANDLE(anv_device, device, _device);
631 struct anv_descriptor_pool *pool;
632
633 const VkDescriptorPoolInlineUniformBlockCreateInfoEXT *inline_info =
634 vk_find_struct_const(pCreateInfo->pNext,
635 DESCRIPTOR_POOL_INLINE_UNIFORM_BLOCK_CREATE_INFO_EXT);
636
637 uint32_t descriptor_count = 0;
638 uint32_t buffer_view_count = 0;
639 uint32_t descriptor_bo_size = 0;
640 for (uint32_t i = 0; i < pCreateInfo->poolSizeCount; i++) {
641 enum anv_descriptor_data desc_data =
642 anv_descriptor_data_for_type(&device->instance->physicalDevice,
643 pCreateInfo->pPoolSizes[i].type);
644
645 if (desc_data & ANV_DESCRIPTOR_BUFFER_VIEW)
646 buffer_view_count += pCreateInfo->pPoolSizes[i].descriptorCount;
647
648 unsigned desc_data_size = anv_descriptor_data_size(desc_data) *
649 pCreateInfo->pPoolSizes[i].descriptorCount;
650
651 /* Combined image sampler descriptors can take up to 3 slots if they
652 * hold a YCbCr image.
653 */
654 if (pCreateInfo->pPoolSizes[i].type ==
655 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
656 desc_data_size *= 3;
657
658 if (pCreateInfo->pPoolSizes[i].type ==
659 VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
660 /* Inline uniform blocks are specified to use the descriptor array
661 * size as the size in bytes of the block.
662 */
663 assert(inline_info);
664 desc_data_size += pCreateInfo->pPoolSizes[i].descriptorCount;
665 }
666
667 descriptor_bo_size += desc_data_size;
668
669 descriptor_count += pCreateInfo->pPoolSizes[i].descriptorCount;
670 }
671 /* We have to align descriptor buffer allocations to 32B so that we can
672 * push descriptor buffers. This means that each descriptor buffer
673 * allocated may burn up to 32B of extra space to get the right alignment.
674 * (Technically, it's at most 28B because we're always going to start at
675 * least 4B aligned but we're being conservative here.) Allocate enough
676 * extra space that we can chop it into maxSets pieces and align each one
677 * of them to 32B.
678 */
679 descriptor_bo_size += 32 * pCreateInfo->maxSets;
680 descriptor_bo_size = ALIGN(descriptor_bo_size, 4096);
681 /* We align inline uniform blocks to 32B */
682 if (inline_info)
683 descriptor_bo_size += 32 * inline_info->maxInlineUniformBlockBindings;
684
685 const size_t pool_size =
686 pCreateInfo->maxSets * sizeof(struct anv_descriptor_set) +
687 descriptor_count * sizeof(struct anv_descriptor) +
688 buffer_view_count * sizeof(struct anv_buffer_view);
689 const size_t total_size = sizeof(*pool) + pool_size;
690
691 pool = vk_alloc2(&device->alloc, pAllocator, total_size, 8,
692 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
693 if (!pool)
694 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
695
696 pool->size = pool_size;
697 pool->next = 0;
698 pool->free_list = EMPTY;
699
700 if (descriptor_bo_size > 0) {
701 VkResult result = anv_bo_init_new(&pool->bo, device, descriptor_bo_size);
702 if (result != VK_SUCCESS) {
703 vk_free2(&device->alloc, pAllocator, pool);
704 return result;
705 }
706
707 anv_gem_set_caching(device, pool->bo.gem_handle, I915_CACHING_CACHED);
708
709 pool->bo.map = anv_gem_mmap(device, pool->bo.gem_handle, 0,
710 descriptor_bo_size, 0);
711 if (pool->bo.map == NULL) {
712 anv_gem_close(device, pool->bo.gem_handle);
713 vk_free2(&device->alloc, pAllocator, pool);
714 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
715 }
716
717 if (device->instance->physicalDevice.use_softpin) {
718 pool->bo.flags |= EXEC_OBJECT_PINNED;
719 anv_vma_alloc(device, &pool->bo);
720 }
721
722 util_vma_heap_init(&pool->bo_heap, POOL_HEAP_OFFSET, descriptor_bo_size);
723 } else {
724 pool->bo.size = 0;
725 }
726
727 anv_state_stream_init(&pool->surface_state_stream,
728 &device->surface_state_pool, 4096);
729 pool->surface_state_free_list = NULL;
730
731 list_inithead(&pool->desc_sets);
732
733 *pDescriptorPool = anv_descriptor_pool_to_handle(pool);
734
735 return VK_SUCCESS;
736 }
737
738 void anv_DestroyDescriptorPool(
739 VkDevice _device,
740 VkDescriptorPool _pool,
741 const VkAllocationCallbacks* pAllocator)
742 {
743 ANV_FROM_HANDLE(anv_device, device, _device);
744 ANV_FROM_HANDLE(anv_descriptor_pool, pool, _pool);
745
746 if (!pool)
747 return;
748
749 if (pool->bo.size) {
750 anv_gem_munmap(pool->bo.map, pool->bo.size);
751 anv_vma_free(device, &pool->bo);
752 anv_gem_close(device, pool->bo.gem_handle);
753 }
754 anv_state_stream_finish(&pool->surface_state_stream);
755
756 list_for_each_entry_safe(struct anv_descriptor_set, set,
757 &pool->desc_sets, pool_link) {
758 anv_descriptor_set_destroy(device, pool, set);
759 }
760
761 util_vma_heap_finish(&pool->bo_heap);
762
763 vk_free2(&device->alloc, pAllocator, pool);
764 }
765
766 VkResult anv_ResetDescriptorPool(
767 VkDevice _device,
768 VkDescriptorPool descriptorPool,
769 VkDescriptorPoolResetFlags flags)
770 {
771 ANV_FROM_HANDLE(anv_device, device, _device);
772 ANV_FROM_HANDLE(anv_descriptor_pool, pool, descriptorPool);
773
774 list_for_each_entry_safe(struct anv_descriptor_set, set,
775 &pool->desc_sets, pool_link) {
776 anv_descriptor_set_destroy(device, pool, set);
777 }
778
779 pool->next = 0;
780 pool->free_list = EMPTY;
781
782 if (pool->bo.size) {
783 util_vma_heap_finish(&pool->bo_heap);
784 util_vma_heap_init(&pool->bo_heap, POOL_HEAP_OFFSET, pool->bo.size);
785 }
786
787 anv_state_stream_finish(&pool->surface_state_stream);
788 anv_state_stream_init(&pool->surface_state_stream,
789 &device->surface_state_pool, 4096);
790 pool->surface_state_free_list = NULL;
791
792 return VK_SUCCESS;
793 }
794
795 struct pool_free_list_entry {
796 uint32_t next;
797 uint32_t size;
798 };
799
800 static VkResult
801 anv_descriptor_pool_alloc_set(struct anv_descriptor_pool *pool,
802 uint32_t size,
803 struct anv_descriptor_set **set)
804 {
805 if (size <= pool->size - pool->next) {
806 *set = (struct anv_descriptor_set *) (pool->data + pool->next);
807 pool->next += size;
808 return VK_SUCCESS;
809 } else {
810 struct pool_free_list_entry *entry;
811 uint32_t *link = &pool->free_list;
812 for (uint32_t f = pool->free_list; f != EMPTY; f = entry->next) {
813 entry = (struct pool_free_list_entry *) (pool->data + f);
814 if (size <= entry->size) {
815 *link = entry->next;
816 *set = (struct anv_descriptor_set *) entry;
817 return VK_SUCCESS;
818 }
819 link = &entry->next;
820 }
821
822 if (pool->free_list != EMPTY) {
823 return vk_error(VK_ERROR_FRAGMENTED_POOL);
824 } else {
825 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY);
826 }
827 }
828 }
829
830 static void
831 anv_descriptor_pool_free_set(struct anv_descriptor_pool *pool,
832 struct anv_descriptor_set *set)
833 {
834 /* Put the descriptor set allocation back on the free list. */
835 const uint32_t index = (char *) set - pool->data;
836 if (index + set->size == pool->next) {
837 pool->next = index;
838 } else {
839 struct pool_free_list_entry *entry = (struct pool_free_list_entry *) set;
840 entry->next = pool->free_list;
841 entry->size = set->size;
842 pool->free_list = (char *) entry - pool->data;
843 }
844
845 list_del(&set->pool_link);
846 }
847
848 struct surface_state_free_list_entry {
849 void *next;
850 struct anv_state state;
851 };
852
853 static struct anv_state
854 anv_descriptor_pool_alloc_state(struct anv_descriptor_pool *pool)
855 {
856 struct surface_state_free_list_entry *entry =
857 pool->surface_state_free_list;
858
859 if (entry) {
860 struct anv_state state = entry->state;
861 pool->surface_state_free_list = entry->next;
862 assert(state.alloc_size == 64);
863 return state;
864 } else {
865 return anv_state_stream_alloc(&pool->surface_state_stream, 64, 64);
866 }
867 }
868
869 static void
870 anv_descriptor_pool_free_state(struct anv_descriptor_pool *pool,
871 struct anv_state state)
872 {
873 /* Put the buffer view surface state back on the free list. */
874 struct surface_state_free_list_entry *entry = state.map;
875 entry->next = pool->surface_state_free_list;
876 entry->state = state;
877 pool->surface_state_free_list = entry;
878 }
879
880 size_t
881 anv_descriptor_set_layout_size(const struct anv_descriptor_set_layout *layout)
882 {
883 return
884 sizeof(struct anv_descriptor_set) +
885 layout->size * sizeof(struct anv_descriptor) +
886 layout->buffer_view_count * sizeof(struct anv_buffer_view);
887 }
888
889 VkResult
890 anv_descriptor_set_create(struct anv_device *device,
891 struct anv_descriptor_pool *pool,
892 struct anv_descriptor_set_layout *layout,
893 struct anv_descriptor_set **out_set)
894 {
895 struct anv_descriptor_set *set;
896 const size_t size = anv_descriptor_set_layout_size(layout);
897
898 VkResult result = anv_descriptor_pool_alloc_set(pool, size, &set);
899 if (result != VK_SUCCESS)
900 return result;
901
902 if (layout->descriptor_buffer_size) {
903 /* Align the size to 32 so that alignment gaps don't cause extra holes
904 * in the heap which can lead to bad performance.
905 */
906 uint64_t pool_vma_offset =
907 util_vma_heap_alloc(&pool->bo_heap,
908 ALIGN(layout->descriptor_buffer_size, 32), 32);
909 if (pool_vma_offset == 0) {
910 anv_descriptor_pool_free_set(pool, set);
911 return vk_error(VK_ERROR_FRAGMENTED_POOL);
912 }
913 assert(pool_vma_offset >= POOL_HEAP_OFFSET &&
914 pool_vma_offset - POOL_HEAP_OFFSET <= INT32_MAX);
915 set->desc_mem.offset = pool_vma_offset - POOL_HEAP_OFFSET;
916 set->desc_mem.alloc_size = layout->descriptor_buffer_size;
917 set->desc_mem.map = pool->bo.map + set->desc_mem.offset;
918
919 set->desc_surface_state = anv_descriptor_pool_alloc_state(pool);
920 anv_fill_buffer_surface_state(device, set->desc_surface_state,
921 ISL_FORMAT_R32G32B32A32_FLOAT,
922 (struct anv_address) {
923 .bo = &pool->bo,
924 .offset = set->desc_mem.offset,
925 },
926 layout->descriptor_buffer_size, 1);
927 } else {
928 set->desc_mem = ANV_STATE_NULL;
929 set->desc_surface_state = ANV_STATE_NULL;
930 }
931
932 set->pool = pool;
933 set->layout = layout;
934 anv_descriptor_set_layout_ref(layout);
935
936 set->size = size;
937 set->buffer_views =
938 (struct anv_buffer_view *) &set->descriptors[layout->size];
939 set->buffer_view_count = layout->buffer_view_count;
940
941 /* By defining the descriptors to be zero now, we can later verify that
942 * a descriptor has not been populated with user data.
943 */
944 memset(set->descriptors, 0, sizeof(struct anv_descriptor) * layout->size);
945
946 /* Go through and fill out immutable samplers if we have any */
947 struct anv_descriptor *desc = set->descriptors;
948 for (uint32_t b = 0; b < layout->binding_count; b++) {
949 if (layout->binding[b].immutable_samplers) {
950 for (uint32_t i = 0; i < layout->binding[b].array_size; i++) {
951 /* The type will get changed to COMBINED_IMAGE_SAMPLER in
952 * UpdateDescriptorSets if needed. However, if the descriptor
953 * set has an immutable sampler, UpdateDescriptorSets may never
954 * touch it, so we need to make sure it's 100% valid now.
955 *
956 * We don't need to actually provide a sampler because the helper
957 * will always write in the immutable sampler regardless of what
958 * is in the sampler parameter.
959 */
960 struct VkDescriptorImageInfo info = { };
961 anv_descriptor_set_write_image_view(device, set, &info,
962 VK_DESCRIPTOR_TYPE_SAMPLER,
963 b, i);
964 }
965 }
966 desc += layout->binding[b].array_size;
967 }
968
969 /* Allocate surface state for the buffer views. */
970 for (uint32_t b = 0; b < layout->buffer_view_count; b++) {
971 set->buffer_views[b].surface_state =
972 anv_descriptor_pool_alloc_state(pool);
973 }
974
975 *out_set = set;
976
977 return VK_SUCCESS;
978 }
979
980 void
981 anv_descriptor_set_destroy(struct anv_device *device,
982 struct anv_descriptor_pool *pool,
983 struct anv_descriptor_set *set)
984 {
985 anv_descriptor_set_layout_unref(device, set->layout);
986
987 if (set->desc_mem.alloc_size) {
988 util_vma_heap_free(&pool->bo_heap,
989 (uint64_t)set->desc_mem.offset + POOL_HEAP_OFFSET,
990 set->desc_mem.alloc_size);
991 anv_descriptor_pool_free_state(pool, set->desc_surface_state);
992 }
993
994 for (uint32_t b = 0; b < set->buffer_view_count; b++)
995 anv_descriptor_pool_free_state(pool, set->buffer_views[b].surface_state);
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 list_addtail(&set->pool_link, &pool->desc_sets);
1021
1022 pDescriptorSets[i] = anv_descriptor_set_to_handle(set);
1023 }
1024
1025 if (result != VK_SUCCESS)
1026 anv_FreeDescriptorSets(_device, pAllocateInfo->descriptorPool,
1027 i, pDescriptorSets);
1028
1029 return result;
1030 }
1031
1032 VkResult anv_FreeDescriptorSets(
1033 VkDevice _device,
1034 VkDescriptorPool descriptorPool,
1035 uint32_t count,
1036 const VkDescriptorSet* pDescriptorSets)
1037 {
1038 ANV_FROM_HANDLE(anv_device, device, _device);
1039 ANV_FROM_HANDLE(anv_descriptor_pool, pool, descriptorPool);
1040
1041 for (uint32_t i = 0; i < count; i++) {
1042 ANV_FROM_HANDLE(anv_descriptor_set, set, pDescriptorSets[i]);
1043
1044 if (!set)
1045 continue;
1046
1047 anv_descriptor_set_destroy(device, pool, set);
1048 }
1049
1050 return VK_SUCCESS;
1051 }
1052
1053 static void
1054 anv_descriptor_set_write_image_param(uint32_t *param_desc_map,
1055 const struct brw_image_param *param)
1056 {
1057 #define WRITE_PARAM_FIELD(field, FIELD) \
1058 for (unsigned i = 0; i < ARRAY_SIZE(param->field); i++) \
1059 param_desc_map[BRW_IMAGE_PARAM_##FIELD##_OFFSET + i] = param->field[i]
1060
1061 WRITE_PARAM_FIELD(offset, OFFSET);
1062 WRITE_PARAM_FIELD(size, SIZE);
1063 WRITE_PARAM_FIELD(stride, STRIDE);
1064 WRITE_PARAM_FIELD(tiling, TILING);
1065 WRITE_PARAM_FIELD(swizzling, SWIZZLING);
1066 WRITE_PARAM_FIELD(size, SIZE);
1067
1068 #undef WRITE_PARAM_FIELD
1069 }
1070
1071 static uint32_t
1072 anv_surface_state_to_handle(struct anv_state state)
1073 {
1074 /* Bits 31:12 of the bindless surface offset in the extended message
1075 * descriptor is bits 25:6 of the byte-based address.
1076 */
1077 assert(state.offset >= 0);
1078 uint32_t offset = state.offset;
1079 assert((offset & 0x3f) == 0 && offset < (1 << 26));
1080 return offset << 6;
1081 }
1082
1083 void
1084 anv_descriptor_set_write_image_view(struct anv_device *device,
1085 struct anv_descriptor_set *set,
1086 const VkDescriptorImageInfo * const info,
1087 VkDescriptorType type,
1088 uint32_t binding,
1089 uint32_t element)
1090 {
1091 const struct anv_descriptor_set_binding_layout *bind_layout =
1092 &set->layout->binding[binding];
1093 struct anv_descriptor *desc =
1094 &set->descriptors[bind_layout->descriptor_index + element];
1095 struct anv_image_view *image_view = NULL;
1096 struct anv_sampler *sampler = NULL;
1097
1098 /* We get called with just VK_DESCRIPTOR_TYPE_SAMPLER as part of descriptor
1099 * set initialization to set the bindless samplers.
1100 */
1101 assert(type == bind_layout->type ||
1102 type == VK_DESCRIPTOR_TYPE_SAMPLER);
1103
1104 switch (type) {
1105 case VK_DESCRIPTOR_TYPE_SAMPLER:
1106 sampler = anv_sampler_from_handle(info->sampler);
1107 break;
1108
1109 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1110 image_view = anv_image_view_from_handle(info->imageView);
1111 sampler = anv_sampler_from_handle(info->sampler);
1112 break;
1113
1114 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1115 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1116 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1117 image_view = anv_image_view_from_handle(info->imageView);
1118 break;
1119
1120 default:
1121 unreachable("invalid descriptor type");
1122 }
1123
1124 /* If this descriptor has an immutable sampler, we don't want to stomp on
1125 * it.
1126 */
1127 sampler = bind_layout->immutable_samplers ?
1128 bind_layout->immutable_samplers[element] :
1129 sampler;
1130
1131 *desc = (struct anv_descriptor) {
1132 .type = type,
1133 .layout = info->imageLayout,
1134 .image_view = image_view,
1135 .sampler = sampler,
1136 };
1137
1138 void *desc_map = set->desc_mem.map + bind_layout->descriptor_offset +
1139 element * anv_descriptor_size(bind_layout);
1140
1141 if (bind_layout->data & ANV_DESCRIPTOR_SAMPLED_IMAGE) {
1142 struct anv_sampled_image_descriptor desc_data[3];
1143 memset(desc_data, 0, sizeof(desc_data));
1144
1145 if (image_view) {
1146 for (unsigned p = 0; p < image_view->n_planes; p++) {
1147 struct anv_surface_state sstate =
1148 (desc->layout == VK_IMAGE_LAYOUT_GENERAL) ?
1149 image_view->planes[p].general_sampler_surface_state :
1150 image_view->planes[p].optimal_sampler_surface_state;
1151 desc_data[p].image = anv_surface_state_to_handle(sstate.state);
1152 }
1153 }
1154
1155 if (sampler) {
1156 for (unsigned p = 0; p < sampler->n_planes; p++)
1157 desc_data[p].sampler = sampler->bindless_state.offset + p * 32;
1158 }
1159
1160 /* We may have max_plane_count < 0 if this isn't a sampled image but it
1161 * can be no more than the size of our array of handles.
1162 */
1163 assert(bind_layout->max_plane_count <= ARRAY_SIZE(desc_data));
1164 memcpy(desc_map, desc_data,
1165 MAX2(1, bind_layout->max_plane_count) * sizeof(desc_data[0]));
1166 }
1167
1168 if (bind_layout->data & ANV_DESCRIPTOR_STORAGE_IMAGE) {
1169 assert(!(bind_layout->data & ANV_DESCRIPTOR_IMAGE_PARAM));
1170 assert(image_view->n_planes == 1);
1171 struct anv_storage_image_descriptor desc_data = {
1172 .read_write = anv_surface_state_to_handle(
1173 image_view->planes[0].storage_surface_state.state),
1174 .write_only = anv_surface_state_to_handle(
1175 image_view->planes[0].writeonly_storage_surface_state.state),
1176 };
1177 memcpy(desc_map, &desc_data, sizeof(desc_data));
1178 }
1179
1180 if (bind_layout->data & ANV_DESCRIPTOR_IMAGE_PARAM) {
1181 /* Storage images can only ever have one plane */
1182 assert(image_view->n_planes == 1);
1183 const struct brw_image_param *image_param =
1184 &image_view->planes[0].storage_image_param;
1185
1186 anv_descriptor_set_write_image_param(desc_map, image_param);
1187 }
1188 }
1189
1190 void
1191 anv_descriptor_set_write_buffer_view(struct anv_device *device,
1192 struct anv_descriptor_set *set,
1193 VkDescriptorType type,
1194 struct anv_buffer_view *buffer_view,
1195 uint32_t binding,
1196 uint32_t element)
1197 {
1198 const struct anv_descriptor_set_binding_layout *bind_layout =
1199 &set->layout->binding[binding];
1200 struct anv_descriptor *desc =
1201 &set->descriptors[bind_layout->descriptor_index + element];
1202
1203 assert(type == bind_layout->type);
1204
1205 *desc = (struct anv_descriptor) {
1206 .type = type,
1207 .buffer_view = buffer_view,
1208 };
1209
1210 void *desc_map = set->desc_mem.map + bind_layout->descriptor_offset +
1211 element * anv_descriptor_size(bind_layout);
1212
1213 if (bind_layout->data & ANV_DESCRIPTOR_SAMPLED_IMAGE) {
1214 struct anv_sampled_image_descriptor desc_data = {
1215 .image = anv_surface_state_to_handle(buffer_view->surface_state),
1216 };
1217 memcpy(desc_map, &desc_data, sizeof(desc_data));
1218 }
1219
1220 if (bind_layout->data & ANV_DESCRIPTOR_STORAGE_IMAGE) {
1221 assert(!(bind_layout->data & ANV_DESCRIPTOR_IMAGE_PARAM));
1222 struct anv_storage_image_descriptor desc_data = {
1223 .read_write = anv_surface_state_to_handle(
1224 buffer_view->storage_surface_state),
1225 .write_only = anv_surface_state_to_handle(
1226 buffer_view->writeonly_storage_surface_state),
1227 };
1228 memcpy(desc_map, &desc_data, sizeof(desc_data));
1229 }
1230
1231 if (bind_layout->data & ANV_DESCRIPTOR_IMAGE_PARAM) {
1232 anv_descriptor_set_write_image_param(desc_map,
1233 &buffer_view->storage_image_param);
1234 }
1235 }
1236
1237 void
1238 anv_descriptor_set_write_buffer(struct anv_device *device,
1239 struct anv_descriptor_set *set,
1240 struct anv_state_stream *alloc_stream,
1241 VkDescriptorType type,
1242 struct anv_buffer *buffer,
1243 uint32_t binding,
1244 uint32_t element,
1245 VkDeviceSize offset,
1246 VkDeviceSize range)
1247 {
1248 const struct anv_descriptor_set_binding_layout *bind_layout =
1249 &set->layout->binding[binding];
1250 struct anv_descriptor *desc =
1251 &set->descriptors[bind_layout->descriptor_index + element];
1252
1253 assert(type == bind_layout->type);
1254
1255 struct anv_address bind_addr = anv_address_add(buffer->address, offset);
1256 uint64_t bind_range = anv_buffer_get_range(buffer, offset, range);
1257
1258 if (type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
1259 type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
1260 *desc = (struct anv_descriptor) {
1261 .type = type,
1262 .buffer = buffer,
1263 .offset = offset,
1264 .range = range,
1265 };
1266 } else {
1267 assert(bind_layout->data & ANV_DESCRIPTOR_BUFFER_VIEW);
1268 struct anv_buffer_view *bview =
1269 &set->buffer_views[bind_layout->buffer_view_index + element];
1270
1271 bview->format = anv_isl_format_for_descriptor_type(type);
1272 bview->range = bind_range;
1273 bview->address = bind_addr;
1274
1275 /* If we're writing descriptors through a push command, we need to
1276 * allocate the surface state from the command buffer. Otherwise it will
1277 * be allocated by the descriptor pool when calling
1278 * vkAllocateDescriptorSets. */
1279 if (alloc_stream)
1280 bview->surface_state = anv_state_stream_alloc(alloc_stream, 64, 64);
1281
1282 anv_fill_buffer_surface_state(device, bview->surface_state,
1283 bview->format, bind_addr, bind_range, 1);
1284
1285 *desc = (struct anv_descriptor) {
1286 .type = type,
1287 .buffer_view = bview,
1288 };
1289 }
1290
1291 void *desc_map = set->desc_mem.map + bind_layout->descriptor_offset +
1292 element * anv_descriptor_size(bind_layout);
1293
1294 if (bind_layout->data & ANV_DESCRIPTOR_ADDRESS_RANGE) {
1295 struct anv_address_range_descriptor desc = {
1296 .address = anv_address_physical(bind_addr),
1297 .range = bind_range,
1298 };
1299 memcpy(desc_map, &desc, sizeof(desc));
1300 }
1301 }
1302
1303 void
1304 anv_descriptor_set_write_inline_uniform_data(struct anv_device *device,
1305 struct anv_descriptor_set *set,
1306 uint32_t binding,
1307 const void *data,
1308 size_t offset,
1309 size_t size)
1310 {
1311 const struct anv_descriptor_set_binding_layout *bind_layout =
1312 &set->layout->binding[binding];
1313
1314 assert(bind_layout->data & ANV_DESCRIPTOR_INLINE_UNIFORM);
1315
1316 void *desc_map = set->desc_mem.map + bind_layout->descriptor_offset;
1317
1318 memcpy(desc_map + offset, data, size);
1319 }
1320
1321 void anv_UpdateDescriptorSets(
1322 VkDevice _device,
1323 uint32_t descriptorWriteCount,
1324 const VkWriteDescriptorSet* pDescriptorWrites,
1325 uint32_t descriptorCopyCount,
1326 const VkCopyDescriptorSet* pDescriptorCopies)
1327 {
1328 ANV_FROM_HANDLE(anv_device, device, _device);
1329
1330 for (uint32_t i = 0; i < descriptorWriteCount; i++) {
1331 const VkWriteDescriptorSet *write = &pDescriptorWrites[i];
1332 ANV_FROM_HANDLE(anv_descriptor_set, set, write->dstSet);
1333
1334 switch (write->descriptorType) {
1335 case VK_DESCRIPTOR_TYPE_SAMPLER:
1336 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1337 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1338 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1339 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1340 for (uint32_t j = 0; j < write->descriptorCount; j++) {
1341 anv_descriptor_set_write_image_view(device, set,
1342 write->pImageInfo + j,
1343 write->descriptorType,
1344 write->dstBinding,
1345 write->dstArrayElement + j);
1346 }
1347 break;
1348
1349 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1350 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1351 for (uint32_t j = 0; j < write->descriptorCount; j++) {
1352 ANV_FROM_HANDLE(anv_buffer_view, bview,
1353 write->pTexelBufferView[j]);
1354
1355 anv_descriptor_set_write_buffer_view(device, set,
1356 write->descriptorType,
1357 bview,
1358 write->dstBinding,
1359 write->dstArrayElement + j);
1360 }
1361 break;
1362
1363 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1364 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1365 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1366 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1367 for (uint32_t j = 0; j < write->descriptorCount; j++) {
1368 assert(write->pBufferInfo[j].buffer);
1369 ANV_FROM_HANDLE(anv_buffer, buffer, write->pBufferInfo[j].buffer);
1370 assert(buffer);
1371
1372 anv_descriptor_set_write_buffer(device, set,
1373 NULL,
1374 write->descriptorType,
1375 buffer,
1376 write->dstBinding,
1377 write->dstArrayElement + j,
1378 write->pBufferInfo[j].offset,
1379 write->pBufferInfo[j].range);
1380 }
1381 break;
1382
1383 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT: {
1384 const VkWriteDescriptorSetInlineUniformBlockEXT *inline_write =
1385 vk_find_struct_const(write->pNext,
1386 WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT);
1387 assert(inline_write->dataSize == write->descriptorCount);
1388 anv_descriptor_set_write_inline_uniform_data(device, set,
1389 write->dstBinding,
1390 inline_write->pData,
1391 write->dstArrayElement,
1392 inline_write->dataSize);
1393 break;
1394 }
1395
1396 default:
1397 break;
1398 }
1399 }
1400
1401 for (uint32_t i = 0; i < descriptorCopyCount; i++) {
1402 const VkCopyDescriptorSet *copy = &pDescriptorCopies[i];
1403 ANV_FROM_HANDLE(anv_descriptor_set, src, copy->srcSet);
1404 ANV_FROM_HANDLE(anv_descriptor_set, dst, copy->dstSet);
1405
1406 const struct anv_descriptor_set_binding_layout *src_layout =
1407 &src->layout->binding[copy->srcBinding];
1408 struct anv_descriptor *src_desc =
1409 &src->descriptors[src_layout->descriptor_index];
1410 src_desc += copy->srcArrayElement;
1411
1412 const struct anv_descriptor_set_binding_layout *dst_layout =
1413 &dst->layout->binding[copy->dstBinding];
1414 struct anv_descriptor *dst_desc =
1415 &dst->descriptors[dst_layout->descriptor_index];
1416 dst_desc += copy->dstArrayElement;
1417
1418 for (uint32_t j = 0; j < copy->descriptorCount; j++)
1419 dst_desc[j] = src_desc[j];
1420
1421 if (src_layout->data & ANV_DESCRIPTOR_INLINE_UNIFORM) {
1422 assert(src_layout->data == ANV_DESCRIPTOR_INLINE_UNIFORM);
1423 memcpy(dst->desc_mem.map + dst_layout->descriptor_offset +
1424 copy->dstArrayElement,
1425 src->desc_mem.map + src_layout->descriptor_offset +
1426 copy->srcArrayElement,
1427 copy->descriptorCount);
1428 } else {
1429 unsigned desc_size = anv_descriptor_size(src_layout);
1430 if (desc_size > 0) {
1431 assert(desc_size == anv_descriptor_size(dst_layout));
1432 memcpy(dst->desc_mem.map + dst_layout->descriptor_offset +
1433 copy->dstArrayElement * desc_size,
1434 src->desc_mem.map + src_layout->descriptor_offset +
1435 copy->srcArrayElement * desc_size,
1436 copy->descriptorCount * desc_size);
1437 }
1438 }
1439 }
1440 }
1441
1442 /*
1443 * Descriptor update templates.
1444 */
1445
1446 void
1447 anv_descriptor_set_write_template(struct anv_device *device,
1448 struct anv_descriptor_set *set,
1449 struct anv_state_stream *alloc_stream,
1450 const struct anv_descriptor_update_template *template,
1451 const void *data)
1452 {
1453 for (uint32_t i = 0; i < template->entry_count; i++) {
1454 const struct anv_descriptor_template_entry *entry =
1455 &template->entries[i];
1456
1457 switch (entry->type) {
1458 case VK_DESCRIPTOR_TYPE_SAMPLER:
1459 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1460 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1461 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1462 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1463 for (uint32_t j = 0; j < entry->array_count; j++) {
1464 const VkDescriptorImageInfo *info =
1465 data + entry->offset + j * entry->stride;
1466 anv_descriptor_set_write_image_view(device, set,
1467 info, entry->type,
1468 entry->binding,
1469 entry->array_element + j);
1470 }
1471 break;
1472
1473 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1474 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1475 for (uint32_t j = 0; j < entry->array_count; j++) {
1476 const VkBufferView *_bview =
1477 data + entry->offset + j * entry->stride;
1478 ANV_FROM_HANDLE(anv_buffer_view, bview, *_bview);
1479
1480 anv_descriptor_set_write_buffer_view(device, set,
1481 entry->type,
1482 bview,
1483 entry->binding,
1484 entry->array_element + j);
1485 }
1486 break;
1487
1488 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1489 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1490 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1491 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1492 for (uint32_t j = 0; j < entry->array_count; j++) {
1493 const VkDescriptorBufferInfo *info =
1494 data + entry->offset + j * entry->stride;
1495 ANV_FROM_HANDLE(anv_buffer, buffer, info->buffer);
1496
1497 anv_descriptor_set_write_buffer(device, set,
1498 alloc_stream,
1499 entry->type,
1500 buffer,
1501 entry->binding,
1502 entry->array_element + j,
1503 info->offset, info->range);
1504 }
1505 break;
1506
1507 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
1508 anv_descriptor_set_write_inline_uniform_data(device, set,
1509 entry->binding,
1510 data + entry->offset,
1511 entry->array_element,
1512 entry->array_count);
1513 break;
1514
1515 default:
1516 break;
1517 }
1518 }
1519 }
1520
1521 VkResult anv_CreateDescriptorUpdateTemplate(
1522 VkDevice _device,
1523 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
1524 const VkAllocationCallbacks* pAllocator,
1525 VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate)
1526 {
1527 ANV_FROM_HANDLE(anv_device, device, _device);
1528 struct anv_descriptor_update_template *template;
1529
1530 size_t size = sizeof(*template) +
1531 pCreateInfo->descriptorUpdateEntryCount * sizeof(template->entries[0]);
1532 template = vk_alloc2(&device->alloc, pAllocator, size, 8,
1533 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1534 if (template == NULL)
1535 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1536
1537 template->bind_point = pCreateInfo->pipelineBindPoint;
1538
1539 if (pCreateInfo->templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET)
1540 template->set = pCreateInfo->set;
1541
1542 template->entry_count = pCreateInfo->descriptorUpdateEntryCount;
1543 for (uint32_t i = 0; i < template->entry_count; i++) {
1544 const VkDescriptorUpdateTemplateEntry *pEntry =
1545 &pCreateInfo->pDescriptorUpdateEntries[i];
1546
1547 template->entries[i] = (struct anv_descriptor_template_entry) {
1548 .type = pEntry->descriptorType,
1549 .binding = pEntry->dstBinding,
1550 .array_element = pEntry->dstArrayElement,
1551 .array_count = pEntry->descriptorCount,
1552 .offset = pEntry->offset,
1553 .stride = pEntry->stride,
1554 };
1555 }
1556
1557 *pDescriptorUpdateTemplate =
1558 anv_descriptor_update_template_to_handle(template);
1559
1560 return VK_SUCCESS;
1561 }
1562
1563 void anv_DestroyDescriptorUpdateTemplate(
1564 VkDevice _device,
1565 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1566 const VkAllocationCallbacks* pAllocator)
1567 {
1568 ANV_FROM_HANDLE(anv_device, device, _device);
1569 ANV_FROM_HANDLE(anv_descriptor_update_template, template,
1570 descriptorUpdateTemplate);
1571
1572 vk_free2(&device->alloc, pAllocator, template);
1573 }
1574
1575 void anv_UpdateDescriptorSetWithTemplate(
1576 VkDevice _device,
1577 VkDescriptorSet descriptorSet,
1578 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1579 const void* pData)
1580 {
1581 ANV_FROM_HANDLE(anv_device, device, _device);
1582 ANV_FROM_HANDLE(anv_descriptor_set, set, descriptorSet);
1583 ANV_FROM_HANDLE(anv_descriptor_update_template, template,
1584 descriptorUpdateTemplate);
1585
1586 anv_descriptor_set_write_template(device, set, NULL, template, pData);
1587 }