Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gpuprogram.h
Go to the documentation of this file.
1 /*
2  This file is part of Mitsuba, a physically based rendering system.
3 
4  Copyright (c) 2007-2014 by Wenzel Jakob and others.
5 
6  Mitsuba is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Mitsuba is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #if !defined(__MITSUBA_HW_GPUPROGRAM_H_)
21 #define __MITSUBA_HW_GPUPROGRAM_H_
22 
23 #include <mitsuba/hw/renderer.h>
24 
26 
27 /** \brief Abstract shader program (for fragment/vertex shading)
28  * \ingroup libhw
29  */
31 public:
32  /// Denotes the different components of this program
33  enum EType {
34  EVertexProgram = 0,
36  EGeometryProgram
37  };
38 
39  /// Common geometry shader input/output types
46  EQuadrilaterals
47  };
48 
49  /// Create an empty program
50  GPUProgram(const std::string &name = "default");
51 
52  /// Set the name of this program
53  inline void setName(const std::string &name) { m_name = name; }
54 
55  /// Return the name of this program
56  inline const std::string &getName() const { return m_name; }
57 
58  /// Set the source code of this program
59  inline void setSource(EType type, const std::string &source) { m_source[type] = source; }
60 
61  /// Set the source code of this program by filename
62  void setSourceFile(EType type, const fs::path &path);
63 
64  /// Get the source code of this program
65  inline const std::string &getSource(EType type) const { return m_source[type]; }
66 
67  /// Upload to the GPU
68  virtual void init() = 0;
69 
70  /// Bind the shader
71  virtual void bind() = 0;
72 
73  /// Set the default shader program
74  virtual void unbind() = 0;
75 
76  /// Remove all allocated handles
77  virtual void cleanup() = 0;
78 
79  /// Determine the ID number of a named parameter
80  virtual int getParameterID(const std::string &name, bool failIfMissing = true) const = 0;
81 
82  /// Set a boolean parameter by name
83  inline void setParameter(const std::string &name, bool value,
84  bool failIfMissing = true) {
85  setParameter(getParameterID(name, failIfMissing), value);
86  }
87 
88  /// Set a integer parameter by name
89  inline void setParameter(const std::string &name, int value,
90  bool failIfMissing = true) {
91  setParameter(getParameterID(name, failIfMissing), value);
92  }
93 
94  /// Set an unsigned integer parameter by name
95  inline void setParameter(const std::string &name, uint32_t value,
96  bool failIfMissing = true) {
97  setParameter(getParameterID(name, failIfMissing), value);
98  }
99 
100  /// Set a float parameter by name
101  inline void setParameter(const std::string &name, Float value,
102  bool failIfMissing = true) {
103  setParameter(getParameterID(name, failIfMissing), value);
104  }
105 
106  /// Set a Vector parameter by name
107  inline void setParameter(const std::string &name, const Vector &value,
108  bool failIfMissing = true) {
109  setParameter(getParameterID(name, failIfMissing), value);
110  }
111 
112  /// Set a Vector3i parameter by name
113  inline void setParameter(const std::string &name, const Vector3i &value,
114  bool failIfMissing = true) {
115  setParameter(getParameterID(name, failIfMissing), value);
116  }
117 
118  /// Set a Vector2 parameter by name
119  inline void setParameter(const std::string &name, const Vector2 &value,
120  bool failIfMissing = true) {
121  setParameter(getParameterID(name, failIfMissing), value);
122  }
123 
124  /// Set a Vector2i parameter by name
125  inline void setParameter(const std::string &name, const Vector2i &value,
126  bool failIfMissing = true) {
127  setParameter(getParameterID(name, failIfMissing), value);
128  }
129 
130  /// Set a Vector4 parameter by name
131  inline void setParameter(const std::string &name, const Vector4 &value,
132  bool failIfMissing = true) {
133  setParameter(getParameterID(name, failIfMissing), value);
134  }
135 
136  /// Set a Point parameter by name
137  inline void setParameter(const std::string &name, const Point &value,
138  bool failIfMissing = true) {
139  setParameter(getParameterID(name, failIfMissing), value);
140  }
141 
142  /// Set a Point3i parameter by name
143  inline void setParameter(const std::string &name, const Point3i &value,
144  bool failIfMissing = true) {
145  setParameter(getParameterID(name, failIfMissing), value);
146  }
147 
148  /// Set a Point2 parameter by name
149  inline void setParameter(const std::string &name, const Point2 &value,
150  bool failIfMissing = true) {
151  setParameter(getParameterID(name, failIfMissing), value);
152  }
153 
154  /// Set a Point2i parameter by name
155  inline void setParameter(const std::string &name, const Point2i &value,
156  bool failIfMissing = true) {
157  setParameter(getParameterID(name, failIfMissing), value);
158  }
159 
160  /// Set a Point4 parameter by name
161  inline void setParameter(const std::string &name, const Point4 &value,
162  bool failIfMissing = true) {
163  setParameter(getParameterID(name, failIfMissing), value);
164  }
165 
166  /// Set a Matrix2x2 parameter by name
167  inline void setParameter(const std::string &name, const Matrix2x2 &value,
168  bool failIfMissing = true) {
169  setParameter(getParameterID(name, failIfMissing), value);
170  }
171 
172  /// Set a Matrix3x3 parameter by name
173  inline void setParameter(const std::string &name, const Matrix3x3 &value,
174  bool failIfMissing = true) {
175  setParameter(getParameterID(name, failIfMissing), value);
176  }
177 
178  /// Set a Matrix4x4 parameter by name
179  inline void setParameter(const std::string &name, const Matrix4x4 &value,
180  bool failIfMissing = true) {
181  setParameter(getParameterID(name, failIfMissing), value);
182  }
183 
184  /// Set a Transform parameter by name
185  inline void setParameter(const std::string &name, const Transform &value,
186  bool failIfMissing = true) {
187  setParameter(getParameterID(name, failIfMissing), value.getMatrix());
188  }
189 
190  /// Set a Color3 parameter by name
191  inline void setParameter(const std::string &name, const Color3 &value,
192  bool failIfMissing = true) {
193  setParameter(getParameterID(name, failIfMissing), value);
194  }
195 
196  /// Set a Spectrum parameter (will be converted to linear RGB) by name
197  inline void setParameter(const std::string &name, const Spectrum &value,
198  bool failIfMissing = true) {
199  setParameter(getParameterID(name, failIfMissing), value);
200  }
201 
202  /** Set a GPUTexture parameter by name. Must be executed after
203  binding the texture to a texture unit */
204  inline void setParameter(const std::string &name, const GPUTexture *value,
205  bool failIfMissing = true) {
206  setParameter(getParameterID(name, failIfMissing), value);
207  }
208 
209  /// Set a boolean parameter
210  virtual void setParameter(int id, bool value) = 0;
211 
212  /// Set a float parameter
213  virtual void setParameter(int id, Float value) = 0;
214 
215  /// Set a int parameter
216  virtual void setParameter(int id, int value) = 0;
217 
218  /// Set a uint32_t parameter
219  virtual void setParameter(int id, uint32_t value) = 0;
220 
221  /// Set a Vector parameter
222  virtual void setParameter(int id, const Vector &value) = 0;
223 
224  /// Set a Vector3i parameter
225  virtual void setParameter(int id, const Vector3i &value) = 0;
226 
227  /// Set a Vector2 parameter
228  virtual void setParameter(int id, const Vector2 &value) = 0;
229 
230  /// Set a Vector2i parameter
231  virtual void setParameter(int id, const Vector2i &value) = 0;
232 
233  /// Set a Vector4 parameter
234  virtual void setParameter(int id, const Vector4 &value) = 0;
235 
236  /// Set a Point parameter
237  virtual void setParameter(int id, const Point &value) = 0;
238 
239  /// Set a Point3i parameter
240  virtual void setParameter(int id, const Point3i &value) = 0;
241 
242  /// Set a Point2 parameter
243  virtual void setParameter(int id, const Point2 &value) = 0;
244 
245  /// Set a Point2i parameter
246  virtual void setParameter(int id, const Point2i &value) = 0;
247 
248  /// Set a Point4 parameter
249  virtual void setParameter(int id, const Point4 &value) = 0;
250 
251  /// Set a Matrix2x2 parameter
252  virtual void setParameter(int id, const Matrix2x2 &value) = 0;
253 
254  /// Set a Matrix3x3 parameter
255  virtual void setParameter(int id, const Matrix3x3 &value) = 0;
256 
257  /// Set a Matrix4x4 parameter
258  virtual void setParameter(int id, const Matrix4x4 &value) = 0;
259 
260  /// Set a Transform parameter
261  inline void setParameter(int id, const Transform &value) {
262  setParameter(id, value.getMatrix());
263  }
264 
265  /// Set a Color3 parameter
266  virtual void setParameter(int id, const Color3 &value) = 0;
267 
268  /// Set a Spectrum parameter (will be converted to linear RGB)
269  virtual void setParameter(int id, const Spectrum &value) = 0;
270 
271  /** Set a GPUTexture parameter. Must be executed after
272  binding the texture to a texture unit */
273  virtual void setParameter(int id, const GPUTexture *value) = 0;
274 
275  /// Return a string representation of this class
276  std::string toString() const;
277 
278  /// Returns the max. number of vertices generated by the geometry shader
279  inline int getMaxVertices() const { return m_maxVertices; }
280 
281  /// Set the max. number of vertices generated by the geometry shader
282  inline void setMaxVertices(int maxVertices) { m_maxVertices = maxVertices; }
283 
284  /// Set the input type of the geometry shader
285  void setInputGeometryType(EGeometryType type) { m_inputGeometryType = type; }
286 
287  /// Return the input type of the geometry shader
288  inline EGeometryType getInputGeometryType() const { return m_inputGeometryType; }
289 
290  /// Set the output type of the geometry shader
291  void setOutputGeometryType(EGeometryType type) { m_outputGeometryType = type; }
292 
293  /// Return the output type of the geometry shader
294  inline EGeometryType getOutputGeometryType() const { return m_outputGeometryType; }
295 
296  /** Return whether or not this program is currently bound */
297  inline bool isBound() const { return m_bound; }
298 
299  /// Create a preprocessor definition (must be used before 'init')
300  inline void define(const std::string &key, const std::string &value = "") {
301  m_definitions[key] = value;
302  }
303 
305 protected:
306  /// Virtual destructor
307  virtual ~GPUProgram();
308 protected:
309  std::string m_name;
310  std::string m_source[3];
311  EGeometryType m_inputGeometryType;
312  EGeometryType m_outputGeometryType;
313  int m_maxVertices;
314  bool m_bound;
315  std::map<std::string, std::string> m_definitions;
316 };
317 
319 
320 #endif /* __MITSUBA_HW_GPUPROGRAM_H_ */
Definition: fwd.h:101
void setOutputGeometryType(EGeometryType type)
Set the output type of the geometry shader.
Definition: gpuprogram.h:291
Definition: gpuprogram.h:45
void setParameter(const std::string &name, const Point &value, bool failIfMissing=true)
Set a Point parameter by name.
Definition: gpuprogram.h:137
void define(const std::string &key, const std::string &value="")
Create a preprocessor definition (must be used before &#39;init&#39;)
Definition: gpuprogram.h:300
EGeometryType getOutputGeometryType() const
Return the output type of the geometry shader.
Definition: gpuprogram.h:294
Definition: gpuprogram.h:42
void setName(const std::string &name)
Set the name of this program.
Definition: gpuprogram.h:53
void setParameter(const std::string &name, const Vector3i &value, bool failIfMissing=true)
Set a Vector3i parameter by name.
Definition: gpuprogram.h:113
#define MTS_EXPORT_HW
Definition: platform.h:114
void setParameter(const std::string &name, const Point3i &value, bool failIfMissing=true)
Set a Point3i parameter by name.
Definition: gpuprogram.h:143
EGeometryType getInputGeometryType() const
Return the input type of the geometry shader.
Definition: gpuprogram.h:288
Basic 2x2 matrix data type.
Definition: matrix.h:455
void setParameter(const std::string &name, const Vector2i &value, bool failIfMissing=true)
Set a Vector2i parameter by name.
Definition: gpuprogram.h:125
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
Abstract shader program (for fragment/vertex shading)
Definition: gpuprogram.h:30
RGB color data type.
Definition: spectrum.h:612
void setInputGeometryType(EGeometryType type)
Set the input type of the geometry shader.
Definition: gpuprogram.h:285
EType
Denotes the different components of this program.
Definition: gpuprogram.h:33
void setParameter(const std::string &name, const Point4 &value, bool failIfMissing=true)
Set a Point4 parameter by name.
Definition: gpuprogram.h:161
void setParameter(const std::string &name, const Matrix4x4 &value, bool failIfMissing=true)
Set a Matrix4x4 parameter by name.
Definition: gpuprogram.h:179
void setParameter(const std::string &name, uint32_t value, bool failIfMissing=true)
Set an unsigned integer parameter by name.
Definition: gpuprogram.h:95
const std::string & getName() const
Return the name of this program.
Definition: gpuprogram.h:56
const Matrix4x4 & getMatrix() const
Return the underlying matrix.
Definition: transform.h:317
void setParameter(int id, const Transform &value)
Set a Transform parameter.
Definition: gpuprogram.h:261
void setParameter(const std::string &name, const Matrix2x2 &value, bool failIfMissing=true)
Set a Matrix2x2 parameter by name.
Definition: gpuprogram.h:167
#define MTS_DECLARE_CLASS()
This macro must be used in the initial definition in classes that derive from Object.
Definition: class.h:158
Definition: fwd.h:99
int getMaxVertices() const
Returns the max. number of vertices generated by the geometry shader.
Definition: gpuprogram.h:279
void setParameter(const std::string &name, const Point2 &value, bool failIfMissing=true)
Set a Point2 parameter by name.
Definition: gpuprogram.h:149
void setParameter(const std::string &name, const Spectrum &value, bool failIfMissing=true)
Set a Spectrum parameter (will be converted to linear RGB) by name.
Definition: gpuprogram.h:197
Definition: gpuprogram.h:41
Encapsulates a 4x4 linear transformation and its inverse.
Definition: transform.h:33
Definition: gpuprogram.h:43
EGeometryType
Common geometry shader input/output types.
Definition: gpuprogram.h:40
void setParameter(const std::string &name, int value, bool failIfMissing=true)
Set a integer parameter by name.
Definition: gpuprogram.h:89
A data structure for 1/2/3D and cube texture mapping. Also has optional render-to-texture functionali...
Definition: gputexture.h:32
Definition: fwd.h:96
void setMaxVertices(int maxVertices)
Set the max. number of vertices generated by the geometry shader.
Definition: gpuprogram.h:282
void setParameter(const std::string &name, bool value, bool failIfMissing=true)
Set a boolean parameter by name.
Definition: gpuprogram.h:83
void setParameter(const std::string &name, const Color3 &value, bool failIfMissing=true)
Set a Color3 parameter by name.
Definition: gpuprogram.h:191
const std::string & getSource(EType type) const
Get the source code of this program.
Definition: gpuprogram.h:65
Basic 4x4 matrix data type.
Definition: matrix.h:656
void setParameter(const std::string &name, Float value, bool failIfMissing=true)
Set a float parameter by name.
Definition: gpuprogram.h:101
void setParameter(const std::string &name, const GPUTexture *value, bool failIfMissing=true)
Definition: gpuprogram.h:204
void setSource(EType type, const std::string &source)
Set the source code of this program.
Definition: gpuprogram.h:59
Definition: fwd.h:100
Parent of all Mitsuba classes.
Definition: object.h:38
bool isBound() const
Definition: gpuprogram.h:297
Discrete spectral power distribution based on a number of wavelength bins over the 360-830 nm range...
Definition: spectrum.h:663
virtual std::string toString() const
Return a human-readable string representation of the object&#39;s contents.
Definition: fwd.h:97
void setParameter(const std::string &name, const Vector &value, bool failIfMissing=true)
Set a Vector parameter by name.
Definition: gpuprogram.h:107
Definition: gpuprogram.h:35
void setParameter(const std::string &name, const Matrix3x3 &value, bool failIfMissing=true)
Set a Matrix3x3 parameter by name.
Definition: gpuprogram.h:173
Definition: fwd.h:95
Definition: gpuprogram.h:44
#define MTS_NAMESPACE_END
Definition: platform.h:138
void setParameter(const std::string &name, const Transform &value, bool failIfMissing=true)
Set a Transform parameter by name.
Definition: gpuprogram.h:185
Basic 3x3 matrix data type.
Definition: matrix.h:560
void setParameter(const std::string &name, const Point2i &value, bool failIfMissing=true)
Set a Point2i parameter by name.
Definition: gpuprogram.h:155
void setParameter(const std::string &name, const Vector2 &value, bool failIfMissing=true)
Set a Vector2 parameter by name.
Definition: gpuprogram.h:119
void setParameter(const std::string &name, const Vector4 &value, bool failIfMissing=true)
Set a Vector4 parameter by name.
Definition: gpuprogram.h:131