diff options
author | Frederico Linhares <fred@linhares.blue> | 2022-12-21 14:46:09 -0300 |
---|---|---|
committer | Frederico Linhares <fred@linhares.blue> | 2022-12-23 11:08:59 -0300 |
commit | 1cd2d838bf1682e125d52d12ac6f2960df65c7e0 (patch) | |
tree | 6e2ae0881b826f94a99b90dc5478a96c85c6d043 /src/vk | |
parent | 993d033617eaeb582e0e5f0525fa51e1c83893da (diff) |
refa Merge Rectangle into Vector4D
Diffstat (limited to 'src/vk')
-rw-r--r-- | src/vk/graphics_pipeline_2d_solid.cpp | 5 | ||||
-rw-r--r-- | src/vk/graphics_pipeline_2d_solid_layout.hpp | 10 | ||||
-rw-r--r-- | src/vk/graphics_pipeline_2d_wired.cpp | 222 | ||||
-rw-r--r-- | src/vk/graphics_pipeline_2d_wired.hpp | 9 | ||||
-rw-r--r-- | src/vk/graphics_pipeline_2d_wired_layout.cpp | 10 | ||||
-rw-r--r-- | src/vk/graphics_pipeline_3d.cpp | 1 | ||||
-rw-r--r-- | src/vk/graphics_pipeline_3d_layout.hpp | 22 | ||||
-rw-r--r-- | src/vk/model.cpp | 1 | ||||
-rw-r--r-- | src/vk/rectangle.cpp | 223 | ||||
-rw-r--r-- | src/vk/rectangle.hpp | 32 | ||||
-rw-r--r-- | src/vk/sprite.cpp | 5 | ||||
-rw-r--r-- | src/vk/uniform_buffer_object.hpp | 64 | ||||
-rw-r--r-- | src/vk/view_2d.cpp | 1 | ||||
-rw-r--r-- | src/vk/view_2d.hpp | 2 | ||||
-rw-r--r-- | src/vk/view_3d.cpp | 1 |
15 files changed, 298 insertions, 310 deletions
diff --git a/src/vk/graphics_pipeline_2d_solid.cpp b/src/vk/graphics_pipeline_2d_solid.cpp index 3906cc7..b7caaa7 100644 --- a/src/vk/graphics_pipeline_2d_solid.cpp +++ b/src/vk/graphics_pipeline_2d_solid.cpp @@ -20,6 +20,7 @@ #include "../core.hpp" #include "sprite.hpp" +#include "uniform_buffer_object.hpp" namespace { @@ -319,9 +320,9 @@ GraphicsPipeline2DSolid::draw( draw_command_buffer, Sprite::vertex_count, positions.size(), 0, 0); } - VK::UBOSpritePositions ubo_sprite_positions; + UBOVectors4D ubo_sprite_positions; for(auto i{0}; i < positions.size(); i++) - ubo_sprite_positions.positions[i] = positions[i]; + ubo_sprite_positions.vectors[i] = positions[i]; sprite->ub_sprite_positions[image_index].copy_data( &ubo_sprite_positions); } diff --git a/src/vk/graphics_pipeline_2d_solid_layout.hpp b/src/vk/graphics_pipeline_2d_solid_layout.hpp index 121ddaa..b04d239 100644 --- a/src/vk/graphics_pipeline_2d_solid_layout.hpp +++ b/src/vk/graphics_pipeline_2d_solid_layout.hpp @@ -22,16 +22,6 @@ namespace VK { -struct UBOView2D -{ - glm::mat4 proj; -}; - -struct UBOSpritePositions -{ - glm::vec4 positions[128]; -}; - struct GraphicsPipeline2DSolidLayout { VkDescriptorSetLayout descriptor_set_view; diff --git a/src/vk/graphics_pipeline_2d_wired.cpp b/src/vk/graphics_pipeline_2d_wired.cpp index 2b416c2..34b6a1b 100644 --- a/src/vk/graphics_pipeline_2d_wired.cpp +++ b/src/vk/graphics_pipeline_2d_wired.cpp @@ -21,6 +21,7 @@ #include "../core.hpp" #include "rectangle.hpp" #include "sprite.hpp" +#include "uniform_buffer_object.hpp" namespace { @@ -97,27 +98,15 @@ load_pipeline(void *obj) frag_shader_stage_info }; - VkVertexInputBindingDescription vertex_input_binding{}; - vertex_input_binding.binding = 0; - vertex_input_binding.stride = sizeof(glm::vec2); - vertex_input_binding.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; - - std::array<VkVertexInputAttributeDescription, 1> vertex_attribute{}; - vertex_attribute[0].location = 0; - vertex_attribute[0].binding = 0; - vertex_attribute[0].format = VK_FORMAT_R32G32_SFLOAT; - vertex_attribute[0].offset = 0; - VkPipelineVertexInputStateCreateInfo vertex_input_info = {}; vertex_input_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; vertex_input_info.pNext = nullptr; vertex_input_info.flags = 0; - vertex_input_info.vertexBindingDescriptionCount = 1; - vertex_input_info.pVertexBindingDescriptions = &vertex_input_binding; - vertex_input_info.vertexAttributeDescriptionCount = - static_cast<uint32_t>(vertex_attribute.size()); - vertex_input_info.pVertexAttributeDescriptions = vertex_attribute.data(); + vertex_input_info.vertexBindingDescriptionCount = 0; + vertex_input_info.pVertexBindingDescriptions = nullptr; + vertex_input_info.vertexAttributeDescriptionCount = 0; + vertex_input_info.pVertexAttributeDescriptions = nullptr; VkPipelineInputAssemblyStateCreateInfo input_assembly = {}; input_assembly.sType = @@ -250,9 +239,182 @@ unload_pipeline(void *obj) cg_core.vk_device_with_swapchain->device, self->graphic_pipeline, nullptr); } +void +load_indexes(void *obj) +{ + auto self = static_cast<VK::GraphicsPipeline2DWired*>(obj); + + self->queue_family = + cg_core.vk_device_with_swapchain->get_queue_family_with_graphics(); + + std::array<uint32_t, 4> indexes{0, 1, 2, 3}; + void *indexes_data{indexes.data()}; + size_t indexes_size{sizeof(indexes[0]) * indexes.size()}; + VK::SourceBuffer source_index_buffer{ + self->queue_family->device, indexes_data, indexes_size}; + self->index_buffer = new VK::DestinationBuffer{ + self->queue_family, &source_index_buffer, + VK_BUFFER_USAGE_INDEX_BUFFER_BIT}; +} + +void +unload_indexes(void *obj) +{ + auto self = static_cast<VK::GraphicsPipeline2DWired*>(obj); + + delete self->index_buffer; +} + +void +load_uniform_rectangles_buffers(void *obj) +{ + auto self = static_cast<VK::GraphicsPipeline2DWired*>(obj); + + self->ub_rectangles.reserve( + cg_core.vk_swapchain->images_count); + for(auto i{0}; i < cg_core.vk_swapchain->images_count; i++) + self->ub_rectangles.emplace_back( + cg_core.vk_device_with_swapchain, sizeof(VK::UBOVectors4D)); +} + +void +unload_uniform_rectangles_buffers(void *obj) +{ + auto self = static_cast<VK::GraphicsPipeline2DWired*>(obj); + + self->ub_rectangles.clear(); +} + +void +load_uniform_rectangle_colors_buffers(void *obj) +{ + auto self = static_cast<VK::GraphicsPipeline2DWired*>(obj); + + self->ub_rectangle_colors.reserve( + cg_core.vk_swapchain->images_count); + for(auto i{0}; i < cg_core.vk_swapchain->images_count; i++) + self->ub_rectangle_colors.emplace_back( + cg_core.vk_device_with_swapchain, sizeof(VK::UBOVectors3D)); +} + +void +unload_uniform_rectangle_colors_buffers(void *obj) +{ + auto self = static_cast<VK::GraphicsPipeline2DWired*>(obj); + + self->ub_rectangle_colors.clear(); +} + +void +load_descriptor_set_pool(void *obj) +{ + auto self = static_cast<VK::GraphicsPipeline2DWired*>(obj); + + std::array<VkDescriptorPoolSize, 1> descriptor_pool_sizes{}; + descriptor_pool_sizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + descriptor_pool_sizes[0].descriptorCount = + self->ub_rectangles.size() + self->ub_rectangle_colors.size(); + + VkDescriptorPoolCreateInfo pool_info{}; + pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; + pool_info.pNext = nullptr; + pool_info.flags = 0; + pool_info.maxSets = + self->ub_rectangles.size() + self->ub_rectangle_colors.size(); + 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<VK::GraphicsPipeline2DWired*>(obj); + + vkDestroyDescriptorPool( + self->queue_family->device->device, self->descriptor_pool, nullptr); +} + +void +load_descriptor_sets(void *obj) +{ + auto self = static_cast<VK::GraphicsPipeline2DWired*>(obj); + + std::vector<VkDescriptorSetLayout> layouts( + cg_core.vk_swapchain->images_count, + cg_core.vk_graphics_pipeline_2d_wired_layout->descriptor_set_instance); + + 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<VK::GraphicsPipeline2DWired*>(obj); + + for(auto i{0}; i < self->ub_rectangles.size(); i++) + { + VkDescriptorBufferInfo rectangles_buffer_info{}; + rectangles_buffer_info.buffer = self->ub_rectangles[i].buffer; + rectangles_buffer_info.offset = 0; + rectangles_buffer_info.range = sizeof(VK::UBOVectors4D); + + VkDescriptorBufferInfo colors_buffer_info{}; + colors_buffer_info.buffer = self->ub_rectangle_colors[i].buffer; + colors_buffer_info.offset = 0; + colors_buffer_info.range = sizeof(VK::UBOVectors3D); + + std::array<VkWriteDescriptorSet, 2> 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_UNIFORM_BUFFER; + write_descriptors[0].pBufferInfo = &rectangles_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_UNIFORM_BUFFER; + write_descriptors[1].pBufferInfo = &colors_buffer_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_indexes, &unload_indexes}, {&load_framebuffer, &unload_framebuffer}, - {&load_pipeline, &unload_pipeline} + {&load_pipeline, &unload_pipeline}, + {&load_uniform_rectangles_buffers, &unload_uniform_rectangles_buffers}, + {&load_uniform_rectangle_colors_buffers, + &unload_uniform_rectangle_colors_buffers}, + {&load_descriptor_set_pool, &unload_descriptor_set_pool}, + {&load_descriptor_sets, nullptr}, + {&load_data_to_descriptor_sets, nullptr} }; } @@ -296,11 +458,10 @@ GraphicsPipeline2DWired::draw( } // Draw rectangles - for(auto& rectangle: view->rectangles_to_draw[current_frame]) { std::array<VkDescriptorSet, 2> vk_descriptor_sets{ view->descriptor_sets_2d[image_index], - rectangle->descriptor_sets[image_index]}; + this->descriptor_sets[image_index]}; VkDeviceSize offsets[]{0}; vkCmdBindDescriptorSets( @@ -310,11 +471,24 @@ GraphicsPipeline2DWired::draw( vkCmdBindPipeline( draw_command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, this->graphic_pipeline); - vkCmdBindVertexBuffers( - draw_command_buffer, 0, 1, - &rectangle->vertex_buffers[image_index].buffer, offsets); - vkCmdDraw( - draw_command_buffer, Rectangle::vertex_count, 1, 0, 0); + vkCmdBindIndexBuffer( + draw_command_buffer, this->index_buffer->buffer, 0, + VK_INDEX_TYPE_UINT32); + vkCmdDrawIndexed( + draw_command_buffer, Rectangle::VertexCount, + view->rectangles_to_draw[current_frame].size(), + 0, 0, 0); + + UBOVectors4D ubo_rectangle_positions; + UBOVectors3D ubo_rectangle_colors; + for(auto i{0}; i < view->rectangles_to_draw[current_frame].size(); i++) + { + auto &rect = view->rectangles_to_draw[current_frame][i]; + ubo_rectangle_positions.vectors[i] = rect.position; + ubo_rectangle_colors.vectors[i] = rect.color; + } + this->ub_rectangles[image_index].copy_data(&ubo_rectangle_positions); + this->ub_rectangle_colors[image_index].copy_data(&ubo_rectangle_colors); } // Prepare for the next frame. diff --git a/src/vk/graphics_pipeline_2d_wired.hpp b/src/vk/graphics_pipeline_2d_wired.hpp index e6c40de..aa975a8 100644 --- a/src/vk/graphics_pipeline_2d_wired.hpp +++ b/src/vk/graphics_pipeline_2d_wired.hpp @@ -29,9 +29,18 @@ namespace VK struct GraphicsPipeline2DWired { + QueueFamily *queue_family; + std::vector<VkFramebuffer> swapchain_framebuffers; VkPipeline graphic_pipeline; + DestinationBuffer *index_buffer; + std::vector<UniformBuffer> ub_rectangles; + std::vector<UniformBuffer> ub_rectangle_colors; + + VkDescriptorPool descriptor_pool; + std::vector<VkDescriptorSet> descriptor_sets; + GraphicsPipeline2DWired(); ~GraphicsPipeline2DWired(); diff --git a/src/vk/graphics_pipeline_2d_wired_layout.cpp b/src/vk/graphics_pipeline_2d_wired_layout.cpp index c523bc7..ae58a2f 100644 --- a/src/vk/graphics_pipeline_2d_wired_layout.cpp +++ b/src/vk/graphics_pipeline_2d_wired_layout.cpp @@ -66,14 +66,20 @@ load_descriptor_set_instance(void *obj) { auto self = static_cast<VK::GraphicsPipeline2DWiredLayout*>(obj); - std::array<VkDescriptorSetLayoutBinding, 1> layout_bindings{}; + std::array<VkDescriptorSetLayoutBinding, 2> layout_bindings{}; layout_bindings[0].binding = 0; 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; + layout_bindings[1].binding = 1; + layout_bindings[1].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + 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; layout_info.pNext = nullptr; diff --git a/src/vk/graphics_pipeline_3d.cpp b/src/vk/graphics_pipeline_3d.cpp index f08e43a..790cad7 100644 --- a/src/vk/graphics_pipeline_3d.cpp +++ b/src/vk/graphics_pipeline_3d.cpp @@ -23,6 +23,7 @@ #include "core.hpp" #include "image.hpp" #include "vertex_3d.hpp" +#include "uniform_buffer_object.hpp" namespace { diff --git a/src/vk/graphics_pipeline_3d_layout.hpp b/src/vk/graphics_pipeline_3d_layout.hpp index d45a778..10c8bd9 100644 --- a/src/vk/graphics_pipeline_3d_layout.hpp +++ b/src/vk/graphics_pipeline_3d_layout.hpp @@ -22,28 +22,6 @@ namespace VK { -struct UBOWorld3D_Vert -{ - glm::vec4 ambient_light_color; -}; - -struct UBOWorld3D_Frag -{ - glm::vec3 directional_light_direction; - glm::vec4 directional_light_color; -}; - -struct UBOView3D -{ - glm::mat4 view; - glm::mat4 proj; -}; - -struct UBOModelInstance -{ - glm::mat4 instances[128]; -}; - struct GraphicsPipeline3DLayout { VkDescriptorSetLayout descriptor_set_world; diff --git a/src/vk/model.cpp b/src/vk/model.cpp index d38342d..2aca963 100644 --- a/src/vk/model.cpp +++ b/src/vk/model.cpp @@ -19,6 +19,7 @@ #include <array> #include "../core.hpp" +#include "uniform_buffer_object.hpp" namespace { diff --git a/src/vk/rectangle.cpp b/src/vk/rectangle.cpp index b0925f1..dc18c45 100644 --- a/src/vk/rectangle.cpp +++ b/src/vk/rectangle.cpp @@ -16,230 +16,15 @@ #include "rectangle.hpp" -#include <array> - -#include "../core.hpp" -#include "graphics_pipeline_2d_wired_layout.hpp" - -namespace -{ - -void -load_mesh(void *obj) -{ - auto self = static_cast<VK::Rectangle*>(obj); - - self->queue_family = - cg_core.vk_device_with_swapchain->get_queue_family_with_graphics(); - - glm::vec2 rect[4]{ - glm::vec2{self->x, self->y}, - glm::vec2{self->x, self->y + self->height}, - glm::vec2{self->x + self->width, self->y}, - glm::vec2{self->x + self->width, self->y + self->height} - }; - - void *vertexes_data{&rect}; - static const size_t vertexes_size = - sizeof(glm::vec2) * VK::Rectangle::vertex_count; - self->source_buffers.reserve(cg_core.vk_swapchain->images_count); - self->vertex_buffers.reserve(cg_core.vk_swapchain->images_count); - for(int i{0}; i < cg_core.vk_swapchain->images_count; i++) - { - self->source_buffers.emplace_back( - self->queue_family->device, vertexes_data, vertexes_size); - self->vertex_buffers.emplace_back( - self->queue_family, &self->source_buffers[i], - VK_BUFFER_USAGE_VERTEX_BUFFER_BIT); - } -} - -void -unload_mesh(void *obj) -{ - auto self = static_cast<VK::Rectangle*>(obj); - - self->vertex_buffers.clear(); - self->source_buffers.clear(); -} - -void -load_uniform_buffer(void *obj) -{ - auto self = static_cast<VK::Rectangle*>(obj); - - self->ub_rectangle.reserve(cg_core.vk_swapchain->images_count); - for(auto i{0}; i < cg_core.vk_swapchain->images_count; i++) - self->ub_rectangle.emplace_back( - cg_core.vk_device_with_swapchain, sizeof(VK::UBOModel2D)); -} - -void -unload_uniform_buffer(void *obj) -{ - auto self = static_cast<VK::Rectangle*>(obj); - - self->ub_rectangle.clear(); -} - -void -load_descriptor_set_pool(void *obj) -{ - auto self = static_cast<VK::Rectangle*>(obj); - - std::array<VkDescriptorPoolSize, 1> descriptor_pool_sizes{}; - descriptor_pool_sizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - descriptor_pool_sizes[0].descriptorCount = self->ub_rectangle.size(); - - VkDescriptorPoolCreateInfo pool_info{}; - pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; - pool_info.pNext = nullptr; - pool_info.flags = 0; - pool_info.maxSets = self->ub_rectangle.size(); - 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<VK::Rectangle*>(obj); - - vkDestroyDescriptorPool( - self->queue_family->device->device, self->descriptor_pool, nullptr); -} - -void -load_descriptor_sets(void *obj) -{ - auto self = static_cast<VK::Rectangle*>(obj); - - std::vector<VkDescriptorSetLayout> layouts( - cg_core.vk_swapchain->images_count, - cg_core.vk_graphics_pipeline_2d_wired_layout->descriptor_set_instance); - - 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<VK::Rectangle*>(obj); - - for(auto i{0}; i < self->ub_rectangle.size(); i++) - { - VkDescriptorBufferInfo buffer_info{}; - buffer_info.buffer = self->ub_rectangle[i].buffer; - buffer_info.offset = 0; - buffer_info.range = sizeof(VK::UBOModel2D); - - std::array<VkWriteDescriptorSet, 1> 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_UNIFORM_BUFFER; - write_descriptors[0].pBufferInfo = &buffer_info; - write_descriptors[0].pImageInfo = nullptr; - 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_uniform_buffer, &unload_uniform_buffer}, - {&load_descriptor_set_pool, &unload_descriptor_set_pool}, - {&load_descriptor_sets, nullptr}, - {&load_data_to_descriptor_sets, nullptr}, -}; - -constexpr bool -_align_vertically(const float a_x, const float a_width, - const float b_x, const float b_width) -{ - return a_x <= b_x + b_width && a_x + a_width >= b_x; -} - -constexpr bool -_align_horizontally(const float a_y, const float a_height, - const float b_y, const float b_height) -{ - return a_y <= b_y + b_height && a_y + a_height >= b_y; -} - -} - namespace VK { -Rectangle::Rectangle(float x, float y, float width, float height): - x{x}, - y{y}, - width{width}, - height{height} -{ - loader.execute(this); -} +const int Rectangle::VertexCount{4}; -Rectangle::~Rectangle() +Rectangle::Rectangle(glm::vec4 position, glm::vec3 color): + position{position}, + color{color} { - loader.revert(this); -} - -bool -Rectangle::align_vertically(const Rectangle &that) const -{ - return _align_vertically(this->x, this->width, that.x, that.width); -} - -bool -Rectangle::align_horizontally(const Rectangle &that) const -{ - return _align_horizontally(this->y, this->height, that.y, that.height); -} - -bool -Rectangle::collide(const Rectangle &that) const -{ - return - _align_vertically(this->x, this->width, that.x, that.width) && - _align_horizontally(this->y, this->height, that.y, that.height); -} - -void -Rectangle::update_vertex_buffer() -{ - glm::vec2 rect[4]{ - glm::vec2{this->x, this->y}, - glm::vec2{this->x, this->y + this->height}, - glm::vec2{this->x + this->width, this->y}, - glm::vec2{this->x + this->width, this->y + this->height} - }; - - void *vertexes_data{&rect}; - this->source_buffers[cg_core.vk_swapchain->current_frame].copy_data( - vertexes_data); - this->vertex_buffers[cg_core.vk_swapchain->current_frame].copy_data(); } } diff --git a/src/vk/rectangle.hpp b/src/vk/rectangle.hpp index fc21135..faee927 100644 --- a/src/vk/rectangle.hpp +++ b/src/vk/rectangle.hpp @@ -29,36 +29,12 @@ namespace VK struct Rectangle { - static const uint32_t vertex_count{4}; + static const int VertexCount; - float x{0}; - float y{0}; - float width{1}; - float height{1}; + glm::vec4 position; + glm::vec3 color; - QueueFamily *queue_family; - - std::vector<SourceBuffer> source_buffers; - std::vector<DestinationBuffer> vertex_buffers; - - std::vector<UniformBuffer> ub_rectangle; - - VkDescriptorPool descriptor_pool; - std::vector<VkDescriptorSet> descriptor_sets; - - Rectangle() = default; - Rectangle(float x, float y, float width, float height); - ~Rectangle(); - - bool - align_vertically(const Rectangle &that) const; - bool - align_horizontally(const Rectangle &that) const; - bool - collide(const Rectangle &that) const; - - void - update_vertex_buffer(); + Rectangle(glm::vec4 position, glm::vec3 color); }; } diff --git a/src/vk/sprite.cpp b/src/vk/sprite.cpp index a7b802a..c2761f3 100644 --- a/src/vk/sprite.cpp +++ b/src/vk/sprite.cpp @@ -20,6 +20,7 @@ #include "../core.hpp" #include "sprite.hpp" +#include "uniform_buffer_object.hpp" namespace { @@ -81,7 +82,7 @@ load_uniform_buffers(void *obj) cg_core.vk_swapchain->images_count); for(auto i{0}; i < cg_core.vk_swapchain->images_count; i++) self->sprite->ub_sprite_positions.emplace_back( - cg_core.vk_device_with_swapchain, sizeof(VK::UBOSpritePositions)); + cg_core.vk_device_with_swapchain, sizeof(VK::UBOVectors4D)); } void @@ -162,7 +163,7 @@ load_buffers_to_descriptor_sets(void *obj) VkDescriptorBufferInfo buffer_info{}; buffer_info.buffer = self->sprite->ub_sprite_positions[i].buffer; buffer_info.offset = 0; - buffer_info.range = sizeof(VK::UBOSpritePositions); + buffer_info.range = sizeof(VK::UBOVectors4D); VkDescriptorImageInfo image_info{}; image_info.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; diff --git a/src/vk/uniform_buffer_object.hpp b/src/vk/uniform_buffer_object.hpp new file mode 100644 index 0000000..fa598ab --- /dev/null +++ b/src/vk/uniform_buffer_object.hpp @@ -0,0 +1,64 @@ +/* + * Copyright 2022 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_UNIFORM_BUFFER_OBJECT_H +#define CANDY_GEAR_VK_UNIFORM_BUFFER_OBJECT_H 1 + +#include "core.hpp" + +namespace VK +{ + +struct UBOView2D +{ + glm::mat4 proj; +}; + +struct UBOView3D +{ + glm::mat4 view; + glm::mat4 proj; +}; + +struct UBOWorld3D_Vert +{ + glm::vec4 ambient_light_color; +}; + +struct UBOWorld3D_Frag +{ + glm::vec3 directional_light_direction; + glm::vec4 directional_light_color; +}; + +struct UBOModelInstance +{ + glm::mat4 instances[128]; +}; + +struct UBOVectors4D +{ + glm::vec4 vectors[128]; +}; + +struct UBOVectors3D +{ + glm::vec3 vectors[128]; +}; + +} + +#endif /* CANDY_GEAR_VK_UNIFORM_BUFFER_OBJECT_H */ diff --git a/src/vk/view_2d.cpp b/src/vk/view_2d.cpp index 011fb1a..a80cc89 100644 --- a/src/vk/view_2d.cpp +++ b/src/vk/view_2d.cpp @@ -19,6 +19,7 @@ #include <array> #include "../core.hpp" +#include "uniform_buffer_object.hpp" namespace { diff --git a/src/vk/view_2d.hpp b/src/vk/view_2d.hpp index ec7225f..f49c3be 100644 --- a/src/vk/view_2d.hpp +++ b/src/vk/view_2d.hpp @@ -38,7 +38,7 @@ struct View2D VkDescriptorPool descriptor_pool; std::vector<VkDescriptorSet> descriptor_sets_2d; - std::vector<std::vector<std::shared_ptr<Rectangle>>> rectangles_to_draw; + std::vector<std::vector<Rectangle>> rectangles_to_draw; std::vector< std::unordered_map<std::shared_ptr<Sprite>, std::vector<glm::vec4>>> sprites_to_draw; diff --git a/src/vk/view_3d.cpp b/src/vk/view_3d.cpp index 9d124d9..39d1159 100644 --- a/src/vk/view_3d.cpp +++ b/src/vk/view_3d.cpp @@ -19,6 +19,7 @@ #include <array> #include "../core.hpp" +#include "uniform_buffer_object.hpp" namespace { |