| Fling Engine 0.00.1 Fling Engine is a game engine written in Vulkan

Shader.h
Go to the documentation of this file.
1 #pragma once
2
3 #include "FlingVulkan.h"
4
5 #include "spirv_cross.hpp"
6 #include "spirv_glsl.hpp"
7 #include "spirv.h"
8
9 #include "Resource.h"
10 #include "FlingExports.h"
11 #include <fstream>
12 #include <vector>
13
14 namespace Fling
15 {
16 #if FLING_LINUX
17 typedef VkDescriptorUpdateTemplateKHR VkDescriptorUpdateTemplate;
18 typedef VkDescriptorUpdateTemplateEntryKHR VkDescriptorUpdateTemplateEntry;
19 #endif
20
21 // Grabbed this and some shader reflection things from https://github.com/zeux/niagara
22 struct DescriptorInfo
23 {
24 union
25 {
26 VkDescriptorImageInfo image;
27 VkDescriptorBufferInfo buffer;
28 };
29
30 DescriptorInfo()
31 {
32 }
33
34 DescriptorInfo(VkImageView imageView, VkImageLayout imageLayout)
35 {
36 image.sampler = VK_NULL_HANDLE;
37 image.imageView = imageView;
38 image.imageLayout = imageLayout;
39 }
40
41 DescriptorInfo(VkSampler sampler, VkImageView imageView, VkImageLayout imageLayout)
42 {
43 image.sampler = sampler;
44 image.imageView = imageView;
45 image.imageLayout = imageLayout;
46 }
47
48 DescriptorInfo(VkBuffer buffer_, VkDeviceSize offset, VkDeviceSize range)
49 {
50 buffer.buffer = buffer_;
51 buffer.offset = offset;
52 buffer.range = range;
53 }
54
55 DescriptorInfo(VkBuffer buffer_)
56 {
57 buffer.buffer = buffer_;
58 buffer.offset = 0;
59 buffer.range = VK_WHOLE_SIZE;
60 }
61 };
62
63 class LogicalDevice;
64
70 class Shader : public Resource
71 {
72 friend class ResourceManager;
73 public:
74
75 static std::shared_ptr<Fling::Shader> Create(Guid t_ID, LogicalDevice* t_Dev);
76
82 explicit Shader(Guid t_ID, LogicalDevice* t_Dev);
83
84 ~Shader();
85
91 VkShaderModule GetShaderModule() const { return m_Module; }
92
94 VkShaderStageFlagBits GetStage() const { return m_Stage; }
95
99 void Release();
100
101 static VkDescriptorSetLayout CreateSetLayout(VkDevice t_Dev, std::vector<Shader*>& t_Shaders, bool t_SupportPushDescriptor = false);
102
103 static VkPipelineLayout CreatePipelineLayout(VkDevice t_Dev, VkDescriptorSetLayout t_SetLayout, VkShaderStageFlags t_PushConstantStages, size_t t_PushConstantSize);
104
105 private:
106
107 static UINT32 GatherResources(const std::vector<Shader*>& t_Shaders, VkDescriptorType(&t_ResourceTypes)[32]);
108
112 void ParseReflectionData(const UINT32* t_Code, UINT32 t_Size);
113
115 VkResult CreateShaderModule(std::vector<char>& t_ShaderCode);
116
120 static std::vector<char> LoadRawBytes(const std::string& t_FilePath);
121
123 VkShaderModule m_Module = VK_NULL_HANDLE;
124
125 // Shader reflection data ----------
126 UINT32 m_ResourceMask {};
127
129 VkDescriptorType m_ResourceTypes[32] {};
130
132 VkShaderStageFlagBits m_Stage = VkShaderStageFlagBits::VK_SHADER_STAGE_VERTEX_BIT;
133
134 const LogicalDevice* m_Device;
135
136 bool m_UsesPushConstants = false;
137
138 // Sizes that can be used by a compute pipeline
139 UINT32 localSizeX {};
140 UINT32 localSizeY {};
141 UINT32 localSizeZ {};
142 };
143
144 } // namespace Fling
Fling::DescriptorInfo::DescriptorInfo
DescriptorInfo()
Definition: Shader.h:30
FlingVulkan.h
Fling::DescriptorInfo::DescriptorInfo
DescriptorInfo(VkBuffer buffer_)
Definition: Shader.h:55
Fling::DescriptorInfo::DescriptorInfo
DescriptorInfo(VkBuffer buffer_, VkDeviceSize offset, VkDeviceSize range)
Definition: Shader.h:48
Fling::DescriptorInfo
Definition: Shader.h:22
Fling::GraphicsHelpers::CreateShaderModule
VkShaderModule CreateShaderModule(std::shared_ptr< File > t_ShaderCode)
Definition: GraphicsHelpers.cpp:592
Fling::DescriptorInfo::buffer
VkDescriptorBufferInfo buffer
Definition: Shader.h:27
Fling::DescriptorInfo::DescriptorInfo
DescriptorInfo(VkImageView imageView, VkImageLayout imageLayout)
Definition: Shader.h:34
Fling::Resource
Base class that represents a loaded resource in the engine.
Definition: Resource.h:11
Fling::LogicalDevice
A logical device represents the application view of the device.
Definition: LogicalDevice.h:13
Fling::Shader::GetShaderModule
VkShaderModule GetShaderModule() const
Create a Shader Module object.
Definition: Shader.h:91
Resource.h
Fling::DescriptorInfo::image
VkDescriptorImageInfo image
Definition: Shader.h:26
Fling::DescriptorInfo::DescriptorInfo
DescriptorInfo(VkSampler sampler, VkImageView imageView, VkImageLayout imageLayout)
Definition: Shader.h:41
Fling::Shader
Class that represents what a shader is in the Fling engine.
Definition: Shader.h:70
Fling::Shader::m_Device
const LogicalDevice * m_Device
Definition: Shader.h:134
Fling::Guid
entt::hashed_string Guid
Definition: FlingTypes.h:26
FlingExports.h
Fling::Shader::GetStage
VkShaderStageFlagBits GetStage() const
get the Vulkan stage bit flags that we should bind to
Definition: Shader.h:94
Fling::ResourceManager
The resource manager handles loading of files off disk.
Definition: ResourceManager.h:23
UINT32
uint32_t UINT32
Definition: FlingTypes.h:13
Fling
Definition: Engine.h:29