CwGraphics-GraphicsPipeline.cpp
1 #include <Clockworks/CwGraphics.hpp> 2 #include <fstream> 3 4 std::vector<VkDynamicState> dynamicStates = {VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR}; 5 6 VkShaderModule CwGraphics::CreateShaderModule(const std::vector<char> &code) { 7 VkShaderModuleCreateInfo CInfo{}; 8 CInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; 9 CInfo.codeSize = code.size(); 10 CInfo.pCode = reinterpret_cast<const uint32_t *>(code.data()); 11 VkShaderModule Module{}; 12 if (vkCreateShaderModule(Dev, &CInfo, nullptr, &Module) != VK_SUCCESS) { 13 throw std::runtime_error("CW-ERROR: Failed to create Shader Module :(\n"); 14 } 15 return Module; 16 } 17 18 void CwGraphics::CreateGraphicsPipeline() { 19 std::vector<char> vShaderCode = ReadFile("Shaders/vert.spiv"); 20 std::vector<char> fShaderCode = ReadFile("Shaders/frag.spiv"); 21 auto Frag = CreateShaderModule(fShaderCode); 22 auto Vert = CreateShaderModule(vShaderCode); 23 24 VkPipelineShaderStageCreateInfo vShaderStageInfo{}; 25 vShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; 26 vShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT; 27 vShaderStageInfo.module = Vert; 28 vShaderStageInfo.pName = "main"; 29 30 VkPipelineShaderStageCreateInfo fShaderStageInfo{}; 31 fShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; 32 fShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT; 33 fShaderStageInfo.module = Frag; 34 fShaderStageInfo.pName = "main"; 35 36 VkPipelineShaderStageCreateInfo ShaderStages[] = {fShaderStageInfo, vShaderStageInfo}; 37 if (ShaderStages[0].sType != VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) { 38 std::cout << "the heck?!"; 39 } 40 41 VkGraphicsPipelineCreateInfo CInfo{}; 42 VkAttachmentDescription Attachment{}; 43 Attachment.format = ChainImageFormat; 44 Attachment.samples = VK_SAMPLE_COUNT_1_BIT; 45 Attachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; 46 Attachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE; 47 Attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; 48 Attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; 49 Attachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; 50 Attachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; 51 52 VkAttachmentReference AttachmentReference{}; 53 AttachmentReference.attachment = 0; 54 AttachmentReference.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; 55 56 VkSubpassDescription Subpass{}; 57 Subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; 58 Subpass.colorAttachmentCount = 1; 59 Subpass.pColorAttachments = &AttachmentReference; 60 61 VkSubpassDependency Dependency{}; 62 Dependency.srcSubpass = VK_SUBPASS_EXTERNAL; 63 Dependency.dstSubpass = 0; 64 Dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; 65 Dependency.srcAccessMask = 0; 66 Dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; 67 Dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; 68 69 VkRenderPassCreateInfo RenderPassCInfo{}; 70 RenderPassCInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; 71 RenderPassCInfo.attachmentCount = 1; 72 RenderPassCInfo.pAttachments = &Attachment; 73 RenderPassCInfo.subpassCount = 1; 74 RenderPassCInfo.pSubpasses = &Subpass; 75 // Filling the vertex buffer 76 RenderPassCInfo.dependencyCount = 1; 77 RenderPassCInfo.pDependencies = &Dependency; 78 79 if (vkCreateRenderPass(Dev, &RenderPassCInfo, nullptr, &RenderPass) != VK_SUCCESS) 80 throw std::runtime_error("CW-ERROR: Failed Render Pass Creation :("); 81 82 VkPipelineDynamicStateCreateInfo DStateInfo{}; 83 DStateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; 84 DStateInfo.dynamicStateCount = static_cast<uint32_t>(dynamicStates.size()); 85 DStateInfo.pDynamicStates = dynamicStates.data(); 86 87 auto BindingDescription = Vertex::GetBindingDescription(); 88 auto AttributeDescription = Vertex::GetAttriuteDescription(); 89 90 VkPipelineVertexInputStateCreateInfo VInputInfo{}; 91 VInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; 92 VInputInfo.vertexBindingDescriptionCount = 1; 93 VInputInfo.pVertexBindingDescriptions = &BindingDescription; 94 VInputInfo.vertexAttributeDescriptionCount = static_cast<uint32_t>(AttributeDescription.size()); 95 VInputInfo.pVertexAttributeDescriptions = AttributeDescription.data(); 96 97 VkPipelineInputAssemblyStateCreateInfo InputAssembly{}; 98 InputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; 99 InputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; 100 InputAssembly.primitiveRestartEnable = VK_FALSE; 101 102 VkViewport Viewport{}; 103 Viewport.x = 0.0f; 104 Viewport.y = 0.0f; 105 Viewport.width = (float)ChainExtent.width; 106 Viewport.height = (float)ChainExtent.height; 107 Viewport.minDepth = 0.0f; 108 Viewport.maxDepth = 1.0f; 109 110 VkRect2D Scissor{}; 111 Scissor.offset = {0, 0}; 112 Scissor.extent = ChainExtent; 113 114 VkPipelineViewportStateCreateInfo ViewportState{}; 115 ViewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; 116 ViewportState.viewportCount = 1; 117 ViewportState.scissorCount = 1; 118 ViewportState.pViewports = &Viewport; 119 ViewportState.pScissors = &Scissor; 120 121 VkPipelineRasterizationStateCreateInfo Rasterizer{}; 122 Rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; 123 Rasterizer.depthClampEnable = VK_FALSE; 124 Rasterizer.rasterizerDiscardEnable = VK_FALSE; 125 Rasterizer.polygonMode = VK_POLYGON_MODE_FILL; 126 Rasterizer.lineWidth = 1.0f; 127 Rasterizer.cullMode = VK_CULL_MODE_BACK_BIT; 128 Rasterizer.frontFace = VK_FRONT_FACE_CLOCKWISE; 129 Rasterizer.depthBiasEnable = VK_FALSE; 130 Rasterizer.depthBiasConstantFactor = 0.0f; 131 Rasterizer.depthBiasClamp = 0.0f; 132 Rasterizer.depthBiasSlopeFactor = 0.0f; 133 134 VkPipelineMultisampleStateCreateInfo MultiSample{}; 135 MultiSample.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; 136 MultiSample.sampleShadingEnable = VK_FALSE; 137 MultiSample.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; 138 MultiSample.minSampleShading = 1.0f; 139 MultiSample.pSampleMask = nullptr; 140 MultiSample.alphaToCoverageEnable = VK_FALSE; 141 MultiSample.alphaToOneEnable = VK_FALSE; 142 143 VkPipelineColorBlendAttachmentState ColorBlendAttachment{}; 144 ColorBlendAttachment.colorWriteMask = 145 VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; 146 ColorBlendAttachment.blendEnable = VK_FALSE; 147 148 VkPipelineColorBlendStateCreateInfo BlendState{}; 149 BlendState.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; 150 BlendState.logicOpEnable = VK_FALSE; 151 BlendState.attachmentCount = 1; 152 BlendState.pAttachments = &ColorBlendAttachment; 153 BlendState.blendConstants[0] = 0.0f; 154 BlendState.blendConstants[1] = 0.0f; 155 BlendState.blendConstants[2] = 0.0f; 156 BlendState.blendConstants[3] = 0.0f; 157 158 VkPipelineLayoutCreateInfo PipelineLayoutInfo{}; 159 PipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; 160 PipelineLayoutInfo.setLayoutCount = 0; 161 PipelineLayoutInfo.pSetLayouts = nullptr; 162 PipelineLayoutInfo.pushConstantRangeCount = 0; 163 PipelineLayoutInfo.pPushConstantRanges = nullptr; 164 165 if (vkCreatePipelineLayout(Dev, &PipelineLayoutInfo, nullptr, &PipelineLayout) != VK_SUCCESS) 166 throw std::runtime_error("CW-ERROR: Failed Pipeline-layout Creation :( \n)"); 167 168 CInfo.layout = PipelineLayout; 169 CInfo.pVertexInputState = &VInputInfo; 170 CInfo.pInputAssemblyState = &InputAssembly; 171 CInfo.pViewportState = &ViewportState; 172 CInfo.pRasterizationState = &Rasterizer; 173 CInfo.pMultisampleState = &MultiSample; 174 CInfo.pDepthStencilState = nullptr; 175 CInfo.pColorBlendState = &BlendState; 176 CInfo.pDynamicState = &DStateInfo; 177 178 CInfo.renderPass = RenderPass; 179 CInfo.subpass = 0; 180 181 CInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; 182 CInfo.stageCount = 2; 183 CInfo.pStages = ShaderStages; 184 CInfo.basePipelineHandle = VK_NULL_HANDLE; // Diable deriving from pipelines 185 CInfo.basePipelineIndex = -1; // Diable deriving from pipelines 186 187 if (vkCreateGraphicsPipelines(Dev, VK_NULL_HANDLE, 1, &CInfo, nullptr, &GraphicsPipeline) != VK_SUCCESS) 188 throw std::runtime_error("CW-ERROR: Graphics Pipeline Creation failed :("); 189 190 vkDestroyShaderModule(Dev, Frag, nullptr); 191 vkDestroyShaderModule(Dev, Vert, nullptr); 192 std::cout << "Cw-Info: Vulkan successfully boot straped :D\n"; 193 } 194 195 std::vector<char> CwGraphics::ReadFile(const std::string &filename) { 196 std::ifstream file(filename, std::ios::ate | std::ios::binary); 197 if (!file.is_open()) { 198 throw std::runtime_error("CW-ERROR: failed to open file :(\n"); 199 } 200 std::vector<char> Buffer(file.tellg()); 201 file.seekg(0, std::ios::beg); 202 file.read(Buffer.data(), static_cast<std::streamsize>(Buffer.size())); 203 file.close(); 204 205 return Buffer; 206 };