Debian Codegraph @

avogadro/rendering/shaderprogram.h

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #ifndef AVOGADRO_RENDERING_SHADERPROGRAM_H
7 #define AVOGADRO_RENDERING_SHADERPROGRAM_H
8
9 #include "avogadrorenderingexport.h"
10 #include <avogadro/core/avogadrocore.h>
11 #include <avogadro/core/types.h>
12 #include <avogadro/core/vector.h>
13
14 #include <map> // For member variables.
15 #include <string> // For member variables.
16 #include <vector> // For member variables.
17
18 namespace Avogadro {
19 namespace Rendering {
20
21 class Shader;
22 class Texture2D;
23
24 /**
25 * @class ShaderProgram shaderprogram.h <avogadro/rendering/shaderprogram.h>
26 * @brief The ShaderProgram uses one or more Shader objects.
27 * @author Marcus D. Hanwell
28 *
29 * This class creates a Vertex or Fragment shader, that can be attached to a
30 * ShaderProgram in order to render geometry etc.
31 */
32
33 class AVOGADRORENDERING_EXPORT ShaderProgram
34 {
35 public:
36 /** Options for attribute normalization. */
37 enum NormalizeOption
38 {
39 /// The values range across the limits of the numeric type.
40 /// This option instructs the rendering engine to normalize them to
41 /// the range [0.0, 1.0] for unsigned types, and [-1.0, 1.0] for signed
42 /// types.
43 /// For example, unsigned char values will be mapped so that 0 = 0.0,
44 /// and 255 = 1.0.
45 /// The resulting floating point numbers will be passed into
46 /// the shader program.
47 Normalize,
48 /// The values should be used as-is. Do not perform any normalization.
49 NoNormalize
50 };
51
52 ShaderProgram();
53 ~ShaderProgram();
54
55 /** Attach the supplied shader to this program.
56 * @note A maximum of one Vertex shader and one Fragment shader can be
57 * attached to a shader prorgram.
58 * @return true on success.
59 */
60 bool attachShader(const Shader& shader);
61
62 /** Detach the supplied shader from this program.
63 * @note A maximum of one Vertex shader and one Fragment shader can be
64 * attached to a shader prorgram.
65 * @return true on success.
66 */
67 bool detachShader(const Shader& shader);
68
69 /** Attempt to link the shader program.
70 * @return false on failure. Query error to get the reason.
71 * @note The shaders attached to the program must have been compiled.
72 */
73 bool link();
74
75 /** Bind the program in order to use it. If the program has not been linked
76 * then link() will be called.
77 */
78 bool bind();
79
80 /** Releases the shader program from the current context. */
81 void release();
82
83 /** Get the error message (empty if none) for the shader program. */
84 std::string error() const { return m_error; }
85
86 /** Enable the named attribute array. Return false if the attribute array is
87 * not contained in the linked shader program.
88 */
89 bool enableAttributeArray(const std::string& name);
90
91 /** Disable the named attribute array. Return false if the attribute array is
92 * not contained in the linked shader program.
93 */
94 bool disableAttributeArray(const std::string& name);
95
96 /** Use the named attribute array with the bound BufferObject.
97 * @param name of the attribute (as seen in the shader program).
98 * @param offset into the bound BufferObject.
99 * @param stride The stride of the element access (i.e. the size of each
100 * element in the currently bound BufferObject). 0 may be used to indicate
101 * tightly packed data.
102 * @param elementType Tag identifying the memory representation of the
103 * element.
104 * @param elementTupleSize The number of elements per vertex (e.g. a 3D
105 * position attribute would be 3).
106 * @param normalize Indicates the range used by the attribute data.
107 * See NormalizeOption for more information.
108 * @return false if the attribute array does not exist.
109 */
110 bool useAttributeArray(const std::string& name, int offset, size_t stride,
111 Avogadro::Type elementType, int elementTupleSize,
112 NormalizeOption normalize);
113
114 /** Upload the supplied array of tightly packed values to the named attribute.
115 * BufferObject attributes should be preferred and this may be removed in
116 * future.
117 *
118 * @param name Attribute name
119 * @param array Container of data. See note.
120 * @param tupleSize The number of elements per vertex, e.g. a 3D coordinate
121 * array will have a tuple size of 3.
122 * @param normalize Indicates the range used by the attribute data.
123 * See NormalizeOption for more information.
124 *
125 * @note The ContainerT type must have tightly packed values of
126 * ContainerT::value_type accessible by reference via ContainerT::operator[].
127 * Additionally, the standard size() and empty() methods must be implemented.
128 * The std::vector and Avogadro::Core::Array classes are examples of such
129 * supported containers.
130 */
131 template <class ContainerT>
132 bool setAttributeArray(const std::string& name, const ContainerT& array,
133 int tupleSize, NormalizeOption normalize);
134
135 /** Set the sampler @a samplerName to use the specified texture. */
136 bool setTextureSampler(const std::string& samplerName,
137 const Texture2D& texture);
138
139 /** Set the @p name uniform value to int @p i. */
140 bool setUniformValue(const std::string& name, int i);
141
142 /** Set the @p name uniform value to float @p f. */
143 bool setUniformValue(const std::string& name, float f);
144
145 /** Set the @p name uniform value to @p matrix. */
146 bool setUniformValue(const std::string& name, const Eigen::Matrix3f& matrix);
147 bool setUniformValue(const std::string& name, const Eigen::Matrix4f& matrix);
148
149 /** Set the @p name uniform value to the supplied value. @{ */
150 bool setUniformValue(const std::string& name, const Vector3f& v);
151 bool setUniformValue(const std::string& name, const Vector2i& v);
152 bool setUniformValue(const std::string& name, const Vector3ub& v);
153 /** @} */
154
155 protected:
156 bool setAttributeArrayInternal(const std::string& name, void* buffer,
157 Avogadro::Type type, int tupleSize,
158 NormalizeOption normalize);
159 Index m_handle;
160 Index m_vertexShader;
161 Index m_fragmentShader;
162
163 bool m_linked;
164
165 std::string m_error;
166
167 std::map<std::string, int> m_attributes;
168
169 std::map<const Texture2D*, int> m_textureUnitBindings;
170 std::vector<bool> m_boundTextureUnits;
171
172 private:
173 void initializeTextureUnits();
174 void releaseAllTextureUnits();
175 int findAttributeArray(const std::string& name);
176 int findUniform(const std::string& name);
177 };
178
179 template <class ContainerT>
180 inline bool ShaderProgram::setAttributeArray(const std::string& name,
181 const ContainerT& array,
182 int tupleSize,
183 NormalizeOption normalize)
184 {
185 if (array.empty()) {
186 m_error = "Refusing to upload empty array for attribute " + name + ".";
187 return false;
188 }
189 Type type = Avogadro::TypeTraits<typename ContainerT::value_type>::EnumValue;
190 return setAttributeArrayInternal(name, &array[0], type, tupleSize, normalize);
191 }
192
193 } // namespace Rendering
194 } // namespace Avogadro
195
196 #endif // AVOGADRO_RENDERING_SHADERPROGRAM_H