aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorladevieq <ladevie.quentin@gmail.com>2021-03-10 22:37:36 +0100
committerladevieq <ladevie.quentin@gmail.com>2021-03-10 22:37:36 +0100
commit66936e950029869d84b26d9224674c9060e1d96f (patch)
tree398eb837806a0f45d908ae11f2d61ee7a2958bc9
parent214c32a5475ec302b0caa837ea4d241ece263174 (diff)
Add naive tonemapping
-rw-r--r--include/vk-renderer.hpp4
-rw-r--r--shaders/tonemapping.comp12
-rw-r--r--src/main.cpp7
-rw-r--r--src/vk-renderer.cpp56
4 files changed, 60 insertions, 19 deletions
diff --git a/include/vk-renderer.hpp b/include/vk-renderer.hpp
index 5308c3b..7da3784 100644
--- a/include/vk-renderer.hpp
+++ b/include/vk-renderer.hpp
@@ -47,6 +47,10 @@ class vkrenderer {
std::vector<VkDescriptorSet> compute_shader_sets;
Pipeline compute_pipeline;
+ std::vector<VkDescriptorSetLayoutBinding> tonemapping_sets_bindings;
+ std::vector<VkDescriptorSet> tonemapping_shader_sets;
+ Pipeline tonemapping_pipeline;
+
std::vector<VkDescriptorSetLayoutBinding> ui_sets_bindings;
std::vector<Buffer> ui_transforms_buffers;
std::vector<Buffer> ui_vertex_buffers;
diff --git a/shaders/tonemapping.comp b/shaders/tonemapping.comp
new file mode 100644
index 0000000..dde91b8
--- /dev/null
+++ b/shaders/tonemapping.comp
@@ -0,0 +1,12 @@
+layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
+
+layout(set = 0, binding = 0, rgba8) uniform writeonly image2D output_image;
+layout(set = 0, binding = 1, rgba8) uniform readonly image2D accumulation_image;
+
+void main() {
+ uint x = gl_GlobalInvocationID.x;
+ uint y = gl_GlobalInvocationID.y;
+
+ vec4 color = imageLoad(accumulation_image, ivec2(x, y));
+ imageStore(output_image, ivec2(x, y), sqrt(color));
+}
diff --git a/src/main.cpp b/src/main.cpp
index fd582a5..0ca6a0a 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -47,11 +47,12 @@ 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 = 25;
+ const uint32_t samples_per_pixel = 100;
window wnd { width , height };
float delta_time = 0.f;
+ uint64_t frame_count = 0;
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);
@@ -181,7 +182,7 @@ int main() {
renderer.compute(width, height);
- ((input_data*) renderer.compute_shader_buffer.alloc_info.pMappedData)->sample_index++;
+ ((input_data*) renderer.compute_shader_buffer.alloc_info.pMappedData)->sample_index = frame_count % 1000;
renderer.ui();
@@ -190,6 +191,8 @@ int main() {
// stop the capture
// if(rdoc_api) rdoc_api->EndFrameCapture(NULL, NULL);
+
+ frame_count++;
}
//-------------------------
diff --git a/src/vk-renderer.cpp b/src/vk-renderer.cpp
index d13ba02..8c225fb 100644
--- a/src/vk-renderer.cpp
+++ b/src/vk-renderer.cpp
@@ -32,7 +32,8 @@ vkrenderer::vkrenderer(window& wnd, const input_data& inputs) {
platform_surface,
min_swapchain_image_count,
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
- VK_IMAGE_USAGE_TRANSFER_DST_BIT,
+ VK_IMAGE_USAGE_TRANSFER_DST_BIT |
+ VK_IMAGE_USAGE_STORAGE_BIT,
std::nullopt
);
@@ -62,6 +63,25 @@ vkrenderer::vkrenderer(window& wnd, const input_data& inputs) {
compute_pipeline = api.create_compute_pipeline("compute", compute_sets_bindings);
compute_shader_sets = api.create_descriptor_sets(compute_pipeline.descriptor_set_layout, swapchain.image_count);
+ tonemapping_sets_bindings = {
+ {
+ .binding = 0,
+ .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
+ .descriptorCount = 1,
+ .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
+ .pImmutableSamplers = VK_NULL_HANDLE,
+ },
+ {
+ .binding = 1,
+ .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
+ .descriptorCount = 1,
+ .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
+ .pImmutableSamplers = VK_NULL_HANDLE,
+ }
+ };
+ tonemapping_pipeline = api.create_compute_pipeline("tonemapping", tonemapping_sets_bindings);
+ tonemapping_shader_sets = api.create_descriptor_sets(tonemapping_pipeline.descriptor_set_layout, swapchain.image_count);
+
command_buffers = api.create_command_buffers(swapchain.image_count);
VkExtent3D image_size = {
@@ -83,7 +103,7 @@ vkrenderer::vkrenderer(window& wnd, const input_data& inputs) {
}
std::vector<VkFormat> attachments_format { swapchain.surface_format.format };
- render_pass = api.create_render_pass(attachments_format, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
+ render_pass = api.create_render_pass(attachments_format, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
for (auto& image : swapchain.images) {
std::vector<Image> attachments { image };
@@ -145,8 +165,8 @@ vkrenderer::vkrenderer(window& wnd, const input_data& inputs) {
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_GENERAL, 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_GENERAL, 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);
@@ -202,7 +222,6 @@ vkrenderer::~vkrenderer() {
api.destroy_semaphores(execution_semaphores);
api.destroy_semaphores(acquire_semaphores);
- api.destroy_descriptor_sets(compute_shader_sets);
api.destroy_images(accumulation_images);
api.destroy_render_pass(render_pass);
@@ -228,9 +247,15 @@ vkrenderer::~vkrenderer() {
}
api.destroy_command_buffers(command_buffers);
+
api.destroy_pipeline(compute_pipeline);
+ api.destroy_pipeline(tonemapping_pipeline);
api.destroy_pipeline(ui_pipeline);
+ api.destroy_descriptor_sets(compute_shader_sets);
+ api.destroy_descriptor_sets(tonemapping_shader_sets);
+ api.destroy_descriptor_sets(ui_sets);
+
api.destroy_swapchain(swapchain);
api.destroy_surface(platform_surface);
}
@@ -289,6 +314,9 @@ void vkrenderer::begin_frame() {
api.update_descriptor_set_image(compute_shader_sets[swapchain_image_index], compute_sets_bindings[1], output_image.view, VK_NULL_HANDLE);
api.update_descriptor_set_image(compute_shader_sets[swapchain_image_index], compute_sets_bindings[2], accumulation_image.view, VK_NULL_HANDLE);
+ api.update_descriptor_set_image(tonemapping_shader_sets[swapchain_image_index], tonemapping_sets_bindings[0], swapchain.images[swapchain_image_index].view, VK_NULL_HANDLE);
+ api.update_descriptor_set_image(tonemapping_shader_sets[swapchain_image_index], tonemapping_sets_bindings[1], output_image.view, VK_NULL_HANDLE);
+
api.start_record(cmd_buf);
}
@@ -340,18 +368,12 @@ void vkrenderer::ui() {
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];
-
- 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.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]);
+ 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.run_compute_pipeline(cmd_buf, tonemapping_pipeline, tonemapping_shader_sets[swapchain_image_index], width / 8, height / 8, 1);
}
void vkrenderer::finish_frame() {
@@ -375,7 +397,7 @@ void vkrenderer::recreate_swapchain() {
swapchain = api.create_swapchain(
platform_surface,
min_swapchain_image_count,
- VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT,
+ VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_STORAGE_BIT,
std::optional<std::reference_wrapper<Swapchain>> { std::ref(swapchain) }
);
@@ -390,8 +412,8 @@ void vkrenderer::recreate_swapchain() {
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_GENERAL, 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_GENERAL, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, accumulation_images[1]);
api.end_record(cmd_buf);