[7d2b0b9] | 1 | #include "graphics-pipeline_vulkan.hpp"
|
---|
| 2 |
|
---|
| 3 | #include <fstream>
|
---|
| 4 | #include <stdexcept>
|
---|
| 5 |
|
---|
[771b33a] | 6 | GraphicsPipeline_Vulkan::GraphicsPipeline_Vulkan(VkDevice device, Viewport viewport, int vertexSize) {
|
---|
[7d2b0b9] | 7 | this->device = device;
|
---|
[771b33a] | 8 | this->viewport = viewport;
|
---|
| 9 |
|
---|
| 10 | this->bindingDescription.binding = 0;
|
---|
| 11 | this->bindingDescription.stride = vertexSize;
|
---|
| 12 | this->bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
|
---|
[7d2b0b9] | 13 | }
|
---|
| 14 |
|
---|
| 15 | GraphicsPipeline_Vulkan::~GraphicsPipeline_Vulkan() {
|
---|
| 16 | }
|
---|
| 17 |
|
---|
[771b33a] | 18 | void GraphicsPipeline_Vulkan::addAttribute(VkFormat format, size_t offset) {
|
---|
| 19 | VkVertexInputAttributeDescription attributeDesc = {};
|
---|
| 20 |
|
---|
| 21 | attributeDesc.binding = 0;
|
---|
| 22 | attributeDesc.location = this->attributeDescriptions.size();
|
---|
| 23 | attributeDesc.format = format;
|
---|
| 24 | attributeDesc.offset = offset;
|
---|
| 25 |
|
---|
| 26 | this->attributeDescriptions.push_back(attributeDesc);
|
---|
| 27 | }
|
---|
| 28 |
|
---|
[7d2b0b9] | 29 | void GraphicsPipeline_Vulkan::createPipeline(string vertShaderFile, string fragShaderFile) {
|
---|
| 30 | vector<char> vertShaderCode = readFile(vertShaderFile);
|
---|
| 31 | vector<char> fragShaderCode = readFile(fragShaderFile);
|
---|
| 32 |
|
---|
| 33 | VkShaderModule vertShaderModule = createShaderModule(vertShaderCode);
|
---|
| 34 | VkShaderModule fragShaderModule = createShaderModule(fragShaderCode);
|
---|
| 35 |
|
---|
| 36 | VkPipelineShaderStageCreateInfo vertShaderStageInfo = {};
|
---|
| 37 | vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
---|
| 38 | vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
|
---|
| 39 | vertShaderStageInfo.module = vertShaderModule;
|
---|
| 40 | vertShaderStageInfo.pName = "main";
|
---|
| 41 |
|
---|
| 42 | VkPipelineShaderStageCreateInfo fragShaderStageInfo = {};
|
---|
| 43 | fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
---|
| 44 | fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
|
---|
| 45 | fragShaderStageInfo.module = fragShaderModule;
|
---|
| 46 | fragShaderStageInfo.pName = "main";
|
---|
| 47 |
|
---|
| 48 | VkPipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo, fragShaderStageInfo };
|
---|
| 49 |
|
---|
| 50 | VkPipelineVertexInputStateCreateInfo vertexInputInfo = {};
|
---|
| 51 | vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
|
---|
| 52 |
|
---|
[771b33a] | 53 | vertexInputInfo.vertexBindingDescriptionCount = 1;
|
---|
| 54 | vertexInputInfo.vertexAttributeDescriptionCount = static_cast<uint32_t>(this->attributeDescriptions.size());
|
---|
| 55 | vertexInputInfo.pVertexBindingDescriptions = &this->bindingDescription;
|
---|
| 56 | vertexInputInfo.pVertexAttributeDescriptions = this->attributeDescriptions.data();
|
---|
| 57 |
|
---|
| 58 | VkPipelineInputAssemblyStateCreateInfo inputAssembly = {};
|
---|
| 59 | inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
|
---|
| 60 | inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
|
---|
| 61 | inputAssembly.primitiveRestartEnable = VK_FALSE;
|
---|
| 62 |
|
---|
| 63 | VkViewport viewport = {};
|
---|
| 64 | viewport.x = (float)this->viewport.x;
|
---|
| 65 | viewport.y = (float)this->viewport.y;
|
---|
| 66 | viewport.width = (float)this->viewport.width;
|
---|
| 67 | viewport.height = (float)this->viewport.height;
|
---|
| 68 | viewport.minDepth = 0.0f;
|
---|
| 69 | viewport.maxDepth = 1.0f;
|
---|
| 70 |
|
---|
| 71 | VkRect2D scissor = {};
|
---|
| 72 | scissor.offset = { 0, 0 };
|
---|
| 73 | scissor.extent = { (uint32_t)this->viewport.width, (uint32_t)this->viewport.height };
|
---|
| 74 |
|
---|
| 75 | VkPipelineViewportStateCreateInfo viewportState = {};
|
---|
| 76 | viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
|
---|
| 77 | viewportState.viewportCount = 1;
|
---|
| 78 | viewportState.pViewports = &viewport;
|
---|
| 79 | viewportState.scissorCount = 1;
|
---|
| 80 | viewportState.pScissors = &scissor;
|
---|
| 81 |
|
---|
| 82 | VkPipelineRasterizationStateCreateInfo rasterizer = {};
|
---|
| 83 | rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
|
---|
| 84 | rasterizer.depthClampEnable = VK_FALSE;
|
---|
| 85 | rasterizer.rasterizerDiscardEnable = VK_FALSE;
|
---|
| 86 | rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
|
---|
| 87 | rasterizer.lineWidth = 1.0f;
|
---|
| 88 | rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
|
---|
| 89 | rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
|
---|
| 90 | rasterizer.depthBiasEnable = VK_FALSE;
|
---|
| 91 |
|
---|
| 92 | VkPipelineMultisampleStateCreateInfo multisampling = {};
|
---|
| 93 | multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
|
---|
| 94 | multisampling.sampleShadingEnable = VK_FALSE;
|
---|
| 95 | multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
|
---|
| 96 |
|
---|
| 97 | VkPipelineColorBlendAttachmentState colorBlendAttachment = {};
|
---|
| 98 | colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
|
---|
| 99 | colorBlendAttachment.blendEnable = VK_TRUE;
|
---|
| 100 | colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
|
---|
| 101 | colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
|
---|
| 102 | colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
|
---|
| 103 | colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
|
---|
| 104 | colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
|
---|
| 105 | colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
|
---|
| 106 |
|
---|
| 107 | VkPipelineColorBlendStateCreateInfo colorBlending = {};
|
---|
| 108 | colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
|
---|
| 109 | colorBlending.logicOpEnable = VK_FALSE;
|
---|
| 110 | colorBlending.logicOp = VK_LOGIC_OP_COPY;
|
---|
| 111 | colorBlending.attachmentCount = 1;
|
---|
| 112 | colorBlending.pAttachments = &colorBlendAttachment;
|
---|
| 113 | colorBlending.blendConstants[0] = 0.0f;
|
---|
| 114 | colorBlending.blendConstants[1] = 0.0f;
|
---|
| 115 | colorBlending.blendConstants[2] = 0.0f;
|
---|
| 116 | colorBlending.blendConstants[3] = 0.0f;
|
---|
| 117 |
|
---|
| 118 | VkPipelineDepthStencilStateCreateInfo depthStencil = {};
|
---|
| 119 | depthStencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
|
---|
| 120 | depthStencil.depthTestEnable = VK_TRUE;
|
---|
| 121 | depthStencil.depthWriteEnable = VK_TRUE;
|
---|
| 122 | depthStencil.depthCompareOp = VK_COMPARE_OP_LESS;
|
---|
| 123 | depthStencil.depthBoundsTestEnable = VK_FALSE;
|
---|
| 124 | depthStencil.minDepthBounds = 0.0f;
|
---|
| 125 | depthStencil.maxDepthBounds = 1.0f;
|
---|
| 126 | depthStencil.stencilTestEnable = VK_FALSE;
|
---|
| 127 | depthStencil.front = {};
|
---|
| 128 | depthStencil.back = {};
|
---|
| 129 |
|
---|
| 130 | VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
|
---|
| 131 | pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
---|
| 132 | pipelineLayoutInfo.setLayoutCount = 1;
|
---|
| 133 |
|
---|
[7d2b0b9] | 134 | vkDestroyShaderModule(device, vertShaderModule, nullptr);
|
---|
| 135 | vkDestroyShaderModule(device, fragShaderModule, nullptr);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | VkShaderModule GraphicsPipeline_Vulkan::createShaderModule(const vector<char>& code) {
|
---|
| 139 | VkShaderModuleCreateInfo createInfo = {};
|
---|
| 140 | createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
|
---|
| 141 | createInfo.codeSize = code.size();
|
---|
| 142 | createInfo.pCode = reinterpret_cast<const uint32_t*>(code.data());
|
---|
| 143 |
|
---|
| 144 | VkShaderModule shaderModule;
|
---|
| 145 | if (vkCreateShaderModule(device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS) {
|
---|
| 146 | throw runtime_error("failed to create shader module!");
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | return shaderModule;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | vector<char> GraphicsPipeline_Vulkan::readFile(const string& filename) {
|
---|
| 153 | ifstream file(filename, ios::ate | ios::binary);
|
---|
| 154 |
|
---|
| 155 | if (!file.is_open()) {
|
---|
| 156 | throw runtime_error("failed to open file!");
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | size_t fileSize = (size_t)file.tellg();
|
---|
| 160 | vector<char> buffer(fileSize);
|
---|
| 161 |
|
---|
| 162 | file.seekg(0);
|
---|
| 163 | file.read(buffer.data(), fileSize);
|
---|
| 164 |
|
---|
| 165 | file.close();
|
---|
| 166 |
|
---|
| 167 | return buffer;
|
---|
| 168 | }
|
---|