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
;
218 _mesa_sha1_update(&ctx
, set_layout
->binding
,
219 sizeof(set_layout
->binding
[0]) * set_layout
->binding_count
);
222 layout
->dynamic_offset_count
= dynamic_offset_count
;
223 layout
->push_constant_size
= 0;
224 for (unsigned i
= 0; i
< pCreateInfo
->pushConstantRangeCount
; ++i
) {
225 const VkPushConstantRange
*range
= pCreateInfo
->pPushConstantRanges
+ i
;
226 layout
->push_constant_size
= MAX2(layout
->push_constant_size
,
227 range
->offset
+ range
->size
);
230 layout
->push_constant_size
= align(layout
->push_constant_size
, 16);
231 _mesa_sha1_update(&ctx
, &layout
->push_constant_size
,
232 sizeof(layout
->push_constant_size
));
233 _mesa_sha1_final(&ctx
, layout
->sha1
);
234 *pPipelineLayout
= radv_pipeline_layout_to_handle(layout
);
239 void radv_DestroyPipelineLayout(
241 VkPipelineLayout _pipelineLayout
,
242 const VkAllocationCallbacks
* pAllocator
)
244 RADV_FROM_HANDLE(radv_device
, device
, _device
);
245 RADV_FROM_HANDLE(radv_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
247 if (!pipeline_layout
)
249 vk_free2(&device
->alloc
, pAllocator
, pipeline_layout
);
255 radv_descriptor_set_create(struct radv_device
*device
,
256 struct radv_descriptor_pool
*pool
,
257 struct radv_cmd_buffer
*cmd_buffer
,
258 const struct radv_descriptor_set_layout
*layout
,
259 struct radv_descriptor_set
**out_set
)
261 struct radv_descriptor_set
*set
;
262 unsigned mem_size
= sizeof(struct radv_descriptor_set
) +
263 sizeof(struct radeon_winsys_bo
*) * layout
->buffer_count
;
264 set
= vk_alloc2(&device
->alloc
, NULL
, mem_size
, 8,
265 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
268 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
270 memset(set
, 0, mem_size
);
272 if (layout
->dynamic_offset_count
) {
273 unsigned size
= sizeof(struct radv_descriptor_range
) *
274 layout
->dynamic_offset_count
;
275 set
->dynamic_descriptors
= vk_alloc2(&device
->alloc
, NULL
, size
, 8,
276 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
278 if (!set
->dynamic_descriptors
) {
279 vk_free2(&device
->alloc
, NULL
, set
);
280 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
284 set
->layout
= layout
;
286 uint32_t layout_size
= align_u32(layout
->size
, 32);
287 set
->size
= layout
->size
;
289 /* try to allocate linearly first, so that we don't spend
290 * time looking for gaps if the app only allocates &
291 * resets via the pool. */
292 if (pool
->current_offset
+ layout_size
<= pool
->size
) {
294 set
->mapped_ptr
= (uint32_t*)(pool
->mapped_ptr
+ pool
->current_offset
);
295 set
->va
= device
->ws
->buffer_get_va(set
->bo
) + pool
->current_offset
;
296 pool
->current_offset
+= layout_size
;
297 list_addtail(&set
->vram_list
, &pool
->vram_list
);
300 struct list_head
*prev
= &pool
->vram_list
;
301 struct radv_descriptor_set
*cur
;
302 LIST_FOR_EACH_ENTRY(cur
, &pool
->vram_list
, vram_list
) {
303 uint64_t start
= (uint8_t*)cur
->mapped_ptr
- pool
->mapped_ptr
;
304 if (start
- offset
>= layout_size
)
307 offset
= start
+ cur
->size
;
308 prev
= &cur
->vram_list
;
311 if (pool
->size
- offset
< layout_size
) {
312 vk_free2(&device
->alloc
, NULL
, set
->dynamic_descriptors
);
313 vk_free2(&device
->alloc
, NULL
, set
);
314 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY_KHR
);
317 set
->mapped_ptr
= (uint32_t*)(pool
->mapped_ptr
+ offset
);
318 set
->va
= device
->ws
->buffer_get_va(set
->bo
) + offset
;
319 list_add(&set
->vram_list
, prev
);
323 if (!radv_cmd_buffer_upload_alloc(cmd_buffer
, set
->size
, 32,
325 (void**)&set
->mapped_ptr
)) {
326 vk_free2(&device
->alloc
, NULL
, set
->dynamic_descriptors
);
327 vk_free2(&device
->alloc
, NULL
, set
);
328 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
331 set
->va
= device
->ws
->buffer_get_va(cmd_buffer
->upload
.upload_bo
);
332 set
->va
+= bo_offset
;
336 for (unsigned i
= 0; i
< layout
->binding_count
; ++i
) {
337 if (!layout
->binding
[i
].immutable_samplers_offset
||
338 layout
->binding
[i
].immutable_samplers_equal
)
341 unsigned offset
= layout
->binding
[i
].offset
/ 4;
342 if (layout
->binding
[i
].type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
345 const uint32_t *samplers
= (const uint32_t*)((const char*)layout
+ layout
->binding
[i
].immutable_samplers_offset
);
346 for (unsigned j
= 0; j
< layout
->binding
[i
].array_size
; ++j
) {
347 memcpy(set
->mapped_ptr
+ offset
, samplers
+ 4 * j
, 16);
348 offset
+= layout
->binding
[i
].size
/ 4;
357 radv_descriptor_set_destroy(struct radv_device
*device
,
358 struct radv_descriptor_pool
*pool
,
359 struct radv_descriptor_set
*set
,
362 if (free_bo
&& set
->size
)
363 list_del(&set
->vram_list
);
364 if (set
->dynamic_descriptors
)
365 vk_free2(&device
->alloc
, NULL
, set
->dynamic_descriptors
);
366 vk_free2(&device
->alloc
, NULL
, set
);
370 radv_temp_descriptor_set_create(struct radv_device
*device
,
371 struct radv_cmd_buffer
*cmd_buffer
,
372 VkDescriptorSetLayout _layout
,
373 VkDescriptorSet
*_set
)
375 RADV_FROM_HANDLE(radv_descriptor_set_layout
, layout
, _layout
);
376 struct radv_descriptor_set
*set
= NULL
;
379 ret
= radv_descriptor_set_create(device
, NULL
, cmd_buffer
, layout
, &set
);
380 *_set
= radv_descriptor_set_to_handle(set
);
385 radv_temp_descriptor_set_destroy(struct radv_device
*device
,
386 VkDescriptorSet _set
)
388 RADV_FROM_HANDLE(radv_descriptor_set
, set
, _set
);
390 radv_descriptor_set_destroy(device
, NULL
, set
, false);
393 VkResult
radv_CreateDescriptorPool(
395 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
396 const VkAllocationCallbacks
* pAllocator
,
397 VkDescriptorPool
* pDescriptorPool
)
399 RADV_FROM_HANDLE(radv_device
, device
, _device
);
400 struct radv_descriptor_pool
*pool
;
401 int size
= sizeof(struct radv_descriptor_pool
);
402 uint64_t bo_size
= 0;
403 pool
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
404 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
406 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
408 memset(pool
, 0, sizeof(*pool
));
410 for (unsigned i
= 0; i
< pCreateInfo
->poolSizeCount
; ++i
) {
411 switch(pCreateInfo
->pPoolSizes
[i
].type
) {
412 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
413 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
415 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
416 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
417 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
418 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
419 case VK_DESCRIPTOR_TYPE_SAMPLER
:
420 /* 32 as we may need to align for images */
421 bo_size
+= 32 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
423 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
424 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
425 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
426 bo_size
+= 64 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
428 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
429 bo_size
+= 96 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
432 unreachable("unknown descriptor type\n");
438 pool
->bo
= device
->ws
->buffer_create(device
->ws
, bo_size
,
439 32, RADEON_DOMAIN_VRAM
, 0);
440 pool
->mapped_ptr
= (uint8_t*)device
->ws
->buffer_map(pool
->bo
);
442 pool
->size
= bo_size
;
444 list_inithead(&pool
->vram_list
);
445 *pDescriptorPool
= radv_descriptor_pool_to_handle(pool
);
449 void radv_DestroyDescriptorPool(
451 VkDescriptorPool _pool
,
452 const VkAllocationCallbacks
* pAllocator
)
454 RADV_FROM_HANDLE(radv_device
, device
, _device
);
455 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, _pool
);
460 list_for_each_entry_safe(struct radv_descriptor_set
, set
,
461 &pool
->vram_list
, vram_list
) {
462 radv_descriptor_set_destroy(device
, pool
, set
, false);
466 device
->ws
->buffer_destroy(pool
->bo
);
467 vk_free2(&device
->alloc
, pAllocator
, pool
);
470 VkResult
radv_ResetDescriptorPool(
472 VkDescriptorPool descriptorPool
,
473 VkDescriptorPoolResetFlags flags
)
475 RADV_FROM_HANDLE(radv_device
, device
, _device
);
476 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, descriptorPool
);
478 list_for_each_entry_safe(struct radv_descriptor_set
, set
,
479 &pool
->vram_list
, vram_list
) {
480 radv_descriptor_set_destroy(device
, pool
, set
, false);
483 list_inithead(&pool
->vram_list
);
485 pool
->current_offset
= 0;
490 VkResult
radv_AllocateDescriptorSets(
492 const VkDescriptorSetAllocateInfo
* pAllocateInfo
,
493 VkDescriptorSet
* pDescriptorSets
)
495 RADV_FROM_HANDLE(radv_device
, device
, _device
);
496 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, pAllocateInfo
->descriptorPool
);
498 VkResult result
= VK_SUCCESS
;
500 struct radv_descriptor_set
*set
;
502 /* allocate a set of buffers for each shader to contain descriptors */
503 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
504 RADV_FROM_HANDLE(radv_descriptor_set_layout
, layout
,
505 pAllocateInfo
->pSetLayouts
[i
]);
507 assert(!(layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
509 result
= radv_descriptor_set_create(device
, pool
, NULL
, layout
, &set
);
510 if (result
!= VK_SUCCESS
)
513 pDescriptorSets
[i
] = radv_descriptor_set_to_handle(set
);
516 if (result
!= VK_SUCCESS
)
517 radv_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
522 VkResult
radv_FreeDescriptorSets(
524 VkDescriptorPool descriptorPool
,
526 const VkDescriptorSet
* pDescriptorSets
)
528 RADV_FROM_HANDLE(radv_device
, device
, _device
);
529 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, descriptorPool
);
531 for (uint32_t i
= 0; i
< count
; i
++) {
532 RADV_FROM_HANDLE(radv_descriptor_set
, set
, pDescriptorSets
[i
]);
535 radv_descriptor_set_destroy(device
, pool
, set
, true);
540 static void write_texel_buffer_descriptor(struct radv_device
*device
,
541 struct radv_cmd_buffer
*cmd_buffer
,
543 struct radeon_winsys_bo
**buffer_list
,
544 const VkBufferView _buffer_view
)
546 RADV_FROM_HANDLE(radv_buffer_view
, buffer_view
, _buffer_view
);
548 memcpy(dst
, buffer_view
->state
, 4 * 4);
551 device
->ws
->cs_add_buffer(cmd_buffer
->cs
, buffer_view
->bo
, 7);
553 *buffer_list
= buffer_view
->bo
;
556 static void write_buffer_descriptor(struct radv_device
*device
,
557 struct radv_cmd_buffer
*cmd_buffer
,
559 struct radeon_winsys_bo
**buffer_list
,
560 const VkDescriptorBufferInfo
*buffer_info
)
562 RADV_FROM_HANDLE(radv_buffer
, buffer
, buffer_info
->buffer
);
563 uint64_t va
= device
->ws
->buffer_get_va(buffer
->bo
);
564 uint32_t range
= buffer_info
->range
;
566 if (buffer_info
->range
== VK_WHOLE_SIZE
)
567 range
= buffer
->size
- buffer_info
->offset
;
569 va
+= buffer_info
->offset
+ buffer
->offset
;
571 dst
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32);
573 dst
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
574 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
575 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
576 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
577 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
578 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
581 device
->ws
->cs_add_buffer(cmd_buffer
->cs
, buffer
->bo
, 7);
583 *buffer_list
= buffer
->bo
;
586 static void write_dynamic_buffer_descriptor(struct radv_device
*device
,
587 struct radv_descriptor_range
*range
,
588 struct radeon_winsys_bo
**buffer_list
,
589 const VkDescriptorBufferInfo
*buffer_info
)
591 RADV_FROM_HANDLE(radv_buffer
, buffer
, buffer_info
->buffer
);
592 uint64_t va
= device
->ws
->buffer_get_va(buffer
->bo
);
593 unsigned size
= buffer_info
->range
;
595 if (buffer_info
->range
== VK_WHOLE_SIZE
)
596 size
= buffer
->size
- buffer_info
->offset
;
598 va
+= buffer_info
->offset
+ buffer
->offset
;
602 *buffer_list
= buffer
->bo
;
606 write_image_descriptor(struct radv_device
*device
,
607 struct radv_cmd_buffer
*cmd_buffer
,
609 struct radeon_winsys_bo
**buffer_list
,
610 const VkDescriptorImageInfo
*image_info
)
612 RADV_FROM_HANDLE(radv_image_view
, iview
, image_info
->imageView
);
613 memcpy(dst
, iview
->descriptor
, 8 * 4);
614 memcpy(dst
+ 8, iview
->fmask_descriptor
, 8 * 4);
617 device
->ws
->cs_add_buffer(cmd_buffer
->cs
, iview
->bo
, 7);
619 *buffer_list
= iview
->bo
;
623 write_combined_image_sampler_descriptor(struct radv_device
*device
,
624 struct radv_cmd_buffer
*cmd_buffer
,
626 struct radeon_winsys_bo
**buffer_list
,
627 const VkDescriptorImageInfo
*image_info
,
630 RADV_FROM_HANDLE(radv_sampler
, sampler
, image_info
->sampler
);
632 write_image_descriptor(device
, cmd_buffer
, dst
, buffer_list
, image_info
);
633 /* copy over sampler state */
635 memcpy(dst
+ 16, sampler
->state
, 16);
639 write_sampler_descriptor(struct radv_device
*device
,
641 const VkDescriptorImageInfo
*image_info
)
643 RADV_FROM_HANDLE(radv_sampler
, sampler
, image_info
->sampler
);
645 memcpy(dst
, sampler
->state
, 16);
648 void radv_update_descriptor_sets(
649 struct radv_device
* device
,
650 struct radv_cmd_buffer
* cmd_buffer
,
651 VkDescriptorSet dstSetOverride
,
652 uint32_t descriptorWriteCount
,
653 const VkWriteDescriptorSet
* pDescriptorWrites
,
654 uint32_t descriptorCopyCount
,
655 const VkCopyDescriptorSet
* pDescriptorCopies
)
658 for (i
= 0; i
< descriptorWriteCount
; i
++) {
659 const VkWriteDescriptorSet
*writeset
= &pDescriptorWrites
[i
];
660 RADV_FROM_HANDLE(radv_descriptor_set
, set
,
661 dstSetOverride
? dstSetOverride
: writeset
->dstSet
);
662 const struct radv_descriptor_set_binding_layout
*binding_layout
=
663 set
->layout
->binding
+ writeset
->dstBinding
;
664 uint32_t *ptr
= set
->mapped_ptr
;
665 struct radeon_winsys_bo
**buffer_list
= set
->descriptors
;
666 /* Immutable samplers are not copied into push descriptors when they are
667 * allocated, so if we are writing push descriptors we have to copy the
668 * immutable samplers into them now.
670 const bool copy_immutable_samplers
= cmd_buffer
&&
671 binding_layout
->immutable_samplers_offset
&& !binding_layout
->immutable_samplers_equal
;
672 const uint32_t *samplers
= radv_immutable_samplers(set
->layout
, binding_layout
);
674 ptr
+= binding_layout
->offset
/ 4;
675 ptr
+= binding_layout
->size
* writeset
->dstArrayElement
/ 4;
676 buffer_list
+= binding_layout
->buffer_offset
;
677 buffer_list
+= binding_layout
->buffer_count
* writeset
->dstArrayElement
;
678 for (j
= 0; j
< writeset
->descriptorCount
; ++j
) {
679 switch(writeset
->descriptorType
) {
680 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
681 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
682 unsigned idx
= writeset
->dstArrayElement
+ j
;
683 idx
+= binding_layout
->dynamic_offset_offset
;
684 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
685 write_dynamic_buffer_descriptor(device
, set
->dynamic_descriptors
+ idx
,
686 buffer_list
, writeset
->pBufferInfo
+ j
);
689 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
690 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
691 write_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
692 writeset
->pBufferInfo
+ j
);
694 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
695 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
696 write_texel_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
697 writeset
->pTexelBufferView
[j
]);
699 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
700 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
701 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
702 write_image_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
703 writeset
->pImageInfo
+ j
);
705 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
706 write_combined_image_sampler_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
707 writeset
->pImageInfo
+ j
,
708 !binding_layout
->immutable_samplers_offset
);
709 if (copy_immutable_samplers
) {
710 const unsigned idx
= writeset
->dstArrayElement
+ j
;
711 memcpy(ptr
+ 16, samplers
+ 4 * idx
, 16);
714 case VK_DESCRIPTOR_TYPE_SAMPLER
:
715 if (!binding_layout
->immutable_samplers_offset
) {
716 write_sampler_descriptor(device
, ptr
,
717 writeset
->pImageInfo
+ j
);
718 } else if (copy_immutable_samplers
) {
719 unsigned idx
= writeset
->dstArrayElement
+ j
;
720 memcpy(ptr
, samplers
+ 4 * idx
, 16);
724 unreachable("unimplemented descriptor type");
727 ptr
+= binding_layout
->size
/ 4;
728 buffer_list
+= binding_layout
->buffer_count
;
732 if (descriptorCopyCount
)
733 radv_finishme("copy descriptors");
736 void radv_UpdateDescriptorSets(
738 uint32_t descriptorWriteCount
,
739 const VkWriteDescriptorSet
* pDescriptorWrites
,
740 uint32_t descriptorCopyCount
,
741 const VkCopyDescriptorSet
* pDescriptorCopies
)
743 RADV_FROM_HANDLE(radv_device
, device
, _device
);
745 radv_update_descriptor_sets(device
, NULL
, VK_NULL_HANDLE
, descriptorWriteCount
, pDescriptorWrites
,
746 descriptorCopyCount
, pDescriptorCopies
);
749 VkResult
radv_CreateDescriptorUpdateTemplateKHR(VkDevice _device
,
750 const VkDescriptorUpdateTemplateCreateInfoKHR
*pCreateInfo
,
751 const VkAllocationCallbacks
*pAllocator
,
752 VkDescriptorUpdateTemplateKHR
*pDescriptorUpdateTemplate
)
754 RADV_FROM_HANDLE(radv_device
, device
, _device
);
755 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
, pCreateInfo
->descriptorSetLayout
);
756 const uint32_t entry_count
= pCreateInfo
->descriptorUpdateEntryCount
;
757 const size_t size
= sizeof(struct radv_descriptor_update_template
) +
758 sizeof(struct radv_descriptor_update_template_entry
) * entry_count
;
759 struct radv_descriptor_update_template
*templ
;
762 templ
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
764 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
766 templ
->entry_count
= entry_count
;
768 for (i
= 0; i
< entry_count
; i
++) {
769 const VkDescriptorUpdateTemplateEntryKHR
*entry
= &pCreateInfo
->pDescriptorUpdateEntries
[i
];
770 const struct radv_descriptor_set_binding_layout
*binding_layout
=
771 set_layout
->binding
+ entry
->dstBinding
;
772 const uint32_t buffer_offset
= binding_layout
->buffer_offset
+
773 binding_layout
->buffer_count
* entry
->dstArrayElement
;
774 const uint32_t *immutable_samplers
= NULL
;
778 /* dst_offset is an offset into dynamic_descriptors when the descriptor
779 is dynamic, and an offset into mapped_ptr otherwise */
780 switch (entry
->descriptorType
) {
781 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
782 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
783 assert(pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET_KHR
);
784 dst_offset
= binding_layout
->dynamic_offset_offset
+ entry
->dstArrayElement
;
785 dst_stride
= 0; /* Not used */
788 switch (entry
->descriptorType
) {
789 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
790 case VK_DESCRIPTOR_TYPE_SAMPLER
:
791 /* Immutable samplers are copied into push descriptors when they are pushed */
792 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR
&&
793 binding_layout
->immutable_samplers_offset
&& !binding_layout
->immutable_samplers_equal
) {
794 immutable_samplers
= radv_immutable_samplers(set_layout
, binding_layout
) + entry
->dstArrayElement
* 4;
800 dst_offset
= binding_layout
->offset
/ 4 + binding_layout
->size
* entry
->dstArrayElement
/ 4;
801 dst_stride
= binding_layout
->size
/ 4;
805 templ
->entry
[i
] = (struct radv_descriptor_update_template_entry
) {
806 .descriptor_type
= entry
->descriptorType
,
807 .descriptor_count
= entry
->descriptorCount
,
808 .src_offset
= entry
->offset
,
809 .src_stride
= entry
->stride
,
810 .dst_offset
= dst_offset
,
811 .dst_stride
= dst_stride
,
812 .buffer_offset
= buffer_offset
,
813 .buffer_count
= binding_layout
->buffer_count
,
814 .has_sampler
= !binding_layout
->immutable_samplers_offset
,
815 .immutable_samplers
= immutable_samplers
819 *pDescriptorUpdateTemplate
= radv_descriptor_update_template_to_handle(templ
);
823 void radv_DestroyDescriptorUpdateTemplateKHR(VkDevice _device
,
824 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate
,
825 const VkAllocationCallbacks
*pAllocator
)
827 RADV_FROM_HANDLE(radv_device
, device
, _device
);
828 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
833 vk_free2(&device
->alloc
, pAllocator
, templ
);
836 void radv_update_descriptor_set_with_template(struct radv_device
*device
,
837 struct radv_cmd_buffer
*cmd_buffer
,
838 struct radv_descriptor_set
*set
,
839 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate
,
842 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
845 for (i
= 0; i
< templ
->entry_count
; ++i
) {
846 struct radeon_winsys_bo
**buffer_list
= set
->descriptors
+ templ
->entry
[i
].buffer_offset
;
847 uint32_t *pDst
= set
->mapped_ptr
+ templ
->entry
[i
].dst_offset
;
848 const uint8_t *pSrc
= ((const uint8_t *) pData
) + templ
->entry
[i
].src_offset
;
851 for (j
= 0; j
< templ
->entry
[i
].descriptor_count
; ++j
) {
852 switch (templ
->entry
[i
].descriptor_type
) {
853 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
854 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
855 const unsigned idx
= templ
->entry
[i
].dst_offset
+ j
;
856 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
857 write_dynamic_buffer_descriptor(device
, set
->dynamic_descriptors
+ idx
,
858 buffer_list
, (struct VkDescriptorBufferInfo
*) pSrc
);
861 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
862 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
863 write_buffer_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
864 (struct VkDescriptorBufferInfo
*) pSrc
);
866 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
867 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
868 write_texel_buffer_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
869 *(VkBufferView
*) pSrc
);
871 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
872 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
873 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
874 write_image_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
875 (struct VkDescriptorImageInfo
*) pSrc
);
877 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
878 write_combined_image_sampler_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
879 (struct VkDescriptorImageInfo
*) pSrc
,
880 templ
->entry
[i
].has_sampler
);
881 if (templ
->entry
[i
].immutable_samplers
)
882 memcpy(pDst
+ 16, templ
->entry
[i
].immutable_samplers
+ 4 * j
, 16);
884 case VK_DESCRIPTOR_TYPE_SAMPLER
:
885 if (templ
->entry
[i
].has_sampler
)
886 write_sampler_descriptor(device
, pDst
,
887 (struct VkDescriptorImageInfo
*) pSrc
);
888 else if (templ
->entry
[i
].immutable_samplers
)
889 memcpy(pDst
, templ
->entry
[i
].immutable_samplers
+ 4 * j
, 16);
892 unreachable("unimplemented descriptor type");
895 pSrc
+= templ
->entry
[i
].src_stride
;
896 pDst
+= templ
->entry
[i
].dst_stride
;
897 buffer_list
+= templ
->entry
[i
].buffer_count
;
902 void radv_UpdateDescriptorSetWithTemplateKHR(VkDevice _device
,
903 VkDescriptorSet descriptorSet
,
904 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate
,
907 RADV_FROM_HANDLE(radv_device
, device
, _device
);
908 RADV_FROM_HANDLE(radv_descriptor_set
, set
, descriptorSet
);
910 radv_update_descriptor_set_with_template(device
, NULL
, set
, descriptorUpdateTemplate
, pData
);