From 128adb69472e1bacbbd827c29ed30b3e2915afda Mon Sep 17 00:00:00 2001 From: Rebekah Rowe Date: Mon, 15 Jul 2024 06:16:59 -0400 Subject: [PATCH] Reordered Functions to better show the direction of code flow. --- src/main.cpp | 219 ++++++++++++++++++++++++++------------------------- 1 file changed, 111 insertions(+), 108 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index ef77dd4..02fb44e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -241,7 +241,11 @@ public: const auto found_graphics_family_idx = std::distance(queue_families.begin(), found_graphics_family); ret.graphics_family = found_graphics_family_idx; - const auto found_screen_surface = VulkanCreateSurface(*this->window, *this->vk_inst); + auto found_screen_surface = [](const SDL2pp::Window& window, const vk::raii::Instance& vk_inst) -> std::unique_ptr { + VkSurfaceKHR _screen_surface; + SDL_Vulkan_CreateSurface(window.Get(), static_cast(static_cast(vk_inst)), &_screen_surface); + return std::make_unique(vk_inst, _screen_surface); + }(*this->window, *this->vk_inst); const auto found_present_family = gfx_card.getSurfaceSupportKHR(found_graphics_family_idx, **found_screen_surface) ? found_graphics_family : [&]() -> decltype(queue_families)::const_iterator { // https://github.com/KhronosGroup/Vulkan-Hpp/blob/6f72ceca515d59f40d64b64cf2734f6261e1f9f2/RAII_Samples/05_InitSwapchain/05_InitSwapchain.cpp#L50 decltype(queue_families)::const_iterator queue_family_to_test; for (queue_family_to_test = queue_families.begin(); queue_family_to_test != queue_families.end(); queue_family_to_test++) { @@ -640,40 +644,6 @@ public: this->vk_command_buffers = std::make_unique(*this->vk_gpu, command_buffer_alloc_info); } { - - const auto CreateBuffer = [this](vk::DeviceSize wanted_size, vk::BufferUsageFlags wanted_usage, vk::MemoryPropertyFlags wanted_properties) -> std::pair, std::unique_ptr> { - const vk::BufferCreateInfo buffer_create_info { - .size = wanted_size, - .usage = wanted_usage, - .sharingMode = vk::SharingMode::eExclusive - }; - const auto allocated_buffer = std::make_unique(*this->vk_gpu, buffer_create_info); - - const auto memory_requirements = allocated_buffer->getMemoryRequirements(); - const auto memory_properties = this->vk_gfx_card->getMemoryProperties(); - const auto FindMemoryType = [&memory_properties](const std::uint32_t& type_filter, const vk::MemoryPropertyFlags& wanted_properties) -> std::uint32_t { - const std::uint32_t memory_type_count = memory_properties.memoryTypeCount; - - std::uint32_t memory_type_to_test; - for (memory_type_to_test = 0; memory_type_to_test < memory_type_count; memory_type_to_test++) { - if ((*reinterpret_cast*>(&type_filter))[memory_type_to_test]) { - if ((memory_properties.memoryTypes[memory_type_to_test].propertyFlags & wanted_properties) == wanted_properties) { - break; - } - } - } - if (memory_type_to_test == memory_type_count) - throw std::runtime_error("failed to find suitable memory type!"); - return memory_type_to_test; - }; - const vk::MemoryAllocateInfo vertex_allocate_info { - .allocationSize = memory_requirements.size, - .memoryTypeIndex = FindMemoryType(memory_requirements.memoryTypeBits, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent) - }; - const auto allocated_buffer_memory = std::make_unique(*this->vk_gpu, vertex_allocate_info); - allocated_buffer->bindMemory(**allocated_buffer_memory, 0); - return { std::make_unique(std::move(*allocated_buffer)), std::make_unique(std::move(*allocated_buffer_memory)) }; - }; const auto vertex_buffer_size = sizeof(triangle_vertices[0]) * triangle_vertices.size(); std::tie(this->vk_buffer_vertex, this->vk_buffer_vertex_memory) = CreateBuffer(vertex_buffer_size, vk::BufferUsageFlagBits::eVertexBuffer, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent); @@ -699,54 +669,71 @@ public: throw "Unknown Exception, throwing..."; } } - ~VulkanExampleApplication() { - this->vk_gpu->waitIdle(); - } - void RecordDynamic(const vk::raii::CommandBuffer& command_buffer) const { - const vk::Viewport viewport { - .x = 0.0f, - .y = 0.0f, - .width = (float)this->vk_swapchain_extent.width, - .height = (float)this->vk_swapchain_extent.height, - .minDepth = 0.0f, - .maxDepth = 1.0f - }; - const vk::Rect2D scissor { - .offset = { 0, 0 }, - .extent = this->vk_swapchain_extent - }; - command_buffer.setViewport(0, viewport); - command_buffer.setScissor(0, scissor); - } - void RecordCommandBuffer(const vk::raii::CommandBuffer& command_buffer, std::uint32_t image_index) const { - constexpr vk::CommandBufferBeginInfo command_buffer_begin_info { - //.flags = vk::CommandBufferUsageFlagBits::, // Optional - //.pInheritanceInfo = nullptr // Optional - }; - command_buffer.begin(command_buffer_begin_info); - constexpr std::array clear_colors = { vk::ClearValue { .color = std::array { 0.0f, 0.0f, 0.0f, 1.0f } } }; - const vk::RenderPassBeginInfo render_pass_info { - .renderPass = **this->vk_render_pass, - .framebuffer = *this->vk_swapchain_framebuffers.at(image_index), - .renderArea = { - .offset = { 0, 0 }, - .extent = this->vk_swapchain_extent }, - .clearValueCount = clear_colors.size(), - .pClearValues = clear_colors.data() +protected: + std::pair, std::unique_ptr> CreateBuffer(vk::DeviceSize wanted_size, vk::BufferUsageFlags wanted_usage, vk::MemoryPropertyFlags wanted_properties) { + const vk::BufferCreateInfo buffer_create_info { + .size = wanted_size, + .usage = wanted_usage, + .sharingMode = vk::SharingMode::eExclusive }; - command_buffer.beginRenderPass(render_pass_info, vk::SubpassContents::eInline); - command_buffer.bindPipeline(vk::PipelineBindPoint::eGraphics, **this->vk_pipeline); - command_buffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, *this->vk_pipeline_layout, 0, { this->vk_descriptor_sets->front() }, nullptr); - command_buffer.bindVertexBuffers(0, { **this->vk_buffer_vertex }, { 0 }); - this->RecordDynamic(command_buffer); + const auto allocated_buffer = std::make_unique(*this->vk_gpu, buffer_create_info); - command_buffer.draw(3, 1, 0, 0); + const auto memory_requirements = allocated_buffer->getMemoryRequirements(); + const auto FindMemoryType = [this](const std::uint32_t& type_filter, const vk::MemoryPropertyFlags& wanted_properties) -> std::uint32_t { + const auto memory_properties = this->vk_gfx_card->getMemoryProperties(); + const std::uint32_t memory_type_count = memory_properties.memoryTypeCount; - command_buffer.endRenderPass(); - command_buffer.end(); + std::uint32_t memory_type_to_test; + for (memory_type_to_test = 0; memory_type_to_test < memory_type_count; memory_type_to_test++) { + if ((*reinterpret_cast*>(&type_filter))[memory_type_to_test]) { + if ((memory_properties.memoryTypes[memory_type_to_test].propertyFlags & wanted_properties) == wanted_properties) { + break; + } + } + } + if (memory_type_to_test == memory_type_count) + throw std::runtime_error("failed to find suitable memory type!"); + return memory_type_to_test; + }; + const vk::MemoryAllocateInfo vertex_allocate_info { + .allocationSize = memory_requirements.size, + .memoryTypeIndex = FindMemoryType(memory_requirements.memoryTypeBits, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent) + }; + const auto allocated_buffer_memory = std::make_unique(*this->vk_gpu, vertex_allocate_info); + allocated_buffer->bindMemory(**allocated_buffer_memory, 0); + return { std::make_unique(std::move(*allocated_buffer)), std::make_unique(std::move(*allocated_buffer_memory)) }; }; +public: + void Run() { + bool running = true; + while (running) { + SDL_Event window_event; + while (SDL_PollEvent(&window_event)) { + if (window_event.type == SDL_QUIT) { + running = false; + break; + } + } + this->DrawFrame(); + } + } + void RunTests() { + constexpr auto amount_of_frames_needed = 15000; + std::cout << "Starting Render Tests with: " << std::to_string(amount_of_frames_needed) << " frames!" << std::endl; + std::uintptr_t posted_about = 0; + for (std::uintptr_t drawn_frames = 0; drawn_frames < amount_of_frames_needed; drawn_frames++) { + this->DrawFrame(); + const auto amount_of_thousands = drawn_frames / 1000; + if (amount_of_thousands > posted_about) { + std::cout << "DrawFrame Count: " << std::to_string(drawn_frames) << std::endl; + posted_about = amount_of_thousands; + } + } + std::cout << "Completed tests with: " << std::to_string(amount_of_frames_needed) << " frames!" << std::endl; + } + void DrawFrame() const { const std::array wait_fences = { *this->vk_fence_in_flight }; const vk::Result fence_result = this->vk_gpu->waitForFences(wait_fences, vk::True, std::numeric_limits::max()); @@ -796,39 +783,55 @@ public: if (present_result != vk::Result::eSuccess) throw std::runtime_error("failed to present image!"); } - void Run() { - bool running = true; - while (running) { - SDL_Event window_event; - while (SDL_PollEvent(&window_event)) { - if (window_event.type == SDL_QUIT) { - running = false; - break; - } - } - this->DrawFrame(); - } - } - void RunTests() { - constexpr auto amount_of_frames_needed = 15000; - std::cout << "Starting Render Tests with: " << std::to_string(amount_of_frames_needed) << " frames!" << std::endl; - std::uintptr_t posted_about = 0; - for (std::uintptr_t drawn_frames = 0; drawn_frames < amount_of_frames_needed; drawn_frames++) { - this->DrawFrame(); - const auto amount_of_thousands = drawn_frames / 1000; - if (amount_of_thousands > posted_about) { - std::cout << "DrawFrame Count: " << std::to_string(drawn_frames) << std::endl; - posted_about = amount_of_thousands; - } - } - std::cout << "Completed tests with: " << std::to_string(amount_of_frames_needed) << " frames!" << std::endl; + + void RecordCommandBuffer(const vk::raii::CommandBuffer& command_buffer, std::uint32_t image_index) const { + constexpr vk::CommandBufferBeginInfo command_buffer_begin_info { + //.flags = vk::CommandBufferUsageFlagBits::, // Optional + //.pInheritanceInfo = nullptr // Optional + }; + command_buffer.begin(command_buffer_begin_info); + + constexpr std::array clear_colors = { vk::ClearValue { .color = std::array { 0.0f, 0.0f, 0.0f, 1.0f } } }; + const vk::RenderPassBeginInfo render_pass_info { + .renderPass = **this->vk_render_pass, + .framebuffer = *this->vk_swapchain_framebuffers.at(image_index), + .renderArea = { + .offset = { 0, 0 }, + .extent = this->vk_swapchain_extent }, + .clearValueCount = clear_colors.size(), + .pClearValues = clear_colors.data() + }; + command_buffer.beginRenderPass(render_pass_info, vk::SubpassContents::eInline); + command_buffer.bindPipeline(vk::PipelineBindPoint::eGraphics, **this->vk_pipeline); + command_buffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, *this->vk_pipeline_layout, 0, { this->vk_descriptor_sets->front() }, nullptr); + command_buffer.bindVertexBuffers(0, { **this->vk_buffer_vertex }, { 0 }); + this->RecordDynamic(command_buffer); + + command_buffer.draw(3, 1, 0, 0); + + command_buffer.endRenderPass(); + command_buffer.end(); + }; + void RecordDynamic(const vk::raii::CommandBuffer& command_buffer) const { + const vk::Viewport viewport { + .x = 0.0f, + .y = 0.0f, + .width = (float)this->vk_swapchain_extent.width, + .height = (float)this->vk_swapchain_extent.height, + .minDepth = 0.0f, + .maxDepth = 1.0f + }; + const vk::Rect2D scissor { + .offset = { 0, 0 }, + .extent = this->vk_swapchain_extent + }; + command_buffer.setViewport(0, viewport); + command_buffer.setScissor(0, scissor); } -private: - static std::unique_ptr VulkanCreateSurface(const SDL2pp::Window& window, const vk::raii::Instance& vk_inst) { - VkSurfaceKHR _screen_surface; - SDL_Vulkan_CreateSurface(window.Get(), static_cast(static_cast(vk_inst)), &_screen_surface); - return std::make_unique(vk_inst, _screen_surface); +public: + ~VulkanExampleApplication() { + this->vk_gpu->waitIdle(); } private: