diff options
| author | ladevieq <ladevie.quentin@gmail.com> | 2021-03-11 22:32:41 +0100 |
|---|---|---|
| committer | ladevieq <ladevie.quentin@gmail.com> | 2021-03-11 22:32:41 +0100 |
| commit | 5812e12d804fc47e1260e52a92e39705e3056364 (patch) | |
| tree | 51357f167872b2e673721b8c30300d716109e04c /src | |
| parent | 7e25e65c44864f5e2c1f8227b41ceb6c0fc5e54f (diff) | |
Fix accumulation textures format and reset accumulation on move/resize
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.cpp | 21 | ||||
| -rw-r--r-- | src/utils.cpp | 2 | ||||
| -rw-r--r-- | src/vk-renderer.cpp | 14 |
3 files changed, 29 insertions, 8 deletions
diff --git a/src/main.cpp b/src/main.cpp index 0ca6a0a..a59254d 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 = 500; window wnd { width , height }; @@ -56,11 +56,13 @@ int main() { auto start = std::chrono::high_resolution_clock::now(); auto end = std::chrono::high_resolution_clock::now(); auto inputs = create_inputs(width, height, samples_per_pixel); - auto canRender = true; + auto can_render = true; vkrenderer renderer { wnd, inputs }; while(wnd.isOpen) { + auto reset_accumulation = false; + end = std::chrono::high_resolution_clock::now(); delta_time = std::chrono::duration<float, std::milli>(end - start).count(); @@ -85,13 +87,16 @@ int main() { io.DisplaySize.y = (float)height; if (event.width == 0 && event.height == 0) { - canRender = false; + can_render = false; } else { - canRender = true; + can_render = true; renderer.recreate_swapchain(); ((input_data*) renderer.compute_shader_buffer.alloc_info.pMappedData)->cam.set_aspect_ratio((float)event.width / (float)event.height); ((input_data*) renderer.compute_shader_buffer.alloc_info.pMappedData)->sample_index = 0; } + + reset_accumulation = true; + break; } case EVENT_TYPES::KEY_PRESS: { @@ -126,7 +131,7 @@ int main() { ((input_data*) renderer.compute_shader_buffer.alloc_info.pMappedData)->cam.move(move_vec); if (!move_vec.near_zero()) { - ((input_data*) renderer.compute_shader_buffer.alloc_info.pMappedData)->sample_index = 0; + reset_accumulation = true; } break; @@ -177,9 +182,13 @@ int main() { // See the documentation below for a longer explanation // if(rdoc_api) rdoc_api->StartFrameCapture(NULL, NULL); - if (canRender) { + if (can_render) { renderer.begin_frame(); + if (reset_accumulation) { + renderer.reset_accumulation(); + } + renderer.compute(width, height); ((input_data*) renderer.compute_shader_buffer.alloc_info.pMappedData)->sample_index = frame_count % 1000; diff --git a/src/utils.cpp b/src/utils.cpp index 3b7c445..5ed2c17 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -97,7 +97,7 @@ struct input_data create_inputs(uint32_t width, uint32_t height, uint32_t sample // Image dimensions const float aspect_ratio = (float)width / (float) height; - const uint32_t max_depth = 20; + const uint32_t max_depth = 50; const vec3 camera_position{ 13.0, 2.0, 3.0 }; const vec3 camera_target{ 0.0, 0.0, 0.0 }; diff --git a/src/vk-renderer.cpp b/src/vk-renderer.cpp index 8c225fb..dfd2bca 100644 --- a/src/vk-renderer.cpp +++ b/src/vk-renderer.cpp @@ -321,6 +321,18 @@ void vkrenderer::begin_frame() { } +void vkrenderer::reset_accumulation() { + auto cmd_buf = command_buffers[frame_index]; + auto accumulation_image = accumulation_images[1]; + 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_image); + + vkCmdClearColorImage(cmd_buf, accumulation_image.handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &clear_color, 1, &accumulation_image.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_image); +} + void vkrenderer::ui() { ImDrawData* draw_data = ImGui::GetDrawData(); @@ -407,7 +419,7 @@ void vkrenderer::recreate_swapchain() { .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); auto cmd_buf = command_buffers[frame_index]; api.start_record(cmd_buf); |
