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