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 size_t size
= sizeof(struct radv_descriptor_set_layout
) +
54 (max_binding
+ 1) * sizeof(set_layout
->binding
[0]) +
55 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
= samplers
;
132 set_layout
->binding
[b
].immutable_samplers_equal
= true;
133 samplers
+= 4 * binding
->descriptorCount
;
135 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++)
136 memcpy(set_layout
->binding
[b
].immutable_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(set_layout
->binding
[b
].immutable_samplers
+ 4 * i
,
139 set_layout
->binding
[b
].immutable_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;
151 set_layout
->size
+= binding
->descriptorCount
* set_layout
->binding
[b
].size
;
152 buffer_count
+= binding
->descriptorCount
* set_layout
->binding
[b
].buffer_count
;
153 dynamic_offset_count
+= binding
->descriptorCount
*
154 set_layout
->binding
[b
].dynamic_offset_count
;
155 set_layout
->shader_stages
|= binding
->stageFlags
;
158 set_layout
->buffer_count
= buffer_count
;
159 set_layout
->dynamic_offset_count
= dynamic_offset_count
;
161 *pSetLayout
= radv_descriptor_set_layout_to_handle(set_layout
);
166 void radv_DestroyDescriptorSetLayout(
168 VkDescriptorSetLayout _set_layout
,
169 const VkAllocationCallbacks
* pAllocator
)
171 RADV_FROM_HANDLE(radv_device
, device
, _device
);
172 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
, _set_layout
);
177 vk_free2(&device
->alloc
, pAllocator
, set_layout
);
181 * Pipeline layouts. These have nothing to do with the pipeline. They are
182 * just muttiple descriptor set layouts pasted together
185 VkResult
radv_CreatePipelineLayout(
187 const VkPipelineLayoutCreateInfo
* pCreateInfo
,
188 const VkAllocationCallbacks
* pAllocator
,
189 VkPipelineLayout
* pPipelineLayout
)
191 RADV_FROM_HANDLE(radv_device
, device
, _device
);
192 struct radv_pipeline_layout
*layout
;
193 struct mesa_sha1 ctx
;
195 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
);
197 layout
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*layout
), 8,
198 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
200 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
202 layout
->num_sets
= pCreateInfo
->setLayoutCount
;
204 unsigned dynamic_offset_count
= 0;
207 _mesa_sha1_init(&ctx
);
208 for (uint32_t set
= 0; set
< pCreateInfo
->setLayoutCount
; set
++) {
209 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
,
210 pCreateInfo
->pSetLayouts
[set
]);
211 layout
->set
[set
].layout
= set_layout
;
213 layout
->set
[set
].dynamic_offset_start
= dynamic_offset_count
;
214 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
215 dynamic_offset_count
+= set_layout
->binding
[b
].array_size
* set_layout
->binding
[b
].dynamic_offset_count
;
217 _mesa_sha1_update(&ctx
, set_layout
->binding
,
218 sizeof(set_layout
->binding
[0]) * set_layout
->binding_count
);
221 layout
->dynamic_offset_count
= dynamic_offset_count
;
222 layout
->push_constant_size
= 0;
223 for (unsigned i
= 0; i
< pCreateInfo
->pushConstantRangeCount
; ++i
) {
224 const VkPushConstantRange
*range
= pCreateInfo
->pPushConstantRanges
+ i
;
225 layout
->push_constant_size
= MAX2(layout
->push_constant_size
,
226 range
->offset
+ range
->size
);
229 layout
->push_constant_size
= align(layout
->push_constant_size
, 16);
230 _mesa_sha1_update(&ctx
, &layout
->push_constant_size
,
231 sizeof(layout
->push_constant_size
));
232 _mesa_sha1_final(&ctx
, layout
->sha1
);
233 *pPipelineLayout
= radv_pipeline_layout_to_handle(layout
);
238 void radv_DestroyPipelineLayout(
240 VkPipelineLayout _pipelineLayout
,
241 const VkAllocationCallbacks
* pAllocator
)
243 RADV_FROM_HANDLE(radv_device
, device
, _device
);
244 RADV_FROM_HANDLE(radv_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
246 if (!pipeline_layout
)
248 vk_free2(&device
->alloc
, pAllocator
, pipeline_layout
);
254 radv_descriptor_set_create(struct radv_device
*device
,
255 struct radv_descriptor_pool
*pool
,
256 struct radv_cmd_buffer
*cmd_buffer
,
257 const struct radv_descriptor_set_layout
*layout
,
258 struct radv_descriptor_set
**out_set
)
260 struct radv_descriptor_set
*set
;
261 unsigned mem_size
= sizeof(struct radv_descriptor_set
) +
262 sizeof(struct radeon_winsys_bo
*) * layout
->buffer_count
;
263 set
= vk_alloc2(&device
->alloc
, NULL
, mem_size
, 8,
264 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
267 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
269 memset(set
, 0, mem_size
);
271 if (layout
->dynamic_offset_count
) {
272 unsigned size
= sizeof(struct radv_descriptor_range
) *
273 layout
->dynamic_offset_count
;
274 set
->dynamic_descriptors
= vk_alloc2(&device
->alloc
, NULL
, size
, 8,
275 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
277 if (!set
->dynamic_descriptors
) {
278 vk_free2(&device
->alloc
, NULL
, set
);
279 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
283 set
->layout
= layout
;
285 uint32_t layout_size
= align_u32(layout
->size
, 32);
286 set
->size
= layout
->size
;
288 /* try to allocate linearly first, so that we don't spend
289 * time looking for gaps if the app only allocates &
290 * resets via the pool. */
291 if (pool
->current_offset
+ layout_size
<= pool
->size
) {
293 set
->mapped_ptr
= (uint32_t*)(pool
->mapped_ptr
+ pool
->current_offset
);
294 set
->va
= device
->ws
->buffer_get_va(set
->bo
) + pool
->current_offset
;
295 pool
->current_offset
+= layout_size
;
296 list_addtail(&set
->vram_list
, &pool
->vram_list
);
299 struct list_head
*prev
= &pool
->vram_list
;
300 struct radv_descriptor_set
*cur
;
301 LIST_FOR_EACH_ENTRY(cur
, &pool
->vram_list
, vram_list
) {
302 uint64_t start
= (uint8_t*)cur
->mapped_ptr
- pool
->mapped_ptr
;
303 if (start
- offset
>= layout_size
)
306 offset
= start
+ cur
->size
;
307 prev
= &cur
->vram_list
;
310 if (pool
->size
- offset
< layout_size
) {
311 vk_free2(&device
->alloc
, NULL
, set
->dynamic_descriptors
);
312 vk_free2(&device
->alloc
, NULL
, set
);
313 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY_KHR
);
316 set
->mapped_ptr
= (uint32_t*)(pool
->mapped_ptr
+ offset
);
317 set
->va
= device
->ws
->buffer_get_va(set
->bo
) + offset
;
318 list_add(&set
->vram_list
, prev
);
322 if (!radv_cmd_buffer_upload_alloc(cmd_buffer
, set
->size
, 32,
324 (void**)&set
->mapped_ptr
)) {
325 vk_free2(&device
->alloc
, NULL
, set
->dynamic_descriptors
);
326 vk_free2(&device
->alloc
, NULL
, set
);
327 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
330 set
->va
= device
->ws
->buffer_get_va(cmd_buffer
->upload
.upload_bo
);
331 set
->va
+= bo_offset
;
335 for (unsigned i
= 0; i
< layout
->binding_count
; ++i
) {
336 if (!layout
->binding
[i
].immutable_samplers
||
337 layout
->binding
[i
].immutable_samplers_equal
)
340 unsigned offset
= layout
->binding
[i
].offset
/ 4;
341 if (layout
->binding
[i
].type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
344 for (unsigned j
= 0; j
< layout
->binding
[i
].array_size
; ++j
) {
345 memcpy(set
->mapped_ptr
+ offset
, layout
->binding
[i
].immutable_samplers
+ 4 * j
, 16);
346 offset
+= layout
->binding
[i
].size
/ 4;
355 radv_descriptor_set_destroy(struct radv_device
*device
,
356 struct radv_descriptor_pool
*pool
,
357 struct radv_descriptor_set
*set
,
360 if (free_bo
&& set
->size
)
361 list_del(&set
->vram_list
);
362 if (set
->dynamic_descriptors
)
363 vk_free2(&device
->alloc
, NULL
, set
->dynamic_descriptors
);
364 vk_free2(&device
->alloc
, NULL
, set
);
368 radv_temp_descriptor_set_create(struct radv_device
*device
,
369 struct radv_cmd_buffer
*cmd_buffer
,
370 VkDescriptorSetLayout _layout
,
371 VkDescriptorSet
*_set
)
373 RADV_FROM_HANDLE(radv_descriptor_set_layout
, layout
, _layout
);
374 struct radv_descriptor_set
*set
= NULL
;
377 ret
= radv_descriptor_set_create(device
, NULL
, cmd_buffer
, layout
, &set
);
378 *_set
= radv_descriptor_set_to_handle(set
);
383 radv_temp_descriptor_set_destroy(struct radv_device
*device
,
384 VkDescriptorSet _set
)
386 RADV_FROM_HANDLE(radv_descriptor_set
, set
, _set
);
388 radv_descriptor_set_destroy(device
, NULL
, set
, false);
391 VkResult
radv_CreateDescriptorPool(
393 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
394 const VkAllocationCallbacks
* pAllocator
,
395 VkDescriptorPool
* pDescriptorPool
)
397 RADV_FROM_HANDLE(radv_device
, device
, _device
);
398 struct radv_descriptor_pool
*pool
;
399 int size
= sizeof(struct radv_descriptor_pool
);
400 uint64_t bo_size
= 0;
401 pool
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
402 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
404 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
406 memset(pool
, 0, sizeof(*pool
));
408 for (unsigned i
= 0; i
< pCreateInfo
->poolSizeCount
; ++i
) {
409 switch(pCreateInfo
->pPoolSizes
[i
].type
) {
410 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
411 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
413 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
414 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
415 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
416 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
417 case VK_DESCRIPTOR_TYPE_SAMPLER
:
418 /* 32 as we may need to align for images */
419 bo_size
+= 32 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
421 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
422 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
423 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
424 bo_size
+= 64 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
426 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
427 bo_size
+= 96 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
430 unreachable("unknown descriptor type\n");
436 pool
->bo
= device
->ws
->buffer_create(device
->ws
, bo_size
,
437 32, RADEON_DOMAIN_VRAM
, 0);
438 pool
->mapped_ptr
= (uint8_t*)device
->ws
->buffer_map(pool
->bo
);
440 pool
->size
= bo_size
;
442 list_inithead(&pool
->vram_list
);
443 *pDescriptorPool
= radv_descriptor_pool_to_handle(pool
);
447 void radv_DestroyDescriptorPool(
449 VkDescriptorPool _pool
,
450 const VkAllocationCallbacks
* pAllocator
)
452 RADV_FROM_HANDLE(radv_device
, device
, _device
);
453 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, _pool
);
458 list_for_each_entry_safe(struct radv_descriptor_set
, set
,
459 &pool
->vram_list
, vram_list
) {
460 radv_descriptor_set_destroy(device
, pool
, set
, false);
464 device
->ws
->buffer_destroy(pool
->bo
);
465 vk_free2(&device
->alloc
, pAllocator
, pool
);
468 VkResult
radv_ResetDescriptorPool(
470 VkDescriptorPool descriptorPool
,
471 VkDescriptorPoolResetFlags flags
)
473 RADV_FROM_HANDLE(radv_device
, device
, _device
);
474 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, descriptorPool
);
476 list_for_each_entry_safe(struct radv_descriptor_set
, set
,
477 &pool
->vram_list
, vram_list
) {
478 radv_descriptor_set_destroy(device
, pool
, set
, false);
481 list_inithead(&pool
->vram_list
);
483 pool
->current_offset
= 0;
488 VkResult
radv_AllocateDescriptorSets(
490 const VkDescriptorSetAllocateInfo
* pAllocateInfo
,
491 VkDescriptorSet
* pDescriptorSets
)
493 RADV_FROM_HANDLE(radv_device
, device
, _device
);
494 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, pAllocateInfo
->descriptorPool
);
496 VkResult result
= VK_SUCCESS
;
498 struct radv_descriptor_set
*set
;
500 /* allocate a set of buffers for each shader to contain descriptors */
501 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
502 RADV_FROM_HANDLE(radv_descriptor_set_layout
, layout
,
503 pAllocateInfo
->pSetLayouts
[i
]);
505 assert(!(layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
507 result
= radv_descriptor_set_create(device
, pool
, NULL
, layout
, &set
);
508 if (result
!= VK_SUCCESS
)
511 pDescriptorSets
[i
] = radv_descriptor_set_to_handle(set
);
514 if (result
!= VK_SUCCESS
)
515 radv_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
520 VkResult
radv_FreeDescriptorSets(
522 VkDescriptorPool descriptorPool
,
524 const VkDescriptorSet
* pDescriptorSets
)
526 RADV_FROM_HANDLE(radv_device
, device
, _device
);
527 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, descriptorPool
);
529 for (uint32_t i
= 0; i
< count
; i
++) {
530 RADV_FROM_HANDLE(radv_descriptor_set
, set
, pDescriptorSets
[i
]);
533 radv_descriptor_set_destroy(device
, pool
, set
, true);
538 static void write_texel_buffer_descriptor(struct radv_device
*device
,
539 struct radv_cmd_buffer
*cmd_buffer
,
541 struct radeon_winsys_bo
**buffer_list
,
542 const VkBufferView _buffer_view
)
544 RADV_FROM_HANDLE(radv_buffer_view
, buffer_view
, _buffer_view
);
546 memcpy(dst
, buffer_view
->state
, 4 * 4);
549 device
->ws
->cs_add_buffer(cmd_buffer
->cs
, buffer_view
->bo
, 7);
551 *buffer_list
= buffer_view
->bo
;
554 static void write_buffer_descriptor(struct radv_device
*device
,
555 struct radv_cmd_buffer
*cmd_buffer
,
557 struct radeon_winsys_bo
**buffer_list
,
558 const VkDescriptorBufferInfo
*buffer_info
)
560 RADV_FROM_HANDLE(radv_buffer
, buffer
, buffer_info
->buffer
);
561 uint64_t va
= device
->ws
->buffer_get_va(buffer
->bo
);
562 uint32_t range
= buffer_info
->range
;
564 if (buffer_info
->range
== VK_WHOLE_SIZE
)
565 range
= buffer
->size
- buffer_info
->offset
;
567 va
+= buffer_info
->offset
+ buffer
->offset
;
569 dst
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32);
571 dst
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
572 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
573 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
574 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
575 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
576 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
579 device
->ws
->cs_add_buffer(cmd_buffer
->cs
, buffer
->bo
, 7);
581 *buffer_list
= buffer
->bo
;
584 static void write_dynamic_buffer_descriptor(struct radv_device
*device
,
585 struct radv_descriptor_range
*range
,
586 struct radeon_winsys_bo
**buffer_list
,
587 const VkDescriptorBufferInfo
*buffer_info
)
589 RADV_FROM_HANDLE(radv_buffer
, buffer
, buffer_info
->buffer
);
590 uint64_t va
= device
->ws
->buffer_get_va(buffer
->bo
);
591 unsigned size
= buffer_info
->range
;
593 if (buffer_info
->range
== VK_WHOLE_SIZE
)
594 size
= buffer
->size
- buffer_info
->offset
;
596 va
+= buffer_info
->offset
+ buffer
->offset
;
600 *buffer_list
= buffer
->bo
;
604 write_image_descriptor(struct radv_device
*device
,
605 struct radv_cmd_buffer
*cmd_buffer
,
607 struct radeon_winsys_bo
**buffer_list
,
608 const VkDescriptorImageInfo
*image_info
)
610 RADV_FROM_HANDLE(radv_image_view
, iview
, image_info
->imageView
);
611 memcpy(dst
, iview
->descriptor
, 8 * 4);
612 memcpy(dst
+ 8, iview
->fmask_descriptor
, 8 * 4);
615 device
->ws
->cs_add_buffer(cmd_buffer
->cs
, iview
->bo
, 7);
617 *buffer_list
= iview
->bo
;
621 write_combined_image_sampler_descriptor(struct radv_device
*device
,
622 struct radv_cmd_buffer
*cmd_buffer
,
624 struct radeon_winsys_bo
**buffer_list
,
625 const VkDescriptorImageInfo
*image_info
,
628 RADV_FROM_HANDLE(radv_sampler
, sampler
, image_info
->sampler
);
630 write_image_descriptor(device
, cmd_buffer
, dst
, buffer_list
, image_info
);
631 /* copy over sampler state */
633 memcpy(dst
+ 16, sampler
->state
, 16);
637 write_sampler_descriptor(struct radv_device
*device
,
639 const VkDescriptorImageInfo
*image_info
)
641 RADV_FROM_HANDLE(radv_sampler
, sampler
, image_info
->sampler
);
643 memcpy(dst
, sampler
->state
, 16);
646 void radv_update_descriptor_sets(
647 struct radv_device
* device
,
648 struct radv_cmd_buffer
* cmd_buffer
,
649 VkDescriptorSet dstSetOverride
,
650 uint32_t descriptorWriteCount
,
651 const VkWriteDescriptorSet
* pDescriptorWrites
,
652 uint32_t descriptorCopyCount
,
653 const VkCopyDescriptorSet
* pDescriptorCopies
)
656 for (i
= 0; i
< descriptorWriteCount
; i
++) {
657 const VkWriteDescriptorSet
*writeset
= &pDescriptorWrites
[i
];
658 RADV_FROM_HANDLE(radv_descriptor_set
, set
,
659 dstSetOverride
? dstSetOverride
: writeset
->dstSet
);
660 const struct radv_descriptor_set_binding_layout
*binding_layout
=
661 set
->layout
->binding
+ writeset
->dstBinding
;
662 uint32_t *ptr
= set
->mapped_ptr
;
663 struct radeon_winsys_bo
**buffer_list
= set
->descriptors
;
664 /* Immutable samplers are not copied into push descriptors when they are
665 * allocated, so if we are writing push descriptors we have to copy the
666 * immutable samplers into them now.
668 const bool copy_immutable_samplers
= cmd_buffer
&&
669 binding_layout
->immutable_samplers
&& !binding_layout
->immutable_samplers_equal
;
671 ptr
+= binding_layout
->offset
/ 4;
672 ptr
+= binding_layout
->size
* writeset
->dstArrayElement
/ 4;
673 buffer_list
+= binding_layout
->buffer_offset
;
674 buffer_list
+= binding_layout
->buffer_count
* writeset
->dstArrayElement
;
675 for (j
= 0; j
< writeset
->descriptorCount
; ++j
) {
676 switch(writeset
->descriptorType
) {
677 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
678 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
679 unsigned idx
= writeset
->dstArrayElement
+ j
;
680 idx
+= binding_layout
->dynamic_offset_offset
;
681 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
682 write_dynamic_buffer_descriptor(device
, set
->dynamic_descriptors
+ idx
,
683 buffer_list
, writeset
->pBufferInfo
+ j
);
686 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
687 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
688 write_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
689 writeset
->pBufferInfo
+ j
);
691 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
692 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
693 write_texel_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
694 writeset
->pTexelBufferView
[j
]);
696 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
697 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
698 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
699 write_image_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
700 writeset
->pImageInfo
+ j
);
702 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
703 write_combined_image_sampler_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
704 writeset
->pImageInfo
+ j
,
705 !binding_layout
->immutable_samplers
);
706 if (copy_immutable_samplers
) {
707 const unsigned idx
= writeset
->dstArrayElement
+ j
;
708 memcpy(ptr
+ 16, binding_layout
->immutable_samplers
+ 4 * idx
, 16);
711 case VK_DESCRIPTOR_TYPE_SAMPLER
:
712 if (!binding_layout
->immutable_samplers
) {
713 write_sampler_descriptor(device
, ptr
,
714 writeset
->pImageInfo
+ j
);
715 } else if (copy_immutable_samplers
) {
716 unsigned idx
= writeset
->dstArrayElement
+ j
;
717 memcpy(ptr
, binding_layout
->immutable_samplers
+ 4 * idx
, 16);
721 unreachable("unimplemented descriptor type");
724 ptr
+= binding_layout
->size
/ 4;
725 buffer_list
+= binding_layout
->buffer_count
;
729 if (descriptorCopyCount
)
730 radv_finishme("copy descriptors");
733 void radv_UpdateDescriptorSets(
735 uint32_t descriptorWriteCount
,
736 const VkWriteDescriptorSet
* pDescriptorWrites
,
737 uint32_t descriptorCopyCount
,
738 const VkCopyDescriptorSet
* pDescriptorCopies
)
740 RADV_FROM_HANDLE(radv_device
, device
, _device
);
742 radv_update_descriptor_sets(device
, NULL
, VK_NULL_HANDLE
, descriptorWriteCount
, pDescriptorWrites
,
743 descriptorCopyCount
, pDescriptorCopies
);
746 VkResult
radv_CreateDescriptorUpdateTemplateKHR(VkDevice _device
,
747 const VkDescriptorUpdateTemplateCreateInfoKHR
*pCreateInfo
,
748 const VkAllocationCallbacks
*pAllocator
,
749 VkDescriptorUpdateTemplateKHR
*pDescriptorUpdateTemplate
)
751 RADV_FROM_HANDLE(radv_device
, device
, _device
);
752 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
, pCreateInfo
->descriptorSetLayout
);
753 const uint32_t entry_count
= pCreateInfo
->descriptorUpdateEntryCount
;
754 const size_t size
= sizeof(struct radv_descriptor_update_template
) +
755 sizeof(struct radv_descriptor_update_template_entry
) * entry_count
;
756 struct radv_descriptor_update_template
*templ
;
759 templ
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
761 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
763 templ
->entry_count
= entry_count
;
765 for (i
= 0; i
< entry_count
; i
++) {
766 const VkDescriptorUpdateTemplateEntryKHR
*entry
= &pCreateInfo
->pDescriptorUpdateEntries
[i
];
767 const struct radv_descriptor_set_binding_layout
*binding_layout
=
768 set_layout
->binding
+ entry
->dstBinding
;
769 const uint32_t buffer_offset
= binding_layout
->buffer_offset
+
770 binding_layout
->buffer_count
* entry
->dstArrayElement
;
771 uint32_t *immutable_samplers
= NULL
;
775 /* dst_offset is an offset into dynamic_descriptors when the descriptor
776 is dynamic, and an offset into mapped_ptr otherwise */
777 switch (entry
->descriptorType
) {
778 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
779 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
780 assert(pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET_KHR
);
781 dst_offset
= binding_layout
->dynamic_offset_offset
+ entry
->dstArrayElement
;
782 dst_stride
= 0; /* Not used */
785 switch (entry
->descriptorType
) {
786 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
787 case VK_DESCRIPTOR_TYPE_SAMPLER
:
788 /* Immutable samplers are copied into push descriptors when they are pushed */
789 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR
&&
790 binding_layout
->immutable_samplers
&& !binding_layout
->immutable_samplers_equal
)
791 immutable_samplers
= binding_layout
->immutable_samplers
+ entry
->dstArrayElement
* 4;
796 dst_offset
= binding_layout
->offset
/ 4 + binding_layout
->size
* entry
->dstArrayElement
/ 4;
797 dst_stride
= binding_layout
->size
/ 4;
801 templ
->entry
[i
] = (struct radv_descriptor_update_template_entry
) {
802 .descriptor_type
= entry
->descriptorType
,
803 .descriptor_count
= entry
->descriptorCount
,
804 .src_offset
= entry
->offset
,
805 .src_stride
= entry
->stride
,
806 .dst_offset
= dst_offset
,
807 .dst_stride
= dst_stride
,
808 .buffer_offset
= buffer_offset
,
809 .buffer_count
= binding_layout
->buffer_count
,
810 .has_sampler
= !binding_layout
->immutable_samplers
,
811 .immutable_samplers
= immutable_samplers
815 *pDescriptorUpdateTemplate
= radv_descriptor_update_template_to_handle(templ
);
819 void radv_DestroyDescriptorUpdateTemplateKHR(VkDevice _device
,
820 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate
,
821 const VkAllocationCallbacks
*pAllocator
)
823 RADV_FROM_HANDLE(radv_device
, device
, _device
);
824 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
829 vk_free2(&device
->alloc
, pAllocator
, templ
);
832 void radv_update_descriptor_set_with_template(struct radv_device
*device
,
833 struct radv_cmd_buffer
*cmd_buffer
,
834 struct radv_descriptor_set
*set
,
835 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate
,
838 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
841 for (i
= 0; i
< templ
->entry_count
; ++i
) {
842 struct radeon_winsys_bo
**buffer_list
= set
->descriptors
+ templ
->entry
[i
].buffer_offset
;
843 uint32_t *pDst
= set
->mapped_ptr
+ templ
->entry
[i
].dst_offset
;
844 const uint8_t *pSrc
= ((const uint8_t *) pData
) + templ
->entry
[i
].src_offset
;
847 for (j
= 0; j
< templ
->entry
[i
].descriptor_count
; ++j
) {
848 switch (templ
->entry
[i
].descriptor_type
) {
849 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
850 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
851 const unsigned idx
= templ
->entry
[i
].dst_offset
+ j
;
852 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
853 write_dynamic_buffer_descriptor(device
, set
->dynamic_descriptors
+ idx
,
854 buffer_list
, (struct VkDescriptorBufferInfo
*) pSrc
);
857 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
858 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
859 write_buffer_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
860 (struct VkDescriptorBufferInfo
*) pSrc
);
862 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
863 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
864 write_texel_buffer_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
865 *(VkBufferView
*) pSrc
);
867 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
868 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
869 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
870 write_image_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
871 (struct VkDescriptorImageInfo
*) pSrc
);
873 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
874 write_combined_image_sampler_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
875 (struct VkDescriptorImageInfo
*) pSrc
,
876 templ
->entry
[i
].has_sampler
);
877 if (templ
->entry
[i
].immutable_samplers
)
878 memcpy(pDst
+ 16, templ
->entry
[i
].immutable_samplers
+ 4 * j
, 16);
880 case VK_DESCRIPTOR_TYPE_SAMPLER
:
881 if (templ
->entry
[i
].has_sampler
)
882 write_sampler_descriptor(device
, pDst
,
883 (struct VkDescriptorImageInfo
*) pSrc
);
884 else if (templ
->entry
[i
].immutable_samplers
)
885 memcpy(pDst
, templ
->entry
[i
].immutable_samplers
+ 4 * j
, 16);
888 unreachable("unimplemented descriptor type");
891 pSrc
+= templ
->entry
[i
].src_stride
;
892 pDst
+= templ
->entry
[i
].dst_stride
;
893 buffer_list
+= templ
->entry
[i
].buffer_count
;
898 void radv_UpdateDescriptorSetWithTemplateKHR(VkDevice _device
,
899 VkDescriptorSet descriptorSet
,
900 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate
,
903 RADV_FROM_HANDLE(radv_device
, device
, _device
);
904 RADV_FROM_HANDLE(radv_descriptor_set
, set
, descriptorSet
);
906 radv_update_descriptor_set_with_template(device
, NULL
, set
, descriptorUpdateTemplate
, pData
);