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
;
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
->pBindings
[j
].binding
);
51 if (pCreateInfo
->pBindings
[j
].pImmutableSamplers
)
52 immutable_sampler_count
+= pCreateInfo
->pBindings
[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_alloc2(&device
->alloc
, pAllocator
, size
, 8,
60 VK_SYSTEM_ALLOCATION_SCOPE_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 for (uint32_t b
= 0; b
<= max_binding
; b
++) {
73 /* Initialize all binding_layout entries to -1 */
74 memset(&set_layout
->binding
[b
], -1, sizeof(set_layout
->binding
[b
]));
76 set_layout
->binding
[b
].immutable_samplers
= NULL
;
79 /* Initialize all samplers to 0 */
80 memset(samplers
, 0, immutable_sampler_count
* sizeof(*samplers
));
82 uint32_t sampler_count
[MESA_SHADER_STAGES
] = { 0, };
83 uint32_t surface_count
[MESA_SHADER_STAGES
] = { 0, };
84 uint32_t image_count
[MESA_SHADER_STAGES
] = { 0, };
85 uint32_t buffer_count
= 0;
86 uint32_t dynamic_offset_count
= 0;
88 for (uint32_t j
= 0; j
< pCreateInfo
->bindingCount
; j
++) {
89 const VkDescriptorSetLayoutBinding
*binding
= &pCreateInfo
->pBindings
[j
];
90 uint32_t b
= binding
->binding
;
92 assert(binding
->descriptorCount
> 0);
93 set_layout
->binding
[b
].array_size
= binding
->descriptorCount
;
94 set_layout
->binding
[b
].descriptor_index
= set_layout
->size
;
95 set_layout
->size
+= binding
->descriptorCount
;
97 switch (binding
->descriptorType
) {
98 case VK_DESCRIPTOR_TYPE_SAMPLER
:
99 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
100 anv_foreach_stage(s
, binding
->stageFlags
) {
101 set_layout
->binding
[b
].stage
[s
].sampler_index
= sampler_count
[s
];
102 sampler_count
[s
] += binding
->descriptorCount
;
109 switch (binding
->descriptorType
) {
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 set_layout
->binding
[b
].buffer_index
= buffer_count
;
115 buffer_count
+= binding
->descriptorCount
;
118 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
119 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
120 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
121 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
122 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
123 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
124 anv_foreach_stage(s
, binding
->stageFlags
) {
125 set_layout
->binding
[b
].stage
[s
].surface_index
= surface_count
[s
];
126 surface_count
[s
] += binding
->descriptorCount
;
133 switch (binding
->descriptorType
) {
134 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
135 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
136 set_layout
->binding
[b
].dynamic_offset_index
= dynamic_offset_count
;
137 dynamic_offset_count
+= binding
->descriptorCount
;
143 switch (binding
->descriptorType
) {
144 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
145 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
146 anv_foreach_stage(s
, binding
->stageFlags
) {
147 set_layout
->binding
[b
].stage
[s
].image_index
= image_count
[s
];
148 image_count
[s
] += binding
->descriptorCount
;
155 if (binding
->pImmutableSamplers
) {
156 set_layout
->binding
[b
].immutable_samplers
= samplers
;
157 samplers
+= binding
->descriptorCount
;
159 for (uint32_t i
= 0; i
< binding
->descriptorCount
; i
++)
160 set_layout
->binding
[b
].immutable_samplers
[i
] =
161 anv_sampler_from_handle(binding
->pImmutableSamplers
[i
]);
163 set_layout
->binding
[b
].immutable_samplers
= NULL
;
166 set_layout
->shader_stages
|= binding
->stageFlags
;
169 set_layout
->buffer_count
= buffer_count
;
170 set_layout
->dynamic_offset_count
= dynamic_offset_count
;
172 *pSetLayout
= anv_descriptor_set_layout_to_handle(set_layout
);
177 void anv_DestroyDescriptorSetLayout(
179 VkDescriptorSetLayout _set_layout
,
180 const VkAllocationCallbacks
* pAllocator
)
182 ANV_FROM_HANDLE(anv_device
, device
, _device
);
183 ANV_FROM_HANDLE(anv_descriptor_set_layout
, set_layout
, _set_layout
);
185 anv_free2(&device
->alloc
, pAllocator
, set_layout
);
189 * Pipeline layouts. These have nothing to do with the pipeline. They are
190 * just muttiple descriptor set layouts pasted together
193 VkResult
anv_CreatePipelineLayout(
195 const VkPipelineLayoutCreateInfo
* pCreateInfo
,
196 const VkAllocationCallbacks
* pAllocator
,
197 VkPipelineLayout
* pPipelineLayout
)
199 ANV_FROM_HANDLE(anv_device
, device
, _device
);
200 struct anv_pipeline_layout
*layout
;
202 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO
);
204 layout
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*layout
), 8,
205 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
207 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
209 layout
->num_sets
= pCreateInfo
->setLayoutCount
;
211 unsigned dynamic_offset_count
= 0;
213 memset(layout
->stage
, 0, sizeof(layout
->stage
));
214 for (uint32_t set
= 0; set
< pCreateInfo
->setLayoutCount
; set
++) {
215 ANV_FROM_HANDLE(anv_descriptor_set_layout
, set_layout
,
216 pCreateInfo
->pSetLayouts
[set
]);
217 layout
->set
[set
].layout
= set_layout
;
219 layout
->set
[set
].dynamic_offset_start
= dynamic_offset_count
;
220 for (uint32_t b
= 0; b
< set_layout
->binding_count
; b
++) {
221 if (set_layout
->binding
[b
].dynamic_offset_index
>= 0)
222 dynamic_offset_count
+= set_layout
->binding
[b
].array_size
;
223 for (gl_shader_stage s
= 0; s
< MESA_SHADER_STAGES
; s
++) {
224 if (set_layout
->binding
[b
].stage
[s
].surface_index
>= 0)
225 layout
->stage
[s
].has_dynamic_offsets
= true;
230 *pPipelineLayout
= anv_pipeline_layout_to_handle(layout
);
235 void anv_DestroyPipelineLayout(
237 VkPipelineLayout _pipelineLayout
,
238 const VkAllocationCallbacks
* pAllocator
)
240 ANV_FROM_HANDLE(anv_device
, device
, _device
);
241 ANV_FROM_HANDLE(anv_pipeline_layout
, pipeline_layout
, _pipelineLayout
);
243 anv_free2(&device
->alloc
, pAllocator
, pipeline_layout
);
247 * Descriptor pools. These are a no-op for now.
250 VkResult
anv_CreateDescriptorPool(
252 const VkDescriptorPoolCreateInfo
* pCreateInfo
,
253 const VkAllocationCallbacks
* pAllocator
,
254 VkDescriptorPool
* pDescriptorPool
)
256 anv_finishme("VkDescriptorPool is a stub");
257 *pDescriptorPool
= (VkDescriptorPool
)1;
261 void anv_DestroyDescriptorPool(
263 VkDescriptorPool _pool
,
264 const VkAllocationCallbacks
* pAllocator
)
266 anv_finishme("VkDescriptorPool is a stub: free the pool's descriptor sets");
269 VkResult
anv_ResetDescriptorPool(
271 VkDescriptorPool descriptorPool
,
272 VkDescriptorPoolResetFlags flags
)
274 anv_finishme("VkDescriptorPool is a stub: free the pool's descriptor sets");
279 anv_descriptor_set_create(struct anv_device
*device
,
280 const struct anv_descriptor_set_layout
*layout
,
281 struct anv_descriptor_set
**out_set
)
283 struct anv_descriptor_set
*set
;
284 size_t size
= sizeof(*set
) + layout
->size
* sizeof(set
->descriptors
[0]);
286 set
= anv_alloc(&device
->alloc
/* XXX: Use the pool */, size
, 8,
287 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
289 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
291 /* A descriptor set may not be 100% filled. Clear the set so we can can
292 * later detect holes in it.
294 memset(set
, 0, size
);
296 set
->layout
= layout
;
298 /* Go through and fill out immutable samplers if we have any */
299 struct anv_descriptor
*desc
= set
->descriptors
;
300 for (uint32_t b
= 0; b
< layout
->binding_count
; b
++) {
301 if (layout
->binding
[b
].immutable_samplers
) {
302 for (uint32_t i
= 0; i
< layout
->binding
[b
].array_size
; i
++)
303 desc
[i
].sampler
= layout
->binding
[b
].immutable_samplers
[i
];
305 desc
+= layout
->binding
[b
].array_size
;
308 /* XXX: Use the pool */
310 anv_alloc(&device
->alloc
,
311 sizeof(set
->buffer_views
[0]) * layout
->buffer_count
, 8,
312 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
313 if (!set
->buffer_views
) {
314 anv_free(&device
->alloc
, set
);
315 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
318 for (uint32_t b
= 0; b
< layout
->buffer_count
; b
++) {
319 set
->buffer_views
[b
].surface_state
=
320 anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
322 set
->buffer_count
= layout
->buffer_count
;
329 anv_descriptor_set_destroy(struct anv_device
*device
,
330 struct anv_descriptor_set
*set
)
332 /* XXX: Use the pool */
333 for (uint32_t b
= 0; b
< set
->buffer_count
; b
++)
334 anv_state_pool_free(&device
->surface_state_pool
,
335 set
->buffer_views
[b
].surface_state
);
337 anv_free(&device
->alloc
, set
->buffer_views
);
338 anv_free(&device
->alloc
, set
);
341 VkResult
anv_AllocateDescriptorSets(
343 const VkDescriptorSetAllocateInfo
* pAllocateInfo
,
344 VkDescriptorSet
* pDescriptorSets
)
346 ANV_FROM_HANDLE(anv_device
, device
, _device
);
348 VkResult result
= VK_SUCCESS
;
349 struct anv_descriptor_set
*set
;
352 for (i
= 0; i
< pAllocateInfo
->descriptorSetCount
; i
++) {
353 ANV_FROM_HANDLE(anv_descriptor_set_layout
, layout
,
354 pAllocateInfo
->pSetLayouts
[i
]);
356 result
= anv_descriptor_set_create(device
, layout
, &set
);
357 if (result
!= VK_SUCCESS
)
360 pDescriptorSets
[i
] = anv_descriptor_set_to_handle(set
);
363 if (result
!= VK_SUCCESS
)
364 anv_FreeDescriptorSets(_device
, pAllocateInfo
->descriptorPool
,
370 VkResult
anv_FreeDescriptorSets(
372 VkDescriptorPool descriptorPool
,
374 const VkDescriptorSet
* pDescriptorSets
)
376 ANV_FROM_HANDLE(anv_device
, device
, _device
);
378 for (uint32_t i
= 0; i
< count
; i
++) {
379 ANV_FROM_HANDLE(anv_descriptor_set
, set
, pDescriptorSets
[i
]);
381 anv_descriptor_set_destroy(device
, set
);
387 void anv_UpdateDescriptorSets(
389 uint32_t descriptorWriteCount
,
390 const VkWriteDescriptorSet
* pDescriptorWrites
,
391 uint32_t descriptorCopyCount
,
392 const VkCopyDescriptorSet
* pDescriptorCopies
)
394 ANV_FROM_HANDLE(anv_device
, device
, _device
);
396 for (uint32_t i
= 0; i
< descriptorWriteCount
; i
++) {
397 const VkWriteDescriptorSet
*write
= &pDescriptorWrites
[i
];
398 ANV_FROM_HANDLE(anv_descriptor_set
, set
, write
->dstSet
);
399 const struct anv_descriptor_set_binding_layout
*bind_layout
=
400 &set
->layout
->binding
[write
->dstBinding
];
401 struct anv_descriptor
*desc
=
402 &set
->descriptors
[bind_layout
->descriptor_index
];
403 desc
+= write
->dstArrayElement
;
405 switch (write
->descriptorType
) {
406 case VK_DESCRIPTOR_TYPE_SAMPLER
:
407 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
408 ANV_FROM_HANDLE(anv_sampler
, sampler
,
409 write
->pImageInfo
[j
].sampler
);
411 desc
[j
] = (struct anv_descriptor
) {
412 .type
= VK_DESCRIPTOR_TYPE_SAMPLER
,
418 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
419 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
420 ANV_FROM_HANDLE(anv_image_view
, iview
,
421 write
->pImageInfo
[j
].imageView
);
422 ANV_FROM_HANDLE(anv_sampler
, sampler
,
423 write
->pImageInfo
[j
].sampler
);
425 desc
[j
].type
= VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
;
426 desc
[j
].image_view
= iview
;
428 /* If this descriptor has an immutable sampler, we don't want
432 desc
[j
].sampler
= sampler
;
436 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE
:
437 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
:
438 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
439 ANV_FROM_HANDLE(anv_image_view
, iview
,
440 write
->pImageInfo
[j
].imageView
);
442 desc
[j
] = (struct anv_descriptor
) {
443 .type
= write
->descriptorType
,
449 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER
:
450 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
:
451 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
452 ANV_FROM_HANDLE(anv_buffer_view
, bview
,
453 write
->pTexelBufferView
[j
]);
455 desc
[j
] = (struct anv_descriptor
) {
456 .type
= write
->descriptorType
,
457 .buffer_view
= bview
,
462 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
:
463 anv_finishme("input attachments not implemented");
466 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
467 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
:
468 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
:
469 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
:
470 for (uint32_t j
= 0; j
< write
->descriptorCount
; j
++) {
471 assert(write
->pBufferInfo
[j
].buffer
);
472 ANV_FROM_HANDLE(anv_buffer
, buffer
, write
->pBufferInfo
[j
].buffer
);
475 struct anv_buffer_view
*view
=
476 &set
->buffer_views
[bind_layout
->buffer_index
];
477 view
+= write
->dstArrayElement
+ j
;
479 const struct anv_format
*format
=
480 anv_format_for_descriptor_type(write
->descriptorType
);
482 view
->format
= format
->isl_format
;
483 view
->bo
= buffer
->bo
;
484 view
->offset
= buffer
->offset
+ write
->pBufferInfo
[j
].offset
;
486 /* For buffers with dynamic offsets, we use the full possible
487 * range in the surface state and do the actual range-checking
490 if (bind_layout
->dynamic_offset_index
>= 0 ||
491 write
->pBufferInfo
[j
].range
== VK_WHOLE_SIZE
)
492 view
->range
= buffer
->size
- write
->pBufferInfo
[j
].offset
;
494 view
->range
= write
->pBufferInfo
[j
].range
;
496 anv_fill_buffer_surface_state(device
, view
->surface_state
,
498 view
->offset
, view
->range
, 1);
500 desc
[j
] = (struct anv_descriptor
) {
501 .type
= write
->descriptorType
,
512 for (uint32_t i
= 0; i
< descriptorCopyCount
; i
++) {
513 const VkCopyDescriptorSet
*copy
= &pDescriptorCopies
[i
];
514 ANV_FROM_HANDLE(anv_descriptor_set
, src
, copy
->dstSet
);
515 ANV_FROM_HANDLE(anv_descriptor_set
, dst
, copy
->dstSet
);
517 const struct anv_descriptor_set_binding_layout
*src_layout
=
518 &src
->layout
->binding
[copy
->srcBinding
];
519 struct anv_descriptor
*src_desc
=
520 &src
->descriptors
[src_layout
->descriptor_index
];
521 src_desc
+= copy
->srcArrayElement
;
523 const struct anv_descriptor_set_binding_layout
*dst_layout
=
524 &dst
->layout
->binding
[copy
->dstBinding
];
525 struct anv_descriptor
*dst_desc
=
526 &dst
->descriptors
[dst_layout
->descriptor_index
];
527 dst_desc
+= copy
->dstArrayElement
;
529 for (uint32_t j
= 0; j
< copy
->descriptorCount
; j
++)
530 dst_desc
[j
] = src_desc
[j
];