Radium Engine  1.5.0
ShaderProgram.hpp
1 #pragma once
2 
3 #include <Engine/RaEngine.hpp>
4 
5 #include <Core/Types.hpp>
6 
7 #include <Core/CoreMacros.hpp>
8 #include <Engine/Data/ShaderConfiguration.hpp>
9 
10 #include <globjects/Program.h>
11 
12 #include <array>
13 #include <memory>
14 #include <string>
15 #include <type_traits>
16 
17 namespace globjects {
18 class Shader;
19 class NamedString;
20 class StaticStringSource;
21 } // namespace globjects
22 
23 namespace Ra {
24 namespace Engine {
25 namespace Data {
26 
27 class Texture;
28 
34 class RA_ENGINE_API ShaderProgram final
35 {
36  public:
37  ShaderProgram();
38  explicit ShaderProgram( const Data::ShaderConfiguration& shaderConfig );
39  ~ShaderProgram();
40 
41  void load( const Data::ShaderConfiguration& shaderConfig );
42  void reload();
43 
44  Data::ShaderConfiguration getBasicConfiguration() const;
45 
46  void bind() const;
47  void validate() const;
48  void unbind() const;
49 
51  template <typename T>
52  void setUniform( const char* name, const T& value ) const;
53  void setUniform( const char* name, Texture* tex, int texUnit ) const;
54 
59  void setUniformTexture( const char* name, Texture* tex ) const;
60 
61  globjects::Program* getProgramObject() const;
62 
64  void addShaderFromSource( Data::ShaderType type,
65  std::unique_ptr<globjects::StaticStringSource>&& source,
66  const std::string& name = "",
67  bool fromFile = true );
68 
69  void link();
70 
71  private:
72  struct TextureBinding {
73  int m_texUnit { -1 };
74  int m_location { -1 };
75  TextureBinding( int unit, int location ) : m_texUnit { unit }, m_location { location } {}
76  TextureBinding() = default;
77  };
78  using TextureUnits = std::map<std::string, TextureBinding>;
79  TextureUnits textureUnits;
80 
81  void loadShader( Data::ShaderType type,
82  const std::string& name,
83  const std::set<std::string>& props,
84  const std::vector<std::pair<std::string, Data::ShaderType>>& includes,
85  bool fromFile = true,
86  const std::string& version = "#version " +
88 
89  std::string preprocessIncludes( const std::string& name,
90  const std::string& shader,
91  int level,
92  int line = 0 );
93 
94  private:
95  Data::ShaderConfiguration m_configuration;
96 
97  std::array<std::pair<bool, std::unique_ptr<globjects::Shader>>, Data::ShaderType_COUNT>
98  m_shaderObjects;
99  std::array<std::unique_ptr<globjects::StaticStringSource>, Data::ShaderType_COUNT>
100  m_shaderSources;
101 
102  std::unique_ptr<globjects::Program> m_program;
103 };
104 
105 // declare specialization, definied in .cpp
106 template <>
107 RA_ENGINE_API void ShaderProgram::setUniform( const char* name, const Core::Vector2d& value ) const;
108 
109 template <>
110 RA_ENGINE_API void ShaderProgram::setUniform( const char* name, const Core::Vector3d& value ) const;
111 
112 template <>
113 RA_ENGINE_API void ShaderProgram::setUniform( const char* name, const Core::Vector4d& value ) const;
114 
115 template <>
116 RA_ENGINE_API void ShaderProgram::setUniform( const char* name, const Core::Matrix2d& value ) const;
117 
118 template <>
119 RA_ENGINE_API void ShaderProgram::setUniform( const char* name, const Core::Matrix3d& value ) const;
120 
121 template <>
122 RA_ENGINE_API void ShaderProgram::setUniform( const char* name, const Core::Matrix4d& value ) const;
123 
124 template <>
125 RA_ENGINE_API void ShaderProgram::setUniform( const char* name, const Scalar& value ) const;
126 
127 template <>
128 RA_ENGINE_API void ShaderProgram::setUniform( const char* name,
129  const std::vector<Scalar>& value ) const;
130 
131 // Uniform setters
132 template <typename T>
133 inline void ShaderProgram::setUniform( const char* name, const T& value ) const {
134  m_program->setUniform<T>( name, value );
135 }
136 
137 } // namespace Data
138 } // namespace Engine
139 } // namespace Ra
static std::string getGLSLVersion()
get the OpenGL version used in the generated header for shaders.
void setUniform(const char *name, const T &value) const
Uniform setters.
Definition: Cage.cpp:3