diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/main.cpp | 4 | ||||
| -rw-r--r-- | src/utils.cpp | 4 | ||||
| -rw-r--r-- | src/vk-api.cpp | 134 | ||||
| -rw-r--r-- | src/vk-renderer.cpp | 6 |
5 files changed, 144 insertions, 6 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index cc03d6b..04dc37e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -22,6 +22,8 @@ add_executable( window.cpp ) +target_link_libraries(path-tracer PRIVATE imgui::imgui) + if(UNIX) target_link_libraries(path-tracer ${CMAKE_DL_LIBS} ${X11_xcb_LIB}) target_link_libraries(path-tracer ${CMAKE_DL_LIBS} ${X11_xkbcommon_LIB}) diff --git a/src/main.cpp b/src/main.cpp index 9e3634f..9736aaf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,7 @@ #include <cstdio> #include <cassert> +#include "imgui.h" #include "thirdparty/renderdoc.h" #include "vk-renderer.hpp" @@ -57,6 +58,9 @@ int main() { auto canRender = true; vkrenderer renderer { wnd, inputs }; + ImGui::Text("Hello, world %d", 123); + + while(wnd.isOpen) { end = std::chrono::high_resolution_clock::now(); diff --git a/src/utils.cpp b/src/utils.cpp index c05cc77..3b7c445 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -32,6 +32,10 @@ std::vector<uint8_t> get_shader_code(const char* path) { return shader_code; } +std::vector<uint8_t> get_shader_code(std::string& path) { + return get_shader_code(path.c_str()); +} + color ground_color { 1.0, 1.0, 1.0 }; color sky_color { 0.5, 0.7, 1.0 }; diff --git a/src/vk-api.cpp b/src/vk-api.cpp index cd401a1..2d97228 100644 --- a/src/vk-api.cpp +++ b/src/vk-api.cpp @@ -229,14 +229,16 @@ void vkapi::destroy_command_buffers(std::vector<VkCommandBuffer> &command_buffer } -ComputePipeline vkapi::create_compute_pipeline(const char* shader_path, std::vector<VkDescriptorSetLayoutBinding> bindings) { - ComputePipeline pipeline; +Pipeline vkapi::create_compute_pipeline(std::string& shader_name, std::vector<VkDescriptorSetLayoutBinding> bindings) { + Pipeline pipeline; - std::vector<uint8_t> shader_code = get_shader_code(shader_path); + std::vector<uint8_t> shader_code = get_shader_code("./shaders/" + shader_name + ".comp.spv"); if (shader_code.size() == 0) { exit(1); } + pipeline.shader_modules.resize(1); + VkShaderModuleCreateInfo shader_create_info = {}; shader_create_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; shader_create_info.pNext = nullptr; @@ -244,14 +246,14 @@ ComputePipeline vkapi::create_compute_pipeline(const char* shader_path, std::vec shader_create_info.codeSize = shader_code.size(); shader_create_info.pCode = (uint32_t*)shader_code.data(); - VKRESULT(vkCreateShaderModule(context.device, &shader_create_info, nullptr, &pipeline.shader_module)) + VKRESULT(vkCreateShaderModule(context.device, &shader_create_info, nullptr, &pipeline.shader_modules[0])) VkPipelineShaderStageCreateInfo stage_create_info = {}; stage_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; stage_create_info.pNext = nullptr; stage_create_info.flags = 0; stage_create_info.stage = VK_SHADER_STAGE_COMPUTE_BIT; - stage_create_info.module = pipeline.shader_module; + stage_create_info.module = pipeline.shader_modules[0]; stage_create_info.pName = "main"; stage_create_info.pSpecializationInfo = nullptr; @@ -295,7 +297,109 @@ ComputePipeline vkapi::create_compute_pipeline(const char* shader_path, std::vec return std::move(pipeline); } -void vkapi::destroy_compute_pipeline(ComputePipeline &pipeline) { +Pipeline vkapi::create_graphics_pipeline(std::string& shader_name, VkShaderStageFlagBits shader_stages, std::vector<VkDescriptorSetLayoutBinding> bindings) { + Pipeline pipeline; + + + std::vector<VkPipelineShaderStageCreateInfo> stages_create_info = {}; + + for (VkShaderStageFlags stage_flag = VK_SHADER_STAGE_VERTEX_BIT; stage_flag <= VK_SHADER_STAGE_ALL_GRAPHICS; stage_flag >>= 2) { + if (stage_flag & shader_stages) { + VkShaderModule shader_module; + + std::vector<uint8_t> shader_code = get_shader_code("./shaders/" + shader_name + shader_stage_extension(stage_flag) + ".spv"); + if (shader_code.size() == 0) { + exit(1); + } + + VkShaderModuleCreateInfo shader_create_info = {}; + shader_create_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; + shader_create_info.pNext = nullptr; + shader_create_info.flags = 0; + shader_create_info.codeSize = shader_code.size(); + shader_create_info.pCode = (uint32_t*)shader_code.data(); + + VKRESULT(vkCreateShaderModule(context.device, &shader_create_info, nullptr, &shader_module)) + + pipeline.shader_modules.push_back(shader_module); + + VkPipelineShaderStageCreateInfo stage_create_info = {}; + stage_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; + stage_create_info.pNext = nullptr; + stage_create_info.flags = 0; + stage_create_info.stage = VK_SHADER_STAGE_COMPUTE_BIT; + stage_create_info.module = shader_module; + stage_create_info.pName = "main"; + stage_create_info.pSpecializationInfo = nullptr; + + stages_create_info.push_back(stage_create_info); + } + } + + + VkDescriptorSetLayoutCreateInfo descriptor_set_layout_create_info = {}; + descriptor_set_layout_create_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; + descriptor_set_layout_create_info.pNext = nullptr; + descriptor_set_layout_create_info.flags = 0; + descriptor_set_layout_create_info.bindingCount = bindings.size(); + descriptor_set_layout_create_info.pBindings = bindings.data(); + + VKRESULT(vkCreateDescriptorSetLayout(context.device, &descriptor_set_layout_create_info, nullptr, &pipeline.descriptor_set_layout)) + + VkPipelineLayoutCreateInfo layout_create_info = {}; + layout_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; + layout_create_info.pNext = nullptr; + layout_create_info.flags = 0; + layout_create_info.setLayoutCount = 1; + layout_create_info.pSetLayouts = &pipeline.descriptor_set_layout; + layout_create_info.pushConstantRangeCount = 0; + layout_create_info.pPushConstantRanges = nullptr; + + VKRESULT(vkCreatePipelineLayout(context.device, &layout_create_info, nullptr, &pipeline.layout)) + + + VkPipelineVertexInputStateCreateInfo vertex_input_state_create_info = {}; + vertex_input_state_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; + vertex_input_state_create_info.pNext = nullptr; + vertex_input_state_create_info.flags = 0; + + VkPipelineInputAssemblyStateCreateInfo input_assembly_state_create_info = {}; + input_assembly_state_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; + input_assembly_state_create_info.pNext = nullptr; + input_assembly_state_create_info.flags = 0; + + VkPipelineViewportStateCreateInfo viewport_state_create_info = {}; + viewport_state_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; + viewport_state_create_info.pNext = nullptr; + viewport_state_create_info.flags = 0; + + VkPipelineRasterizationStateCreateInfo rasterization_state_create_info = {}; + rasterization_state_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; + rasterization_state_create_info.pNext = nullptr; + rasterization_state_create_info.flags = 0; + + VkPipelineMultisampleStateCreateInfo multisample_state_create_info = {}; + multisample_state_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; + multisample_state_create_info.pNext = nullptr; + multisample_state_create_info.flags = 0; + + VkPipelineColorBlendStateCreateInfo color_blend_state_create_info = {}; + color_blend_state_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; + color_blend_state_create_info.pNext = nullptr; + color_blend_state_create_info.flags = 0; + + + VkGraphicsPipelineCreateInfo pipeline_create_info = {}; + pipeline_create_info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; + pipeline_create_info.pNext = nullptr; + pipeline_create_info.flags = 0; + + VkPipelineCache pipeline_cache = {}; + + VKRESULT(vkCreateGraphicsPipeline(context.device, &pipeline_cache, 1, &pipeline_create_info, VK_NULL_HANDLE, pipeline.handle); +} + +void vkapi::destroy_pipeline(Pipeline &pipeline) { vkDestroyShaderModule(context.device, pipeline.shader_module, nullptr); vkDestroyDescriptorSetLayout(context.device, pipeline.descriptor_set_layout, nullptr); vkDestroyPipelineLayout(context.device, pipeline.layout, nullptr); @@ -626,3 +730,21 @@ VkResult vkapi::present(Swapchain swapchain, uint32_t image_index, VkSemaphore w return vkQueuePresentKHR(context.queue, &present_info); } + + + +std::string vkapi::shader_stage_extension(VkShaderStageFlags shader_stage) { + switch(shader_stage) { + case VK_SHADER_STAGE_VERTEX_BIT: { + return std::string(".vert"); + } + case VK_SHADER_STAGE_FRAGMENT_BIT: { + return std::string(".frag"); + } + case VK_SHADER_STAGE_COMPUTE_BIT: { + return std::string(".comp"); + } + default: + std::cerr << "Stage not supported" << std::endl; + } +} diff --git a/src/vk-renderer.cpp b/src/vk-renderer.cpp index 25ec624..3bec679 100644 --- a/src/vk-renderer.cpp +++ b/src/vk-renderer.cpp @@ -2,6 +2,7 @@ #include <cstring> #include <cassert> +#include "imgui.h" #include "vk-renderer.hpp" #include "thirdparty/vk_mem_alloc.h" @@ -88,6 +89,11 @@ vkrenderer::~vkrenderer() { api.destroy_surface(platform_surface); } +void vkrenderer::ui() { + ImGui::NewFrame(); + ImGui::Render(); + ImDrawData* draw_data = ImGui::GetDrawData(); +} void vkrenderer::compute(uint32_t width, uint32_t height, bool clear) { VKRESULT(vkWaitForFences(api.context.device, 1, &submission_fences[frame_index], VK_TRUE, UINT64_MAX)) |
