MRPT  2.0.0
Shader.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/core/exceptions.h>
12 #include <mrpt/core/optional_ref.h>
13 #include <memory>
14 #include <unordered_map>
15 #include <vector>
16 
17 namespace mrpt::opengl
18 {
19 /** Type for IDs of shaders.
20  * \sa DefaultShaderID, LoadDefaultShader()
21  * \ingroup mrpt_opengl_grp
22  */
23 using shader_id_t = uint8_t;
24 
25 /** A list of shader IDs */
26 using shader_list_t = std::vector<shader_id_t>;
27 
28 /** A resource handling helper for OpenGL "Shader" compiled code fragment.
29  *
30  * The OpenGL shader resource will be freed upon destruction or when clear() is
31  * called. Normally, users want shader(s) to be linked into a
32  * mrpt::opengl::Program.
33  *
34  * \sa CRenderizable
35  * \ingroup mrpt_opengl_grp
36  */
37 class Shader
38 {
39  public:
40  Shader() = default;
41  ~Shader();
42  Shader& operator=(const Shader&) = delete;
43  Shader(const Shader&) = delete;
45  Shader(Shader&&);
46 
47  bool empty() const { return m_shader == 0; }
48  /** Frees the shader program in OpenGL. */
49  void clear();
50 
51  /** Build a shader from source code.
52  * \param[in] type Any valid argument to glCreateShader()
53  * \param[in] shaderCode The shading source code. Tip: users can read it
54  * from a file with mrpt::io::file_get_contents().
55  * \param[out] outErrorMessages If provided, build errors will be saved
56  * here. If not, they will dumped to std::cerr
57  * \return false on error.
58  */
59  bool compile(
60  unsigned int type, const std::string& shaderCode,
61  mrpt::optional_ref<std::string> outErrorMessages = std::nullopt);
62 
63  unsigned int handle() const { return m_shader; }
64 
65  private:
66  unsigned int m_shader = 0;
67 };
68 
69 /** A resource handling helper for OpenGL Shader "programs".
70  *
71  * The OpenGL "program" resource will be freed upon destruction or when clear()
72  * is called.
73  *
74  * \sa CRenderizable
75  * \ingroup mrpt_opengl_grp
76  */
77 class Program
78 {
79  public:
80  Program() = default;
81  ~Program();
82 
84 
85  bool empty() const { return m_program == 0; }
86  /** Frees the shader program in OpenGL. */
87  void clear();
88 
89  /** Links an OpenGL program with all shader code fragments previously
90  * inserted into shaders.
91  * \param[in,out] shaders The shader code fragments. Will be moved into this
92  * Program object, who will become the owner from now on and will eventually
93  * free its resources.
94  * \param[out] outErrorMessages If provided, build
95  * errors will be saved here. If not, they will dumped to std::cerr
96  *
97  * \return
98  * false on error.
99  */
100  bool linkProgram(
101  std::vector<Shader>& shaders,
102  mrpt::optional_ref<std::string> outErrorMessages = std::nullopt);
103 
104  void declareUniform(const std::string& name);
105  void declareAttribute(const std::string& name);
106 
107  unsigned int programId() const
108  {
109  ASSERT_(m_program != 0);
110  return m_program;
111  }
112 
113  int uniformId(const char* name) const { return m_uniforms.at(name); }
114  int attributeId(const char* name) const { return m_attribs.at(name); }
115 
116  bool hasUniform(const char* name) const
117  {
118  return m_uniforms.count(name) != 0;
119  }
120  bool hasAttribute(const char* name) const
121  {
122  return m_attribs.count(name) != 0;
123  }
124 
125  /** Prints a textual summary of the program */
126  void dumpProgramDescription(std::ostream& o) const;
127 
128  private:
129  std::vector<Shader> m_shaders;
130  unsigned int m_program = 0;
131 
132  /** OpenGL Uniforms/attribs defined by the user as inputs/outputs in shader
133  * code.
134  * \sa declareUniform(), declareAttribute();
135  */
136  std::unordered_map<std::string, int> m_uniforms, m_attribs;
137 };
138 
139 } // namespace mrpt::opengl
bool hasAttribute(const char *name) const
Definition: Shader.h:120
Shader & operator=(const Shader &)=delete
std::optional< std::reference_wrapper< T > > optional_ref
Shorter name for std::optional<std::reference_wrapper<T>>
Definition: optional_ref.h:20
bool compile(unsigned int type, const std::string &shaderCode, mrpt::optional_ref< std::string > outErrorMessages=std::nullopt)
Build a shader from source code.
Definition: Shader.cpp:43
std::vector< shader_id_t > shader_list_t
A list of shader IDs.
Definition: Shader.h:26
void declareAttribute(const std::string &name)
Definition: Shader.cpp:172
void declareUniform(const std::string &name)
Definition: Shader.cpp:152
int attributeId(const char *name) const
Definition: Shader.h:114
unsigned int m_program
Definition: Shader.h:130
unsigned int programId() const
Definition: Shader.h:107
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
std::unordered_map< std::string, int > m_attribs
Definition: Shader.h:136
uint8_t shader_id_t
Type for IDs of shaders.
Definition: Shader.h:23
A resource handling helper for OpenGL "Shader" compiled code fragment.
Definition: Shader.h:37
bool empty() const
Definition: Shader.h:85
bool hasUniform(const char *name) const
Definition: Shader.h:116
void clear()
Frees the shader program in OpenGL.
Definition: Shader.cpp:87
void clear()
Frees the shader program in OpenGL.
Definition: Shader.cpp:34
unsigned int m_shader
Definition: Shader.h:66
bool empty() const
Definition: Shader.h:47
bool linkProgram(std::vector< Shader > &shaders, mrpt::optional_ref< std::string > outErrorMessages=std::nullopt)
Links an OpenGL program with all shader code fragments previously inserted into shaders.
Definition: Shader.cpp:109
unsigned int handle() const
Definition: Shader.h:63
void dumpProgramDescription(std::ostream &o) const
Prints a textual summary of the program.
Definition: Shader.cpp:194
std::vector< Shader > m_shaders
Definition: Shader.h:129
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
int uniformId(const char *name) const
Definition: Shader.h:113
A resource handling helper for OpenGL Shader "programs".
Definition: Shader.h:77
std::unordered_map< std::string, int > m_uniforms
OpenGL Uniforms/attribs defined by the user as inputs/outputs in shader code.
Definition: Shader.h:136



Page generated by Doxygen 1.8.14 for MRPT 2.0.0 Git: b38439d21 Tue Mar 31 19:58:06 2020 +0200 at miƩ abr 1 00:50:30 CEST 2020