Files
Cheap-Vulkan-Renderer/cve_pipeline.cpp
2026-02-20 10:33:11 +00:00

60 lines
2.1 KiB
C++

#include "cve_pipeline.hpp"
#include <fstream>
#include <stdexcept>
#include <iostream>
namespace cve {
CvePipeline::CvePipeline(CveDevice &device, const std::string& vertFilepath,
const std::string& fragFilepath, const PipelineConfigInfo &configInfo) : cveDevice{device} {
createGraphicsPipeline(vertFilepath, fragFilepath, configInfo);
}
std::vector<char> CvePipeline::readFile(const std::string& filepath) {
std::ifstream file{filepath, std::ios::ate | std::ios::binary};
if (!file.is_open()) {
throw std::runtime_error("Failed to open file: "
+ filepath);
}
size_t fileSize = static_cast<size_t>(file.tellg());
std::vector<char> buffer(fileSize);
file.seekg(0);
file.read(buffer.data(), fileSize);
file.close();
return buffer;
}
void CvePipeline::createGraphicsPipeline(const std::string& vertFilepath,
const std::string& fragFilepath, const PipelineConfigInfo &configInfo) {
auto vertCode = readFile(vertFilepath);
auto fragCode = readFile(fragFilepath);
std::cout << "Vertex shader code size: " << vertCode.size() << '\n';
std::cout << "Fragment shader code size: " << fragCode.size() << '\n';
}
void CvePipeline::createShaderModule(const std::vector<char>& code, VkShaderModule *shaderModule) {
VkShaderModuleCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
createInfo.codeSize = code.size();
createInfo.pCode = reinterpret_cast<const uint32_t*>(code.data());
if (vkCreateShaderModule(cveDevice.device(), &createInfo, nullptr, shaderModule) != VK_SUCCESS) {
throw std::runtime_error("Failed to create shader module.");
}
}
PipelineConfigInfo CvePipeline::defaultPipelineConfigInfo(uint32_t width, uint32_t height) {
PipelineConfigInfo configInfo{};
configInfo.inputAssemblyInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
configInfo.inputAssemblyInfo.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
configInfo.inputAssemblyInfo.primitiveRestartEnable = VK_FALSE;
return configInfo;
}
}