Kale
Loading...
Searching...
No Matches
GraphicsPipeline.cpp
Go to the documentation of this file.
1/*
2 Copyright 2022 Rishi Challa
3
4 Licensed under the Apache License, Version 2.0 (the "License") {
5
6}
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17*/
18
19#ifdef KALE_VULKAN
20
21#include "GraphicsPipeline.hpp"
22
25
26#include <array>
27
28using namespace Kale;
29using namespace Kale::Vulkan;
30
35 // Empty Body
36}
37
44GraphicsPipeline::GraphicsPipeline(const std::string& vert, const std::string& frag, const Renderer& renderer) {
45 init(vert, frag, renderer);
46}
47
52 vk::PipelineLayoutCreateInfo layoutCreateInfo;
53 layout = Core::device.logicalDevice->createPipelineLayoutUnique(layoutCreateInfo);
54}
55
62void GraphicsPipeline::init(const std::string& vert, const std::string& frag, const Renderer& renderer) {
64
65 // Shaders
66 Shader vertShader(vert, ShaderType::Vertex, Core::device);
67 Shader fragShader(frag, ShaderType::Fragment, Core::device);
68 std::array<vk::PipelineShaderStageCreateInfo, 2> shaderStage = {
69 vertShader.getShaderPipelineInfo(),
70 fragShader.getShaderPipelineInfo()
71 };
72
73 // Vertex Input - Choose defaults
74 vk::PipelineVertexInputStateCreateInfo vertexCreateInfo;
75
76 // Input Assembly
77 vk::PipelineInputAssemblyStateCreateInfo inputAssemblyCreateInfo(vk::PipelineInputAssemblyStateCreateFlags(),
78 vk::PrimitiveTopology::eTriangleList, VK_FALSE);
79
80 // Viewports & Scissors
81 vk::Viewport viewport(0.0f, 0.0f, static_cast<float>(Core::swapchain.extent.width),
82 static_cast<float>(Core::swapchain.extent.height), 0.0f, 1.0f);
83 vk::Rect2D scissors(vk::Offset2D(), Core::swapchain.extent);
84 vk::PipelineViewportStateCreateInfo viewportCreateInfo(vk::PipelineViewportStateCreateFlags(),
85 1, &viewport, 1, &scissors);
86
87 // Rasterizer
88 vk::PipelineRasterizationStateCreateInfo rasterizerCreateInfo;
89 rasterizerCreateInfo.polygonMode = vk::PolygonMode::eFill;
90 rasterizerCreateInfo.cullMode = vk::CullModeFlagBits::eBack;
91 rasterizerCreateInfo.lineWidth = 1.0f;
92 rasterizerCreateInfo.frontFace = vk::FrontFace::eClockwise;
93
94 // Multisampling
95 vk::PipelineMultisampleStateCreateInfo multisamplingCreateInfo;
96 multisamplingCreateInfo.minSampleShading = 1.0f;
97
98 // Color Blending
99 vk::PipelineColorBlendAttachmentState attachmentState(VK_TRUE, vk::BlendFactor::eSrcAlpha,
100 vk::BlendFactor::eOneMinusSrcAlpha, vk::BlendOp::eAdd, vk::BlendFactor::eOne,
101 vk::BlendFactor::eZero, vk::BlendOp::eAdd, vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG |
102 vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA);
103
104 vk::PipelineColorBlendStateCreateInfo colorBlendingCreateInfo(vk::PipelineColorBlendStateCreateFlags(),
105 VK_FALSE, vk::LogicOp::eCopy, 1, &attachmentState);
106
107 // Dynamic State - Viewport will not change on android/ios
108 #if defined(KALE_IOS) || defined(KALE_ANDROID)
109 std::vector<vk::DynamicState> dynamicStates;
110 #else
111 std::vector<vk::DynamicState> dynamicStates = {vk::DynamicState::eViewport};
112 #endif
113 vk::PipelineDynamicStateCreateInfo dynamicCreateInfo(vk::PipelineDynamicStateCreateFlags(),
114 static_cast<uint32_t>(dynamicStates.size()), dynamicStates.data());
115
116 // Pipeline Layout
118
119 // Pipeline Create Info
120 vk::GraphicsPipelineCreateInfo createInfo(vk::PipelineCreateFlags(), 2, shaderStage.data(), &vertexCreateInfo,
121 &inputAssemblyCreateInfo, nullptr, &viewportCreateInfo, &rasterizerCreateInfo, &multisamplingCreateInfo,
122 nullptr, &colorBlendingCreateInfo, &dynamicCreateInfo, layout.get(), renderer.renderPass.get());
123 pipeline = Core::device.logicalDevice->createGraphicsPipelineUnique({}, createInfo).value;
124}
125
131 layout.reset();
132 pipeline.reset();
133}
134
135#endif
virtual void freeResources(bool remove=true)
virtual void init(Parent &parent)
static Device device
Definition Core.hpp:99
static SwapChain swapchain
Definition Core.hpp:104
vk::UniqueDevice logicalDevice
Definition Device.hpp:59
void freeResources(bool remove=true) override
vk::UniquePipelineLayout layout
void init(const std::string &vert, const std::string &frag, const Renderer &renderer)
vk::UniqueRenderPass renderPass
Definition Renderer.hpp:59
vk::PipelineShaderStageCreateInfo getShaderPipelineInfo() const
Definition Shader.cpp:117