From e5b15b871f0cf73563431565a492d9ae3e92e35b Mon Sep 17 00:00:00 2001 From: Frederico Linhares Date: Sat, 30 Sep 2023 16:35:55 -0300 Subject: refa Remove redundant descriptor sets * src/vk/light.hpp: Move to this class, the descriptor sets used for lighting by the 3D graphics pipelines. --- src/core.cpp | 20 +++ src/core.hpp | 2 + src/vk/graphics_pipeline_3d.cpp | 162 +----------------------- src/vk/graphics_pipeline_3d.hpp | 9 -- src/vk/graphics_pipeline_3d_skeletal.cpp | 175 +------------------------- src/vk/graphics_pipeline_3d_skeletal.hpp | 10 -- src/vk/light.cpp | 205 +++++++++++++++++++++++++++++++ src/vk/light.hpp | 41 +++++++ src/vk/renderer.cpp | 4 +- 9 files changed, 272 insertions(+), 356 deletions(-) create mode 100644 src/vk/light.cpp create mode 100644 src/vk/light.hpp (limited to 'src') diff --git a/src/core.cpp b/src/core.cpp index 2f3cd77..0197e97 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -622,6 +622,25 @@ unload_vk_graphics_pipeline_2d_wired_layout(void *obj) delete cg_core.vk_graphics_pipeline_2d_wired_layout; } +void +load_vk_light(void *obj) +{ + try + { + cg_core.vk_light = new VK::Light(); + } + catch(const CommandError &e) + { + throw CommandError{"Failed to descriptor sets for light."}; + } +} + +void +unload_vk_light(void *obj) +{ + delete cg_core.vk_light; +} + void load_vk_graphics_pipeline_3d(void *obj) { @@ -774,6 +793,7 @@ const CommandChain cg_sCore::loader{ &unload_vk_graphics_pipeline_2d_solid_layout}, {&load_vk_graphics_pipeline_2d_wired_layout, &unload_vk_graphics_pipeline_2d_wired_layout}, + {&load_vk_light, &unload_vk_light}, // TODO: finish skeletal mesh animation // {&load_vk_graphics_pipeline_3d_skeletal, // &unload_vk_graphics_pipeline_3d_skeletal}, diff --git a/src/core.hpp b/src/core.hpp index 6336f6e..917920b 100644 --- a/src/core.hpp +++ b/src/core.hpp @@ -52,6 +52,7 @@ #include "vk/render_pass.hpp" #include "vk/graphics_pipeline_2d_solid_layout.hpp" #include "vk/graphics_pipeline_2d_wired_layout.hpp" +#include "vk/light.hpp" #include "vk/graphics_pipeline_2d_solid.hpp" #include "vk/graphics_pipeline_2d_wired.hpp" #include "vk/graphics_pipeline_3d_layout.hpp" @@ -124,6 +125,7 @@ struct cg_sCore VK::GraphicsPipeline3DLayout *vk_graphics_pipeline_3d_layout; VK::GraphicsPipeline2DSolidLayout *vk_graphics_pipeline_2d_solid_layout; VK::GraphicsPipeline2DWiredLayout *vk_graphics_pipeline_2d_wired_layout; + VK::Light *vk_light; std::unique_ptr vk_graphics_pipeline_3d; std::unique_ptr vk_graphics_pipeline_3d_skeletal; diff --git a/src/vk/graphics_pipeline_3d.cpp b/src/vk/graphics_pipeline_3d.cpp index a1a1785..a98db2c 100644 --- a/src/vk/graphics_pipeline_3d.cpp +++ b/src/vk/graphics_pipeline_3d.cpp @@ -28,160 +28,6 @@ namespace { -void -load_world_vert_uniform_buffer(void *obj) -{ - auto self = static_cast(obj); - - try - { - self->ub_world_vert.reserve(cg_core.vk_swapchain->images_count); - for(auto i{0}; i < cg_core.vk_swapchain->images_count; i++) - self->ub_world_vert.emplace_back( - cg_core.vk_device_with_swapchain, sizeof(VK::UDOWorld3D_Vert)); - } - catch(const std::exception& e) - { - throw CommandError{e.what()}; - } -} - -void -unload_world_vert_uniform_buffer(void *obj) -{ - auto self = static_cast(obj); - - self->ub_world_vert.clear(); -} - -void -load_world_frag_uniform_buffer(void *obj) -{ - auto self = static_cast(obj); - - try - { - self->ub_world_frag.reserve(cg_core.vk_swapchain->images_count); - for(auto i{0}; i < cg_core.vk_swapchain->images_count; i++) - self->ub_world_frag.emplace_back( - cg_core.vk_device_with_swapchain, sizeof(VK::UDOWorld3D_Frag)); - } - catch(const std::exception& e) - { - throw CommandError{e.what()}; - } -} - -void -unload_world_frag_uniform_buffer(void *obj) -{ - auto self = static_cast(obj); - - self->ub_world_frag.clear(); -} - -void -load_descriptor_pool(void *obj) -{ - auto self = static_cast(obj); - - uint32_t uniform_buffers_count = - self->ub_world_vert.size() + self->ub_world_vert.size(); - - VkDescriptorPoolSize descriptor_pool_size{}; - descriptor_pool_size.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - descriptor_pool_size.descriptorCount = uniform_buffers_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 = uniform_buffers_count; - pool_info.poolSizeCount = 1; - pool_info.pPoolSizes = &descriptor_pool_size; - - if(vkCreateDescriptorPool( - cg_core.vk_device_with_swapchain->device, &pool_info, nullptr, - &self->descriptor_pool) != VK_SUCCESS) - throw CommandError{"Failed to create a Vulkan descriptor pool."}; -} - -void -unload_descriptor_pool(void *obj) -{ - auto self = static_cast(obj); - - vkDestroyDescriptorPool( - cg_core.vk_device_with_swapchain->device, self->descriptor_pool, - nullptr); -} - -void -load_descriptor_sets_world(void *obj) -{ - auto self = static_cast(obj); - - std::vector layouts( - cg_core.vk_swapchain->images_count, - cg_core.vk_descriptor_set_layout->world); - - 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_world.resize(layouts.size()); - if(vkAllocateDescriptorSets( - cg_core.vk_device_with_swapchain->device, &alloc_info, - self->descriptor_sets_world.data()) != VK_SUCCESS) - throw CommandError{"Failed to create Vulkan world descriptor set."}; -} - -void -load_resources_to_descriptor_sets(void *obj) -{ - auto self = static_cast(obj); - - for(auto i{0}; i < cg_core.vk_swapchain->images_count; i++) - { - VkDescriptorBufferInfo world_vert_info{}; - world_vert_info.buffer = self->ub_world_vert[i].buffer; - world_vert_info.offset = 0; - world_vert_info.range = sizeof(VK::UDOWorld3D_Vert); - - VkDescriptorBufferInfo world_frag_info{}; - world_frag_info.buffer = self->ub_world_frag[i].buffer; - world_frag_info.offset = 0; - world_frag_info.range = sizeof(VK::UDOWorld3D_Frag); - - std::array write_descriptors{}; - write_descriptors[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - write_descriptors[0].dstSet = self->descriptor_sets_world[i]; - write_descriptors[0].dstBinding = 0; - write_descriptors[0].dstArrayElement = 0; - write_descriptors[0].descriptorCount = 1; - write_descriptors[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - write_descriptors[0].pBufferInfo = &world_vert_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_world[i]; - write_descriptors[1].dstBinding = 1; - write_descriptors[1].dstArrayElement = 0; - write_descriptors[1].descriptorCount = 1; - write_descriptors[1].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - write_descriptors[1].pBufferInfo = &world_frag_info; - write_descriptors[1].pImageInfo = nullptr; - write_descriptors[1].pTexelBufferView = nullptr; - - vkUpdateDescriptorSets( - cg_core.vk_device_with_swapchain->device, write_descriptors.size(), - write_descriptors.data(), 0, nullptr); - } -} - void load_depth_image(void *obj) { @@ -504,12 +350,6 @@ unload_pipeline(void *obj) } const CommandChain loader{ - {&load_world_vert_uniform_buffer, &unload_world_vert_uniform_buffer}, - {&load_world_frag_uniform_buffer, &unload_world_frag_uniform_buffer}, - {&load_descriptor_pool, &unload_descriptor_pool}, - // By destroying the pool the sets are also destroyed. - {&load_descriptor_sets_world, nullptr}, - {&load_resources_to_descriptor_sets, nullptr}, {&load_depth_image, &unload_depth_image}, {&load_depth_image_view, &unload_depth_image_view}, {&load_framebuffer, &unload_framebuffer}, @@ -582,7 +422,7 @@ GraphicsPipeline3D::draw( base_matrix, instance->rotation->z, glm::vec3{0.0, 0.0, 1.0}); std::array vk_descriptor_sets{ - this->descriptor_sets_world[image_index], + cg_core.vk_light->descriptor_sets_world[image_index], view->descriptor_sets_3d[image_index], instance->descriptor_sets[image_index]}; diff --git a/src/vk/graphics_pipeline_3d.hpp b/src/vk/graphics_pipeline_3d.hpp index d174b6e..af09d28 100644 --- a/src/vk/graphics_pipeline_3d.hpp +++ b/src/vk/graphics_pipeline_3d.hpp @@ -20,10 +20,8 @@ #include #include -#include "../command.hpp" #include "core.hpp" #include "command_pool.hpp" -#include "uniform_buffer.hpp" #include "view_3d.hpp" namespace VK @@ -36,13 +34,6 @@ struct GraphicsPipeline3D VkDeviceMemory depth_image_memory; VkImageView depth_image_view; - // FIXME: if this vector get resized, it will cause a segmentation fault! - std::vector ub_world_vert; - std::vector ub_world_frag; - - VkDescriptorPool descriptor_pool; - std::vector descriptor_sets_world; - std::vector swapchain_framebuffers; VkPipeline graphic_pipeline; diff --git a/src/vk/graphics_pipeline_3d_skeletal.cpp b/src/vk/graphics_pipeline_3d_skeletal.cpp index bf75b25..3386352 100644 --- a/src/vk/graphics_pipeline_3d_skeletal.cpp +++ b/src/vk/graphics_pipeline_3d_skeletal.cpp @@ -28,160 +28,6 @@ namespace { -void -load_world_vert_uniform_buffer(void *obj) -{ - auto self = static_cast(obj); - - try - { - self->ub_world_vert.reserve(cg_core.vk_swapchain->images_count); - for(auto i{0}; i < cg_core.vk_swapchain->images_count; i++) - self->ub_world_vert.emplace_back( - cg_core.vk_device_with_swapchain, sizeof(VK::UDOWorld3D_Vert)); - } - catch(const std::exception& e) - { - throw CommandError{e.what()}; - } -} - -void -unload_world_vert_uniform_buffer(void *obj) -{ - auto self = static_cast(obj); - - self->ub_world_vert.clear(); -} - -void -load_world_frag_uniform_buffer(void *obj) -{ - auto self = static_cast(obj); - - try - { - self->ub_world_frag.reserve(cg_core.vk_swapchain->images_count); - for(auto i{0}; i < cg_core.vk_swapchain->images_count; i++) - self->ub_world_frag.emplace_back( - cg_core.vk_device_with_swapchain, sizeof(VK::UDOWorld3D_Frag)); - } - catch(const std::exception& e) - { - throw CommandError{e.what()}; - } -} - -void -unload_world_frag_uniform_buffer(void *obj) -{ - auto self = static_cast(obj); - - self->ub_world_frag.clear(); -} - -void -load_descriptor_pool(void *obj) -{ - auto self = static_cast(obj); - - uint32_t uniform_buffers_count = - self->ub_world_vert.size() + self->ub_world_vert.size(); - - VkDescriptorPoolSize descriptor_pool_size{}; - descriptor_pool_size.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - descriptor_pool_size.descriptorCount = uniform_buffers_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 = uniform_buffers_count; - pool_info.poolSizeCount = 1; - pool_info.pPoolSizes = &descriptor_pool_size; - - if(vkCreateDescriptorPool( - cg_core.vk_device_with_swapchain->device, &pool_info, nullptr, - &self->descriptor_pool) != VK_SUCCESS) - throw CommandError{"Failed to create a Vulkan descriptor pool."}; -} - -void -unload_descriptor_pool(void *obj) -{ - auto self = static_cast(obj); - - vkDestroyDescriptorPool( - cg_core.vk_device_with_swapchain->device, self->descriptor_pool, - nullptr); -} - -void -load_descriptor_sets_world(void *obj) -{ - auto self = static_cast(obj); - - std::vector layouts( - cg_core.vk_swapchain->images_count, - cg_core.vk_descriptor_set_layout->world); - - 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_world.resize(layouts.size()); - if(vkAllocateDescriptorSets( - cg_core.vk_device_with_swapchain->device, &alloc_info, - self->descriptor_sets_world.data()) != VK_SUCCESS) - throw CommandError{"Failed to create Vulkan world descriptor set."}; -} - -void -load_resources_to_descriptor_sets(void *obj) -{ - auto self = static_cast(obj); - - for(auto i{0}; i < cg_core.vk_swapchain->images_count; i++) - { - VkDescriptorBufferInfo world_vert_info{}; - world_vert_info.buffer = self->ub_world_vert[i].buffer; - world_vert_info.offset = 0; - world_vert_info.range = sizeof(VK::UDOWorld3D_Vert); - - VkDescriptorBufferInfo world_frag_info{}; - world_frag_info.buffer = self->ub_world_frag[i].buffer; - world_frag_info.offset = 0; - world_frag_info.range = sizeof(VK::UDOWorld3D_Frag); - - std::array write_descriptors{}; - write_descriptors[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - write_descriptors[0].dstSet = self->descriptor_sets_world[i]; - write_descriptors[0].dstBinding = 0; - write_descriptors[0].dstArrayElement = 0; - write_descriptors[0].descriptorCount = 1; - write_descriptors[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - write_descriptors[0].pBufferInfo = &world_vert_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_world[i]; - write_descriptors[1].dstBinding = 1; - write_descriptors[1].dstArrayElement = 0; - write_descriptors[1].descriptorCount = 1; - write_descriptors[1].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - write_descriptors[1].pBufferInfo = &world_frag_info; - write_descriptors[1].pImageInfo = nullptr; - write_descriptors[1].pTexelBufferView = nullptr; - - vkUpdateDescriptorSets( - cg_core.vk_device_with_swapchain->device, write_descriptors.size(), - write_descriptors.data(), 0, nullptr); - } -} - void load_depth_image(void *obj) { @@ -514,12 +360,6 @@ unload_pipeline(void *obj) } const CommandChain loader{ - {&load_world_vert_uniform_buffer, &unload_world_vert_uniform_buffer}, - {&load_world_frag_uniform_buffer, &unload_world_frag_uniform_buffer}, - {&load_descriptor_pool, &unload_descriptor_pool}, - // By destroying the pool the sets are also destroyed. - {&load_descriptor_sets_world, nullptr}, - {&load_resources_to_descriptor_sets, nullptr}, {&load_depth_image, &unload_depth_image}, {&load_depth_image_view, &unload_depth_image_view}, {&load_framebuffer, &unload_framebuffer}, @@ -592,7 +432,7 @@ GraphicsPipeline3DSkeletal::draw( base_matrix, instance->rotation->z, glm::vec3{0.0, 0.0, 1.0}); std::array vk_descriptor_sets{ - this->descriptor_sets_world[image_index], + cg_core.vk_light->descriptor_sets_world[image_index], view->descriptor_sets_3d[image_index], instance->descriptor_sets[image_index]}; @@ -637,19 +477,6 @@ GraphicsPipeline3DSkeletal::draw( view->ub_3d[image_index].copy_data(&ubo_view_3d); } - - // TODO: Do not update this for each view. All views use the same data. - { // Update world uniform buffer - UDOWorld3D_Vert ubo_world_3d_vert{}; - ubo_world_3d_vert.ambient_light_color = glm::vec4{0.25, 0.25, 0.25, 1.0}; - this->ub_world_vert[image_index].copy_data(&ubo_world_3d_vert); - - UDOWorld3D_Frag ubo_world_3d_frag{}; - ubo_world_3d_frag.directional_light_direction = - glm::vec3{-0.57735, 0.57735, -0.57735}; - ubo_world_3d_frag.directional_light_color = glm::vec4{0.8, 0.8, 0.8, 1.0}; - this->ub_world_frag[image_index].copy_data(&ubo_world_3d_frag); - } } } diff --git a/src/vk/graphics_pipeline_3d_skeletal.hpp b/src/vk/graphics_pipeline_3d_skeletal.hpp index 9563e14..fa9226a 100644 --- a/src/vk/graphics_pipeline_3d_skeletal.hpp +++ b/src/vk/graphics_pipeline_3d_skeletal.hpp @@ -20,11 +20,8 @@ #include #include -#include "../command.hpp" #include "core.hpp" #include "command_pool.hpp" -#include "skeletal_model.hpp" -#include "uniform_buffer.hpp" #include "view_3d.hpp" namespace VK @@ -37,13 +34,6 @@ struct GraphicsPipeline3DSkeletal VkDeviceMemory depth_image_memory; VkImageView depth_image_view; - // FIXME: if this vector get resized, it will cause a segmentation fault! - std::vector ub_world_vert; - std::vector ub_world_frag; - - VkDescriptorPool descriptor_pool; - std::vector descriptor_sets_world; - std::vector swapchain_framebuffers; VkPipeline graphic_pipeline; diff --git a/src/vk/light.cpp b/src/vk/light.cpp new file mode 100644 index 0000000..5ed07ee --- /dev/null +++ b/src/vk/light.cpp @@ -0,0 +1,205 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "light.hpp" + +#include + +#include "../core.hpp" +#include "uniform_data_object.hpp" + +namespace +{ + +void +load_world_vert_uniform_buffer(void *obj) +{ + auto self = static_cast(obj); + + try + { + self->ub_world_vert.reserve(cg_core.vk_swapchain->images_count); + for(auto i{0}; i < cg_core.vk_swapchain->images_count; i++) + self->ub_world_vert.emplace_back( + cg_core.vk_device_with_swapchain, sizeof(VK::UDOWorld3D_Vert)); + } + catch(const std::exception& e) + { + throw CommandError{e.what()}; + } +} + +void +unload_world_vert_uniform_buffer(void *obj) +{ + auto self = static_cast(obj); + + self->ub_world_vert.clear(); +} + +void +load_world_frag_uniform_buffer(void *obj) +{ + auto self = static_cast(obj); + + try + { + self->ub_world_frag.reserve(cg_core.vk_swapchain->images_count); + for(auto i{0}; i < cg_core.vk_swapchain->images_count; i++) + self->ub_world_frag.emplace_back( + cg_core.vk_device_with_swapchain, sizeof(VK::UDOWorld3D_Frag)); + } + catch(const std::exception& e) + { + throw CommandError{e.what()}; + } +} + +void +unload_world_frag_uniform_buffer(void *obj) +{ + auto self = static_cast(obj); + + self->ub_world_frag.clear(); +} + +void +load_descriptor_pool(void *obj) +{ + auto self = static_cast(obj); + + uint32_t uniform_buffers_count = + self->ub_world_vert.size() + self->ub_world_vert.size(); + + VkDescriptorPoolSize descriptor_pool_size{}; + descriptor_pool_size.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + descriptor_pool_size.descriptorCount = uniform_buffers_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 = uniform_buffers_count; + pool_info.poolSizeCount = 1; + pool_info.pPoolSizes = &descriptor_pool_size; + + if(vkCreateDescriptorPool( + cg_core.vk_device_with_swapchain->device, &pool_info, nullptr, + &self->descriptor_pool) != VK_SUCCESS) + throw CommandError{"Failed to create a Vulkan descriptor pool."}; +} + +void +unload_descriptor_pool(void *obj) +{ + auto self = static_cast(obj); + + vkDestroyDescriptorPool( + cg_core.vk_device_with_swapchain->device, self->descriptor_pool, + nullptr); +} + +void +load_descriptor_sets_world(void *obj) +{ + auto self = static_cast(obj); + + std::vector layouts( + cg_core.vk_swapchain->images_count, + cg_core.vk_descriptor_set_layout->world); + + 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_world.resize(layouts.size()); + if(vkAllocateDescriptorSets( + cg_core.vk_device_with_swapchain->device, &alloc_info, + self->descriptor_sets_world.data()) != VK_SUCCESS) + throw CommandError{"Failed to create Vulkan world descriptor set."}; +} + +void +load_resources_to_descriptor_sets(void *obj) +{ + auto self = static_cast(obj); + + for(auto i{0}; i < cg_core.vk_swapchain->images_count; i++) + { + VkDescriptorBufferInfo world_vert_info{}; + world_vert_info.buffer = self->ub_world_vert[i].buffer; + world_vert_info.offset = 0; + world_vert_info.range = sizeof(VK::UDOWorld3D_Vert); + + VkDescriptorBufferInfo world_frag_info{}; + world_frag_info.buffer = self->ub_world_frag[i].buffer; + world_frag_info.offset = 0; + world_frag_info.range = sizeof(VK::UDOWorld3D_Frag); + + std::array write_descriptors{}; + write_descriptors[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + write_descriptors[0].dstSet = self->descriptor_sets_world[i]; + write_descriptors[0].dstBinding = 0; + write_descriptors[0].dstArrayElement = 0; + write_descriptors[0].descriptorCount = 1; + write_descriptors[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + write_descriptors[0].pBufferInfo = &world_vert_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_world[i]; + write_descriptors[1].dstBinding = 1; + write_descriptors[1].dstArrayElement = 0; + write_descriptors[1].descriptorCount = 1; + write_descriptors[1].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + write_descriptors[1].pBufferInfo = &world_frag_info; + write_descriptors[1].pImageInfo = nullptr; + write_descriptors[1].pTexelBufferView = nullptr; + + vkUpdateDescriptorSets( + cg_core.vk_device_with_swapchain->device, write_descriptors.size(), + write_descriptors.data(), 0, nullptr); + } +} + +const CommandChain loader{ + {&load_world_vert_uniform_buffer, &unload_world_vert_uniform_buffer}, + {&load_world_frag_uniform_buffer, &unload_world_frag_uniform_buffer}, + {&load_descriptor_pool, &unload_descriptor_pool}, + // By destroying the pool the sets are also destroyed. + {&load_descriptor_sets_world, nullptr}, + {&load_resources_to_descriptor_sets, nullptr} +}; + +} + +namespace VK +{ + +Light::Light() +{ + loader.execute(this); +} + +Light::~Light() +{ + loader.revert(this); +} + +} diff --git a/src/vk/light.hpp b/src/vk/light.hpp new file mode 100644 index 0000000..80e44c7 --- /dev/null +++ b/src/vk/light.hpp @@ -0,0 +1,41 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CANDY_GEAR_VK_LIGHT_H +#define CANDY_GEAR_VK_LIGHT_H 1 + +#include "core.hpp" +#include "uniform_buffer.hpp" + +namespace VK +{ + +struct Light +{ + // FIXME: if this vector get resized, it will cause a segmentation fault! + std::vector ub_world_vert; + std::vector ub_world_frag; + + VkDescriptorPool descriptor_pool; + std::vector descriptor_sets_world; + + Light(); + ~Light(); +}; + +} + +#endif /* CANDY_GEAR_VK_LIGHT_H */ diff --git a/src/vk/renderer.cpp b/src/vk/renderer.cpp index 3daeee7..3497db9 100644 --- a/src/vk/renderer.cpp +++ b/src/vk/renderer.cpp @@ -212,7 +212,7 @@ Renderer::draw() UDOWorld3D_Vert ubo_world_3d_vert{}; ubo_world_3d_vert.ambient_light_color = glm::vec4{0.25, 0.25, 0.25, 1.0}; - cg_core.vk_graphics_pipeline_3d->ub_world_vert[image_index].copy_data( + cg_core.vk_light->ub_world_vert[image_index].copy_data( &ubo_world_3d_vert); UDOWorld3D_Frag ubo_world_3d_frag{}; @@ -220,7 +220,7 @@ Renderer::draw() glm::vec3{-0.57735, 0.57735, -0.57735}; ubo_world_3d_frag.directional_light_color = glm::vec4{0.8, 0.8, 0.8, 1.0}; - cg_core.vk_graphics_pipeline_3d->ub_world_frag[image_index].copy_data( + cg_core.vk_light->ub_world_frag[image_index].copy_data( &ubo_world_3d_frag); } -- cgit v1.2.3