Engine
Frameworkcreatedbymeusableforthecreationofsimplegames.CurrentlysupportsOpenGL(Verysimple)andVulkan.
VulkanPipeline.hpp
Go to the documentation of this file.
1 #pragma once
3 #ifdef USING_VULKAN
4 
5 #include <ThirdParty/Vulkan/Include/vulkan/vulkan.h>
6 #include <ThirdParty/EASTL-master/include/EASTL/string.h>
7 #include <ThirdParty/EASTL-master/include/EASTL/vector.h>
8 
9 #define VULKAN_PIPELINE_COMPILE_MISSING_SHADER_VERTEX 1
10 #define VULKAN_PIPELINE_COMPILE_MISSING_SHADER_FRAGMENT 2
11 
12 namespace Engine {
13  class VulkanLogicalDevice;
14  class VulkanRenderer;
15 
16  class VulkanPipeline {
17  public:
18  typedef size_t descriptorSetHandle;
19 
20  VulkanPipeline(VulkanLogicalDevice* device, VulkanRenderer* renderer);
21  ~VulkanPipeline();
22 
23  enum SHADER_TYPE {
24  VERTEX_SHADER = 0,
25  TESLATION_EVALUATION_SHADER,
26  TESLATION_CONTROL_SHADER,
27  GEOMETRY_SHADER,
28  FRAGMENT_SHADER
29  };
30 
31  /* The system automatically looks in the Shaders\Compiled folder for shaders. Currently spirv need to be true. If it's false, the function fails.
32  * Returns false if the function fails
33  */
34  bool LoadShader(SHADER_TYPE type, eastl::string name, bool spirv = true);
35 
36  /* Returns 0 if succesfull, otherwise, an error code is returned.
37  */
38  int Compile();
39 
40  void SetInputAssemblyState(VkPrimitiveTopology topology, bool primitiveRestartEnabled);
41 
42  void AddVertexInputBindingDescription(uint32_t binding, uint32_t stride, VkVertexInputRate inputRate);
43  void AddVertexInputBindingDescription(VkVertexInputBindingDescription description);
44  void AddVertexInputAttributeDescription(uint32_t location, uint32_t binding, VkFormat format, uint32_t offset);
45  void AddVertexInputAttributeDescription(VkVertexInputAttributeDescription description);
46  void SetRasterizerSettings(VkBool32 depthClamp = VK_FALSE, VkBool32 discardEnable = VK_FALSE, VkPolygonMode polygonMode = VK_POLYGON_MODE_FILL, float lineWidth = 1.f,
47  VkCullModeFlagBits culling = VK_CULL_MODE_BACK_BIT, VkFrontFace frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE,
48  VkBool32 depthBiasEnabled = VK_FALSE, float depthBiasConstantFactor = 0.f, float depthBiasClamp = 0.f, float depthBiasSlopeFactor = 0.f);
49 
50  void CreateColorBlendAttachment(VkBool32 blendEnabled = VK_TRUE, VkColorComponentFlags colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT,
51  VkBlendFactor colorSrcFactor = VK_BLEND_FACTOR_SRC_ALPHA, VkBlendFactor colorDstFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, VkBlendOp colorBlendOp = VK_BLEND_OP_ADD,
52  VkBlendFactor alphaSrcFactor = VK_BLEND_FACTOR_ONE, VkBlendFactor alphaDstFactor = VK_BLEND_FACTOR_ZERO, VkBlendOp alphaBlendOp = VK_BLEND_OP_ADD);
53 
54  void SetDepthStencilState(VkBool32 depthTestEnable = VK_TRUE,
55  VkBool32 depthWriteEnable = VK_TRUE,
56  VkCompareOp depthCompareOp = VK_COMPARE_OP_LESS,
57  VkBool32 depthBoundsTestEnable = VK_FALSE,
58  float minDepthBounds = 0.f,
59  float maxDepthBounds = 1.f,
60  VkBool32 stencilTestEnable = VK_FALSE,
61  VkStencilOpState front = {},
62  VkStencilOpState back = {});
63 
64  void AddDescriptorSetBinding(descriptorSetHandle set, uint32_t binding, VkDescriptorType type, uint32_t descriptorCount, VkShaderStageFlags shaderStage, const VkSampler* immutableSamplers);
65 
66  void SetRenderPassInfo(VkRenderPass renderPass, uint32_t subPass);
67 
68  descriptorSetHandle CreateDescriptorSet();
69  descriptorSetHandle AddDescriptorSetLayout(VkDescriptorSetLayout layout);
70 
71  VkDescriptorSetLayout GetDescriptorSetLayout(descriptorSetHandle handle);
72 
73  VkPipeline GetPipeline() const;
74 
75  VkPipelineLayout GetPipelineLayout() const;
76 
77  uint32_t GetPipelineId() const;
78 
79  void AddPushConstantRange(VkShaderStageFlags stageFlags, uint32_t offset, uint32_t size);
80 
81  void Clean() const;
82  void Recreate();
83 
84  private:
85  typedef struct {
86  eastl::vector<VkDescriptorSetLayoutBinding> descriptionSetLayoutBindings;
87  VkDescriptorSetLayout descriptorSet;
88  bool external;
89  }DescriptorSet_t;
90 
91  eastl::vector<char> vertexShader;
92  eastl::vector<char> teslationEvaluationShader;
93  eastl::vector<char> teslationControlShader;
94  eastl::vector<char> geometryShader;
95  eastl::vector<char> fragmentShader;
96 
97  VkShaderModule vertexShaderModule;
98  VkShaderModule teslationEvaluationShaderModule;
99  VkShaderModule teslationControlShaderModule;
100  VkShaderModule geometryShaderModule;
101  VkShaderModule fragmentShaderModule;
102 
103  VulkanLogicalDevice* device;
104  VulkanRenderer* renderer;
105 
106  VkViewport viewport;
107  VkRect2D scissor;
108 
109  VkPipelineLayout pipelineLayout;
110 
111  VkPipeline pipeline;
112 
113  VkDescriptorSetLayout descriptorSetLayout;
114 
115  VkPipelineRasterizationStateCreateInfo rasterizer;
116 
117  VkPipelineInputAssemblyStateCreateInfo InputAssemblyStateCreateInfo;
118  VkPipelineVertexInputStateCreateInfo vertexInputInfo;
119 
120  VkPipelineDepthStencilStateCreateInfo depthStencilStateCreateInfo;
121 
122  VkPipelineMultisampleStateCreateInfo multisampling;
123  eastl::vector<VkPipelineColorBlendAttachmentState> colorBlendAttachments;
124  VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo;
125 
126  VkRenderPass renderPass;
127  uint32_t subPass;
128 
129  eastl::vector<VkVertexInputBindingDescription> vertexInputBindingDescriptions;
130  eastl::vector<VkVertexInputAttributeDescription> vertexInputAttributeDescriptions;
131 
132  eastl::vector<DescriptorSet_t> descriptorSets;
133 
134  eastl::vector<VkPushConstantRange> pushConstantRanges;
135 
136  int CompileShaderModule(eastl::vector<char> code, VkShaderModule* shaderModule);
137 
138  VkPipelineShaderStageCreateInfo ShaderStageCreateInfo[5] = { {},{},{},{},{} };
139 
140  VkDynamicState dynamicStates[5] = {
141  VK_DYNAMIC_STATE_VIEWPORT,
142  VK_DYNAMIC_STATE_SCISSOR };
143 
144  uint32_t pipelineId;
145 
146  static uint32_t pipelineIdCounter;
147 
148  };
149 
150 }
151 
152 #endif // USING_VULKAN