CgContext.Rasterization.cpp 9.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
#define GLM_FORCE_RADIANS
#include <algorithm>
#include <chrono>
#include <cmath>
#include <filesystem>
#include <iostream>
#include <iterator>
#include <limits>
#include <set>
#include <random>

#include "Config.hpp"
#include "Core/CameraModel.hpp"
#include "Core/CgContext.hpp"
#include "Core/CoreInitException.hpp"
#include "Core/Model/AssimpModel.hpp"
#include "Core/Model/Object.hpp"
#include "Core/Model/SceneDescription.hpp"
#include "Util/Logging.hpp"
#include "Util/NameList.hpp"
#include "Util/PredefinedMeshes.hpp"
#include "Util/Utils.hpp"
#include "VkUtil/VkUtil.hpp"
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "imgui_impl_vulkan.h"
#include "imgui_impl_glfw.h"

using lib::log::Log;
using lib::log::LogDebug;
using lib::log::LogError;
using lib::log::LogWarning;

bool lib::CgContext::TryCreateRasterizerPipeline()
{
    auto success = true;

    auto vertexModule = this->LoadShader(this->appInfo.shaderTable.postProcessingVertexShader);

    success = success && vertexModule;

    auto fragmentModule = this->LoadShader(this->appInfo.shaderTable.postProcessingFragmentShader);

    success = success && vertexModule;

    if (success)
    {
        std::vector<vk::PipelineShaderStageCreateInfo> shaderStages{
            vk::PipelineShaderStageCreateInfo{
                vk::PipelineShaderStageCreateFlags{},
                vk::ShaderStageFlagBits::eVertex,
                vertexModule,
                "main",
            },
            vk::PipelineShaderStageCreateInfo{
                vk::PipelineShaderStageCreateFlags{},
                vk::ShaderStageFlagBits::eFragment,
                fragmentModule,
                "main",
            },
        };

        vk::PipelineVertexInputStateCreateInfo emptyInputCreateInfo;

        vk::PipelineInputAssemblyStateCreateInfo inputAssemblyCreateInfo;
        inputAssemblyCreateInfo.topology = vk::PrimitiveTopology::eTriangleList;
        inputAssemblyCreateInfo.primitiveRestartEnable = false;

        vk::Viewport viewport;
        viewport.width = static_cast<float>(this->swapChainExtent.width);
        viewport.height = static_cast<float>(this->swapChainExtent.height);
        viewport.minDepth = 0.f;
        viewport.maxDepth = 1.f;

        vk::Rect2D scissor;
        scissor.extent = this->swapChainExtent;

        vk::PipelineViewportStateCreateInfo viewportStateCreateInfo;
        viewportStateCreateInfo.viewportCount = 1;
        viewportStateCreateInfo.pViewports = &viewport;
        viewportStateCreateInfo.scissorCount = 1;
        viewportStateCreateInfo.pScissors = &scissor;

        vk::PipelineRasterizationStateCreateInfo rasterizationStateCreateInfo;
        rasterizationStateCreateInfo.cullMode = vk::CullModeFlagBits::eFront;
        rasterizationStateCreateInfo.frontFace = vk::FrontFace::eCounterClockwise;
        rasterizationStateCreateInfo.lineWidth = 1.f;
        rasterizationStateCreateInfo.polygonMode = vk::PolygonMode::eFill;

        vk::PipelineMultisampleStateCreateInfo multiSampleStageCreateInfo;
        multiSampleStageCreateInfo.sampleShadingEnable = false;
        multiSampleStageCreateInfo.rasterizationSamples = vk::SampleCountFlagBits::e1;

        vk::PipelineColorBlendAttachmentState colorBlendAttachmentState;
        colorBlendAttachmentState.colorWriteMask = vk::ColorComponentFlagBits::eR |
                                                   vk::ColorComponentFlagBits::eG |
                                                   vk::ColorComponentFlagBits::eB |
                                                   vk::ColorComponentFlagBits::eA;
        colorBlendAttachmentState.blendEnable = false;

        vk::PipelineColorBlendStateCreateInfo colorBlendStateCreateInfo;
        colorBlendStateCreateInfo.logicOpEnable = false;
        colorBlendStateCreateInfo.logicOp = vk::LogicOp::eCopy;
        colorBlendStateCreateInfo.attachmentCount = 1;
        colorBlendStateCreateInfo.pAttachments = &colorBlendAttachmentState;

        vk::PipelineLayoutCreateInfo layoutCreateInfo;
        layoutCreateInfo.setLayoutCount = 1;
        layoutCreateInfo.pSetLayouts = &this->rasterizationStep.descriptorSetLayout;

        this->rasterizationStep.pipelineLayout = this->logicalDevice.createPipelineLayout(layoutCreateInfo);

        vk::GraphicsPipelineCreateInfo createInfo;
        createInfo.stageCount = static_cast<uint32_t>(shaderStages.size());
        createInfo.pStages = shaderStages.data();
        createInfo.pVertexInputState = &emptyInputCreateInfo;
        createInfo.pInputAssemblyState = &inputAssemblyCreateInfo;
        createInfo.pViewportState = &viewportStateCreateInfo;
        createInfo.pRasterizationState = &rasterizationStateCreateInfo;
        createInfo.pMultisampleState = &multiSampleStageCreateInfo;
        createInfo.pColorBlendState = &colorBlendStateCreateInfo;
        createInfo.layout = this->rasterizationStep.pipelineLayout;

Lukas Tietze's avatar
Lukas Tietze committed
124 125
        const auto result = this->logicalDevice.createGraphicsPipeline(nullptr, createInfo);
        this->rasterizationStep.pipeline = result.value;
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    }

    if (vertexModule)
    {
        this->logicalDevice.destroyShaderModule(vertexModule);
    }

    if (fragmentModule)
    {
        this->logicalDevice.destroyShaderModule(fragmentModule);
    }

    return success;
}

