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