anv/image: Refactor anv_image_make_surface()
[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 uint32_t s;
45
46 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO);
47
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;
54 }
55
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 *);
59
60 set_layout = anv_alloc2(&device->alloc, pAllocator, size, 8,
61 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
62 if (!set_layout)
63 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
64
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];
68
69 set_layout->binding_count = max_binding + 1;
70 set_layout->shader_stages = 0;
71 set_layout->size = 0;
72
73 /* Initialize all binding_layout entries to -1 */
74 memset(set_layout->binding, -1,
75 (max_binding + 1) * sizeof(set_layout->binding[0]));
76
77 /* Initialize all samplers to 0 */
78 memset(samplers, 0, immutable_sampler_count * sizeof(*samplers));
79
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;
83
84 for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
85 const VkDescriptorSetLayoutBinding *binding = &pCreateInfo->pBinding[j];
86 uint32_t b = binding->binding;
87
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;
92
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;
99 }
100 break;
101 default:
102 break;
103 }
104
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;
119 }
120 break;
121 default:
122 break;
123 }
124
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;
130 break;
131 default:
132 break;
133 }
134
135 if (binding->pImmutableSamplers) {
136 set_layout->binding[b].immutable_samplers = samplers;
137 samplers += binding->descriptorCount;
138
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]);
142 } else {
143 set_layout->binding[b].immutable_samplers = NULL;
144 }
145
146 set_layout->shader_stages |= binding->stageFlags;
147 }
148
149 set_layout->dynamic_offset_count = dynamic_offset_count;
150
151 *pSetLayout = anv_descriptor_set_layout_to_handle(set_layout);
152
153 return VK_SUCCESS;
154 }
155
156 void anv_DestroyDescriptorSetLayout(
157 VkDevice _device,
158 VkDescriptorSetLayout _set_layout,
159 const VkAllocationCallbacks* pAllocator)
160 {
161 ANV_FROM_HANDLE(anv_device, device, _device);
162 ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout, _set_layout);
163
164 anv_free2(&device->alloc, pAllocator, set_layout);
165 }
166
167 /*
168 * Pipeline layouts. These have nothing to do with the pipeline. They are
169 * just muttiple descriptor set layouts pasted together
170 */
171
172 VkResult anv_CreatePipelineLayout(
173 VkDevice _device,
174 const VkPipelineLayoutCreateInfo* pCreateInfo,
175 const VkAllocationCallbacks* pAllocator,
176 VkPipelineLayout* pPipelineLayout)
177 {
178 ANV_FROM_HANDLE(anv_device, device, _device);
179 struct anv_pipeline_layout l, *layout;
180
181 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
182
183 l.num_sets = pCreateInfo->setLayoutCount;
184
185 unsigned dynamic_offset_count = 0;
186
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;
192
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;
197 }
198
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;
202
203 for (uint32_t b = 0; b < set_layout->binding_count; b++) {
204 unsigned array_size = set_layout->binding[b].array_size;
205
206 if (set_layout->binding[b].stage[s].surface_index >= 0) {
207 l.stage[s].surface_count += array_size;
208
209 if (set_layout->binding[b].dynamic_offset_index >= 0)
210 l.stage[s].has_dynamic_offsets = true;
211 }
212
213 if (set_layout->binding[b].stage[s].sampler_index >= 0)
214 l.stage[s].sampler_count += array_size;
215 }
216 }
217 }
218
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;
222
223 size_t size = sizeof(*layout) + num_bindings * sizeof(layout->entries[0]);
224
225 layout = anv_alloc2(&device->alloc, pAllocator, size, 8,
226 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
227 if (layout == NULL)
228 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
229
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;
237
238 int surface = 0;
239 int sampler = 0;
240 for (uint32_t set = 0; set < pCreateInfo->setLayoutCount; set++) {
241 struct anv_descriptor_set_layout *set_layout = l.set[set].layout;
242
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;
246
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;
253 }
254 surface += array_size;
255 }
256
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;
263 }
264 sampler += array_size;
265 }
266 }
267 }
268 }
269
270 /* Finally, we're done setting it up, copy into the allocated version */
271 *layout = l;
272
273 *pPipelineLayout = anv_pipeline_layout_to_handle(layout);
274
275 return VK_SUCCESS;
276 }
277
278 void anv_DestroyPipelineLayout(
279 VkDevice _device,
280 VkPipelineLayout _pipelineLayout,
281 const VkAllocationCallbacks* pAllocator)
282 {
283 ANV_FROM_HANDLE(anv_device, device, _device);
284 ANV_FROM_HANDLE(anv_pipeline_layout, pipeline_layout, _pipelineLayout);
285
286 anv_free2(&device->alloc, pAllocator, pipeline_layout);
287 }
288
289 /*
290 * Descriptor pools. These are a no-op for now.
291 */
292
293 VkResult anv_CreateDescriptorPool(
294 VkDevice device,
295 const VkDescriptorPoolCreateInfo* pCreateInfo,
296 const VkAllocationCallbacks* pAllocator,
297 VkDescriptorPool* pDescriptorPool)
298 {
299 anv_finishme("VkDescriptorPool is a stub");
300 *pDescriptorPool = (VkDescriptorPool)1;
301 return VK_SUCCESS;
302 }
303
304 void anv_DestroyDescriptorPool(
305 VkDevice _device,
306 VkDescriptorPool _pool,
307 const VkAllocationCallbacks* pAllocator)
308 {
309 anv_finishme("VkDescriptorPool is a stub: free the pool's descriptor sets");
310 }
311
312 VkResult anv_ResetDescriptorPool(
313 VkDevice device,
314 VkDescriptorPool descriptorPool,
315 VkDescriptorPoolResetFlags flags)
316 {
317 anv_finishme("VkDescriptorPool is a stub: free the pool's descriptor sets");
318 return VK_SUCCESS;
319 }
320
321 VkResult
322 anv_descriptor_set_create(struct anv_device *device,
323 const struct anv_descriptor_set_layout *layout,
324 struct anv_descriptor_set **out_set)
325 {
326 struct anv_descriptor_set *set;
327 size_t size = sizeof(*set) + layout->size * sizeof(set->descriptors[0]);
328
329 set = anv_alloc(&device->alloc /* XXX: Use the pool */, size, 8,
330 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
331 if (!set)
332 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
333
334 /* A descriptor set may not be 100% filled. Clear the set so we can can
335 * later detect holes in it.
336 */
337 memset(set, 0, size);
338
339 set->layout = layout;
340
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];
347 }
348 desc += layout->binding[b].array_size;
349 }
350
351 *out_set = set;
352
353 return VK_SUCCESS;
354 }
355
356 void
357 anv_descriptor_set_destroy(struct anv_device *device,
358 struct anv_descriptor_set *set)
359 {
360 anv_free(&device->alloc /* XXX: Use the pool */, set);
361 }
362
363 VkResult anv_AllocateDescriptorSets(
364 VkDevice _device,
365 const VkDescriptorSetAllocateInfo* pAllocateInfo,
366 VkDescriptorSet* pDescriptorSets)
367 {
368 ANV_FROM_HANDLE(anv_device, device, _device);
369
370 VkResult result = VK_SUCCESS;
371 struct anv_descriptor_set *set;
372 uint32_t i;
373
374 for (i = 0; i < pAllocateInfo->setLayoutCount; i++) {
375 ANV_FROM_HANDLE(anv_descriptor_set_layout, layout,
376 pAllocateInfo->pSetLayouts[i]);
377
378 result = anv_descriptor_set_create(device, layout, &set);
379 if (result != VK_SUCCESS)
380 break;
381
382 pDescriptorSets[i] = anv_descriptor_set_to_handle(set);
383 }
384
385 if (result != VK_SUCCESS)
386 anv_FreeDescriptorSets(_device, pAllocateInfo->descriptorPool,
387 i, pDescriptorSets);
388
389 return result;
390 }
391
392 VkResult anv_FreeDescriptorSets(
393 VkDevice _device,
394 VkDescriptorPool descriptorPool,
395 uint32_t count,
396 const VkDescriptorSet* pDescriptorSets)
397 {
398 ANV_FROM_HANDLE(anv_device, device, _device);
399
400 for (uint32_t i = 0; i < count; i++) {
401 ANV_FROM_HANDLE(anv_descriptor_set, set, pDescriptorSets[i]);
402
403 anv_descriptor_set_destroy(device, set);
404 }
405
406 return VK_SUCCESS;
407 }
408
409 void anv_UpdateDescriptorSets(
410 VkDevice device,
411 uint32_t descriptorWriteCount,
412 const VkWriteDescriptorSet* pDescriptorWrites,
413 uint32_t descriptorCopyCount,
414 const VkCopyDescriptorSet* pDescriptorCopies)
415 {
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];
423
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);
429
430 desc[j] = (struct anv_descriptor) {
431 .type = VK_DESCRIPTOR_TYPE_SAMPLER,
432 .sampler = sampler,
433 };
434 }
435 break;
436
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);
443
444 desc[j].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
445 desc[j].image_view = iview;
446
447 /* If this descriptor has an immutable sampler, we don't want
448 * to stomp on it.
449 */
450 if (sampler)
451 desc[j].sampler = sampler;
452 }
453 break;
454
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);
460
461 desc[j] = (struct anv_descriptor) {
462 .type = write->descriptorType,
463 .image_view = iview,
464 };
465 }
466 break;
467
468 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
469 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
470 anv_finishme("texel buffers not implemented");
471 break;
472
473 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
474 anv_finishme("input attachments not implemented");
475 break;
476
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);
484 assert(buffer);
485
486 desc[j] = (struct anv_descriptor) {
487 .type = write->descriptorType,
488 .buffer = buffer,
489 .offset = write->pBufferInfo[j].offset,
490 .range = write->pBufferInfo[j].range,
491 };
492
493 /* For buffers with dynamic offsets, we use the full possible
494 * range in the surface state and do the actual range-checking
495 * in the shader.
496 */
497 if (bind_layout->dynamic_offset_index >= 0)
498 desc[j].range = buffer->size - desc[j].offset;
499 }
500
501 default:
502 break;
503 }
504 }
505
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];
513 }
514 }
515 }