Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
renderer.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_RENDERER_H_)
21 #define __MITSUBA_HW_RENDERER_H_
22 
23 #include <mitsuba/hw/device.h>
24 #include <mitsuba/render/sensor.h>
25 #include <mitsuba/render/trimesh.h>
26 
28 
29 class GPUTexture;
30 class GPUGeometry;
31 class GPUProgram;
32 class GPUSync;
33 class Bitmap;
34 class Font;
35 
36 /**
37  * \brief Helper class, which documents the capabilities of a renderer implementation
38  * \ingroup libhw
39  */
41 public:
42  enum ECapability {
43  EShadingLanguage = 0,
54  ECapabilityCount
55  };
56 
58  memset(m_capabilities, 0, sizeof(m_capabilities));
59  }
60 
61  inline void setSupported(ECapability cap, bool supported) {
62  m_capabilities[cap] = supported;
63  }
64 
65  inline bool isSupported(ECapability cap) const {
66  return m_capabilities[cap];
67  }
68 
69  std::string toString() const;
70 
72 protected:
73  bool m_capabilities[ECapabilityCount];
74 };
75 
76 /** \brief Abstract renderer implementation
77  * \ingroup libhw
78  */
79 class MTS_EXPORT_HW Renderer : public Object {
80 public:
81  typedef std::pair<const GPUGeometry *, Matrix4x4> TransformedGPUGeometry;
82 
83  /* Possible blending modes */
84  enum EBlendMode {
85  EBlendNone = 0, ///< Blending turned off
86  EBlendAlpha, ///< Normal alpha blending
87  EBlendAdditive ///< Additive blending
88  };
89 
90  /// Possible culling modes
91  enum ECullMode {
92  ECullNone = 0, ///< No culling
93  ECullFront, ///< Front-face culling
94  ECullBack ///< Back-face culling
95  };
96 
97  /// Matrices of the fixed function pipeline
98  enum EMatrixType {
99  EProjection = 0,
100  EModelView
101  };
102 
103  // Instantiate a new renderer using the appropriate implementation.
104  static Renderer *create(Session *session);
105 
106  /// Return the renderer's capabilities
107  inline const RendererCapabilities *getCapabilities() const {
108  return m_capabilities.get();
109  }
110 
111  /**
112  * Initialize the renderer. Optionally, an existing renderer instance
113  * can be provided as a second argument -- this establishes a link
114  * between them to permit sharing of textures, programs, etc.
115  */
116  virtual void init(Device *device, Renderer *other = NULL);
117 
118  /**
119  * \brief Reconfigure the renderer for a certain device
120  * (e.g. after a resize event)
121  */
122  virtual void reconfigure(const Device *device) = 0;
123 
124  /// Shut the renderer down
125  virtual void shutdown();
126 
127  /// Create a new GPU texture object
128  virtual GPUTexture *createGPUTexture(const std::string &name,
129  Bitmap *bitmap = NULL) = 0;
130 
131  /// Create a new GPU program object
132  virtual GPUProgram *createGPUProgram(const std::string &name) = 0;
133 
134  /// Create a new synchronization object
135  virtual GPUSync *createGPUSync() = 0;
136 
137  /// Clear the viewport
138  virtual void clear() = 0;
139 
140  /// Configure the camera
141  virtual void setCamera(const ProjectiveCamera *pCamera,
142  const Point2 &apertureSample = Point2(0.5f),
143  const Point2 &aaSample = Point2(0.5f),
144  Float timeSample = 0.5f) = 0;
145 
146  /// Configure the camera (manual)
147  virtual void setCamera(const Matrix4x4 &proj, const Matrix4x4 &view) = 0;
148 
149  /// Directly set the modelview or projection matrix
150  virtual void setMatrix(EMatrixType type, const Matrix4x4 &value) = 0;
151 
152  /// Fetch the currently set modelview or projection matrix
153  virtual Matrix4x4 getMatrix(EMatrixType type) const = 0;
154 
155  /// Set up the renderer for drawing triangle geometry
156  virtual void beginDrawingMeshes(bool transmitOnlyPositions = false) = 0;
157 
158  /// Send a triangle mesh to the renderer
159  virtual void drawMesh(const TriMesh *shape) = 0;
160 
161  /// Send a triangle mesh to the renderer
162  virtual void drawMesh(const GPUGeometry *geo) = 0;
163 
164  /// Clean up the renderer after drawing triangle geometry
165  virtual void endDrawingMeshes() = 0;
166 
167  /**
168  * \brief Quickly draw all geometry that has been registered
169  * with the renderer.
170  *
171  * Only transmits positions, hence this is mainly useful for
172  * shadow mapping.
173  */
174  virtual void drawAll(const std::vector<TransformedGPUGeometry> &geo) = 0;
175 
176  /// Draw a quad using the given texture
177  virtual void blitTexture(const GPUTexture *texture,
178  bool flipVertically = false,
179  bool centerHoriz = true, bool centerVert = true,
180  const Vector2i &offset = Vector2i(0, 0)) = 0;
181 
182  /// Blit a screen-sized quad
183  virtual void blitQuad(bool flipVertically) = 0;
184 
185  /**
186  * Draw a line of text on the screen. The coordinates are specified
187  * in pixel coordinates, where the upper left corner is the origin
188  */
189  virtual void drawText(const Point2i &pos,
190  const Font *font, const std::string &text) = 0;
191 
192  /// Set the size of point primitives
193  virtual void setPointSize(Float size) = 0;
194 
195  /// Draw a point
196  virtual void drawPoint(const Point &p) = 0;
197 
198  /// Draw a line between two specified points
199  virtual void drawLine(const Point &a, const Point &b) = 0;
200 
201  /// Draw a point (2D)
202  virtual void drawPoint(const Point2 &p) = 0;
203 
204  /// Draw a point (2D, integer coordinates)
205  virtual void drawPoint(const Point2i &p) = 0;
206 
207  /// Draw a line between two specified points (2D)
208  virtual void drawLine(const Point2 &a, const Point2 &b) = 0;
209 
210  /// Draw a line between two specified points (2D, integer coordinates)
211  virtual void drawLine(const Point2i &a, const Point2i &b) = 0;
212 
213  /// Draw a rectangle between two specified points (2D)
214  virtual void drawRectangle(const Point2 &a, const Point2 &b) = 0;
215 
216  /// Draw a rectangle between two specified points (2D, integer coordinates)
217  virtual void drawRectangle(const Point2i &a, const Point2i &b) = 0;
218 
219  /// Draw a filled rectangle between two specified points (2D)
220  virtual void drawFilledRectangle(const Point2 &a, const Point2 &b) = 0;
221 
222  /// Draw a filled rectangle between two specified points (2D, integer coordinates)
223  virtual void drawFilledRectangle(const Point2i &a, const Point2i &b) = 0;
224 
225  /// Draw an ellipse with the specified center and axes
226  virtual void drawEllipse(const Point &center,
227  const Vector &axis1, const Vector &axis2) = 0;
228 
229  /// Draw a wire-frame axis-aligned box
230  virtual void drawAABB(const AABB &aabb) = 0;
231 
232  /// Set the currently active blending mode
233  virtual void setBlendMode(EBlendMode mode) = 0;
234 
235  /// Set the currently active culling mode
236  virtual void setCullMode(ECullMode mode) = 0;
237 
238  /// Activate or deactivate depth testing
239  virtual void setDepthTest(bool value) = 0;
240 
241  /// Activate or deactivate the writing of depth information
242  virtual void setDepthMask(bool value) = 0;
243 
244  /// Set the current fixed-function pipeline color
245  virtual void setColor(const Color3 &color, Float alpha = 1.0f) = 0;
246 
247  /// Set the current fixed-function pipeline color
248  virtual void setColor(const Spectrum &spec, Float alpha = 1.0f) = 0;
249 
250  /// Set the depth value that is written by \ref clear()
251  virtual void setClearDepth(Float depth) = 0;
252 
253  /// Set the color value that is written by \ref clear()
254  virtual void setClearColor(const Color3 &color) = 0;
255 
256  /// Clear the view and projection transformations
257  virtual void clearTransforms() = 0;
258 
259  /// Flush outstanding rendering commands
260  virtual void flush() = 0;
261 
262  /// Completely finish outstanding rendering commands
263  virtual void finish() = 0;
264 
265  /// Check for any error indications
266  virtual void checkError(bool onlyWarn = true) = 0;
267 
268  /**
269  * Register a shader with this renderer. Increases the
270  * reference count if it already exists
271  */
272  Shader *registerShaderForResource(const HWResource *res);
273 
274  /// Look up a shader by the associated HWResource object
275  Shader *getShaderForResource(const HWResource *res);
276 
277  /**
278  * \brief Decrease the reference count of a shader. Deletes
279  * it when zero is reached
280  */
281  void unregisterShaderForResource(const HWResource *res);
282 
283  /// Create a new GPU geometry object
284  virtual GPUGeometry *createGPUGeometry(const Shape *mesh) = 0;
285 
286  /**
287  * Register a triangle mesh with the renderer. This
288  * will transfer the associated geometry to the GPU,
289  * which accelerates later calls to drawMesh()
290  */
291  GPUGeometry *registerGeometry(const Shape *shape);
292 
293  /// Unregister a triangle mesh from the renderer.
294  bool unregisterGeometry(const Shape *shape);
295 
296  /// Set the log level
297  inline void setLogLevel(ELogLevel logLevel) { m_logLevel = logLevel; }
298 
299  /// Set the log level for warnings
300  inline void setWarnLogLevel(ELogLevel logLevel) { m_warnLogLevel = logLevel; }
301 
302  /**
303  * \brief Send a debug string to the rendering backend
304  *
305  * This is mainly useful when an OpenGL trace is captured
306  * by a tool such as 'apitrace'.
307  */
308  virtual void debugString(const std::string &text) = 0;
309 
311 protected:
312  /// Construct a new OpenI rendering interface
313  Renderer(Session *session);
314 
315  /// Virtual destructor
316  virtual ~Renderer();
317 protected:
318  struct ShaderRecord {
319  int refCount;
321  };
322 
326  std::map<const HWResource*, ShaderRecord> m_shaders;
327  std::map<const Shape*, GPUGeometry *> m_geometry;
328  bool m_initialized, m_borrowed;
329  std::string m_driverVendor;
330  std::string m_driverRenderer;
331  std::string m_driverVersion;
333 };
334 
336 
337 #endif /* __MITSUBA_HW_RENDERER_H_ */
Shader * shader
Definition: renderer.h:320
TVector2< int > Vector2i
Definition: fwd.h:108
EBlendMode
Definition: renderer.h:84
ref< Session > m_session
Definition: renderer.h:323
ELogLevel m_warnLogLevel
Definition: renderer.h:332
void setWarnLogLevel(ELogLevel logLevel)
Set the log level for warnings.
Definition: renderer.h:300
Abstract triangle mesh base class.
Definition: trimesh.h:68
General-purpose bitmap class with read and write support for several common file formats.
Definition: bitmap.h:50
Definition: renderer.h:318
Shader base class for use with a VPL-style renderer.
Definition: shader.h:54
ECapability
Definition: renderer.h:42
Utility class used to render text inside OpenGL programs using pre-rasterized TrueType fonts stored a...
Definition: font.h:34
#define MTS_EXPORT_HW
Definition: platform.h:114
std::pair< const GPUGeometry *, Matrix4x4 > TransformedGPUGeometry
Definition: renderer.h:81
Helper class, which documents the capabilities of a renderer implementation.
Definition: renderer.h:40
std::string m_driverVersion
Definition: renderer.h:331
Front-face culling.
Definition: renderer.h:93
Abstract GPU synchronization object implementing a memory fence operation.
Definition: gpusync.h:31
RendererCapabilities()
Definition: renderer.h:57
Abstract hardware resource.
Definition: shader.h:39
std::string m_driverRenderer
Definition: renderer.h:330
ref< Device > m_device
Definition: renderer.h:324
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
Abstract shader program (for fragment/vertex shading)
Definition: gpuprogram.h:30
std::string m_driverVendor
Definition: renderer.h:329
Abstract base class of all shapes.
Definition: shape.h:178
RGB color data type.
Definition: spectrum.h:612
ref< RendererCapabilities > m_capabilities
Definition: renderer.h:325
TPoint2< Float > Point2
Definition: fwd.h:129
Projective camera interface.
Definition: sensor.h:393
An abstract drawing device.
Definition: device.h:136
Axis-aligned bounding box data structure in three dimensions.
Definition: aabb.h:437
#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
ECullMode
Possible culling modes.
Definition: renderer.h:91
Reference counting helper.
Definition: ref.h:40
Abstract renderer implementation.
Definition: renderer.h:79
ELogLevel
Available Log message types.
Definition: formatter.h:28
A data structure for 1/2/3D and cube texture mapping. Also has optional render-to-texture functionali...
Definition: gputexture.h:32
Abstract windowing environment session.
Definition: session.h:32
Definition: fwd.h:96
Abstract geometry storage on a graphics card.
Definition: gpugeometry.h:32
bool m_initialized
Definition: renderer.h:328
Basic 4x4 matrix data type.
Definition: matrix.h:656
Normal alpha blending.
Definition: renderer.h:86
std::map< const HWResource *, ShaderRecord > m_shaders
Definition: renderer.h:326
void setLogLevel(ELogLevel logLevel)
Set the log level.
Definition: renderer.h:297
EMatrixType
Matrices of the fixed function pipeline.
Definition: renderer.h:98
void setSupported(ECapability cap, bool supported)
Definition: renderer.h:61
Definition: fwd.h:100
Parent of all Mitsuba classes.
Definition: object.h:38
int refCount
Definition: renderer.h:319
bool isSupported(ECapability cap) const
Definition: renderer.h:65
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:95
#define MTS_NAMESPACE_END
Definition: platform.h:138
const RendererCapabilities * getCapabilities() const
Return the renderer&#39;s capabilities.
Definition: renderer.h:107
std::map< const Shape *, GPUGeometry * > m_geometry
Definition: renderer.h:327