void lib::CgContext::CreateRasterizationDescriptorSets()
{
Lukas Tietze's avatar
Lukas Tietze committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    const auto count = static_cast<uint32_t>(this->frames.size());

    std::vector<vk::DescriptorSetLayout> layouts(count, this->rasterizationStep.descriptorSetLayout);

    vk::DescriptorSetAllocateInfo allocateInfo;
    allocateInfo.descriptorPool = this->rasterizationStep.descriptorPool;
    allocateInfo.descriptorSetCount = count;
    allocateInfo.pSetLayouts = layouts.data();

    this->rasterizationStep.descriptorSets = this->logicalDevice.allocateDescriptorSets(allocateInfo);

    for (uint32_t i = 0; i < count; i++)
    {
        vk::DescriptorImageInfo rtImageInfo;
        rtImageInfo.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
        rtImageInfo.imageView = this->frames[i].raytracingTexture->GetView();
        rtImageInfo.sampler = this->frames[i].raytracingTexture->GetSampler();

        vk::WriteDescriptorSet rtImageWrite;
        rtImageWrite.descriptorCount = 1;
        rtImageWrite.descriptorType = vk::DescriptorType::eCombinedImageSampler;
        rtImageWrite.dstArrayElement = 0;
        rtImageWrite.dstBinding = 0;
        rtImageWrite.pImageInfo = &rtImageInfo;
        rtImageWrite.dstSet = this->rasterizationStep.descriptorSets[i];

        this->logicalDevice.updateDescriptorSets(rtImageWrite, {});
    }
171 172 173 174
}

void lib::CgContext::CreateRasterizationDescriptorPool()
{
Lukas Tietze's avatar
Lukas Tietze committed
175 176 177
    const auto count = static_cast<uint32_t>(this->frames.size());

    std::vector<vk::DescriptorPoolSize> poolSizes{
Lukas Tietze's avatar
Lukas Tietze committed
178
        {vk::DescriptorType::eCombinedImageSampler, count},
Lukas Tietze's avatar
Lukas Tietze committed
179 180 181 182 183 184 185 186
    };

    vk::DescriptorPoolCreateInfo createInfo;
    createInfo.poolSizeCount = static_cast<uint32_t>(poolSizes.size());
    createInfo.pPoolSizes = poolSizes.data();
    createInfo.maxSets = count;

    this->rasterizationStep.descriptorPool = this->logicalDevice.createDescriptorPool(createInfo);
187 188 189 190 191
}

void lib::CgContext::CreateRasterizationDescriptorSetLayout()
{
    std::vector<vk::DescriptorSetLayoutBinding> layoutBindings;
Lukas Tietze's avatar
Lukas Tietze committed
192

Lukas Tietze's avatar
Lukas Tietze committed
193
    auto &item = layoutBindings.emplace_back();
Lukas Tietze's avatar
Lukas Tietze committed
194 195
    item.binding = 0;
    item.descriptorType = vk::DescriptorType::eCombinedImageSampler;
Lukas Tietze's avatar
Lukas Tietze committed
196 197 198 199 200 201 202 203
    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);
204 205 206 207
}

void lib::CgContext::CreateRasterizationRenderPass()
{
Lukas Tietze's avatar
Lukas Tietze committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    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;

234
    vk::RenderPassCreateInfo createInfo;
Lukas Tietze's avatar
Lukas Tietze committed
235 236 237 238 239 240 241 242
    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);
243
}