2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 #include "util/mesa-sha1.h"
31 #include "radv_private.h"
34 VkResult
radv_CreateDescriptorSetLayout(
36 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
37 const VkAllocationCallbacks
* pAllocator
,
38 VkDescriptorSetLayout
* pSetLayout
)
40 RADV_FROM_HANDLE(radv_device
, device
, _device
);
41 struct radv_descriptor_set_layout
*set_layout
;
43 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
);
45 uint32_t max_binding
= 0;
46 uint32_t immutable_sampler_count
= 0;
47 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
48 max_binding
= MAX2(max_binding
, pCreateInfo
->pBindings
[j
].binding
);
49 if (pCreateInfo
->pBindings
[j
].pImmutableSamplers
)
50 immutable_sampler_count
+= pCreateInfo
->pBindings
[j
].descriptorCount
;
53 uint32_t samplers_offset
= sizeof(struct radv_descriptor_set_layout
) +
54 (max_binding
+ 1) * sizeof(set_layout
->binding
[0]);
55 size_t size
= samplers_offset
+ immutable_sampler_count
* 4 * sizeof(uint32_t);
57 set_layout
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
58 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
60 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
62 set_layout
->flags
= pCreateInfo
->flags
;
64 /* We just allocate all the samplers at the end of the struct */
65 uint32_t *samplers
= (uint32_t*)&set_layout
->binding
[max_binding
+ 1];
67 set_layout
->binding_count
= max_binding
+ 1;
68 set_layout
->shader_stages
= 0;
71 memset(set_layout
->binding
, 0, size
- sizeof(struct radv_descriptor_set_layout
));
73 uint32_t buffer_count
= 0;
74 uint32_t dynamic_offset_count
= 0;
76 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
77 const VkDescriptorSetLayoutBinding
*binding
= &pCreateInfo
->pBindings
[j
];
78 uint32_t b
= binding
->binding
;
81 switch (binding
->descriptorType
) {
82 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
83 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
84 assert(!(pCreateInfo
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
85 set_layout
->binding
[b
].dynamic_offset_count
= 1;
86 set_layout
->dynamic_shader_stages
|= binding
->stageFlags
;
87 set_layout
->binding
[b
].size
= 0;
88 set_layout
->binding
[b
].buffer_count
= 1;
91 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
92 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
93 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
94 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
95 set_layout
->binding
[b
].size
= 16;
96 set_layout
->binding
[b
].buffer_count
= 1;
99 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
100 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
101 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
102 /* main descriptor + fmask descriptor */
103 set_layout
->binding
[b
].size
= 64;
104 set_layout
->binding
[b
].buffer_count
= 1;
107 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
108 /* main descriptor + fmask descriptor + sampler */
109 set_layout
->binding
[b
].size
= 96;
110 set_layout
->binding
[b
].buffer_count
= 1;
113 case VK_DESCRIPTOR_TYPE_SAMPLER
:
114 set_layout
->binding
[b
].size
= 16;
118 unreachable("unknown descriptor type\n");
122 set_layout
->size
= align(set_layout
->size
, alignment
);
123 assert(binding
->descriptorCount
> 0);
124 set_layout
->binding
[b
].type
= binding
->descriptorType
;
125 set_layout
->binding
[b
].array_size
= binding
->descriptorCount
;
126 set_layout
->binding
[b
].offset
= set_layout
->size
;
127 set_layout
->binding
[b
].buffer_offset
= buffer_count
;
128 set_layout
->binding
[b
].dynamic_offset_offset
= dynamic_offset_count
;
130 if (binding
->pImmutableSamplers
) {
131 set_layout
->binding
[b
].immutable_samplers_offset
= samplers_offset
;
132 set_layout
->binding
[b
].immutable_samplers_equal
= true;
135 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++)
136 memcpy(samplers
+ 4 * i
, &radv_sampler_from_handle(binding
->pImmutableSamplers
[i
])->state
, 16);
137 for (uint32_t i
= 1; i
< binding
->descriptorCount
; i
++)
138 if (memcmp(samplers
+ 4 * i
, samplers
, 16) != 0)
139 set_layout
->binding
[b
].immutable_samplers_equal
= false;
141 /* Don't reserve space for the samplers if they're not accessed. */
142 if (set_layout
->binding
[b
].immutable_samplers_equal
) {
143 if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
144 set_layout
->binding
[b
].size
-= 32;
145 else if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_SAMPLER
)
146 set_layout
->binding
[b
].size
-= 16;
148 samplers
+= 4 * binding
->descriptorCount
;
149 samplers_offset
+= 4 * sizeof(uint32_t) * binding
->descriptorCount
;
152 set_layout
->size
+= binding
->descriptorCount
* set_layout
->binding
[b
].size
;
153 buffer_count
+= binding
->descriptorCount
* set_layout
->binding
[b
].buffer_count
;
154 dynamic_offset_count
+= binding
->descriptorCount
*
155 set_layout
->binding
[b
].dynamic_offset_count
;
156 set_layout
->shader_stages
|= binding
->stageFlags
;
159 set_layout
->buffer_count
= buffer_count
;
160 set_layout
->dynamic_offset_count
= dynamic_offset_count
;
162 *pSetLayout
= radv_descriptor_set_layout_to_handle(set_layout
);
167 void radv_DestroyDescriptorSetLayout(
169 VkDescriptorSetLayout _set_layout
,
170 const VkAllocationCallbacks
* pAllocator
)
172 RADV_FROM_HANDLE(radv_device
, device
, _device
);
173 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
, _set_layout
);
178 vk_free2(&device
->alloc
, pAllocator
, set_layout
);
182 * Pipeline layouts. These have nothing to do with the pipeline. They are
183 * just muttiple descriptor set layouts pasted together
186 VkResult
radv_CreatePipelineLayout(
188 const VkPipelineLayoutCreateInfo
* pCreateInfo
,
189 const VkAllocationCallbacks
* pAllocator
,
190 VkPipelineLayout
* pPipelineLayout
)
192 RADV_FROM_HANDLE(radv_device
, device
, _device
);
193 struct radv_pipeline_layout
*layout
;
194 struct mesa_sha1 ctx
;
196 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
);
198 layout
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*layout
), 8,
199 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
201 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
203 layout
->num_sets
= pCreateInfo
->setLayoutCount
;
205 unsigned dynamic_offset_count
= 0;
208 _mesa_sha1_init(&ctx
);
209 for (uint32_t set
= 0; set
< pCreateInfo
->setLayoutCount
; set
++) {
210 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
,
211 pCreateInfo
->pSetLayouts
[set
]);
212 layout
->set
[set
].layout
= set_layout
;
214 layout
->set
[set
].dynamic_offset_start
= dynamic_offset_count
;
215 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
216 dynamic_offset_count
+= set_layout
->binding
[b
].array_size
* set_layout
->binding
[b
].dynamic_offset_count
;
217 if (set_layout
->binding
[b
].immutable_samplers_offset
)
218 _mesa_sha1_update(&ctx
, radv_immutable_samplers(set_layout
, set_layout
->binding
+ b
),
219 set_layout
->binding
[b
].array_size
* 4 * sizeof(uint32_t));
221 _mesa_sha1_update(&ctx
, set_layout
->binding
,
222 sizeof(set_layout
->binding
[0]) * set_layout
->binding_count
);
225 layout
->dynamic_offset_count
= dynamic_offset_count
;
226 layout
->push_constant_size
= 0;
227 for (unsigned i
= 0; i
< pCreateInfo
->pushConstantRangeCount
; ++i
) {
228 const VkPushConstantRange
*range
= pCreateInfo
->pPushConstantRanges
+ i
;
229 layout
->push_constant_size
= MAX2(layout
->push_constant_size
,
230 range
->offset
+ range
->size
);
233 layout
->push_constant_size
= align(layout
->push_constant_size
, 16);
234 _mesa_sha1_update(&ctx
, &layout
->push_constant_size
,
235 sizeof(layout
->push_constant_size
));
236 _mesa_sha1_final(&ctx
, layout
->sha1
);
237 *pPipelineLayout
= radv_pipeline_layout_to_handle(layout
);
242 void radv_DestroyPipelineLayout(
244 VkPipelineLayout _pipelineLayout
,
245 const VkAllocationCallbacks
* pAllocator
)
247 RADV_FROM_HANDLE(radv_device
, device
, _device
);
248 RADV_FROM_HANDLE(radv_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
250 if (!pipeline_layout
)
252 vk_free2(&device
->alloc
, pAllocator
, pipeline_layout
);
258 radv_descriptor_set_create(struct radv_device
*device
,
259 struct radv_descriptor_pool
*pool
,
260 const struct radv_descriptor_set_layout
*layout
,
261 struct radv_descriptor_set
**out_set
)
263 struct radv_descriptor_set
*set
;
264 unsigned mem_size
= sizeof(struct radv_descriptor_set
) +
265 sizeof(struct radeon_winsys_bo
*) * layout
->buffer_count
;
266 set
= vk_alloc2(&device
->alloc
, NULL
, mem_size
, 8,
267 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
270 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
272 memset(set
, 0, mem_size
);
274 if (layout
->dynamic_offset_count
) {
275 unsigned size
= sizeof(struct radv_descriptor_range
) *
276 layout
->dynamic_offset_count
;
277 set
->dynamic_descriptors
= vk_alloc2(&device
->alloc
, NULL
, size
, 8,
278 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
280 if (!set
->dynamic_descriptors
) {
281 vk_free2(&device
->alloc
, NULL
, set
);
282 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
286 set
->layout
= layout
;
288 uint32_t layout_size
= align_u32(layout
->size
, 32);
289 set
->size
= layout
->size
;
291 /* try to allocate linearly first, so that we don't spend
292 * time looking for gaps if the app only allocates &
293 * resets via the pool. */
294 if (pool
->current_offset
+ layout_size
<= pool
->size
) {
296 set
->mapped_ptr
= (uint32_t*)(pool
->mapped_ptr
+ pool
->current_offset
);
297 set
->va
= device
->ws
->buffer_get_va(set
->bo
) + pool
->current_offset
;
298 pool
->current_offset
+= layout_size
;
299 list_addtail(&set
->vram_list
, &pool
->vram_list
);
302 struct list_head
*prev
= &pool
->vram_list
;
303 struct radv_descriptor_set
*cur
;
304 LIST_FOR_EACH_ENTRY(cur
, &pool
->vram_list
, vram_list
) {
305 uint64_t start
= (uint8_t*)cur
->mapped_ptr
- pool
->mapped_ptr
;
306 if (start
- offset
>= layout_size
)
309 offset
= start
+ cur
->size
;
310 prev
= &cur
->vram_list
;
313 if (pool
->size
- offset
< layout_size
) {
314 vk_free2(&device
->alloc
, NULL
, set
->dynamic_descriptors
);
315 vk_free2(&device
->alloc
, NULL
, set
);
316 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY_KHR
);
319 set
->mapped_ptr
= (uint32_t*)(pool
->mapped_ptr
+ offset
);
320 set
->va
= device
->ws
->buffer_get_va(set
->bo
) + offset
;
321 list_add(&set
->vram_list
, prev
);
325 for (unsigned i
= 0; i
< layout
->binding_count
; ++i
) {
326 if (!layout
->binding
[i
].immutable_samplers_offset
||
327 layout
->binding
[i
].immutable_samplers_equal
)
330 unsigned offset
= layout
->binding
[i
].offset
/ 4;
331 if (layout
->binding
[i
].type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
334 const uint32_t *samplers
= (const uint32_t*)((const char*)layout
+ layout
->binding
[i
].immutable_samplers_offset
);
335 for (unsigned j
= 0; j
< layout
->binding
[i
].array_size
; ++j
) {
336 memcpy(set
->mapped_ptr
+ offset
, samplers
+ 4 * j
, 16);
337 offset
+= layout
->binding
[i
].size
/ 4;
346 radv_descriptor_set_destroy(struct radv_device
*device
,
347 struct radv_descriptor_pool
*pool
,
348 struct radv_descriptor_set
*set
,
351 if (free_bo
&& set
->size
)
352 list_del(&set
->vram_list
);
353 if (set
->dynamic_descriptors
)
354 vk_free2(&device
->alloc
, NULL
, set
->dynamic_descriptors
);
355 vk_free2(&device
->alloc
, NULL
, set
);
358 VkResult
radv_CreateDescriptorPool(
360 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
361 const VkAllocationCallbacks
* pAllocator
,
362 VkDescriptorPool
* pDescriptorPool
)
364 RADV_FROM_HANDLE(radv_device
, device
, _device
);
365 struct radv_descriptor_pool
*pool
;
366 int size
= sizeof(struct radv_descriptor_pool
);
367 uint64_t bo_size
= 0;
368 pool
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
369 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
371 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
373 memset(pool
, 0, sizeof(*pool
));
375 for (unsigned i
= 0; i
< pCreateInfo
->poolSizeCount
; ++i
) {
376 switch(pCreateInfo
->pPoolSizes
[i
].type
) {
377 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
378 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
380 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
381 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
382 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
383 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
384 case VK_DESCRIPTOR_TYPE_SAMPLER
:
385 /* 32 as we may need to align for images */
386 bo_size
+= 32 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
388 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
389 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
390 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
391 bo_size
+= 64 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
393 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
394 bo_size
+= 96 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
397 unreachable("unknown descriptor type\n");
403 pool
->bo
= device
->ws
->buffer_create(device
->ws
, bo_size
,
404 32, RADEON_DOMAIN_VRAM
, 0);
405 pool
->mapped_ptr
= (uint8_t*)device
->ws
->buffer_map(pool
->bo
);
407 pool
->size
= bo_size
;
409 list_inithead(&pool
->vram_list
);
410 *pDescriptorPool
= radv_descriptor_pool_to_handle(pool
);
414 void radv_DestroyDescriptorPool(
416 VkDescriptorPool _pool
,
417 const VkAllocationCallbacks
* pAllocator
)
419 RADV_FROM_HANDLE(radv_device
, device
, _device
);
420 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, _pool
);
425 list_for_each_entry_safe(struct radv_descriptor_set
, set
,
426 &pool
->vram_list
, vram_list
) {
427 radv_descriptor_set_destroy(device
, pool
, set
, false);
431 device
->ws
->buffer_destroy(pool
->bo
);
432 vk_free2(&device
->alloc
, pAllocator
, pool
);
435 VkResult
radv_ResetDescriptorPool(
437 VkDescriptorPool descriptorPool
,
438 VkDescriptorPoolResetFlags flags
)
440 RADV_FROM_HANDLE(radv_device
, device
, _device
);
441 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, descriptorPool
);
443 list_for_each_entry_safe(struct radv_descriptor_set
, set
,
444 &pool
->vram_list
, vram_list
) {
445 radv_descriptor_set_destroy(device
, pool
, set
, false);
448 list_inithead(&pool
->vram_list
);
450 pool
->current_offset
= 0;
455 VkResult
radv_AllocateDescriptorSets(
457 const VkDescriptorSetAllocateInfo
* pAllocateInfo
,
458 VkDescriptorSet
* pDescriptorSets
)
460 RADV_FROM_HANDLE(radv_device
, device
, _device
);
461 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, pAllocateInfo
->descriptorPool
);
463 VkResult result
= VK_SUCCESS
;
465 struct radv_descriptor_set
*set
;
467 /* allocate a set of buffers for each shader to contain descriptors */
468 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
469 RADV_FROM_HANDLE(radv_descriptor_set_layout
, layout
,
470 pAllocateInfo
->pSetLayouts
[i
]);
472 assert(!(layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
474 result
= radv_descriptor_set_create(device
, pool
, layout
, &set
);
475 if (result
!= VK_SUCCESS
)
478 pDescriptorSets
[i
] = radv_descriptor_set_to_handle(set
);
481 if (result
!= VK_SUCCESS
)
482 radv_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
487 VkResult
radv_FreeDescriptorSets(
489 VkDescriptorPool descriptorPool
,
491 const VkDescriptorSet
* pDescriptorSets
)
493 RADV_FROM_HANDLE(radv_device
, device
, _device
);
494 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, descriptorPool
);
496 for (uint32_t i
= 0; i
< count
; i
++) {
497 RADV_FROM_HANDLE(radv_descriptor_set
, set
, pDescriptorSets
[i
]);
500 radv_descriptor_set_destroy(device
, pool
, set
, true);
505 static void write_texel_buffer_descriptor(struct radv_device
*device
,
506 struct radv_cmd_buffer
*cmd_buffer
,
508 struct radeon_winsys_bo
**buffer_list
,
509 const VkBufferView _buffer_view
)
511 RADV_FROM_HANDLE(radv_buffer_view
, buffer_view
, _buffer_view
);
513 memcpy(dst
, buffer_view
->state
, 4 * 4);
516 device
->ws
->cs_add_buffer(cmd_buffer
->cs
, buffer_view
->bo
, 7);
518 *buffer_list
= buffer_view
->bo
;
521 static void write_buffer_descriptor(struct radv_device
*device
,
522 struct radv_cmd_buffer
*cmd_buffer
,
524 struct radeon_winsys_bo
**buffer_list
,
525 const VkDescriptorBufferInfo
*buffer_info
)
527 RADV_FROM_HANDLE(radv_buffer
, buffer
, buffer_info
->buffer
);
528 uint64_t va
= device
->ws
->buffer_get_va(buffer
->bo
);
529 uint32_t range
= buffer_info
->range
;
531 if (buffer_info
->range
== VK_WHOLE_SIZE
)
532 range
= buffer
->size
- buffer_info
->offset
;
534 va
+= buffer_info
->offset
+ buffer
->offset
;
536 dst
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32);
538 dst
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
539 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
540 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
541 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
542 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
543 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
546 device
->ws
->cs_add_buffer(cmd_buffer
->cs
, buffer
->bo
, 7);
548 *buffer_list
= buffer
->bo
;
551 static void write_dynamic_buffer_descriptor(struct radv_device
*device
,
552 struct radv_descriptor_range
*range
,
553 struct radeon_winsys_bo
**buffer_list
,
554 const VkDescriptorBufferInfo
*buffer_info
)
556 RADV_FROM_HANDLE(radv_buffer
, buffer
, buffer_info
->buffer
);
557 uint64_t va
= device
->ws
->buffer_get_va(buffer
->bo
);
558 unsigned size
= buffer_info
->range
;
560 if (buffer_info
->range
== VK_WHOLE_SIZE
)
561 size
= buffer
->size
- buffer_info
->offset
;
563 va
+= buffer_info
->offset
+ buffer
->offset
;
567 *buffer_list
= buffer
->bo
;
571 write_image_descriptor(struct radv_device
*device
,
572 struct radv_cmd_buffer
*cmd_buffer
,
574 struct radeon_winsys_bo
**buffer_list
,
575 const VkDescriptorImageInfo
*image_info
)
577 RADV_FROM_HANDLE(radv_image_view
, iview
, image_info
->imageView
);
578 memcpy(dst
, iview
->descriptor
, 8 * 4);
579 memcpy(dst
+ 8, iview
->fmask_descriptor
, 8 * 4);
582 device
->ws
->cs_add_buffer(cmd_buffer
->cs
, iview
->bo
, 7);
584 *buffer_list
= iview
->bo
;
588 write_combined_image_sampler_descriptor(struct radv_device
*device
,
589 struct radv_cmd_buffer
*cmd_buffer
,
591 struct radeon_winsys_bo
**buffer_list
,
592 const VkDescriptorImageInfo
*image_info
,
595 RADV_FROM_HANDLE(radv_sampler
, sampler
, image_info
->sampler
);
597 write_image_descriptor(device
, cmd_buffer
, dst
, buffer_list
, image_info
);
598 /* copy over sampler state */
600 memcpy(dst
+ 16, sampler
->state
, 16);
604 write_sampler_descriptor(struct radv_device
*device
,
606 const VkDescriptorImageInfo
*image_info
)
608 RADV_FROM_HANDLE(radv_sampler
, sampler
, image_info
->sampler
);
610 memcpy(dst
, sampler
->state
, 16);
613 void radv_update_descriptor_sets(
614 struct radv_device
* device
,
615 struct radv_cmd_buffer
* cmd_buffer
,
616 VkDescriptorSet dstSetOverride
,
617 uint32_t descriptorWriteCount
,
618 const VkWriteDescriptorSet
* pDescriptorWrites
,
619 uint32_t descriptorCopyCount
,
620 const VkCopyDescriptorSet
* pDescriptorCopies
)
623 for (i
= 0; i
< descriptorWriteCount
; i
++) {
624 const VkWriteDescriptorSet
*writeset
= &pDescriptorWrites
[i
];
625 RADV_FROM_HANDLE(radv_descriptor_set
, set
,
626 dstSetOverride
? dstSetOverride
: writeset
->dstSet
);
627 const struct radv_descriptor_set_binding_layout
*binding_layout
=
628 set
->layout
->binding
+ writeset
->dstBinding
;
629 uint32_t *ptr
= set
->mapped_ptr
;
630 struct radeon_winsys_bo
**buffer_list
= set
->descriptors
;
631 /* Immutable samplers are not copied into push descriptors when they are
632 * allocated, so if we are writing push descriptors we have to copy the
633 * immutable samplers into them now.
635 const bool copy_immutable_samplers
= cmd_buffer
&&
636 binding_layout
->immutable_samplers_offset
&& !binding_layout
->immutable_samplers_equal
;
637 const uint32_t *samplers
= radv_immutable_samplers(set
->layout
, binding_layout
);
639 ptr
+= binding_layout
->offset
/ 4;
640 ptr
+= binding_layout
->size
* writeset
->dstArrayElement
/ 4;
641 buffer_list
+= binding_layout
->buffer_offset
;
642 buffer_list
+= binding_layout
->buffer_count
* writeset
->dstArrayElement
;
643 for (j
= 0; j
< writeset
->descriptorCount
; ++j
) {
644 switch(writeset
->descriptorType
) {
645 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
646 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
647 unsigned idx
= writeset
->dstArrayElement
+ j
;
648 idx
+= binding_layout
->dynamic_offset_offset
;
649 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
650 write_dynamic_buffer_descriptor(device
, set
->dynamic_descriptors
+ idx
,
651 buffer_list
, writeset
->pBufferInfo
+ j
);
654 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
655 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
656 write_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
657 writeset
->pBufferInfo
+ j
);
659 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
660 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
661 write_texel_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
662 writeset
->pTexelBufferView
[j
]);
664 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
665 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
666 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
667 write_image_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
668 writeset
->pImageInfo
+ j
);
670 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
671 write_combined_image_sampler_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
672 writeset
->pImageInfo
+ j
,
673 !binding_layout
->immutable_samplers_offset
);
674 if (copy_immutable_samplers
) {
675 const unsigned idx
= writeset
->dstArrayElement
+ j
;
676 memcpy(ptr
+ 16, samplers
+ 4 * idx
, 16);
679 case VK_DESCRIPTOR_TYPE_SAMPLER
:
680 if (!binding_layout
->immutable_samplers_offset
) {
681 write_sampler_descriptor(device
, ptr
,
682 writeset
->pImageInfo
+ j
);
683 } else if (copy_immutable_samplers
) {
684 unsigned idx
= writeset
->dstArrayElement
+ j
;
685 memcpy(ptr
, samplers
+ 4 * idx
, 16);
689 unreachable("unimplemented descriptor type");
692 ptr
+= binding_layout
->size
/ 4;
693 buffer_list
+= binding_layout
->buffer_count
;
697 if (descriptorCopyCount
)
698 radv_finishme("copy descriptors");
701 void radv_UpdateDescriptorSets(
703 uint32_t descriptorWriteCount
,
704 const VkWriteDescriptorSet
* pDescriptorWrites
,
705 uint32_t descriptorCopyCount
,
706 const VkCopyDescriptorSet
* pDescriptorCopies
)
708 RADV_FROM_HANDLE(radv_device
, device
, _device
);
710 radv_update_descriptor_sets(device
, NULL
, VK_NULL_HANDLE
, descriptorWriteCount
, pDescriptorWrites
,
711 descriptorCopyCount
, pDescriptorCopies
);
714 VkResult
radv_CreateDescriptorUpdateTemplateKHR(VkDevice _device
,
715 const VkDescriptorUpdateTemplateCreateInfoKHR
*pCreateInfo
,
716 const VkAllocationCallbacks
*pAllocator
,
717 VkDescriptorUpdateTemplateKHR
*pDescriptorUpdateTemplate
)
719 RADV_FROM_HANDLE(radv_device
, device
, _device
);
720 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
, pCreateInfo
->descriptorSetLayout
);
721 const uint32_t entry_count
= pCreateInfo
->descriptorUpdateEntryCount
;
722 const size_t size
= sizeof(struct radv_descriptor_update_template
) +
723 sizeof(struct radv_descriptor_update_template_entry
) * entry_count
;
724 struct radv_descriptor_update_template
*templ
;
727 templ
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
729 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
731 templ
->entry_count
= entry_count
;
733 for (i
= 0; i
< entry_count
; i
++) {
734 const VkDescriptorUpdateTemplateEntryKHR
*entry
= &pCreateInfo
->pDescriptorUpdateEntries
[i
];
735 const struct radv_descriptor_set_binding_layout
*binding_layout
=
736 set_layout
->binding
+ entry
->dstBinding
;
737 const uint32_t buffer_offset
= binding_layout
->buffer_offset
+
738 binding_layout
->buffer_count
* entry
->dstArrayElement
;
739 const uint32_t *immutable_samplers
= NULL
;
743 /* dst_offset is an offset into dynamic_descriptors when the descriptor
744 is dynamic, and an offset into mapped_ptr otherwise */
745 switch (entry
->descriptorType
) {
746 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
747 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
748 assert(pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET_KHR
);
749 dst_offset
= binding_layout
->dynamic_offset_offset
+ entry
->dstArrayElement
;
750 dst_stride
= 0; /* Not used */
753 switch (entry
->descriptorType
) {
754 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
755 case VK_DESCRIPTOR_TYPE_SAMPLER
:
756 /* Immutable samplers are copied into push descriptors when they are pushed */
757 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR
&&
758 binding_layout
->immutable_samplers_offset
&& !binding_layout
->immutable_samplers_equal
) {
759 immutable_samplers
= radv_immutable_samplers(set_layout
, binding_layout
) + entry
->dstArrayElement
* 4;
765 dst_offset
= binding_layout
->offset
/ 4 + binding_layout
->size
* entry
->dstArrayElement
/ 4;
766 dst_stride
= binding_layout
->size
/ 4;
770 templ
->entry
[i
] = (struct radv_descriptor_update_template_entry
) {
771 .descriptor_type
= entry
->descriptorType
,
772 .descriptor_count
= entry
->descriptorCount
,
773 .src_offset
= entry
->offset
,
774 .src_stride
= entry
->stride
,
775 .dst_offset
= dst_offset
,
776 .dst_stride
= dst_stride
,
777 .buffer_offset
= buffer_offset
,
778 .buffer_count
= binding_layout
->buffer_count
,
779 .has_sampler
= !binding_layout
->immutable_samplers_offset
,
780 .immutable_samplers
= immutable_samplers
784 *pDescriptorUpdateTemplate
= radv_descriptor_update_template_to_handle(templ
);
788 void radv_DestroyDescriptorUpdateTemplateKHR(VkDevice _device
,
789 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate
,
790 const VkAllocationCallbacks
*pAllocator
)
792 RADV_FROM_HANDLE(radv_device
, device
, _device
);
793 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
798 vk_free2(&device
->alloc
, pAllocator
, templ
);
801 void radv_update_descriptor_set_with_template(struct radv_device
*device
,
802 struct radv_cmd_buffer
*cmd_buffer
,
803 struct radv_descriptor_set
*set
,
804 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate
,
807 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
810 for (i
= 0; i
< templ
->entry_count
; ++i
) {
811 struct radeon_winsys_bo
**buffer_list
= set
->descriptors
+ templ
->entry
[i
].buffer_offset
;
812 uint32_t *pDst
= set
->mapped_ptr
+ templ
->entry
[i
].dst_offset
;
813 const uint8_t *pSrc
= ((const uint8_t *) pData
) + templ
->entry
[i
].src_offset
;
816 for (j
= 0; j
< templ
->entry
[i
].descriptor_count
; ++j
) {
817 switch (templ
->entry
[i
].descriptor_type
) {
818 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
819 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
820 const unsigned idx
= templ
->entry
[i
].dst_offset
+ j
;
821 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
822 write_dynamic_buffer_descriptor(device
, set
->dynamic_descriptors
+ idx
,
823 buffer_list
, (struct VkDescriptorBufferInfo
*) pSrc
);
826 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
827 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
828 write_buffer_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
829 (struct VkDescriptorBufferInfo
*) pSrc
);
831 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
832 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
833 write_texel_buffer_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
834 *(VkBufferView
*) pSrc
);
836 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
837 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
838 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
839 write_image_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
840 (struct VkDescriptorImageInfo
*) pSrc
);
842 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
843 write_combined_image_sampler_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
844 (struct VkDescriptorImageInfo
*) pSrc
,
845 templ
->entry
[i
].has_sampler
);
846 if (templ
->entry
[i
].immutable_samplers
)
847 memcpy(pDst
+ 16, templ
->entry
[i
].immutable_samplers
+ 4 * j
, 16);
849 case VK_DESCRIPTOR_TYPE_SAMPLER
:
850 if (templ
->entry
[i
].has_sampler
)
851 write_sampler_descriptor(device
, pDst
,
852 (struct VkDescriptorImageInfo
*) pSrc
);
853 else if (templ
->entry
[i
].immutable_samplers
)
854 memcpy(pDst
, templ
->entry
[i
].immutable_samplers
+ 4 * j
, 16);
857 unreachable("unimplemented descriptor type");
860 pSrc
+= templ
->entry
[i
].src_stride
;
861 pDst
+= templ
->entry
[i
].dst_stride
;
862 buffer_list
+= templ
->entry
[i
].buffer_count
;
867 void radv_UpdateDescriptorSetWithTemplateKHR(VkDevice _device
,
868 VkDescriptorSet descriptorSet
,
869 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate
,
872 RADV_FROM_HANDLE(radv_device
, device
, _device
);
873 RADV_FROM_HANDLE(radv_descriptor_set
, set
, descriptorSet
);
875 radv_update_descriptor_set_with_template(device
, NULL
, set
, descriptorUpdateTemplate
, pData
);