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