-// Descriptor set functions
-
-VkResult anv_CreateDescriptorSetLayout(
- VkDevice _device,
- const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
- VkDescriptorSetLayout* pSetLayout)
-{
- ANV_FROM_HANDLE(anv_device, device, _device);
- struct anv_descriptor_set_layout *set_layout;
- uint32_t s;
-
- assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO);
-
- uint32_t immutable_sampler_count = 0;
- for (uint32_t b = 0; b < pCreateInfo->count; b++) {
- if (pCreateInfo->pBinding[b].pImmutableSamplers)
- immutable_sampler_count += pCreateInfo->pBinding[b].arraySize;
- }
-
- size_t size = sizeof(struct anv_descriptor_set_layout) +
- pCreateInfo->count * sizeof(set_layout->binding[0]) +
- immutable_sampler_count * sizeof(struct anv_sampler *);
-
- set_layout = anv_device_alloc(device, size, 8,
- VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
- if (!set_layout)
- return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
-
- /* We just allocate all the samplers at the end of the struct */
- struct anv_sampler **samplers =
- (struct anv_sampler **)&set_layout->binding[pCreateInfo->count];
-
- set_layout->binding_count = pCreateInfo->count;
- set_layout->shader_stages = 0;
- set_layout->size = 0;
-
- /* Initialize all binding_layout entries to -1 */
- memset(set_layout->binding, -1,
- pCreateInfo->count * sizeof(set_layout->binding[0]));
-
- /* Initialize all samplers to 0 */
- memset(samplers, 0, immutable_sampler_count * sizeof(*samplers));
-
- uint32_t sampler_count[VK_SHADER_STAGE_NUM] = { 0, };
- uint32_t surface_count[VK_SHADER_STAGE_NUM] = { 0, };
- uint32_t dynamic_offset_count = 0;
-
- for (uint32_t b = 0; b < pCreateInfo->count; b++) {
- uint32_t array_size = MAX2(1, pCreateInfo->pBinding[b].arraySize);
- set_layout->binding[b].array_size = array_size;
- set_layout->binding[b].descriptor_index = set_layout->size;
- set_layout->size += array_size;
-
- switch (pCreateInfo->pBinding[b].descriptorType) {
- case VK_DESCRIPTOR_TYPE_SAMPLER:
- case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
- for_each_bit(s, pCreateInfo->pBinding[b].stageFlags) {
- set_layout->binding[b].stage[s].sampler_index = sampler_count[s];
- sampler_count[s] += array_size;
- }
- break;
- default:
- break;
- }
-
- switch (pCreateInfo->pBinding[b].descriptorType) {
- case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
- case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
- case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
- case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
- case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
- case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
- case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
- case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
- case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
- case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
- for_each_bit(s, pCreateInfo->pBinding[b].stageFlags) {
- set_layout->binding[b].stage[s].surface_index = surface_count[s];
- surface_count[s] += array_size;
- }
- break;
- default:
- break;
- }
-
- switch (pCreateInfo->pBinding[b].descriptorType) {
- case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
- case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
- set_layout->binding[b].dynamic_offset_index = dynamic_offset_count;
- dynamic_offset_count += array_size;
- break;
- default:
- break;
- }
-
- if (pCreateInfo->pBinding[b].pImmutableSamplers) {
- set_layout->binding[b].immutable_samplers = samplers;
- samplers += array_size;
-
- for (uint32_t i = 0; i < array_size; i++)
- set_layout->binding[b].immutable_samplers[i] =
- anv_sampler_from_handle(pCreateInfo->pBinding[b].pImmutableSamplers[i]);
- } else {
- set_layout->binding[b].immutable_samplers = NULL;
- }
-
- set_layout->shader_stages |= pCreateInfo->pBinding[b].stageFlags;
- }
-
- set_layout->dynamic_offset_count = dynamic_offset_count;
-
- *pSetLayout = anv_descriptor_set_layout_to_handle(set_layout);
-
- return VK_SUCCESS;
-}
-
-void anv_DestroyDescriptorSetLayout(
- VkDevice _device,
- VkDescriptorSetLayout _set_layout)
-{
- ANV_FROM_HANDLE(anv_device, device, _device);
- ANV_FROM_HANDLE(anv_descriptor_set_layout, set_layout, _set_layout);
-
- anv_device_free(device, set_layout);
-}
-
-VkResult anv_CreateDescriptorPool(
- VkDevice device,
- const VkDescriptorPoolCreateInfo* pCreateInfo,
- VkDescriptorPool* pDescriptorPool)
-{
- anv_finishme("VkDescriptorPool is a stub");
- pDescriptorPool->handle = 1;
- return VK_SUCCESS;
-}
-
-void anv_DestroyDescriptorPool(
- VkDevice _device,
- VkDescriptorPool _pool)
-{
- anv_finishme("VkDescriptorPool is a stub: free the pool's descriptor sets");
-}
-
-VkResult anv_ResetDescriptorPool(
- VkDevice device,
- VkDescriptorPool descriptorPool)
-{
- anv_finishme("VkDescriptorPool is a stub: free the pool's descriptor sets");
- return VK_SUCCESS;
-}
-
-VkResult
-anv_descriptor_set_create(struct anv_device *device,
- const struct anv_descriptor_set_layout *layout,
- struct anv_descriptor_set **out_set)
-{
- struct anv_descriptor_set *set;
- size_t size = sizeof(*set) + layout->size * sizeof(set->descriptors[0]);
-
- set = anv_device_alloc(device, size, 8, VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
- if (!set)
- return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
-
- /* A descriptor set may not be 100% filled. Clear the set so we can can
- * later detect holes in it.
- */
- memset(set, 0, size);
-
- set->layout = layout;
-
- /* Go through and fill out immutable samplers if we have any */
- struct anv_descriptor *desc = set->descriptors;
- for (uint32_t b = 0; b < layout->binding_count; b++) {
- if (layout->binding[b].immutable_samplers) {
- for (uint32_t i = 0; i < layout->binding[b].array_size; i++)
- desc[i].sampler = layout->binding[b].immutable_samplers[i];
- }
- desc += layout->binding[b].array_size;
- }
-
- *out_set = set;
-
- return VK_SUCCESS;
-}
-
-void
-anv_descriptor_set_destroy(struct anv_device *device,
- struct anv_descriptor_set *set)
-{
- anv_device_free(device, set);
-}
-
-VkResult anv_AllocDescriptorSets(
- VkDevice _device,
- VkDescriptorPool descriptorPool,
- VkDescriptorSetUsage setUsage,
- uint32_t count,
- const VkDescriptorSetLayout* pSetLayouts,
- VkDescriptorSet* pDescriptorSets)
-{
- ANV_FROM_HANDLE(anv_device, device, _device);
-
- VkResult result = VK_SUCCESS;
- struct anv_descriptor_set *set;
- uint32_t i;
-
- for (i = 0; i < count; i++) {
- ANV_FROM_HANDLE(anv_descriptor_set_layout, layout, pSetLayouts[i]);
-
- result = anv_descriptor_set_create(device, layout, &set);
- if (result != VK_SUCCESS)
- break;
-
- pDescriptorSets[i] = anv_descriptor_set_to_handle(set);
- }
-
- if (result != VK_SUCCESS)
- anv_FreeDescriptorSets(_device, descriptorPool, i, pDescriptorSets);
-
- return result;
-}
-
-VkResult anv_FreeDescriptorSets(
- VkDevice _device,
- VkDescriptorPool descriptorPool,
- uint32_t count,
- const VkDescriptorSet* pDescriptorSets)
-{
- ANV_FROM_HANDLE(anv_device, device, _device);
-
- for (uint32_t i = 0; i < count; i++) {
- ANV_FROM_HANDLE(anv_descriptor_set, set, pDescriptorSets[i]);
-
- anv_descriptor_set_destroy(device, set);
- }
-
- return VK_SUCCESS;
-}
-
-void anv_UpdateDescriptorSets(
- VkDevice device,
- uint32_t writeCount,
- const VkWriteDescriptorSet* pDescriptorWrites,
- uint32_t copyCount,
- const VkCopyDescriptorSet* pDescriptorCopies)
-{
- for (uint32_t i = 0; i < writeCount; i++) {
- const VkWriteDescriptorSet *write = &pDescriptorWrites[i];
- ANV_FROM_HANDLE(anv_descriptor_set, set, write->destSet);
- const struct anv_descriptor_set_binding_layout *bind_layout =
- &set->layout->binding[write->destBinding];
- struct anv_descriptor *desc =
- &set->descriptors[bind_layout->descriptor_index];
-
- switch (write->descriptorType) {
- case VK_DESCRIPTOR_TYPE_SAMPLER:
- for (uint32_t j = 0; j < write->count; j++) {
- ANV_FROM_HANDLE(anv_sampler, sampler,
- write->pDescriptors[j].sampler);
-
- desc[j] = (struct anv_descriptor) {
- .type = ANV_DESCRIPTOR_TYPE_SAMPLER,
- .sampler = sampler,
- };
- }
- break;
-
- case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
- for (uint32_t j = 0; j < write->count; j++) {
- ANV_FROM_HANDLE(anv_image_view, iview,
- write->pDescriptors[j].imageView);
- ANV_FROM_HANDLE(anv_sampler, sampler,
- write->pDescriptors[j].sampler);
-
- desc[j].type = ANV_DESCRIPTOR_TYPE_IMAGE_VIEW_AND_SAMPLER;
- desc[j].image_view = iview;
-
- /* If this descriptor has an immutable sampler, we don't want
- * to stomp on it.
- */
- if (sampler)
- desc->sampler = sampler;
- }
- break;
-
- case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
- case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
- for (uint32_t j = 0; j < write->count; j++) {
- ANV_FROM_HANDLE(anv_image_view, iview,
- write->pDescriptors[j].imageView);
-
- desc[j] = (struct anv_descriptor) {
- .type = ANV_DESCRIPTOR_TYPE_IMAGE_VIEW,
- .image_view = iview,
- };
- }
- break;
-
- case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
- case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
- anv_finishme("texel buffers not implemented");
- break;
-
- case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
- anv_finishme("input attachments not implemented");
- break;
-
- case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
- case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
- case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
- case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
- for (uint32_t j = 0; j < write->count; j++) {
- if (write->pDescriptors[j].bufferView.handle) {
- ANV_FROM_HANDLE(anv_buffer_view, bview,
- write->pDescriptors[j].bufferView);
-
- desc[j] = (struct anv_descriptor) {
- .type = ANV_DESCRIPTOR_TYPE_BUFFER_VIEW,
- .buffer_view = bview,
- };
- } else {
- ANV_FROM_HANDLE(anv_buffer, buffer,
- write->pDescriptors[j].bufferInfo.buffer);
- assert(buffer);
-
- desc[j] = (struct anv_descriptor) {
- .type = ANV_DESCRIPTOR_TYPE_BUFFER_AND_OFFSET,
- .buffer = buffer,
- .offset = write->pDescriptors[j].bufferInfo.offset,
- .range = write->pDescriptors[j].bufferInfo.range,
- };
- }
- }
-
- default:
- break;
- }
- }
-
- for (uint32_t i = 0; i < copyCount; i++) {
- const VkCopyDescriptorSet *copy = &pDescriptorCopies[i];
- ANV_FROM_HANDLE(anv_descriptor_set, src, copy->destSet);
- ANV_FROM_HANDLE(anv_descriptor_set, dest, copy->destSet);
- for (uint32_t j = 0; j < copy->count; j++) {
- dest->descriptors[copy->destBinding + j] =
- src->descriptors[copy->srcBinding + j];
- }
- }
-}
-