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