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
;
80 unsigned binding_buffer_count
= 0;
82 switch (binding
->descriptorType
) {
83 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
84 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
85 assert(!(pCreateInfo
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
86 set_layout
->binding
[b
].dynamic_offset_count
= 1;
87 set_layout
->dynamic_shader_stages
|= binding
->stageFlags
;
88 set_layout
->binding
[b
].size
= 0;
89 binding_buffer_count
= 1;
92 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
93 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
94 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
95 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
96 set_layout
->binding
[b
].size
= 16;
97 binding_buffer_count
= 1;
100 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
101 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
102 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
103 /* main descriptor + fmask descriptor */
104 set_layout
->binding
[b
].size
= 64;
105 binding_buffer_count
= 1;
108 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
109 /* main descriptor + fmask descriptor + sampler */
110 set_layout
->binding
[b
].size
= 96;
111 binding_buffer_count
= 1;
114 case VK_DESCRIPTOR_TYPE_SAMPLER
:
115 set_layout
->binding
[b
].size
= 16;
119 unreachable("unknown descriptor type\n");
123 set_layout
->size
= align(set_layout
->size
, alignment
);
124 assert(binding
->descriptorCount
> 0);
125 set_layout
->binding
[b
].type
= binding
->descriptorType
;
126 set_layout
->binding
[b
].array_size
= binding
->descriptorCount
;
127 set_layout
->binding
[b
].offset
= set_layout
->size
;
128 set_layout
->binding
[b
].buffer_offset
= buffer_count
;
129 set_layout
->binding
[b
].dynamic_offset_offset
= dynamic_offset_count
;
131 if (binding
->pImmutableSamplers
) {
132 set_layout
->binding
[b
].immutable_samplers_offset
= samplers_offset
;
133 set_layout
->binding
[b
].immutable_samplers_equal
= true;
136 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++)
137 memcpy(samplers
+ 4 * i
, &radv_sampler_from_handle(binding
->pImmutableSamplers
[i
])->state
, 16);
138 for (uint32_t i
= 1; i
< binding
->descriptorCount
; i
++)
139 if (memcmp(samplers
+ 4 * i
, samplers
, 16) != 0)
140 set_layout
->binding
[b
].immutable_samplers_equal
= false;
142 /* Don't reserve space for the samplers if they're not accessed. */
143 if (set_layout
->binding
[b
].immutable_samplers_equal
) {
144 if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
145 set_layout
->binding
[b
].size
-= 32;
146 else if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_SAMPLER
)
147 set_layout
->binding
[b
].size
-= 16;
149 samplers
+= 4 * binding
->descriptorCount
;
150 samplers_offset
+= 4 * sizeof(uint32_t) * binding
->descriptorCount
;
153 set_layout
->size
+= binding
->descriptorCount
* set_layout
->binding
[b
].size
;
154 buffer_count
+= binding
->descriptorCount
* binding_buffer_count
;
155 dynamic_offset_count
+= binding
->descriptorCount
*
156 set_layout
->binding
[b
].dynamic_offset_count
;
157 set_layout
->shader_stages
|= binding
->stageFlags
;
160 set_layout
->buffer_count
= buffer_count
;
161 set_layout
->dynamic_offset_count
= dynamic_offset_count
;
163 *pSetLayout
= radv_descriptor_set_layout_to_handle(set_layout
);
168 void radv_DestroyDescriptorSetLayout(
170 VkDescriptorSetLayout _set_layout
,
171 const VkAllocationCallbacks
* pAllocator
)
173 RADV_FROM_HANDLE(radv_device
, device
, _device
);
174 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
, _set_layout
);
179 vk_free2(&device
->alloc
, pAllocator
, set_layout
);
183 * Pipeline layouts. These have nothing to do with the pipeline. They are
184 * just muttiple descriptor set layouts pasted together
187 VkResult
radv_CreatePipelineLayout(
189 const VkPipelineLayoutCreateInfo
* pCreateInfo
,
190 const VkAllocationCallbacks
* pAllocator
,
191 VkPipelineLayout
* pPipelineLayout
)
193 RADV_FROM_HANDLE(radv_device
, device
, _device
);
194 struct radv_pipeline_layout
*layout
;
195 struct mesa_sha1 ctx
;
197 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
);
199 layout
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*layout
), 8,
200 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
202 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
204 layout
->num_sets
= pCreateInfo
->setLayoutCount
;
206 unsigned dynamic_offset_count
= 0;
209 _mesa_sha1_init(&ctx
);
210 for (uint32_t set
= 0; set
< pCreateInfo
->setLayoutCount
; set
++) {
211 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
,
212 pCreateInfo
->pSetLayouts
[set
]);
213 layout
->set
[set
].layout
= set_layout
;
215 layout
->set
[set
].dynamic_offset_start
= dynamic_offset_count
;
216 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
217 dynamic_offset_count
+= set_layout
->binding
[b
].array_size
* set_layout
->binding
[b
].dynamic_offset_count
;
218 if (set_layout
->binding
[b
].immutable_samplers_offset
)
219 _mesa_sha1_update(&ctx
, radv_immutable_samplers(set_layout
, set_layout
->binding
+ b
),
220 set_layout
->binding
[b
].array_size
* 4 * sizeof(uint32_t));
222 _mesa_sha1_update(&ctx
, set_layout
->binding
,
223 sizeof(set_layout
->binding
[0]) * set_layout
->binding_count
);
226 layout
->dynamic_offset_count
= dynamic_offset_count
;
227 layout
->push_constant_size
= 0;
228 for (unsigned i
= 0; i
< pCreateInfo
->pushConstantRangeCount
; ++i
) {
229 const VkPushConstantRange
*range
= pCreateInfo
->pPushConstantRanges
+ i
;
230 layout
->push_constant_size
= MAX2(layout
->push_constant_size
,
231 range
->offset
+ range
->size
);
234 layout
->push_constant_size
= align(layout
->push_constant_size
, 16);
235 _mesa_sha1_update(&ctx
, &layout
->push_constant_size
,
236 sizeof(layout
->push_constant_size
));
237 _mesa_sha1_final(&ctx
, layout
->sha1
);
238 *pPipelineLayout
= radv_pipeline_layout_to_handle(layout
);
243 void radv_DestroyPipelineLayout(
245 VkPipelineLayout _pipelineLayout
,
246 const VkAllocationCallbacks
* pAllocator
)
248 RADV_FROM_HANDLE(radv_device
, device
, _device
);
249 RADV_FROM_HANDLE(radv_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
251 if (!pipeline_layout
)
253 vk_free2(&device
->alloc
, pAllocator
, pipeline_layout
);
259 radv_descriptor_set_create(struct radv_device
*device
,
260 struct radv_descriptor_pool
*pool
,
261 const struct radv_descriptor_set_layout
*layout
,
262 struct radv_descriptor_set
**out_set
)
264 struct radv_descriptor_set
*set
;
265 unsigned range_offset
= sizeof(struct radv_descriptor_set
) +
266 sizeof(struct radeon_winsys_bo
*) * layout
->buffer_count
;
267 unsigned mem_size
= range_offset
+
268 sizeof(struct radv_descriptor_range
) * layout
->dynamic_offset_count
;
270 if (pool
->host_memory_base
) {
271 if (pool
->host_memory_end
- pool
->host_memory_ptr
< mem_size
)
272 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY_KHR
);
274 set
= (struct radv_descriptor_set
*)pool
->host_memory_ptr
;
275 pool
->host_memory_ptr
+= mem_size
;
277 set
= vk_alloc2(&device
->alloc
, NULL
, mem_size
, 8,
278 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
281 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
284 memset(set
, 0, mem_size
);
286 if (layout
->dynamic_offset_count
) {
287 set
->dynamic_descriptors
= (struct radv_descriptor_range
*)((uint8_t*)set
+ range_offset
);
290 set
->layout
= layout
;
292 uint32_t layout_size
= align_u32(layout
->size
, 32);
293 set
->size
= layout
->size
;
295 /* try to allocate linearly first, so that we don't spend
296 * time looking for gaps if the app only allocates &
297 * resets via the pool. */
298 if (pool
->current_offset
+ layout_size
<= pool
->size
) {
300 set
->mapped_ptr
= (uint32_t*)(pool
->mapped_ptr
+ pool
->current_offset
);
301 set
->va
= device
->ws
->buffer_get_va(set
->bo
) + pool
->current_offset
;
302 pool
->current_offset
+= layout_size
;
303 list_addtail(&set
->vram_list
, &pool
->vram_list
);
304 } else if (!pool
->host_memory_base
) {
306 struct list_head
*prev
= &pool
->vram_list
;
307 struct radv_descriptor_set
*cur
;
309 assert(!pool
->host_memory_base
);
310 LIST_FOR_EACH_ENTRY(cur
, &pool
->vram_list
, vram_list
) {
311 uint64_t start
= (uint8_t*)cur
->mapped_ptr
- pool
->mapped_ptr
;
312 if (start
- offset
>= layout_size
)
315 offset
= start
+ cur
->size
;
316 prev
= &cur
->vram_list
;
319 if (pool
->size
- offset
< layout_size
) {
320 vk_free2(&device
->alloc
, NULL
, set
);
321 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY_KHR
);
324 set
->mapped_ptr
= (uint32_t*)(pool
->mapped_ptr
+ offset
);
325 set
->va
= device
->ws
->buffer_get_va(set
->bo
) + offset
;
326 list_add(&set
->vram_list
, prev
);
328 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY_KHR
);
331 for (unsigned i
= 0; i
< layout
->binding_count
; ++i
) {
332 if (!layout
->binding
[i
].immutable_samplers_offset
||
333 layout
->binding
[i
].immutable_samplers_equal
)
336 unsigned offset
= layout
->binding
[i
].offset
/ 4;
337 if (layout
->binding
[i
].type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
340 const uint32_t *samplers
= (const uint32_t*)((const char*)layout
+ layout
->binding
[i
].immutable_samplers_offset
);
341 for (unsigned j
= 0; j
< layout
->binding
[i
].array_size
; ++j
) {
342 memcpy(set
->mapped_ptr
+ offset
, samplers
+ 4 * j
, 16);
343 offset
+= layout
->binding
[i
].size
/ 4;
352 radv_descriptor_set_destroy(struct radv_device
*device
,
353 struct radv_descriptor_pool
*pool
,
354 struct radv_descriptor_set
*set
,
357 assert(!pool
->host_memory_base
);
359 if (free_bo
&& set
->size
)
360 list_del(&set
->vram_list
);
361 vk_free2(&device
->alloc
, NULL
, set
);
364 VkResult
radv_CreateDescriptorPool(
366 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
367 const VkAllocationCallbacks
* pAllocator
,
368 VkDescriptorPool
* pDescriptorPool
)
370 RADV_FROM_HANDLE(radv_device
, device
, _device
);
371 struct radv_descriptor_pool
*pool
;
372 int size
= sizeof(struct radv_descriptor_pool
);
373 uint64_t bo_size
= 0, bo_count
= 0, range_count
= 0;
376 for (unsigned i
= 0; i
< pCreateInfo
->poolSizeCount
; ++i
) {
377 if (pCreateInfo
->pPoolSizes
[i
].type
!= VK_DESCRIPTOR_TYPE_SAMPLER
)
378 bo_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
380 switch(pCreateInfo
->pPoolSizes
[i
].type
) {
381 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
382 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
383 range_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
385 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
386 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
387 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
388 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
389 case VK_DESCRIPTOR_TYPE_SAMPLER
:
390 /* 32 as we may need to align for images */
391 bo_size
+= 32 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
393 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
394 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
395 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
396 bo_size
+= 64 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
398 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
399 bo_size
+= 96 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
402 unreachable("unknown descriptor type\n");
407 if (!(pCreateInfo
->flags
& VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT
)) {
408 uint64_t host_size
= pCreateInfo
->maxSets
* sizeof(struct radv_descriptor_set
);
409 host_size
+= sizeof(struct radeon_winsys_bo
*) * bo_count
;
410 host_size
+= sizeof(struct radv_descriptor_range
) * range_count
;
414 pool
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
415 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
417 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
419 memset(pool
, 0, sizeof(*pool
));
421 if (!(pCreateInfo
->flags
& VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT
)) {
422 pool
->host_memory_base
= (uint8_t*)pool
+ sizeof(struct radv_descriptor_pool
);
423 pool
->host_memory_ptr
= pool
->host_memory_base
;
424 pool
->host_memory_end
= (uint8_t*)pool
+ size
;
428 pool
->bo
= device
->ws
->buffer_create(device
->ws
, bo_size
,
429 32, RADEON_DOMAIN_VRAM
, 0);
430 pool
->mapped_ptr
= (uint8_t*)device
->ws
->buffer_map(pool
->bo
);
432 pool
->size
= bo_size
;
434 list_inithead(&pool
->vram_list
);
435 *pDescriptorPool
= radv_descriptor_pool_to_handle(pool
);
439 void radv_DestroyDescriptorPool(
441 VkDescriptorPool _pool
,
442 const VkAllocationCallbacks
* pAllocator
)
444 RADV_FROM_HANDLE(radv_device
, device
, _device
);
445 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, _pool
);
450 if (!pool
->host_memory_base
) {
451 list_for_each_entry_safe(struct radv_descriptor_set
, set
,
452 &pool
->vram_list
, vram_list
) {
453 radv_descriptor_set_destroy(device
, pool
, set
, false);
458 device
->ws
->buffer_destroy(pool
->bo
);
459 vk_free2(&device
->alloc
, pAllocator
, pool
);
462 VkResult
radv_ResetDescriptorPool(
464 VkDescriptorPool descriptorPool
,
465 VkDescriptorPoolResetFlags flags
)
467 RADV_FROM_HANDLE(radv_device
, device
, _device
);
468 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, descriptorPool
);
470 if (!pool
->host_memory_base
) {
471 list_for_each_entry_safe(struct radv_descriptor_set
, set
,
472 &pool
->vram_list
, vram_list
) {
473 radv_descriptor_set_destroy(device
, pool
, set
, false);
477 list_inithead(&pool
->vram_list
);
479 pool
->current_offset
= 0;
480 pool
->host_memory_ptr
= pool
->host_memory_base
;
485 VkResult
radv_AllocateDescriptorSets(
487 const VkDescriptorSetAllocateInfo
* pAllocateInfo
,
488 VkDescriptorSet
* pDescriptorSets
)
490 RADV_FROM_HANDLE(radv_device
, device
, _device
);
491 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, pAllocateInfo
->descriptorPool
);
493 VkResult result
= VK_SUCCESS
;
495 struct radv_descriptor_set
*set
;
497 /* allocate a set of buffers for each shader to contain descriptors */
498 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
499 RADV_FROM_HANDLE(radv_descriptor_set_layout
, layout
,
500 pAllocateInfo
->pSetLayouts
[i
]);
502 assert(!(layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
504 result
= radv_descriptor_set_create(device
, pool
, layout
, &set
);
505 if (result
!= VK_SUCCESS
)
508 pDescriptorSets
[i
] = radv_descriptor_set_to_handle(set
);
511 if (result
!= VK_SUCCESS
)
512 radv_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
517 VkResult
radv_FreeDescriptorSets(
519 VkDescriptorPool descriptorPool
,
521 const VkDescriptorSet
* pDescriptorSets
)
523 RADV_FROM_HANDLE(radv_device
, device
, _device
);
524 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, descriptorPool
);
526 for (uint32_t i
= 0; i
< count
; i
++) {
527 RADV_FROM_HANDLE(radv_descriptor_set
, set
, pDescriptorSets
[i
]);
529 if (set
&& !pool
->host_memory_base
)
530 radv_descriptor_set_destroy(device
, pool
, set
, true);
535 static void write_texel_buffer_descriptor(struct radv_device
*device
,
536 struct radv_cmd_buffer
*cmd_buffer
,
538 struct radeon_winsys_bo
**buffer_list
,
539 const VkBufferView _buffer_view
)
541 RADV_FROM_HANDLE(radv_buffer_view
, buffer_view
, _buffer_view
);
543 memcpy(dst
, buffer_view
->state
, 4 * 4);
546 device
->ws
->cs_add_buffer(cmd_buffer
->cs
, buffer_view
->bo
, 7);
548 *buffer_list
= buffer_view
->bo
;
551 static void write_buffer_descriptor(struct radv_device
*device
,
552 struct radv_cmd_buffer
*cmd_buffer
,
554 struct radeon_winsys_bo
**buffer_list
,
555 const VkDescriptorBufferInfo
*buffer_info
)
557 RADV_FROM_HANDLE(radv_buffer
, buffer
, buffer_info
->buffer
);
558 uint64_t va
= device
->ws
->buffer_get_va(buffer
->bo
);
559 uint32_t range
= buffer_info
->range
;
561 if (buffer_info
->range
== VK_WHOLE_SIZE
)
562 range
= buffer
->size
- buffer_info
->offset
;
564 va
+= buffer_info
->offset
+ buffer
->offset
;
566 dst
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32);
568 dst
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
569 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
570 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
571 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
572 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
573 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
576 device
->ws
->cs_add_buffer(cmd_buffer
->cs
, buffer
->bo
, 7);
578 *buffer_list
= buffer
->bo
;
581 static void write_dynamic_buffer_descriptor(struct radv_device
*device
,
582 struct radv_descriptor_range
*range
,
583 struct radeon_winsys_bo
**buffer_list
,
584 const VkDescriptorBufferInfo
*buffer_info
)
586 RADV_FROM_HANDLE(radv_buffer
, buffer
, buffer_info
->buffer
);
587 uint64_t va
= device
->ws
->buffer_get_va(buffer
->bo
);
588 unsigned size
= buffer_info
->range
;
590 if (buffer_info
->range
== VK_WHOLE_SIZE
)
591 size
= buffer
->size
- buffer_info
->offset
;
593 va
+= buffer_info
->offset
+ buffer
->offset
;
597 *buffer_list
= buffer
->bo
;
601 write_image_descriptor(struct radv_device
*device
,
602 struct radv_cmd_buffer
*cmd_buffer
,
604 struct radeon_winsys_bo
**buffer_list
,
605 VkDescriptorType descriptor_type
,
606 const VkDescriptorImageInfo
*image_info
)
608 RADV_FROM_HANDLE(radv_image_view
, iview
, image_info
->imageView
);
610 if (descriptor_type
== VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
) {
611 memcpy(dst
, iview
->storage_descriptor
, 8 * 4);
612 memcpy(dst
+ 8, iview
->storage_fmask_descriptor
, 8 * 4);
614 memcpy(dst
, iview
->descriptor
, 8 * 4);
615 memcpy(dst
+ 8, iview
->fmask_descriptor
, 8 * 4);
619 device
->ws
->cs_add_buffer(cmd_buffer
->cs
, iview
->bo
, 7);
621 *buffer_list
= iview
->bo
;
625 write_combined_image_sampler_descriptor(struct radv_device
*device
,
626 struct radv_cmd_buffer
*cmd_buffer
,
628 struct radeon_winsys_bo
**buffer_list
,
629 VkDescriptorType descriptor_type
,
630 const VkDescriptorImageInfo
*image_info
,
633 RADV_FROM_HANDLE(radv_sampler
, sampler
, image_info
->sampler
);
635 write_image_descriptor(device
, cmd_buffer
, dst
, buffer_list
, descriptor_type
, image_info
);
636 /* copy over sampler state */
638 memcpy(dst
+ 16, sampler
->state
, 16);
642 write_sampler_descriptor(struct radv_device
*device
,
644 const VkDescriptorImageInfo
*image_info
)
646 RADV_FROM_HANDLE(radv_sampler
, sampler
, image_info
->sampler
);
648 memcpy(dst
, sampler
->state
, 16);
651 void radv_update_descriptor_sets(
652 struct radv_device
* device
,
653 struct radv_cmd_buffer
* cmd_buffer
,
654 VkDescriptorSet dstSetOverride
,
655 uint32_t descriptorWriteCount
,
656 const VkWriteDescriptorSet
* pDescriptorWrites
,
657 uint32_t descriptorCopyCount
,
658 const VkCopyDescriptorSet
* pDescriptorCopies
)
661 for (i
= 0; i
< descriptorWriteCount
; i
++) {
662 const VkWriteDescriptorSet
*writeset
= &pDescriptorWrites
[i
];
663 RADV_FROM_HANDLE(radv_descriptor_set
, set
,
664 dstSetOverride
? dstSetOverride
: writeset
->dstSet
);
665 const struct radv_descriptor_set_binding_layout
*binding_layout
=
666 set
->layout
->binding
+ writeset
->dstBinding
;
667 uint32_t *ptr
= set
->mapped_ptr
;
668 struct radeon_winsys_bo
**buffer_list
= set
->descriptors
;
669 /* Immutable samplers are not copied into push descriptors when they are
670 * allocated, so if we are writing push descriptors we have to copy the
671 * immutable samplers into them now.
673 const bool copy_immutable_samplers
= cmd_buffer
&&
674 binding_layout
->immutable_samplers_offset
&& !binding_layout
->immutable_samplers_equal
;
675 const uint32_t *samplers
= radv_immutable_samplers(set
->layout
, binding_layout
);
677 ptr
+= binding_layout
->offset
/ 4;
678 ptr
+= binding_layout
->size
* writeset
->dstArrayElement
/ 4;
679 buffer_list
+= binding_layout
->buffer_offset
;
680 buffer_list
+= writeset
->dstArrayElement
;
681 for (j
= 0; j
< writeset
->descriptorCount
; ++j
) {
682 switch(writeset
->descriptorType
) {
683 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
684 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
685 unsigned idx
= writeset
->dstArrayElement
+ j
;
686 idx
+= binding_layout
->dynamic_offset_offset
;
687 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
688 write_dynamic_buffer_descriptor(device
, set
->dynamic_descriptors
+ idx
,
689 buffer_list
, writeset
->pBufferInfo
+ j
);
692 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
693 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
694 write_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
695 writeset
->pBufferInfo
+ j
);
697 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
698 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
699 write_texel_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
700 writeset
->pTexelBufferView
[j
]);
702 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
703 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
704 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
705 write_image_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
706 writeset
->descriptorType
,
707 writeset
->pImageInfo
+ j
);
709 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
710 write_combined_image_sampler_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
711 writeset
->descriptorType
,
712 writeset
->pImageInfo
+ j
,
713 !binding_layout
->immutable_samplers_offset
);
714 if (copy_immutable_samplers
) {
715 const unsigned idx
= writeset
->dstArrayElement
+ j
;
716 memcpy(ptr
+ 16, samplers
+ 4 * idx
, 16);
719 case VK_DESCRIPTOR_TYPE_SAMPLER
:
720 if (!binding_layout
->immutable_samplers_offset
) {
721 write_sampler_descriptor(device
, ptr
,
722 writeset
->pImageInfo
+ j
);
723 } else if (copy_immutable_samplers
) {
724 unsigned idx
= writeset
->dstArrayElement
+ j
;
725 memcpy(ptr
, samplers
+ 4 * idx
, 16);
729 unreachable("unimplemented descriptor type");
732 ptr
+= binding_layout
->size
/ 4;
737 if (descriptorCopyCount
)
738 radv_finishme("copy descriptors");
741 void radv_UpdateDescriptorSets(
743 uint32_t descriptorWriteCount
,
744 const VkWriteDescriptorSet
* pDescriptorWrites
,
745 uint32_t descriptorCopyCount
,
746 const VkCopyDescriptorSet
* pDescriptorCopies
)
748 RADV_FROM_HANDLE(radv_device
, device
, _device
);
750 radv_update_descriptor_sets(device
, NULL
, VK_NULL_HANDLE
, descriptorWriteCount
, pDescriptorWrites
,
751 descriptorCopyCount
, pDescriptorCopies
);
754 VkResult
radv_CreateDescriptorUpdateTemplateKHR(VkDevice _device
,
755 const VkDescriptorUpdateTemplateCreateInfoKHR
*pCreateInfo
,
756 const VkAllocationCallbacks
*pAllocator
,
757 VkDescriptorUpdateTemplateKHR
*pDescriptorUpdateTemplate
)
759 RADV_FROM_HANDLE(radv_device
, device
, _device
);
760 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
, pCreateInfo
->descriptorSetLayout
);
761 const uint32_t entry_count
= pCreateInfo
->descriptorUpdateEntryCount
;
762 const size_t size
= sizeof(struct radv_descriptor_update_template
) +
763 sizeof(struct radv_descriptor_update_template_entry
) * entry_count
;
764 struct radv_descriptor_update_template
*templ
;
767 templ
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
769 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
771 templ
->entry_count
= entry_count
;
773 for (i
= 0; i
< entry_count
; i
++) {
774 const VkDescriptorUpdateTemplateEntryKHR
*entry
= &pCreateInfo
->pDescriptorUpdateEntries
[i
];
775 const struct radv_descriptor_set_binding_layout
*binding_layout
=
776 set_layout
->binding
+ entry
->dstBinding
;
777 const uint32_t buffer_offset
= binding_layout
->buffer_offset
+ entry
->dstArrayElement
;
778 const uint32_t *immutable_samplers
= NULL
;
782 /* dst_offset is an offset into dynamic_descriptors when the descriptor
783 is dynamic, and an offset into mapped_ptr otherwise */
784 switch (entry
->descriptorType
) {
785 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
786 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
787 assert(pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET_KHR
);
788 dst_offset
= binding_layout
->dynamic_offset_offset
+ entry
->dstArrayElement
;
789 dst_stride
= 0; /* Not used */
792 switch (entry
->descriptorType
) {
793 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
794 case VK_DESCRIPTOR_TYPE_SAMPLER
:
795 /* Immutable samplers are copied into push descriptors when they are pushed */
796 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR
&&
797 binding_layout
->immutable_samplers_offset
&& !binding_layout
->immutable_samplers_equal
) {
798 immutable_samplers
= radv_immutable_samplers(set_layout
, binding_layout
) + entry
->dstArrayElement
* 4;
804 dst_offset
= binding_layout
->offset
/ 4 + binding_layout
->size
* entry
->dstArrayElement
/ 4;
805 dst_stride
= binding_layout
->size
/ 4;
809 templ
->entry
[i
] = (struct radv_descriptor_update_template_entry
) {
810 .descriptor_type
= entry
->descriptorType
,
811 .descriptor_count
= entry
->descriptorCount
,
812 .src_offset
= entry
->offset
,
813 .src_stride
= entry
->stride
,
814 .dst_offset
= dst_offset
,
815 .dst_stride
= dst_stride
,
816 .buffer_offset
= buffer_offset
,
817 .has_sampler
= !binding_layout
->immutable_samplers_offset
,
818 .immutable_samplers
= immutable_samplers
822 *pDescriptorUpdateTemplate
= radv_descriptor_update_template_to_handle(templ
);
826 void radv_DestroyDescriptorUpdateTemplateKHR(VkDevice _device
,
827 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate
,
828 const VkAllocationCallbacks
*pAllocator
)
830 RADV_FROM_HANDLE(radv_device
, device
, _device
);
831 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
836 vk_free2(&device
->alloc
, pAllocator
, templ
);
839 void radv_update_descriptor_set_with_template(struct radv_device
*device
,
840 struct radv_cmd_buffer
*cmd_buffer
,
841 struct radv_descriptor_set
*set
,
842 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate
,
845 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
848 for (i
= 0; i
< templ
->entry_count
; ++i
) {
849 struct radeon_winsys_bo
**buffer_list
= set
->descriptors
+ templ
->entry
[i
].buffer_offset
;
850 uint32_t *pDst
= set
->mapped_ptr
+ templ
->entry
[i
].dst_offset
;
851 const uint8_t *pSrc
= ((const uint8_t *) pData
) + templ
->entry
[i
].src_offset
;
854 for (j
= 0; j
< templ
->entry
[i
].descriptor_count
; ++j
) {
855 switch (templ
->entry
[i
].descriptor_type
) {
856 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
857 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
858 const unsigned idx
= templ
->entry
[i
].dst_offset
+ j
;
859 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
860 write_dynamic_buffer_descriptor(device
, set
->dynamic_descriptors
+ idx
,
861 buffer_list
, (struct VkDescriptorBufferInfo
*) pSrc
);
864 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
865 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
866 write_buffer_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
867 (struct VkDescriptorBufferInfo
*) pSrc
);
869 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
870 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
871 write_texel_buffer_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
872 *(VkBufferView
*) pSrc
);
874 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
875 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
876 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
877 write_image_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
878 templ
->entry
[i
].descriptor_type
,
879 (struct VkDescriptorImageInfo
*) pSrc
);
881 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
882 write_combined_image_sampler_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
883 templ
->entry
[i
].descriptor_type
,
884 (struct VkDescriptorImageInfo
*) pSrc
,
885 templ
->entry
[i
].has_sampler
);
886 if (templ
->entry
[i
].immutable_samplers
)
887 memcpy(pDst
+ 16, templ
->entry
[i
].immutable_samplers
+ 4 * j
, 16);
889 case VK_DESCRIPTOR_TYPE_SAMPLER
:
890 if (templ
->entry
[i
].has_sampler
)
891 write_sampler_descriptor(device
, pDst
,
892 (struct VkDescriptorImageInfo
*) pSrc
);
893 else if (templ
->entry
[i
].immutable_samplers
)
894 memcpy(pDst
, templ
->entry
[i
].immutable_samplers
+ 4 * j
, 16);
897 unreachable("unimplemented descriptor type");
900 pSrc
+= templ
->entry
[i
].src_stride
;
901 pDst
+= templ
->entry
[i
].dst_stride
;
907 void radv_UpdateDescriptorSetWithTemplateKHR(VkDevice _device
,
908 VkDescriptorSet descriptorSet
,
909 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate
,
912 RADV_FROM_HANDLE(radv_device
, device
, _device
);
913 RADV_FROM_HANDLE(radv_descriptor_set
, set
, descriptorSet
);
915 radv_update_descriptor_set_with_template(device
, NULL
, set
, descriptorUpdateTemplate
, pData
);