anv: fix alignments for uniform buffers
[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 pool->next += size;
854 return VK_SUCCESS;
855 } else {
856 struct pool_free_list_entry *entry;
857 uint32_t *link = &pool->free_list;
858 for (uint32_t f = pool->free_list; f != EMPTY; f = entry->next) {
859 entry = (struct pool_free_list_entry *) (pool->data + f);
860 if (size <= entry->size) {
861 *link = entry->next;
862 *set = (struct anv_descriptor_set *) entry;
863 return VK_SUCCESS;
864 }
865 link = &entry->next;
866 }
867
868 if (pool->free_list != EMPTY) {
869 return vk_error(VK_ERROR_FRAGMENTED_POOL);
870 } else {
871 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY);
872 }
873 }
874 }
875
876 static void
877 anv_descriptor_pool_free_set(struct anv_descriptor_pool *pool,
878 struct anv_descriptor_set *set)
879 {
880 /* Put the descriptor set allocation back on the free list. */
881 const uint32_t index = (char *) set - pool->data;
882 if (index + set->size == pool->next) {
883 pool->next = index;
884 } else {
885 struct pool_free_list_entry *entry = (struct pool_free_list_entry *) set;
886 entry->next = pool->free_list;
887 entry->size = set->size;
888 pool->free_list = (char *) entry - pool->data;
889 }
890 }
891
892 struct surface_state_free_list_entry {
893 void *next;
894 struct anv_state state;
895 };
896
897 static struct anv_state
898 anv_descriptor_pool_alloc_state(struct anv_descriptor_pool *pool)
899 {
900 struct surface_state_free_list_entry *entry =
901 pool->surface_state_free_list;
902
903 if (entry) {
904 struct anv_state state = entry->state;
905 pool->surface_state_free_list = entry->next;
906 assert(state.alloc_size == 64);
907 return state;
908 } else {
909 return anv_state_stream_alloc(&pool->surface_state_stream, 64, 64);
910 }
911 }
912
913 static void
914 anv_descriptor_pool_free_state(struct anv_descriptor_pool *pool,
915 struct anv_state state)
916 {
917 /* Put the buffer view surface state back on the free list. */
918 struct surface_state_free_list_entry *entry = state.map;
919 entry->next = pool->surface_state_free_list;
920 entry->state = state;
921 pool->surface_state_free_list = entry;
922 }
923
924 size_t
925 anv_descriptor_set_layout_size(const struct anv_descriptor_set_layout *layout)
926 {
927 return
928 sizeof(struct anv_descriptor_set) +
929 layout->size * sizeof(struct anv_descriptor) +
930 layout->buffer_view_count * sizeof(struct anv_buffer_view);
931 }
932
933 VkResult
934 anv_descriptor_set_create(struct anv_device *device,
935 struct anv_descriptor_pool *pool,
936 struct anv_descriptor_set_layout *layout,
937 struct anv_descriptor_set **out_set)
938 {
939 struct anv_descriptor_set *set;
940 const size_t size = anv_descriptor_set_layout_size(layout);
941
942 VkResult result = anv_descriptor_pool_alloc_set(pool, size, &set);
943 if (result != VK_SUCCESS)
944 return result;
945
946 if (layout->descriptor_buffer_size) {
947 /* Align the size to 32 so that alignment gaps don't cause extra holes
948 * in the heap which can lead to bad performance.
949 */
950 uint32_t set_buffer_size = ALIGN(layout->descriptor_buffer_size, 32);
951 uint64_t pool_vma_offset =
952 util_vma_heap_alloc(&pool->bo_heap, set_buffer_size, 32);
953 if (pool_vma_offset == 0) {
954 vk_object_base_finish(&set->base);
955 return vk_error(VK_ERROR_FRAGMENTED_POOL);
956 }
957 assert(pool_vma_offset >= POOL_HEAP_OFFSET &&
958 pool_vma_offset - POOL_HEAP_OFFSET <= INT32_MAX);
959 set->desc_mem.offset = pool_vma_offset - POOL_HEAP_OFFSET;
960 set->desc_mem.alloc_size = set_buffer_size;
961 set->desc_mem.map = pool->bo->map + set->desc_mem.offset;
962
963 set->desc_surface_state = anv_descriptor_pool_alloc_state(pool);
964 anv_fill_buffer_surface_state(device, set->desc_surface_state,
965 ISL_FORMAT_R32G32B32A32_FLOAT,
966 (struct anv_address) {
967 .bo = pool->bo,
968 .offset = set->desc_mem.offset,
969 },
970 layout->descriptor_buffer_size, 1);
971 } else {
972 set->desc_mem = ANV_STATE_NULL;
973 set->desc_surface_state = ANV_STATE_NULL;
974 }
975
976 vk_object_base_init(&device->vk, &set->base,
977 VK_OBJECT_TYPE_DESCRIPTOR_SET);
978 set->pool = pool;
979 set->layout = layout;
980 anv_descriptor_set_layout_ref(layout);
981
982 set->size = size;
983 set->buffer_views =
984 (struct anv_buffer_view *) &set->descriptors[layout->size];
985 set->buffer_view_count = layout->buffer_view_count;
986
987 /* By defining the descriptors to be zero now, we can later verify that
988 * a descriptor has not been populated with user data.
989 */
990 memset(set->descriptors, 0, sizeof(struct anv_descriptor) * layout->size);
991
992 /* Go through and fill out immutable samplers if we have any */
993 struct anv_descriptor *desc = set->descriptors;
994 for (uint32_t b = 0; b < layout->binding_count; b++) {
995 if (layout->binding[b].immutable_samplers) {
996 for (uint32_t i = 0; i < layout->binding[b].array_size; i++) {
997 /* The type will get changed to COMBINED_IMAGE_SAMPLER in
998 * UpdateDescriptorSets if needed. However, if the descriptor
999 * set has an immutable sampler, UpdateDescriptorSets may never
1000 * touch it, so we need to make sure it's 100% valid now.
1001 *
1002 * We don't need to actually provide a sampler because the helper
1003 * will always write in the immutable sampler regardless of what
1004 * is in the sampler parameter.
1005 */
1006 VkDescriptorImageInfo info = { };
1007 anv_descriptor_set_write_image_view(device, set, &info,
1008 VK_DESCRIPTOR_TYPE_SAMPLER,
1009 b, i);
1010 }
1011 }
1012 desc += layout->binding[b].array_size;
1013 }
1014
1015 /* Allocate surface state for the buffer views. */
1016 for (uint32_t b = 0; b < layout->buffer_view_count; b++) {
1017 set->buffer_views[b].surface_state =
1018 anv_descriptor_pool_alloc_state(pool);
1019 }
1020
1021 list_addtail(&set->pool_link, &pool->desc_sets);
1022
1023 *out_set = set;
1024
1025 return VK_SUCCESS;
1026 }
1027
1028 void
1029 anv_descriptor_set_destroy(struct anv_device *device,
1030 struct anv_descriptor_pool *pool,
1031 struct anv_descriptor_set *set)
1032 {
1033 anv_descriptor_set_layout_unref(device, set->layout);
1034
1035 if (set->desc_mem.alloc_size) {
1036 util_vma_heap_free(&pool->bo_heap,
1037 (uint64_t)set->desc_mem.offset + POOL_HEAP_OFFSET,
1038 set->desc_mem.alloc_size);
1039 anv_descriptor_pool_free_state(pool, set->desc_surface_state);
1040 }
1041
1042 for (uint32_t b = 0; b < set->buffer_view_count; b++)
1043 anv_descriptor_pool_free_state(pool, set->buffer_views[b].surface_state);
1044
1045 list_del(&set->pool_link);
1046
1047 vk_object_base_finish(&set->base);
1048 anv_descriptor_pool_free_set(pool, set);
1049 }
1050
1051 VkResult anv_AllocateDescriptorSets(
1052 VkDevice _device,
1053 const VkDescriptorSetAllocateInfo* pAllocateInfo,
1054 VkDescriptorSet* pDescriptorSets)
1055 {
1056 ANV_FROM_HANDLE(anv_device, device, _device);
1057 ANV_FROM_HANDLE(anv_descriptor_pool, pool, pAllocateInfo->descriptorPool);
1058
1059 VkResult result = VK_SUCCESS;
1060 struct anv_descriptor_set *set;
1061 uint32_t i;
1062
1063 for (i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
1064 ANV_FROM_HANDLE(anv_descriptor_set_layout, layout,
1065 pAllocateInfo->pSetLayouts[i]);
1066
1067 result = anv_descriptor_set_create(device, pool, layout, &set);
1068 if (result != VK_SUCCESS)
1069 break;
1070
1071 pDescriptorSets[i] = anv_descriptor_set_to_handle(set);
1072 }
1073
1074 if (result != VK_SUCCESS)
1075 anv_FreeDescriptorSets(_device, pAllocateInfo->descriptorPool,
1076 i, pDescriptorSets);
1077
1078 return result;
1079 }
1080
1081 VkResult anv_FreeDescriptorSets(
1082 VkDevice _device,
1083 VkDescriptorPool descriptorPool,
1084 uint32_t count,
1085 const VkDescriptorSet* pDescriptorSets)
1086 {
1087 ANV_FROM_HANDLE(anv_device, device, _device);
1088 ANV_FROM_HANDLE(anv_descriptor_pool, pool, descriptorPool);
1089
1090 for (uint32_t i = 0; i < count; i++) {
1091 ANV_FROM_HANDLE(anv_descriptor_set, set, pDescriptorSets[i]);
1092
1093 if (!set)
1094 continue;
1095
1096 anv_descriptor_set_destroy(device, pool, set);
1097 }
1098
1099 return VK_SUCCESS;
1100 }
1101
1102 static void
1103 anv_descriptor_set_write_image_param(uint32_t *param_desc_map,
1104 const struct brw_image_param *param)
1105 {
1106 #define WRITE_PARAM_FIELD(field, FIELD) \
1107 for (unsigned i = 0; i < ARRAY_SIZE(param->field); i++) \
1108 param_desc_map[BRW_IMAGE_PARAM_##FIELD##_OFFSET + i] = param->field[i]
1109
1110 WRITE_PARAM_FIELD(offset, OFFSET);
1111 WRITE_PARAM_FIELD(size, SIZE);
1112 WRITE_PARAM_FIELD(stride, STRIDE);
1113 WRITE_PARAM_FIELD(tiling, TILING);
1114 WRITE_PARAM_FIELD(swizzling, SWIZZLING);
1115 WRITE_PARAM_FIELD(size, SIZE);
1116
1117 #undef WRITE_PARAM_FIELD
1118 }
1119
1120 static uint32_t
1121 anv_surface_state_to_handle(struct anv_state state)
1122 {
1123 /* Bits 31:12 of the bindless surface offset in the extended message
1124 * descriptor is bits 25:6 of the byte-based address.
1125 */
1126 assert(state.offset >= 0);
1127 uint32_t offset = state.offset;
1128 assert((offset & 0x3f) == 0 && offset < (1 << 26));
1129 return offset << 6;
1130 }
1131
1132 void
1133 anv_descriptor_set_write_image_view(struct anv_device *device,
1134 struct anv_descriptor_set *set,
1135 const VkDescriptorImageInfo * const info,
1136 VkDescriptorType type,
1137 uint32_t binding,
1138 uint32_t element)
1139 {
1140 const struct anv_descriptor_set_binding_layout *bind_layout =
1141 &set->layout->binding[binding];
1142 struct anv_descriptor *desc =
1143 &set->descriptors[bind_layout->descriptor_index + element];
1144 struct anv_image_view *image_view = NULL;
1145 struct anv_sampler *sampler = NULL;
1146
1147 /* We get called with just VK_DESCRIPTOR_TYPE_SAMPLER as part of descriptor
1148 * set initialization to set the bindless samplers.
1149 */
1150 assert(type == bind_layout->type ||
1151 type == VK_DESCRIPTOR_TYPE_SAMPLER);
1152
1153 switch (type) {
1154 case VK_DESCRIPTOR_TYPE_SAMPLER:
1155 sampler = bind_layout->immutable_samplers ?
1156 bind_layout->immutable_samplers[element] :
1157 anv_sampler_from_handle(info->sampler);
1158 break;
1159
1160 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1161 image_view = anv_image_view_from_handle(info->imageView);
1162 sampler = bind_layout->immutable_samplers ?
1163 bind_layout->immutable_samplers[element] :
1164 anv_sampler_from_handle(info->sampler);
1165 break;
1166
1167 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1168 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1169 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1170 image_view = anv_image_view_from_handle(info->imageView);
1171 break;
1172
1173 default:
1174 unreachable("invalid descriptor type");
1175 }
1176
1177 *desc = (struct anv_descriptor) {
1178 .type = type,
1179 .layout = info->imageLayout,
1180 .image_view = image_view,
1181 .sampler = sampler,
1182 };
1183
1184 void *desc_map = set->desc_mem.map + bind_layout->descriptor_offset +
1185 element * anv_descriptor_size(bind_layout);
1186 memset(desc_map, 0, anv_descriptor_size(bind_layout));
1187
1188 if (bind_layout->data & ANV_DESCRIPTOR_SAMPLED_IMAGE) {
1189 struct anv_sampled_image_descriptor desc_data[3];
1190 memset(desc_data, 0, sizeof(desc_data));
1191
1192 if (image_view) {
1193 for (unsigned p = 0; p < image_view->n_planes; p++) {
1194 struct anv_surface_state sstate =
1195 (desc->layout == VK_IMAGE_LAYOUT_GENERAL) ?
1196 image_view->planes[p].general_sampler_surface_state :
1197 image_view->planes[p].optimal_sampler_surface_state;
1198 desc_data[p].image = anv_surface_state_to_handle(sstate.state);
1199 }
1200 }
1201
1202 if (sampler) {
1203 for (unsigned p = 0; p < sampler->n_planes; p++)
1204 desc_data[p].sampler = sampler->bindless_state.offset + p * 32;
1205 }
1206
1207 /* We may have max_plane_count < 0 if this isn't a sampled image but it
1208 * can be no more than the size of our array of handles.
1209 */
1210 assert(bind_layout->max_plane_count <= ARRAY_SIZE(desc_data));
1211 memcpy(desc_map, desc_data,
1212 MAX2(1, bind_layout->max_plane_count) * sizeof(desc_data[0]));
1213 }
1214
1215 if (image_view == NULL)
1216 return;
1217
1218 if (bind_layout->data & ANV_DESCRIPTOR_STORAGE_IMAGE) {
1219 assert(!(bind_layout->data & ANV_DESCRIPTOR_IMAGE_PARAM));
1220 assert(image_view->n_planes == 1);
1221 struct anv_storage_image_descriptor desc_data = {
1222 .read_write = anv_surface_state_to_handle(
1223 image_view->planes[0].storage_surface_state.state),
1224 .write_only = anv_surface_state_to_handle(
1225 image_view->planes[0].writeonly_storage_surface_state.state),
1226 };
1227 memcpy(desc_map, &desc_data, sizeof(desc_data));
1228 }
1229
1230 if (bind_layout->data & ANV_DESCRIPTOR_IMAGE_PARAM) {
1231 /* Storage images can only ever have one plane */
1232 assert(image_view->n_planes == 1);
1233 const struct brw_image_param *image_param =
1234 &image_view->planes[0].storage_image_param;
1235
1236 anv_descriptor_set_write_image_param(desc_map, image_param);
1237 }
1238
1239 if (bind_layout->data & ANV_DESCRIPTOR_TEXTURE_SWIZZLE) {
1240 assert(!(bind_layout->data & ANV_DESCRIPTOR_SAMPLED_IMAGE));
1241 assert(image_view);
1242 struct anv_texture_swizzle_descriptor desc_data[3];
1243 memset(desc_data, 0, sizeof(desc_data));
1244
1245 for (unsigned p = 0; p < image_view->n_planes; p++) {
1246 desc_data[p] = (struct anv_texture_swizzle_descriptor) {
1247 .swizzle = {
1248 (uint8_t)image_view->planes[p].isl.swizzle.r,
1249 (uint8_t)image_view->planes[p].isl.swizzle.g,
1250 (uint8_t)image_view->planes[p].isl.swizzle.b,
1251 (uint8_t)image_view->planes[p].isl.swizzle.a,
1252 },
1253 };
1254 }
1255 memcpy(desc_map, desc_data,
1256 MAX2(1, bind_layout->max_plane_count) * sizeof(desc_data[0]));
1257 }
1258 }
1259
1260 void
1261 anv_descriptor_set_write_buffer_view(struct anv_device *device,
1262 struct anv_descriptor_set *set,
1263 VkDescriptorType type,
1264 struct anv_buffer_view *buffer_view,
1265 uint32_t binding,
1266 uint32_t element)
1267 {
1268 const struct anv_descriptor_set_binding_layout *bind_layout =
1269 &set->layout->binding[binding];
1270 struct anv_descriptor *desc =
1271 &set->descriptors[bind_layout->descriptor_index + element];
1272
1273 assert(type == bind_layout->type);
1274
1275 void *desc_map = set->desc_mem.map + bind_layout->descriptor_offset +
1276 element * anv_descriptor_size(bind_layout);
1277
1278 if (buffer_view == NULL) {
1279 *desc = (struct anv_descriptor) { .type = type, };
1280 memset(desc_map, 0, anv_descriptor_size(bind_layout));
1281 return;
1282 }
1283
1284 *desc = (struct anv_descriptor) {
1285 .type = type,
1286 .buffer_view = buffer_view,
1287 };
1288
1289 if (bind_layout->data & ANV_DESCRIPTOR_SAMPLED_IMAGE) {
1290 struct anv_sampled_image_descriptor desc_data = {
1291 .image = anv_surface_state_to_handle(buffer_view->surface_state),
1292 };
1293 memcpy(desc_map, &desc_data, sizeof(desc_data));
1294 }
1295
1296 if (bind_layout->data & ANV_DESCRIPTOR_STORAGE_IMAGE) {
1297 assert(!(bind_layout->data & ANV_DESCRIPTOR_IMAGE_PARAM));
1298 struct anv_storage_image_descriptor desc_data = {
1299 .read_write = anv_surface_state_to_handle(
1300 buffer_view->storage_surface_state),
1301 .write_only = anv_surface_state_to_handle(
1302 buffer_view->writeonly_storage_surface_state),
1303 };
1304 memcpy(desc_map, &desc_data, sizeof(desc_data));
1305 }
1306
1307 if (bind_layout->data & ANV_DESCRIPTOR_IMAGE_PARAM) {
1308 anv_descriptor_set_write_image_param(desc_map,
1309 &buffer_view->storage_image_param);
1310 }
1311 }
1312
1313 void
1314 anv_descriptor_set_write_buffer(struct anv_device *device,
1315 struct anv_descriptor_set *set,
1316 struct anv_state_stream *alloc_stream,
1317 VkDescriptorType type,
1318 struct anv_buffer *buffer,
1319 uint32_t binding,
1320 uint32_t element,
1321 VkDeviceSize offset,
1322 VkDeviceSize range)
1323 {
1324 const struct anv_descriptor_set_binding_layout *bind_layout =
1325 &set->layout->binding[binding];
1326 struct anv_descriptor *desc =
1327 &set->descriptors[bind_layout->descriptor_index + element];
1328
1329 assert(type == bind_layout->type);
1330
1331 void *desc_map = set->desc_mem.map + bind_layout->descriptor_offset +
1332 element * anv_descriptor_size(bind_layout);
1333
1334 if (buffer == NULL) {
1335 *desc = (struct anv_descriptor) { .type = type, };
1336 memset(desc_map, 0, anv_descriptor_size(bind_layout));
1337 return;
1338 }
1339
1340 struct anv_address bind_addr = anv_address_add(buffer->address, offset);
1341 uint64_t bind_range = anv_buffer_get_range(buffer, offset, range);
1342
1343 /* We report a bounds checking alignment of 32B for the sake of block
1344 * messages which read an entire register worth at a time.
1345 */
1346 if (type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER ||
1347 type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC)
1348 bind_range = align_u64(bind_range, ANV_UBO_ALIGNMENT);
1349
1350 if (type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
1351 type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
1352 *desc = (struct anv_descriptor) {
1353 .type = type,
1354 .buffer = buffer,
1355 .offset = offset,
1356 .range = range,
1357 };
1358 } else {
1359 assert(bind_layout->data & ANV_DESCRIPTOR_BUFFER_VIEW);
1360 struct anv_buffer_view *bview =
1361 &set->buffer_views[bind_layout->buffer_view_index + element];
1362
1363 bview->format = anv_isl_format_for_descriptor_type(type);
1364 bview->range = bind_range;
1365 bview->address = bind_addr;
1366
1367 /* If we're writing descriptors through a push command, we need to
1368 * allocate the surface state from the command buffer. Otherwise it will
1369 * be allocated by the descriptor pool when calling
1370 * vkAllocateDescriptorSets. */
1371 if (alloc_stream)
1372 bview->surface_state = anv_state_stream_alloc(alloc_stream, 64, 64);
1373
1374 anv_fill_buffer_surface_state(device, bview->surface_state,
1375 bview->format, bind_addr, bind_range, 1);
1376
1377 *desc = (struct anv_descriptor) {
1378 .type = type,
1379 .buffer_view = bview,
1380 };
1381 }
1382
1383 if (bind_layout->data & ANV_DESCRIPTOR_ADDRESS_RANGE) {
1384 struct anv_address_range_descriptor desc_data = {
1385 .address = anv_address_physical(bind_addr),
1386 .range = bind_range,
1387 };
1388 memcpy(desc_map, &desc_data, sizeof(desc_data));
1389 }
1390 }
1391
1392 void
1393 anv_descriptor_set_write_inline_uniform_data(struct anv_device *device,
1394 struct anv_descriptor_set *set,
1395 uint32_t binding,
1396 const void *data,
1397 size_t offset,
1398 size_t size)
1399 {
1400 const struct anv_descriptor_set_binding_layout *bind_layout =
1401 &set->layout->binding[binding];
1402
1403 assert(bind_layout->data & ANV_DESCRIPTOR_INLINE_UNIFORM);
1404
1405 void *desc_map = set->desc_mem.map + bind_layout->descriptor_offset;
1406
1407 memcpy(desc_map + offset, data, size);
1408 }
1409
1410 void anv_UpdateDescriptorSets(
1411 VkDevice _device,
1412 uint32_t descriptorWriteCount,
1413 const VkWriteDescriptorSet* pDescriptorWrites,
1414 uint32_t descriptorCopyCount,
1415 const VkCopyDescriptorSet* pDescriptorCopies)
1416 {
1417 ANV_FROM_HANDLE(anv_device, device, _device);
1418
1419 for (uint32_t i = 0; i < descriptorWriteCount; i++) {
1420 const VkWriteDescriptorSet *write = &pDescriptorWrites[i];
1421 ANV_FROM_HANDLE(anv_descriptor_set, set, write->dstSet);
1422
1423 switch (write->descriptorType) {
1424 case VK_DESCRIPTOR_TYPE_SAMPLER:
1425 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1426 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1427 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1428 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1429 for (uint32_t j = 0; j < write->descriptorCount; j++) {
1430 anv_descriptor_set_write_image_view(device, set,
1431 write->pImageInfo + j,
1432 write->descriptorType,
1433 write->dstBinding,
1434 write->dstArrayElement + j);
1435 }
1436 break;
1437
1438 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1439 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1440 for (uint32_t j = 0; j < write->descriptorCount; j++) {
1441 ANV_FROM_HANDLE(anv_buffer_view, bview,
1442 write->pTexelBufferView[j]);
1443
1444 anv_descriptor_set_write_buffer_view(device, set,
1445 write->descriptorType,
1446 bview,
1447 write->dstBinding,
1448 write->dstArrayElement + j);
1449 }
1450 break;
1451
1452 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1453 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1454 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1455 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1456 for (uint32_t j = 0; j < write->descriptorCount; j++) {
1457 ANV_FROM_HANDLE(anv_buffer, buffer, write->pBufferInfo[j].buffer);
1458
1459 anv_descriptor_set_write_buffer(device, set,
1460 NULL,
1461 write->descriptorType,
1462 buffer,
1463 write->dstBinding,
1464 write->dstArrayElement + j,
1465 write->pBufferInfo[j].offset,
1466 write->pBufferInfo[j].range);
1467 }
1468 break;
1469
1470 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT: {
1471 const VkWriteDescriptorSetInlineUniformBlockEXT *inline_write =
1472 vk_find_struct_const(write->pNext,
1473 WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT);
1474 assert(inline_write->dataSize == write->descriptorCount);
1475 anv_descriptor_set_write_inline_uniform_data(device, set,
1476 write->dstBinding,
1477 inline_write->pData,
1478 write->dstArrayElement,
1479 inline_write->dataSize);
1480 break;
1481 }
1482
1483 default:
1484 break;
1485 }
1486 }
1487
1488 for (uint32_t i = 0; i < descriptorCopyCount; i++) {
1489 const VkCopyDescriptorSet *copy = &pDescriptorCopies[i];
1490 ANV_FROM_HANDLE(anv_descriptor_set, src, copy->srcSet);
1491 ANV_FROM_HANDLE(anv_descriptor_set, dst, copy->dstSet);
1492
1493 const struct anv_descriptor_set_binding_layout *src_layout =
1494 &src->layout->binding[copy->srcBinding];
1495 struct anv_descriptor *src_desc =
1496 &src->descriptors[src_layout->descriptor_index];
1497 src_desc += copy->srcArrayElement;
1498
1499 const struct anv_descriptor_set_binding_layout *dst_layout =
1500 &dst->layout->binding[copy->dstBinding];
1501 struct anv_descriptor *dst_desc =
1502 &dst->descriptors[dst_layout->descriptor_index];
1503 dst_desc += copy->dstArrayElement;
1504
1505 if (src_layout->data & ANV_DESCRIPTOR_INLINE_UNIFORM) {
1506 assert(src_layout->data == ANV_DESCRIPTOR_INLINE_UNIFORM);
1507 memcpy(dst->desc_mem.map + dst_layout->descriptor_offset +
1508 copy->dstArrayElement,
1509 src->desc_mem.map + src_layout->descriptor_offset +
1510 copy->srcArrayElement,
1511 copy->descriptorCount);
1512 } else {
1513 for (uint32_t j = 0; j < copy->descriptorCount; j++)
1514 dst_desc[j] = src_desc[j];
1515
1516 unsigned desc_size = anv_descriptor_size(src_layout);
1517 if (desc_size > 0) {
1518 assert(desc_size == anv_descriptor_size(dst_layout));
1519 memcpy(dst->desc_mem.map + dst_layout->descriptor_offset +
1520 copy->dstArrayElement * desc_size,
1521 src->desc_mem.map + src_layout->descriptor_offset +
1522 copy->srcArrayElement * desc_size,
1523 copy->descriptorCount * desc_size);
1524 }
1525 }
1526 }
1527 }
1528
1529 /*
1530 * Descriptor update templates.
1531 */
1532
1533 void
1534 anv_descriptor_set_write_template(struct anv_device *device,
1535 struct anv_descriptor_set *set,
1536 struct anv_state_stream *alloc_stream,
1537 const struct anv_descriptor_update_template *template,
1538 const void *data)
1539 {
1540 for (uint32_t i = 0; i < template->entry_count; i++) {
1541 const struct anv_descriptor_template_entry *entry =
1542 &template->entries[i];
1543
1544 switch (entry->type) {
1545 case VK_DESCRIPTOR_TYPE_SAMPLER:
1546 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1547 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1548 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1549 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1550 for (uint32_t j = 0; j < entry->array_count; j++) {
1551 const VkDescriptorImageInfo *info =
1552 data + entry->offset + j * entry->stride;
1553 anv_descriptor_set_write_image_view(device, set,
1554 info, entry->type,
1555 entry->binding,
1556 entry->array_element + j);
1557 }
1558 break;
1559
1560 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1561 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1562 for (uint32_t j = 0; j < entry->array_count; j++) {
1563 const VkBufferView *_bview =
1564 data + entry->offset + j * entry->stride;
1565 ANV_FROM_HANDLE(anv_buffer_view, bview, *_bview);
1566
1567 anv_descriptor_set_write_buffer_view(device, set,
1568 entry->type,
1569 bview,
1570 entry->binding,
1571 entry->array_element + j);
1572 }
1573 break;
1574
1575 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1576 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1577 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1578 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1579 for (uint32_t j = 0; j < entry->array_count; j++) {
1580 const VkDescriptorBufferInfo *info =
1581 data + entry->offset + j * entry->stride;
1582 ANV_FROM_HANDLE(anv_buffer, buffer, info->buffer);
1583
1584 anv_descriptor_set_write_buffer(device, set,
1585 alloc_stream,
1586 entry->type,
1587 buffer,
1588 entry->binding,
1589 entry->array_element + j,
1590 info->offset, info->range);
1591 }
1592 break;
1593
1594 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
1595 anv_descriptor_set_write_inline_uniform_data(device, set,
1596 entry->binding,
1597 data + entry->offset,
1598 entry->array_element,
1599 entry->array_count);
1600 break;
1601
1602 default:
1603 break;
1604 }
1605 }
1606 }
1607
1608 VkResult anv_CreateDescriptorUpdateTemplate(
1609 VkDevice _device,
1610 const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
1611 const VkAllocationCallbacks* pAllocator,
1612 VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate)
1613 {
1614 ANV_FROM_HANDLE(anv_device, device, _device);
1615 struct anv_descriptor_update_template *template;
1616
1617 size_t size = sizeof(*template) +
1618 pCreateInfo->descriptorUpdateEntryCount * sizeof(template->entries[0]);
1619 template = vk_alloc2(&device->vk.alloc, pAllocator, size, 8,
1620 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1621 if (template == NULL)
1622 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1623
1624 vk_object_base_init(&device->vk, &template->base,
1625 VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE);
1626 template->bind_point = pCreateInfo->pipelineBindPoint;
1627
1628 if (pCreateInfo->templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET)
1629 template->set = pCreateInfo->set;
1630
1631 template->entry_count = pCreateInfo->descriptorUpdateEntryCount;
1632 for (uint32_t i = 0; i < template->entry_count; i++) {
1633 const VkDescriptorUpdateTemplateEntry *pEntry =
1634 &pCreateInfo->pDescriptorUpdateEntries[i];
1635
1636 template->entries[i] = (struct anv_descriptor_template_entry) {
1637 .type = pEntry->descriptorType,
1638 .binding = pEntry->dstBinding,
1639 .array_element = pEntry->dstArrayElement,
1640 .array_count = pEntry->descriptorCount,
1641 .offset = pEntry->offset,
1642 .stride = pEntry->stride,
1643 };
1644 }
1645
1646 *pDescriptorUpdateTemplate =
1647 anv_descriptor_update_template_to_handle(template);
1648
1649 return VK_SUCCESS;
1650 }
1651
1652 void anv_DestroyDescriptorUpdateTemplate(
1653 VkDevice _device,
1654 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1655 const VkAllocationCallbacks* pAllocator)
1656 {
1657 ANV_FROM_HANDLE(anv_device, device, _device);
1658 ANV_FROM_HANDLE(anv_descriptor_update_template, template,
1659 descriptorUpdateTemplate);
1660
1661 vk_object_base_finish(&template->base);
1662 vk_free2(&device->vk.alloc, pAllocator, template);
1663 }
1664
1665 void anv_UpdateDescriptorSetWithTemplate(
1666 VkDevice _device,
1667 VkDescriptorSet descriptorSet,
1668 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
1669 const void* pData)
1670 {
1671 ANV_FROM_HANDLE(anv_device, device, _device);
1672 ANV_FROM_HANDLE(anv_descriptor_set, set, descriptorSet);
1673 ANV_FROM_HANDLE(anv_descriptor_update_template, template,
1674 descriptorUpdateTemplate);
1675
1676 anv_descriptor_set_write_template(device, set, NULL, template, pData);
1677 }