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 // Copyright Contributors to the MaterialX Project
3 // SPDX-License-Identifier: Apache-2.0
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 /// Render context handle
22 /// Provides a provision for an application to share any renderer specific settings
23 using RenderContextHandle = void*;
24 
25 /// Shared pointer to a shader renderer
26 using ShaderRendererPtr = std::shared_ptr<class ShaderRenderer>;
27 
28 /// @class ShaderRenderer
29 /// Base class for renderers that generate shader code to produce images.
31 {
32  public:
33  /// Viewing API matrix conventions designation (default to OpenGL).
34  enum class MatrixConvention
35  {
36  OpenGL = 0,
37  Metal = 1
38  };
39  /// A map with name and source code for each shader stage.
41 
42  public:
43  virtual ~ShaderRenderer() { }
44 
45  /// @name Setup
46  /// @{
47 
48  /// Initialize the renderer.
49  virtual void initialize(RenderContextHandle = nullptr) { }
50 
51  /// Set the camera.
52  void setCamera(CameraPtr camera)
53  {
54  _camera = camera;
55  }
56 
57  /// Return the camera.
59  {
60  return _camera;
61  }
62 
63  /// Set the image handler used by this renderer for image I/O.
64  void setImageHandler(ImageHandlerPtr imageHandler)
65  {
66  _imageHandler = imageHandler;
67  }
68 
69  /// Return the image handler.
71  {
72  return _imageHandler;
73  }
74 
75  /// Set the light handler used by this renderer for light bindings.
76  void setLightHandler(LightHandlerPtr lightHandler)
77  {
78  _lightHandler = lightHandler;
79  }
80 
81  /// Return the light handler.
83  {
84  return _lightHandler;
85  }
86 
87  /// Set the geometry handler.
88  void setGeometryHandler(GeometryHandlerPtr geometryHandler)
89  {
90  _geometryHandler = geometryHandler;
91  }
92 
93  /// Return the geometry handler.
95  {
96  return _geometryHandler;
97  }
98 
99  /// @}
100  /// @name Rendering
101  /// @{
102 
103  /// Create program based on an input shader.
104  virtual void createProgram(ShaderPtr shader);
105 
106  /// Create program based on shader stage source code.
107  /// @param stages Map of name and source code for the shader stages.
108  virtual void createProgram(const StageMap& stages);
109 
110  /// Validate inputs for the program.
111  virtual void validateInputs() { }
112 
113  /// Update the program with value of the uniform.
114  virtual void updateUniform(const string& name, ConstValuePtr value);
115 
116  /// Set the size of the rendered image.
117  virtual void setSize(unsigned int width, unsigned int height);
118 
119  /// Render the current program to produce an image.
120  virtual void render() { }
121 
122  /// @}
123  /// @name Utilities
124  /// @{
125 
126  /// Capture the current rendered output as an image.
127  virtual ImagePtr captureImage(ImagePtr image = nullptr)
128  {
129  return nullptr;
130  }
131 
132  /// @}
133 
134  protected:
135  ShaderRenderer(unsigned int width, unsigned int height, Image::BaseType baseType,
136  MatrixConvention matrixConvention = MatrixConvention::OpenGL);
137 
138  protected:
139  unsigned int _width;
140  unsigned int _height;
142 
144 
149 };
150 
151 /// @class ExceptionRenderError
152 /// An exception that is thrown when a rendering operation fails.
153 /// Optionally stores an additional error log, which can be used to
154 /// store and retrieve shader compilation errors.
156 {
157  public:
158  ExceptionRenderError(const string& msg, const StringVec& errorLog = StringVec()) :
159  Exception(msg),
160  _errorLog(errorLog)
161  {
162  }
163 
165  Exception(e),
166  _errorLog(e._errorLog)
167  {
168  }
169 
171  {
173  _errorLog = e._errorLog;
174  return *this;
175  }
176 
177  const StringVec& errorLog() const
178  {
179  return _errorLog;
180  }
181 
182  private:
183  StringVec _errorLog;
184 };
185 
187 
188 #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:25
vector< string > StringVec
A vector of strings.
Definition: Library.h:57
Image::BaseType _baseType
void * RenderContextHandle
GeometryHandlerPtr _geometryHandler
shared_ptr< const Value > ConstValuePtr
A shared pointer to a const Value.
Definition: Value.h:31
ExceptionRenderError(const ExceptionRenderError &e)
virtual ~ShaderRenderer()
GLenum GLenum GLsizei void * image
Definition: glad.h:5132
std::shared_ptr< class LightHandler > LightHandlerPtr
Shared pointer to a LightHandler.
Definition: LightHandler.h:25
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.
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
BaseType
Definition: Image.h:48
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
MatrixConvention
Viewing API matrix conventions designation (default to OpenGL).
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.
GLuint const GLchar * name
Definition: glcorearb.h:786
void setImageHandler(ImageHandlerPtr imageHandler)
Set the image handler used by this renderer for image I/O.
CameraPtr _camera
virtual void initialize(RenderContextHandle=nullptr)
Initialize the renderer.
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
MatrixConvention _matrixConvention
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
GLint GLsizei width
Definition: glcorearb.h:103
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.
Definition: core.h:1131
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
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.
CameraPtr getCamera() const
Return the camera.
ImageHandlerPtr _imageHandler