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