vk/0.210.0: Rework descriptor sets
[mesa.git] / src / vulkan / anv_descriptor_set.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "anv_private.h"
31
32 /*
33 * Descriptor set layouts.
34 */
35
36 VkResult anv_CreateDescriptorSetLayout(
37 VkDevice _device,
38 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
39 VkDescriptorSetLayout* pSetLayout)
40 {
41 ANV_FROM_HANDLE(anv_device, device, _device);
42 struct anv_descriptor_set_layout *set_layout;
43 uint32_t s;
44
45 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO);
46
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;
53 }
54
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 *);
58
59 set_layout = anv_device_alloc(device, size, 8,
60 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
61 if (!set_layout)
62 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
63
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];
67
68 set_layout->binding_count = max_binding + 1;
69 set_layout->shader_stages = 0;
70 set_layout->size = 0;
71
72 /* Initialize all binding_layout entries to -1 */
73 memset(set_layout->binding, -1,
74 (max_binding + 1) * sizeof(set_layout->binding[0]));
75
76 /* Initialize all samplers to 0 */
77 memset(samplers, 0, immutable_sampler_count * sizeof(*samplers));
78
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;
82
83 for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
84 const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBinding[j];
85 uint32_t b = binding->binding;
86
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;
91
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;
98 }
99 break;
100 default:
101 break;
102 }
103
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;
118 }
119 break;
120 default:
121 break;
122 }
123
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;
129 break;
130 default:
131 break;
132 }
133
134 if (binding->pImmutableSamplers) {
135 set_layout->binding[b].immutable_samplers = samplers;
136 samplers += binding->descriptorCount;
137
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]);
141 } else {
142 set_layout->binding[b].immutable_samplers = NULL;
143 }
144
145 set_layout->shader_stages |= binding->stageFlags;
146 }
147
148 set_layout->dynamic_offset_count = dynamic_offset_count;
149
150 *pSetLayout = anv_descriptor_set_layout_to_handle(set_layout);
151
152 return VK_SUCCESS;
153 }
154
155 void anv_DestroyDescriptorSetLayout(
156 VkDevice _device,
157 VkDescriptorSetLayout _set_layout)
158 {
159 ANV_FROM_HANDLE(anv_device, device, _device);
160 ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout, _set_layout);
161
162 anv_device_free(device, set_layout);
163 }
164
165 /*
166 * Pipeline layouts. These have nothing to do with the pipeline. They are
167 * just muttiple descriptor set layouts pasted together
168 */
169
170 VkResult anv_CreatePipelineLayout(
171 VkDevice _device,
172 const VkPipelineLayoutCreateInfo* pCreateInfo,
173 VkPipelineLayout* pPipelineLayout)
174 {
175 ANV_FROM_HANDLE(anv_device, device, _device);
176 struct anv_pipeline_layout l, *layout;
177
178 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
179
180 l.num_sets = pCreateInfo->setLayoutCount;
181
182 unsigned dynamic_offset_count = 0;
183
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;
189
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;
194 }
195
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;
199
200 for (uint32_t b = 0; b < set_layout->binding_count; b++) {
201 unsigned array_size = set_layout->binding[b].array_size;
202
203 if (set_layout->binding[b].stage[s].surface_index >= 0) {
204 l.stage[s].surface_count += array_size;
205
206 if (set_layout->binding[b].dynamic_offset_index >= 0)
207 l.stage[s].has_dynamic_offsets = true;
208 }
209
210 if (set_layout->binding[b].stage[s].sampler_index >= 0)
211 l.stage[s].sampler_count += array_size;
212 }
213 }
214 }
215
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;
219
220 size_t size = sizeof(*layout) + num_bindings * sizeof(layout->entries[0]);
221
222 layout = anv_device_alloc(device, size, 8, VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
223 if (layout == NULL)
224 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
225
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;
233
234 int surface = 0;
235 int sampler = 0;
236 for (uint32_t set = 0; set < pCreateInfo->setLayoutCount; set++) {
237 struct anv_descriptor_set_layout *set_layout = l.set[set].layout;
238
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;
242
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;
249 }
250 surface += array_size;
251 }
252
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;
259 }
260 sampler += array_size;
261 }
262 }
263 }
264 }
265
266 /* Finally, we're done setting it up, copy into the allocated version */
267 *layout = l;
268
269 *pPipelineLayout = anv_pipeline_layout_to_handle(layout);
270
271 return VK_SUCCESS;
272 }
273
274 void anv_DestroyPipelineLayout(
275 VkDevice _device,
276 VkPipelineLayout _pipelineLayout)
277 {
278 ANV_FROM_HANDLE(anv_device, device, _device);
279 ANV_FROM_HANDLE(anv_pipeline_layout, pipeline_layout, _pipelineLayout);
280
281 anv_device_free(device, pipeline_layout);
282 }
283
284 /*
285 * Descriptor pools. These are a no-op for now.
286 */
287
288 VkResult anv_CreateDescriptorPool(
289 VkDevice device,
290 const VkDescriptorPoolCreateInfo* pCreateInfo,
291 VkDescriptorPool* pDescriptorPool)
292 {
293 anv_finishme("VkDescriptorPool is a stub");
294 *pDescriptorPool = (VkDescriptorPool)1;
295 return VK_SUCCESS;
296 }
297
298 void anv_DestroyDescriptorPool(
299 VkDevice _device,
300 VkDescriptorPool _pool)
301 {
302 anv_finishme("VkDescriptorPool is a stub: free the pool's descriptor sets");
303 }
304
305 VkResult anv_ResetDescriptorPool(
306 VkDevice device,
307 VkDescriptorPool descriptorPool,
308 VkDescriptorPoolResetFlags flags)
309 {
310 anv_finishme("VkDescriptorPool is a stub: free the pool's descriptor sets");
311 return VK_SUCCESS;
312 }
313
314 VkResult
315 anv_descriptor_set_create(struct anv_device *device,
316 const struct anv_descriptor_set_layout *layout,
317 struct anv_descriptor_set **out_set)
318 {
319 struct anv_descriptor_set *set;
320 size_t size = sizeof(*set) + layout->size * sizeof(set->descriptors[0]);
321
322 set = anv_device_alloc(device, size, 8, VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
323 if (!set)
324 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
325
326 /* A descriptor set may not be 100% filled. Clear the set so we can can
327 * later detect holes in it.
328 */
329 memset(set, 0, size);
330
331 set->layout = layout;
332
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];
339 }
340 desc += layout->binding[b].array_size;
341 }
342
343 *out_set = set;
344
345 return VK_SUCCESS;
346 }
347
348 void
349 anv_descriptor_set_destroy(struct anv_device *device,
350 struct anv_descriptor_set *set)
351 {
352 anv_device_free(device, set);
353 }
354
355 VkResult anv_AllocateDescriptorSets(
356 VkDevice _device,
357 const VkDescriptorSetAllocateInfo* pAllocateInfo,
358 VkDescriptorSet* pDescriptorSets)
359 {
360 ANV_FROM_HANDLE(anv_device, device, _device);
361
362 VkResult result = VK_SUCCESS;
363 struct anv_descriptor_set *set;
364 uint32_t i;
365
366 for (i = 0; i < pAllocateInfo->setLayoutCount; i++) {
367 ANV_FROM_HANDLE(anv_descriptor_set_layout, layout,
368 pAllocateInfo->pSetLayouts[i]);
369
370 result = anv_descriptor_set_create(device, layout, &set);
371 if (result != VK_SUCCESS)
372 break;
373
374 pDescriptorSets[i] = anv_descriptor_set_to_handle(set);
375 }
376
377 if (result != VK_SUCCESS)
378 anv_FreeDescriptorSets(_device, pAllocateInfo->descriptorPool,
379 i, pDescriptorSets);
380
381 return result;
382 }
383
384 VkResult anv_FreeDescriptorSets(
385 VkDevice _device,
386 VkDescriptorPool descriptorPool,
387 uint32_t count,
388 const VkDescriptorSet* pDescriptorSets)
389 {
390 ANV_FROM_HANDLE(anv_device, device, _device);
391
392 for (uint32_t i = 0; i < count; i++) {
393 ANV_FROM_HANDLE(anv_descriptor_set, set, pDescriptorSets[i]);
394
395 anv_descriptor_set_destroy(device, set);
396 }
397
398 return VK_SUCCESS;
399 }
400
401 void anv_UpdateDescriptorSets(
402 VkDevice device,
403 uint32_t descriptorWriteCount,
404 const VkWriteDescriptorSet* pDescriptorWrites,
405 uint32_t descriptorCopyCount,
406 const VkCopyDescriptorSet* pDescriptorCopies)
407 {
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];
415
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);
421
422 desc[j] = (struct anv_descriptor) {
423 .type = VK_DESCRIPTOR_TYPE_SAMPLER,
424 .sampler = sampler,
425 };
426 }
427 break;
428
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);
435
436 desc[j].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
437 desc[j].image_view = iview;
438
439 /* If this descriptor has an immutable sampler, we don't want
440 * to stomp on it.
441 */
442 if (sampler)
443 desc[j].sampler = sampler;
444 }
445 break;
446
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);
452
453 desc[j] = (struct anv_descriptor) {
454 .type = write->descriptorType,
455 .image_view = iview,
456 };
457 }
458 break;
459
460 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
461 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
462 anv_finishme("texel buffers not implemented");
463 break;
464
465 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
466 anv_finishme("input attachments not implemented");
467 break;
468
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);
476 assert(buffer);
477
478 desc[j] = (struct anv_descriptor) {
479 .type = write->descriptorType,
480 .buffer = buffer,
481 .offset = write->pBufferInfo[j].offset,
482 .range = write->pBufferInfo[j].range,
483 };
484
485 /* For buffers with dynamic offsets, we use the full possible
486 * range in the surface state and do the actual range-checking
487 * in the shader.
488 */
489 if (bind_layout->dynamic_offset_index >= 0)
490 desc[j].range = buffer->size - desc[j].offset;
491 }
492
493 default:
494 break;
495 }
496 }
497
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];
505 }
506 }
507 }