summaryrefslogtreecommitdiff
path: root/src/vk
diff options
context:
space:
mode:
Diffstat (limited to 'src/vk')
-rw-r--r--src/vk/graphics_pipeline_3d.cpp162
-rw-r--r--src/vk/graphics_pipeline_3d.hpp9
-rw-r--r--src/vk/graphics_pipeline_3d_skeletal.cpp175
-rw-r--r--src/vk/graphics_pipeline_3d_skeletal.hpp10
-rw-r--r--src/vk/light.cpp205
-rw-r--r--src/vk/light.hpp41
-rw-r--r--src/vk/renderer.cpp4
7 files changed, 250 insertions, 356 deletions
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
@@ -29,160 +29,6 @@ namespace
{
void
-load_world_vert_uniform_buffer(void *obj)
-{
- auto self = static_cast<VK::GraphicsPipeline3D*>(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<VK::GraphicsPipeline3D*>(obj);
-
- self->ub_world_vert.clear();
-}
-
-void
-load_world_frag_uniform_buffer(void *obj)
-{
- auto self = static_cast<VK::GraphicsPipeline3D*>(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<VK::GraphicsPipeline3D*>(obj);
-
- self->ub_world_frag.clear();
-}
-
-void
-load_descriptor_pool(void *obj)
-{
- auto self = static_cast<VK::GraphicsPipeline3D*>(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<VK::GraphicsPipeline3D*>(obj);
-
- vkDestroyDescriptorPool(
- cg_core.vk_device_with_swapchain->device, self->descriptor_pool,
- nullptr);
-}
-
-void
-load_descriptor_sets_world(void *obj)
-{
- auto self = static_cast<VK::GraphicsPipeline3D*>(obj);
-
- std::vector<VkDescriptorSetLayout> 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<VK::GraphicsPipeline3D*>(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<VkWriteDescriptorSet, 2> 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)
{
auto self = static_cast<VK::GraphicsPipeline3D*>(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<VkDescriptorSet, 3> 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 <memory>
#include <unordered_map>
-#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<UniformBuffer> ub_world_vert;
- std::vector<UniformBuffer> ub_world_frag;
-
- VkDescriptorPool descriptor_pool;
- std::vector<VkDescriptorSet> descriptor_sets_world;
-
std::vector<VkFramebuffer> 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
@@ -29,160 +29,6 @@ namespace
{
void
-load_world_vert_uniform_buffer(void *obj)
-{
- auto self = static_cast<VK::GraphicsPipeline3DSkeletal*>(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<VK::GraphicsPipeline3DSkeletal*>(obj);
-
- self->ub_world_vert.clear();
-}
-
-void
-load_world_frag_uniform_buffer(void *obj)
-{
- auto self = static_cast<VK::GraphicsPipeline3DSkeletal*>(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<VK::GraphicsPipeline3DSkeletal*>(obj);
-
- self->ub_world_frag.clear();
-}
-
-void
-load_descriptor_pool(void *obj)
-{
- auto self = static_cast<VK::GraphicsPipeline3DSkeletal*>(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<VK::GraphicsPipeline3DSkeletal*>(obj);
-
- vkDestroyDescriptorPool(
- cg_core.vk_device_with_swapchain->device, self->descriptor_pool,
- nullptr);
-}
-
-void
-load_descriptor_sets_world(void *obj)
-{
- auto self = static_cast<VK::GraphicsPipeline3DSkeletal*>(obj);
-
- std::vector<VkDescriptorSetLayout> 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<VK::GraphicsPipeline3DSkeletal*>(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<VkWriteDescriptorSet, 2> 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)
{
auto self = static_cast<VK::GraphicsPipeline3DSkeletal*>(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<VkDescriptorSet, 3> 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 <memory>
#include <unordered_map>
-#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<UniformBuffer> ub_world_vert;
- std::vector<UniformBuffer> ub_world_frag;
-
- VkDescriptorPool descriptor_pool;
- std::vector<VkDescriptorSet> descriptor_sets_world;
-
std::vector<VkFramebuffer> 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 <array>
+
+#include "../core.hpp"
+#include "uniform_data_object.hpp"
+
+namespace
+{
+
+void
+load_world_vert_uniform_buffer(void *obj)
+{
+ auto self = static_cast<VK::Light*>(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<VK::Light*>(obj);
+
+ self->ub_world_vert.clear();
+}
+
+void
+load_world_frag_uniform_buffer(void *obj)
+{
+ auto self = static_cast<VK::Light*>(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<VK::Light*>(obj);
+
+ self->ub_world_frag.clear();
+}
+
+void
+load_descriptor_pool(void *obj)
+{
+ auto self = static_cast<VK::Light*>(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<VK::Light*>(obj);
+
+ vkDestroyDescriptorPool(
+ cg_core.vk_device_with_swapchain->device, self->descriptor_pool,
+ nullptr);
+}
+
+void
+load_descriptor_sets_world(void *obj)
+{
+ auto self = static_cast<VK::Light*>(obj);
+
+ std::vector<VkDescriptorSetLayout> 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<VK::Light*>(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<VkWriteDescriptorSet, 2> 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<UniformBuffer> ub_world_vert;
+ std::vector<UniformBuffer> ub_world_frag;
+
+ VkDescriptorPool descriptor_pool;
+ std::vector<VkDescriptorSet> 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);
}