diff options
| author | ladevieq <ladevie.quentin@gmail.com> | 2022-01-16 22:03:56 +0100 |
|---|---|---|
| committer | ladevieq <ladevie.quentin@gmail.com> | 2022-01-16 22:03:56 +0100 |
| commit | ab39ab82b4dc60e4a181140dc91ee046e5daa6ea (patch) | |
| tree | b228c4b586f55b09e59e5572ee3dd8d2e81f1236 | |
| parent | c24761cfaf0d4c510504e6ddc08f6a8399295cac (diff) | |
Fix barriers
| -rw-r--r-- | include/vk-api.hpp | 16 | ||||
| -rw-r--r-- | include/vk-renderer.hpp | 8 | ||||
| -rw-r--r-- | shaders/compute.comp | 3 | ||||
| -rw-r--r-- | src/vk-api.cpp | 20 | ||||
| -rw-r--r-- | src/vk-renderer.cpp | 18 |
5 files changed, 38 insertions, 27 deletions
diff --git a/include/vk-api.hpp b/include/vk-api.hpp index 1127bd7..864632e 100644 --- a/include/vk-api.hpp +++ b/include/vk-api.hpp @@ -14,7 +14,7 @@ struct buffer { VkDeviceAddress device_address; }; -struct Pipeline { +struct pipeline { std::vector<VkShaderModule> shader_modules; VkPipeline handle; VkPipelineBindPoint bind_point; @@ -136,9 +136,9 @@ class vkapi { void destroy_framebuffers(std::vector<VkFramebuffer>& framebuffers); - Pipeline create_compute_pipeline(const char* shader_name); - Pipeline create_graphics_pipeline(const char* shader_name, VkShaderStageFlagBits shader_stages, VkRenderPass render_pass, std::vector<VkDynamicState> dynamic_states); - void destroy_pipeline(Pipeline &pipeline); + pipeline create_compute_pipeline(const char* shader_name); + pipeline create_graphics_pipeline(const char* shader_name, VkShaderStageFlagBits shader_stages, VkRenderPass render_pass, std::vector<VkDynamicState> dynamic_states); + void destroy_pipeline(pipeline &pipeline); std::vector<VkDescriptorSet> create_descriptor_sets(VkDescriptorSetLayout descriptor_sets_layout, size_t descriptor_sets_count); @@ -160,13 +160,13 @@ class vkapi { 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 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); diff --git a/include/vk-renderer.hpp b/include/vk-renderer.hpp index 99521a6..2ec9ef8 100644 --- a/include/vk-renderer.hpp +++ b/include/vk-renderer.hpp @@ -54,16 +54,16 @@ class vkrenderer { std::vector<VkCommandBuffer> command_buffers; - Pipeline compute_pipeline; + pipeline compute_pipeline; buffer staging_buffer; - Pipeline tonemapping_pipeline; + pipeline tonemapping_pipeline; std::vector<buffer> ui_vertex_buffers; std::vector<buffer> ui_index_buffers; - Pipeline ui_pipeline; + pipeline ui_pipeline; VkRenderPass render_pass; std::vector<VkFramebuffer> framebuffers; @@ -75,7 +75,7 @@ class vkrenderer { swapchain swapchain; const uint32_t min_swapchain_image_count = 3; - const uint32_t virtual_frames_count = 2; + static constexpr uint32_t virtual_frames_count = 2; }; #endif // !__VK_RENDERER_HPP_ diff --git a/shaders/compute.comp b/shaders/compute.comp index c8257cf..6ca193e 100644 --- a/shaders/compute.comp +++ b/shaders/compute.comp @@ -270,6 +270,9 @@ ray generate_camera_ray(ivec2 coords, uint seed) { } void main() { + if (any(greaterThanEqual(gl_GlobalInvocationID.xy, uvec2(bufs.scene.width, bufs.scene.height)))) + return; + ivec2 coords = ivec2(gl_GlobalInvocationID.xy) * bufs.scene.downscale_factor; uint seed = uint(coords.x * uint(1973) + coords.y * uint(9277) + bufs.scene.sample_index * uint(26699)) | uint(1); diff --git a/src/vk-api.cpp b/src/vk-api.cpp index 4bfae42..020efc6 100644 --- a/src/vk-api.cpp +++ b/src/vk-api.cpp @@ -442,7 +442,7 @@ VkRenderPass vkapi::create_render_pass(std::vector<VkFormat>& color_attachments_ attachment_description.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; attachment_description.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; attachment_description.initialLayout = initial_layout; - attachment_description.finalLayout = final_layout; + attachment_description.finalLayout = initial_layout; attachments_description.push_back(attachment_description); @@ -527,8 +527,8 @@ void vkapi::destroy_framebuffers(std::vector<VkFramebuffer>& framebuffers) { } -Pipeline vkapi::create_compute_pipeline(const char* shader_name) { - Pipeline pipeline { +pipeline vkapi::create_compute_pipeline(const char* shader_name) { + pipeline pipeline { .bind_point = VK_PIPELINE_BIND_POINT_COMPUTE }; @@ -579,8 +579,8 @@ Pipeline vkapi::create_compute_pipeline(const char* shader_name) { return std::move(pipeline); } -Pipeline vkapi::create_graphics_pipeline(const char* shader_name, VkShaderStageFlagBits shader_stages, VkRenderPass render_pass, std::vector<VkDynamicState> dynamic_states) { - Pipeline pipeline { +pipeline vkapi::create_graphics_pipeline(const char* shader_name, VkShaderStageFlagBits shader_stages, VkRenderPass render_pass, std::vector<VkDynamicState> dynamic_states) { + pipeline pipeline { .bind_point = VK_PIPELINE_BIND_POINT_GRAPHICS }; @@ -707,7 +707,7 @@ Pipeline vkapi::create_graphics_pipeline(const char* shader_name, VkShaderStageF return pipeline; } -void vkapi::destroy_pipeline(Pipeline &pipeline) { +void vkapi::destroy_pipeline(pipeline &pipeline) { for (auto& shader_module: pipeline.shader_modules) { vkDestroyShaderModule(context.device, shader_module, nullptr); } @@ -986,7 +986,7 @@ void vkapi::image_barrier(VkCommandBuffer command_buffer, VkImageLayout src_layo } -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, @@ -1014,7 +1014,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); @@ -1022,14 +1022,14 @@ 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); diff --git a/src/vk-renderer.cpp b/src/vk-renderer.cpp index 76185d0..7dd566f 100644 --- a/src/vk-renderer.cpp +++ b/src/vk-renderer.cpp @@ -213,17 +213,17 @@ void vkrenderer::reset_accumulation() { auto cmd_buf = command_buffers[virtual_frame_index]; VkClearColorValue clear_color = { 0.f, 0.f, 0.f, 1.f }; - api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, accumulation_images[0]); + api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_ACCESS_TRANSFER_WRITE_BIT, accumulation_images[0]); vkCmdClearColorImage(cmd_buf, accumulation_images[0].handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &clear_color, 1, &accumulation_images[0].subresource_range); - api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT, accumulation_images[0]); + api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT, accumulation_images[0]); - api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, accumulation_images[1]); + api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_ACCESS_TRANSFER_WRITE_BIT, accumulation_images[1]); vkCmdClearColorImage(cmd_buf, accumulation_images[1].handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &clear_color, 1, &accumulation_images[1].subresource_range); - api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT, accumulation_images[1]); + api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT, accumulation_images[1]); } void vkrenderer::ui() { @@ -288,6 +288,10 @@ void vkrenderer::ui() { void vkrenderer::compute(uint32_t width, uint32_t height) { auto cmd_buf = command_buffers[virtual_frame_index]; + api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT, swapchain.images[swapchain_image_index]); + + api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_GENERAL, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT, accumulation_images[1]); + vkCmdPushConstants(cmd_buf, api.bindless_descriptor.pipeline_layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, 8, (void*)&scene_buffer_addr); vkCmdPushConstants(cmd_buf, api.bindless_descriptor.pipeline_layout, VK_SHADER_STAGE_COMPUTE_BIT, 8, 8, (void*)&bvh_buffer_addr); vkCmdPushConstants(cmd_buf, api.bindless_descriptor.pipeline_layout, VK_SHADER_STAGE_COMPUTE_BIT, 16, 8, (void*)&indices_buffer_addr); @@ -297,11 +301,13 @@ void vkrenderer::compute(uint32_t width, uint32_t height) { vkCmdPushConstants(cmd_buf, api.bindless_descriptor.pipeline_layout, VK_SHADER_STAGE_COMPUTE_BIT, 48, 8, (void*)&materials_buffer_addr); vkCmdPushConstants(cmd_buf, api.bindless_descriptor.pipeline_layout, VK_SHADER_STAGE_COMPUTE_BIT, 56, 4, (void*)&accumulation_images[0].bindless_storage_index); vkCmdPushConstants(cmd_buf, api.bindless_descriptor.pipeline_layout, VK_SHADER_STAGE_COMPUTE_BIT, 60, 4, (void*)&accumulation_images[1].bindless_storage_index); - api.run_compute_pipeline(cmd_buf, compute_pipeline, (width / 8) + 1, (height / 8) + 1, 1); + api.run_compute_pipeline(cmd_buf, compute_pipeline, (width / 8) + 1, (height / 8) + 1, 1); api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, swapchain.images[swapchain_image_index]); + api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_GENERAL, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT, accumulation_images[0]); + vkCmdPushConstants(cmd_buf, api.bindless_descriptor.pipeline_layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, 8, (void*)&scene_buffer_addr); vkCmdPushConstants(cmd_buf, api.bindless_descriptor.pipeline_layout, VK_SHADER_STAGE_COMPUTE_BIT, 56, 4, (void*)&swapchain.images[swapchain_image_index].bindless_storage_index); vkCmdPushConstants(cmd_buf, api.bindless_descriptor.pipeline_layout, VK_SHADER_STAGE_COMPUTE_BIT, 60, 4, (void*)&accumulation_images[0].bindless_storage_index); @@ -312,6 +318,8 @@ void vkrenderer::compute(uint32_t width, uint32_t height) { void vkrenderer::finish_frame() { auto cmd_buf = command_buffers[virtual_frame_index]; + api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_ACCESS_SHADER_WRITE_BIT, 0, swapchain.images[swapchain_image_index]); + api.end_record(cmd_buf); api.submit(cmd_buf, acquire_semaphores[virtual_frame_index], execution_semaphores[virtual_frame_index], submission_fences[virtual_frame_index]); |
