From 81dbf32c66a4c07e814c58de39be8d425f3e2dd9 Mon Sep 17 00:00:00 2001 From: Simon Gaubert Date: Sat, 2 Mar 2024 19:53:19 +0100 Subject: [PATCH] REMOVE draw image from vulkan api --- src/app/threads/render/RenderThread.cpp | 46 ---------------- src/app/threads/render/imgui_windows.cpp | 9 ++-- src/app/vulkan/VulkanAPI.cpp | 68 ++---------------------- src/app/vulkan/VulkanAPI.hpp | 20 +++---- 4 files changed, 16 insertions(+), 127 deletions(-) diff --git a/src/app/threads/render/RenderThread.cpp b/src/app/threads/render/RenderThread.cpp index 4d0b5ca0..205052b3 100644 --- a/src/app/threads/render/RenderThread.cpp +++ b/src/app/threads/render/RenderThread.cpp @@ -157,52 +157,6 @@ void RenderThread::loop() m_end_cpu_rendering_time = std::chrono::steady_clock::now().time_since_epoch(); vkCmdEndRendering(vk.render_command_buffers[vk.current_frame]); - //############################################################################################################ - // # - // Auguste tu commence ici # - // # - //############################################################################################################ - - /* - * vk.width() = the width of the image - * vk.height() = the height of the image - * vk.clearPixels() = clear the draw image - * vk.putPixel(x, y, r, g, b, a = 255) = put a pixel at the position (x, y) with the color (r, g, b, a) - */ - - // static Perlin perlin_pinpin(0, 5, 1, 0.5, 2.0); - - // (void)time; - // vk.clearPixels(); - // // int nope; - // // std::cin >> nope; std::cin.clear(); - - // for(size_t i = 0; i < vk.height(); i++) - // { - // for(size_t j = 0; j < vk.width(); j++) - // { - // float value = perlin_pinpin.noise(glm::vec2(j * 0.01f, i * 0.01f)); - - // //normalize to range [0, 1] - // value += 1; - // value /= 2; - - // int light = value * 255; - // light &= 0xFF; - - // vk.putPixel(j, i, light, light, light); - // // std::cout << std::setw(3) << light << " "; - // } - // // std::cout << std::endl; - // } - - //############################################################################################################ - // # - // Et tu t'arrĂȘtes la :) # - // # - //############################################################################################################ - - //############################################################################################################ // # // Submit the command buffer to the graphics queue # diff --git a/src/app/threads/render/imgui_windows.cpp b/src/app/threads/render/imgui_windows.cpp index 185264c8..0f9b018d 100644 --- a/src/app/threads/render/imgui_windows.cpp +++ b/src/app/threads/render/imgui_windows.cpp @@ -14,16 +14,17 @@ void RenderThread::updateImGui() ImGui::Text("FPS: %f", m_fps); ImGui::Text("Triangle count: %d", m_triangle_count); - - // display image - for (uint32_t x = 0; x < vk.imgui_texture.extent.width; x++) + // write to texture + vk.imgui_texture.clear(); + for (uint32_t x = 0; x < vk.imgui_texture.width(); x++) { - for (uint32_t y = 0; y < vk.imgui_texture.extent.height; y++) + for (uint32_t y = 0; y < vk.imgui_texture.height(); y++) { vk.imgui_texture.putPixel(x, y, 255, 0, 0, 255); } } + // display image ImGui::Image( (void *)vk.imgui_texture.descriptor_set, ImVec2(vk.imgui_texture.extent.width, vk.imgui_texture.extent.height) diff --git a/src/app/vulkan/VulkanAPI.cpp b/src/app/vulkan/VulkanAPI.cpp index 7d39fa4b..53825332 100644 --- a/src/app/vulkan/VulkanAPI.cpp +++ b/src/app/vulkan/VulkanAPI.cpp @@ -21,7 +21,6 @@ VulkanAPI::VulkanAPI(GLFWwindow * window): createCommandBuffer(); createSyncObjects(); - createDrawImage(); createColorResources(); createDepthResources(); createUniformBuffers(); @@ -56,10 +55,6 @@ VulkanAPI::~VulkanAPI() vkDestroyImageView(device, texture_image_view, nullptr); vkDestroySampler(device, texture_sampler, nullptr); - vkDestroyImage(device, draw_image, nullptr); - vkUnmapMemory(device, draw_image_memory); - vkFreeMemory(device, draw_image_memory, nullptr); - for (int i = 0; i < max_frames_in_flight; i++) { vkUnmapMemory(device, uniform_buffers_memory[i]); @@ -565,15 +560,13 @@ void VulkanAPI::recreateSwapChain(GLFWwindow * window) vkDeviceWaitIdle(device); + destroyImGuiTexture(imgui_texture); + ImGui_ImplVulkan_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); vkDestroyDescriptorPool(device, imgui_descriptor_pool, nullptr); - vkDestroyImage(device, draw_image, nullptr); - vkUnmapMemory(device, draw_image_memory); - vkFreeMemory(device, draw_image_memory, nullptr); - vkDestroyImageView(device, color_attachement_view, nullptr); vkFreeMemory(device, color_attachement_memory, nullptr); vkDestroyImage(device, color_attachement_image, nullptr); @@ -596,8 +589,8 @@ void VulkanAPI::recreateSwapChain(GLFWwindow * window) createColorResources(); createDepthResources(); createPipeline(); - createDrawImage(); setupImgui(); + createImGuiTexture(100, 100); } VkSurfaceFormatKHR VulkanAPI::chooseSwapSurfaceFormat(const std::vector & available_formats) @@ -1230,39 +1223,6 @@ VkShaderModule VulkanAPI::createShaderModule(const std::vector & code) return shader_module; } -void VulkanAPI::createDrawImage() -{ - draw_image_format = VK_FORMAT_R8G8B8A8_SRGB; - draw_image_extent = swap_chain_extent; - - createImage( - draw_image_extent.width, - draw_image_extent.height, - 1, - draw_image_format, - VK_IMAGE_TILING_LINEAR, - VK_IMAGE_USAGE_TRANSFER_SRC_BIT, - VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, - draw_image, - draw_image_memory - ); - - transitionImageLayout( - draw_image, - VK_IMAGE_LAYOUT_UNDEFINED, - VK_IMAGE_LAYOUT_GENERAL, - VK_IMAGE_ASPECT_COLOR_BIT, - 1, - 0, - 0, - VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, - VK_PIPELINE_STAGE_TRANSFER_BIT - ); - - // map memory - vkMapMemory(device, draw_image_memory, 0, VK_WHOLE_SIZE, 0, &draw_image_mapped_memory); -} - uint64_t VulkanAPI::createImGuiTexture(const uint32_t width, const uint32_t height) { imgui_texture.extent = { width, height }; @@ -1538,28 +1498,6 @@ void VulkanAPI::setupImgui() } -void VulkanAPI::clearPixels() -{ - std::memset(draw_image_mapped_memory, 0, draw_image_extent.width * draw_image_extent.height * 4); -} - -void VulkanAPI::putPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a) -{ - if (x >= draw_image_extent.width || y >= draw_image_extent.height) - { - throw std::runtime_error("Pixel coordinates out of range"); - } - - uint8_t * pixel = reinterpret_cast(draw_image_mapped_memory); - pixel += (y * draw_image_extent.width + x) * 4; - pixel[0] = r; - pixel[1] = g; - pixel[2] = b; - pixel[3] = a; - -} - - VkCommandBuffer VulkanAPI::beginSingleTimeCommands() { vkResetFences(device, 1, &single_time_command_fence); diff --git a/src/app/vulkan/VulkanAPI.hpp b/src/app/vulkan/VulkanAPI.hpp index 577200cf..de50cfd9 100644 --- a/src/app/vulkan/VulkanAPI.hpp +++ b/src/app/vulkan/VulkanAPI.hpp @@ -144,6 +144,14 @@ struct ImGuiTexture VkFormat format; VkExtent2D extent; + uint32_t width() const { return extent.width; } + uint32_t height() const { return extent.height; } + + void clear() + { + memset(mapped_memory, 0, extent.width * extent.height * 4); + } + void putPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) { uint8_t * pixel = (uint8_t *)mapped_memory + (y * extent.width + x) * 4; @@ -181,11 +189,6 @@ class VulkanAPI void recreateSwapChain(GLFWwindow * window); - uint32_t width() const { return draw_image_extent.width; } - uint32_t height() const { return draw_image_extent.height; } - void clearPixels(); - void putPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255); - uint64_t createMesh(const Chunk & chunk); uint64_t storeMesh(const std::vector & vertices, const std::vector & indices); @@ -237,12 +240,6 @@ class VulkanAPI VkFormat depth_attachement_format; VkExtent2D depth_attachement_extent; - VkImage draw_image; - VkDeviceMemory draw_image_memory; - void * draw_image_mapped_memory; - VkFormat draw_image_format; - VkExtent2D draw_image_extent; - std::vector uniform_buffers; std::vector uniform_buffers_memory; std::vector uniform_buffers_mapped_memory; @@ -333,7 +330,6 @@ class VulkanAPI void createColorResources(); void createDepthResources(); - void createDrawImage(); void createUniformBuffers(); void createImageTexture(const std::string & file_path);