Aether3D Game Engine
Shader.hpp
1 #ifndef SHADER_H
2 #define SHADER_H
3 
4 #include <map>
5 #include <string>
6 #if AETHER3D_METAL
7 #import <Metal/Metal.h>
8 #endif
9 #if AETHER3D_D3D12
10 #include <d3d12.h>
11 #include <d3dcompiler.h>
12 #endif
13 
14 namespace ae3d
15 {
16  namespace FileSystem
17  {
18  struct FileContentsData;
19  }
20 
22  class Shader
23  {
24  public:
26  bool IsValid() const { return id != 0; }
27 
30  void Load( const char* vertexSource, const char* fragmentSource );
31 
38  void Load( const FileSystem::FileContentsData& vertexDataGLSL, const FileSystem::FileContentsData& fragmentDataGLSL,
39  const char* metalVertexShaderName, const char* metalFragmentShaderName,
40  const FileSystem::FileContentsData& vertexDataHLSL, const FileSystem::FileContentsData& fragmentDataHLSL );
41 
42 #if AETHER3D_METAL
43  void LoadFromLibrary( const char* vertexShaderName, const char* fragmentShaderName );
44 #endif
45 
47  void Use();
48 
51  void SetMatrix( const char* name, const float* matrix4x4 );
52 
56  void SetTexture( const char* name, const class Texture2D* texture, int textureUnit );
57 
61  void SetTexture( const char* name, const class TextureCube* texture, int textureUnit );
62 
66  void SetRenderTexture( const char* name, const class RenderTexture* renderTexture, int textureUnit );
67 
70  void SetInt( const char* name, int value );
71 
74  void SetFloat( const char* name, float value );
75 
78  void SetVector3( const char* name, const float* vec3 );
79 
82  void SetVector4( const char* name, const float* vec4 );
83 
84 #if AETHER3D_D3D12
85  ID3DBlob* blobShaderVertex = nullptr;
86  ID3DBlob* blobShaderPixel = nullptr;
87 #endif
88 
89 #if AETHER3D_METAL
90  enum class UniformType { Float, Float2, Float3, Float4, Matrix4x4 };
91 
92  struct Uniform
93  {
94  UniformType type = UniformType::Float;
95  unsigned long offsetFromBufferStart = 0;
96  float floatValue[ 4 ];
97  float matrix4x4[ 16 ];
98  };
99 
100  std::map< std::string, Uniform > uniforms;
101 
102  void LoadUniforms( MTLRenderPipelineReflection* reflection );
103  id <MTLBuffer> GetUniformBuffer() { return uniformBuffer; }
104 
105  id <MTLFunction> vertexProgram;
106  id <MTLFunction> fragmentProgram;
107  id <MTLBuffer> uniformBuffer;
108 #endif
109  struct IntDefaultedToMinusOne
111  {
113  int i = -1;
114  };
115 
116  private:
117 #if AETHER3D_D3D12
118  void CreateConstantBuffer();
119  void ReflectVariables();
120 
121  ID3D12Resource* constantBuffer = nullptr;
122  void* constantBufferUpload = nullptr;
123  ID3D12ShaderReflection* reflector = nullptr;
124 #endif
125  unsigned id = 0;
126  std::map<std::string, IntDefaultedToMinusOne > uniformLocations;
127  };
128 }
129 #endif
void SetRenderTexture(const char *name, const class RenderTexture *renderTexture, int textureUnit)
Definition: AudioClip.hpp:4
Render texture.
Definition: RenderTexture.hpp:10
void SetVector4(const char *name, const float *vec4)
void SetVector3(const char *name, const float *vec3)
void SetInt(const char *name, int value)
void SetFloat(const char *name, float value)
Cube Map texture.
Definition: TextureCube.hpp:15
Shader program containing a vertex and pixel shader.
Definition: Shader.hpp:22
void Use()
Activates the shader to be used in a draw call.
2D texture.
Definition: Texture2D.hpp:15
void Load(const char *vertexSource, const char *fragmentSource)
int i
-1 means unused/missing uniform.
Definition: Shader.hpp:113
bool IsValid() const
Definition: Shader.hpp:26
void SetTexture(const char *name, const class Texture2D *texture, int textureUnit)
Definition: FileSystem.hpp:12
void SetMatrix(const char *name, const float *matrix4x4)