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