1 | #include "graphics-pipeline_vulkan.hpp"
|
---|
2 |
|
---|
3 | #include <fstream>
|
---|
4 | #include <stdexcept>
|
---|
5 |
|
---|
6 | GraphicsPipeline_Vulkan::GraphicsPipeline_Vulkan(VkDevice device) {
|
---|
7 | this->device = device;
|
---|
8 | }
|
---|
9 |
|
---|
10 | GraphicsPipeline_Vulkan::~GraphicsPipeline_Vulkan() {
|
---|
11 | }
|
---|
12 |
|
---|
13 | void GraphicsPipeline_Vulkan::createPipeline(string vertShaderFile, string fragShaderFile) {
|
---|
14 | vector<char> vertShaderCode = readFile(vertShaderFile);
|
---|
15 | vector<char> fragShaderCode = readFile(fragShaderFile);
|
---|
16 |
|
---|
17 | VkShaderModule vertShaderModule = createShaderModule(vertShaderCode);
|
---|
18 | VkShaderModule fragShaderModule = createShaderModule(fragShaderCode);
|
---|
19 |
|
---|
20 | VkPipelineShaderStageCreateInfo vertShaderStageInfo = {};
|
---|
21 | vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
---|
22 | vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
|
---|
23 | vertShaderStageInfo.module = vertShaderModule;
|
---|
24 | vertShaderStageInfo.pName = "main";
|
---|
25 |
|
---|
26 | VkPipelineShaderStageCreateInfo fragShaderStageInfo = {};
|
---|
27 | fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
---|
28 | fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
|
---|
29 | fragShaderStageInfo.module = fragShaderModule;
|
---|
30 | fragShaderStageInfo.pName = "main";
|
---|
31 |
|
---|
32 | VkPipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo, fragShaderStageInfo };
|
---|
33 |
|
---|
34 | VkPipelineVertexInputStateCreateInfo vertexInputInfo = {};
|
---|
35 | vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
|
---|
36 |
|
---|
37 | vkDestroyShaderModule(device, vertShaderModule, nullptr);
|
---|
38 | vkDestroyShaderModule(device, fragShaderModule, nullptr);
|
---|
39 | }
|
---|
40 |
|
---|
41 | VkShaderModule GraphicsPipeline_Vulkan::createShaderModule(const vector<char>& code) {
|
---|
42 | VkShaderModuleCreateInfo createInfo = {};
|
---|
43 | createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
|
---|
44 | createInfo.codeSize = code.size();
|
---|
45 | createInfo.pCode = reinterpret_cast<const uint32_t*>(code.data());
|
---|
46 |
|
---|
47 | VkShaderModule shaderModule;
|
---|
48 | if (vkCreateShaderModule(device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS) {
|
---|
49 | throw runtime_error("failed to create shader module!");
|
---|
50 | }
|
---|
51 |
|
---|
52 | return shaderModule;
|
---|
53 | }
|
---|
54 |
|
---|
55 | vector<char> GraphicsPipeline_Vulkan::readFile(const string& filename) {
|
---|
56 | ifstream file(filename, ios::ate | ios::binary);
|
---|
57 |
|
---|
58 | if (!file.is_open()) {
|
---|
59 | throw runtime_error("failed to open file!");
|
---|
60 | }
|
---|
61 |
|
---|
62 | size_t fileSize = (size_t)file.tellg();
|
---|
63 | vector<char> buffer(fileSize);
|
---|
64 |
|
---|
65 | file.seekg(0);
|
---|
66 | file.read(buffer.data(), fileSize);
|
---|
67 |
|
---|
68 | file.close();
|
---|
69 |
|
---|
70 | return buffer;
|
---|
71 | }
|
---|