Commit 7618de36 authored by Lukas Tietze's avatar Lukas Tietze

Post-Processing

parent 2e02858a
......@@ -59,6 +59,7 @@ add_library(
src/Lib/src/Util/FileIo.cpp
src/Lib/src/Util/Logging.cpp
src/Lib/src/Util/VectorConversions.cpp
src/Lib/src/Util/PerlinGenerator.cpp
src/Lib/src/VkUtil/CompileShader.cpp
src/Lib/src/VkUtil/AccelerationStructureBase.cpp
......
......@@ -7,5 +7,48 @@ layout(location = 0) out vec4 color;
layout(binding = 0) uniform sampler2D texSampler;
void main() {
color = vec4(texture(texSampler, texCoord).rgb, 1.0);
vec2 offsets[9] = {
{-1, 1}, {0, 1}, {1, 1},
{-1, 0}, {0, 0}, {1, 0},
{-1, -1}, {0, -1}, {1, -1},
};
float edgeDetectKernel[9] = {
0, 1, 0,
1, -4, 1,
0, 1, 0,
};
float blurKernel[9] = {
1.0 / 84.0, 4.0 / 84.0, 1.0 / 84.0,
4.0 / 84.0, 16.0 / 84.0, 4.0 / 84.0,
1.0 / 84.0, 4.0 / 84.0, 1.0 / 84.0,
};
vec2 relativePixelSize = vec2(1.0) / textureSize(texSampler, 0);
vec3 samples[9];
for(int i = 0; i < 9; i++) {
samples[i] = texture(texSampler, texCoord + offsets[i] * relativePixelSize).rgb;
}
vec3 edge = vec3(0);
for(int i = 0; i < 9; i++) {
edge += samples[i] * edgeDetectKernel[i];
}
vec3 blur = vec3(0);
for(int i = 0; i < 9; i++) {
blur += samples[i] * blurKernel[i];
}
float l = length(edge);
vec3 res = vec3(0);
res = l * blur + (1 - l) * samples[5];
color = vec4(res, 1.0);
}
\ No newline at end of file
#version 450
// #extension GL_GOOGLE_include_directive : enable
// #extension GL_ARB_separate_shader_objects : enable
#extension GL_GOOGLE_include_directive : enable
#extension GL_ARB_separate_shader_objects : enable
layout(location = 0) out vec2 texCoord;
......
#pragma once
#include <random>
namespace lib
{
class PerlinGenerator
{
private:
uint32_t width;
uint32_t height;
std::default_random_engine engine;
std::uniform_real_distribution<float> rnd;
// std::vector<> grid;
double perlin(double x, double y) const;
public:
PerlinGenerator(uint32_t w, uint32_t h, uint32_t seed);
uint32_t operator()(uint32_t x, uint32_t y) const;
};
} // namespace lib
......@@ -88,7 +88,7 @@ bool lib::CgContext::TryCreateRasterizerPipeline()
rasterizationStateCreateInfo.rasterizerDiscardEnable = false;
rasterizationStateCreateInfo.polygonMode = vk::PolygonMode::eFill;
rasterizationStateCreateInfo.lineWidth = 1.f;
rasterizationStateCreateInfo.frontFace = vk::FrontFace::eCounterClockwise;
rasterizationStateCreateInfo.frontFace = vk::FrontFace::eClockwise;
rasterizationStateCreateInfo.cullMode = vk::CullModeFlagBits::eBack;
rasterizationStateCreateInfo.depthBiasEnable = false;
......@@ -175,12 +175,12 @@ void lib::CgContext::CreateRasterizationDescriptorSets()
frame.rasterizationDescriptorSet = *setsIt++;
vk::DescriptorImageInfo rtImageInfo;
rtImageInfo.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
// rtImageInfo.imageView = frame.raytracingTexture->GetView();
// rtImageInfo.sampler = frame.raytracingTexture->GetSampler();
rtImageInfo.imageLayout = vk::ImageLayout::eGeneral;
rtImageInfo.imageView = frame.raytracingTexture->GetView();
rtImageInfo.sampler = frame.raytracingTexture->GetSampler();
rtImageInfo.imageView = this->dummyTexture->GetView();
rtImageInfo.sampler = this->dummyTexture->GetSampler();
// rtImageInfo.imageView = this->dummyTexture->GetView();
// rtImageInfo.sampler = this->dummyTexture->GetSampler();
vk::WriteDescriptorSet rtImageWrite;
rtImageWrite.descriptorCount = 1;
......@@ -237,7 +237,7 @@ void lib::CgContext::CreateRasterizationRenderPass()
{
vk::AttachmentDescription colorAttachment;
colorAttachment.initialLayout = vk::ImageLayout::eUndefined;
colorAttachment.finalLayout = vk::ImageLayout::ePresentSrcKHR;
colorAttachment.finalLayout = vk::ImageLayout::eColorAttachmentOptimal;
colorAttachment.loadOp = vk::AttachmentLoadOp::eDontCare;
colorAttachment.storeOp = vk::AttachmentStoreOp::eStore;
colorAttachment.format = this->swapChainImageFormat;
......
......@@ -740,61 +740,56 @@ void lib::CgContext::UpdateCommandBuffer(uint32_t imageIndex)
frame.commandBuffer.begin(beginInfo);
// if (this->IsOk())
// {
// frame.commandBuffer.bindPipeline(vk::PipelineBindPoint::eRayTracingNV, this->raytracingStep.pipeline);
// frame.commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eRayTracingNV,
// this->raytracingStep.pipelineLayout,
// 0, frame.raytracingDescriptorSet,
// {});
// TransitionImageLayout(frame.commandBuffer,
// frame.raytracingTexture->GetHandle(), this->swapChainImageFormat,
// vk::ImageLayout::eUndefined, vk::ImageLayout::eGeneral,
// vk::PipelineStageFlagBits::eTopOfPipe,
// vk::PipelineStageFlagBits::eRayTracingShaderNV);
// frame.ubo->ScheduleTransfer(frame.commandBuffer);
// frame.commandBuffer.pushConstants(this->raytracingStep.pipelineLayout,
// vk::ShaderStageFlagBits::eRaygenNV | vk::ShaderStageFlagBits::eClosestHitNV,
// 0, sizeof(this->pushConstant),
// &this->pushConstant);
// auto sbtEntrySize = this->physicalDeviceInfo.GetRaytracingProperties().shaderGroupBaseAlignment;
// frame.commandBuffer.traceRaysNV(this->shaderBindingTableBuffer->GetBufferHandle(), this->shaderOffsets.rayGen * sbtEntrySize,
// this->shaderBindingTableBuffer->GetBufferHandle(), this->shaderOffsets.miss * sbtEntrySize, sbtEntrySize,
// this->shaderBindingTableBuffer->GetBufferHandle(), this->shaderOffsets.hitGroup * sbtEntrySize, sbtEntrySize,
// this->shaderBindingTableBuffer->GetBufferHandle(), 0, 0,
// this->swapChainExtent.width, this->swapChainExtent.height, 1);
// }
if (this->IsOk())
{
frame.commandBuffer.bindPipeline(vk::PipelineBindPoint::eRayTracingNV, this->raytracingStep.pipeline);
frame.commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eRayTracingNV,
this->raytracingStep.pipelineLayout,
0, frame.raytracingDescriptorSet,
{});
TransitionImageLayout(frame.commandBuffer,
frame.raytracingTexture->GetHandle(), this->swapChainImageFormat,
vk::ImageLayout::eUndefined, vk::ImageLayout::eGeneral,
vk::PipelineStageFlagBits::eTopOfPipe,
vk::PipelineStageFlagBits::eRayTracingShaderNV);
frame.ubo->ScheduleTransfer(frame.commandBuffer);
frame.commandBuffer.pushConstants(this->raytracingStep.pipelineLayout,
vk::ShaderStageFlagBits::eRaygenNV | vk::ShaderStageFlagBits::eClosestHitNV,
0, sizeof(this->pushConstant),
&this->pushConstant);
auto sbtEntrySize = this->physicalDeviceInfo.GetRaytracingProperties().shaderGroupBaseAlignment;
frame.commandBuffer.traceRaysNV(this->shaderBindingTableBuffer->GetBufferHandle(), this->shaderOffsets.rayGen * sbtEntrySize,
this->shaderBindingTableBuffer->GetBufferHandle(), this->shaderOffsets.miss * sbtEntrySize, sbtEntrySize,
this->shaderBindingTableBuffer->GetBufferHandle(), this->shaderOffsets.hitGroup * sbtEntrySize, sbtEntrySize,
this->shaderBindingTableBuffer->GetBufferHandle(), 0, 0,
this->swapChainExtent.width, this->swapChainExtent.height, 1);
}
// TransitionImageLayout(frame.commandBuffer,
// frame.raytracingTexture->GetHandle(), this->swapChainImageFormat,
// vk::ImageLayout::eGeneral, vk::ImageLayout::eShaderReadOnlyOptimal,
// vk::PipelineStageFlagBits::eRayTracingShaderNV, vk::PipelineStageFlagBits::eAllGraphics);
TransitionImageLayout(frame.commandBuffer,
frame.swapChainImage, this->swapChainImageFormat,
vk::ImageLayout::eUndefined, vk::ImageLayout::eColorAttachmentOptimal,
vk::PipelineStageFlagBits::eRayTracingShaderNV, vk::PipelineStageFlagBits::eAllGraphics);
if (this->IsOk())
{
// vk::RenderPassBeginInfo ppBeginInfo;
// ppBeginInfo.renderPass = this->rasterizationStep.renderPass;
// ppBeginInfo.framebuffer = frame.swapChainFramebufferForRasterizationRenderPass;
// ppBeginInfo.renderArea.offset = {0, 0};
// ppBeginInfo.renderArea.extent = this->swapChainExtent;
// frame.commandBuffer.beginRenderPass(ppBeginInfo, vk::SubpassContents::eInline);
// frame.commandBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, this->rasterizationStep.pipeline);
// frame.commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics,
// this->rasterizationStep.pipelineLayout,
// 0, frame.rasterizationDescriptorSet, {});
// frame.commandBuffer.draw(3, 1, 0, 0);
// frame.commandBuffer.endRenderPass();
vk::RenderPassBeginInfo ppBeginInfo;
ppBeginInfo.renderPass = this->rasterizationStep.renderPass;
ppBeginInfo.framebuffer = frame.swapChainFramebufferForRasterizationRenderPass;
ppBeginInfo.renderArea.offset = {0, 0};
ppBeginInfo.renderArea.extent = this->swapChainExtent;
frame.commandBuffer.beginRenderPass(ppBeginInfo, vk::SubpassContents::eInline);
frame.commandBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, this->rasterizationStep.pipeline);
frame.commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics,
this->rasterizationStep.pipelineLayout,
0, frame.rasterizationDescriptorSet, {});
frame.commandBuffer.draw(3, 1, 0, 0);
frame.commandBuffer.endRenderPass();
}
if (this->imGui.show)
......
#include "Util/PerlinGenerator.hpp"
#include "glm/glm.hpp"
#include <cmath>
#include <array>
namespace
{
double interpolate(double a, double b, double weight)
{
// TODO kann optimiert werden
auto trueWeight = 6.0 * std::pow(weight, 5.0) - 15.0 * std::pow(weight, 4.0) + 10.0 * std::pow(weight, 3.0);
return trueWeight * a + (1 - trueWeight) * b;
}
} // namespace
double lib::PerlinGenerator::perlin(double x, double y) const
{
// glm::vec2 point{std::fmod(x, 1.f), std::fmod(y, 1.f)};
// std::array<glm::vec2, 4> gradients;
// for (auto &gradient : gradients)
// {
// gradient.x = this->rnd(this->engine);
// gradient.y = this->rnd(this->engine);
// }
// static constexpr std::array<glm::vec2, 4> corners = {
// glm::vec2{0.f, 0.f},
// glm::vec2{0.f, 1.f},
// glm::vec2{1.f, 1.f},
// glm::vec2{1.f, 0.f},
// };
// std::array<float, 4> dotValues;
// for (size_t i = 0; i < dotValues.size(); i++)
// {
// dotValues[i] = glm::dot(point - corners[i], gradients[i]);
// }
// return interpolate(interpolate(dotValues[0], dotValues[1], point.x),
// interpolate(dotValues[2], dotValues[3], point.x),
// point.y);
return 0.0;
}
lib::PerlinGenerator::PerlinGenerator(uint32_t w, uint32_t h, uint32_t seed)
: width(w),
height(h),
engine(seed),
rnd()
{
}
uint32_t lib::PerlinGenerator::operator()(uint32_t x, uint32_t y) const
{
auto perlin = this->perlin(static_cast<double>(x) / this->width,
static_cast<double>(y) / this->height);
auto value = static_cast<uint8_t>(perlin * 255);
return uint32_t{0xff} |
static_cast<uint32_t>(value) << 8 |
static_cast<uint32_t>(value) << 16 |
static_cast<uint32_t>(value) << 24;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment