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