source: opengl-game/graphics-pipeline_vulkan.cpp@ 83b5b4b

feature/imgui-sdl points-test
Last change on this file since 83b5b4b was 7d2b0b9, checked in by Dmitry Portnoy <dmp1488@…>, 5 years ago

Add and begin implementing a GraphicsPipeline class to hold info for the Vulkan graphics pipeline

  • Property mode set to 100644
File size: 2.4 KB
Line 
1#include "graphics-pipeline_vulkan.hpp"
2
3#include <fstream>
4#include <stdexcept>
5
6GraphicsPipeline_Vulkan::GraphicsPipeline_Vulkan(VkDevice device) {
7 this->device = device;
8}
9
10GraphicsPipeline_Vulkan::~GraphicsPipeline_Vulkan() {
11}
12
13void 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
41VkShaderModule 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
55vector<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}
Note: See TracBrowser for help on using the repository browser.