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