Commit 374951bb authored by Lukas Tietze's avatar Lukas Tietze

Wiederherstellung der normalen Funktion mit TR

parent 9fe21789
......@@ -70,6 +70,7 @@ add_library(
src/Lib/src/VkUtil/VkResult.cpp
src/Lib/src/VkUtil/Buffer.cpp
src/Lib/src/VkUtil/Texture.cpp
src/Lib/src/VkUtil/RenderTarget.cpp
src/Lib/src/VkUtil/StagedBuffer.cpp
src/Lib/src/VkUtil/OneTimeCommandBuffer.cpp
src/Lib/src/VkUtil/StbImageImpl.cpp
......
......@@ -4,7 +4,7 @@
layout(location = 0) in vec2 texCoord;
layout(location = 0) out vec4 color;
layout(binding = 0) uniform sampler2D texSampler;
layout(binding = 0) uniform sampler2D texSampler[4];
void main() {
vec2 offsets[9] = {
......@@ -25,12 +25,12 @@ void main() {
1.0 / 84.0, 4.0 / 84.0, 1.0 / 84.0,
};
vec2 relativePixelSize = vec2(1.0) / textureSize(texSampler, 0);
vec2 relativePixelSize = vec2(1.0) / textureSize(texSampler[0], 0);
vec4 samples[9];
for(int i = 0; i < 9; i++) {
samples[i] = texture(texSampler, texCoord + offsets[i] * relativePixelSize);
samples[i] = texture(texSampler[0], texCoord + offsets[i] * relativePixelSize);
}
vec3 edge = vec3(0);
......@@ -50,6 +50,6 @@ void main() {
res = l * blur + (1 - l) * samples[5].rgb;
color = vec4(vec3(samples[5].a * 0.5), 1.0);
// color = vec4(res, 1.0);
// color = vec4(vec3(samples[5].a * 0.5), 1.0);
color = vec4(res, 1.0);
}
\ No newline at end of file
......@@ -24,6 +24,7 @@
#include "VkUtil/TopLevelAccelerationStructure.hpp"
#include "VkUtil/VkUtil.hpp"
#include "VkUtil/Texture.hpp"
#include "VkUtil/RenderTarget.hpp"
#include "imgui_impl_vulkan.h"
namespace lib
......@@ -191,7 +192,6 @@ namespace lib
const uint32_t lightSources = 5;
const uint32_t vertices = 6;
const uint32_t indices = 7;
const uint32_t dummyTexture = 8;
} const descriptorBindings;
struct
......@@ -203,6 +203,9 @@ namespace lib
} shaderOffsets;
std::unique_ptr<lib::Buffer> shaderBindingTableBuffer;
const uint32_t temporalInterpolationCount = 4;
uint32_t temporalInterpolationImageIndex = 0;
std::vector<lib::TemporalInterpolationFrame> temporalInterpolationFrames;
bool modelLoaded;
bool shadersLoaded;
......@@ -288,7 +291,7 @@ namespace lib
bool IsOk() const;
bool IsRecoverable() const;
void UpdateCommandBuffer(uint32_t imageIndex);
void UpdateCommandBuffer(uint32_t imageIndex, uint32_t temporalInterpolationImageIndex);
void BeginFrame();
void DrawFrame(lib::time_diff_t diff);
void EndFrame();
......
......@@ -2,7 +2,7 @@
#include <memory>
#include "VkUtil/Texture.hpp"
#include "VkUtil/RenderTarget.hpp"
#include "GlfwAndVulkan.hpp"
#include "VkUtil/StagedBuffer.hpp"
......@@ -10,18 +10,23 @@ namespace lib
{
struct Frame
{
std::unique_ptr<lib::StagedBuffer> ubo;
vk::CommandPool commandPool;
vk::CommandBuffer commandBuffer;
vk::Fence inFlightFence;
vk::DescriptorSet raytracingDescriptorSet;
vk::DescriptorSet rasterizationDescriptorSet;
vk::Image swapChainImage;
vk::ImageView swapChainImageView;
vk::Framebuffer swapChainFramebufferForRasterizationRenderPass;
vk::Framebuffer swapChainFramebufferForImGui;
};
std::unique_ptr<lib::Texture> raytracingTexture;
struct TemporalInterpolationFrame
{
vk::DescriptorSet raytracingDescriptorSet;
vk::DescriptorSet rasterizationDescriptorSet;
std::unique_ptr<lib::RenderTarget> raytracingRenderTarget;
// TODO gehört eigentlich zum Frame, nicht zum TemporalInterpolationFrame
std::unique_ptr<lib::StagedBuffer> ubo;
};
} // namespace lib
#pragma once
#include "GlfwAndVulkan.hpp"
#include "VkUtil/Texture.hpp"
#include <vector>
#include <map>
namespace lib
{
class RenderTarget : public lib::Texture
{
private:
std::map<vk::RenderPass, vk::Framebuffer> framebuffersByRenderPass;
vk::Semaphore semaphore;
void CreateFramebufferForRenderPass(vk::RenderPass renderPass);
public:
RenderTarget(CgContext *parent, uint32_t width, uint32_t height, const lib::TextureCreateInfo &textureCreateInfo);
RenderTarget(const RenderTarget &copy) = delete;
RenderTarget(RenderTarget &&move) = delete;
~RenderTarget();
RenderTarget &operator=(const RenderTarget &copy) = delete;
RenderTarget &operator=(RenderTarget &&move) = delete;
const vk::Semaphore &GetSemaphore() const;
const vk::Framebuffer &GetFrameBufferForRenderPass(const vk::RenderPass &renderpass) const;
void CreateFramebuffersForRenderPasses(const std::initializer_list<vk::RenderPass> &renderpasses);
template <typename TForwardIt>
void CreateFramebuffersForRenderPasses(TForwardIt begin, TForwardIt end)
{
for (; begin != end; ++begin)
{
this->CreateFramebufferForRenderPass(*begin);
}
}
};
} // namespace lib
......@@ -69,6 +69,9 @@ namespace lib
vk::DeviceMemory memory;
vk::Sampler sampler;
uint32_t width;
uint32_t height;
protected:
CgContext *parent;
......@@ -137,16 +140,20 @@ namespace lib
Texture(Texture &&) = delete;
Texture(const Texture &) = delete;
~Texture();
Texture &operator=(Texture &&) = delete;
Texture &operator=(const Texture &) = delete;
const vk::Image &GetHandle() const;
const vk::ImageView &GetView() const;
const vk::Sampler &GetSampler() const;
Texture &operator=(Texture &&) = delete;
Texture &operator=(const Texture &) = delete;
uint32_t GetHeight() const;
uint32_t GetWidth() const;
}; // namespace lib
} // namespace lib
......@@ -158,7 +158,7 @@ bool lib::CgContext::TryCreateRasterizerPipeline()
void lib::CgContext::CreateRasterizationDescriptorSets()
{
const auto count = static_cast<uint32_t>(this->frames.size());
const auto count = static_cast<uint32_t>(this->temporalInterpolationFrames.size());
std::vector<vk::DescriptorSetLayout> layouts(count, this->rasterizationStep.descriptorSetLayout);
......@@ -170,27 +170,41 @@ void lib::CgContext::CreateRasterizationDescriptorSets()
auto sets = this->logicalDevice.allocateDescriptorSets(allocateInfo);
auto setsIt = std::begin(sets);
for (auto &frame : this->frames)
for (size_t i = 0; i < count; i++)
{
auto &frame = this->temporalInterpolationFrames[i];
frame.rasterizationDescriptorSet = *setsIt++;
vk::DescriptorImageInfo rtImageInfo;
rtImageInfo.imageLayout = vk::ImageLayout::eGeneral;
rtImageInfo.imageView = frame.raytracingTexture->GetView();
rtImageInfo.sampler = frame.raytracingTexture->GetSampler();
std::vector<vk::DescriptorImageInfo> imageInfos;
std::vector<vk::WriteDescriptorSet> writes;
// rtImageInfo.imageView = this->dummyTexture->GetView();
// rtImageInfo.sampler = this->dummyTexture->GetSampler();
imageInfos.reserve(temporalInterpolationCount + 1);
writes.reserve(temporalInterpolationCount + 1);
vk::WriteDescriptorSet rtImageWrite;
rtImageWrite.descriptorCount = 1;
rtImageWrite.descriptorType = vk::DescriptorType::eCombinedImageSampler;
rtImageWrite.dstArrayElement = 0;
rtImageWrite.dstBinding = 0;
rtImageWrite.pImageInfo = &rtImageInfo;
rtImageWrite.dstSet = frame.rasterizationDescriptorSet;
for (uint32_t j = 0; j < this->temporalInterpolationCount + 1; j++)
{
auto frameIndex = this->temporalInterpolationCount > 0
? (this->temporalInterpolationCount + i - j) % this->temporalInterpolationCount
: i;
auto &backFrame = this->temporalInterpolationFrames[frameIndex];
auto &info = imageInfos.emplace_back();
info.imageLayout = vk::ImageLayout::eGeneral;
info.imageView = backFrame.raytracingRenderTarget->GetView();
info.sampler = backFrame.raytracingRenderTarget->GetSampler();
auto &imageWrite = writes.emplace_back();
imageWrite.descriptorCount = 1;
imageWrite.descriptorType = vk::DescriptorType::eCombinedImageSampler;
imageWrite.dstArrayElement = j;
imageWrite.dstBinding = 0;
imageWrite.pImageInfo = &info;
imageWrite.dstSet = frame.rasterizationDescriptorSet;
}
this->logicalDevice.updateDescriptorSets(rtImageWrite, {});
this->logicalDevice.updateDescriptorSets(writes, {});
}
lib::log::Info("Successfully created rasterization descriptor sets!");
......@@ -198,10 +212,10 @@ void lib::CgContext::CreateRasterizationDescriptorSets()
void lib::CgContext::CreateRasterizationDescriptorPool()
{
const auto count = static_cast<uint32_t>(this->frames.size());
const auto count = static_cast<uint32_t>(this->temporalInterpolationFrames.size());
std::vector<vk::DescriptorPoolSize> poolSizes{
{vk::DescriptorType::eCombinedImageSampler, count},
{vk::DescriptorType::eCombinedImageSampler, count * (this->temporalInterpolationCount + 1)},
};
vk::DescriptorPoolCreateInfo createInfo;
......@@ -221,7 +235,7 @@ void lib::CgContext::CreateRasterizationDescriptorSetLayout()
auto &item = layoutBindings.emplace_back();
item.binding = 0;
item.descriptorType = vk::DescriptorType::eCombinedImageSampler;
item.descriptorCount = 1;
item.descriptorCount = this->temporalInterpolationCount + 1;
item.stageFlags = vk::ShaderStageFlagBits::eFragment;
vk::DescriptorSetLayoutCreateInfo createInfo;
......@@ -276,9 +290,7 @@ void lib::CgContext::CreateRasterizationRenderPass()
void lib::CgContext::CreateRasterizationFrameBuffers()
{
const auto count = static_cast<uint32_t>(frames.size());
for (auto &frame : this->frames)
for (auto &frame : this->temporalInterpolationFrames)
{
const lib::TextureCreateInfo textureCreateInfo{
{
......@@ -292,13 +304,16 @@ void lib::CgContext::CreateRasterizationFrameBuffers()
vk::ImageUsageFlagBits::eStorage,
vk::ImageLayout::eGeneral};
auto newTexture = new lib::Texture{this,
this->swapChainExtent.width,
this->swapChainExtent.height,
textureCreateInfo};
auto newTexture = new lib::RenderTarget{this,
this->swapChainExtent.width,
this->swapChainExtent.height,
textureCreateInfo};
frame.raytracingTexture.reset(newTexture);
frame.raytracingRenderTarget.reset(newTexture);
}
for (auto &frame : this->frames)
{
vk::FramebufferCreateInfo createInfo;
createInfo.renderPass = this->rasterizationStep.renderPass;
createInfo.attachmentCount = 1;
......
......@@ -113,13 +113,6 @@ void lib::CgContext::CreateRaytracingDescriptorSetLayout()
layoutBindings.back().descriptorType = vk::DescriptorType::eStorageBuffer;
layoutBindings.back().stageFlags = vk::ShaderStageFlagBits::eClosestHitNV;
// dummy texture
layoutBindings.emplace_back();
layoutBindings.back().binding = this->descriptorBindings.dummyTexture;
layoutBindings.back().descriptorCount = 1;
layoutBindings.back().descriptorType = vk::DescriptorType::eCombinedImageSampler;
layoutBindings.back().stageFlags = vk::ShaderStageFlagBits::eClosestHitNV | vk::ShaderStageFlagBits::eRaygenNV;
vk::DescriptorSetLayoutCreateInfo createInfo;
createInfo.bindingCount = static_cast<uint32_t>(layoutBindings.size());
createInfo.pBindings = layoutBindings.data();
......@@ -131,7 +124,7 @@ void lib::CgContext::CreateRaytracingDescriptorSetLayout()
void lib::CgContext::CreateRaytracingDescriptorPool()
{
auto count = static_cast<uint32_t>(this->frames.size());
auto count = static_cast<uint32_t>(this->temporalInterpolationFrames.size());
std::vector<vk::DescriptorPoolSize> sizes{
//Acceleration structure
vk::DescriptorPoolSize(vk::DescriptorType::eAccelerationStructureNV, count),
......@@ -163,7 +156,7 @@ void lib::CgContext::CreateRaytracingDescriptorPool()
void lib::CgContext::CreateRaytracingDescriptorSets()
{
auto count = static_cast<uint32_t>(this->frames.size());
auto count = static_cast<uint32_t>(this->temporalInterpolationFrames.size());
std::vector<vk::DescriptorSetLayout> layouts(count, this->raytracingStep.descriptorSetLayout);
vk::DescriptorSetAllocateInfo allocateInfo;
......@@ -175,7 +168,7 @@ void lib::CgContext::CreateRaytracingDescriptorSets()
for (size_t i = 0; i < count; i++)
{
auto &frame = this->frames[i];
auto &frame = this->temporalInterpolationFrames[i];
frame.raytracingDescriptorSet = descriptorSets[i];
vk::StructureChain<vk::WriteDescriptorSet, vk::WriteDescriptorSetAccelerationStructureNV> accelerationStructureWrite;
......@@ -189,7 +182,7 @@ void lib::CgContext::CreateRaytracingDescriptorSets()
vk::DescriptorImageInfo outputImageInfo;
outputImageInfo.imageLayout = vk::ImageLayout::eGeneral;
outputImageInfo.imageView = frame.raytracingTexture->GetView();
outputImageInfo.imageView = frame.raytracingRenderTarget->GetView();
vk::WriteDescriptorSet outputImageWrite;
outputImageWrite.dstSet = frame.raytracingDescriptorSet;
......@@ -271,27 +264,15 @@ void lib::CgContext::CreateRaytracingDescriptorSets()
objectInstanceWrite.descriptorCount = 1;
objectInstanceWrite.pBufferInfo = &objectInstanceInfo;
vk::DescriptorImageInfo dummyTextureInfo;
dummyTextureInfo.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
dummyTextureInfo.imageView = this->dummyTexture->GetView();
dummyTextureInfo.sampler = this->dummyTexture->GetSampler();
vk::WriteDescriptorSet dummyTextureWrite;
dummyTextureWrite.dstSet = frame.raytracingDescriptorSet;
dummyTextureWrite.dstBinding = this->descriptorBindings.dummyTexture;
dummyTextureWrite.dstArrayElement = 0;
dummyTextureWrite.descriptorType = vk::DescriptorType::eCombinedImageSampler;
dummyTextureWrite.descriptorCount = 1;
dummyTextureWrite.pImageInfo = &dummyTextureInfo;
this->logicalDevice.updateDescriptorSets({accelerationStructureWrite.get<vk::WriteDescriptorSet>(),
outputImageWrite,
camWrite,
vertexBufferWrite,
indexBufferWrite,
materialBufferWrite,
objectInstanceWrite,
dummyTextureWrite},
this->logicalDevice.updateDescriptorSets({
accelerationStructureWrite.get<vk::WriteDescriptorSet>(),
outputImageWrite,
camWrite,
vertexBufferWrite,
indexBufferWrite,
materialBufferWrite,
objectInstanceWrite,
},
{});
}
......
......@@ -158,10 +158,14 @@ lib::CgContext::~CgContext()
for (auto &frame : this->frames)
{
frame.ubo.reset();
this->logicalDevice.destroyCommandPool(frame.commandPool);
}
for (auto &frame : this->temporalInterpolationFrames)
{
frame.ubo.reset();
}
for (auto &semaphore : this->imageAvailableSemaphores)
{
if (semaphore)
......@@ -286,11 +290,11 @@ void lib::CgContext::CleanupSwapChain()
{
this->logicalDevice.destroyImageView(frame.swapChainImageView);
}
}
if (frame.raytracingTexture)
{
frame.raytracingTexture.reset();
}
for (auto &frame : this->temporalInterpolationFrames)
{
frame.raytracingRenderTarget.reset();
}
if (this->swapChain)
......@@ -696,6 +700,8 @@ void lib::CgContext::CreateSwapChain()
this->frames[i].swapChainImage = swapChainImages[i];
}
this->temporalInterpolationFrames.resize(swapChainImages.size() + this->temporalInterpolationCount);
lib::log::Info("Successfully acquired swap chain images!");
}
......@@ -729,9 +735,10 @@ void lib::CgContext::CreateCommandPool()
lib::log::Info("Successfully created command pools!");
}
void lib::CgContext::UpdateCommandBuffer(uint32_t imageIndex)
void lib::CgContext::UpdateCommandBuffer(uint32_t imageIndex, uint32_t temporalInterpolationImageIndex)
{
auto &frame = this->frames[imageIndex];
auto &interpolationFrame = this->temporalInterpolationFrames[temporalInterpolationImageIndex];
this->logicalDevice.resetCommandPool(frame.commandPool, vk::CommandPoolResetFlagBits::eReleaseResources);
this->pushConstant.rngSeed = this->randomDistribution(this->randomEngine);
......@@ -745,16 +752,16 @@ void lib::CgContext::UpdateCommandBuffer(uint32_t imageIndex)
frame.commandBuffer.bindPipeline(vk::PipelineBindPoint::eRayTracingNV, this->raytracingStep.pipeline);
frame.commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eRayTracingNV,
this->raytracingStep.pipelineLayout,
0, frame.raytracingDescriptorSet,
0, interpolationFrame.raytracingDescriptorSet,
{});
TransitionImageLayout(frame.commandBuffer,
frame.raytracingTexture->GetHandle(), this->swapChainImageFormat,
interpolationFrame.raytracingRenderTarget->GetHandle(), this->swapChainImageFormat,
vk::ImageLayout::eUndefined, vk::ImageLayout::eGeneral,
vk::PipelineStageFlagBits::eTopOfPipe,
vk::PipelineStageFlagBits::eRayTracingShaderNV);
frame.ubo->ScheduleTransfer(frame.commandBuffer);
interpolationFrame.ubo->ScheduleTransfer(frame.commandBuffer);
frame.commandBuffer.pushConstants(this->raytracingStep.pipelineLayout,
vk::ShaderStageFlagBits::eRaygenNV | vk::ShaderStageFlagBits::eClosestHitNV,
......@@ -782,7 +789,7 @@ void lib::CgContext::UpdateCommandBuffer(uint32_t imageIndex)
frame.commandBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, this->rasterizationStep.pipeline);
frame.commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics,
this->rasterizationStep.pipelineLayout,
0, frame.rasterizationDescriptorSet, {});
0, interpolationFrame.rasterizationDescriptorSet, {});
frame.commandBuffer.draw(3, 1, 0, 0);
frame.commandBuffer.endRenderPass();
}
......@@ -962,7 +969,7 @@ vk::ShaderModule lib::CgContext::LoadShader(const ShaderInfo &info)
void lib::CgContext::UpdateUniformBuffer(uint32_t imageIndex, lib::time_diff_t delta)
{
this->camera.UpdateMatrices();
this->frames[imageIndex].ubo->BufferDataHostOnly(SceneDescription{
this->temporalInterpolationFrames[imageIndex].ubo->BufferDataHostOnly(SceneDescription{
this->camera.GetModel(),
this->lightSources,
this->clearColor});
......@@ -1009,7 +1016,7 @@ void lib::CgContext::DrawFrame(lib::time_diff_t delta)
this->logicalDevice.waitForFences(this->frames[imageIndex].inFlightFence, VK_TRUE, std::numeric_limits<uint64_t>::max());
}
this->UpdateCommandBuffer(imageIndex);
this->UpdateCommandBuffer(imageIndex, imageIndex);
this->frames[imageIndex].inFlightFence = this->inFlightFences[this->frameIndex];
......@@ -1208,7 +1215,7 @@ void lib::CgContext::CreateUniformBuffers()
int i = 0;
for (auto &frame : this->frames)
for (auto &frame : this->temporalInterpolationFrames)
{
const auto name = "UBO" + std::to_string(i);
......
#include "Core/CgContext.hpp"
#include "VkUtil/RenderTarget.hpp"
lib::RenderTarget::RenderTarget(CgContext *parent, uint32_t width, uint32_t height, const lib::TextureCreateInfo &textureCreateInfo)
: lib::Texture(parent, width, height, textureCreateInfo)
{
this->semaphore = this->parent->GetLogicalDevice().createSemaphore({});
}
lib::RenderTarget::~RenderTarget()
{
auto logicalDevice = this->parent->GetLogicalDevice();
if (this->semaphore)
{
logicalDevice.destroySemaphore(this->semaphore);
}
for (auto [_, framebuffer] : this->framebuffersByRenderPass)
{
if (framebuffer)
{
logicalDevice.destroyFramebuffer(framebuffer);
}
}
}
void lib::RenderTarget::CreateFramebufferForRenderPass(vk::RenderPass renderPass)
{
auto logicalDevice = this->parent->GetLogicalDevice();
auto existing = this->framebuffersByRenderPass.find(renderPass);
if (existing != this->framebuffersByRenderPass.end())
{
logicalDevice.destroyFramebuffer(existing->second);
}
vk::FramebufferCreateInfo createInfo;
createInfo.attachmentCount = 1;
createInfo.height = this->GetHeight();
createInfo.width = this->GetWidth();
createInfo.layers = 1;
createInfo.renderPass = renderPass;
this->framebuffersByRenderPass[renderPass] = logicalDevice.createFramebuffer(createInfo);
}
const vk::Semaphore &lib::RenderTarget::GetSemaphore() const
{
return this->semaphore;
}
const vk::Framebuffer &lib::RenderTarget::GetFrameBufferForRenderPass(const vk::RenderPass &renderPass) const
{
return this->framebuffersByRenderPass.at(renderPass);
}
void lib::RenderTarget::CreateFramebuffersForRenderPasses(const std::initializer_list<vk::RenderPass> &renderpasses)
{
this->CreateFramebuffersForRenderPasses(std::begin(renderpasses), std::end(renderpasses));
}
......@@ -67,6 +67,9 @@ lib::Texture::~Texture()
void lib::Texture::LoadData(uint32_t width, uint32_t height, const void *data, uint8_t pixelByteCount, const TextureCreateInfo &createInfo)
{
this->width = width;
this->height = height;
auto queueFamilyIndices = createInfo.queueFamilyIndices;
lib::FilterUnique(queueFamilyIndices);
......@@ -181,4 +184,13 @@ const vk::ImageView &lib::Texture::GetView() const
const vk::Sampler &lib::Texture::GetSampler() const
{
return this->sampler;
}
\ No newline at end of file
}
uint32_t lib::Texture::GetHeight() const
{
return this->height;
}
uint32_t lib::Texture::GetWidth() const
{
return this->width;
}
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