From 214c32a5475ec302b0caa837ea4d241ece263174 Mon Sep 17 00:00:00 2001 From: ladevieq Date: Wed, 10 Mar 2021 21:59:56 +0100 Subject: Fix TAA to have infinite accumulation --- include/vk-renderer.hpp | 2 +- shaders/compute.comp | 6 ++++-- src/main.cpp | 11 +++-------- src/vk-renderer.cpp | 45 ++++++++++++++++++++++++--------------------- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/include/vk-renderer.hpp b/include/vk-renderer.hpp index 5dc6c97..5308c3b 100644 --- a/include/vk-renderer.hpp +++ b/include/vk-renderer.hpp @@ -19,7 +19,7 @@ class vkrenderer { void ui(); - void compute(uint32_t width, uint32_t height, bool clear, bool render_sample); + void compute(uint32_t width, uint32_t height); void finish_frame(); diff --git a/shaders/compute.comp b/shaders/compute.comp index 411c520..a47b9ce 100644 --- a/shaders/compute.comp +++ b/shaders/compute.comp @@ -218,16 +218,18 @@ void main() { vec2 uv = vec2(float(x) / (width - 1), 1.0 - (float(y) / (height - 1))); + sample_index %= 1000; + vec2 jittered_uvs = uv + vec2(random_disk[sample_index].x / (width - 1), random_disk[sample_index].y / (height - 1)); vec2 lens_disk = random_disk[sample_index] * cam.lens_radius; vec3 offset = lens_disk.x * cam.right.xyz + lens_disk.y * cam.up.xyz; vec4 proj_plane_pos = cam.first_pixel + jittered_uvs.x * cam.horizontal + jittered_uvs.y * cam.vertical; - ray r = ray(cam.position.xyz + offset, proj_plane_pos.xyz - cam.position.xyz - offset); + ray r = ray(cam.position.xyz, proj_plane_pos.xyz - cam.position.xyz); vec4 out_color = ray_color(r, sample_index); float spp_scale = (1.0 / samples_per_pixel); vec4 acc_color = imageLoad(accumulation_image, ivec2(x, y)); - imageStore(output_image, ivec2(x, y), acc_color + out_color * spp_scale); + imageStore(output_image, ivec2(x, y), mix(acc_color, out_color, spp_scale)); } diff --git a/src/main.cpp b/src/main.cpp index d541bb6..fd582a5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,7 +47,7 @@ int main() { const float aspect_ratio = 16.0 / 9.0; uint32_t width = 400; uint32_t height = width / aspect_ratio; - const uint32_t samples_per_pixel = 100; + const uint32_t samples_per_pixel = 25; window wnd { width , height }; @@ -179,14 +179,9 @@ int main() { if (canRender) { renderer.begin_frame(); - if (((input_data*) renderer.compute_shader_buffer.alloc_info.pMappedData)->sample_index < samples_per_pixel) { - bool clear = ((input_data*) renderer.compute_shader_buffer.alloc_info.pMappedData)->sample_index == 0; - renderer.compute(width, height, clear, true); + renderer.compute(width, height); - ((input_data*) renderer.compute_shader_buffer.alloc_info.pMappedData)->sample_index++; - } else { - renderer.compute(width, height, false, false); - } + ((input_data*) renderer.compute_shader_buffer.alloc_info.pMappedData)->sample_index++; renderer.ui(); diff --git a/src/vk-renderer.cpp b/src/vk-renderer.cpp index d8c84cf..d13ba02 100644 --- a/src/vk-renderer.cpp +++ b/src/vk-renderer.cpp @@ -69,7 +69,7 @@ vkrenderer::vkrenderer(window& wnd, const input_data& inputs) { .height = swapchain.extent.height, .depth = 1, }; - accumulation_images = api.create_images(image_size, swapchain.surface_format.format, VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, 2); + accumulation_images = api.create_images(image_size, VK_FORMAT_R32G32B32A32_SFLOAT, VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, 2); submission_fences = api.create_fences(swapchain.image_count); execution_semaphores = api.create_semaphores(swapchain.image_count); @@ -141,9 +141,13 @@ vkrenderer::vkrenderer(window& wnd, const input_data& inputs) { VKRESULT(vkResetFences(api.context.device, 1, &submission_fences[frame_index])) + // Initialization frame auto cmd_buf = command_buffers[frame_index]; api.start_record(cmd_buf); + api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, accumulation_images[0]); + api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, accumulation_images[1]); + 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); VkImageSubresourceLayers image_subresource_layers = {}; @@ -333,33 +337,18 @@ void vkrenderer::ui() { api.end_render_pass(cmd_buf); } -void vkrenderer::compute(uint32_t width, uint32_t height, bool clear, bool render_sample) { +void vkrenderer::compute(uint32_t width, uint32_t height) { auto cmd_buf = command_buffers[frame_index]; auto output_image = accumulation_images[0]; auto accumulation_image = accumulation_images[1]; - if (render_sample) { - if (clear) { - api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_ACCESS_TRANSFER_WRITE_BIT, accumulation_image); - - VkClearColorValue color = { 0.f, 0.f, 0.f, 1.f }; - vkCmdClearColorImage(cmd_buf, accumulation_image.handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &color, 1, &accumulation_image.subresource_range); + api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, accumulation_image); + api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, VK_ACCESS_SHADER_WRITE_BIT, output_image); - 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_image); - - api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, VK_ACCESS_SHADER_WRITE_BIT, output_image); - } else { - api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_ACCESS_TRANSFER_READ_BIT, VK_ACCESS_SHADER_READ_BIT, accumulation_image); - api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, VK_ACCESS_SHADER_WRITE_BIT, output_image); - } - - api.run_compute_pipeline(cmd_buf, compute_pipeline, compute_shader_sets[swapchain_image_index], width / 8, height / 8, 1); - - 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, output_image); - - } + api.run_compute_pipeline(cmd_buf, compute_pipeline, compute_shader_sets[swapchain_image_index], width / 8, height / 8, 1); + 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, output_image); api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_ACCESS_TRANSFER_WRITE_BIT, swapchain.images[swapchain_image_index]); api.blit_full(cmd_buf, output_image, swapchain.images[swapchain_image_index]); @@ -398,6 +387,20 @@ void vkrenderer::recreate_swapchain() { }; accumulation_images = api.create_images(image_size, swapchain.surface_format.format, VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, 2); + auto cmd_buf = command_buffers[frame_index]; + api.start_record(cmd_buf); + + api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, accumulation_images[0]); + api.image_barrier(cmd_buf, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, accumulation_images[1]); + + api.end_record(cmd_buf); + + VKRESULT(vkResetFences(api.context.device, 1, &submission_fences[0])) + + api.submit(cmd_buf, VK_NULL_HANDLE, VK_NULL_HANDLE, submission_fences[0]); + + VKRESULT(vkWaitForFences(api.context.device, 1, &submission_fences[0], VK_TRUE, UINT64_MAX)) + for (auto& framebuffer: framebuffers) { api.destroy_framebuffer(framebuffer); } -- cgit v1.3