diff options
| author | ladevieq <ladevie.quentin@gmail.com> | 2022-02-13 07:30:30 +0100 |
|---|---|---|
| committer | ladevieq <ladevie.quentin@gmail.com> | 2022-02-13 07:30:30 +0100 |
| commit | bbb45f80298dc4a41d31805416bad19363c8eff8 (patch) | |
| tree | 24f98eadcfc5a0611d4c4c70be4f1c2718236d56 | |
| parent | 4ae9a520d97dcaf69f4a0cd044a4fc3fe689924d (diff) | |
Improve image barrier code
| -rw-r--r-- | include/compute-renderpass.hpp | 116 | ||||
| -rw-r--r-- | include/vk-api.hpp | 16 | ||||
| -rw-r--r-- | include/vk-renderer.hpp | 2 | ||||
| -rw-r--r-- | shaders/compute.comp | 5 | ||||
| -rw-r--r-- | src/compute-renderpass.cpp | 74 | ||||
| -rw-r--r-- | src/main.cpp | 24 | ||||
| -rw-r--r-- | src/vk-api.cpp | 23 | ||||
| -rw-r--r-- | src/vk-renderer.cpp | 107 |
8 files changed, 188 insertions, 179 deletions
diff --git a/include/compute-renderpass.hpp b/include/compute-renderpass.hpp index 5393032..39cbf79 100644 --- a/include/compute-renderpass.hpp +++ b/include/compute-renderpass.hpp @@ -1,57 +1,59 @@ -#pragma once
-
-#include "vk-api.hpp"
-#include <array>
-
-class Texture;
-class Renderpass {
- public:
-
- Renderpass(vkapi& api)
- :api(api)
- {};
-
- virtual ~Renderpass() {
- api.destroy_pipeline(pipeline);
- }
-
- void set_pipeline(pipeline pipeline) {
- this->pipeline = pipeline;
- }
-
- // TODO: Do not use vulkan api type
- virtual void execute(VkCommandBuffer command_buffer) = 0;
-
- protected:
- vkapi &api;
-
- pipeline pipeline;
-};
-
-class ComputeRenderpass : public Renderpass {
- public:
-
- ComputeRenderpass(vkapi& api)
- : Renderpass(api)
- {}
-
- void set_dispatch_size(size_t count_x, size_t count_y, size_t count_z);
-
- void set_ouput_texture(Texture* ouput_texture);
-
- void set_constant(off_t offset, uint64_t* constant);
- void set_constant(off_t offset, Texture* texture);
-
- // TODO: Do not use vulkan api type
- void execute(VkCommandBuffer command_buffer) override final;
-
- Texture* output_texture;
-
- private:
-
- std::array<uint8_t, 64> constants;
-
- size_t group_count_x;
- size_t group_count_y;
- size_t group_count_z;
-};
+#pragma once + +#include "vk-api.hpp" +#include <array> + +class Texture; +class Renderpass { + public: + + Renderpass(vkapi& api) + :api(api) + {}; + + virtual ~Renderpass() { + api.destroy_pipeline(pipeline); + } + + void set_pipeline(pipeline pipeline) { + this->pipeline = pipeline; + } + + // TODO: Do not use vulkan api type + virtual void execute(VkCommandBuffer command_buffer) = 0; + + protected: + vkapi &api; + + pipeline pipeline; +}; + +class ComputeRenderpass : public Renderpass { + public: + + ComputeRenderpass(vkapi& api) + : Renderpass(api) + {} + + void set_dispatch_size(size_t count_x, size_t count_y, size_t count_z); + + void set_ouput_texture(Texture* ouput_texture); + + void set_constant(off_t offset, uint64_t* constant); + void set_constant(off_t offset, Texture* texture); + + // TODO: Do not use vulkan api type + void execute(VkCommandBuffer command_buffer) override final; + + Texture* output_texture; + + private: + + std::vector<Texture*> input_textures; + + std::array<uint8_t, 64> constants; + + size_t group_count_x; + size_t group_count_y; + size_t group_count_z; +}; diff --git a/include/vk-api.hpp b/include/vk-api.hpp index 9cc7707..174d050 100644 --- a/include/vk-api.hpp +++ b/include/vk-api.hpp @@ -31,6 +31,10 @@ struct image { VkExtent3D size; uint32_t bindless_storage_index; uint32_t bindless_sampled_index; + + VkImageLayout previous_layout = VK_IMAGE_LAYOUT_UNDEFINED; + VkPipelineStageFlags previous_stage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; + VkAccessFlags previous_access = 0; }; struct swapchain { @@ -160,17 +164,17 @@ class vkapi { void start_record(VkCommandBuffer command_buffer); - void image_barrier(VkCommandBuffer command_buffer, VkImageLayout src_layout, VkImageLayout dst_layout, VkPipelineStageFlagBits src_stage, VkPipelineStageFlagBits dst_stage, VkAccessFlags src_access, VkAccessFlags dst_access, image image); + void image_barrier(VkCommandBuffer command_buffer, VkImageLayout dst_layout, VkPipelineStageFlagBits dst_stage, VkAccessFlags dst_access, image& image); - void begin_render_pass(VkCommandBuffer command_buffer, VkRenderPass render_pass, VkFramebuffer framebuffer, VkExtent2D size, pipeline pipeline); + void begin_render_pass(VkCommandBuffer command_buffer, VkRenderPass render_pass, VkFramebuffer framebuffer, VkExtent2D size, pipeline& pipeline); void end_render_pass(VkCommandBuffer command_buffer); - void run_compute_pipeline(VkCommandBuffer command_buffer, pipeline pipeline, size_t group_count_x, size_t group_count_y, size_t group_count_z); + void run_compute_pipeline(VkCommandBuffer command_buffer, pipeline& pipeline, size_t group_count_x, size_t group_count_y, size_t group_count_z); - void draw(VkCommandBuffer command_buffer, pipeline pipeline, uint32_t vertex_count, uint32_t vertex_offset); - void draw(VkCommandBuffer command_buffer, pipeline pipeline, buffer index_buffer, uint32_t primitive_count, uint32_t indices_offset, uint32_t vertices_offset); + void draw(VkCommandBuffer command_buffer, pipeline& pipeline, uint32_t vertex_count, uint32_t vertex_offset); + void draw(VkCommandBuffer command_buffer, pipeline& pipeline, buffer& index_buffer, uint32_t primitive_count, uint32_t indices_offset, uint32_t vertices_offset); - void blit_full(VkCommandBuffer command_buffer, image src_image, image dst_image); + void blit_full(VkCommandBuffer command_buffer, image& src_image, image& dst_image); void end_record(VkCommandBuffer command_buffer); diff --git a/include/vk-renderer.hpp b/include/vk-renderer.hpp index 2666b64..ba9b5a0 100644 --- a/include/vk-renderer.hpp +++ b/include/vk-renderer.hpp @@ -37,7 +37,7 @@ class vkrenderer { Texture* create_2d_texture(size_t width, size_t height); - void new_renderpass(Renderpass* renderpass); + ComputeRenderpass* create_compute_renderpass(); void render(); diff --git a/shaders/compute.comp b/shaders/compute.comp index 83a8f0d..6ca193e 100644 --- a/shaders/compute.comp +++ b/shaders/compute.comp @@ -286,9 +286,8 @@ void main() { } float spp_scale = (1.0 / bufs.scene.sample_index); - // vec3 acc_color = srgb_to_linear(imageLoad(images[nonuniformEXT(bufs.accumulation_image_index)], coords).xyz); - // out_color = linear_to_srgb(mix(acc_color, out_color, spp_scale)); - out_color = linear_to_srgb(out_color); + vec3 acc_color = srgb_to_linear(imageLoad(images[nonuniformEXT(bufs.accumulation_image_index)], coords).xyz); + out_color = linear_to_srgb(mix(acc_color, out_color, spp_scale)); for (uint i = 0; i < bufs.scene.downscale_factor; i++) { for (uint j = 0; j < bufs.scene.downscale_factor; j++) { diff --git a/src/compute-renderpass.cpp b/src/compute-renderpass.cpp index fe772bf..8fd4895 100644 --- a/src/compute-renderpass.cpp +++ b/src/compute-renderpass.cpp @@ -1,32 +1,42 @@ -#include "compute-renderpass.hpp"
-
-#include "vk-api.hpp"
-
-#include "vk-renderer.hpp"
-
-void ComputeRenderpass::set_ouput_texture(Texture* output_texture) {
- this->output_texture = output_texture;
-
- // TODO: Remove hardcoded offset
- memcpy(constants.data() + 56, &output_texture->device_image.bindless_storage_index, 4);
-}
-
-void ComputeRenderpass::set_dispatch_size(size_t count_x, size_t count_y, size_t count_z) {
- group_count_x = count_x;
- group_count_y = count_y;
- group_count_z = count_z;
-}
-
-void ComputeRenderpass::set_constant(off_t offset, uint64_t* constant) {
- memcpy(constants.data() + offset, constant, sizeof(uint64_t));
-}
-
-void ComputeRenderpass::set_constant(off_t offset, Texture* texture) {
- memcpy(constants.data() + offset, (void*)&texture->device_image.bindless_sampled_index, 4);
-}
-
-void ComputeRenderpass::execute(VkCommandBuffer command_buffer) {
- vkCmdPushConstants(command_buffer, api.bindless_descriptor.pipeline_layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, 64, (void*)&constants);
-
- api.run_compute_pipeline(command_buffer, pipeline, group_count_x, group_count_y, group_count_z);
-}
+#include "compute-renderpass.hpp" + +#include "vk-api.hpp" + +#include "vk-renderer.hpp" + +void ComputeRenderpass::set_ouput_texture(Texture* output_texture) { + this->output_texture = output_texture; + + // TODO: Remove hardcoded offset + memcpy(constants.data() + 56, &output_texture->device_image.bindless_storage_index, 4); +} + +void ComputeRenderpass::set_dispatch_size(size_t count_x, size_t count_y, size_t count_z) { + group_count_x = count_x; + group_count_y = count_y; + group_count_z = count_z; +} + +void ComputeRenderpass::set_constant(off_t offset, uint64_t* constant) { + memcpy(constants.data() + offset, constant, sizeof(uint64_t)); +} + +void ComputeRenderpass::set_constant(off_t offset, Texture* texture) { + memcpy(constants.data() + offset, (void*)&texture->device_image.bindless_storage_index, 4); + + input_textures.push_back(texture); +} + +void ComputeRenderpass::execute(VkCommandBuffer command_buffer) { + vkCmdPushConstants(command_buffer, api.bindless_descriptor.pipeline_layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, 64, (void*)&constants); + + for (auto& texture: input_textures) { + api.image_barrier(command_buffer, VK_IMAGE_LAYOUT_GENERAL, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_ACCESS_SHADER_READ_BIT, texture->device_image); + } + + api.image_barrier(command_buffer, VK_IMAGE_LAYOUT_GENERAL, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_ACCESS_SHADER_WRITE_BIT, output_texture->device_image); + + api.run_compute_pipeline(command_buffer, pipeline, group_count_x, group_count_y, group_count_z); + + input_textures.clear(); +} diff --git a/src/main.cpp b/src/main.cpp index bd9c48c..4b8edad 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,7 +4,6 @@ #include <cstdio> #include <cassert> -#include "compute-renderpass.hpp" #include "thirdparty/renderdoc.h" #include "imgui.h" @@ -67,7 +66,7 @@ int main() { auto main_scene = scene(cam, width, height, renderer); - auto* raytracing_pass = new ComputeRenderpass(renderer.api); + auto raytracing_pass = renderer.create_compute_renderpass(); raytracing_pass->set_pipeline(renderer.api.create_compute_pipeline("compute")); auto* accumulation_texture = renderer.create_2d_texture(width, height); @@ -80,8 +79,6 @@ int main() { raytracing_pass->set_constant(40, &main_scene.uvs_buffer.device_address); raytracing_pass->set_constant(48, &main_scene.materials_buffer.device_address); - renderer.new_renderpass(raytracing_pass); - auto can_render = true; @@ -254,14 +251,6 @@ int main() { // See the documentation below for a longer explanation // if(rdoc_api) rdoc_api->StartFrameCapture(NULL, NULL); - // if (can_render) { - // renderer.begin_frame(); - - // if (reset_accumulation) { - // ((scene*) main_scene.scene_buffer_ptr())->meta.sample_index = 0; - // renderer.reset_accumulation(); - // } - // float scale = (float)((scene*) main_scene.scene_buffer_ptr())->meta.downscale_factor; // renderer.compute(width / scale, height / scale); @@ -269,16 +258,19 @@ int main() { // renderer.ui(); - // renderer.finish_frame(); - // } - if (can_render) { raytracing_pass->set_ouput_texture(output_texture); raytracing_pass->set_constant(60, accumulation_texture); + if (reset_accumulation) { + ((scene*) main_scene.scene_buffer_ptr())->meta.sample_index = 0; + } + + ((scene*) main_scene.scene_buffer_ptr())->meta.sample_index++; + renderer.render(); - // std::swap(output_texture, accumulation_texture); + std::swap(output_texture, accumulation_texture); } // stop the capture diff --git a/src/vk-api.cpp b/src/vk-api.cpp index a3c02de..27d0da7 100644 --- a/src/vk-api.cpp +++ b/src/vk-api.cpp @@ -3,7 +3,6 @@ #include <iostream> #include "vk-api.hpp" -#include "vulkan/vulkan_core.h" #include "window.hpp" #include "utils.hpp" @@ -984,13 +983,13 @@ void vkapi::start_record(VkCommandBuffer command_buffer) { vkBeginCommandBuffer(command_buffer, &cmd_buf_begin_info); } -void vkapi::image_barrier(VkCommandBuffer command_buffer, VkImageLayout src_layout, VkImageLayout dst_layout, VkPipelineStageFlagBits src_stage, VkPipelineStageFlagBits dst_stage, VkAccessFlags src_access, VkAccessFlags dst_access, image image) { +void vkapi::image_barrier(VkCommandBuffer command_buffer, VkImageLayout dst_layout, VkPipelineStageFlagBits dst_stage, VkAccessFlags dst_access, image& image) { VkImageMemoryBarrier undefined_to_general_barrier = {}; undefined_to_general_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; undefined_to_general_barrier.pNext = nullptr; - undefined_to_general_barrier.srcAccessMask = src_access; + undefined_to_general_barrier.srcAccessMask = image.previous_access; undefined_to_general_barrier.dstAccessMask = dst_access; - undefined_to_general_barrier.oldLayout = src_layout; + undefined_to_general_barrier.oldLayout = image.previous_layout; undefined_to_general_barrier.newLayout = dst_layout; undefined_to_general_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; undefined_to_general_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; @@ -999,7 +998,7 @@ void vkapi::image_barrier(VkCommandBuffer command_buffer, VkImageLayout src_layo vkCmdPipelineBarrier( command_buffer, - src_stage, + image.previous_stage, dst_stage, 0, 0, @@ -1009,10 +1008,14 @@ void vkapi::image_barrier(VkCommandBuffer command_buffer, VkImageLayout src_layo 1, &undefined_to_general_barrier ); + + image.previous_layout = dst_layout; + image.previous_access = dst_access; + image.previous_stage = dst_stage; } -void vkapi::begin_render_pass(VkCommandBuffer command_buffer, VkRenderPass render_pass, VkFramebuffer framebuffer, VkExtent2D size, pipeline pipeline) { +void vkapi::begin_render_pass(VkCommandBuffer command_buffer, VkRenderPass render_pass, VkFramebuffer framebuffer, VkExtent2D size, pipeline& pipeline) { VkRect2D render_area = { { 0, @@ -1040,7 +1043,7 @@ void vkapi::end_render_pass(VkCommandBuffer command_buffer) { } -void vkapi::run_compute_pipeline(VkCommandBuffer command_buffer, pipeline pipeline, size_t group_count_x, size_t group_count_y, size_t group_count_z) { +void vkapi::run_compute_pipeline(VkCommandBuffer command_buffer, pipeline& pipeline, size_t group_count_x, size_t group_count_y, size_t group_count_z) { vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, bindless_descriptor.pipeline_layout, 0, 1, &bindless_descriptor.set, 0, nullptr); vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline.handle); @@ -1048,21 +1051,21 @@ void vkapi::run_compute_pipeline(VkCommandBuffer command_buffer, pipeline pipeli vkCmdDispatch(command_buffer, group_count_x, group_count_y, group_count_z); } -void vkapi::draw(VkCommandBuffer command_buffer, pipeline pipeline, uint32_t vertex_count, uint32_t vertex_offset) { +void vkapi::draw(VkCommandBuffer command_buffer, pipeline& pipeline, uint32_t vertex_count, uint32_t vertex_offset) { vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, bindless_descriptor.pipeline_layout, 0, 1, &bindless_descriptor.set, 0, nullptr); vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.handle); vkCmdDraw(command_buffer, vertex_count, 1, vertex_offset, 0); } -void vkapi::draw(VkCommandBuffer command_buffer, pipeline pipeline, buffer index_buffer, uint32_t index_count, uint32_t index_offset, uint32_t vertex_offset) { +void vkapi::draw(VkCommandBuffer command_buffer, pipeline& pipeline, buffer& index_buffer, uint32_t index_count, uint32_t index_offset, uint32_t vertex_offset) { vkCmdBindIndexBuffer(command_buffer, index_buffer.handle, 0, VK_INDEX_TYPE_UINT16); vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, bindless_descriptor.pipeline_layout, 0, 1, &bindless_descriptor.set, 0, nullptr); vkCmdDrawIndexed(command_buffer, index_count, 1, index_offset, vertex_offset, 0); } -void vkapi::blit_full(VkCommandBuffer command_buffer, image src_image, image dst_image) { +void vkapi::blit_full(VkCommandBuffer command_buffer, image& src_image, image& dst_image) { VkImageSubresourceLayers images_subres_layers { .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .mipLevel = 0, diff --git a/src/vk-renderer.cpp b/src/vk-renderer.cpp index dd6fedc..b449d81 100644 --- a/src/vk-renderer.cpp +++ b/src/vk-renderer.cpp @@ -80,27 +80,27 @@ vkrenderer::vkrenderer(window& wnd) { staging_buffer = api.create_buffer(4096 * 4096 * 4 * sizeof(uint32_t), VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_ONLY); std::memcpy(staging_buffer.alloc_info.pMappedData, pixels, atlas_width * atlas_height * sizeof(uint32_t)); - ui_texture = api.create_image(atlas_size, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT); - io.Fonts->SetTexID((void*)&ui_texture); + // ui_texture = api.create_image(atlas_size, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT); + // io.Fonts->SetTexID((void*)&ui_texture); - VKRESULT(vkResetFences(api.context.device, 1, &submission_fences[virtual_frame_index])) + // VKRESULT(vkResetFences(api.context.device, 1, &submission_fences[virtual_frame_index])) // Initialization frame - auto cmd_buf = command_buffers[virtual_frame_index]; - api.start_record(cmd_buf); + // auto cmd_buf = command_buffers[virtual_frame_index]; + // api.start_record(cmd_buf); - api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_MEMORY_WRITE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, ui_texture); + // api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_MEMORY_WRITE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, ui_texture); - api.copy_buffer(cmd_buf, staging_buffer, ui_texture); + // api.copy_buffer(cmd_buf, staging_buffer, ui_texture); - api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, 0, ui_texture); + // api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, 0, ui_texture); - api.end_record(cmd_buf); + // api.end_record(cmd_buf); - api.submit(cmd_buf, VK_NULL_HANDLE, VK_NULL_HANDLE, submission_fences[virtual_frame_index]); + // api.submit(cmd_buf, VK_NULL_HANDLE, VK_NULL_HANDLE, submission_fences[virtual_frame_index]); - VKRESULT(vkWaitForFences(api.context.device, 1, &submission_fences[virtual_frame_index], VK_TRUE, UINT64_MAX)) + // VKRESULT(vkWaitForFences(api.context.device, 1, &submission_fences[virtual_frame_index], VK_TRUE, UINT64_MAX)) std::vector<image> images = { swapchain.images[0], @@ -110,17 +110,17 @@ vkrenderer::vkrenderer(window& wnd) { api.update_descriptor_images(images, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE); - std::vector<image> sampled_images = { - ui_texture - }; - api.update_descriptor_images(sampled_images, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE); + // std::vector<image> sampled_images = { + // ui_texture + // }; + // api.update_descriptor_images(sampled_images, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE); - ui_texture_sampler = api.create_sampler(VK_FILTER_LINEAR, VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE); + // ui_texture_sampler = api.create_sampler(VK_FILTER_LINEAR, VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE); - std::vector<sampler> samplers = { - ui_texture_sampler, - }; - api.update_descriptor_samplers(samplers); + // std::vector<sampler> samplers = { + // ui_texture_sampler, + // }; + // api.update_descriptor_samplers(samplers); virtual_frame_index++; @@ -141,7 +141,7 @@ vkrenderer::~vkrenderer() { api.destroy_render_pass(render_pass); - api.destroy_sampler(ui_texture_sampler); + // api.destroy_sampler(ui_texture_sampler); for (auto& framebuffer: framebuffers) { api.destroy_framebuffer(framebuffer); @@ -158,39 +158,39 @@ vkrenderer::~vkrenderer() { void vkrenderer::begin_frame() { ImGui::Render(); - ImDrawData* draw_data = ImGui::GetDrawData(); + // ImDrawData* draw_data = ImGui::GetDrawData(); VKRESULT(vkWaitForFences(api.context.device, 1, &submission_fences[virtual_frame_index], VK_TRUE, UINT64_MAX)) VKRESULT(vkResetFences(api.context.device, 1, &submission_fences[virtual_frame_index])) - if (ui_vertex_buffers[virtual_frame_index].size < draw_data->TotalVtxCount * sizeof(VkVertex)) { - api.destroy_buffer(ui_vertex_buffers[virtual_frame_index]); + // if (ui_vertex_buffers[virtual_frame_index].size < draw_data->TotalVtxCount * sizeof(VkVertex)) { + // api.destroy_buffer(ui_vertex_buffers[virtual_frame_index]); - ui_vertex_buffers[virtual_frame_index] = api.create_buffer(draw_data->TotalVtxCount * sizeof(VkVertex), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); - } - if (ui_index_buffers[virtual_frame_index].size < draw_data->TotalIdxCount * sizeof(ImDrawIdx)) { - api.destroy_buffer(ui_index_buffers[virtual_frame_index]); + // ui_vertex_buffers[virtual_frame_index] = api.create_buffer(draw_data->TotalVtxCount * sizeof(VkVertex), VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); + // } + // if (ui_index_buffers[virtual_frame_index].size < draw_data->TotalIdxCount * sizeof(ImDrawIdx)) { + // api.destroy_buffer(ui_index_buffers[virtual_frame_index]); - ui_index_buffers[virtual_frame_index] = api.create_buffer(draw_data->TotalIdxCount * sizeof(ImDrawIdx), VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); - } + // ui_index_buffers[virtual_frame_index] = api.create_buffer(draw_data->TotalIdxCount * sizeof(ImDrawIdx), VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU); + // } - off_t vertex_buffer_offset = 0; - off_t index_buffer_offset = 0; - for (uint32_t draw_list_index = 0; draw_list_index < draw_data->CmdListsCount; draw_list_index++) { - auto vertex_buffer = draw_data->CmdLists[draw_list_index]->VtxBuffer; - auto index_buffer = draw_data->CmdLists[draw_list_index]->IdxBuffer; - std::vector<VkVertex> vertices; + // off_t vertex_buffer_offset = 0; + // off_t index_buffer_offset = 0; + // for (uint32_t draw_list_index = 0; draw_list_index < draw_data->CmdListsCount; draw_list_index++) { + // auto vertex_buffer = draw_data->CmdLists[draw_list_index]->VtxBuffer; + // auto index_buffer = draw_data->CmdLists[draw_list_index]->IdxBuffer; + // std::vector<VkVertex> vertices; - for (auto& imVertex: vertex_buffer) { - vertices.push_back(*((VkVertex*)&imVertex)); - } + // for (auto& imVertex: vertex_buffer) { + // vertices.push_back(*((VkVertex*)&imVertex)); + // } - std::memcpy((void*) ((uint64_t)ui_vertex_buffers[virtual_frame_index].alloc_info.pMappedData + (vertex_buffer_offset * sizeof(VkVertex))), vertices.data(), vertex_buffer.Size * sizeof(VkVertex)); - std::memcpy((void*) ((uint64_t)ui_index_buffers[virtual_frame_index].alloc_info.pMappedData + (index_buffer_offset * sizeof(ImDrawIdx))), index_buffer.Data, index_buffer.Size * sizeof(ImDrawIdx)); + // std::memcpy((void*) ((uint64_t)ui_vertex_buffers[virtual_frame_index].alloc_info.pMappedData + (vertex_buffer_offset * sizeof(VkVertex))), vertices.data(), vertex_buffer.Size * sizeof(VkVertex)); + // std::memcpy((void*) ((uint64_t)ui_index_buffers[virtual_frame_index].alloc_info.pMappedData + (index_buffer_offset * sizeof(ImDrawIdx))), index_buffer.Data, index_buffer.Size * sizeof(ImDrawIdx)); - vertex_buffer_offset += vertex_buffer.Size; - index_buffer_offset += index_buffer.Size; - } + // vertex_buffer_offset += vertex_buffer.Size; + // index_buffer_offset += index_buffer.Size; + // } swapchain_image_index = 0; auto acquire_result = vkAcquireNextImageKHR(api.context.device, swapchain.handle, UINT64_MAX, acquire_semaphores[virtual_frame_index], VK_NULL_HANDLE, &swapchain_image_index); @@ -357,11 +357,11 @@ void vkrenderer::update_image(image img, void* data, size_t size) { auto cmd_buf = command_buffers[virtual_frame_index]; api.start_record(cmd_buf); - api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_MEMORY_WRITE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, img); + api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, img); api.copy_buffer(cmd_buf, staging_buffer, img); - api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, 0, img); + api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_GENERAL, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, img); api.end_record(cmd_buf); @@ -385,8 +385,10 @@ Texture* vkrenderer::create_2d_texture(size_t width, size_t height) { return texture; } -void vkrenderer::new_renderpass(Renderpass* renderpass) { - renderpasses.push_back(renderpass); +ComputeRenderpass* vkrenderer::create_compute_renderpass() { + renderpasses.push_back(new ComputeRenderpass(api)); + + return (ComputeRenderpass*)renderpasses.back(); } void vkrenderer::render() { @@ -395,18 +397,15 @@ void vkrenderer::render() { auto cmd_buf = command_buffers[virtual_frame_index]; for (auto& renderpass: renderpasses) { - api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_ACCESS_TRANSFER_READ_BIT, VK_ACCESS_SHADER_WRITE_BIT, ((ComputeRenderpass*)renderpass)->output_texture->device_image); - renderpass->execute(cmd_buf); - api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT, ((ComputeRenderpass*)renderpass)->output_texture->device_image); - + api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_READ_BIT, ((ComputeRenderpass*)renderpass)->output_texture->device_image); - api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_ACCESS_TRANSFER_WRITE_BIT, swapchain.images[swapchain_image_index]); + api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, swapchain.images[swapchain_image_index]); api.blit_full(cmd_buf, ((ComputeRenderpass*)renderpass)->output_texture->device_image, swapchain.images[swapchain_image_index]); - api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, 0, swapchain.images[swapchain_image_index]); + api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, swapchain.images[swapchain_image_index]); } finish_frame(); |
