Commit 40b326ea authored by Lukas Tietze's avatar Lukas Tietze

Dinge die nicht gehen :(

parent 43cde21b
#version 460
#extension GL_GOOGLE_include_directive : enable
layout(location = 0) in vec2 texCoord;
layout(location = 0) out vec4 color;
layout(binding = 0) uniform sampler2D texSampler;
void main() {
color = vec4(texture(texSampler, texCoord).rgb, 1.0);
}
\ No newline at end of file
......@@ -11,24 +11,37 @@ namespace lib
{
class CgContext;
struct TextureCreateInfo
{
inline TextureCreateInfo(vk::Format format = vk::Format{},
vk::ImageUsageFlags usage = vk::ImageUsageFlagBits{},
vk::ImageLayout layout = vk::ImageLayout{})
: format(format),
usage(usage),
layout(layout)
{
}
vk::Format format;
vk::ImageUsageFlags usage;
vk::ImageLayout layout;
};
/**
* @brief Stellt eine Textur dar.
*/
class Texture
{
private:
vk::Format format;
vk::Image image;
vk::ImageView view;
vk::DeviceMemory memory;
vk::Sampler sampler;
protected:
static constexpr vk::Format DefaultFormat = vk::Format::eR8G8B8A8Srgb;
CgContext *parent;
virtual void LoadData(uint32_t width, uint32_t height, const void *data, uint8_t pixelByteCount);
void LoadData(uint32_t width, uint32_t height, const void *data, uint8_t pixelByteCount, const TextureCreateInfo &createInfo);
public:
/**
......@@ -39,7 +52,7 @@ namespace lib
* @param height Die H枚he in Pixeln.
* @param format Das Zielformat der Daten, Standard ist RGBA mit 8 bit pro Kanal.
*/
Texture(CgContext *parent, uint32_t width, uint32_t height, vk::Format format = DefaultFormat);
Texture(CgContext *parent, uint32_t width, uint32_t height, const TextureCreateInfo &createInfo);
/**
* @brief Initialisiert eine neue Instanz der Texture Klasse und l盲dt die Daten aus
......@@ -49,7 +62,7 @@ namespace lib
* @param format Das Format der Daten, Standard ist RGBA mit 8 bit pro Kanal.
* @param path Der Pfad zur Quelldatei.
*/
Texture(CgContext *parent, const std::string &path, vk::Format format = DefaultFormat);
Texture(CgContext *parent, const std::string &path, const TextureCreateInfo &createInfo);
/**
* @brief Initialisiert eine neue Instanz der Texture Klasse und l盲dt die Daten aus einem Puffer.
......@@ -61,7 +74,7 @@ namespace lib
* @param pixelByteCount bytes pro Pixel.
* @param format Das Zielformat der Daten, Standard ist RGBA mit 8 bit pro Kanal.
*/
Texture(CgContext *parent, uint32_t width, uint32_t height, uint8_t pixelByteCount, const void *data, vk::Format format = DefaultFormat);
Texture(CgContext *parent, uint32_t width, uint32_t height, uint8_t pixelByteCount, const void *data, const TextureCreateInfo &createInfo);
/**
* @brief Initialisiert eine neue Instanz der Texture Klasse und erzeugt die Daten mittels
......@@ -75,8 +88,8 @@ namespace lib
* @param format Das Format der Daten, Standard ist RGBA mit 8 bit pro Kanal.
*/
template <typename T>
Texture(CgContext *parent, uint32_t width, uint32_t height, T &generator, vk::Format format = DefaultFormat) : parent(parent),
format(format)
Texture(CgContext *parent, uint32_t width, uint32_t height, T &generator, const TextureCreateInfo &createInfo)
: parent(parent)
{
std::vector<decltype(generator(uint32_t{0}, uint32_t{0}))> buf(width * height);
......@@ -88,7 +101,7 @@ namespace lib
}
}
this->LoadData(width, height, buf.data(), sizeof(buf.front()));
this->LoadData(width, height, buf.data(), sizeof(buf.front()), createInfo);
}
~Texture();
......
......@@ -39,7 +39,7 @@ namespace
colorAttachment.storeOp = vk::AttachmentStoreOp::eStore;
colorAttachment.stencilLoadOp = vk::AttachmentLoadOp::eDontCare;
colorAttachment.stencilStoreOp = vk::AttachmentStoreOp::eDontCare;
colorAttachment.initialLayout = vk::ImageLayout::eGeneral;
colorAttachment.initialLayout = vk::ImageLayout::eColorAttachmentOptimal;
colorAttachment.finalLayout = vk::ImageLayout::ePresentSrcKHR;
vk::AttachmentReference attachmentReference;
......
......@@ -121,7 +121,8 @@ bool lib::CgContext::TryCreateRasterizerPipeline()
createInfo.pColorBlendState = &colorBlendStateCreateInfo;
createInfo.layout = this->rasterizationStep.pipelineLayout;
this->rasterizationStep.pipeline = this->logicalDevice.createGraphicsPipeline(nullptr, createInfo);
const auto result = this->logicalDevice.createGraphicsPipeline(nullptr, createInfo);
this->rasterizationStep.pipeline = result.value;
}
if (vertexModule)
......@@ -174,7 +175,7 @@ void lib::CgContext::CreateRasterizationDescriptorPool()
const auto count = static_cast<uint32_t>(this->frames.size());
std::vector<vk::DescriptorPoolSize> poolSizes{
{vk::DescriptorType::eCombinedImageSampler, count * 2},
{vk::DescriptorType::eCombinedImageSampler, count},
};
vk::DescriptorPoolCreateInfo createInfo;
......@@ -189,15 +190,54 @@ void lib::CgContext::CreateRasterizationDescriptorSetLayout()
{
std::vector<vk::DescriptorSetLayoutBinding> layoutBindings;
auto & item = layoutBindings.emplace_back();
auto &item = layoutBindings.emplace_back();
item.binding = 0;
item.descriptorCount = 1;
item.descriptorType = vk::DescriptorType::eCombinedImageSampler;
// item.pImmutableSamplers = &this->
item.descriptorCount = 1;
item.stageFlags = vk::ShaderStageFlagBits::eFragment;
vk::DescriptorSetLayoutCreateInfo createInfo;
createInfo.bindingCount = static_cast<uint32_t>(layoutBindings.size());
createInfo.pBindings = layoutBindings.data();
this->rasterizationStep.descriptorSetLayout = this->logicalDevice.createDescriptorSetLayout(createInfo);
}
void lib::CgContext::CreateRasterizationRenderPass()
{
// TODO
vk::AttachmentDescription colorAttachment;
colorAttachment.initialLayout = vk::ImageLayout::eColorAttachmentOptimal;
colorAttachment.finalLayout = vk::ImageLayout::eColorAttachmentOptimal;
colorAttachment.loadOp = vk::AttachmentLoadOp::eDontCare;
colorAttachment.storeOp = vk::AttachmentStoreOp::eStore;
colorAttachment.format = this->swapChainImageFormat;
colorAttachment.stencilLoadOp = vk::AttachmentLoadOp::eDontCare;
colorAttachment.stencilStoreOp = vk::AttachmentStoreOp::eDontCare;
vk::AttachmentReference colorAttachmentRef;
colorAttachmentRef.layout = vk::ImageLayout::eColorAttachmentOptimal;
colorAttachmentRef.attachment = 0;
vk::SubpassDescription subpassDescription;
subpassDescription.pipelineBindPoint = vk::PipelineBindPoint::eGraphics;
subpassDescription.colorAttachmentCount = 1;
subpassDescription.pColorAttachments = &colorAttachmentRef;
vk::SubpassDependency subpassDependency;
subpassDependency.srcSubpass = VK_SUBPASS_EXTERNAL;
subpassDependency.dstSubpass = 0;
subpassDependency.srcStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput;
subpassDependency.srcAccessMask = vk::AccessFlagBits{};
subpassDependency.dstStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput;
subpassDependency.dstAccessMask = vk::AccessFlagBits::eColorAttachmentWrite;
vk::RenderPassCreateInfo createInfo;
createInfo.subpassCount = 1;
createInfo.pSubpasses = &subpassDescription;
createInfo.dependencyCount = 1;
createInfo.pDependencies = &subpassDependency;
createInfo.attachmentCount = 1;
createInfo.pAttachments = &colorAttachment;
this->rasterizationStep.renderPass = this->logicalDevice.createRenderPass(createInfo);
}
......@@ -192,7 +192,7 @@ void lib::CgContext::CreateRaytracingDescriptorSets()
vk::DescriptorImageInfo outputImageInfo;
outputImageInfo.imageLayout = vk::ImageLayout::eGeneral;
outputImageInfo.imageView = frame.swapChainImageView;
outputImageInfo.imageView = frame.raytracingTexture->GetView();
vk::WriteDescriptorSet outputImageWrite;
outputImageWrite.dstSet = frame.descriptorSet;
......@@ -412,7 +412,8 @@ bool lib::CgContext::TryCreateRayTracingPipeline()
pipelineCreateInfo.maxRecursionDepth = this->physicalDeviceInfo.GetRaytracingProperties().maxRecursionDepth;
pipelineCreateInfo.layout = this->raytracingStep.pipelineLayout;
this->raytracingStep.pipeline = this->logicalDevice.createRayTracingPipelinesNV(vk::PipelineCache(), pipelineCreateInfo)[0];
const auto result = this->logicalDevice.createRayTracingPipelinesNV(vk::PipelineCache(), pipelineCreateInfo);
this->raytracingStep.pipeline = result.value[0];
Log("Successfully created raytracing pipeline!");
success = true;
......
......@@ -709,8 +709,6 @@ void lib::CgContext::UpdateCommandBuffer(uint32_t imageIndex)
frame.commandBuffer.begin(beginInfo);
auto sbtEntrySize = this->physicalDeviceInfo.GetRaytracingProperties().shaderGroupHandleSize;
if (this->IsOk())
{
frame.commandBuffer.bindPipeline(vk::PipelineBindPoint::eRayTracingNV, this->raytracingStep.pipeline);
......@@ -719,7 +717,7 @@ void lib::CgContext::UpdateCommandBuffer(uint32_t imageIndex)
}
TransitionImageLayout(frame.commandBuffer,
frame.swapChainImage, this->swapChainImageFormat,
frame.raytracingTexture->GetHandle(), this->swapChainImageFormat,
vk::ImageLayout::eUndefined, vk::ImageLayout::eGeneral,
vk::PipelineStageFlagBits::eTopOfPipe,
vk::PipelineStageFlagBits::eRayTracingShaderNV);
......@@ -733,22 +731,43 @@ void lib::CgContext::UpdateCommandBuffer(uint32_t imageIndex)
0, sizeof(this->pushConstant),
&this->pushConstant);
auto sbtEntrySize = this->physicalDeviceInfo.GetRaytracingProperties().shaderGroupHandleSize;
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::eColorAttachmentOptimal,
vk::PipelineStageFlagBits::eRayTracingShaderNV, vk::PipelineStageFlagBits::eFragmentShader);
{
vk::RenderPassBeginInfo ppBeginInfo;
ppBeginInfo.renderPass = this->rasterizationStep.renderPass;
ppBeginInfo.framebuffer = this->imGui.frameBuffers[imageIndex];
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, this->rasterizationStep.descriptorSets, {});
frame.commandBuffer.draw(3, 1, 0, 0);
frame.commandBuffer.endRenderPass();
}
if (this->imGui.show)
{
vk::RenderPassBeginInfo uiRenderInfo;
uiRenderInfo.renderPass = this->imGui.renderPass;
uiRenderInfo.framebuffer = this->imGui.frameBuffers[imageIndex];
uiRenderInfo.renderArea.offset = {0, 0};
uiRenderInfo.renderArea.extent = this->swapChainExtent;
vk::RenderPassBeginInfo uiBeginInfo;
uiBeginInfo.renderPass = this->imGui.renderPass;
uiBeginInfo.framebuffer = this->imGui.frameBuffers[imageIndex];
uiBeginInfo.renderArea.offset = {0, 0};
uiBeginInfo.renderArea.extent = this->swapChainExtent;
frame.commandBuffer.beginRenderPass(uiRenderInfo, vk::SubpassContents::eInline);
frame.commandBuffer.beginRenderPass(uiBeginInfo, vk::SubpassContents::eInline);
ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), frame.commandBuffer);
......@@ -1004,20 +1023,41 @@ void lib::CgContext::DrawFrame(lib::time_diff_t delta)
void lib::CgContext::LoadDummyTexture()
{
this->dummyTexture.reset(new lib::Texture(this, 256, 256, [](uint32_t x, uint32_t y) {
auto rx = x / 255.f;
auto ry = y / 255.f;
auto r = static_cast<uint8_t>(rx * 255);
auto g = static_cast<uint8_t>(rx * 255);
auto b = static_cast<uint8_t>(rx * 255);
auto a = uint8_t{255};
return (uint32_t{r} << 24) |
(uint32_t{g} << 16) |
(uint32_t{b} << 8) |
(uint32_t{a} << 0);
}));
this->dummyTexture.reset(new lib::Texture{this, 256, 256, [](uint32_t x, uint32_t y) {
auto rx = x / 255.f;
auto ry = y / 255.f;
auto r = static_cast<uint8_t>(rx * 255);
auto g = static_cast<uint8_t>(rx * 255);
auto b = static_cast<uint8_t>(rx * 255);
auto a = uint8_t{255};
return (uint32_t{r} << 24) |
(uint32_t{g} << 16) |
(uint32_t{b} << 8) |
(uint32_t{a} << 0);
},
lib::TextureCreateInfo{vk::Format::eR8G8B8A8Srgb, vk::ImageUsageFlagBits::eSampled, vk::ImageLayout::eShaderReadOnlyOptimal}});
for (auto &frame : this->frames)
{
frame.raytracingTexture.reset(new lib::Texture{this,
this->swapChainExtent.width,
this->swapChainExtent.height,
lib::TextureCreateInfo{
this->swapChainImageFormat,
vk::ImageUsageFlagBits::eStorage,
vk::ImageLayout::eGeneral}});
vk::FramebufferCreateInfo createInfo;
createInfo.renderPass = this->rasterizationStep.renderPass;
createInfo.attachmentCount = 1;
createInfo.pAttachments = &frame.raytracingTexture->GetView();
createInfo.width = this->swapChainExtent.width;
createInfo.height = this->swapChainExtent.height;
createInfo.layers = 1;
frame.raytracingTextureFbo = this->logicalDevice.createFramebuffer(createInfo);
}
}
bool lib::CgContext::TryLoadModel()
......@@ -1177,4 +1217,3 @@ bool lib::CgContext::TryCreatePipelines()
return this->TryCreateRasterizerPipeline() &&
this->TryCreateRayTracingPipeline();
}
......@@ -130,6 +130,9 @@ bool lib::CompileShader(const std::string &glsl, const lib::ShaderInfo &info, li
std::unique_ptr<BasicIncluder> includer(new BasicIncluder());
options.SetIncluder(std::move(includer));
options.SetTargetEnvironment(shaderc_target_env_vulkan, shaderc_env_version_vulkan_1_1);
options.SetSourceLanguage(shaderc_source_language_glsl);
options.SetTargetSpirv(shaderc_spirv_version_1_5);
switch (info.compileOptions.optimizationLevel)
{
......@@ -160,7 +163,7 @@ bool lib::CompileShader(const std::string &glsl, const lib::ShaderInfo &info, li
}
auto preprocessedText = std::string(preprocessingResult.begin(), preprocessingResult.end());
auto compileResult = compiler.CompileGlslToSpv(preprocessedText, shaderKind, "shader_src");
auto compileResult = compiler.CompileGlslToSpv(preprocessedText, shaderKind, "shader_src.frag", options);
if (compileResult.GetCompilationStatus() != shaderc_compilation_status_success)
{
......
......@@ -3,14 +3,14 @@
#include "stb_image.h"
#include "VkUtil/VkUtil.hpp"
lib::Texture::Texture(CgContext *parent, uint32_t width, uint32_t height, vk::Format format) : parent(parent),
format(format)
lib::Texture::Texture(CgContext *parent, uint32_t width, uint32_t height, const TextureCreateInfo &createInfo)
: parent(parent)
{
this->LoadData(width, height, nullptr, 0);
this->LoadData(width, height, nullptr, 0, createInfo);
}
lib::Texture::Texture(CgContext *parent, const std::string &path, vk::Format format) : parent(parent),
format(format)
lib::Texture::Texture(CgContext *parent, const std::string &path, const TextureCreateInfo &createInfo)
: parent(parent)
{
int width;
int height;
......@@ -24,15 +24,15 @@ lib::Texture::Texture(CgContext *parent, const std::string &path, vk::Format for
throw std::runtime_error("Failed to read image!");
}
this->LoadData(width, height, pixels, texChannels);
this->LoadData(width, height, pixels, texChannels, createInfo);
stbi_image_free(pixels);
}
lib::Texture::Texture(CgContext *parent, uint32_t width, uint32_t height, uint8_t pixelByteCount, const void *data, vk::Format format) : parent(parent),
format(format)
lib::Texture::Texture(CgContext *parent, uint32_t width, uint32_t height, uint8_t pixelByteCount, const void *data, const TextureCreateInfo &createInfo)
: parent(parent)
{
this->LoadData(width, height, data, pixelByteCount);
this->LoadData(width, height, data, pixelByteCount, createInfo);
}
lib::Texture::~Texture()
......@@ -64,34 +64,40 @@ lib::Texture::~Texture()
}
}
void lib::Texture::LoadData(uint32_t width, uint32_t height, const void *data, uint8_t pixelByteCount)
void lib::Texture::LoadData(uint32_t width, uint32_t height, const void *data, uint8_t pixelByteCount, const TextureCreateInfo &createInfo)
{
std::unique_ptr<lib::Buffer> stagingBuffer;
if (data && pixelByteCount)
const auto loadInitialData = data && pixelByteCount;
if (loadInitialData)
{
stagingBuffer = std::make_unique<lib::Buffer>(std::move(this->parent->CreateStagingSrcBuffer()));
stagingBuffer->BufferData(data, width * height * pixelByteCount);
}
vk::ImageCreateInfo createInfo;
createInfo.imageType = vk::ImageType::e2D;
createInfo.extent.width = width;
createInfo.extent.height = height;
createInfo.extent.depth = 1;
createInfo.mipLevels = 1;
createInfo.arrayLayers = 1;
createInfo.format = this->format;
createInfo.tiling = vk::ImageTiling::eOptimal;
createInfo.initialLayout = vk::ImageLayout::eUndefined;
createInfo.usage = vk::ImageUsageFlagBits::eTransferDst |
vk::ImageUsageFlagBits::eSampled; // TODO: Struktur bauen, die noch bestimmt, wie auf das Bild zugegriffen werden soll.
createInfo.sharingMode = vk::SharingMode::eExclusive;
createInfo.samples = vk::SampleCountFlagBits::e1;
vk::ImageCreateInfo imageCreateInfo;
imageCreateInfo.imageType = vk::ImageType::e2D;
imageCreateInfo.extent.width = width;
imageCreateInfo.extent.height = height;
imageCreateInfo.extent.depth = 1;
imageCreateInfo.mipLevels = 1;
imageCreateInfo.arrayLayers = 1;
imageCreateInfo.format = createInfo.format;
imageCreateInfo.tiling = vk::ImageTiling::eOptimal;
imageCreateInfo.initialLayout = vk::ImageLayout::eUndefined;
imageCreateInfo.sharingMode = vk::SharingMode::eExclusive;
imageCreateInfo.samples = vk::SampleCountFlagBits::e1;
imageCreateInfo.usage = createInfo.usage;
if (loadInitialData)
{
imageCreateInfo.usage |= vk::ImageUsageFlagBits::eTransferDst;
}
const auto &device = this->parent->GetLogicalDevice();
this->image = device.createImage(createInfo);
this->image = device.createImage(imageCreateInfo);
vk::MemoryRequirements memoryRequirements = device.getImageMemoryRequirements(this->image);
......@@ -107,10 +113,10 @@ void lib::Texture::LoadData(uint32_t width, uint32_t height, const void *data, u
device.bindImageMemory(this->image, this->memory, 0);
if (data && pixelByteCount)
{
auto commandBuffer = this->parent->AllocateSingleUseStagingCommandBuffer();
auto commandBuffer = this->parent->AllocateSingleUseStagingCommandBuffer();
if (loadInitialData)
{
vk::BufferImageCopy region;
region.imageSubresource.aspectMask = vk::ImageAspectFlagBits::eColor;
region.imageSubresource.mipLevel = 0;
......@@ -119,21 +125,27 @@ void lib::Texture::LoadData(uint32_t width, uint32_t height, const void *data, u
region.imageOffset = {0, 0, 0};
region.imageExtent = {width, height, 1};
TransitionImageLayout(commandBuffer, this->image, this->format,
TransitionImageLayout(commandBuffer, this->image, createInfo.format,
vk::ImageLayout::eUndefined, vk::ImageLayout::eTransferDstOptimal,
vk::PipelineStageFlagBits::eTopOfPipe, vk::PipelineStageFlagBits::eTransfer);
commandBuffer.GetHandle().copyBufferToImage(*stagingBuffer, this->image,
vk::ImageLayout::eTransferDstOptimal, region);
TransitionImageLayout(commandBuffer, this->image, this->format,
vk::ImageLayout::eTransferDstOptimal, vk::ImageLayout::eShaderReadOnlyOptimal, // TODO das muss auch noch ge盲ndert werden wenn die Texture beschrieben werden soll
TransitionImageLayout(commandBuffer, this->image, createInfo.format,
vk::ImageLayout::eTransferDstOptimal, createInfo.layout,
vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eRayTracingShaderNV);
commandBuffer.Run();
}
else
{
TransitionImageLayout(commandBuffer, this->image, createInfo.format,
vk::ImageLayout::eUndefined, createInfo.layout,
vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eRayTracingShaderNV);
}
commandBuffer.Run();
this->view = CreateImageView(device, this->image, this->format);
this->view = CreateImageView(device, this->image, createInfo.format);
vk::SamplerCreateInfo samplerCreateInfo;
samplerCreateInfo.magFilter = vk::Filter::eLinear;
......
Subproject commit 97a6d80cb1a233fff3fff0a60cfcd9e303a8f0c3
Subproject commit 4cd7e48f19921542b521e5ae69f9175b113b5e73
Subproject commit 5e976e9b0584878b5503fd86e81795c14981d1de
Subproject commit 078571b7a99d87d83140d5e13f101c5528e25c48
Subproject commit 76406c7894912125afae59e6e5be00222ba10b48
Subproject commit bf1c62b2612dba79365e836830fe2a6105adbe78
Subproject commit 6bd53cc9e529cfb749f1badbbd23b1b8e6773651
Subproject commit 23e0701c0483283440d4d1bcd17eb7070fa8eb75
Subproject commit 3fc2c56b0cd56d2f7ebc3cad110f035879584291
Subproject commit caa519ca532a6a3a0279509fce2ceb791c4f4651
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