CgContext.Rasterization.cpp 7.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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
#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;

        this->rasterizationStep.pipeline = this->logicalDevice.createGraphicsPipeline(nullptr, createInfo);
    }

    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
142 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
    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, {});
    }
170 171 172 173
}

void lib::CgContext::CreateRasterizationDescriptorPool()
{
Lukas Tietze's avatar
Lukas Tietze committed
174 175 176 177 178 179 180 181 182 183 184 185
    const auto count = static_cast<uint32_t>(this->frames.size());

    std::vector<vk::DescriptorPoolSize> poolSizes{
        {vk::DescriptorType::eCombinedImageSampler, count * 2},
    };

    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);
186 187 188 189 190
}

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

    auto & item = layoutBindings.emplace_back();
    item.binding = 0;
    item.descriptorCount = 1;
    item.descriptorType = vk::DescriptorType::eCombinedImageSampler;
    // item.pImmutableSamplers = &this->
197 198 199 200 201 202 203
}

void lib::CgContext::CreateRasterizationRenderPass()
{
    // TODO
    vk::RenderPassCreateInfo createInfo;
}