HoviTron Video Pipeline
VulkanPipelineAbstract.cpp
1/* ----------------------
2* Copyright 2023 Université Libre de Bruxelles(ULB), Universidad Politécnica de Madrid(UPM), CREAL, Deutsches Zentrum für Luft - und Raumfahrt(DLR)
3
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at < http://www.apache.org/licenses/LICENSE-2.0>
7
8* Unless required by applicable law or agreed to in writing, software
9* distributed under the License is distributed on an "AS IS" BASIS,
10* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11* See the License for the specific language governing permissionsand
12* limitations under the License.
13---------------------- */
14
16#include "VulkanContext.h"
17#include "VulkanRenderPass.h"
18#include "Shader.h"
19#include <iostream>
20#include "VulkanWrapper.h"
21
23{
24 vk::ShaderModuleCreateInfo shaderModuleCreateInfo(vk::ShaderModuleCreateFlags(), shader.size, reinterpret_cast<const uint32_t*>(shader.source));
25 vk::ShaderModule shaderModule = context->device.createShaderModule(shaderModuleCreateInfo);
26#ifndef NDEBUG
27 vk::DebugUtilsObjectNameInfoEXT debug(vk::ObjectType::eShaderModule, (uint64_t) static_cast<VkShaderModule>(shaderModule), shader.name.c_str());
28 context->device.setDebugUtilsObjectNameEXT(debug);
29#endif // !NDEBUG
30
31 return shaderModule;
32}
33
34void VulkanPipelineAbsract::createPipeline(std::vector<vk::PipelineShaderStageCreateInfo>& pipelineShaderStageCreateInfos)
35{
36 auto descr = bufferController->getAttributeDescription();
37 D(std::cout << "pipeline attribute descrp: " << descr.size() << std::endl;)
38
39 auto descrBinding = bufferController->getBindingDescription();
40
41 vk::PipelineVertexInputStateCreateInfo pipelineVertexInputStateCreateInfo(
42 vk::PipelineVertexInputStateCreateFlags(), // flags
43 1, // vertexBindingDescriptions Count
44 &(descrBinding), // vertexBindingDescriptions
45 static_cast<uint32_t>((descr).size()), // vertexAttributeDescriptionsCount
46 (descr.data()) // vertexAttributeDescriptions
47 );
48
49 vk::PipelineInputAssemblyStateCreateInfo pipelineInputAssemblyStateCreateInfo(
50 vk::PipelineInputAssemblyStateCreateFlags(), // Flags
51 vk::PrimitiveTopology::eTriangleList, // Topology
52 0 // Primitive restart enable
53 );
54
55
56 //vk::Rect2D scissor{ {0,0},renderPass->renderingExtent };
57
58 //viewport state
59 vk::PipelineViewportStateCreateInfo pipelineViewportStateCreateInfo(
60 vk::PipelineViewportStateCreateFlags(),
62
63 //rasterizer
64 vk::PipelineRasterizationStateCreateInfo pipelineRasterizationStateCreateInfo(
65 vk::PipelineRasterizationStateCreateFlags(), // flags
66 false, // depthClampEnable
67 false, // rasterizerDiscardEnable
68 vk::PolygonMode::eFill, // polygonMode
69 vk::CullModeFlagBits::eNone, // cullMode
70 vk::FrontFace::eClockwise, // frontFace
71 false, // depthBiasEnable
72 0.0f, // depthBiasConstantFactor
73 0.0f, // depthBiasClamp
74 0.0f, // depthBiasSlopeFactor
75 1.0f // lineWidth
76 );
77
78 vk::PipelineMultisampleStateCreateInfo pipelineMultisampleStateCreateInfo(
79 vk::PipelineMultisampleStateCreateFlags(), // flags
80 vk::SampleCountFlagBits::e1 // rasterizationSamples
81 // other values can be default
82 );
83
84 //depthStencil ?
85
86 vk::PipelineDepthStencilStateCreateInfo pipelineDepthStencilStateCreateInfo(
87 vk::PipelineDepthStencilStateCreateFlags(), // flags
88 true, // depthTestEnable
89 true, // depthWriteEnable
90 (! this->depthTest && depthWrite) ? vk::CompareOp::eAlways : vk::CompareOp::eLess, // depthCompareOp
91 false, // depthBoundTestEnable
92 false, // stencilTestEnable
93 {}, // front
94 {}, // back
95 0.0f, //minDepthBounds
96 1.0f //maxDepthBounds
97 );
98
99 //color blending
100 vk::ColorComponentFlags colorComponentFlags(vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG |
101 vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA);
102 vk::PipelineColorBlendAttachmentState pipelineColorBlendAttachmentState(
103 false, // blendEnable
104 vk::BlendFactor::eOne, // srcColorBlendFactor //change this for alpha blending
105 vk::BlendFactor::eZero, // dstColorBlendFactor //change this for alpha blending
106 vk::BlendOp::eAdd, // colorBlendOp
107 vk::BlendFactor::eOne, // srcAlphaBlendFactor
108 vk::BlendFactor::eZero, // dstAlphaBlendFactor
109 vk::BlendOp::eAdd, // alphaBlendOp
110 colorComponentFlags // colorWriteMask
111 );
112
113 //std::array<vk::PipelineColorBlendAttachmentState, 3> pipelineColorBlendAttachementArray = { pipelineColorBlendAttachmentState,pipelineColorBlendAttachmentState,pipelineColorBlendAttachmentState };
114 std::vector<vk::PipelineColorBlendAttachmentState> pipelineColorBlendAttachementArray(colorAttachmentNb, pipelineColorBlendAttachmentState);
115
116 vk::PipelineColorBlendStateCreateInfo pipelineColorBlendStateCreateInfo(
117 vk::PipelineColorBlendStateCreateFlags(), // flags
118 false, // logicOpEnable
119 vk::LogicOp::eCopy, // logicOp //TODO check this
120 static_cast<uint32_t>(pipelineColorBlendAttachementArray.size()),
121 pipelineColorBlendAttachementArray.data(), // attachments
122 { { 0.0f, 0.0f, 0.0f, 0.0f } } // blendConstants
123 );
124
125 //dynamic state
126 //std::array<vk::DynamicState, 2> dynamicStates = { vk::DynamicState::eViewport, vk::DynamicState::eScissor };
127 //vk::PipelineDynamicStateCreateInfo pipelineDynamicStateCreateInfo(vk::PipelineDynamicStateCreateFlags(), dynamicStates.size(), dynamicStates.data());
128
129 //pipeline Layout
130 vk::PipelineLayoutCreateInfo pipelineLayoutInfo(
131 vk::PipelineLayoutCreateFlags(), //flags
132 1, //layoutCount
133 &descriptorSetLayout, //pSetLayout
134 0, //pushConstantRangeCount
135 nullptr); //pushConstantRange
136 pipelineLayout = context->device.createPipelineLayout(pipelineLayoutInfo);
137
138 //pipelineInfo
139 vk::GraphicsPipelineCreateInfo graphicsPipelineCreateInfo(
140 vk::PipelineCreateFlags(), // flags
141 pipelineShaderStageCreateInfos.size(), //stageCount
142 pipelineShaderStageCreateInfos.data(), // stages
143 &pipelineVertexInputStateCreateInfo, // pVertexInputState
144 &pipelineInputAssemblyStateCreateInfo, // pInputAssemblyState
145 nullptr, // pTessellationState
146 &pipelineViewportStateCreateInfo, // pViewportState
147 &pipelineRasterizationStateCreateInfo, // pRasterizationState
148 &pipelineMultisampleStateCreateInfo, // pMultisampleState
149 depthTest || depthWrite ? &pipelineDepthStencilStateCreateInfo : nullptr, // pDepthStencilState
150 &pipelineColorBlendStateCreateInfo, // pColorBlendState
151 nullptr, // pDynamicState
152 pipelineLayout, // layout
153 renderPass->renderPass, // renderPass
154 subPass //subPass
155 );
156
157 std::array<vk::DynamicState, 2> dynamicStates = { vk::DynamicState::eViewport, vk::DynamicState::eScissor };
158 vk::PipelineDynamicStateCreateInfo dynamicInfo(
159 vk::PipelineDynamicStateCreateFlags(),
160 static_cast<uint32_t> (dynamicStates.size()),
161 dynamicStates.data()
162 );
163
164 if (wrapper->isUsingDifferentViewSize() && wrapper->multiviewSetup) {
165 //vk::DynamicState::eViewport
166 graphicsPipelineCreateInfo.pDynamicState = &dynamicInfo;
167
168 }
169
170#ifdef __ANDROID__
171 pipeline = context->device.createGraphicsPipeline(nullptr, graphicsPipelineCreateInfo);
172#else
173 vk::Result result;
174 std::tie(result, pipeline) = context->device.createGraphicsPipeline(nullptr, graphicsPipelineCreateInfo);
175
176 switch (result)
177 {
178 case vk::Result::eSuccess: break;
179 case vk::Result::ePipelineCompileRequiredEXT:
180 // something meaningfull here
181 throw std::runtime_error("failed to create graphics pipeline! result was ePipelineCompileRequiredEXT");
182 break;
183 default: throw std::runtime_error("failed to create graphics pipeline!");; // should never happen
184 }
185#endif
186}
187
189{
190 return bufferController->getIndiceCount();
191}
192
193
194void VulkanPipelineAbsract::cmdBufferBindBuffer(vk::CommandBuffer& cmdBuffer, int index, InputProvider::StreamFrameInfo& frameInfo, int view)
195{
196 bufferController->bindBuffers(cmdBuffer, index, frameInfo, view);
197}
198
199
200void VulkanPipelineAbsract::updateAllBuffer(uint32_t currentImage, int view)
201{
202 bufferController->updateAllUniformBuffer(currentImage, view);
203}
204
205void VulkanPipelineAbsract::updateBuffer(uint32_t currentImage, int view)
206{
207 bufferController->updateUniformBuffer(currentImage, view);
208}
209
210
Contains the class that loads the SPIR-V shaders from files.
File that contain the VulkanContext class to manage Vulkan Instance, Physical device,...
file that contains the abstract class for common code between the two pipelines (blending and warping...
file that contains the class that manage the renderPass containing the synthesis and blending steps
file that contains the VulkanWrapper class that manages the classes related to Vulkan code and ease t...
Class that represents SPIR-V shader.
Definition: Shader.h:42
char * source
Definition: Shader.h:45
size_t size
Definition: Shader.h:47
std::string name
Definition: Shader.h:50
vk::Device device
Definition: VulkanContext.h:87
std::unique_ptr< BuffersController > bufferController
void updateBuffer(uint32_t currentImage, int view)
void createPipeline(std::vector< vk::PipelineShaderStageCreateInfo > &arr)
VulkanRenderPass * renderPass
vk::ShaderModule createShaderModule(const Shader &shader)
void updateAllBuffer(uint32_t currentImage, int view)
void cmdBufferBindBuffer(vk::CommandBuffer &cmdBuffer, int index, InputProvider::StreamFrameInfo &frameInfo, int view)