2 * Copyright © 2015 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 #include "anv_private.h"
33 * Descriptor set layouts.
36 VkResult
anv_CreateDescriptorSetLayout(
38 const VkDescriptorSetLayoutCreateInfo
* pCreateInfo
,
39 VkDescriptorSetLayout
* pSetLayout
)
41 ANV_FROM_HANDLE(anv_device
, device
, _device
);
42 struct anv_descriptor_set_layout
*set_layout
;
45 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
);
47 uint32_t max_binding
= 0;
48 uint32_t immutable_sampler_count
= 0;
49 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
50 max_binding
= MAX2(max_binding
, pCreateInfo
->pBinding
[j
].binding
);
51 if (pCreateInfo
->pBinding
[j
].pImmutableSamplers
)
52 immutable_sampler_count
+= pCreateInfo
->pBinding
[j
].descriptorCount
;
55 size_t size
= sizeof(struct anv_descriptor_set_layout
) +
56 (max_binding
+ 1) * sizeof(set_layout
->binding
[0]) +
57 immutable_sampler_count
* sizeof(struct anv_sampler
*);
59 set_layout
= anv_device_alloc(device
, size
, 8,
60 VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
62 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
64 /* We just allocate all the samplers at the end of the struct */
65 struct anv_sampler
**samplers
=
66 (struct anv_sampler
**)&set_layout
->binding
[max_binding
+ 1];
68 set_layout
->binding_count
= max_binding
+ 1;
69 set_layout
->shader_stages
= 0;
72 /* Initialize all binding_layout entries to -1 */
73 memset(set_layout
->binding
, -1,
74 (max_binding
+ 1) * sizeof(set_layout
->binding
[0]));
76 /* Initialize all samplers to 0 */
77 memset(samplers
, 0, immutable_sampler_count
* sizeof(*samplers
));
79 uint32_t sampler_count
[VK_SHADER_STAGE_NUM
] = { 0, };
80 uint32_t surface_count
[VK_SHADER_STAGE_NUM
] = { 0, };
81 uint32_t dynamic_offset_count
= 0;
83 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
84 const VkDescriptorSetLayoutBinding
*binding
= &pCreateInfo
->pBinding
[j
];
85 uint32_t b
= binding
->binding
;
87 assert(binding
->descriptorCount
> 0);
88 set_layout
->binding
[b
].array_size
= binding
->descriptorCount
;
89 set_layout
->binding
[b
].descriptor_index
= set_layout
->size
;
90 set_layout
->size
+= binding
->descriptorCount
;
92 switch (binding
->descriptorType
) {
93 case VK_DESCRIPTOR_TYPE_SAMPLER
:
94 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
95 for_each_bit(s
, binding
->stageFlags
) {
96 set_layout
->binding
[b
].stage
[s
].sampler_index
= sampler_count
[s
];
97 sampler_count
[s
] += binding
->descriptorCount
;
104 switch (binding
->descriptorType
) {
105 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
106 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
107 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
108 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
109 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
110 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
111 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
112 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
113 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
114 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
115 for_each_bit(s
, binding
->stageFlags
) {
116 set_layout
->binding
[b
].stage
[s
].surface_index
= surface_count
[s
];
117 surface_count
[s
] += binding
->descriptorCount
;
124 switch (binding
->descriptorType
) {
125 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
126 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
127 set_layout
->binding
[b
].dynamic_offset_index
= dynamic_offset_count
;
128 dynamic_offset_count
+= binding
->descriptorCount
;
134 if (binding
->pImmutableSamplers
) {
135 set_layout
->binding
[b
].immutable_samplers
= samplers
;
136 samplers
+= binding
->descriptorCount
;
138 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++)
139 set_layout
->binding
[b
].immutable_samplers
[i
] =
140 anv_sampler_from_handle(binding
->pImmutableSamplers
[i
]);
142 set_layout
->binding
[b
].immutable_samplers
= NULL
;
145 set_layout
->shader_stages
|= binding
->stageFlags
;
148 set_layout
->dynamic_offset_count
= dynamic_offset_count
;
150 *pSetLayout
= anv_descriptor_set_layout_to_handle(set_layout
);
155 void anv_DestroyDescriptorSetLayout(
157 VkDescriptorSetLayout _set_layout
)
159 ANV_FROM_HANDLE(anv_device
, device
, _device
);
160 ANV_FROM_HANDLE(anv_descriptor_set_layout
, set_layout
, _set_layout
);
162 anv_device_free(device
, set_layout
);
166 * Pipeline layouts. These have nothing to do with the pipeline. They are
167 * just muttiple descriptor set layouts pasted together
170 VkResult
anv_CreatePipelineLayout(
172 const VkPipelineLayoutCreateInfo
* pCreateInfo
,
173 VkPipelineLayout
* pPipelineLayout
)
175 ANV_FROM_HANDLE(anv_device
, device
, _device
);
176 struct anv_pipeline_layout l
, *layout
;
178 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
);
180 l
.num_sets
= pCreateInfo
->setLayoutCount
;
182 unsigned dynamic_offset_count
= 0;
184 memset(l
.stage
, 0, sizeof(l
.stage
));
185 for (uint32_t set
= 0; set
< pCreateInfo
->setLayoutCount
; set
++) {
186 ANV_FROM_HANDLE(anv_descriptor_set_layout
, set_layout
,
187 pCreateInfo
->pSetLayouts
[set
]);
188 l
.set
[set
].layout
= set_layout
;
190 l
.set
[set
].dynamic_offset_start
= dynamic_offset_count
;
191 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
192 if (set_layout
->binding
[b
].dynamic_offset_index
>= 0)
193 dynamic_offset_count
+= set_layout
->binding
[b
].array_size
;
196 for (VkShaderStage s
= 0; s
< VK_SHADER_STAGE_NUM
; s
++) {
197 l
.set
[set
].stage
[s
].surface_start
= l
.stage
[s
].surface_count
;
198 l
.set
[set
].stage
[s
].sampler_start
= l
.stage
[s
].sampler_count
;
200 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
201 unsigned array_size
= set_layout
->binding
[b
].array_size
;
203 if (set_layout
->binding
[b
].stage
[s
].surface_index
>= 0) {
204 l
.stage
[s
].surface_count
+= array_size
;
206 if (set_layout
->binding
[b
].dynamic_offset_index
>= 0)
207 l
.stage
[s
].has_dynamic_offsets
= true;
210 if (set_layout
->binding
[b
].stage
[s
].sampler_index
>= 0)
211 l
.stage
[s
].sampler_count
+= array_size
;
216 unsigned num_bindings
= 0;
217 for (VkShaderStage s
= 0; s
< VK_SHADER_STAGE_NUM
; s
++)
218 num_bindings
+= l
.stage
[s
].surface_count
+ l
.stage
[s
].sampler_count
;
220 size_t size
= sizeof(*layout
) + num_bindings
* sizeof(layout
->entries
[0]);
222 layout
= anv_device_alloc(device
, size
, 8, VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
224 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
226 /* Now we can actually build our surface and sampler maps */
227 struct anv_pipeline_binding
*entry
= layout
->entries
;
228 for (VkShaderStage s
= 0; s
< VK_SHADER_STAGE_NUM
; s
++) {
229 l
.stage
[s
].surface_to_descriptor
= entry
;
230 entry
+= l
.stage
[s
].surface_count
;
231 l
.stage
[s
].sampler_to_descriptor
= entry
;
232 entry
+= l
.stage
[s
].sampler_count
;
236 for (uint32_t set
= 0; set
< pCreateInfo
->setLayoutCount
; set
++) {
237 struct anv_descriptor_set_layout
*set_layout
= l
.set
[set
].layout
;
239 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
240 unsigned array_size
= set_layout
->binding
[b
].array_size
;
241 unsigned set_offset
= set_layout
->binding
[b
].descriptor_index
;
243 if (set_layout
->binding
[b
].stage
[s
].surface_index
>= 0) {
244 assert(surface
== l
.set
[set
].stage
[s
].surface_start
+
245 set_layout
->binding
[b
].stage
[s
].surface_index
);
246 for (unsigned i
= 0; i
< array_size
; i
++) {
247 l
.stage
[s
].surface_to_descriptor
[surface
+ i
].set
= set
;
248 l
.stage
[s
].surface_to_descriptor
[surface
+ i
].offset
= set_offset
+ i
;
250 surface
+= array_size
;
253 if (set_layout
->binding
[b
].stage
[s
].sampler_index
>= 0) {
254 assert(sampler
== l
.set
[set
].stage
[s
].sampler_start
+
255 set_layout
->binding
[b
].stage
[s
].sampler_index
);
256 for (unsigned i
= 0; i
< array_size
; i
++) {
257 l
.stage
[s
].sampler_to_descriptor
[sampler
+ i
].set
= set
;
258 l
.stage
[s
].sampler_to_descriptor
[sampler
+ i
].offset
= set_offset
+ i
;
260 sampler
+= array_size
;
266 /* Finally, we're done setting it up, copy into the allocated version */
269 *pPipelineLayout
= anv_pipeline_layout_to_handle(layout
);
274 void anv_DestroyPipelineLayout(
276 VkPipelineLayout _pipelineLayout
)
278 ANV_FROM_HANDLE(anv_device
, device
, _device
);
279 ANV_FROM_HANDLE(anv_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
281 anv_device_free(device
, pipeline_layout
);
285 * Descriptor pools. These are a no-op for now.
288 VkResult
anv_CreateDescriptorPool(
290 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
291 VkDescriptorPool
* pDescriptorPool
)
293 anv_finishme("VkDescriptorPool is a stub");
294 *pDescriptorPool
= (VkDescriptorPool
)1;
298 void anv_DestroyDescriptorPool(
300 VkDescriptorPool _pool
)
302 anv_finishme("VkDescriptorPool is a stub: free the pool's descriptor sets");
305 VkResult
anv_ResetDescriptorPool(
307 VkDescriptorPool descriptorPool
,
308 VkDescriptorPoolResetFlags flags
)
310 anv_finishme("VkDescriptorPool is a stub: free the pool's descriptor sets");
315 anv_descriptor_set_create(struct anv_device
*device
,
316 const struct anv_descriptor_set_layout
*layout
,
317 struct anv_descriptor_set
**out_set
)
319 struct anv_descriptor_set
*set
;
320 size_t size
= sizeof(*set
) + layout
->size
* sizeof(set
->descriptors
[0]);
322 set
= anv_device_alloc(device
, size
, 8, VK_SYSTEM_ALLOC_TYPE_API_OBJECT
);
324 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
326 /* A descriptor set may not be 100% filled. Clear the set so we can can
327 * later detect holes in it.
329 memset(set
, 0, size
);
331 set
->layout
= layout
;
333 /* Go through and fill out immutable samplers if we have any */
334 struct anv_descriptor
*desc
= set
->descriptors
;
335 for (uint32_t b
= 0; b
< layout
->binding_count
; b
++) {
336 if (layout
->binding
[b
].immutable_samplers
) {
337 for (uint32_t i
= 0; i
< layout
->binding
[b
].array_size
; i
++)
338 desc
[i
].sampler
= layout
->binding
[b
].immutable_samplers
[i
];
340 desc
+= layout
->binding
[b
].array_size
;
349 anv_descriptor_set_destroy(struct anv_device
*device
,
350 struct anv_descriptor_set
*set
)
352 anv_device_free(device
, set
);
355 VkResult
anv_AllocateDescriptorSets(
357 const VkDescriptorSetAllocateInfo
* pAllocateInfo
,
358 VkDescriptorSet
* pDescriptorSets
)
360 ANV_FROM_HANDLE(anv_device
, device
, _device
);
362 VkResult result
= VK_SUCCESS
;
363 struct anv_descriptor_set
*set
;
366 for (i
= 0; i
< pAllocateInfo
->setLayoutCount
; i
++) {
367 ANV_FROM_HANDLE(anv_descriptor_set_layout
, layout
,
368 pAllocateInfo
->pSetLayouts
[i
]);
370 result
= anv_descriptor_set_create(device
, layout
, &set
);
371 if (result
!= VK_SUCCESS
)
374 pDescriptorSets
[i
] = anv_descriptor_set_to_handle(set
);
377 if (result
!= VK_SUCCESS
)
378 anv_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
384 VkResult
anv_FreeDescriptorSets(
386 VkDescriptorPool descriptorPool
,
388 const VkDescriptorSet
* pDescriptorSets
)
390 ANV_FROM_HANDLE(anv_device
, device
, _device
);
392 for (uint32_t i
= 0; i
< count
; i
++) {
393 ANV_FROM_HANDLE(anv_descriptor_set
, set
, pDescriptorSets
[i
]);
395 anv_descriptor_set_destroy(device
, set
);
401 void anv_UpdateDescriptorSets(
403 uint32_t descriptorWriteCount
,
404 const VkWriteDescriptorSet
* pDescriptorWrites
,
405 uint32_t descriptorCopyCount
,
406 const VkCopyDescriptorSet
* pDescriptorCopies
)
408 for (uint32_t i
= 0; i
< descriptorWriteCount
; i
++) {
409 const VkWriteDescriptorSet
*write
= &pDescriptorWrites
[i
];
410 ANV_FROM_HANDLE(anv_descriptor_set
, set
, write
->dstSet
);
411 const struct anv_descriptor_set_binding_layout
*bind_layout
=
412 &set
->layout
->binding
[write
->dstBinding
];
413 struct anv_descriptor
*desc
=
414 &set
->descriptors
[bind_layout
->descriptor_index
];
416 switch (write
->descriptorType
) {
417 case VK_DESCRIPTOR_TYPE_SAMPLER
:
418 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
419 ANV_FROM_HANDLE(anv_sampler
, sampler
,
420 write
->pImageInfo
[j
].sampler
);
422 desc
[j
] = (struct anv_descriptor
) {
423 .type
= VK_DESCRIPTOR_TYPE_SAMPLER
,
429 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
430 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
431 ANV_FROM_HANDLE(anv_image_view
, iview
,
432 write
->pImageInfo
[j
].imageView
);
433 ANV_FROM_HANDLE(anv_sampler
, sampler
,
434 write
->pImageInfo
[j
].sampler
);
436 desc
[j
].type
= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
;
437 desc
[j
].image_view
= iview
;
439 /* If this descriptor has an immutable sampler, we don't want
443 desc
[j
].sampler
= sampler
;
447 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
448 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
449 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
450 ANV_FROM_HANDLE(anv_image_view
, iview
,
451 write
->pImageInfo
[j
].imageView
);
453 desc
[j
] = (struct anv_descriptor
) {
454 .type
= write
->descriptorType
,
460 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
461 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
462 anv_finishme("texel buffers not implemented");
465 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
466 anv_finishme("input attachments not implemented");
469 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
470 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
471 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
472 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
473 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
474 assert(write
->pBufferInfo
[j
].buffer
);
475 ANV_FROM_HANDLE(anv_buffer
, buffer
, write
->pBufferInfo
[j
].buffer
);
478 desc
[j
] = (struct anv_descriptor
) {
479 .type
= write
->descriptorType
,
481 .offset
= write
->pBufferInfo
[j
].offset
,
482 .range
= write
->pBufferInfo
[j
].range
,
485 /* For buffers with dynamic offsets, we use the full possible
486 * range in the surface state and do the actual range-checking
489 if (bind_layout
->dynamic_offset_index
>= 0)
490 desc
[j
].range
= buffer
->size
- desc
[j
].offset
;
498 for (uint32_t i
= 0; i
< descriptorCopyCount
; i
++) {
499 const VkCopyDescriptorSet
*copy
= &pDescriptorCopies
[i
];
500 ANV_FROM_HANDLE(anv_descriptor_set
, src
, copy
->dstSet
);
501 ANV_FROM_HANDLE(anv_descriptor_set
, dest
, copy
->dstSet
);
502 for (uint32_t j
= 0; j
< copy
->descriptorCount
; j
++) {
503 dest
->descriptors
[copy
->dstBinding
+ j
] =
504 src
->descriptors
[copy
->srcBinding
+ j
];