vengine  0.1.0
3D graphics engine made with Vulkan
Loading...
Searching...
No Matches
shaders.cpp
Go to the documentation of this file.
1#include <stdexcept>
2#include <fstream>
3
4#include "VEngine/Shaders.hpp"
5
7{
8 vkDestroyShaderModule(m_device.device(), m_vertShaderModule, nullptr);
9 vkDestroyShaderModule(m_device.device(), m_fragShaderModule, nullptr);
10 vkDestroyPipeline(m_device.device(), m_graphicsPipeline, nullptr);
11}
12
13std::vector<char> ven::Shaders::readFile(const std::string &filename)
14{
15 std::ifstream file(filename, std::ios::ate | std::ios::binary);
16
17 if (!file.is_open()) {
18 throw std::runtime_error("failed to open file!");
19 }
20
21 const std::streamsize fileSize = file.tellg();
22 std::vector<char> buffer(static_cast<unsigned long>(fileSize));
23
24 file.seekg(0);
25 file.read(buffer.data(), fileSize);
26
27 file.close();
28 return buffer;
29}
30
31void ven::Shaders::createGraphicsPipeline(const std::string& vertFilepath, const std::string& fragFilepath, const PipelineConfigInfo& configInfo)
32{
33 const std::vector<char> vertCode = readFile(vertFilepath);
34 const std::vector<char> fragCode = readFile(fragFilepath);
35
36 createShaderModule(vertCode, &m_vertShaderModule);
37 createShaderModule(fragCode, &m_fragShaderModule);
38
39 VkPipelineShaderStageCreateInfo shaderStages[2];
40 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
41 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
42 shaderStages[0].module = m_vertShaderModule;
43 shaderStages[0].pName = "main";
44 shaderStages[0].flags = 0;
45 shaderStages[0].pNext = nullptr;
46 shaderStages[0].pSpecializationInfo = nullptr;
47
48 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
49 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
50 shaderStages[1].module = m_fragShaderModule;
51 shaderStages[1].pName = "main";
52 shaderStages[1].flags = 0;
53 shaderStages[1].pNext = nullptr;
54 shaderStages[1].pSpecializationInfo = nullptr;
55
56 const auto& bindingDescriptions = configInfo.bindingDescriptions;
57 const auto& attributeDescriptions = configInfo.attributeDescriptions;
58 VkPipelineVertexInputStateCreateInfo vertexInputInfo{};
59 vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
60 vertexInputInfo.vertexAttributeDescriptionCount = static_cast<uint32_t>(attributeDescriptions.size());
61 vertexInputInfo.vertexBindingDescriptionCount = static_cast<uint32_t>(bindingDescriptions.size());
62 vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions.data();
63 vertexInputInfo.pVertexBindingDescriptions = bindingDescriptions.data();
64
65
66 VkPipelineViewportStateCreateInfo viewportInfo{};
67 viewportInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
68 viewportInfo.viewportCount = 1;
69 viewportInfo.pViewports = nullptr;
70 viewportInfo.scissorCount = 1;
71 viewportInfo.pScissors = nullptr;
72
73
74 VkGraphicsPipelineCreateInfo pipelineInfo{};
75 pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
76 pipelineInfo.stageCount = 2;
77 pipelineInfo.pStages = shaderStages;
78 pipelineInfo.pVertexInputState = &vertexInputInfo;
79 pipelineInfo.pInputAssemblyState = &configInfo.inputAssemblyInfo;
80 pipelineInfo.pViewportState = &viewportInfo;
81 pipelineInfo.pRasterizationState = &configInfo.rasterizationInfo;
82 pipelineInfo.pMultisampleState = &configInfo.multisampleInfo;
83
84 pipelineInfo.pColorBlendState = &configInfo.colorBlendInfo;
85 pipelineInfo.pDepthStencilState = &configInfo.depthStencilInfo;
86 pipelineInfo.pDynamicState = &configInfo.dynamicStateInfo;
87
88 pipelineInfo.layout = configInfo.pipelineLayout;
89 pipelineInfo.renderPass = configInfo.renderPass;
90 pipelineInfo.subpass = configInfo.subpass;
91
92 pipelineInfo.basePipelineIndex = -1;
93 pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
94
95 if (vkCreateGraphicsPipelines(m_device.device(), VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &m_graphicsPipeline) != VK_SUCCESS) {
96 throw std::runtime_error("failed to create graphics pipeline");
97 }
98}
99
100void ven::Shaders::createShaderModule(const std::vector<char> &code, VkShaderModule *shaderModule) const
101{
102 VkShaderModuleCreateInfo createInfo{};
103 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
104 createInfo.codeSize = code.size();
105 createInfo.pCode = reinterpret_cast<const uint32_t*>(code.data());
106
107 if (vkCreateShaderModule(m_device.device(), &createInfo, nullptr, shaderModule) != VK_SUCCESS) {
108 throw std::runtime_error("failed to create shader module");
109 }
110}
111
113{
114 configInfo.inputAssemblyInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
115 configInfo.inputAssemblyInfo.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
116 configInfo.inputAssemblyInfo.primitiveRestartEnable = VK_FALSE;
117
118 configInfo.rasterizationInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
119 configInfo.rasterizationInfo.depthClampEnable = VK_FALSE;
120 configInfo.rasterizationInfo.rasterizerDiscardEnable = VK_FALSE;
121 configInfo.rasterizationInfo.polygonMode = VK_POLYGON_MODE_FILL;
122 configInfo.rasterizationInfo.lineWidth = 1.0F;
123 configInfo.rasterizationInfo.cullMode = VK_CULL_MODE_NONE;
124 configInfo.rasterizationInfo.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
125 configInfo.rasterizationInfo.depthBiasEnable = VK_FALSE;
126 configInfo.rasterizationInfo.depthBiasConstantFactor = 0.0F;
127 configInfo.rasterizationInfo.depthBiasClamp = 0.0F;
128 configInfo.rasterizationInfo.depthBiasSlopeFactor = 0.0F;
129
130 configInfo.multisampleInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
131 configInfo.multisampleInfo.sampleShadingEnable = VK_FALSE;
132 configInfo.multisampleInfo.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
133 configInfo.multisampleInfo.minSampleShading = 1.0F;
134 configInfo.multisampleInfo.pSampleMask = nullptr;
135 configInfo.multisampleInfo.alphaToCoverageEnable = VK_FALSE;
136 configInfo.multisampleInfo.alphaToOneEnable = VK_FALSE;
137
138 configInfo.colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
139 configInfo.colorBlendAttachment.blendEnable = VK_FALSE;
140 configInfo.colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_ONE;
141 configInfo.colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ZERO;
142 configInfo.colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
143 configInfo.colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
144 configInfo.colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
145 configInfo.colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
146
147 configInfo.colorBlendInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
148 configInfo.colorBlendInfo.logicOpEnable = VK_FALSE;
149 configInfo.colorBlendInfo.logicOp = VK_LOGIC_OP_COPY;
150 configInfo.colorBlendInfo.attachmentCount = 1;
151 configInfo.colorBlendInfo.pAttachments = &configInfo.colorBlendAttachment;
152 configInfo.colorBlendInfo.blendConstants[0] = 0.0F;
153 configInfo.colorBlendInfo.blendConstants[1] = 0.0F;
154 configInfo.colorBlendInfo.blendConstants[2] = 0.0F;
155 configInfo.colorBlendInfo.blendConstants[3] = 0.0F;
156
157 configInfo.depthStencilInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
158 configInfo.depthStencilInfo.depthTestEnable = VK_TRUE;
159 configInfo.depthStencilInfo.depthWriteEnable = VK_TRUE;
160 configInfo.depthStencilInfo.depthCompareOp = VK_COMPARE_OP_LESS;
161 configInfo.depthStencilInfo.depthBoundsTestEnable = VK_FALSE;
162 configInfo.depthStencilInfo.minDepthBounds = 0.0F;
163 configInfo.depthStencilInfo.maxDepthBounds = 1.0F;
164 configInfo.depthStencilInfo.stencilTestEnable = VK_FALSE;
165 configInfo.depthStencilInfo.front = {};
166 configInfo.depthStencilInfo.back = {};
167
168 configInfo.dynamicStateEnables = {VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR};
169 configInfo.dynamicStateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
170 configInfo.dynamicStateInfo.pDynamicStates = configInfo.dynamicStateEnables.data();
171 configInfo.dynamicStateInfo.dynamicStateCount = static_cast<uint32_t>(configInfo.dynamicStateEnables.size());
172 configInfo.dynamicStateInfo.flags = 0;
175}
This file contains the Shader class.
VkDevice device() const
Definition Device.hpp:48
VkPipeline m_graphicsPipeline
Definition Shaders.hpp:59
void createGraphicsPipeline(const std::string &vertFilepath, const std::string &fragFilepath, const PipelineConfigInfo &configInfo)
Definition shaders.cpp:31
Device & m_device
Definition Shaders.hpp:58
static std::vector< char > readFile(const std::string &filename)
Definition shaders.cpp:13
void createShaderModule(const std::vector< char > &code, VkShaderModule *shaderModule) const
Definition shaders.cpp:100
static void defaultPipelineConfigInfo(PipelineConfigInfo &configInfo)
Definition shaders.cpp:112
VkShaderModule m_vertShaderModule
Definition Shaders.hpp:60
VkShaderModule m_fragShaderModule
Definition Shaders.hpp:61
static std::vector< VkVertexInputAttributeDescription > getAttributeDescriptions()
Definition model.cpp:108
static std::vector< VkVertexInputBindingDescription > getBindingDescriptions()
Definition model.cpp:99
std::vector< VkDynamicState > dynamicStateEnables
Definition Shaders.hpp:32
std::vector< VkVertexInputBindingDescription > bindingDescriptions
Definition Shaders.hpp:24
VkPipelineDepthStencilStateCreateInfo depthStencilInfo
Definition Shaders.hpp:31
VkPipelineRasterizationStateCreateInfo rasterizationInfo
Definition Shaders.hpp:27
std::vector< VkVertexInputAttributeDescription > attributeDescriptions
Definition Shaders.hpp:25
VkPipelineColorBlendAttachmentState colorBlendAttachment
Definition Shaders.hpp:29
VkPipelineInputAssemblyStateCreateInfo inputAssemblyInfo
Definition Shaders.hpp:26
VkPipelineMultisampleStateCreateInfo multisampleInfo
Definition Shaders.hpp:28
VkPipelineColorBlendStateCreateInfo colorBlendInfo
Definition Shaders.hpp:30
VkPipelineDynamicStateCreateInfo dynamicStateInfo
Definition Shaders.hpp:33
VkRenderPass renderPass
Definition Shaders.hpp:35
VkPipelineLayout pipelineLayout
Definition Shaders.hpp:34