vk/0.210.0: Rework a few raster/input enums
[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 immutable_sampler_count = 0;
48 for (uint32_t b = 0; b < pCreateInfo->count; b++) {
49 if (pCreateInfo->pBinding[b].pImmutableSamplers)
50 immutable_sampler_count += pCreateInfo->pBinding[b].arraySize;
51 }
52
53 size_t size = sizeof(struct anv_descriptor_set_layout) +
54 pCreateInfo->count * sizeof(set_layout->binding[0]) +
55 immutable_sampler_count * sizeof(struct anv_sampler *);
56
57 set_layout = anv_device_alloc(device, size, 8,
58 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
59 if (!set_layout)
60 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
61
62 /* We just allocate all the samplers at the end of the struct */
63 struct anv_sampler **samplers =
64 (struct anv_sampler **)&set_layout->binding[pCreateInfo->count];
65
66 set_layout->binding_count = pCreateInfo->count;
67 set_layout->shader_stages = 0;
68 set_layout->size = 0;
69
70 /* Initialize all binding_layout entries to -1 */
71 memset(set_layout->binding, -1,
72 pCreateInfo->count * sizeof(set_layout->binding[0]));
73
74 /* Initialize all samplers to 0 */
75 memset(samplers, 0, immutable_sampler_count * sizeof(*samplers));
76
77 uint32_t sampler_count[VK_SHADER_STAGE_NUM] = { 0, };
78 uint32_t surface_count[VK_SHADER_STAGE_NUM] = { 0, };
79 uint32_t dynamic_offset_count = 0;
80
81 for (uint32_t b = 0; b < pCreateInfo->count; b++) {
82 uint32_t array_size = MAX2(1, pCreateInfo->pBinding[b].arraySize);
83 set_layout->binding[b].array_size = array_size;
84 set_layout->binding[b].descriptor_index = set_layout->size;
85 set_layout->size += array_size;
86
87 switch (pCreateInfo->pBinding[b].descriptorType) {
88 case VK_DESCRIPTOR_TYPE_SAMPLER:
89 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
90 for_each_bit(s, pCreateInfo->pBinding[b].stageFlags) {
91 set_layout->binding[b].stage[s].sampler_index = sampler_count[s];
92 sampler_count[s] += array_size;
93 }
94 break;
95 default:
96 break;
97 }
98
99 switch (pCreateInfo->pBinding[b].descriptorType) {
100 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
101 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
102 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
103 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
104 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
105 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
106 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
107 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
108 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
109 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
110 for_each_bit(s, pCreateInfo->pBinding[b].stageFlags) {
111 set_layout->binding[b].stage[s].surface_index = surface_count[s];
112 surface_count[s] += array_size;
113 }
114 break;
115 default:
116 break;
117 }
118
119 switch (pCreateInfo->pBinding[b].descriptorType) {
120 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
121 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
122 set_layout->binding[b].dynamic_offset_index = dynamic_offset_count;
123 dynamic_offset_count += array_size;
124 break;
125 default:
126 break;
127 }
128
129 if (pCreateInfo->pBinding[b].pImmutableSamplers) {
130 set_layout->binding[b].immutable_samplers = samplers;
131 samplers += array_size;
132
133 for (uint32_t i = 0; i < array_size; i++)
134 set_layout->binding[b].immutable_samplers[i] =
135 anv_sampler_from_handle(pCreateInfo->pBinding[b].pImmutableSamplers[i]);
136 } else {
137 set_layout->binding[b].immutable_samplers = NULL;
138 }
139
140 set_layout->shader_stages |= pCreateInfo->pBinding[b].stageFlags;
141 }
142
143 set_layout->dynamic_offset_count = dynamic_offset_count;
144
145 *pSetLayout = anv_descriptor_set_layout_to_handle(set_layout);
146
147 return VK_SUCCESS;
148 }
149
150 void anv_DestroyDescriptorSetLayout(
151 VkDevice _device,
152 VkDescriptorSetLayout _set_layout)
153 {
154 ANV_FROM_HANDLE(anv_device, device, _device);
155 ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout, _set_layout);
156
157 anv_device_free(device, set_layout);
158 }
159
160 /*
161 * Pipeline layouts. These have nothing to do with the pipeline. They are
162 * just muttiple descriptor set layouts pasted together
163 */
164
165 VkResult anv_CreatePipelineLayout(
166 VkDevice _device,
167 const VkPipelineLayoutCreateInfo* pCreateInfo,
168 VkPipelineLayout* pPipelineLayout)
169 {
170 ANV_FROM_HANDLE(anv_device, device, _device);
171 struct anv_pipeline_layout l, *layout;
172
173 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
174
175 l.num_sets = pCreateInfo->descriptorSetCount;
176
177 unsigned dynamic_offset_count = 0;
178
179 memset(l.stage, 0, sizeof(l.stage));
180 for (uint32_t set = 0; set < pCreateInfo->descriptorSetCount; set++) {
181 ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout,
182 pCreateInfo->pSetLayouts[set]);
183 l.set[set].layout = set_layout;
184
185 l.set[set].dynamic_offset_start = dynamic_offset_count;
186 for (uint32_t b = 0; b < set_layout->binding_count; b++) {
187 if (set_layout->binding[b].dynamic_offset_index >= 0)
188 dynamic_offset_count += set_layout->binding[b].array_size;
189 }
190
191 for (VkShaderStage s = 0; s < VK_SHADER_STAGE_NUM; s++) {
192 l.set[set].stage[s].surface_start = l.stage[s].surface_count;
193 l.set[set].stage[s].sampler_start = l.stage[s].sampler_count;
194
195 for (uint32_t b = 0; b < set_layout->binding_count; b++) {
196 unsigned array_size = set_layout->binding[b].array_size;
197
198 if (set_layout->binding[b].stage[s].surface_index >= 0) {
199 l.stage[s].surface_count += array_size;
200
201 if (set_layout->binding[b].dynamic_offset_index >= 0)
202 l.stage[s].has_dynamic_offsets = true;
203 }
204
205 if (set_layout->binding[b].stage[s].sampler_index >= 0)
206 l.stage[s].sampler_count += array_size;
207 }
208 }
209 }
210
211 unsigned num_bindings = 0;
212 for (VkShaderStage s = 0; s < VK_SHADER_STAGE_NUM; s++)
213 num_bindings += l.stage[s].surface_count + l.stage[s].sampler_count;
214
215 size_t size = sizeof(*layout) + num_bindings * sizeof(layout->entries[0]);
216
217 layout = anv_device_alloc(device, size, 8, VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
218 if (layout == NULL)
219 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
220
221 /* Now we can actually build our surface and sampler maps */
222 struct anv_pipeline_binding *entry = layout->entries;
223 for (VkShaderStage s = 0; s < VK_SHADER_STAGE_NUM; s++) {
224 l.stage[s].surface_to_descriptor = entry;
225 entry += l.stage[s].surface_count;
226 l.stage[s].sampler_to_descriptor = entry;
227 entry += l.stage[s].sampler_count;
228
229 int surface = 0;
230 int sampler = 0;
231 for (uint32_t set = 0; set < pCreateInfo->descriptorSetCount; set++) {
232 struct anv_descriptor_set_layout *set_layout = l.set[set].layout;
233
234 for (uint32_t b = 0; b < set_layout->binding_count; b++) {
235 unsigned array_size = set_layout->binding[b].array_size;
236 unsigned set_offset = set_layout->binding[b].descriptor_index;
237
238 if (set_layout->binding[b].stage[s].surface_index >= 0) {
239 assert(surface == l.set[set].stage[s].surface_start +
240 set_layout->binding[b].stage[s].surface_index);
241 for (unsigned i = 0; i < array_size; i++) {
242 l.stage[s].surface_to_descriptor[surface + i].set = set;
243 l.stage[s].surface_to_descriptor[surface + i].offset = set_offset + i;
244 }
245 surface += array_size;
246 }
247
248 if (set_layout->binding[b].stage[s].sampler_index >= 0) {
249 assert(sampler == l.set[set].stage[s].sampler_start +
250 set_layout->binding[b].stage[s].sampler_index);
251 for (unsigned i = 0; i < array_size; i++) {
252 l.stage[s].sampler_to_descriptor[sampler + i].set = set;
253 l.stage[s].sampler_to_descriptor[sampler + i].offset = set_offset + i;
254 }
255 sampler += array_size;
256 }
257 }
258 }
259 }
260
261 /* Finally, we're done setting it up, copy into the allocated version */
262 *layout = l;
263
264 *pPipelineLayout = anv_pipeline_layout_to_handle(layout);
265
266 return VK_SUCCESS;
267 }
268
269 void anv_DestroyPipelineLayout(
270 VkDevice _device,
271 VkPipelineLayout _pipelineLayout)
272 {
273 ANV_FROM_HANDLE(anv_device, device, _device);
274 ANV_FROM_HANDLE(anv_pipeline_layout, pipeline_layout, _pipelineLayout);
275
276 anv_device_free(device, pipeline_layout);
277 }
278
279 /*
280 * Descriptor pools. These are a no-op for now.
281 */
282
283 VkResult anv_CreateDescriptorPool(
284 VkDevice device,
285 const VkDescriptorPoolCreateInfo* pCreateInfo,
286 VkDescriptorPool* pDescriptorPool)
287 {
288 anv_finishme("VkDescriptorPool is a stub");
289 *pDescriptorPool = (VkDescriptorPool)1;
290 return VK_SUCCESS;
291 }
292
293 void anv_DestroyDescriptorPool(
294 VkDevice _device,
295 VkDescriptorPool _pool)
296 {
297 anv_finishme("VkDescriptorPool is a stub: free the pool's descriptor sets");
298 }
299
300 VkResult anv_ResetDescriptorPool(
301 VkDevice device,
302 VkDescriptorPool descriptorPool)
303 {
304 anv_finishme("VkDescriptorPool is a stub: free the pool's descriptor sets");
305 return VK_SUCCESS;
306 }
307
308 VkResult
309 anv_descriptor_set_create(struct anv_device *device,
310 const struct anv_descriptor_set_layout *layout,
311 struct anv_descriptor_set **out_set)
312 {
313 struct anv_descriptor_set *set;
314 size_t size = sizeof(*set) + layout->size * sizeof(set->descriptors[0]);
315
316 set = anv_device_alloc(device, size, 8, VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
317 if (!set)
318 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
319
320 /* A descriptor set may not be 100% filled. Clear the set so we can can
321 * later detect holes in it.
322 */
323 memset(set, 0, size);
324
325 set->layout = layout;
326
327 /* Go through and fill out immutable samplers if we have any */
328 struct anv_descriptor *desc = set->descriptors;
329 for (uint32_t b = 0; b < layout->binding_count; b++) {
330 if (layout->binding[b].immutable_samplers) {
331 for (uint32_t i = 0; i < layout->binding[b].array_size; i++)
332 desc[i].sampler = layout->binding[b].immutable_samplers[i];
333 }
334 desc += layout->binding[b].array_size;
335 }
336
337 *out_set = set;
338
339 return VK_SUCCESS;
340 }
341
342 void
343 anv_descriptor_set_destroy(struct anv_device *device,
344 struct anv_descriptor_set *set)
345 {
346 anv_device_free(device, set);
347 }
348
349 VkResult anv_AllocDescriptorSets(
350 VkDevice _device,
351 VkDescriptorPool descriptorPool,
352 VkDescriptorSetUsage setUsage,
353 uint32_t count,
354 const VkDescriptorSetLayout* pSetLayouts,
355 VkDescriptorSet* pDescriptorSets)
356 {
357 ANV_FROM_HANDLE(anv_device, device, _device);
358
359 VkResult result = VK_SUCCESS;
360 struct anv_descriptor_set *set;
361 uint32_t i;
362
363 for (i = 0; i < count; i++) {
364 ANV_FROM_HANDLE(anv_descriptor_set_layout, layout, pSetLayouts[i]);
365
366 result = anv_descriptor_set_create(device, layout, &set);
367 if (result != VK_SUCCESS)
368 break;
369
370 pDescriptorSets[i] = anv_descriptor_set_to_handle(set);
371 }
372
373 if (result != VK_SUCCESS)
374 anv_FreeDescriptorSets(_device, descriptorPool, i, pDescriptorSets);
375
376 return result;
377 }
378
379 VkResult anv_FreeDescriptorSets(
380 VkDevice _device,
381 VkDescriptorPool descriptorPool,
382 uint32_t count,
383 const VkDescriptorSet* pDescriptorSets)
384 {
385 ANV_FROM_HANDLE(anv_device, device, _device);
386
387 for (uint32_t i = 0; i < count; i++) {
388 ANV_FROM_HANDLE(anv_descriptor_set, set, pDescriptorSets[i]);
389
390 anv_descriptor_set_destroy(device, set);
391 }
392
393 return VK_SUCCESS;
394 }
395
396 void anv_UpdateDescriptorSets(
397 VkDevice device,
398 uint32_t writeCount,
399 const VkWriteDescriptorSet* pDescriptorWrites,
400 uint32_t copyCount,
401 const VkCopyDescriptorSet* pDescriptorCopies)
402 {
403 for (uint32_t i = 0; i < writeCount; i++) {
404 const VkWriteDescriptorSet *write = &pDescriptorWrites[i];
405 ANV_FROM_HANDLE(anv_descriptor_set, set, write->destSet);
406 const struct anv_descriptor_set_binding_layout *bind_layout =
407 &set->layout->binding[write->destBinding];
408 struct anv_descriptor *desc =
409 &set->descriptors[bind_layout->descriptor_index];
410
411 switch (write->descriptorType) {
412 case VK_DESCRIPTOR_TYPE_SAMPLER:
413 for (uint32_t j = 0; j < write->count; j++) {
414 ANV_FROM_HANDLE(anv_sampler, sampler,
415 write->pDescriptors[j].sampler);
416
417 desc[j] = (struct anv_descriptor) {
418 .type = VK_DESCRIPTOR_TYPE_SAMPLER,
419 .sampler = sampler,
420 };
421 }
422 break;
423
424 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
425 for (uint32_t j = 0; j < write->count; j++) {
426 ANV_FROM_HANDLE(anv_image_view, iview,
427 write->pDescriptors[j].imageView);
428 ANV_FROM_HANDLE(anv_sampler, sampler,
429 write->pDescriptors[j].sampler);
430
431 desc[j].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
432 desc[j].image_view = iview;
433
434 /* If this descriptor has an immutable sampler, we don't want
435 * to stomp on it.
436 */
437 if (sampler)
438 desc[j].sampler = sampler;
439 }
440 break;
441
442 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
443 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
444 for (uint32_t j = 0; j < write->count; j++) {
445 ANV_FROM_HANDLE(anv_image_view, iview,
446 write->pDescriptors[j].imageView);
447
448 desc[j] = (struct anv_descriptor) {
449 .type = write->descriptorType,
450 .image_view = iview,
451 };
452 }
453 break;
454
455 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
456 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
457 anv_finishme("texel buffers not implemented");
458 break;
459
460 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
461 anv_finishme("input attachments not implemented");
462 break;
463
464 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
465 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
466 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
467 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
468 for (uint32_t j = 0; j < write->count; j++) {
469 assert(write->pDescriptors[j].bufferInfo.buffer);
470 ANV_FROM_HANDLE(anv_buffer, buffer,
471 write->pDescriptors[j].bufferInfo.buffer);
472 assert(buffer);
473
474 desc[j] = (struct anv_descriptor) {
475 .type = write->descriptorType,
476 .buffer = buffer,
477 .offset = write->pDescriptors[j].bufferInfo.offset,
478 .range = write->pDescriptors[j].bufferInfo.range,
479 };
480
481 /* For buffers with dynamic offsets, we use the full possible
482 * range in the surface state and do the actual range-checking
483 * in the shader.
484 */
485 if (bind_layout->dynamic_offset_index >= 0)
486 desc[j].range = buffer->size - desc[j].offset;
487 }
488
489 default:
490 break;
491 }
492 }
493
494 for (uint32_t i = 0; i < copyCount; i++) {
495 const VkCopyDescriptorSet *copy = &pDescriptorCopies[i];
496 ANV_FROM_HANDLE(anv_descriptor_set, src, copy->destSet);
497 ANV_FROM_HANDLE(anv_descriptor_set, dest, copy->destSet);
498 for (uint32_t j = 0; j < copy->count; j++) {
499 dest->descriptors[copy->destBinding + j] =
500 src->descriptors[copy->srcBinding + j];
501 }
502 }
503 }