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"
35 static bool has_equal_immutable_samplers(const VkSampler
*samplers
, uint32_t count
)
39 for(uint32_t i
= 1; i
< count
; ++i
) {
40 if (memcmp(radv_sampler_from_handle(samplers
[0])->state
,
41 radv_sampler_from_handle(samplers
[i
])->state
, 16)) {
48 VkResult
radv_CreateDescriptorSetLayout(
50 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
51 const VkAllocationCallbacks
* pAllocator
,
52 VkDescriptorSetLayout
* pSetLayout
)
54 RADV_FROM_HANDLE(radv_device
, device
, _device
);
55 struct radv_descriptor_set_layout
*set_layout
;
57 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
);
59 uint32_t max_binding
= 0;
60 uint32_t immutable_sampler_count
= 0;
61 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
62 max_binding
= MAX2(max_binding
, pCreateInfo
->pBindings
[j
].binding
);
63 if (pCreateInfo
->pBindings
[j
].pImmutableSamplers
)
64 immutable_sampler_count
+= pCreateInfo
->pBindings
[j
].descriptorCount
;
67 uint32_t samplers_offset
= sizeof(struct radv_descriptor_set_layout
) +
68 (max_binding
+ 1) * sizeof(set_layout
->binding
[0]);
69 size_t size
= samplers_offset
+ immutable_sampler_count
* 4 * sizeof(uint32_t);
71 set_layout
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
72 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
74 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
76 set_layout
->flags
= pCreateInfo
->flags
;
78 /* We just allocate all the samplers at the end of the struct */
79 uint32_t *samplers
= (uint32_t*)&set_layout
->binding
[max_binding
+ 1];
81 set_layout
->binding_count
= max_binding
+ 1;
82 set_layout
->shader_stages
= 0;
83 set_layout
->dynamic_shader_stages
= 0;
84 set_layout
->has_immutable_samplers
= false;
87 memset(set_layout
->binding
, 0, size
- sizeof(struct radv_descriptor_set_layout
));
89 uint32_t buffer_count
= 0;
90 uint32_t dynamic_offset_count
= 0;
92 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
93 const VkDescriptorSetLayoutBinding
*binding
= &pCreateInfo
->pBindings
[j
];
94 uint32_t b
= binding
->binding
;
96 unsigned binding_buffer_count
= 0;
98 switch (binding
->descriptorType
) {
99 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
100 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
101 assert(!(pCreateInfo
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
102 set_layout
->binding
[b
].dynamic_offset_count
= 1;
103 set_layout
->dynamic_shader_stages
|= binding
->stageFlags
;
104 set_layout
->binding
[b
].size
= 0;
105 binding_buffer_count
= 1;
108 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
109 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
110 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
111 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
112 set_layout
->binding
[b
].size
= 16;
113 binding_buffer_count
= 1;
116 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
117 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
118 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
119 /* main descriptor + fmask descriptor */
120 set_layout
->binding
[b
].size
= 64;
121 binding_buffer_count
= 1;
124 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
125 /* main descriptor + fmask descriptor + sampler */
126 set_layout
->binding
[b
].size
= 96;
127 binding_buffer_count
= 1;
130 case VK_DESCRIPTOR_TYPE_SAMPLER
:
131 set_layout
->binding
[b
].size
= 16;
135 unreachable("unknown descriptor type\n");
139 set_layout
->size
= align(set_layout
->size
, alignment
);
140 assert(binding
->descriptorCount
> 0);
141 set_layout
->binding
[b
].type
= binding
->descriptorType
;
142 set_layout
->binding
[b
].array_size
= binding
->descriptorCount
;
143 set_layout
->binding
[b
].offset
= set_layout
->size
;
144 set_layout
->binding
[b
].buffer_offset
= buffer_count
;
145 set_layout
->binding
[b
].dynamic_offset_offset
= dynamic_offset_count
;
147 if (binding
->pImmutableSamplers
) {
148 set_layout
->binding
[b
].immutable_samplers_offset
= samplers_offset
;
149 set_layout
->binding
[b
].immutable_samplers_equal
=
150 has_equal_immutable_samplers(binding
->pImmutableSamplers
, binding
->descriptorCount
);
151 set_layout
->has_immutable_samplers
= true;
154 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++)
155 memcpy(samplers
+ 4 * i
, &radv_sampler_from_handle(binding
->pImmutableSamplers
[i
])->state
, 16);
157 /* Don't reserve space for the samplers if they're not accessed. */
158 if (set_layout
->binding
[b
].immutable_samplers_equal
) {
159 if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
160 set_layout
->binding
[b
].size
-= 32;
161 else if (binding
->descriptorType
== VK_DESCRIPTOR_TYPE_SAMPLER
)
162 set_layout
->binding
[b
].size
-= 16;
164 samplers
+= 4 * binding
->descriptorCount
;
165 samplers_offset
+= 4 * sizeof(uint32_t) * binding
->descriptorCount
;
168 set_layout
->size
+= binding
->descriptorCount
* set_layout
->binding
[b
].size
;
169 buffer_count
+= binding
->descriptorCount
* binding_buffer_count
;
170 dynamic_offset_count
+= binding
->descriptorCount
*
171 set_layout
->binding
[b
].dynamic_offset_count
;
172 set_layout
->shader_stages
|= binding
->stageFlags
;
175 set_layout
->buffer_count
= buffer_count
;
176 set_layout
->dynamic_offset_count
= dynamic_offset_count
;
178 *pSetLayout
= radv_descriptor_set_layout_to_handle(set_layout
);
183 void radv_DestroyDescriptorSetLayout(
185 VkDescriptorSetLayout _set_layout
,
186 const VkAllocationCallbacks
* pAllocator
)
188 RADV_FROM_HANDLE(radv_device
, device
, _device
);
189 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
, _set_layout
);
194 vk_free2(&device
->alloc
, pAllocator
, set_layout
);
197 void radv_GetDescriptorSetLayoutSupport(VkDevice device
,
198 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
199 VkDescriptorSetLayoutSupport
* pSupport
)
201 bool supported
= true;
203 for (uint32_t i
= 0; i
< pCreateInfo
->bindingCount
; i
++) {
204 const VkDescriptorSetLayoutBinding
*binding
= &pCreateInfo
->pBindings
[i
];
206 if (binding
->descriptorCount
== 0)
209 uint64_t descriptor_size
= 0;
210 uint64_t descriptor_alignment
= 1;
211 switch (binding
->descriptorType
) {
212 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
213 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
215 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
216 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
217 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
218 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
219 descriptor_size
= 16;
220 descriptor_alignment
= 16;
222 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
223 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
224 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
225 descriptor_size
= 64;
226 descriptor_alignment
= 32;
228 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
229 if (!has_equal_immutable_samplers(binding
->pImmutableSamplers
, binding
->descriptorCount
)) {
230 descriptor_size
= 64;
232 descriptor_size
= 96;
234 descriptor_alignment
= 32;
236 case VK_DESCRIPTOR_TYPE_SAMPLER
:
237 if (!has_equal_immutable_samplers(binding
->pImmutableSamplers
, binding
->descriptorCount
)) {
238 descriptor_size
= 16;
239 descriptor_alignment
= 16;
243 unreachable("unknown descriptor type\n");
247 if (size
&& !align_u64(size
, descriptor_alignment
)) {
250 size
= align_u64(size
, descriptor_alignment
);
251 if (descriptor_size
&& (UINT64_MAX
- size
) / descriptor_size
< binding
->descriptorCount
) {
254 size
+= binding
->descriptorCount
* descriptor_size
;
257 pSupport
->supported
= supported
;
261 * Pipeline layouts. These have nothing to do with the pipeline. They are
262 * just muttiple descriptor set layouts pasted together
265 VkResult
radv_CreatePipelineLayout(
267 const VkPipelineLayoutCreateInfo
* pCreateInfo
,
268 const VkAllocationCallbacks
* pAllocator
,
269 VkPipelineLayout
* pPipelineLayout
)
271 RADV_FROM_HANDLE(radv_device
, device
, _device
);
272 struct radv_pipeline_layout
*layout
;
273 struct mesa_sha1 ctx
;
275 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
);
277 layout
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*layout
), 8,
278 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
280 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
282 layout
->num_sets
= pCreateInfo
->setLayoutCount
;
284 unsigned dynamic_offset_count
= 0;
287 _mesa_sha1_init(&ctx
);
288 for (uint32_t set
= 0; set
< pCreateInfo
->setLayoutCount
; set
++) {
289 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
,
290 pCreateInfo
->pSetLayouts
[set
]);
291 layout
->set
[set
].layout
= set_layout
;
293 layout
->set
[set
].dynamic_offset_start
= dynamic_offset_count
;
294 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
295 dynamic_offset_count
+= set_layout
->binding
[b
].array_size
* set_layout
->binding
[b
].dynamic_offset_count
;
296 if (set_layout
->binding
[b
].immutable_samplers_offset
)
297 _mesa_sha1_update(&ctx
, radv_immutable_samplers(set_layout
, set_layout
->binding
+ b
),
298 set_layout
->binding
[b
].array_size
* 4 * sizeof(uint32_t));
300 _mesa_sha1_update(&ctx
, set_layout
->binding
,
301 sizeof(set_layout
->binding
[0]) * set_layout
->binding_count
);
304 layout
->dynamic_offset_count
= dynamic_offset_count
;
305 layout
->push_constant_size
= 0;
307 for (unsigned i
= 0; i
< pCreateInfo
->pushConstantRangeCount
; ++i
) {
308 const VkPushConstantRange
*range
= pCreateInfo
->pPushConstantRanges
+ i
;
309 layout
->push_constant_size
= MAX2(layout
->push_constant_size
,
310 range
->offset
+ range
->size
);
313 layout
->push_constant_size
= align(layout
->push_constant_size
, 16);
314 _mesa_sha1_update(&ctx
, &layout
->push_constant_size
,
315 sizeof(layout
->push_constant_size
));
316 _mesa_sha1_final(&ctx
, layout
->sha1
);
317 *pPipelineLayout
= radv_pipeline_layout_to_handle(layout
);
322 void radv_DestroyPipelineLayout(
324 VkPipelineLayout _pipelineLayout
,
325 const VkAllocationCallbacks
* pAllocator
)
327 RADV_FROM_HANDLE(radv_device
, device
, _device
);
328 RADV_FROM_HANDLE(radv_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
330 if (!pipeline_layout
)
332 vk_free2(&device
->alloc
, pAllocator
, pipeline_layout
);
338 radv_descriptor_set_create(struct radv_device
*device
,
339 struct radv_descriptor_pool
*pool
,
340 const struct radv_descriptor_set_layout
*layout
,
341 struct radv_descriptor_set
**out_set
)
343 struct radv_descriptor_set
*set
;
344 unsigned range_offset
= sizeof(struct radv_descriptor_set
) +
345 sizeof(struct radeon_winsys_bo
*) * layout
->buffer_count
;
346 unsigned mem_size
= range_offset
+
347 sizeof(struct radv_descriptor_range
) * layout
->dynamic_offset_count
;
349 if (pool
->host_memory_base
) {
350 if (pool
->host_memory_end
- pool
->host_memory_ptr
< mem_size
)
351 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY_KHR
);
353 set
= (struct radv_descriptor_set
*)pool
->host_memory_ptr
;
354 pool
->host_memory_ptr
+= mem_size
;
356 set
= vk_alloc2(&device
->alloc
, NULL
, mem_size
, 8,
357 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
360 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
363 memset(set
, 0, mem_size
);
365 if (layout
->dynamic_offset_count
) {
366 set
->dynamic_descriptors
= (struct radv_descriptor_range
*)((uint8_t*)set
+ range_offset
);
369 set
->layout
= layout
;
371 uint32_t layout_size
= align_u32(layout
->size
, 32);
372 set
->size
= layout
->size
;
374 if (!pool
->host_memory_base
&& pool
->entry_count
== pool
->max_entry_count
) {
375 vk_free2(&device
->alloc
, NULL
, set
);
376 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY_KHR
);
379 /* try to allocate linearly first, so that we don't spend
380 * time looking for gaps if the app only allocates &
381 * resets via the pool. */
382 if (pool
->current_offset
+ layout_size
<= pool
->size
) {
384 set
->mapped_ptr
= (uint32_t*)(pool
->mapped_ptr
+ pool
->current_offset
);
385 set
->va
= radv_buffer_get_va(set
->bo
) + pool
->current_offset
;
386 if (!pool
->host_memory_base
) {
387 pool
->entries
[pool
->entry_count
].offset
= pool
->current_offset
;
388 pool
->entries
[pool
->entry_count
].size
= layout_size
;
389 pool
->entries
[pool
->entry_count
].set
= set
;
392 pool
->current_offset
+= layout_size
;
393 } else if (!pool
->host_memory_base
) {
397 for (index
= 0; index
< pool
->entry_count
; ++index
) {
398 if (pool
->entries
[index
].offset
- offset
>= layout_size
)
400 offset
= pool
->entries
[index
].offset
+ pool
->entries
[index
].size
;
403 if (pool
->size
- offset
< layout_size
) {
404 vk_free2(&device
->alloc
, NULL
, set
);
405 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY_KHR
);
408 set
->mapped_ptr
= (uint32_t*)(pool
->mapped_ptr
+ offset
);
409 set
->va
= radv_buffer_get_va(set
->bo
) + offset
;
410 memmove(&pool
->entries
[index
+ 1], &pool
->entries
[index
],
411 sizeof(pool
->entries
[0]) * (pool
->entry_count
- index
));
412 pool
->entries
[index
].offset
= offset
;
413 pool
->entries
[index
].size
= layout_size
;
414 pool
->entries
[index
].set
= set
;
417 return vk_error(VK_ERROR_OUT_OF_POOL_MEMORY_KHR
);
420 if (layout
->has_immutable_samplers
) {
421 for (unsigned i
= 0; i
< layout
->binding_count
; ++i
) {
422 if (!layout
->binding
[i
].immutable_samplers_offset
||
423 layout
->binding
[i
].immutable_samplers_equal
)
426 unsigned offset
= layout
->binding
[i
].offset
/ 4;
427 if (layout
->binding
[i
].type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
)
430 const uint32_t *samplers
= (const uint32_t*)((const char*)layout
+ layout
->binding
[i
].immutable_samplers_offset
);
431 for (unsigned j
= 0; j
< layout
->binding
[i
].array_size
; ++j
) {
432 memcpy(set
->mapped_ptr
+ offset
, samplers
+ 4 * j
, 16);
433 offset
+= layout
->binding
[i
].size
/ 4;
443 radv_descriptor_set_destroy(struct radv_device
*device
,
444 struct radv_descriptor_pool
*pool
,
445 struct radv_descriptor_set
*set
,
448 assert(!pool
->host_memory_base
);
450 if (free_bo
&& set
->size
&& !pool
->host_memory_base
) {
451 uint32_t offset
= (uint8_t*)set
->mapped_ptr
- pool
->mapped_ptr
;
452 for (int i
= 0; i
< pool
->entry_count
; ++i
) {
453 if (pool
->entries
[i
].offset
== offset
) {
454 memmove(&pool
->entries
[i
], &pool
->entries
[i
+1],
455 sizeof(pool
->entries
[i
]) * (pool
->entry_count
- i
- 1));
461 vk_free2(&device
->alloc
, NULL
, set
);
464 VkResult
radv_CreateDescriptorPool(
466 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
467 const VkAllocationCallbacks
* pAllocator
,
468 VkDescriptorPool
* pDescriptorPool
)
470 RADV_FROM_HANDLE(radv_device
, device
, _device
);
471 struct radv_descriptor_pool
*pool
;
472 int size
= sizeof(struct radv_descriptor_pool
);
473 uint64_t bo_size
= 0, bo_count
= 0, range_count
= 0;
476 for (unsigned i
= 0; i
< pCreateInfo
->poolSizeCount
; ++i
) {
477 if (pCreateInfo
->pPoolSizes
[i
].type
!= VK_DESCRIPTOR_TYPE_SAMPLER
)
478 bo_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
480 switch(pCreateInfo
->pPoolSizes
[i
].type
) {
481 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
482 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
483 range_count
+= pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
485 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
486 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
487 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
488 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
489 case VK_DESCRIPTOR_TYPE_SAMPLER
:
490 /* 32 as we may need to align for images */
491 bo_size
+= 32 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
493 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
494 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
495 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
496 bo_size
+= 64 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
498 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
499 bo_size
+= 96 * pCreateInfo
->pPoolSizes
[i
].descriptorCount
;
502 unreachable("unknown descriptor type\n");
507 if (!(pCreateInfo
->flags
& VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT
)) {
508 uint64_t host_size
= pCreateInfo
->maxSets
* sizeof(struct radv_descriptor_set
);
509 host_size
+= sizeof(struct radeon_winsys_bo
*) * bo_count
;
510 host_size
+= sizeof(struct radv_descriptor_range
) * range_count
;
513 size
+= sizeof(struct radv_descriptor_pool_entry
) * pCreateInfo
->maxSets
;
516 pool
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8,
517 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
519 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
521 memset(pool
, 0, sizeof(*pool
));
523 if (!(pCreateInfo
->flags
& VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT
)) {
524 pool
->host_memory_base
= (uint8_t*)pool
+ sizeof(struct radv_descriptor_pool
);
525 pool
->host_memory_ptr
= pool
->host_memory_base
;
526 pool
->host_memory_end
= (uint8_t*)pool
+ size
;
530 pool
->bo
= device
->ws
->buffer_create(device
->ws
, bo_size
, 32,
532 RADEON_FLAG_NO_INTERPROCESS_SHARING
|
533 RADEON_FLAG_READ_ONLY
);
534 pool
->mapped_ptr
= (uint8_t*)device
->ws
->buffer_map(pool
->bo
);
536 pool
->size
= bo_size
;
537 pool
->max_entry_count
= pCreateInfo
->maxSets
;
539 *pDescriptorPool
= radv_descriptor_pool_to_handle(pool
);
543 void radv_DestroyDescriptorPool(
545 VkDescriptorPool _pool
,
546 const VkAllocationCallbacks
* pAllocator
)
548 RADV_FROM_HANDLE(radv_device
, device
, _device
);
549 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, _pool
);
554 if (!pool
->host_memory_base
) {
555 for(int i
= 0; i
< pool
->entry_count
; ++i
) {
556 radv_descriptor_set_destroy(device
, pool
, pool
->entries
[i
].set
, false);
561 device
->ws
->buffer_destroy(pool
->bo
);
562 vk_free2(&device
->alloc
, pAllocator
, pool
);
565 VkResult
radv_ResetDescriptorPool(
567 VkDescriptorPool descriptorPool
,
568 VkDescriptorPoolResetFlags flags
)
570 RADV_FROM_HANDLE(radv_device
, device
, _device
);
571 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, descriptorPool
);
573 if (!pool
->host_memory_base
) {
574 for(int i
= 0; i
< pool
->entry_count
; ++i
) {
575 radv_descriptor_set_destroy(device
, pool
, pool
->entries
[i
].set
, false);
577 pool
->entry_count
= 0;
580 pool
->current_offset
= 0;
581 pool
->host_memory_ptr
= pool
->host_memory_base
;
586 VkResult
radv_AllocateDescriptorSets(
588 const VkDescriptorSetAllocateInfo
* pAllocateInfo
,
589 VkDescriptorSet
* pDescriptorSets
)
591 RADV_FROM_HANDLE(radv_device
, device
, _device
);
592 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, pAllocateInfo
->descriptorPool
);
594 VkResult result
= VK_SUCCESS
;
596 struct radv_descriptor_set
*set
= NULL
;
598 /* allocate a set of buffers for each shader to contain descriptors */
599 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
600 RADV_FROM_HANDLE(radv_descriptor_set_layout
, layout
,
601 pAllocateInfo
->pSetLayouts
[i
]);
603 assert(!(layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
605 result
= radv_descriptor_set_create(device
, pool
, layout
, &set
);
606 if (result
!= VK_SUCCESS
)
609 pDescriptorSets
[i
] = radv_descriptor_set_to_handle(set
);
612 if (result
!= VK_SUCCESS
)
613 radv_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
618 VkResult
radv_FreeDescriptorSets(
620 VkDescriptorPool descriptorPool
,
622 const VkDescriptorSet
* pDescriptorSets
)
624 RADV_FROM_HANDLE(radv_device
, device
, _device
);
625 RADV_FROM_HANDLE(radv_descriptor_pool
, pool
, descriptorPool
);
627 for (uint32_t i
= 0; i
< count
; i
++) {
628 RADV_FROM_HANDLE(radv_descriptor_set
, set
, pDescriptorSets
[i
]);
630 if (set
&& !pool
->host_memory_base
)
631 radv_descriptor_set_destroy(device
, pool
, set
, true);
636 static void write_texel_buffer_descriptor(struct radv_device
*device
,
637 struct radv_cmd_buffer
*cmd_buffer
,
639 struct radeon_winsys_bo
**buffer_list
,
640 const VkBufferView _buffer_view
)
642 RADV_FROM_HANDLE(radv_buffer_view
, buffer_view
, _buffer_view
);
644 memcpy(dst
, buffer_view
->state
, 4 * 4);
647 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, buffer_view
->bo
, 7);
649 *buffer_list
= buffer_view
->bo
;
652 static void write_buffer_descriptor(struct radv_device
*device
,
653 struct radv_cmd_buffer
*cmd_buffer
,
655 struct radeon_winsys_bo
**buffer_list
,
656 const VkDescriptorBufferInfo
*buffer_info
)
658 RADV_FROM_HANDLE(radv_buffer
, buffer
, buffer_info
->buffer
);
659 uint64_t va
= radv_buffer_get_va(buffer
->bo
);
660 uint32_t range
= buffer_info
->range
;
662 if (buffer_info
->range
== VK_WHOLE_SIZE
)
663 range
= buffer
->size
- buffer_info
->offset
;
665 va
+= buffer_info
->offset
+ buffer
->offset
;
667 dst
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32);
669 dst
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
670 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
671 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
672 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
673 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
674 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
677 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, buffer
->bo
, 7);
679 *buffer_list
= buffer
->bo
;
682 static void write_dynamic_buffer_descriptor(struct radv_device
*device
,
683 struct radv_descriptor_range
*range
,
684 struct radeon_winsys_bo
**buffer_list
,
685 const VkDescriptorBufferInfo
*buffer_info
)
687 RADV_FROM_HANDLE(radv_buffer
, buffer
, buffer_info
->buffer
);
688 uint64_t va
= radv_buffer_get_va(buffer
->bo
);
689 unsigned size
= buffer_info
->range
;
691 if (buffer_info
->range
== VK_WHOLE_SIZE
)
692 size
= buffer
->size
- buffer_info
->offset
;
694 va
+= buffer_info
->offset
+ buffer
->offset
;
698 *buffer_list
= buffer
->bo
;
702 write_image_descriptor(struct radv_device
*device
,
703 struct radv_cmd_buffer
*cmd_buffer
,
705 struct radeon_winsys_bo
**buffer_list
,
706 VkDescriptorType descriptor_type
,
707 const VkDescriptorImageInfo
*image_info
)
709 RADV_FROM_HANDLE(radv_image_view
, iview
, image_info
->imageView
);
710 uint32_t *descriptor
;
712 if (descriptor_type
== VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
) {
713 descriptor
= iview
->storage_descriptor
;
715 descriptor
= iview
->descriptor
;
718 memcpy(dst
, descriptor
, 16 * 4);
721 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, iview
->bo
, 7);
723 *buffer_list
= iview
->bo
;
727 write_combined_image_sampler_descriptor(struct radv_device
*device
,
728 struct radv_cmd_buffer
*cmd_buffer
,
730 struct radeon_winsys_bo
**buffer_list
,
731 VkDescriptorType descriptor_type
,
732 const VkDescriptorImageInfo
*image_info
,
735 RADV_FROM_HANDLE(radv_sampler
, sampler
, image_info
->sampler
);
737 write_image_descriptor(device
, cmd_buffer
, dst
, buffer_list
, descriptor_type
, image_info
);
738 /* copy over sampler state */
740 memcpy(dst
+ 16, sampler
->state
, 16);
744 write_sampler_descriptor(struct radv_device
*device
,
746 const VkDescriptorImageInfo
*image_info
)
748 RADV_FROM_HANDLE(radv_sampler
, sampler
, image_info
->sampler
);
750 memcpy(dst
, sampler
->state
, 16);
753 void radv_update_descriptor_sets(
754 struct radv_device
* device
,
755 struct radv_cmd_buffer
* cmd_buffer
,
756 VkDescriptorSet dstSetOverride
,
757 uint32_t descriptorWriteCount
,
758 const VkWriteDescriptorSet
* pDescriptorWrites
,
759 uint32_t descriptorCopyCount
,
760 const VkCopyDescriptorSet
* pDescriptorCopies
)
763 for (i
= 0; i
< descriptorWriteCount
; i
++) {
764 const VkWriteDescriptorSet
*writeset
= &pDescriptorWrites
[i
];
765 RADV_FROM_HANDLE(radv_descriptor_set
, set
,
766 dstSetOverride
? dstSetOverride
: writeset
->dstSet
);
767 const struct radv_descriptor_set_binding_layout
*binding_layout
=
768 set
->layout
->binding
+ writeset
->dstBinding
;
769 uint32_t *ptr
= set
->mapped_ptr
;
770 struct radeon_winsys_bo
**buffer_list
= set
->descriptors
;
771 /* Immutable samplers are not copied into push descriptors when they are
772 * allocated, so if we are writing push descriptors we have to copy the
773 * immutable samplers into them now.
775 const bool copy_immutable_samplers
= cmd_buffer
&&
776 binding_layout
->immutable_samplers_offset
&& !binding_layout
->immutable_samplers_equal
;
777 const uint32_t *samplers
= radv_immutable_samplers(set
->layout
, binding_layout
);
779 ptr
+= binding_layout
->offset
/ 4;
780 ptr
+= binding_layout
->size
* writeset
->dstArrayElement
/ 4;
781 buffer_list
+= binding_layout
->buffer_offset
;
782 buffer_list
+= writeset
->dstArrayElement
;
783 for (j
= 0; j
< writeset
->descriptorCount
; ++j
) {
784 switch(writeset
->descriptorType
) {
785 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
786 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
787 unsigned idx
= writeset
->dstArrayElement
+ j
;
788 idx
+= binding_layout
->dynamic_offset_offset
;
789 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
790 write_dynamic_buffer_descriptor(device
, set
->dynamic_descriptors
+ idx
,
791 buffer_list
, writeset
->pBufferInfo
+ j
);
794 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
795 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
796 write_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
797 writeset
->pBufferInfo
+ j
);
799 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
800 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
801 write_texel_buffer_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
802 writeset
->pTexelBufferView
[j
]);
804 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
805 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
806 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
807 write_image_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
808 writeset
->descriptorType
,
809 writeset
->pImageInfo
+ j
);
811 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
812 write_combined_image_sampler_descriptor(device
, cmd_buffer
, ptr
, buffer_list
,
813 writeset
->descriptorType
,
814 writeset
->pImageInfo
+ j
,
815 !binding_layout
->immutable_samplers_offset
);
816 if (copy_immutable_samplers
) {
817 const unsigned idx
= writeset
->dstArrayElement
+ j
;
818 memcpy(ptr
+ 16, samplers
+ 4 * idx
, 16);
821 case VK_DESCRIPTOR_TYPE_SAMPLER
:
822 if (!binding_layout
->immutable_samplers_offset
) {
823 write_sampler_descriptor(device
, ptr
,
824 writeset
->pImageInfo
+ j
);
825 } else if (copy_immutable_samplers
) {
826 unsigned idx
= writeset
->dstArrayElement
+ j
;
827 memcpy(ptr
, samplers
+ 4 * idx
, 16);
831 unreachable("unimplemented descriptor type");
834 ptr
+= binding_layout
->size
/ 4;
840 for (i
= 0; i
< descriptorCopyCount
; i
++) {
841 const VkCopyDescriptorSet
*copyset
= &pDescriptorCopies
[i
];
842 RADV_FROM_HANDLE(radv_descriptor_set
, src_set
,
844 RADV_FROM_HANDLE(radv_descriptor_set
, dst_set
,
846 const struct radv_descriptor_set_binding_layout
*src_binding_layout
=
847 src_set
->layout
->binding
+ copyset
->srcBinding
;
848 const struct radv_descriptor_set_binding_layout
*dst_binding_layout
=
849 dst_set
->layout
->binding
+ copyset
->dstBinding
;
850 uint32_t *src_ptr
= src_set
->mapped_ptr
;
851 uint32_t *dst_ptr
= dst_set
->mapped_ptr
;
852 struct radeon_winsys_bo
**src_buffer_list
= src_set
->descriptors
;
853 struct radeon_winsys_bo
**dst_buffer_list
= dst_set
->descriptors
;
855 src_ptr
+= src_binding_layout
->offset
/ 4;
856 dst_ptr
+= dst_binding_layout
->offset
/ 4;
858 src_ptr
+= src_binding_layout
->size
* copyset
->srcArrayElement
/ 4;
859 dst_ptr
+= dst_binding_layout
->size
* copyset
->dstArrayElement
/ 4;
861 src_buffer_list
+= src_binding_layout
->buffer_offset
;
862 src_buffer_list
+= copyset
->srcArrayElement
;
864 dst_buffer_list
+= dst_binding_layout
->buffer_offset
;
865 dst_buffer_list
+= copyset
->dstArrayElement
;
867 for (j
= 0; j
< copyset
->descriptorCount
; ++j
) {
868 switch (src_binding_layout
->type
) {
869 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
870 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
871 unsigned src_idx
= copyset
->srcArrayElement
+ j
;
872 unsigned dst_idx
= copyset
->dstArrayElement
+ j
;
873 struct radv_descriptor_range
*src_range
, *dst_range
;
874 src_idx
+= src_binding_layout
->dynamic_offset_offset
;
875 dst_idx
+= dst_binding_layout
->dynamic_offset_offset
;
877 src_range
= src_set
->dynamic_descriptors
+ src_idx
;
878 dst_range
= dst_set
->dynamic_descriptors
+ dst_idx
;
879 *dst_range
= *src_range
;
883 memcpy(dst_ptr
, src_ptr
, src_binding_layout
->size
);
885 src_ptr
+= src_binding_layout
->size
/ 4;
886 dst_ptr
+= dst_binding_layout
->size
/ 4;
887 dst_buffer_list
[j
] = src_buffer_list
[j
];
894 void radv_UpdateDescriptorSets(
896 uint32_t descriptorWriteCount
,
897 const VkWriteDescriptorSet
* pDescriptorWrites
,
898 uint32_t descriptorCopyCount
,
899 const VkCopyDescriptorSet
* pDescriptorCopies
)
901 RADV_FROM_HANDLE(radv_device
, device
, _device
);
903 radv_update_descriptor_sets(device
, NULL
, VK_NULL_HANDLE
, descriptorWriteCount
, pDescriptorWrites
,
904 descriptorCopyCount
, pDescriptorCopies
);
907 VkResult
radv_CreateDescriptorUpdateTemplate(VkDevice _device
,
908 const VkDescriptorUpdateTemplateCreateInfoKHR
*pCreateInfo
,
909 const VkAllocationCallbacks
*pAllocator
,
910 VkDescriptorUpdateTemplateKHR
*pDescriptorUpdateTemplate
)
912 RADV_FROM_HANDLE(radv_device
, device
, _device
);
913 RADV_FROM_HANDLE(radv_descriptor_set_layout
, set_layout
, pCreateInfo
->descriptorSetLayout
);
914 const uint32_t entry_count
= pCreateInfo
->descriptorUpdateEntryCount
;
915 const size_t size
= sizeof(struct radv_descriptor_update_template
) +
916 sizeof(struct radv_descriptor_update_template_entry
) * entry_count
;
917 struct radv_descriptor_update_template
*templ
;
920 templ
= vk_alloc2(&device
->alloc
, pAllocator
, size
, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
922 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
924 templ
->entry_count
= entry_count
;
925 templ
->bind_point
= pCreateInfo
->pipelineBindPoint
;
927 for (i
= 0; i
< entry_count
; i
++) {
928 const VkDescriptorUpdateTemplateEntryKHR
*entry
= &pCreateInfo
->pDescriptorUpdateEntries
[i
];
929 const struct radv_descriptor_set_binding_layout
*binding_layout
=
930 set_layout
->binding
+ entry
->dstBinding
;
931 const uint32_t buffer_offset
= binding_layout
->buffer_offset
+ entry
->dstArrayElement
;
932 const uint32_t *immutable_samplers
= NULL
;
936 /* dst_offset is an offset into dynamic_descriptors when the descriptor
937 is dynamic, and an offset into mapped_ptr otherwise */
938 switch (entry
->descriptorType
) {
939 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
940 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
941 assert(pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET_KHR
);
942 dst_offset
= binding_layout
->dynamic_offset_offset
+ entry
->dstArrayElement
;
943 dst_stride
= 0; /* Not used */
946 switch (entry
->descriptorType
) {
947 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
948 case VK_DESCRIPTOR_TYPE_SAMPLER
:
949 /* Immutable samplers are copied into push descriptors when they are pushed */
950 if (pCreateInfo
->templateType
== VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR
&&
951 binding_layout
->immutable_samplers_offset
&& !binding_layout
->immutable_samplers_equal
) {
952 immutable_samplers
= radv_immutable_samplers(set_layout
, binding_layout
) + entry
->dstArrayElement
* 4;
958 dst_offset
= binding_layout
->offset
/ 4 + binding_layout
->size
* entry
->dstArrayElement
/ 4;
959 dst_stride
= binding_layout
->size
/ 4;
963 templ
->entry
[i
] = (struct radv_descriptor_update_template_entry
) {
964 .descriptor_type
= entry
->descriptorType
,
965 .descriptor_count
= entry
->descriptorCount
,
966 .src_offset
= entry
->offset
,
967 .src_stride
= entry
->stride
,
968 .dst_offset
= dst_offset
,
969 .dst_stride
= dst_stride
,
970 .buffer_offset
= buffer_offset
,
971 .has_sampler
= !binding_layout
->immutable_samplers_offset
,
972 .immutable_samplers
= immutable_samplers
976 *pDescriptorUpdateTemplate
= radv_descriptor_update_template_to_handle(templ
);
980 void radv_DestroyDescriptorUpdateTemplate(VkDevice _device
,
981 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate
,
982 const VkAllocationCallbacks
*pAllocator
)
984 RADV_FROM_HANDLE(radv_device
, device
, _device
);
985 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
990 vk_free2(&device
->alloc
, pAllocator
, templ
);
993 void radv_update_descriptor_set_with_template(struct radv_device
*device
,
994 struct radv_cmd_buffer
*cmd_buffer
,
995 struct radv_descriptor_set
*set
,
996 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate
,
999 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
1002 for (i
= 0; i
< templ
->entry_count
; ++i
) {
1003 struct radeon_winsys_bo
**buffer_list
= set
->descriptors
+ templ
->entry
[i
].buffer_offset
;
1004 uint32_t *pDst
= set
->mapped_ptr
+ templ
->entry
[i
].dst_offset
;
1005 const uint8_t *pSrc
= ((const uint8_t *) pData
) + templ
->entry
[i
].src_offset
;
1008 for (j
= 0; j
< templ
->entry
[i
].descriptor_count
; ++j
) {
1009 switch (templ
->entry
[i
].descriptor_type
) {
1010 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
1011 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
: {
1012 const unsigned idx
= templ
->entry
[i
].dst_offset
+ j
;
1013 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
1014 write_dynamic_buffer_descriptor(device
, set
->dynamic_descriptors
+ idx
,
1015 buffer_list
, (struct VkDescriptorBufferInfo
*) pSrc
);
1018 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
1019 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
1020 write_buffer_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
1021 (struct VkDescriptorBufferInfo
*) pSrc
);
1023 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
1024 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
1025 write_texel_buffer_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
1026 *(VkBufferView
*) pSrc
);
1028 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
1029 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
1030 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
1031 write_image_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
1032 templ
->entry
[i
].descriptor_type
,
1033 (struct VkDescriptorImageInfo
*) pSrc
);
1035 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
1036 write_combined_image_sampler_descriptor(device
, cmd_buffer
, pDst
, buffer_list
,
1037 templ
->entry
[i
].descriptor_type
,
1038 (struct VkDescriptorImageInfo
*) pSrc
,
1039 templ
->entry
[i
].has_sampler
);
1040 if (templ
->entry
[i
].immutable_samplers
)
1041 memcpy(pDst
+ 16, templ
->entry
[i
].immutable_samplers
+ 4 * j
, 16);
1043 case VK_DESCRIPTOR_TYPE_SAMPLER
:
1044 if (templ
->entry
[i
].has_sampler
)
1045 write_sampler_descriptor(device
, pDst
,
1046 (struct VkDescriptorImageInfo
*) pSrc
);
1047 else if (templ
->entry
[i
].immutable_samplers
)
1048 memcpy(pDst
, templ
->entry
[i
].immutable_samplers
+ 4 * j
, 16);
1051 unreachable("unimplemented descriptor type");
1054 pSrc
+= templ
->entry
[i
].src_stride
;
1055 pDst
+= templ
->entry
[i
].dst_stride
;
1061 void radv_UpdateDescriptorSetWithTemplate(VkDevice _device
,
1062 VkDescriptorSet descriptorSet
,
1063 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate
,
1066 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1067 RADV_FROM_HANDLE(radv_descriptor_set
, set
, descriptorSet
);
1069 radv_update_descriptor_set_with_template(device
, NULL
, set
, descriptorUpdateTemplate
, pData
);