Skip to content

Commit

Permalink
REMOVE draw image from vulkan api
Browse files Browse the repository at this point in the history
  • Loading branch information
SaumonDesMers authored and afaure42 committed Mar 3, 2024
1 parent 15c6621 commit 81dbf32
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 127 deletions.
46 changes: 0 additions & 46 deletions src/app/threads/render/RenderThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 #
Expand Down
9 changes: 5 additions & 4 deletions src/app/threads/render/imgui_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
68 changes: 3 additions & 65 deletions src/app/vulkan/VulkanAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ VulkanAPI::VulkanAPI(GLFWwindow * window):
createCommandBuffer();
createSyncObjects();

createDrawImage();
createColorResources();
createDepthResources();
createUniformBuffers();
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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);
Expand All @@ -596,8 +589,8 @@ void VulkanAPI::recreateSwapChain(GLFWwindow * window)
createColorResources();
createDepthResources();
createPipeline();
createDrawImage();
setupImgui();
createImGuiTexture(100, 100);
}

VkSurfaceFormatKHR VulkanAPI::chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR> & available_formats)
Expand Down Expand Up @@ -1230,39 +1223,6 @@ VkShaderModule VulkanAPI::createShaderModule(const std::vector<char> & 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 };
Expand Down Expand Up @@ -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<uint8_t *>(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);
Expand Down
20 changes: 8 additions & 12 deletions src/app/vulkan/VulkanAPI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<BlockVertex> & vertices, const std::vector<uint32_t> & indices);

Expand Down Expand Up @@ -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<VkBuffer> uniform_buffers;
std::vector<VkDeviceMemory> uniform_buffers_memory;
std::vector<void *> uniform_buffers_mapped_memory;
Expand Down Expand Up @@ -333,7 +330,6 @@ class VulkanAPI

void createColorResources();
void createDepthResources();
void createDrawImage();

void createUniformBuffers();
void createImageTexture(const std::string & file_path);
Expand Down

0 comments on commit 81dbf32

Please sign in to comment.