From 85337fb5aa1ce63023ddd5e284ae6b2ccdbb5e30 Mon Sep 17 00:00:00 2001 From: Frederico Linhares Date: Mon, 30 Oct 2023 15:06:56 -0300 Subject: refa Move texture sampler to texture object --- src/vk/descriptor_set_layout.cpp | 44 +++++------- src/vk/descriptor_set_layout.hpp | 2 +- src/vk/graphics_pipeline_2d_solid.cpp | 2 +- src/vk/graphics_pipeline_2d_solid_layout.cpp | 2 +- src/vk/graphics_pipeline_3d.cpp | 5 +- src/vk/graphics_pipeline_3d_layout.cpp | 5 +- src/vk/graphics_pipeline_3d_skeletal.cpp | 3 +- src/vk/graphics_pipeline_sprite_3d.cpp | 5 +- src/vk/skeletal_model.cpp | 22 +----- src/vk/sprite.cpp | 92 +----------------------- src/vk/sprite.hpp | 5 +- src/vk/sprite_3d.cpp | 17 +---- src/vk/static_model.cpp | 22 +----- src/vk/texture.cpp | 103 ++++++++++++++++++++++++++- src/vk/texture.hpp | 6 ++ 15 files changed, 148 insertions(+), 187 deletions(-) (limited to 'src/vk') diff --git a/src/vk/descriptor_set_layout.cpp b/src/vk/descriptor_set_layout.cpp index 0a96a56..8a2f4ef 100644 --- a/src/vk/descriptor_set_layout.cpp +++ b/src/vk/descriptor_set_layout.cpp @@ -101,22 +101,18 @@ unload_view(void *obj) } void -load_model(void *obj) +load_texture(void *obj) { auto self = static_cast(obj); - std::array layout_bindings; + std::array layout_bindings{}; + layout_bindings[0].binding = 0; - layout_bindings[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + layout_bindings[0].descriptorType = + VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; layout_bindings[0].descriptorCount = 1; - layout_bindings[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT; + layout_bindings[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; layout_bindings[0].pImmutableSamplers = nullptr; - layout_bindings[1].binding = 1; - layout_bindings[1].descriptorType = - VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; - layout_bindings[1].descriptorCount = 1; - layout_bindings[1].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; - layout_bindings[1].pImmutableSamplers = nullptr; VkDescriptorSetLayoutCreateInfo layout_info{}; layout_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; @@ -127,32 +123,30 @@ load_model(void *obj) if(vkCreateDescriptorSetLayout( cg_core.vk_device_with_swapchain->device, &layout_info, nullptr, - &self->model) != VK_SUCCESS) + &self->texture) != VK_SUCCESS) throw CommandError{ - "Failed to create Vulkan descriptor set layout for model instance."}; + "Failed to create Vulkan descriptor set layout for textures."}; } void -unload_model(void *obj) +unload_texture(void *obj) { auto self = static_cast(obj); vkDestroyDescriptorSetLayout( - cg_core.vk_device_with_swapchain->device, self->model, nullptr); + cg_core.vk_device_with_swapchain->device, self->texture, nullptr); } void -load_sprite(void *obj) +load_model(void *obj) { auto self = static_cast(obj); - std::array layout_bindings{}; - + std::array layout_bindings; layout_bindings[0].binding = 0; - layout_bindings[0].descriptorType = - VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; + layout_bindings[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; layout_bindings[0].descriptorCount = 1; - layout_bindings[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; + layout_bindings[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT; layout_bindings[0].pImmutableSamplers = nullptr; VkDescriptorSetLayoutCreateInfo layout_info{}; @@ -164,25 +158,25 @@ load_sprite(void *obj) if(vkCreateDescriptorSetLayout( cg_core.vk_device_with_swapchain->device, &layout_info, nullptr, - &self->sprite) != VK_SUCCESS) + &self->model) != VK_SUCCESS) throw CommandError{ - "Failed to create Vulkan descriptor set layout for sprites."}; + "Failed to create Vulkan descriptor set layout for model instance."}; } void -unload_sprite(void *obj) +unload_model(void *obj) { auto self = static_cast(obj); vkDestroyDescriptorSetLayout( - cg_core.vk_device_with_swapchain->device, self->sprite, nullptr); + cg_core.vk_device_with_swapchain->device, self->model, nullptr); } const CommandChain loader{ {&load_world, &unload_world}, {&load_view, &unload_view}, + {&load_texture, &unload_texture}, {&load_model, &unload_model}, - {&load_sprite, &unload_sprite}, }; } diff --git a/src/vk/descriptor_set_layout.hpp b/src/vk/descriptor_set_layout.hpp index 5401fed..242d2fa 100644 --- a/src/vk/descriptor_set_layout.hpp +++ b/src/vk/descriptor_set_layout.hpp @@ -26,8 +26,8 @@ struct DescriptorSetLayout { VkDescriptorSetLayout world; VkDescriptorSetLayout view; + VkDescriptorSetLayout texture; VkDescriptorSetLayout model; - VkDescriptorSetLayout sprite; DescriptorSetLayout(); ~DescriptorSetLayout(); diff --git a/src/vk/graphics_pipeline_2d_solid.cpp b/src/vk/graphics_pipeline_2d_solid.cpp index 00bc26b..f914594 100644 --- a/src/vk/graphics_pipeline_2d_solid.cpp +++ b/src/vk/graphics_pipeline_2d_solid.cpp @@ -271,7 +271,7 @@ GraphicsPipeline2DSolid::draw( { std::array vk_descriptor_sets{ view->descriptor_sets_2d[image_index], - sprite_to_draw.sprite->descriptor_sets[image_index]}; + sprite_to_draw.sprite->texture->descriptor_sets[image_index]}; VkDeviceSize offsets[]{0}; vkCmdBindDescriptorSets( diff --git a/src/vk/graphics_pipeline_2d_solid_layout.cpp b/src/vk/graphics_pipeline_2d_solid_layout.cpp index 712e840..11d080b 100644 --- a/src/vk/graphics_pipeline_2d_solid_layout.cpp +++ b/src/vk/graphics_pipeline_2d_solid_layout.cpp @@ -31,7 +31,7 @@ load_pipeline(void *obj) std::array set_layouts{ cg_core.vk_descriptor_set_layout->view, - cg_core.vk_descriptor_set_layout->sprite + cg_core.vk_descriptor_set_layout->texture }; std::array push_constants; diff --git a/src/vk/graphics_pipeline_3d.cpp b/src/vk/graphics_pipeline_3d.cpp index 6bfd559..f657a54 100644 --- a/src/vk/graphics_pipeline_3d.cpp +++ b/src/vk/graphics_pipeline_3d.cpp @@ -305,10 +305,11 @@ GraphicsPipeline3D::draw( base_matrix = glm::rotate( base_matrix, instance->rotation->z, glm::vec3{0.0, 0.0, 1.0}); - std::array vk_descriptor_sets{ + std::array vk_descriptor_sets{ cg_core.vk_light->descriptor_sets_world[image_index], view->descriptor_sets_3d[image_index], - instance->descriptor_sets[image_index]}; + instance->descriptor_sets[image_index], + instance->texture->descriptor_sets[image_index]}; vkCmdBindDescriptorSets( draw_command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, diff --git a/src/vk/graphics_pipeline_3d_layout.cpp b/src/vk/graphics_pipeline_3d_layout.cpp index 5f8cf5b..c316518 100644 --- a/src/vk/graphics_pipeline_3d_layout.cpp +++ b/src/vk/graphics_pipeline_3d_layout.cpp @@ -29,10 +29,11 @@ load_pipeline(void *obj) { auto self = static_cast(obj); - std::array set_layouts{ + std::array set_layouts{ cg_core.vk_descriptor_set_layout->world, cg_core.vk_descriptor_set_layout->view, - cg_core.vk_descriptor_set_layout->model}; + cg_core.vk_descriptor_set_layout->model, + cg_core.vk_descriptor_set_layout->texture}; VkPipelineLayoutCreateInfo pipeline_layout_info{}; pipeline_layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; diff --git a/src/vk/graphics_pipeline_3d_skeletal.cpp b/src/vk/graphics_pipeline_3d_skeletal.cpp index e0e75a9..c4dabdf 100644 --- a/src/vk/graphics_pipeline_3d_skeletal.cpp +++ b/src/vk/graphics_pipeline_3d_skeletal.cpp @@ -314,9 +314,10 @@ GraphicsPipeline3DSkeletal::draw( base_matrix = glm::rotate( base_matrix, instance->rotation->z, glm::vec3{0.0, 0.0, 1.0}); - std::array vk_descriptor_sets{ + std::array vk_descriptor_sets{ cg_core.vk_light->descriptor_sets_world[image_index], view->descriptor_sets_3d[image_index], + instance->texture->descriptor_sets[image_index], instance->descriptor_sets[image_index]}; vkCmdBindDescriptorSets( diff --git a/src/vk/graphics_pipeline_sprite_3d.cpp b/src/vk/graphics_pipeline_sprite_3d.cpp index d14cb48..577824a 100644 --- a/src/vk/graphics_pipeline_sprite_3d.cpp +++ b/src/vk/graphics_pipeline_sprite_3d.cpp @@ -307,10 +307,11 @@ GraphicsPipelineSprite3D::draw( // Draw sprites for(auto& sprite: sprite_3d_order) { - std::array vk_descriptor_sets{ + std::array vk_descriptor_sets{ cg_core.vk_light->descriptor_sets_world[image_index], view->descriptor_sets_3d[image_index], - sprite.sprite_3d->descriptor_sets[image_index]}; + sprite.sprite_3d->descriptor_sets[image_index], + sprite.sprite_3d->sprite->texture->descriptor_sets[image_index]}; VkDeviceSize offsets[]{0}; vkCmdBindVertexBuffers( diff --git a/src/vk/skeletal_model.cpp b/src/vk/skeletal_model.cpp index 71957f9..b2d5fc3 100644 --- a/src/vk/skeletal_model.cpp +++ b/src/vk/skeletal_model.cpp @@ -53,13 +53,10 @@ load_descriptor_set_pool(void *obj) { auto self = static_cast(obj); - std::array descriptor_pool_sizes{}; + std::array descriptor_pool_sizes{}; descriptor_pool_sizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; descriptor_pool_sizes[0].descriptorCount = self->uniform_buffers.size(); - descriptor_pool_sizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; - descriptor_pool_sizes[1].descriptorCount = - cg_core.vk_swapchain->images_count; VkDescriptorPoolCreateInfo pool_info{}; pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; @@ -119,12 +116,7 @@ load_buffers_to_descriptor_sets(void *obj) buffer_info.offset = 0; buffer_info.range = sizeof(VK::UDOSkeletalModel); - VkDescriptorImageInfo image_info{}; - image_info.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; - image_info.imageView = self->texture->view; - image_info.sampler = self->texture->sampler; - - std::array write_descriptors{}; + std::array write_descriptors{}; write_descriptors[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; write_descriptors[0].dstSet = self->descriptor_sets[i]; write_descriptors[0].dstBinding = 0; @@ -134,16 +126,6 @@ load_buffers_to_descriptor_sets(void *obj) write_descriptors[0].pBufferInfo = &buffer_info; write_descriptors[0].pImageInfo = nullptr; write_descriptors[0].pTexelBufferView = nullptr; - write_descriptors[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - write_descriptors[1].dstSet = self->descriptor_sets[i]; - write_descriptors[1].dstBinding = 1; - write_descriptors[1].dstArrayElement = 0; - write_descriptors[1].descriptorCount = 1; - write_descriptors[1].descriptorType = - VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; - write_descriptors[1].pBufferInfo = nullptr; - write_descriptors[1].pImageInfo = &image_info; - write_descriptors[1].pTexelBufferView = nullptr; vkUpdateDescriptorSets( cg_core.vk_device_with_swapchain->device, write_descriptors.size(), diff --git a/src/vk/sprite.cpp b/src/vk/sprite.cpp index 146d07d..95e11a3 100644 --- a/src/vk/sprite.cpp +++ b/src/vk/sprite.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2022 Frederico de Oliveira Linhares + * Copyright 2022-2023 Frederico de Oliveira Linhares * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -73,98 +73,8 @@ unload_mesh(void *obj) delete self->sprite->source_buffer; } -void -load_descriptor_set_pool(void *obj) -{ - auto self = static_cast(obj); - - std::array descriptor_pool_sizes{}; - descriptor_pool_sizes[0].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; - descriptor_pool_sizes[0].descriptorCount = - cg_core.vk_swapchain->images_count; - - VkDescriptorPoolCreateInfo pool_info{}; - pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; - pool_info.pNext = nullptr; - pool_info.flags = 0; - pool_info.maxSets = cg_core.vk_swapchain->images_count; - pool_info.poolSizeCount = descriptor_pool_sizes.size(); - pool_info.pPoolSizes = descriptor_pool_sizes.data(); - - if(vkCreateDescriptorPool( - self->sprite->queue_family->device->device, &pool_info, nullptr, - &self->sprite->descriptor_pool) != VK_SUCCESS) - throw CommandError{"Failed to create a Vulkan descriptor pool."}; - -} - -void -unload_descriptor_set_pool(void *obj) -{ - auto self = static_cast(obj); - - vkDestroyDescriptorPool( - self->sprite->queue_family->device->device, self->sprite->descriptor_pool, - nullptr); -} - -void -load_descriptor_sets(void *obj) -{ - auto self = static_cast(obj); - - std::vector layouts( - cg_core.vk_swapchain->images_count, - cg_core.vk_descriptor_set_layout->sprite); - - VkDescriptorSetAllocateInfo alloc_info{}; - alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; - alloc_info.descriptorPool = self->sprite->descriptor_pool; - alloc_info.descriptorSetCount = layouts.size(); - alloc_info.pSetLayouts = layouts.data(); - - self->sprite->descriptor_sets.resize(layouts.size()); - if(vkAllocateDescriptorSets( - self->sprite->queue_family->device->device, &alloc_info, - self->sprite->descriptor_sets.data()) != VK_SUCCESS) - CommandError{"Failed to create Vulkan descriptor set."}; -} - -void -load_buffers_to_descriptor_sets(void *obj) -{ - auto self = static_cast(obj); - - for(auto i{0}; i < cg_core.vk_swapchain->images_count; i++) - { - VkDescriptorImageInfo image_info{}; - image_info.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; - image_info.imageView = self->sprite->texture->view; - image_info.sampler = self->sprite->texture->sampler; - - std::array write_descriptors{}; - write_descriptors[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - write_descriptors[0].dstSet = self->sprite->descriptor_sets[i]; - write_descriptors[0].dstBinding = 0; - write_descriptors[0].dstArrayElement = 0; - write_descriptors[0].descriptorCount = 1; - write_descriptors[0].descriptorType = - VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; - write_descriptors[0].pBufferInfo = nullptr; - write_descriptors[0].pImageInfo = &image_info; - write_descriptors[0].pTexelBufferView = nullptr; - - vkUpdateDescriptorSets( - cg_core.vk_device_with_swapchain->device, write_descriptors.size(), - write_descriptors.data(), 0, nullptr); - } -} - static const CommandChain loader{ {&load_mesh, &unload_mesh}, - {&load_descriptor_set_pool, &unload_descriptor_set_pool}, - {&load_descriptor_sets, nullptr}, - {&load_buffers_to_descriptor_sets, nullptr}, }; } diff --git a/src/vk/sprite.hpp b/src/vk/sprite.hpp index 5934501..791144e 100644 --- a/src/vk/sprite.hpp +++ b/src/vk/sprite.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2022 Frederico de Oliveira Linhares + * Copyright 2022-2023 Frederico de Oliveira Linhares * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,9 +39,6 @@ struct Sprite SourceBuffer *source_buffer; DestinationBuffer *vertex_buffer; - VkDescriptorPool descriptor_pool; - std::vector descriptor_sets; - std::shared_ptr texture; Sprite(std::shared_ptr texture, glm::vec4 &rect); diff --git a/src/vk/sprite_3d.cpp b/src/vk/sprite_3d.cpp index 9794285..66199f1 100644 --- a/src/vk/sprite_3d.cpp +++ b/src/vk/sprite_3d.cpp @@ -53,13 +53,10 @@ load_descriptor_set_pool(void *obj) { auto self = static_cast(obj); - std::array descriptor_pool_sizes{}; + std::array descriptor_pool_sizes{}; descriptor_pool_sizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; descriptor_pool_sizes[0].descriptorCount = self->uniform_buffers.size(); - descriptor_pool_sizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; - descriptor_pool_sizes[1].descriptorCount = - cg_core.vk_swapchain->images_count; VkDescriptorPoolCreateInfo pool_info{}; pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; @@ -122,7 +119,7 @@ load_buffers_to_descriptor_sets(void *obj) image_info.imageView = self->sprite->texture->view; image_info.sampler = self->sprite->texture->sampler; - std::array write_descriptors{}; + std::array write_descriptors{}; write_descriptors[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; write_descriptors[0].dstSet = self->descriptor_sets[i]; write_descriptors[0].dstBinding = 0; @@ -132,16 +129,6 @@ load_buffers_to_descriptor_sets(void *obj) write_descriptors[0].pBufferInfo = &buffer_info; write_descriptors[0].pImageInfo = nullptr; write_descriptors[0].pTexelBufferView = nullptr; - write_descriptors[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - write_descriptors[1].dstSet = self->descriptor_sets[i]; - write_descriptors[1].dstBinding = 1; - write_descriptors[1].dstArrayElement = 0; - write_descriptors[1].descriptorCount = 1; - write_descriptors[1].descriptorType = - VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; - write_descriptors[1].pBufferInfo = nullptr; - write_descriptors[1].pImageInfo = &image_info; - write_descriptors[1].pTexelBufferView = nullptr; vkUpdateDescriptorSets( cg_core.vk_device_with_swapchain->device, write_descriptors.size(), diff --git a/src/vk/static_model.cpp b/src/vk/static_model.cpp index 4de0302..a89d79a 100644 --- a/src/vk/static_model.cpp +++ b/src/vk/static_model.cpp @@ -53,13 +53,10 @@ load_descriptor_set_pool(void *obj) { auto self = static_cast(obj); - std::array descriptor_pool_sizes{}; + std::array descriptor_pool_sizes{}; descriptor_pool_sizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; descriptor_pool_sizes[0].descriptorCount = self->uniform_buffers.size(); - descriptor_pool_sizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; - descriptor_pool_sizes[1].descriptorCount = - cg_core.vk_swapchain->images_count; VkDescriptorPoolCreateInfo pool_info{}; pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; @@ -119,12 +116,7 @@ load_buffers_to_descriptor_sets(void *obj) buffer_info.offset = 0; buffer_info.range = sizeof(VK::UDOStaticModel); - VkDescriptorImageInfo image_info{}; - image_info.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; - image_info.imageView = self->texture->view; - image_info.sampler = self->texture->sampler; - - std::array write_descriptors{}; + std::array write_descriptors{}; write_descriptors[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; write_descriptors[0].dstSet = self->descriptor_sets[i]; write_descriptors[0].dstBinding = 0; @@ -134,16 +126,6 @@ load_buffers_to_descriptor_sets(void *obj) write_descriptors[0].pBufferInfo = &buffer_info; write_descriptors[0].pImageInfo = nullptr; write_descriptors[0].pTexelBufferView = nullptr; - write_descriptors[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - write_descriptors[1].dstSet = self->descriptor_sets[i]; - write_descriptors[1].dstBinding = 1; - write_descriptors[1].dstArrayElement = 0; - write_descriptors[1].descriptorCount = 1; - write_descriptors[1].descriptorType = - VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; - write_descriptors[1].pBufferInfo = nullptr; - write_descriptors[1].pImageInfo = &image_info; - write_descriptors[1].pTexelBufferView = nullptr; vkUpdateDescriptorSets( cg_core.vk_device_with_swapchain->device, write_descriptors.size(), diff --git a/src/vk/texture.cpp b/src/vk/texture.cpp index 92ce5b5..063eb86 100644 --- a/src/vk/texture.cpp +++ b/src/vk/texture.cpp @@ -111,8 +111,7 @@ load_image(void *obj) // Copy image from vulkan buffer into vulkan image. { - auto queue_family{ - cg_core.vk_device_with_swapchain->get_queue_family_with_presentation()}; + auto queue_family = self->texture->queue_family; auto queue{queue_family->get_queue()}; VK::CommandPool command_pool{queue_family, 1}; VkCommandBuffer vk_command_buffer{command_pool.command_buffers[0]}; @@ -419,6 +418,97 @@ const CommandChain text_loader{ {&load_view, &unload_view} }; +void +load_descriptor_set_pool(void *obj) +{ + auto self = static_cast(obj); + + std::array descriptor_pool_sizes{}; + descriptor_pool_sizes[0].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; + descriptor_pool_sizes[0].descriptorCount = + cg_core.vk_swapchain->images_count; + + VkDescriptorPoolCreateInfo pool_info{}; + pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; + pool_info.pNext = nullptr; + pool_info.flags = 0; + pool_info.maxSets = cg_core.vk_swapchain->images_count; + pool_info.poolSizeCount = descriptor_pool_sizes.size(); + pool_info.pPoolSizes = descriptor_pool_sizes.data(); + + if(vkCreateDescriptorPool( + self->queue_family->device->device, &pool_info, nullptr, + &self->descriptor_pool) != VK_SUCCESS) + throw CommandError{"Failed to create a Vulkan descriptor pool."}; +} + +void +unload_descriptor_set_pool(void *obj) +{ + auto self = static_cast(obj); + + vkDestroyDescriptorPool( + self->queue_family->device->device, self->descriptor_pool, nullptr); +} + +void +load_descriptor_sets(void *obj) +{ + auto self = static_cast(obj); + + std::vector layouts( + cg_core.vk_swapchain->images_count, + cg_core.vk_descriptor_set_layout->texture); + + VkDescriptorSetAllocateInfo alloc_info{}; + alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; + alloc_info.descriptorPool = self->descriptor_pool; + alloc_info.descriptorSetCount = layouts.size(); + alloc_info.pSetLayouts = layouts.data(); + + self->descriptor_sets.resize(layouts.size()); + if(vkAllocateDescriptorSets( + self->queue_family->device->device, &alloc_info, + self->descriptor_sets.data()) != VK_SUCCESS) + CommandError{"Failed to create Vulkan descriptor set."}; +} + +void +load_data_to_descriptor_sets(void *obj) +{ + auto self = static_cast(obj); + + for(auto i{0}; i < cg_core.vk_swapchain->images_count; i++) + { + VkDescriptorImageInfo image_info{}; + image_info.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; + image_info.imageView = self->view; + image_info.sampler = self->sampler; + + std::array write_descriptors{}; + write_descriptors[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + write_descriptors[0].dstSet = self->descriptor_sets[i]; + write_descriptors[0].dstBinding = 0; + write_descriptors[0].dstArrayElement = 0; + write_descriptors[0].descriptorCount = 1; + write_descriptors[0].descriptorType = + VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; + write_descriptors[0].pBufferInfo = nullptr; + write_descriptors[0].pImageInfo = &image_info; + write_descriptors[0].pTexelBufferView = nullptr; + + vkUpdateDescriptorSets( + cg_core.vk_device_with_swapchain->device, write_descriptors.size(), + write_descriptors.data(), 0, nullptr); + } +} + +const CommandChain descriptor_loader{ + {&load_descriptor_set_pool, &unload_descriptor_set_pool}, + {&load_descriptor_sets, nullptr}, + {&load_data_to_descriptor_sets, nullptr} +}; + } namespace VK @@ -426,14 +516,22 @@ namespace VK Texture::Texture(Font *font, const char* str) { + this->queue_family = + cg_core.vk_device_with_swapchain->get_queue_family_with_presentation(); + TextTextureBuilder text_builder(this, font, str); text_loader.execute(&text_builder); + descriptor_loader.execute(this); } Texture::Texture(std::string texture_path) { + this->queue_family = + cg_core.vk_device_with_swapchain->get_queue_family_with_presentation(); + ImageTextureBuilder texture_builder(this, texture_path); image_loader.execute(&texture_builder); + descriptor_loader.execute(this); } Texture::Texture(const char* texture_path): @@ -445,6 +543,7 @@ Texture::~Texture() { ImageTextureBuilder texture_builder(this, ""); image_loader.revert(&texture_builder); + descriptor_loader.revert(this); } } diff --git a/src/vk/texture.hpp b/src/vk/texture.hpp index c489e90..22711eb 100644 --- a/src/vk/texture.hpp +++ b/src/vk/texture.hpp @@ -21,12 +21,15 @@ #include "core.hpp" #include "font.hpp" +#include "queue_family.hpp" namespace VK { struct Texture { + QueueFamily *queue_family; + VkImage image; VkSampler sampler; VkImageView view; @@ -34,6 +37,9 @@ struct Texture uint32_t width, height; uint32_t mip_levels; + VkDescriptorPool descriptor_pool; + std::vector descriptor_sets; + Texture(Font *font, const char *str); Texture(std::string texture_path); Texture(const char* texture_path); -- cgit v1.2.3