Silicon
A realtime platform for creating interactive media.
Pipeline.cpp
Go to the documentation of this file.
1/*
2 * BSD 2-Clause License
3 *
4 * Copyright (c) 2022 Matthew McCall
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29//
30// Created by Matthew McCall on 1/3/22.
31//
32
33#include <array>
34#include <utility>
35
37
38#include "Pipeline.hpp"
39
40namespace Si {
41
42template <>
43vk::VertexInputBindingDescription Vertex::getBindingDescription()
44{
45 return {0, sizeof(Vertex), vk::VertexInputRate::eVertex};
46}
47
48template <>
50{
51 vk::VertexInputAttributeDescription vertexPosition {0, 0, vk::Format::eR32G32Sfloat, offsetof(Vertex, position)};
52 vk::VertexInputAttributeDescription vertexColor {1, 0, vk::Format::eR32G32B32Sfloat, offsetof(Vertex, color)};
53
54 return {vertexPosition, vertexColor};
55}
56
57namespace Vulkan {
58
60 : m_renderPass(renderPass)
61 , m_device(m_renderPass.getDevice())
62 , m_pipelineLayout(m_device)
63 , m_shaders(std::move(shaders))
64 {
65 addDependency(m_pipelineLayout);
66 addDependency(m_renderPass);
67
68 for (Shader &shader : m_shaders) {
69 addDependency(shader);
70 }
71 }
72
74 {
75 for (Shader &shader : m_shaders) {
76 removeDependency(shader);
77 }
78
79 m_shaders = std::move(shaders);
80
81 for (Shader &shader : m_shaders) {
82 addDependency(shader);
83 }
84 }
85
87 {
89 shaderStages.reserve(m_shaders.size());
90
91 vk::ShaderStageFlagBits stage;
92
93 for (Shader &shader : m_shaders) {
94
95 switch (shader.getType()) {
97 stage = vk::ShaderStageFlagBits::eVertex;
98 break;
100 stage = vk::ShaderStageFlagBits::eFragment;
101 break;
103 stage = vk::ShaderStageFlagBits::eTessellationControl;
104 break;
106 stage = vk::ShaderStageFlagBits::eTessellationEvaluation;
107 break;
109 stage = vk::ShaderStageFlagBits::eGeometry;
110 break;
112 stage = vk::ShaderStageFlagBits::eCompute;
113 break;
115 stage = vk::ShaderStageFlagBits::eRaygenKHR;
116 break;
118 stage = vk::ShaderStageFlagBits::eAnyHitKHR;
119 break;
121 stage = vk::ShaderStageFlagBits::eClosestHitKHR;
122 break;
124 stage = vk::ShaderStageFlagBits::eMissKHR;
125 break;
127 stage = vk::ShaderStageFlagBits::eIntersectionKHR;
128 break;
130 stage = vk::ShaderStageFlagBits::eCallableKHR;
131
132 break;
133 }
134
135 shaderStages.push_back({{}, stage, *shader, "main"});
136 }
137
138 std::array<vk::VertexInputBindingDescription, 1> vertexBindingDescriptions {Vertex::getBindingDescription<vk::VertexInputBindingDescription>()};
139 auto vertexAttributeDescriptions = Vertex::getAttributeDescriptions<vk::VertexInputAttributeDescription>();
140
141 vk::PipelineVertexInputStateCreateInfo vertexInputStateCreateInfo {
142 {},
143 vertexBindingDescriptions,
144 vertexAttributeDescriptions};
145
146 vk::PipelineInputAssemblyStateCreateInfo inputAssemblyStateCreateInfo {{}, vk::PrimitiveTopology::eTriangleList, VK_FALSE};
147
148 vk::PipelineViewportStateCreateInfo viewportStateCreateInfo {{}, 1, nullptr, 1, nullptr};
149
150 vk::PipelineRasterizationStateCreateInfo rasterizationStateCreateInfo {
151 {},
152 VK_FALSE,
153 VK_FALSE,
154 vk::PolygonMode::eFill,
155 vk::CullModeFlagBits::eBack,
156 vk::FrontFace::eClockwise,
157 VK_FALSE,
158 0,
159 0,
160 0,
161 1};
162
163 vk::PipelineMultisampleStateCreateInfo multisampleStateCreateInfo {{}, vk::SampleCountFlagBits::e1, VK_FALSE, 1};
164
165 vk::PipelineColorBlendAttachmentState colorBlendAttachmentState {
166 VK_TRUE,
167 vk::BlendFactor::eSrcAlpha,
168 vk::BlendFactor::eOneMinusSrcAlpha,
169 vk::BlendOp::eAdd,
170 vk::BlendFactor::eOne,
171 vk::BlendFactor::eZero,
172 vk::BlendOp::eAdd,
173 vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA};
174
175 std::array<vk::PipelineColorBlendAttachmentState, 1> colorBlendAttachmentStates {colorBlendAttachmentState};
176
177 vk::PipelineColorBlendStateCreateInfo colorBlendStateCreateInfo {
178 {},
179 VK_FALSE,
180 vk::LogicOp::eCopy,
181 colorBlendAttachmentStates,
182 {0, 0, 0, 0}};
183
184 std::array<vk::DynamicState, 2> dynamicStates = {vk::DynamicState::eViewport, vk::DynamicState::eScissor /*, vk::DynamicState::eLineWidth */};
185 vk::PipelineDynamicStateCreateInfo dynamicStateCreateInfo {{}, dynamicStates};
186
187 vk::GraphicsPipelineCreateInfo graphicsPipelineCreateInfo {
188 {},
189 shaderStages,
190 &vertexInputStateCreateInfo,
191 &inputAssemblyStateCreateInfo,
192 nullptr,
193 &viewportStateCreateInfo,
194 &rasterizationStateCreateInfo,
195 &multisampleStateCreateInfo,
196 nullptr,
197 &colorBlendStateCreateInfo,
198 &dynamicStateCreateInfo,
199 *m_pipelineLayout,
200 *m_renderPass,
201 0};
202
203 m_handle = m_device->createGraphicsPipeline(VK_NULL_HANDLE, graphicsPipelineCreateInfo).value;
204
205 return true;
206 }
207
209 {
210 m_device->destroy(m_handle);
211 }
212
213}
214}
void removeDependency(HandleBase &handle)
Definition: Handle.cpp:113
void destroy()
Destroys a handle, including its dependents.
Definition: Handle.cpp:71
void addDependency(HandleBase &handle)
Definition: Handle.cpp:107
Pipeline(RenderPass &renderPass, Vector< Shader > shaders={})
Definition: Pipeline.cpp:59
void setShaders(Vector< Shader > shaders)
Definition: Pipeline.cpp:73
void destroyImpl() override
Definition: Pipeline.cpp:208
bool createImpl() override
Definition: Pipeline.cpp:86
Handle wrapper for Vulkan Render Pass.
Definition: RenderPass.hpp:47
Definition: Allocator.hpp:36
T push_back(T... args)
T reserve(T... args)
Vec3 color
Definition: Vertex.hpp:45
static T getBindingDescription()
static std::array< T, 2 > getAttributeDescriptions()
Vec2 position
Definition: Vertex.hpp:44