aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorladevieq <ladevie.quentin@gmail.com>2021-03-11 22:32:41 +0100
committerladevieq <ladevie.quentin@gmail.com>2021-03-11 22:32:41 +0100
commit5812e12d804fc47e1260e52a92e39705e3056364 (patch)
tree51357f167872b2e673721b8c30300d716109e04c
parent7e25e65c44864f5e2c1f8227b41ceb6c0fc5e54f (diff)
Fix accumulation textures format and reset accumulation on move/resize
-rw-r--r--include/vk-renderer.hpp2
-rw-r--r--shaders/compute.comp6
-rw-r--r--src/main.cpp21
-rw-r--r--src/utils.cpp2
-rw-r--r--src/vk-renderer.cpp14
5 files changed, 34 insertions, 11 deletions
diff --git a/include/vk-renderer.hpp b/include/vk-renderer.hpp
index 7da3784..630588f 100644
--- a/include/vk-renderer.hpp
+++ b/include/vk-renderer.hpp
@@ -17,6 +17,8 @@ class vkrenderer {
void begin_frame();
+ void reset_accumulation();
+
void ui();
void compute(uint32_t width, uint32_t height);
diff --git a/shaders/compute.comp b/shaders/compute.comp
index cc33aab..b70b021 100644
--- a/shaders/compute.comp
+++ b/shaders/compute.comp
@@ -69,8 +69,8 @@ layout(set = 0, binding = 0) buffer Inputs {
uint[3] padding;
};
-layout(set = 0, binding = 1, rgba8) uniform writeonly image2D output_image;
-layout(set = 0, binding = 2, rgba8) uniform readonly image2D accumulation_image;
+layout(set = 0, binding = 1, rgba32f) uniform writeonly image2D output_image;
+layout(set = 0, binding = 2, rgba32f) uniform readonly image2D accumulation_image;
vec3 at(ray r, float t) {
return r.origin + r.direction * t;
@@ -231,5 +231,5 @@ void main() {
float spp_scale = (1.0 / samples_per_pixel);
vec4 acc_color = imageLoad(accumulation_image, ivec2(x, y));
- imageStore(output_image, ivec2(x, y), mix(acc_color, out_color, spp_scale));
+ imageStore(output_image, ivec2(x, y), vec4(mix(acc_color, out_color, spp_scale)));
}
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);