Skip to content
Snippets Groups Projects
PipelineManager.cpp 11.30 KiB
#include "PipelineManager.hpp"
#include "vkcv/Image.hpp"
#include "vkcv/Logger.hpp"

namespace vkcv
{

    PipelineManager::PipelineManager(vk::Device device) noexcept :
    m_Device{device},
    m_Pipelines{},
    m_Configs{}
    {}

    PipelineManager::~PipelineManager() noexcept
    {
    	for (uint64_t id = 0; id < m_Pipelines.size(); id++) {
			destroyPipelineById(id);
    	}
    }

	// currently assuming default 32 bit formats, no lower precision or normalized variants supported
	vk::Format vertexFormatToVulkanFormat(const VertexFormat format) {
		switch (format) {
		case VertexFormat::FLOAT:
			return vk::Format::eR32Sfloat;
		case VertexFormat::FLOAT2:
			return vk::Format::eR32G32Sfloat;
		case VertexFormat::FLOAT3:
			return vk::Format::eR32G32B32Sfloat;
		case VertexFormat::FLOAT4:
			return vk::Format::eR32G32B32A32Sfloat;
		case VertexFormat::INT:
			return vk::Format::eR32Sint;
		case VertexFormat::INT2:
			return vk::Format::eR32G32Sint;
		case VertexFormat::INT3:
			return vk::Format::eR32G32B32Sint;
		case VertexFormat::INT4:
			return vk::Format::eR32G32B32A32Sint;
		default:
			vkcv_log(vkcv::LogLevel::WARNING, "Unknown vertex format");
			return vk::Format::eUndefined;
		}
	}

    PipelineHandle PipelineManager::createPipeline(const PipelineConfig &config, PassManager& passManager)
    {
		const vk::RenderPass &pass = passManager.getVkPass(config.m_PassHandle);
    	
        const bool existsVertexShader = config.m_ShaderProgram.existsShader(ShaderStage::VERTEX);
        const bool existsFragmentShader = config.m_ShaderProgram.existsShader(ShaderStage::FRAGMENT);
        if (!(existsVertexShader && existsFragmentShader))
        {
			vkcv_log(vkcv::LogLevel::ERROR, "Requires vertex and fragment shader code");
            return PipelineHandle();
        }

        // vertex shader stage
        std::vector<char> vertexCode = config.m_ShaderProgram.getShader(ShaderStage::VERTEX).shaderCode;
        vk::ShaderModuleCreateInfo vertexModuleInfo({}, vertexCode.size(), reinterpret_cast<uint32_t*>(vertexCode.data()));
        vk::ShaderModule vertexModule{};
        if (m_Device.createShaderModule(&vertexModuleInfo, nullptr, &vertexModule) != vk::Result::eSuccess)
            return PipelineHandle();

        vk::PipelineShaderStageCreateInfo pipelineVertexShaderStageInfo(
                {},
                vk::ShaderStageFlagBits::eVertex,
                vertexModule,
                "main",
                nullptr