GLW
Computer Graphics Structures library
ShaderProgram.h
1 /*
2  Copyright (C) Skeletex Research, s.r.o. - All Rights Reserved
3  Unauthorized copying of this file, via any medium is strictly prohibited
4  Proprietary and confidential
5 */
6 #ifndef GLW_SHADER_PROGRAM_H
7 #define GLW_SHADER_PROGRAM_H
8 #include <memory>
9 #include <vector>
10 #include <map>
11 #include <glbinding/gl/gl.h>
12 #include <GLW/API.h>
13 
14 
15 
16 namespace glw
17 {
18 
20  enum class ShaderType
21  {
22  vertex = (unsigned int)gl::GLenum::GL_VERTEX_SHADER,
23  tess_ctrl = (unsigned int)gl::GLenum::GL_TESS_CONTROL_SHADER,
24  tess_eval = (unsigned int)gl::GLenum::GL_TESS_EVALUATION_SHADER,
25  geometry = (unsigned int)gl::GLenum::GL_GEOMETRY_SHADER,
26  fragment = (unsigned int)gl::GLenum::GL_FRAGMENT_SHADER,
27  compute = (unsigned int)gl::GLenum::GL_COMPUTE_SHADER,
28  none = (unsigned int)gl::GLenum::GL_NONE
29  };
30 
32  GLW_API glw::ShaderType GetShaderTypeFromFileName(const std::string &filename);
33 
34 
35 
36  using PShader = std::shared_ptr<class Shader>;
37 
39  class GLW_API Shader
40  {
41  public:
42 
43  using ReplaceMap = std::map<std::string, std::string>;
44 
46  static PShader Create(const ShaderType shader_type);
47 
49  static PShader CreateFromFile(const ShaderType shader_type, const std::string &file_name, const ReplaceMap &replace_map = ReplaceMap());
50 
52  static PShader CreateFromStr(const ShaderType shader_type, const std::string &shader_code, const ReplaceMap &replace_map = ReplaceMap());
53 
55  Shader(const ShaderType shader_type);
56 
58  Shader(const gl::GLenum shader_type);
59 
61  ~Shader();
62 
64  bool LoadFromStr(const std::string &shader_code, const ReplaceMap &replace_map = ReplaceMap());
65 
67  bool LoadFromFile(const std::string &file_name, const ReplaceMap &replace_map = ReplaceMap());
68 
70  bool Compile();
71 
73  bool IsCompiled() const;
74 
76  gl::GLuint GetID() const;
77 
79  ShaderType GetType() const;
80 
82  std::string GetCompilationLog() const;
83 
84  private:
85  ShaderType type_;
86  gl::GLuint shader_id_ = 0;
87  bool was_compiled_successfully_ = false;
88  };
89 
90 
91 
92  using PProgram = std::shared_ptr <class Program>;
93 
95  class GLW_API Program
96  {
97  public:
99  static PProgram Create();
100 
102  explicit Program();
103 
105  ~Program();
106 
108  bool AttachShader(const PShader &shader_ptr);
109 
111  bool Link();
112 
114  bool IsLinked() const;
115 
117  gl::GLuint GetID() const;
118 
120  std::string GetLinkLog() const;
121 
123  void Use() const;
124 
126  gl::GLint GetUniformLocation(const std::string &name) const;
127 
129  gl::GLint GetUniformBlockLocation(const std::string &name) const;
130 
131  private:
132  gl::GLuint program_id_ = 0;
133  bool is_linked_successfuly = false;
134  std::map<ShaderType, PShader> attached_shaders_;
135  };
136 
137 }
138 
139 #endif /* !GLW_SHADER_PROGRAM_H */
OpenGL wrapper library namespace. Provides simplified interface for using OpenGL features.
Definition: API.h:17
Maintains the source code strings that defines a shader.
Definition: ShaderProgram.h:39
Holds several compiled shaders which are used as a whole.
Definition: ShaderProgram.h:95
GLW_API glw::ShaderType GetShaderTypeFromFileName(const std::string &filename)
Creates ShaderType from extension of specified shader file name.
ShaderType
Indicates the type of shader.
Definition: ShaderProgram.h:20