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 const VkAllocationCallbacks
* pAllocator
,
40 VkDescriptorSetLayout
* pSetLayout
)
42 ANV_FROM_HANDLE(anv_device
, device
, _device
);
43 struct anv_descriptor_set_layout
*set_layout
;
46 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO
);
48 uint32_t max_binding
= 0;
49 uint32_t immutable_sampler_count
= 0;
50 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
51 max_binding
= MAX2(max_binding
, pCreateInfo
->pBinding
[j
].binding
);
52 if (pCreateInfo
->pBinding
[j
].pImmutableSamplers
)
53 immutable_sampler_count
+= pCreateInfo
->pBinding
[j
].descriptorCount
;
56 size_t size
= sizeof(struct anv_descriptor_set_layout
) +
57 (max_binding
+ 1) * sizeof(set_layout
->binding
[0]) +
58 immutable_sampler_count
* sizeof(struct anv_sampler
*);
60 set_layout
= anv_alloc2(&device
->alloc
, pAllocator
, size
, 8,
61 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
63 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
65 /* We just allocate all the samplers at the end of the struct */
66 struct anv_sampler
**samplers
=
67 (struct anv_sampler
**)&set_layout
->binding
[max_binding
+ 1];
69 set_layout
->binding_count
= max_binding
+ 1;
70 set_layout
->shader_stages
= 0;
73 /* Initialize all binding_layout entries to -1 */
74 memset(set_layout
->binding
, -1,
75 (max_binding
+ 1) * sizeof(set_layout
->binding
[0]));
77 /* Initialize all samplers to 0 */
78 memset(samplers
, 0, immutable_sampler_count
* sizeof(*samplers
));
80 uint32_t sampler_count
[MESA_SHADER_STAGES
] = { 0, };
81 uint32_t surface_count
[MESA_SHADER_STAGES
] = { 0, };
82 uint32_t dynamic_offset_count
= 0;
84 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
85 const VkDescriptorSetLayoutBinding
*binding
= &pCreateInfo
->pBinding
[j
];
86 uint32_t b
= binding
->binding
;
88 assert(binding
->descriptorCount
> 0);
89 set_layout
->binding
[b
].array_size
= binding
->descriptorCount
;
90 set_layout
->binding
[b
].descriptor_index
= set_layout
->size
;
91 set_layout
->size
+= binding
->descriptorCount
;
93 switch (binding
->descriptorType
) {
94 case VK_DESCRIPTOR_TYPE_SAMPLER
:
95 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
96 for_each_bit(s
, binding
->stageFlags
) {
97 set_layout
->binding
[b
].stage
[s
].sampler_index
= sampler_count
[s
];
98 sampler_count
[s
] += binding
->descriptorCount
;
105 switch (binding
->descriptorType
) {
106 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
107 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
108 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
109 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
110 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
111 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
112 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
113 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
114 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
115 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
116 for_each_bit(s
, binding
->stageFlags
) {
117 set_layout
->binding
[b
].stage
[s
].surface_index
= surface_count
[s
];
118 surface_count
[s
] += binding
->descriptorCount
;
125 switch (binding
->descriptorType
) {
126 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
127 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
128 set_layout
->binding
[b
].dynamic_offset_index
= dynamic_offset_count
;
129 dynamic_offset_count
+= binding
->descriptorCount
;
135 if (binding
->pImmutableSamplers
) {
136 set_layout
->binding
[b
].immutable_samplers
= samplers
;
137 samplers
+= binding
->descriptorCount
;
139 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++)
140 set_layout
->binding
[b
].immutable_samplers
[i
] =
141 anv_sampler_from_handle(binding
->pImmutableSamplers
[i
]);
143 set_layout
->binding
[b
].immutable_samplers
= NULL
;
146 set_layout
->shader_stages
|= binding
->stageFlags
;
149 set_layout
->dynamic_offset_count
= dynamic_offset_count
;
151 *pSetLayout
= anv_descriptor_set_layout_to_handle(set_layout
);
156 void anv_DestroyDescriptorSetLayout(
158 VkDescriptorSetLayout _set_layout
,
159 const VkAllocationCallbacks
* pAllocator
)
161 ANV_FROM_HANDLE(anv_device
, device
, _device
);
162 ANV_FROM_HANDLE(anv_descriptor_set_layout
, set_layout
, _set_layout
);
164 anv_free2(&device
->alloc
, pAllocator
, set_layout
);
168 * Pipeline layouts. These have nothing to do with the pipeline. They are
169 * just muttiple descriptor set layouts pasted together
172 VkResult
anv_CreatePipelineLayout(
174 const VkPipelineLayoutCreateInfo
* pCreateInfo
,
175 const VkAllocationCallbacks
* pAllocator
,
176 VkPipelineLayout
* pPipelineLayout
)
178 ANV_FROM_HANDLE(anv_device
, device
, _device
);
179 struct anv_pipeline_layout l
, *layout
;
181 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
);
183 l
.num_sets
= pCreateInfo
->setLayoutCount
;
185 unsigned dynamic_offset_count
= 0;
187 memset(l
.stage
, 0, sizeof(l
.stage
));
188 for (uint32_t set
= 0; set
< pCreateInfo
->setLayoutCount
; set
++) {
189 ANV_FROM_HANDLE(anv_descriptor_set_layout
, set_layout
,
190 pCreateInfo
->pSetLayouts
[set
]);
191 l
.set
[set
].layout
= set_layout
;
193 l
.set
[set
].dynamic_offset_start
= dynamic_offset_count
;
194 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
195 if (set_layout
->binding
[b
].dynamic_offset_index
>= 0)
196 dynamic_offset_count
+= set_layout
->binding
[b
].array_size
;
199 for (gl_shader_stage s
= 0; s
< MESA_SHADER_STAGES
; s
++) {
200 l
.set
[set
].stage
[s
].surface_start
= l
.stage
[s
].surface_count
;
201 l
.set
[set
].stage
[s
].sampler_start
= l
.stage
[s
].sampler_count
;
203 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
204 unsigned array_size
= set_layout
->binding
[b
].array_size
;
206 if (set_layout
->binding
[b
].stage
[s
].surface_index
>= 0) {
207 l
.stage
[s
].surface_count
+= array_size
;
209 if (set_layout
->binding
[b
].dynamic_offset_index
>= 0)
210 l
.stage
[s
].has_dynamic_offsets
= true;
213 if (set_layout
->binding
[b
].stage
[s
].sampler_index
>= 0)
214 l
.stage
[s
].sampler_count
+= array_size
;
219 unsigned num_bindings
= 0;
220 for (gl_shader_stage s
= 0; s
< MESA_SHADER_STAGES
; s
++)
221 num_bindings
+= l
.stage
[s
].surface_count
+ l
.stage
[s
].sampler_count
;
223 size_t size
= sizeof(*layout
) + num_bindings
* sizeof(layout
->entries
[0]);
225 layout
= anv_alloc2(&device
->alloc
, pAllocator
, size
, 8,
226 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
228 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
230 /* Now we can actually build our surface and sampler maps */
231 struct anv_pipeline_binding
*entry
= layout
->entries
;
232 for (gl_shader_stage s
= 0; s
< MESA_SHADER_STAGES
; s
++) {
233 l
.stage
[s
].surface_to_descriptor
= entry
;
234 entry
+= l
.stage
[s
].surface_count
;
235 l
.stage
[s
].sampler_to_descriptor
= entry
;
236 entry
+= l
.stage
[s
].sampler_count
;
240 for (uint32_t set
= 0; set
< pCreateInfo
->setLayoutCount
; set
++) {
241 struct anv_descriptor_set_layout
*set_layout
= l
.set
[set
].layout
;
243 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
244 unsigned array_size
= set_layout
->binding
[b
].array_size
;
245 unsigned set_offset
= set_layout
->binding
[b
].descriptor_index
;
247 if (set_layout
->binding
[b
].stage
[s
].surface_index
>= 0) {
248 assert(surface
== l
.set
[set
].stage
[s
].surface_start
+
249 set_layout
->binding
[b
].stage
[s
].surface_index
);
250 for (unsigned i
= 0; i
< array_size
; i
++) {
251 l
.stage
[s
].surface_to_descriptor
[surface
+ i
].set
= set
;
252 l
.stage
[s
].surface_to_descriptor
[surface
+ i
].offset
= set_offset
+ i
;
254 surface
+= array_size
;
257 if (set_layout
->binding
[b
].stage
[s
].sampler_index
>= 0) {
258 assert(sampler
== l
.set
[set
].stage
[s
].sampler_start
+
259 set_layout
->binding
[b
].stage
[s
].sampler_index
);
260 for (unsigned i
= 0; i
< array_size
; i
++) {
261 l
.stage
[s
].sampler_to_descriptor
[sampler
+ i
].set
= set
;
262 l
.stage
[s
].sampler_to_descriptor
[sampler
+ i
].offset
= set_offset
+ i
;
264 sampler
+= array_size
;
270 /* Finally, we're done setting it up, copy into the allocated version */
273 *pPipelineLayout
= anv_pipeline_layout_to_handle(layout
);
278 void anv_DestroyPipelineLayout(
280 VkPipelineLayout _pipelineLayout
,
281 const VkAllocationCallbacks
* pAllocator
)
283 ANV_FROM_HANDLE(anv_device
, device
, _device
);
284 ANV_FROM_HANDLE(anv_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
286 anv_free2(&device
->alloc
, pAllocator
, pipeline_layout
);
290 * Descriptor pools. These are a no-op for now.
293 VkResult
anv_CreateDescriptorPool(
295 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
296 const VkAllocationCallbacks
* pAllocator
,
297 VkDescriptorPool
* pDescriptorPool
)
299 anv_finishme("VkDescriptorPool is a stub");
300 *pDescriptorPool
= (VkDescriptorPool
)1;
304 void anv_DestroyDescriptorPool(
306 VkDescriptorPool _pool
,
307 const VkAllocationCallbacks
* pAllocator
)
309 anv_finishme("VkDescriptorPool is a stub: free the pool's descriptor sets");
312 VkResult
anv_ResetDescriptorPool(
314 VkDescriptorPool descriptorPool
,
315 VkDescriptorPoolResetFlags flags
)
317 anv_finishme("VkDescriptorPool is a stub: free the pool's descriptor sets");
322 anv_descriptor_set_create(struct anv_device
*device
,
323 const struct anv_descriptor_set_layout
*layout
,
324 struct anv_descriptor_set
**out_set
)
326 struct anv_descriptor_set
*set
;
327 size_t size
= sizeof(*set
) + layout
->size
* sizeof(set
->descriptors
[0]);
329 set
= anv_alloc(&device
->alloc
/* XXX: Use the pool */, size
, 8,
330 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
332 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
334 /* A descriptor set may not be 100% filled. Clear the set so we can can
335 * later detect holes in it.
337 memset(set
, 0, size
);
339 set
->layout
= layout
;
341 /* Go through and fill out immutable samplers if we have any */
342 struct anv_descriptor
*desc
= set
->descriptors
;
343 for (uint32_t b
= 0; b
< layout
->binding_count
; b
++) {
344 if (layout
->binding
[b
].immutable_samplers
) {
345 for (uint32_t i
= 0; i
< layout
->binding
[b
].array_size
; i
++)
346 desc
[i
].sampler
= layout
->binding
[b
].immutable_samplers
[i
];
348 desc
+= layout
->binding
[b
].array_size
;
357 anv_descriptor_set_destroy(struct anv_device
*device
,
358 struct anv_descriptor_set
*set
)
360 anv_free(&device
->alloc
/* XXX: Use the pool */, set
);
363 VkResult
anv_AllocateDescriptorSets(
365 const VkDescriptorSetAllocateInfo
* pAllocateInfo
,
366 VkDescriptorSet
* pDescriptorSets
)
368 ANV_FROM_HANDLE(anv_device
, device
, _device
);
370 VkResult result
= VK_SUCCESS
;
371 struct anv_descriptor_set
*set
;
374 for (i
= 0; i
< pAllocateInfo
->setLayoutCount
; i
++) {
375 ANV_FROM_HANDLE(anv_descriptor_set_layout
, layout
,
376 pAllocateInfo
->pSetLayouts
[i
]);
378 result
= anv_descriptor_set_create(device
, layout
, &set
);
379 if (result
!= VK_SUCCESS
)
382 pDescriptorSets
[i
] = anv_descriptor_set_to_handle(set
);
385 if (result
!= VK_SUCCESS
)
386 anv_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
392 VkResult
anv_FreeDescriptorSets(
394 VkDescriptorPool descriptorPool
,
396 const VkDescriptorSet
* pDescriptorSets
)
398 ANV_FROM_HANDLE(anv_device
, device
, _device
);
400 for (uint32_t i
= 0; i
< count
; i
++) {
401 ANV_FROM_HANDLE(anv_descriptor_set
, set
, pDescriptorSets
[i
]);
403 anv_descriptor_set_destroy(device
, set
);
409 void anv_UpdateDescriptorSets(
411 uint32_t descriptorWriteCount
,
412 const VkWriteDescriptorSet
* pDescriptorWrites
,
413 uint32_t descriptorCopyCount
,
414 const VkCopyDescriptorSet
* pDescriptorCopies
)
416 for (uint32_t i
= 0; i
< descriptorWriteCount
; i
++) {
417 const VkWriteDescriptorSet
*write
= &pDescriptorWrites
[i
];
418 ANV_FROM_HANDLE(anv_descriptor_set
, set
, write
->dstSet
);
419 const struct anv_descriptor_set_binding_layout
*bind_layout
=
420 &set
->layout
->binding
[write
->dstBinding
];
421 struct anv_descriptor
*desc
=
422 &set
->descriptors
[bind_layout
->descriptor_index
];
424 switch (write
->descriptorType
) {
425 case VK_DESCRIPTOR_TYPE_SAMPLER
:
426 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
427 ANV_FROM_HANDLE(anv_sampler
, sampler
,
428 write
->pImageInfo
[j
].sampler
);
430 desc
[j
] = (struct anv_descriptor
) {
431 .type
= VK_DESCRIPTOR_TYPE_SAMPLER
,
437 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
438 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
439 ANV_FROM_HANDLE(anv_image_view
, iview
,
440 write
->pImageInfo
[j
].imageView
);
441 ANV_FROM_HANDLE(anv_sampler
, sampler
,
442 write
->pImageInfo
[j
].sampler
);
444 desc
[j
].type
= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
;
445 desc
[j
].image_view
= iview
;
447 /* If this descriptor has an immutable sampler, we don't want
451 desc
[j
].sampler
= sampler
;
455 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
456 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
457 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
458 ANV_FROM_HANDLE(anv_image_view
, iview
,
459 write
->pImageInfo
[j
].imageView
);
461 desc
[j
] = (struct anv_descriptor
) {
462 .type
= write
->descriptorType
,
468 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
469 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
470 anv_finishme("texel buffers not implemented");
473 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
474 anv_finishme("input attachments not implemented");
477 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
478 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
479 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
480 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
481 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
482 assert(write
->pBufferInfo
[j
].buffer
);
483 ANV_FROM_HANDLE(anv_buffer
, buffer
, write
->pBufferInfo
[j
].buffer
);
486 desc
[j
] = (struct anv_descriptor
) {
487 .type
= write
->descriptorType
,
489 .offset
= write
->pBufferInfo
[j
].offset
,
490 .range
= write
->pBufferInfo
[j
].range
,
493 /* For buffers with dynamic offsets, we use the full possible
494 * range in the surface state and do the actual range-checking
497 if (bind_layout
->dynamic_offset_index
>= 0)
498 desc
[j
].range
= buffer
->size
- desc
[j
].offset
;
506 for (uint32_t i
= 0; i
< descriptorCopyCount
; i
++) {
507 const VkCopyDescriptorSet
*copy
= &pDescriptorCopies
[i
];
508 ANV_FROM_HANDLE(anv_descriptor_set
, src
, copy
->dstSet
);
509 ANV_FROM_HANDLE(anv_descriptor_set
, dest
, copy
->dstSet
);
510 for (uint32_t j
= 0; j
< copy
->descriptorCount
; j
++) {
511 dest
->descriptors
[copy
->dstBinding
+ j
] =
512 src
->descriptors
[copy
->srcBinding
+ j
];