Skip to content
Snippets Groups Projects
Commit 9234ff14 authored by Alexander Gauggel's avatar Alexander Gauggel
Browse files

[#69] Add HDR rendering with tonemapping

parent c2e4a92d
No related branches found
No related tags found
1 merge request!56Resolve "Partikelsystem"
...@@ -24,7 +24,7 @@ if(MSVC) ...@@ -24,7 +24,7 @@ if(MSVC)
endif() endif()
# including headers of dependencies and the VkCV framework # including headers of dependencies and the VkCV framework
target_include_directories(particle_simulation SYSTEM BEFORE PRIVATE ${vkcv_include} ${vkcv_includes} ${vkcv_testing_include} ${vkcv_camera_include}) target_include_directories(particle_simulation SYSTEM BEFORE PRIVATE ${vkcv_include} ${vkcv_includes} ${vkcv_testing_include} ${vkcv_camera_include} ${vkcv_shader_compiler_include})
# linking with libraries from all dependencies and the VkCV framework # linking with libraries from all dependencies and the VkCV framework
target_link_libraries(particle_simulation vkcv vkcv_testing vkcv_camera) target_link_libraries(particle_simulation vkcv vkcv_testing vkcv_camera vkcv_shader_compiler)
#version 440
layout(set=0, binding=0, rgba16f) uniform image2D inImage;
layout(set=0, binding=1, rgba8) uniform image2D outImage;
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
void main(){
if(any(greaterThanEqual(gl_GlobalInvocationID.xy, imageSize(inImage)))){
return;
}
ivec2 uv = ivec2(gl_GlobalInvocationID.xy);
vec3 linearColor = imageLoad(inImage, uv).rgb;
vec3 tonemapped = linearColor / (linearColor + 1); // reinhard tonemapping
vec3 gammaCorrected = pow(tonemapped, vec3(1.f / 2.2f));
imageStore(outImage, uv, vec4(gammaCorrected, 0.f));
}
\ No newline at end of file
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <random> #include <random>
#include <glm/gtc/matrix_access.hpp> #include <glm/gtc/matrix_access.hpp>
#include <time.h> #include <time.h>
#include <vkcv/shader/GLSLCompiler.hpp>
int main(int argc, const char **argv) { int main(int argc, const char **argv) {
const char *applicationName = "Particlesystem"; const char *applicationName = "Particlesystem";
...@@ -36,12 +37,12 @@ int main(int argc, const char **argv) { ...@@ -36,12 +37,12 @@ int main(int argc, const char **argv) {
uint16_t indices[3] = {0, 1, 2}; uint16_t indices[3] = {0, 1, 2};
particleIndexBuffer.fill(&indices[0], sizeof(indices)); particleIndexBuffer.fill(&indices[0], sizeof(indices));
vk::Format colorFormat = vk::Format::eR16G16B16A16Sfloat;
// an example attachment for passes that output to the window // an example attachment for passes that output to the window
const vkcv::AttachmentDescription present_color_attachment( const vkcv::AttachmentDescription present_color_attachment(
vkcv::AttachmentOperation::STORE, vkcv::AttachmentOperation::STORE,
vkcv::AttachmentOperation::CLEAR, vkcv::AttachmentOperation::CLEAR,
core.getSwapchain().getFormat()); colorFormat);
vkcv::PassConfig particlePassDefinition({present_color_attachment}); vkcv::PassConfig particlePassDefinition({present_color_attachment});
...@@ -206,6 +207,19 @@ int main(int argc, const char **argv) { ...@@ -206,6 +207,19 @@ int main(int argc, const char **argv) {
cameraManager.getCamera(camIndex1).setPosition(glm::vec3(0.0f, 0.0f, -2.0f)); cameraManager.getCamera(camIndex1).setPosition(glm::vec3(0.0f, 0.0f, -2.0f));
cameraManager.getCamera(camIndex1).setCenter(glm::vec3(0.0f, 0.0f, 0.0f)); cameraManager.getCamera(camIndex1).setCenter(glm::vec3(0.0f, 0.0f, 0.0f));
vkcv::Image colorBuffer = core.createImage(colorFormat, windowWidth, windowHeight, 1, false, true, true);
vkcv::ShaderProgram tonemappingShader;
vkcv::shader::GLSLCompiler compiler;
compiler.compile(vkcv::ShaderStage::COMPUTE, "shaders/tonemapping.comp", [&](vkcv::ShaderStage shaderStage, const std::filesystem::path& path) {
tonemappingShader.addShader(shaderStage, path);
});
vkcv::DescriptorSetHandle tonemappingDescriptor = core.createDescriptorSet(tonemappingShader.getReflectedDescriptors()[0]);
vkcv::PipelineHandle tonemappingPipe = core.createComputePipeline(
tonemappingShader,
{ core.getDescriptorSet(tonemappingDescriptor).layout });
std::uniform_real_distribution<float> rdm = std::uniform_real_distribution<float>(0.95f, 1.05f); std::uniform_real_distribution<float> rdm = std::uniform_real_distribution<float>(0.95f, 1.05f);
std::default_random_engine rdmEngine; std::default_random_engine rdmEngine;
while (window.isWindowOpen()) { while (window.isWindowOpen()) {
...@@ -252,7 +266,30 @@ int main(int argc, const char **argv) { ...@@ -252,7 +266,30 @@ int main(int argc, const char **argv) {
particlePipeline, particlePipeline,
pushConstantDataDraw, pushConstantDataDraw,
{drawcalls}, {drawcalls},
{swapchainInput}); { colorBuffer.getHandle() });
core.prepareImageForStorage(cmdStream, colorBuffer.getHandle());
core.prepareImageForStorage(cmdStream, swapchainInput);
vkcv::DescriptorWrites tonemappingDescriptorWrites;
tonemappingDescriptorWrites.storageImageWrites = {
vkcv::StorageImageDescriptorWrite(0, colorBuffer.getHandle()),
vkcv::StorageImageDescriptorWrite(1, swapchainInput)
};
core.writeDescriptorSet(tonemappingDescriptor, tonemappingDescriptorWrites);
uint32_t tonemappingDispatchCount[3];
tonemappingDispatchCount[0] = std::ceil(windowWidth / 8.f);
tonemappingDispatchCount[1] = std::ceil(windowHeight / 8.f);
tonemappingDispatchCount[2] = 1;
core.recordComputeDispatchToCmdStream(
cmdStream,
tonemappingPipe,
tonemappingDispatchCount,
{vkcv::DescriptorSetUsage(0, core.getDescriptorSet(tonemappingDescriptor).vulkanHandle) },
vkcv::PushConstantData(nullptr, 0));
core.prepareSwapchainImageForPresent(cmdStream); core.prepareSwapchainImageForPresent(cmdStream);
core.submitCommandStream(cmdStream); core.submitCommandStream(cmdStream);
core.endFrame(); core.endFrame();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment