From 8bedf8a366cb6c1179bc89678c863517b9356d48 Mon Sep 17 00:00:00 2001 From: Frederico Linhares Date: Sat, 21 Jun 2025 16:51:22 -0300 Subject: refa Remove View2D View2D is almost useless and add too much complexity for the engine, so I am removing it. --- src/blu_cat/gra/graphics_pipeline_2d_solid.cpp | 36 ++--- src/blu_cat/gra/graphics_pipeline_2d_solid.hpp | 8 +- src/blu_cat/gra/graphics_pipeline_2d_wired.cpp | 37 ++--- src/blu_cat/gra/graphics_pipeline_2d_wired.hpp | 10 +- src/blu_cat/gra/graphics_pipeline_3d.cpp | 6 +- src/blu_cat/gra/graphics_pipeline_3d.hpp | 6 +- src/blu_cat/gra/graphics_pipeline_3d_skeletal.cpp | 6 +- src/blu_cat/gra/graphics_pipeline_3d_skeletal.hpp | 6 +- src/blu_cat/gra/graphics_pipeline_sprite_3d.cpp | 6 +- src/blu_cat/gra/graphics_pipeline_sprite_3d.hpp | 6 +- src/blu_cat/gra/renderer.cpp | 163 ++++++++++++++++------ src/blu_cat/gra/renderer.hpp | 28 ++-- src/blu_cat/gra/texture.cpp | 6 +- src/blu_cat/gra/view.cpp | 158 +++++++++++++++++++++ src/blu_cat/gra/view.hpp | 55 ++++++++ src/blu_cat/gra/view_2d.cpp | 160 --------------------- src/blu_cat/gra/view_2d.hpp | 60 -------- src/blu_cat/gra/view_3d.cpp | 155 -------------------- src/blu_cat/gra/view_3d.hpp | 48 ------- src/blu_cat/int/core.cpp | 10 +- 20 files changed, 422 insertions(+), 548 deletions(-) create mode 100644 src/blu_cat/gra/view.cpp create mode 100644 src/blu_cat/gra/view.hpp delete mode 100644 src/blu_cat/gra/view_2d.cpp delete mode 100644 src/blu_cat/gra/view_2d.hpp delete mode 100644 src/blu_cat/gra/view_3d.cpp delete mode 100644 src/blu_cat/gra/view_3d.hpp (limited to 'src/blu_cat') diff --git a/src/blu_cat/gra/graphics_pipeline_2d_solid.cpp b/src/blu_cat/gra/graphics_pipeline_2d_solid.cpp index 7acca09..05dcbe1 100644 --- a/src/blu_cat/gra/graphics_pipeline_2d_solid.cpp +++ b/src/blu_cat/gra/graphics_pipeline_2d_solid.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 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. @@ -235,26 +235,25 @@ GraphicsPipeline2DSolid::~GraphicsPipeline2DSolid() void GraphicsPipeline2DSolid::draw( - std::shared_ptr view, const VkCommandBuffer draw_command_buffer, - const size_t current_frame, const size_t next_frame, - const uint32_t image_index) + const VkCommandBuffer draw_command_buffer, const size_t current_frame, + const size_t next_frame, const uint32_t image_index) { // TODO set viewport just once per view, not once per pipeline. { // Set viewport VkViewport vk_viewport{}; - vk_viewport.x = view->region.x; - vk_viewport.y = view->region.y; - vk_viewport.width = view->region.z; - vk_viewport.height = view->region.w; + vk_viewport.x = 0; + vk_viewport.y = 0; + vk_viewport.width = static_cast(BluCat::INT::core.display_width); + vk_viewport.height = static_cast(BluCat::INT::core.display_height); vk_viewport.minDepth = 0.0f; vk_viewport.maxDepth = 1.0f; vkCmdSetViewport(draw_command_buffer, 0, 1, &vk_viewport); VkRect2D vk_scissor{}; - vk_scissor.offset.x = static_cast(view->region.x); - vk_scissor.offset.y = static_cast(view->region.y); - vk_scissor.extent.width = static_cast(view->region.z); - vk_scissor.extent.height = static_cast(view->region.w); + vk_scissor.offset.x = 0; + vk_scissor.offset.y = 0; + vk_scissor.extent.width = BluCat::INT::core.display_width; + vk_scissor.extent.height = BluCat::INT::core.display_height; vkCmdSetScissor(draw_command_buffer, 0, 1, &vk_scissor); } @@ -264,14 +263,17 @@ GraphicsPipeline2DSolid::draw( // FIXME: I know sorting is expensive, but I need to figure out better what // to do here. - std::sort(view->sprites_to_draw[current_frame].begin(), - view->sprites_to_draw[current_frame].end()); + std::sort(BluCat::INT::core.vk_renderer->sprites_to_draw[ + current_frame].begin(), + BluCat::INT::core.vk_renderer->sprites_to_draw[ + current_frame].end()); // Draw sprites - for(auto& sprite_to_draw: view->sprites_to_draw[current_frame]) + for(auto& sprite_to_draw: + BluCat::INT::core.vk_renderer->sprites_to_draw[current_frame]) { std::array vk_descriptor_sets{ - view->descriptor_sets_2d[image_index], + BluCat::INT::core.vk_renderer->descriptor_sets_2d[image_index], sprite_to_draw.sprite->texture->descriptor_sets[image_index]}; VkDeviceSize offsets[]{0}; @@ -292,7 +294,7 @@ GraphicsPipeline2DSolid::draw( } // Prepare for the next frame. - view->sprites_to_draw[next_frame].clear(); + BluCat::INT::core.vk_renderer->sprites_to_draw[next_frame].clear(); } } diff --git a/src/blu_cat/gra/graphics_pipeline_2d_solid.hpp b/src/blu_cat/gra/graphics_pipeline_2d_solid.hpp index c1520c2..c2d85d5 100644 --- a/src/blu_cat/gra/graphics_pipeline_2d_solid.hpp +++ b/src/blu_cat/gra/graphics_pipeline_2d_solid.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 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. @@ -21,7 +21,6 @@ #include "vulkan.hpp" #include "command_pool.hpp" -#include "view_2d.hpp" namespace BluCat::GRA { @@ -34,9 +33,8 @@ struct GraphicsPipeline2DSolid ~GraphicsPipeline2DSolid(); void - draw(std::shared_ptr view, const VkCommandBuffer draw_command_buffer, - const size_t current_frame, const size_t next_frame, - const uint32_t image_index); + draw(const VkCommandBuffer draw_command_buffer, const size_t current_frame, + const size_t next_frame, const uint32_t image_index); }; } diff --git a/src/blu_cat/gra/graphics_pipeline_2d_wired.cpp b/src/blu_cat/gra/graphics_pipeline_2d_wired.cpp index 20491ed..1273c67 100644 --- a/src/blu_cat/gra/graphics_pipeline_2d_wired.cpp +++ b/src/blu_cat/gra/graphics_pipeline_2d_wired.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 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. @@ -247,33 +247,32 @@ GraphicsPipeline2DWired::~GraphicsPipeline2DWired() void GraphicsPipeline2DWired::draw( - std::shared_ptr view, const VkCommandBuffer draw_command_buffer, - const size_t current_frame, const size_t next_frame, - const uint32_t image_index) + const VkCommandBuffer draw_command_buffer, const size_t current_frame, + const size_t next_frame, const uint32_t image_index) { - // Set viewport - { + // TODO set viewport just once per view, not once per pipeline. + { // Set viewport VkViewport vk_viewport{}; - vk_viewport.x = view->region.x; - vk_viewport.y = view->region.y; - vk_viewport.width = view->region.z; - vk_viewport.height = view->region.w; + vk_viewport.x = 0; + vk_viewport.y = 0; + vk_viewport.width = static_cast(BluCat::INT::core.display_width); + vk_viewport.height = static_cast(BluCat::INT::core.display_height); vk_viewport.minDepth = 0.0f; vk_viewport.maxDepth = 1.0f; vkCmdSetViewport(draw_command_buffer, 0, 1, &vk_viewport); VkRect2D vk_scissor{}; - vk_scissor.offset.x = static_cast(view->region.x); - vk_scissor.offset.y = static_cast(view->region.y); - vk_scissor.extent.width = static_cast(view->region.z); - vk_scissor.extent.height = static_cast(view->region.w); + vk_scissor.offset.x = 0; + vk_scissor.offset.y = 0; + vk_scissor.extent.width = BluCat::INT::core.display_width; + vk_scissor.extent.height = BluCat::INT::core.display_height; vkCmdSetScissor(draw_command_buffer, 0, 1, &vk_scissor); } // Draw rectangles { std::array vk_descriptor_sets{ - view->descriptor_sets_2d[image_index]}; + BluCat::INT::core.vk_renderer->descriptor_sets_2d[image_index]}; VkDeviceSize offsets[]{0}; vkCmdBindDescriptorSets( @@ -287,9 +286,11 @@ GraphicsPipeline2DWired::draw( draw_command_buffer, this->index_buffer->buffer, 0, VK_INDEX_TYPE_UINT32); - for(auto i{0}; i < view->rectangles_to_draw[current_frame].size(); i++) + for(auto i{0}; i < BluCat::INT::core.vk_renderer->rectangles_to_draw[ + current_frame].size(); i++) { - auto &rect{view->rectangles_to_draw[current_frame][i]}; + auto &rect{BluCat::INT::core.vk_renderer->rectangles_to_draw[ + current_frame][i]}; UDOVector4D position{rect.position}; UDOVector3D color{rect.color}; @@ -308,7 +309,7 @@ GraphicsPipeline2DWired::draw( } // Prepare for the next frame. - view->rectangles_to_draw[next_frame].clear(); + BluCat::INT::core.vk_renderer->rectangles_to_draw[next_frame].clear(); } } diff --git a/src/blu_cat/gra/graphics_pipeline_2d_wired.hpp b/src/blu_cat/gra/graphics_pipeline_2d_wired.hpp index 7d2752e..9fc46c4 100644 --- a/src/blu_cat/gra/graphics_pipeline_2d_wired.hpp +++ b/src/blu_cat/gra/graphics_pipeline_2d_wired.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 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. @@ -20,7 +20,8 @@ #include #include "vulkan.hpp" -#include "view_2d.hpp" +#include "command_pool.hpp" +#include "destination_buffer.hpp" namespace BluCat::GRA { @@ -37,9 +38,8 @@ struct GraphicsPipeline2DWired ~GraphicsPipeline2DWired(); void - draw(std::shared_ptr view, const VkCommandBuffer draw_command_buffer, - const size_t current_frame, const size_t next_frame, - const uint32_t image_index); + draw(const VkCommandBuffer draw_command_buffer, const size_t current_frame, + const size_t next_frame, const uint32_t image_index); }; } diff --git a/src/blu_cat/gra/graphics_pipeline_3d.cpp b/src/blu_cat/gra/graphics_pipeline_3d.cpp index 1f7324a..288e186 100644 --- a/src/blu_cat/gra/graphics_pipeline_3d.cpp +++ b/src/blu_cat/gra/graphics_pipeline_3d.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 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. @@ -257,7 +257,7 @@ GraphicsPipeline3D::~GraphicsPipeline3D() void GraphicsPipeline3D::draw( - std::shared_ptr view, const VkCommandBuffer draw_command_buffer, + std::shared_ptr view, const VkCommandBuffer draw_command_buffer, const size_t current_frame, const uint32_t image_index) { vkCmdBindPipeline( @@ -286,7 +286,7 @@ GraphicsPipeline3D::draw( std::array vk_descriptor_sets{ INT::core.vk_light->descriptor_sets_world[image_index], - view->descriptor_sets_3d[image_index], + view->descriptor_sets[image_index], instance->descriptor_sets[image_index], instance->texture->descriptor_sets[image_index]}; diff --git a/src/blu_cat/gra/graphics_pipeline_3d.hpp b/src/blu_cat/gra/graphics_pipeline_3d.hpp index 0dc0b03..2e30e02 100644 --- a/src/blu_cat/gra/graphics_pipeline_3d.hpp +++ b/src/blu_cat/gra/graphics_pipeline_3d.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 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. @@ -21,7 +21,7 @@ #include "vulkan.hpp" #include "command_pool.hpp" -#include "view_3d.hpp" +#include "view.hpp" namespace BluCat::GRA { @@ -34,7 +34,7 @@ struct GraphicsPipeline3D ~GraphicsPipeline3D(); void - draw(std::shared_ptr view, const VkCommandBuffer draw_command_buffer, + draw(std::shared_ptr view, const VkCommandBuffer draw_command_buffer, const size_t current_frame, const uint32_t image_index); }; diff --git a/src/blu_cat/gra/graphics_pipeline_3d_skeletal.cpp b/src/blu_cat/gra/graphics_pipeline_3d_skeletal.cpp index e8eda6b..79a85f1 100644 --- a/src/blu_cat/gra/graphics_pipeline_3d_skeletal.cpp +++ b/src/blu_cat/gra/graphics_pipeline_3d_skeletal.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 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. @@ -266,7 +266,7 @@ GraphicsPipeline3DSkeletal::~GraphicsPipeline3DSkeletal() void GraphicsPipeline3DSkeletal::draw( - std::shared_ptr view, const VkCommandBuffer draw_command_buffer, + std::shared_ptr view, const VkCommandBuffer draw_command_buffer, const size_t current_frame, const uint32_t image_index) { vkCmdBindPipeline( @@ -295,7 +295,7 @@ GraphicsPipeline3DSkeletal::draw( std::array vk_descriptor_sets{ INT::core.vk_light->descriptor_sets_world[image_index], - view->descriptor_sets_3d[image_index], + view->descriptor_sets[image_index], instance->descriptor_sets[image_index], instance->texture->descriptor_sets[image_index]}; diff --git a/src/blu_cat/gra/graphics_pipeline_3d_skeletal.hpp b/src/blu_cat/gra/graphics_pipeline_3d_skeletal.hpp index bad117d..16d65b0 100644 --- a/src/blu_cat/gra/graphics_pipeline_3d_skeletal.hpp +++ b/src/blu_cat/gra/graphics_pipeline_3d_skeletal.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 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. @@ -21,7 +21,7 @@ #include "vulkan.hpp" #include "command_pool.hpp" -#include "view_3d.hpp" +#include "view.hpp" namespace BluCat::GRA { @@ -34,7 +34,7 @@ struct GraphicsPipeline3DSkeletal ~GraphicsPipeline3DSkeletal(); void - draw(std::shared_ptr view, const VkCommandBuffer draw_command_buffer, + draw(std::shared_ptr view, const VkCommandBuffer draw_command_buffer, const size_t current_frame, const uint32_t image_index); }; diff --git a/src/blu_cat/gra/graphics_pipeline_sprite_3d.cpp b/src/blu_cat/gra/graphics_pipeline_sprite_3d.cpp index 14796d3..63e10b9 100644 --- a/src/blu_cat/gra/graphics_pipeline_sprite_3d.cpp +++ b/src/blu_cat/gra/graphics_pipeline_sprite_3d.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 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. @@ -265,7 +265,7 @@ GraphicsPipelineSprite3D::~GraphicsPipelineSprite3D() void GraphicsPipelineSprite3D::draw( - std::shared_ptr view, const VkCommandBuffer draw_command_buffer, + std::shared_ptr view, const VkCommandBuffer draw_command_buffer, const size_t current_frame, const uint32_t image_index) { vkCmdBindPipeline( @@ -290,7 +290,7 @@ GraphicsPipelineSprite3D::draw( { std::array vk_descriptor_sets{ INT::core.vk_light->descriptor_sets_world[image_index], - view->descriptor_sets_3d[image_index], + view->descriptor_sets[image_index], sprite.sprite_3d->descriptor_sets[image_index], sprite.sprite_3d->sprite->texture->descriptor_sets[image_index]}; VkDeviceSize offsets[]{0}; diff --git a/src/blu_cat/gra/graphics_pipeline_sprite_3d.hpp b/src/blu_cat/gra/graphics_pipeline_sprite_3d.hpp index 325d943..d03b8e8 100644 --- a/src/blu_cat/gra/graphics_pipeline_sprite_3d.hpp +++ b/src/blu_cat/gra/graphics_pipeline_sprite_3d.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 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. @@ -22,7 +22,7 @@ #include "vulkan.hpp" #include "command_pool.hpp" #include "sprite_3d.hpp" -#include "view_3d.hpp" +#include "view.hpp" namespace BluCat::GRA { @@ -35,7 +35,7 @@ struct GraphicsPipelineSprite3D ~GraphicsPipelineSprite3D(); void - draw(std::shared_ptr view, const VkCommandBuffer draw_command_buffer, + draw(std::shared_ptr view, const VkCommandBuffer draw_command_buffer, const size_t current_frame, const uint32_t image_index); }; diff --git a/src/blu_cat/gra/renderer.cpp b/src/blu_cat/gra/renderer.cpp index d13f814..36b9925 100644 --- a/src/blu_cat/gra/renderer.cpp +++ b/src/blu_cat/gra/renderer.cpp @@ -24,16 +24,40 @@ namespace { +void +load_2d_uniform_buffer(void *obj) +{ + auto self = static_cast(obj); + + try + { + self->ub_2d.reserve(BluCat::INT::core.vk_swapchain->images_count); + for(auto i{0}; i < BluCat::INT::core.vk_swapchain->images_count; i++) + self->ub_2d.emplace_back( + BluCat::INT::core.vk_device_with_swapchain, + sizeof(BluCat::GRA::UDOView2D)); + } + catch(const std::exception& e) + { + throw CommandError{e.what()}; + } +} + +void +unload_2d_uniform_buffer(void *obj) +{ + auto self = static_cast(obj); + + self->ub_2d.clear(); +} + void load_descriptor_pool(void *obj) { auto self = static_cast(obj); - uint32_t uniform_buffer_count = 0; - for(auto &view : self->views_3d) - uniform_buffer_count += (view->ub_3d.size() + view->ub_2d.size()); - for(auto &view : self->views_2d) - uniform_buffer_count += (view->ub_2d.size()); + uint32_t uniform_buffer_count = self->ub_2d.size(); + for(auto &view : self->views) uniform_buffer_count += view->ub_3d.size(); VkDescriptorPoolSize descriptor_pool_size{}; descriptor_pool_size.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; @@ -52,9 +76,7 @@ load_descriptor_pool(void *obj) &self->descriptor_pool) != VK_SUCCESS) throw CommandError{"Failed to create a Vulkan descriptor pool."}; - for(auto &view : self->views_3d) - view->load_descriptor_sets(self->descriptor_pool); - for(auto &view : self->views_2d) + for(auto &view : self->views) view->load_descriptor_sets(self->descriptor_pool); } @@ -63,14 +85,71 @@ unload_descriptor_pool(void *obj) { auto self = static_cast(obj); - for(auto &view : self->views_3d) view->unload_descriptor_sets(); - for(auto &view : self->views_2d) view->unload_descriptor_sets(); + for(auto &view : self->views) view->unload_descriptor_sets(); vkDestroyDescriptorPool( BluCat::INT::core.vk_device_with_swapchain->device, self->descriptor_pool, nullptr); } +void +load_descriptor_sets_2d(void *obj) +{ + auto self = static_cast(obj); + + std::vector layouts( + BluCat::INT::core.vk_swapchain->images_count, + BluCat::INT::core.vk_descriptor_set_layout->view); + + 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_2d.resize(layouts.size()); + if(vkAllocateDescriptorSets( + BluCat::INT::core.vk_device_with_swapchain->device, &alloc_info, + self->descriptor_sets_2d.data()) != VK_SUCCESS) + throw CommandError{"Failed to create Vulkan descriptor sets for view."}; +} + +void +load_resources_to_descriptor_sets_2d(void *obj) +{ + auto self = static_cast(obj); + + for(auto i{0}; i < self->ub_2d.size(); i++) + { + VkDescriptorBufferInfo view_2d_info{}; + view_2d_info.buffer = self->ub_2d[i].buffer; + view_2d_info.offset = 0; + view_2d_info.range = sizeof(BluCat::GRA::UDOView2D); + + std::array write_descriptors{}; + write_descriptors[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + write_descriptors[0].dstSet = self->descriptor_sets_2d[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 = &view_2d_info; + write_descriptors[0].pImageInfo = nullptr; + write_descriptors[0].pTexelBufferView = nullptr; + + vkUpdateDescriptorSets( + BluCat::INT::core.vk_device_with_swapchain->device, write_descriptors.size(), + write_descriptors.data(), 0, nullptr); + + BluCat::GRA::UDOView2D ubo_view_2d; + ubo_view_2d.proj = glm::ortho( + 0.0f, self->projection_width, + 0.0f, self->projection_height, + 0.0f, 100.0f); + self->ub_2d[i].copy_data(&ubo_view_2d); + } +} + void load_queue_family(void *obj) { @@ -128,7 +207,10 @@ load_draw_command_buffer(void *obj) } const CommandChain loader{ + {&load_2d_uniform_buffer, &unload_2d_uniform_buffer}, {&load_descriptor_pool, &unload_descriptor_pool}, + {&load_descriptor_sets_2d, nullptr}, + {&load_resources_to_descriptor_sets_2d, nullptr}, {&load_queue_family, nullptr}, {&load_command_pool, &unload_command_pool}, {&load_draw_command_buffer, nullptr} @@ -139,21 +221,25 @@ const CommandChain loader{ namespace BluCat::GRA { -Renderer::Renderer(std::vector> views_2d, - std::vector> views_3d): +Renderer::Renderer( + std::vector> views, F32 width, F32 height): skeletal_models_to_draw{BluCat::INT::core.vk_swapchain->images_count}, static_models_to_draw{BluCat::INT::core.vk_swapchain->images_count}, sprites_3d_to_draw{BluCat::INT::core.vk_swapchain->images_count}, + rectangles_to_draw{BluCat::INT::core.vk_swapchain->images_count}, + sprites_to_draw{BluCat::INT::core.vk_swapchain->images_count}, + projection_width{width}, + projection_height{height}, clear_screen_color{0.0f, 0.0f, 0.0f, 1.0f}, - views_2d{views_2d}, - views_3d{views_3d} + views{views} { loader.execute(this); } -Renderer::Renderer(std::initializer_list> views_2d, - std::initializer_list> views_3d): - Renderer(std::vector(views_2d), std::vector(views_3d)) +Renderer::Renderer( + std::initializer_list> views, + F32 width, F32 height): + Renderer(std::vector(views), width, height) { } @@ -251,7 +337,7 @@ Renderer::draw() draw_command_buffer, &render_pass_begin, VK_SUBPASS_CONTENTS_INLINE); } - for(auto &view: this->views_3d) + for(auto &view: this->views) { { // Set viewport VkViewport vk_viewport{}; @@ -272,16 +358,17 @@ Renderer::draw() } BluCat::INT::core.vk_graphics_pipeline_3d->draw( - view, draw_command_buffer, BluCat::INT::core.vk_swapchain->current_frame, + view, draw_command_buffer, + BluCat::INT::core.vk_swapchain->current_frame, image_index); BluCat::INT::core.vk_graphics_pipeline_sprite_3d->draw( - view, draw_command_buffer, BluCat::INT::core.vk_swapchain->current_frame, - image_index); + view, draw_command_buffer, + BluCat::INT::core.vk_swapchain->current_frame, image_index); BluCat::INT::core.vk_graphics_pipeline_3d_skeletal->draw( - view, draw_command_buffer, BluCat::INT::core.vk_swapchain->current_frame, - image_index); + view, draw_command_buffer, + BluCat::INT::core.vk_swapchain->current_frame, image_index); { // Update view uniform buffers BluCat::GRA::UDOView3D ubo_view_3d{}; @@ -325,27 +412,15 @@ Renderer::draw() } { // 2D solid drawing - for(auto &view: this->views_2d) - BluCat::INT::core.vk_graphics_pipeline_2d_solid->draw( - view, draw_command_buffer, BluCat::INT::core.vk_swapchain->current_frame, - next_frame, image_index); - - for(auto &view: this->views_3d) - BluCat::INT::core.vk_graphics_pipeline_2d_solid->draw( - view, draw_command_buffer, BluCat::INT::core.vk_swapchain->current_frame, - next_frame, image_index); + BluCat::INT::core.vk_graphics_pipeline_2d_solid->draw( + draw_command_buffer, BluCat::INT::core.vk_swapchain->current_frame, + next_frame, image_index); } { // 2D wired drawing - for(auto &view: this->views_2d) - BluCat::INT::core.vk_graphics_pipeline_2d_wired->draw( - view, draw_command_buffer, BluCat::INT::core.vk_swapchain->current_frame, - next_frame, image_index); - - for(auto &view: this->views_3d) - BluCat::INT::core.vk_graphics_pipeline_2d_wired->draw( - view, draw_command_buffer, BluCat::INT::core.vk_swapchain->current_frame, - next_frame, image_index); + BluCat::INT::core.vk_graphics_pipeline_2d_wired->draw( + draw_command_buffer, BluCat::INT::core.vk_swapchain->current_frame, + next_frame, image_index); } vkCmdEndRenderPass(draw_command_buffer); @@ -415,10 +490,8 @@ Renderer::draw() BluCat::INT::core.vk_swapchain->current_frame].clear(); this->sprites_3d_to_draw[ BluCat::INT::core.vk_swapchain->current_frame].clear(); - for(auto &view: this->views_2d) - view->sprites_to_draw[BluCat::INT::core.vk_swapchain->current_frame].clear(); - for(auto &view: this->views_3d) - view->sprites_to_draw[BluCat::INT::core.vk_swapchain->current_frame].clear(); + this->sprites_to_draw[ + BluCat::INT::core.vk_swapchain->current_frame].clear(); } } diff --git a/src/blu_cat/gra/renderer.hpp b/src/blu_cat/gra/renderer.hpp index 82948cb..58b9972 100644 --- a/src/blu_cat/gra/renderer.hpp +++ b/src/blu_cat/gra/renderer.hpp @@ -22,14 +22,15 @@ #include #include "vulkan.hpp" +#include "queue_family.hpp" +#include "rectangle.hpp" #include "skeletal_mesh.hpp" #include "skeletal_model.hpp" #include "sprite_3d.hpp" +#include "sprite_to_draw.hpp" #include "static_mesh.hpp" #include "static_model.hpp" -#include "queue_family.hpp" -#include "view_2d.hpp" -#include "view_3d.hpp" +#include "view.hpp" namespace BluCat::GRA { @@ -52,16 +53,25 @@ struct Renderer VkDescriptorPool descriptor_pool; VkClearColorValue clear_screen_color; - std::vector> views_2d; - std::vector> views_3d; + + float projection_width, projection_height; + + // FIXME: if these vectors get resized, they can cause a segmentation fault! + std::vector ub_2d; + std::vector descriptor_sets_2d; + + std::vector> rectangles_to_draw; + std::vector> sprites_to_draw; + + std::vector> views; + QueueFamily *queue_family; VkCommandPool command_pool; std::vector draw_command_buffers; - Renderer(std::vector> views_2d, - std::vector> views_3d); - Renderer(std::initializer_list> views_2d, - std::initializer_list> views_3d); + Renderer(std::vector> views, F32 width, F32 height); + Renderer(std::initializer_list> views, + F32 width, F32 height); ~Renderer(); void diff --git a/src/blu_cat/gra/texture.cpp b/src/blu_cat/gra/texture.cpp index 5abf751..7546d56 100644 --- a/src/blu_cat/gra/texture.cpp +++ b/src/blu_cat/gra/texture.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 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. @@ -173,8 +173,8 @@ load_sampler(void *obj) sampler_info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; sampler_info.pNext = nullptr; sampler_info.flags = 0; - sampler_info.magFilter = VK_FILTER_LINEAR; - sampler_info.minFilter = VK_FILTER_LINEAR; + sampler_info.magFilter = VK_FILTER_NEAREST; + sampler_info.minFilter = VK_FILTER_NEAREST; sampler_info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR; sampler_info.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT; sampler_info.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT; diff --git a/src/blu_cat/gra/view.cpp b/src/blu_cat/gra/view.cpp new file mode 100644 index 0000000..26017ce --- /dev/null +++ b/src/blu_cat/gra/view.cpp @@ -0,0 +1,158 @@ +/* + * Copyright 2022-2025 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 "view.hpp" + +#include + +#include "../int/core.hpp" +#include "uniform_data_object.hpp" + +#include + +namespace +{ + +void +load_uniform_buffer(void *obj) +{ + auto self = static_cast(obj); + + try + { + self->ub_3d.reserve(BluCat::INT::core.vk_swapchain->images_count); + for(auto i{0}; i < BluCat::INT::core.vk_swapchain->images_count; i++) + self->ub_3d.emplace_back( + BluCat::INT::core.vk_device_with_swapchain, + sizeof(BluCat::GRA::UDOView3D)); + } + catch(const std::exception& e) + { + throw CommandError{e.what()}; + } +} + +void +unload_uniform_buffer(void *obj) +{ + auto self = static_cast(obj); + + self->ub_3d.clear(); +} + +void +load_descriptor_sets(void *obj) +{ + auto self = static_cast(obj); + + std::vector layouts( + BluCat::INT::core.vk_swapchain->images_count, + BluCat::INT::core.vk_descriptor_set_layout->view); + + 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( + BluCat::INT::core.vk_device_with_swapchain->device, &alloc_info, + self->descriptor_sets.data()) != VK_SUCCESS) + throw CommandError{"Failed to create Vulkan descriptor sets for view."}; +} + +void +load_resources_to_descriptor_sets(void *obj) +{ + auto self = static_cast(obj); + + for(auto i{0}; i < self->ub_3d.size(); i++) + { + VkDescriptorBufferInfo view_3d_info{}; + view_3d_info.buffer = self->ub_3d[i].buffer; + view_3d_info.offset = 0; + view_3d_info.range = sizeof(BluCat::GRA::UDOView3D); + + 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_UNIFORM_BUFFER; + write_descriptors[0].pBufferInfo = &view_3d_info; + write_descriptors[0].pImageInfo = nullptr; + write_descriptors[0].pTexelBufferView = nullptr; + + vkUpdateDescriptorSets( + BluCat::INT::core.vk_device_with_swapchain->device, + write_descriptors.size(), write_descriptors.data(), 0, nullptr); + } +} + +const CommandChain loader{ + {&load_uniform_buffer, &unload_uniform_buffer} +}; + +const CommandChain descriptor_sets_loader{ + {&load_descriptor_sets, nullptr}, + {&load_resources_to_descriptor_sets, nullptr} +}; + +} + +namespace BluCat::GRA +{ + +View::View( + glm::vec4 region, float projection_width, + float projection_height): + region{region}, + projection_width{projection_width}, + projection_height{projection_height}, + field_of_view{45.0f}, + descriptor_pool{VK_NULL_HANDLE}, + camera_position{std::make_shared(0.0f, 0.0f, 0.0f)}, + camera_orientation{std::make_shared(0.0f, 0.0f, 0.0f, 0.0f)} +{ + loader.execute(this); +} + +View::~View() +{ + loader.revert(this); +} + +void +View::load_descriptor_sets(VkDescriptorPool descriptor_pool) +{ + if(this->descriptor_pool != VK_NULL_HANDLE) return; + + this->descriptor_pool = descriptor_pool; + ::descriptor_sets_loader.execute(this); +} + +void +View::unload_descriptor_sets() +{ + if(this->descriptor_pool == VK_NULL_HANDLE) return; + + this->descriptor_pool = VK_NULL_HANDLE; + ::descriptor_sets_loader.revert(this); +} + +} diff --git a/src/blu_cat/gra/view.hpp b/src/blu_cat/gra/view.hpp new file mode 100644 index 0000000..a61ac34 --- /dev/null +++ b/src/blu_cat/gra/view.hpp @@ -0,0 +1,55 @@ +/* + * Copyright 2022-2025 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 BLU_CAT_GRA_VIEW_H +#define BLU_CAT_GRA_VIEW_H 1 + +#include +#include +#include + +#include "vulkan.hpp" +#include "uniform_buffer.hpp" + +namespace BluCat::GRA +{ + +struct View +{ + glm::vec4 region; + float field_of_view, projection_width, projection_height; + + // FIXME: if this vector get resized, it can cause a segmentation fault! + std::vector ub_3d; + + VkDescriptorPool descriptor_pool; + std::vector descriptor_sets; + + std::shared_ptr camera_position; + std::shared_ptr camera_orientation; + + void + load_descriptor_sets(VkDescriptorPool descriptor_pool); + void + unload_descriptor_sets(); + + View(glm::vec4 region, float projection_width, float projection_height); + ~View(); +}; + +} + +#endif /* BLU_CAT_GRA_VIEW_H */ diff --git a/src/blu_cat/gra/view_2d.cpp b/src/blu_cat/gra/view_2d.cpp deleted file mode 100644 index 948b145..0000000 --- a/src/blu_cat/gra/view_2d.cpp +++ /dev/null @@ -1,160 +0,0 @@ -/* - * Copyright 2022-2024 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 "view_2d.hpp" - -#include - -#include "../int/core.hpp" -#include "uniform_data_object.hpp" - -namespace -{ - -void -load_2d_uniform_buffer(void *obj) -{ - auto self = static_cast(obj); - - try - { - self->ub_2d.reserve(BluCat::INT::core.vk_swapchain->images_count); - for(auto i{0}; i < BluCat::INT::core.vk_swapchain->images_count; i++) - self->ub_2d.emplace_back( - BluCat::INT::core.vk_device_with_swapchain, sizeof(BluCat::GRA::UDOView2D)); - } - catch(const std::exception& e) - { - throw CommandError{e.what()}; - } -} - -void -unload_2d_uniform_buffer(void *obj) -{ - auto self = static_cast(obj); - - self->ub_2d.clear(); -} - -void -load_descriptor_sets_2d(void *obj) -{ - auto self = static_cast(obj); - - std::vector layouts( - BluCat::INT::core.vk_swapchain->images_count, - BluCat::INT::core.vk_descriptor_set_layout->view); - - 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_2d.resize(layouts.size()); - if(vkAllocateDescriptorSets( - BluCat::INT::core.vk_device_with_swapchain->device, &alloc_info, - self->descriptor_sets_2d.data()) != VK_SUCCESS) - throw CommandError{"Failed to create Vulkan descriptor sets for view."}; -} - -void -load_resources_to_descriptor_sets_2d(void *obj) -{ - auto self = static_cast(obj); - - for(auto i{0}; i < self->ub_2d.size(); i++) - { - VkDescriptorBufferInfo view_2d_info{}; - view_2d_info.buffer = self->ub_2d[i].buffer; - view_2d_info.offset = 0; - view_2d_info.range = sizeof(BluCat::GRA::UDOView2D); - - std::array write_descriptors{}; - write_descriptors[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - write_descriptors[0].dstSet = self->descriptor_sets_2d[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 = &view_2d_info; - write_descriptors[0].pImageInfo = nullptr; - write_descriptors[0].pTexelBufferView = nullptr; - - vkUpdateDescriptorSets( - BluCat::INT::core.vk_device_with_swapchain->device, write_descriptors.size(), - write_descriptors.data(), 0, nullptr); - - BluCat::GRA::UDOView2D ubo_view_2d; - ubo_view_2d.proj = glm::ortho( - 0.0f, self->projection_width, - 0.0f, self->projection_height, - 0.0f, 100.0f); - self->ub_2d[i].copy_data(&ubo_view_2d); - } -} - -} - -namespace BluCat::GRA -{ - -const CommandChain View2D::loader{ - {&load_2d_uniform_buffer, &unload_2d_uniform_buffer} -}; - -const CommandChain View2D::descriptor_sets_loader{ - {&load_descriptor_sets_2d, nullptr}, - {&load_resources_to_descriptor_sets_2d, nullptr} -}; - -View2D::View2D( - glm::vec4 region, float projection_width, float projection_height): - projection_width{projection_width}, - projection_height{projection_height}, - region{region}, - descriptor_pool{VK_NULL_HANDLE}, - rectangles_to_draw{BluCat::INT::core.vk_swapchain->images_count}, - sprites_to_draw{BluCat::INT::core.vk_swapchain->images_count} -{ - loader.execute(this); -} - -View2D::~View2D() -{ - loader.revert(this); -} - -void -View2D::load_descriptor_sets(VkDescriptorPool descriptor_pool) -{ - if(this->descriptor_pool != VK_NULL_HANDLE) return; - - this->descriptor_pool = descriptor_pool; - descriptor_sets_loader.execute(this); -} - -void -View2D::unload_descriptor_sets() -{ - if(this->descriptor_pool == VK_NULL_HANDLE) return; - - this->descriptor_pool = VK_NULL_HANDLE; - descriptor_sets_loader.revert(this); -} - -} diff --git a/src/blu_cat/gra/view_2d.hpp b/src/blu_cat/gra/view_2d.hpp deleted file mode 100644 index 84b9f09..0000000 --- a/src/blu_cat/gra/view_2d.hpp +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2022-2024 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 BLU_CAT_GRA_VIEW_2D_H -#define BLU_CAT_GRA_VIEW_2D_H 1 - -#include -#include -#include - -#include "vulkan.hpp" -#include "sprite_to_draw.hpp" -#include "rectangle.hpp" - -namespace BluCat::GRA -{ - -struct View2D -{ - glm::vec4 region; - float projection_width, projection_height; - - // FIXME: if these vectors get resized, they can cause a segmentation fault! - std::vector ub_2d; - - VkDescriptorPool descriptor_pool; - std::vector descriptor_sets_2d; - - std::vector> rectangles_to_draw; - std::vector> sprites_to_draw; - - View2D(glm::vec4 region, float projection_width, float projection_height); - virtual ~View2D(); - - void - virtual load_descriptor_sets(VkDescriptorPool descriptor_pool); - - void - virtual unload_descriptor_sets(); - -protected: - static const CommandChain loader, descriptor_sets_loader; -}; - -} - -#endif /* BLU_CAT_GRA_VIEW_2D_H */ diff --git a/src/blu_cat/gra/view_3d.cpp b/src/blu_cat/gra/view_3d.cpp deleted file mode 100644 index dc310e3..0000000 --- a/src/blu_cat/gra/view_3d.cpp +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Copyright 2022-2024 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 "view_3d.hpp" - -#include - -#include "../int/core.hpp" -#include "uniform_data_object.hpp" - -namespace -{ - -void -load_3d_uniform_buffer(void *obj) -{ - auto self = static_cast(obj); - - try - { - self->ub_3d.reserve(BluCat::INT::core.vk_swapchain->images_count); - for(auto i{0}; i < BluCat::INT::core.vk_swapchain->images_count; i++) - self->ub_3d.emplace_back( - BluCat::INT::core.vk_device_with_swapchain, sizeof(BluCat::GRA::UDOView3D)); - } - catch(const std::exception& e) - { - throw CommandError{e.what()}; - } -} - -void -unload_3d_uniform_buffer(void *obj) -{ - auto self = static_cast(obj); - - self->ub_3d.clear(); -} - -void -load_descriptor_sets_3d(void *obj) -{ - auto self = static_cast(obj); - - std::vector layouts( - BluCat::INT::core.vk_swapchain->images_count, - BluCat::INT::core.vk_descriptor_set_layout->view); - - 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_3d.resize(layouts.size()); - if(vkAllocateDescriptorSets( - BluCat::INT::core.vk_device_with_swapchain->device, &alloc_info, - self->descriptor_sets_3d.data()) != VK_SUCCESS) - throw CommandError{"Failed to create Vulkan descriptor sets for view."}; -} - -void -load_resources_to_descriptor_sets_3d(void *obj) -{ - auto self = static_cast(obj); - - for(auto i{0}; i < self->ub_3d.size(); i++) - { - VkDescriptorBufferInfo view_3d_info{}; - view_3d_info.buffer = self->ub_3d[i].buffer; - view_3d_info.offset = 0; - view_3d_info.range = sizeof(BluCat::GRA::UDOView3D); - - std::array write_descriptors{}; - write_descriptors[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - write_descriptors[0].dstSet = self->descriptor_sets_3d[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 = &view_3d_info; - write_descriptors[0].pImageInfo = nullptr; - write_descriptors[0].pTexelBufferView = nullptr; - - vkUpdateDescriptorSets( - BluCat::INT::core.vk_device_with_swapchain->device, write_descriptors.size(), - write_descriptors.data(), 0, nullptr); - } -} - -const CommandChain loader{ - {&load_3d_uniform_buffer, &unload_3d_uniform_buffer} -}; - -const CommandChain descriptor_sets_loader{ - {&load_descriptor_sets_3d, nullptr}, - {&load_resources_to_descriptor_sets_3d, nullptr} -}; - -} - -namespace BluCat::GRA -{ - -View3D::View3D( - glm::vec4 region, float projection_width, float projection_height): - View2D{region, projection_width, projection_height}, - field_of_view{45.0f}, - camera_position{std::make_shared(0.0f, 0.0f, 0.0f)}, - camera_orientation{std::make_shared(0.0f, 0.0f, 0.0f, 0.0f)} -{ - ::loader.execute(this); -} - -View3D::~View3D() -{ - ::loader.revert(this); -} - -void -View3D::load_descriptor_sets(VkDescriptorPool descriptor_pool) -{ - if(this->descriptor_pool != VK_NULL_HANDLE) return; - - auto parent = dynamic_cast(this); - this->descriptor_pool = descriptor_pool; - View2D::descriptor_sets_loader.execute(parent); - ::descriptor_sets_loader.execute(this); -} - -void -View3D::unload_descriptor_sets() -{ - if(this->descriptor_pool == VK_NULL_HANDLE) return; - - auto parent = dynamic_cast(this); - this->descriptor_pool = VK_NULL_HANDLE; - ::descriptor_sets_loader.revert(this); - View2D::descriptor_sets_loader.revert(parent); -} - -} diff --git a/src/blu_cat/gra/view_3d.hpp b/src/blu_cat/gra/view_3d.hpp deleted file mode 100644 index f09c70f..0000000 --- a/src/blu_cat/gra/view_3d.hpp +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2022-2024 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 BLU_CAT_GRA_VIEW_3D_H -#define BLU_CAT_GRA_VIEW_3D_H 1 - -#include "view_2d.hpp" - -namespace BluCat::GRA -{ - -struct View3D: public View2D -{ - float field_of_view; - // FIXME: if this vector get resized, it can cause a segmentation fault! - std::vector ub_3d; - - std::vector descriptor_sets_3d; - - std::shared_ptr camera_position; - std::shared_ptr camera_orientation; - - View3D(glm::vec4 region, float projection_width, float projection_height); - ~View3D(); - - void - load_descriptor_sets(VkDescriptorPool descriptor_pool); - - void - unload_descriptor_sets(); -}; - -} - -#endif /* BLU_CAT_GRA_VIEW_3D_H */ diff --git a/src/blu_cat/int/core.cpp b/src/blu_cat/int/core.cpp index d631d17..7e78b89 100644 --- a/src/blu_cat/int/core.cpp +++ b/src/blu_cat/int/core.cpp @@ -692,13 +692,13 @@ load_renderer(void *obj) { try { + auto width{static_cast(BluCat::INT::core.display_width)}; + auto height{static_cast(BluCat::INT::core.display_height)}; glm::vec4 region( - 0.f, 0.f, - static_cast(BluCat::INT::core.display_width), - static_cast(BluCat::INT::core.display_height)); + 0.f, 0.f, width, height); BluCat::INT::core.vk_renderer = new BluCat::GRA::Renderer( - {}, - {std::make_shared(region, region.z, region.w)}); + {std::make_shared(region, region.z, region.w)}, + width, height); } catch(const CommandError &e) { -- cgit v1.2.3