anv: Gut anv_pipeline_layout
[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 const VkAllocationCallbacks* pAllocator,
40 VkDescriptorSetLayout* pSetLayout)
41 {
42 ANV_FROM_HANDLE(anv_device, device, _device);
43 struct anv_descriptor_set_layout *set_layout;
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->pBindings[j].binding);
51 if (pCreateInfo->pBindings[j].pImmutableSamplers)
52 immutable_sampler_count += pCreateInfo->pBindings[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_alloc2(&device->alloc, pAllocator, size, 8,
60 VK_SYSTEM_ALLOCATION_SCOPE_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 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]));
75
76 set_layout->binding[b].immutable_samplers = NULL;
77 }
78
79 /* Initialize all samplers to 0 */
80 memset(samplers, 0, immutable_sampler_count * sizeof(*samplers));
81
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;
87
88 for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
89 const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBindings[j];
90 uint32_t b = binding->binding;
91
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;
96
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;
103 }
104 break;
105 default:
106 break;
107 }
108
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;
116 /* fall through */
117
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;
127 }
128 break;
129 default:
130 break;
131 }
132
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;
138 break;
139 default:
140 break;
141 }
142
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;
149 }
150 break;
151 default:
152 break;
153 }
154
155 if (binding->pImmutableSamplers) {
156 set_layout->binding[b].immutable_samplers = samplers;
157 samplers += binding->descriptorCount;
158
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]);
162 } else {
163 set_layout->binding[b].immutable_samplers = NULL;
164 }
165
166 set_layout->shader_stages |= binding->stageFlags;
167 }
168
169 set_layout->buffer_count = buffer_count;
170 set_layout->dynamic_offset_count = dynamic_offset_count;
171
172 *pSetLayout = anv_descriptor_set_layout_to_handle(set_layout);
173
174 return VK_SUCCESS;
175 }
176
177 void anv_DestroyDescriptorSetLayout(
178 VkDevice _device,
179 VkDescriptorSetLayout _set_layout,
180 const VkAllocationCallbacks* pAllocator)
181 {
182 ANV_FROM_HANDLE(anv_device, device, _device);
183 ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout, _set_layout);
184
185 anv_free2(&device->alloc, pAllocator, set_layout);
186 }
187
188 /*
189 * Pipeline layouts. These have nothing to do with the pipeline. They are
190 * just muttiple descriptor set layouts pasted together
191 */
192
193 VkResult anv_CreatePipelineLayout(
194 VkDevice _device,
195 const VkPipelineLayoutCreateInfo* pCreateInfo,
196 const VkAllocationCallbacks* pAllocator,
197 VkPipelineLayout* pPipelineLayout)
198 {
199 ANV_FROM_HANDLE(anv_device, device, _device);
200 struct anv_pipeline_layout *layout;
201
202 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
203
204 layout = anv_alloc2(&device->alloc, pAllocator, sizeof(*layout), 8,
205 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
206 if (layout == NULL)
207 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
208
209 layout->num_sets = pCreateInfo->setLayoutCount;
210
211 unsigned dynamic_offset_count = 0;
212
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;
218
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;
226 }
227 }
228 }
229
230 *pPipelineLayout = anv_pipeline_layout_to_handle(layout);
231
232 return VK_SUCCESS;
233 }
234
235 void anv_DestroyPipelineLayout(
236 VkDevice _device,
237 VkPipelineLayout _pipelineLayout,
238 const VkAllocationCallbacks* pAllocator)
239 {
240 ANV_FROM_HANDLE(anv_device, device, _device);
241 ANV_FROM_HANDLE(anv_pipeline_layout, pipeline_layout, _pipelineLayout);
242
243 anv_free2(&device->alloc, pAllocator, pipeline_layout);
244 }
245
246 /*
247 * Descriptor pools. These are a no-op for now.
248 */
249
250 VkResult anv_CreateDescriptorPool(
251 VkDevice device,
252 const VkDescriptorPoolCreateInfo* pCreateInfo,
253 const VkAllocationCallbacks* pAllocator,
254 VkDescriptorPool* pDescriptorPool)
255 {
256 anv_finishme("VkDescriptorPool is a stub");
257 *pDescriptorPool = (VkDescriptorPool)1;
258 return VK_SUCCESS;
259 }
260
261 void anv_DestroyDescriptorPool(
262 VkDevice _device,
263 VkDescriptorPool _pool,
264 const VkAllocationCallbacks* pAllocator)
265 {
266 anv_finishme("VkDescriptorPool is a stub: free the pool's descriptor sets");
267 }
268
269 VkResult anv_ResetDescriptorPool(
270 VkDevice device,
271 VkDescriptorPool descriptorPool,
272 VkDescriptorPoolResetFlags flags)
273 {
274 anv_finishme("VkDescriptorPool is a stub: free the pool's descriptor sets");
275 return VK_SUCCESS;
276 }
277
278 VkResult
279 anv_descriptor_set_create(struct anv_device *device,
280 const struct anv_descriptor_set_layout *layout,
281 struct anv_descriptor_set **out_set)
282 {
283 struct anv_descriptor_set *set;
284 size_t size = sizeof(*set) + layout->size * sizeof(set->descriptors[0]);
285
286 set = anv_alloc(&device->alloc /* XXX: Use the pool */, size, 8,
287 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
288 if (!set)
289 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
290
291 /* A descriptor set may not be 100% filled. Clear the set so we can can
292 * later detect holes in it.
293 */
294 memset(set, 0, size);
295
296 set->layout = layout;
297
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];
304 }
305 desc += layout->binding[b].array_size;
306 }
307
308 /* XXX: Use the pool */
309 set->buffer_views =
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);
316 }
317
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);
321 }
322 set->buffer_count = layout->buffer_count;
323 *out_set = set;
324
325 return VK_SUCCESS;
326 }
327
328 void
329 anv_descriptor_set_destroy(struct anv_device *device,
330 struct anv_descriptor_set *set)
331 {
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);
336
337 anv_free(&device->alloc, set->buffer_views);
338 anv_free(&device->alloc, set);
339 }
340
341 VkResult anv_AllocateDescriptorSets(
342 VkDevice _device,
343 const VkDescriptorSetAllocateInfo* pAllocateInfo,
344 VkDescriptorSet* pDescriptorSets)
345 {
346 ANV_FROM_HANDLE(anv_device, device, _device);
347
348 VkResult result = VK_SUCCESS;
349 struct anv_descriptor_set *set;
350 uint32_t i;
351
352 for (i = 0; i < pAllocateInfo->descriptorSetCount; i++) {
353 ANV_FROM_HANDLE(anv_descriptor_set_layout, layout,
354 pAllocateInfo->pSetLayouts[i]);
355
356 result = anv_descriptor_set_create(device, layout, &set);
357 if (result != VK_SUCCESS)
358 break;
359
360 pDescriptorSets[i] = anv_descriptor_set_to_handle(set);
361 }
362
363 if (result != VK_SUCCESS)
364 anv_FreeDescriptorSets(_device, pAllocateInfo->descriptorPool,
365 i, pDescriptorSets);
366
367 return result;
368 }
369
370 VkResult anv_FreeDescriptorSets(
371 VkDevice _device,
372 VkDescriptorPool descriptorPool,
373 uint32_t count,
374 const VkDescriptorSet* pDescriptorSets)
375 {
376 ANV_FROM_HANDLE(anv_device, device, _device);
377
378 for (uint32_t i = 0; i < count; i++) {
379 ANV_FROM_HANDLE(anv_descriptor_set, set, pDescriptorSets[i]);
380
381 anv_descriptor_set_destroy(device, set);
382 }
383
384 return VK_SUCCESS;
385 }
386
387 void anv_UpdateDescriptorSets(
388 VkDevice _device,
389 uint32_t descriptorWriteCount,
390 const VkWriteDescriptorSet* pDescriptorWrites,
391 uint32_t descriptorCopyCount,
392 const VkCopyDescriptorSet* pDescriptorCopies)
393 {
394 ANV_FROM_HANDLE(anv_device, device, _device);
395
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;
404
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);
410
411 desc[j] = (struct anv_descriptor) {
412 .type = VK_DESCRIPTOR_TYPE_SAMPLER,
413 .sampler = sampler,
414 };
415 }
416 break;
417
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);
424
425 desc[j].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
426 desc[j].image_view = iview;
427
428 /* If this descriptor has an immutable sampler, we don't want
429 * to stomp on it.
430 */
431 if (sampler)
432 desc[j].sampler = sampler;
433 }
434 break;
435
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);
441
442 desc[j] = (struct anv_descriptor) {
443 .type = write->descriptorType,
444 .image_view = iview,
445 };
446 }
447 break;
448
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]);
454
455 desc[j] = (struct anv_descriptor) {
456 .type = write->descriptorType,
457 .buffer_view = bview,
458 };
459 }
460 break;
461
462 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
463 anv_finishme("input attachments not implemented");
464 break;
465
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);
473 assert(buffer);
474
475 struct anv_buffer_view *view =
476 &set->buffer_views[bind_layout->buffer_index];
477 view += write->dstArrayElement + j;
478
479 const struct anv_format *format =
480 anv_format_for_descriptor_type(write->descriptorType);
481
482 view->format = format->isl_format;
483 view->bo = buffer->bo;
484 view->offset = buffer->offset + write->pBufferInfo[j].offset;
485
486 /* For buffers with dynamic offsets, we use the full possible
487 * range in the surface state and do the actual range-checking
488 * in the shader.
489 */
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;
493 else
494 view->range = write->pBufferInfo[j].range;
495
496 anv_fill_buffer_surface_state(device, view->surface_state,
497 view->format,
498 view->offset, view->range, 1);
499
500 desc[j] = (struct anv_descriptor) {
501 .type = write->descriptorType,
502 .buffer_view = view,
503 };
504
505 }
506
507 default:
508 break;
509 }
510 }
511
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);
516
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;
522
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;
528
529 for (uint32_t j = 0; j < copy->descriptorCount; j++)
530 dst_desc[j] = src_desc[j];
531 }
532 }