HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ShaderRenderer.h
Go to the documentation of this file.
1 //
2 // TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
3 // All rights reserved. See LICENSE.txt for license.
4 //
5 
6 #ifndef MATERIALX_SHADERRENDERER_H
7 #define MATERIALX_SHADERRENDERER_H
8 
9 /// @file
10 /// Base class for shader rendering
11 
12 #include <MaterialXRender/Camera.h>
16 
18 
20 
21 /// Shared pointer to a shader renderer
22 using ShaderRendererPtr = std::shared_ptr<class ShaderRenderer>;
23 
24 /// @class ShaderRenderer
25 /// Base class for renderers that generate shader code to produce images.
27 {
28  public:
29  /// A map with name and source code for each shader stage.
31 
32  public:
33  virtual ~ShaderRenderer() { }
34 
35  /// @name Setup
36  /// @{
37 
38  /// Initialize the renderer.
39  virtual void initialize() { }
40 
41  /// Set the camera.
42  void setCamera(CameraPtr camera)
43  {
44  _camera = camera;
45  }
46 
47  /// Return the camera.
49  {
50  return _camera;
51  }
52 
53  /// Set the image handler used by this renderer for image I/O.
54  void setImageHandler(ImageHandlerPtr imageHandler)
55  {
56  _imageHandler = imageHandler;
57  }
58 
59  /// Return the image handler.
61  {
62  return _imageHandler;
63  }
64 
65  /// Set the light handler used by this renderer for light bindings.
66  void setLightHandler(LightHandlerPtr lightHandler)
67  {
68  _lightHandler = lightHandler;
69  }
70 
71  /// Return the light handler.
73  {
74  return _lightHandler;
75  }
76 
77  /// Set the geometry handler.
78  void setGeometryHandler(GeometryHandlerPtr geometryHandler)
79  {
80  _geometryHandler = geometryHandler;
81  }
82 
83  /// Return the geometry handler.
85  {
86  return _geometryHandler;
87  }
88 
89  /// @}
90  /// @name Rendering
91  /// @{
92 
93  /// Create program based on an input shader.
94  virtual void createProgram(ShaderPtr shader);
95 
96  /// Create program based on shader stage source code.
97  /// @param stages Map of name and source code for the shader stages.
98  virtual void createProgram(const StageMap& stages);
99 
100  /// Validate inputs for the program.
101  virtual void validateInputs() { }
102 
103  /// Set the size of the rendered image.
104  virtual void setSize(unsigned int width, unsigned int height);
105 
106  /// Render the current program to produce an image.
107  virtual void render() { }
108 
109  /// @}
110  /// @name Utilities
111  /// @{
112 
113  /// Capture the current rendered output as an image.
114  virtual ImagePtr captureImage(ImagePtr image = nullptr)
115  {
116  return nullptr;
117  }
118 
119  /// @}
120 
121  protected:
123  _width(0),
124  _height(0),
125  _baseType(Image::BaseType::UINT8)
126  { }
127 
128  ShaderRenderer(unsigned int width, unsigned int height, Image::BaseType baseType) :
129  _width(width),
130  _height(height),
131  _baseType(baseType)
132  { }
133 
134  protected:
135  unsigned int _width;
136  unsigned int _height;
138 
143 };
144 
145 /// @class ExceptionRenderError
146 /// An exception that is thrown when a rendering operation fails.
147 /// Optionally stores an additional error log, which can be used to
148 /// store and retrieve shader compilation errors.
150 {
151  public:
152  ExceptionRenderError(const string& msg, const StringVec& errorLog = StringVec()) :
153  Exception(msg),
154  _errorLog(errorLog)
155  {
156  }
157 
159  Exception(e),
160  _errorLog(e._errorLog)
161  {
162  }
163 
165  {
167  _errorLog = e._errorLog;
168  return *this;
169  }
170 
171  const StringVec& errorLog() const
172  {
173  return _errorLog;
174  }
175 
176  private:
177  StringVec _errorLog;
178 };
179 
181 
182 #endif
std::shared_ptr< class Camera > CameraPtr
Shared pointer to a Camera.
Definition: Camera.h:14
ExceptionRenderError & operator=(const ExceptionRenderError &e)
virtual void validateInputs()
Validate inputs for the program.
GLbitfield stages
Definition: glcorearb.h:1931
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:23
vector< string > StringVec
A vector of strings.
Definition: Library.h:57
Image::BaseType _baseType
GeometryHandlerPtr _geometryHandler
GLenum GLsizei GLenum GLenum const void * image
Definition: glew.h:4973
ExceptionRenderError(const ExceptionRenderError &e)
virtual ~ShaderRenderer()
Definition: Image.h:42
std::shared_ptr< class LightHandler > LightHandlerPtr
Shared pointer to a LightHandler.
Definition: LightHandler.h:24
unsigned int _width
Exception & operator=(const Exception &e)
Definition: Exception.h:34
StringMap StageMap
A map with name and source code for each shader stage.
BaseType
Definition: Image.h:45
GeometryHandlerPtr getGeometryHandler() const
Return the geometry handler.
ExceptionRenderError(const string &msg, const StringVec &errorLog=StringVec())
void setGeometryHandler(GeometryHandlerPtr geometryHandler)
Set the geometry handler.
LightHandlerPtr _lightHandler
const StringVec & errorLog() const
std::shared_ptr< ImageHandler > ImageHandlerPtr
Shared pointer to an ImageHandler.
Definition: ImageHandler.h:32
unsigned int _height
ImageHandlerPtr getImageHandler() const
Return the image handler.
void setCamera(CameraPtr camera)
Set the camera.
void setImageHandler(ImageHandlerPtr imageHandler)
Set the image handler used by this renderer for image I/O.
virtual void initialize()
Initialize the renderer.
GLint GLsizei width
Definition: glcorearb.h:103
CameraPtr _camera
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
GLuint shader
Definition: glcorearb.h:785
std::shared_ptr< class ShaderRenderer > ShaderRendererPtr
Shared pointer to a shader renderer.
#define MX_RENDER_API
Definition: Export.h:18
std::shared_ptr< class GeometryHandler > GeometryHandlerPtr
Shared pointer to an GeometryHandler.
LightHandlerPtr getLightHandler() const
Return the light handler.
std::unordered_map< string, string > StringMap
An unordered map with strings as both keys and values.
Definition: Library.h:59
shared_ptr< Shader > ShaderPtr
Shared pointer to a Shader.
Definition: Library.h:34
void setLightHandler(LightHandlerPtr lightHandler)
Set the light handler used by this renderer for light bindings.
virtual ImagePtr captureImage(ImagePtr image=nullptr)
Capture the current rendered output as an image.
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:24
shared_ptr< Image > ImagePtr
A shared pointer to an image.
Definition: Image.h:23
virtual void render()
Render the current program to produce an image.
ShaderRenderer(unsigned int width, unsigned int height, Image::BaseType baseType)
CameraPtr getCamera() const
Return the camera.
ImageHandlerPtr _imageHandler