From f17c558dab2269c95289190cea1543d085dfa873 Mon Sep 17 00:00:00 2001
From: Alexander Gauggel <agauggel@uni-koblenz.de>
Date: Sat, 26 Jun 2021 14:54:19 +0200
Subject: [PATCH] [#82] Add lens dirt

---
 .../src/vkcv/asset/asset_loader.cpp           |  2 +-
 projects/voxelization/resources/lensDirt.jpg  |  3 +++
 .../shaders/bloomFlaresComposite.comp         | 20 ++++++++++++++++++-
 projects/voxelization/src/BloomAndFlares.cpp  | 13 ++++++++++--
 projects/voxelization/src/BloomAndFlares.hpp  |  2 +-
 5 files changed, 35 insertions(+), 5 deletions(-)
 create mode 100644 projects/voxelization/resources/lensDirt.jpg

diff --git a/modules/asset_loader/src/vkcv/asset/asset_loader.cpp b/modules/asset_loader/src/vkcv/asset/asset_loader.cpp
index 06329d80..a572fdaa 100644
--- a/modules/asset_loader/src/vkcv/asset/asset_loader.cpp
+++ b/modules/asset_loader/src/vkcv/asset/asset_loader.cpp
@@ -368,7 +368,7 @@ int loadScene(const std::string &path, Scene &scene){
 
 TextureData loadTexture(const std::filesystem::path& path) {
     TextureData texture;
-    uint8_t* data = stbi_load("resources/RadialLut.png", &texture.width, &texture.height, &texture.componentCount, 4);
+    uint8_t* data = stbi_load(path.string().c_str(), &texture.width, &texture.height, &texture.componentCount, 4);
     texture.data.resize(texture.width * texture.height * 4);
     memcpy(texture.data.data(), data, texture.data.size());
     return texture;
diff --git a/projects/voxelization/resources/lensDirt.jpg b/projects/voxelization/resources/lensDirt.jpg
new file mode 100644
index 00000000..f9415675
--- /dev/null
+++ b/projects/voxelization/resources/lensDirt.jpg
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:95982351ecf3d4d129d17612b40a8092944a285c0bffdd74d4886dd8489695ca
+size 107603
diff --git a/projects/voxelization/resources/shaders/bloomFlaresComposite.comp b/projects/voxelization/resources/shaders/bloomFlaresComposite.comp
index 2bf8a5e6..45d50fdc 100644
--- a/projects/voxelization/resources/shaders/bloomFlaresComposite.comp
+++ b/projects/voxelization/resources/shaders/bloomFlaresComposite.comp
@@ -7,6 +7,7 @@ layout(set=0, binding=2) uniform sampler                            linearSample
 layout(set=0, binding=3, r11f_g11f_b10f) uniform image2D            colorBuffer;
 layout(set=0, binding=4) uniform texture2D                          radialLUT;
 layout(set=0, binding=5) uniform sampler                            radialLUTSampler;
+layout(set=0, binding=6) uniform texture2D                          dirtTexture;
 
 layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
 
@@ -31,6 +32,19 @@ float starburst(vec2 uv){
     return mix(1, burst, falloff);
 }
 
+float getLensDirtWeight(vec2 uv){
+    vec2    targetTextureRes    = imageSize(colorBuffer);
+    float   targetAspectRatio   = targetTextureRes.x / targetTextureRes.y;
+    
+    vec2    dirtTextureRes    = textureSize(sampler2D(dirtTexture, linearSampler), 0);
+    float   dirtAspectRatio   = dirtTextureRes.x / dirtTextureRes.y;
+    
+    uv.x                        *= targetAspectRatio / dirtAspectRatio;
+    float   dirt                = texture(sampler2D(dirtTexture, radialLUTSampler), uv).r;
+    float   dirtStrength        = 0.75;
+    return mix(1, dirt, dirtStrength);
+}
+
 void main()
 {
     if(any(greaterThanEqual(gl_GlobalInvocationID.xy, imageSize(colorBuffer)))){
@@ -53,7 +67,11 @@ void main()
     float main_weight = 1 - (bloom_weight + lens_weight);
 
     lens_color *= starburst(UV);
-
+    
+    float lensDirtWeight = getLensDirtWeight(UV);
+    bloom_weight        *= lensDirtWeight;
+    lens_weight         *= lensDirtWeight;
+    
     composite_color.rgb = blur_color * bloom_weight +
                           lens_color * lens_weight  +
                           main_color * main_weight;
diff --git a/projects/voxelization/src/BloomAndFlares.cpp b/projects/voxelization/src/BloomAndFlares.cpp
index 03518b79..3e0066b2 100644
--- a/projects/voxelization/src/BloomAndFlares.cpp
+++ b/projects/voxelization/src/BloomAndFlares.cpp
@@ -2,6 +2,13 @@
 #include <vkcv/shader/GLSLCompiler.hpp>
 #include <vkcv/asset/asset_loader.hpp>
 
+vkcv::Image loadLenseDirtTexture(vkcv::Core* corePtr) {
+    const auto texture = vkcv::asset::loadTexture("resources/lensDirt.jpg");
+    vkcv::Image image = corePtr->createImage(vk::Format::eR8G8B8A8Unorm, texture.width, texture.height);
+    image.fill((void*)texture.data.data(), texture.data.size());
+    return image;
+}
+
 BloomAndFlares::BloomAndFlares(
         vkcv::Core *p_Core,
         vk::Format colorBufferFormat,
@@ -22,7 +29,8 @@ BloomAndFlares::BloomAndFlares(
             vkcv::SamplerAddressMode::REPEAT)),
         m_Blur(p_Core->createImage(colorBufferFormat, m_Width, m_Height, 1, true, true, false)),
         m_LensFeatures(p_Core->createImage(colorBufferFormat, m_Width, m_Height, 1, true, true, false)),
-        m_radialLut(p_Core->createImage(vk::Format::eR8G8B8A8Unorm, 128, 10, 1))
+        m_radialLut(p_Core->createImage(vk::Format::eR8G8B8A8Unorm, 128, 10, 1)),
+        m_lensDirt(loadLenseDirtTexture(p_Core))
 {
     vkcv::shader::GLSLCompiler compiler;
 
@@ -286,7 +294,8 @@ void BloomAndFlares::execCompositePipe(const vkcv::CommandStreamHandle &cmdStrea
     vkcv::DescriptorWrites compositeWrites;
     compositeWrites.sampledImageWrites = {vkcv::SampledImageDescriptorWrite(0, m_Blur.getHandle()),
                                           vkcv::SampledImageDescriptorWrite(1, m_LensFeatures.getHandle()),
-                                          vkcv::SampledImageDescriptorWrite(4, m_radialLut.getHandle()) };
+                                          vkcv::SampledImageDescriptorWrite(4, m_radialLut.getHandle()),
+                                          vkcv::SampledImageDescriptorWrite(6, m_lensDirt.getHandle()) };
     compositeWrites.samplerWrites = {vkcv::SamplerDescriptorWrite(2, m_LinearSampler),
                                      vkcv::SamplerDescriptorWrite(5, m_RadialLutSampler) };
     compositeWrites.storageImageWrites = {vkcv::StorageImageDescriptorWrite(3, colorAttachment)};
diff --git a/projects/voxelization/src/BloomAndFlares.hpp b/projects/voxelization/src/BloomAndFlares.hpp
index f86c75c4..2b410e5b 100644
--- a/projects/voxelization/src/BloomAndFlares.hpp
+++ b/projects/voxelization/src/BloomAndFlares.hpp
@@ -27,7 +27,7 @@ private:
     vkcv::Image m_LensFeatures;
 
     vkcv::Image m_radialLut;
-
+    vkcv::Image m_lensDirt;
 
     vkcv::PipelineHandle                     m_DownsamplePipe;
     std::vector<vkcv::DescriptorSetHandle>   m_DownsampleDescSets; // per mip desc set
-- 
GitLab