FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_graphicPipeline.hpp
1/*
2 * Copyright 2024 Guillaume Guillet
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_VULKAN_C_GRAPHICPIPELINE_HPP_INCLUDED
18#define _FGE_VULKAN_C_GRAPHICPIPELINE_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21#include "volk.h"
22#include "C_blendMode.hpp"
23#include "C_contextAware.hpp"
24#include "C_shader.hpp"
25#include "C_vertexBuffer.hpp"
26#include "C_viewport.hpp"
27#include <initializer_list>
28
29namespace fge::vulkan
30{
31
32class CommandBuffer;
33
34class FGE_API LayoutPipeline : public ContextAware
35{
36public:
37 explicit LayoutPipeline(Context const& context);
39 LayoutPipeline(LayoutPipeline&& r) noexcept;
40 ~LayoutPipeline() override;
41
42 LayoutPipeline& operator=(LayoutPipeline const& r) = delete;
43 LayoutPipeline& operator=(LayoutPipeline&& r) noexcept = delete;
44
45 bool updateIfNeeded(bool force = false);
46
47 void addDescriptorSetLayout(VkDescriptorSetLayout descriptorSetLayout);
48 void setDescriptorSetLayouts(std::initializer_list<VkDescriptorSetLayout> descriptorSetLayouts);
49 void setDescriptorSetLayouts(std::vector<VkDescriptorSetLayout> const& descriptorSetLayouts);
50 [[nodiscard]] std::vector<VkDescriptorSetLayout> const& getDescriptorSetLayouts() const;
51
52 void addPushConstantRanges(std::vector<VkPushConstantRange> const& pushConstantRanges);
53 void setPushConstantRanges(std::initializer_list<VkPushConstantRange> pushConstantRanges);
54 void setPushConstantRanges(std::vector<VkPushConstantRange> const& pushConstantRanges);
55 [[nodiscard]] std::vector<VkPushConstantRange> const& getPushConstantRanges() const;
56
57 [[nodiscard]] VkPipelineLayout get() const;
58
59 void destroy() final;
60
61 struct Key
62 {
63 VkShaderModule _vertexShader;
64 VkShaderModule _geometryShader;
65 VkShaderModule _fragmentShader;
66
67 struct Hash
68 {
69 [[nodiscard]] std::size_t operator()(Key const& r) const;
70 };
71
72 struct Compare
73 {
74 [[nodiscard]] bool operator()(Key const& lhs, Key const& rhs) const;
75 };
76 };
77
78private:
79 void clean();
80
81 mutable bool g_needUpdate;
82
83 mutable VkPipelineLayout g_pipeline;
84
85 std::vector<VkPushConstantRange> g_pushConstantRanges;
86 std::vector<VkDescriptorSetLayout> g_descriptorSetLayouts;
87};
88
89class FGE_API GraphicPipeline : public ContextAware
90{
91public:
92 explicit GraphicPipeline(Context const& context);
94 GraphicPipeline(GraphicPipeline&& r) noexcept;
95 ~GraphicPipeline() override;
96
97 GraphicPipeline& operator=(GraphicPipeline const& r) = delete;
98 GraphicPipeline& operator=(GraphicPipeline&& r) noexcept = delete;
99
100 bool updateIfNeeded(VkRenderPass renderPass, bool force = false);
101
102 void clearShader(Shader::Type type = Shader::Type::SHADER_NONE);
103 void setShader(Shader const& shader);
104 [[nodiscard]] Shader const* getShader(Shader::Type type = Shader::Type::SHADER_NONE) const;
105
106 void setBlendMode(BlendMode const& blendMode);
107 [[nodiscard]] BlendMode const& getBlendMode() const;
108
109 void setPrimitiveTopology(VkPrimitiveTopology topology) const;
110 [[nodiscard]] VkPrimitiveTopology getPrimitiveTopology() const;
111
112 void recordCommandBuffer(CommandBuffer& commandBuffer,
113 Viewport const& viewport,
114 VkRect2D const& scissor,
115 VertexBuffer const* vertexBuffer,
116 IndexBuffer const* indexBuffer) const;
117
118 void setPipelineLayout(LayoutPipeline const& layoutPipeline) const;
119 [[nodiscard]] VkPipelineLayout getPipelineLayout() const;
120 [[nodiscard]] VkPipeline getPipeline() const;
121
122 void destroy() final;
123
124 struct Key
125 {
126 VkShaderModule _shaderCompute;
127 VkShaderModule _shaderVertex;
128 VkShaderModule _shaderFragment;
129 VkShaderModule _shaderGeometry;
130
131 VkPrimitiveTopology _primitiveTopology;
132
133 BlendMode _blendMode;
134
135 VkPipelineLayout _pipelineLayout;
136
137 struct Hash
138 {
139 [[nodiscard]] std::size_t operator()(Key const& r) const;
140 };
141
142 struct Compare
143 {
144 [[nodiscard]] bool operator()(Key const& lhs, Key const& rhs) const;
145 };
146 };
147
148private:
149 void clean();
150
151 mutable bool g_needUpdate;
152
153 Shader const* g_shaderCompute;
154 Shader const* g_shaderVertex;
155 Shader const* g_shaderFragment;
156 Shader const* g_shaderGeometry;
157
158 mutable VkPrimitiveTopology g_primitiveTopology;
159
160 mutable BlendMode g_blendMode;
161
162 mutable VkPipelineLayout g_pipelineLayout;
163 mutable VkPipeline g_graphicsPipeline;
164};
165
166} // namespace fge::vulkan
167
168#endif //_FGE_VULKAN_C_GRAPHICPIPELINE_HPP_INCLUDED
Vulkan command buffer wrapper.
Definition C_commandBuffer.hpp:36
Definition C_contextAware.hpp:28
Vulkan context.
Definition C_context.hpp:70
Definition C_graphicPipeline.hpp:90
Definition C_vertexBuffer.hpp:104
Definition C_graphicPipeline.hpp:35
Definition C_shader.hpp:36
Definition C_vertexBuffer.hpp:45
Definition C_viewport.hpp:27
This is a simple abstraction class for VkBlendFactor, VkBlendOp.
Definition C_blendMode.hpp:37
Definition C_graphicPipeline.hpp:143
Definition C_graphicPipeline.hpp:138
Definition C_graphicPipeline.hpp:125
Definition C_graphicPipeline.hpp:73
Definition C_graphicPipeline.hpp:68
Definition C_graphicPipeline.hpp:62