Aether3D Game Engine
Shader.hpp
1 #ifndef SHADER_H
2 #define SHADER_H
3 
4 #include <map>
5 #include <string>
6 #if RENDERER_METAL
7 #import <Metal/Metal.h>
8 #endif
9 #if RENDERER_D3D12
10 #include <d3d12.h>
11 #include <d3dcompiler.h>
12 #endif
13 #if RENDERER_VULKAN
14 #include <vulkan/vulkan.h>
15 #include <cstdint>
16 #endif
17 
18 namespace ae3d
19 {
20  namespace FileSystem
21  {
22  struct FileContentsData;
23  }
24 
26  class Shader
27  {
28  public:
33  void Load( const char* vertexSource, const char* fragmentSource );
34 #if RENDERER_VULKAN
35  bool IsValid() const { return true; }
36 
39  void LoadSPIRV( const FileSystem::FileContentsData& vertexData, const FileSystem::FileContentsData& fragmentData );
40 #endif
41  void Load( const FileSystem::FileContentsData& vertexDataGLSL, const FileSystem::FileContentsData& fragmentDataGLSL,
50  const char* metalVertexShaderName, const char* metalFragmentShaderName,
51  const FileSystem::FileContentsData& vertexDataHLSL, const FileSystem::FileContentsData& fragmentDataHLSL,
52  const FileSystem::FileContentsData& vertexDataSPIRV, const FileSystem::FileContentsData& fragmentDataSPIRV );
53 
54 #if RENDERER_METAL
55  void LoadFromLibrary( const char* vertexShaderName, const char* fragmentShaderName );
56 #endif
57 #if RENDERER_OPENGL
58  bool IsValid() const { return handle != 0; }
60  unsigned GetHandle() const { return handle; }
61 #endif
62 
64  void Use();
65 
68  void SetMatrix( const char* name, const float* matrix4x4 );
69 
73  void SetTexture( const char* name, class Texture2D* texture, int textureUnit );
74 
78  void SetTexture( const char* name, class TextureCube* texture, int textureUnit );
79 
83  void SetRenderTexture( const char* name, class RenderTexture* renderTexture, int textureUnit );
84 
87  void SetInt( const char* name, int value );
88 
91  void SetFloat( const char* name, float value );
92 
95  void SetVector3( const char* name, const float* vec3 );
96 
99  void SetVector4( const char* name, const float* vec4 );
100 
101 #if RENDERER_NULL
102  bool IsValid() const { return true; }
103 #endif
104 #if RENDERER_D3D12
105  bool IsValid() const { return blobShaderVertex != nullptr; }
106  ID3DBlob* blobShaderVertex = nullptr;
107  ID3DBlob* blobShaderPixel = nullptr;
108 #endif
109 
110 #if RENDERER_METAL
111  bool IsValid() const { return vertexProgram != nullptr; }
112 
113  enum class UniformType { Float, Float2, Float3, Float4, Matrix4x4 };
114 
115  struct Uniform
116  {
117  UniformType type = UniformType::Float;
118  unsigned long offsetFromBufferStart = 0;
119  float floatValue[ 4 ];
120  float matrix4x4[ 16 ];
121  };
122 
123  std::map< std::string, Uniform > uniforms;
124 
125  void LoadUniforms( MTLRenderPipelineReflection* reflection );
126 
127  id <MTLFunction> vertexProgram;
128  id <MTLFunction> fragmentProgram;
129 #endif
130 #if RENDERER_VULKAN
131  VkPipelineShaderStageCreateInfo& GetVertexInfo() { return vertexInfo; }
132  VkPipelineShaderStageCreateInfo& GetFragmentInfo() { return fragmentInfo; }
133 #endif
136  {
138  int i = -1;
139  };
140 
141  private:
142  std::string fragmentPath;
143 
144 #if RENDERER_D3D12
145  void ReflectVariables();
146 
147  ID3D12ShaderReflection* reflector = nullptr;
148  std::map<std::string, IntDefaultedToMinusOne > uniformLocations;
149 #endif
150 #if RENDERER_VULKAN
151  VkPipelineShaderStageCreateInfo vertexInfo;
152  VkPipelineShaderStageCreateInfo fragmentInfo;
153 #endif
154 #if RENDERER_OPENGL
155  unsigned handle = 0;
156  std::map<std::string, IntDefaultedToMinusOne > uniformLocations;
157 #endif
158  };
159 }
160 #endif
Definition: AudioClip.hpp:4
Render texture.
Definition: RenderTexture.hpp:12
Wraps an int that is defaulted to -1. Needed for uniform handling.
Definition: Shader.hpp:135
Cube Map texture.
Definition: TextureCube.hpp:18
Shader program containing a vertex and pixel shader.
Definition: Shader.hpp:26
2D texture.
Definition: Texture2D.hpp:17
Definition: FileSystem.hpp:12