HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GlslRenderer.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_GLSLRENDERER_H
7 #define MATERIALX_GLSLRENDERER_H
8 
9 /// @file
10 /// GLSL code renderer
11 
13 
17 
19 
21 
22 using GLContextPtr = std::shared_ptr<class GLContext>;
23 using SimpleWindowPtr = std::shared_ptr<class SimpleWindow>;
24 
25 /// Shared pointer to a GlslRenderer
26 using GlslRendererPtr = std::shared_ptr<class GlslRenderer>;
27 
28 /// @class GlslRenderer
29 /// Helper class for rendering generated GLSL code to produce images.
30 ///
31 /// There are two main interfaces which can be used. One which takes in a HwShader and one which
32 /// allows for explicit setting of shader stage code.
33 ///
34 /// The main services provided are:
35 /// - Validation: All shader stages are compiled and atteched to a GLSL shader program.
36 /// - Introspection: The compiled shader program is examined for uniforms and attributes.
37 /// - Binding: Uniforms and attributes which match the predefined variables generated the GLSL code generator
38 /// will have values assigned to this. This includes matrices, attribute streams, and textures.
39 /// - Rendering: The program with bound inputs will be used to drawing geometry to an offscreen buffer.
40 /// An interface is provided to save this offscreen buffer to disk using an externally defined image handler.
41 ///
43 {
44  public:
45  /// Create a GLSL renderer instance
46  static GlslRendererPtr create(unsigned int width = 512, unsigned int height = 512, Image::BaseType baseType = Image::BaseType::UINT8);
47 
48  /// Create a texture handler for OpenGL textures
50  {
51  return GLTextureHandler::create(imageLoader);
52  }
53 
54  /// Destructor
55  virtual ~GlslRenderer() { }
56 
57  /// @name Setup
58  /// @{
59 
60  /// Internal initialization of stages and OpenGL constructs
61  /// required for program validation and rendering.
62  /// An exception is thrown on failure.
63  /// The exception will contain a list of initialization errors.
64  /// @param renderContextHandle allows initializing the GlslRenderer with a Shared OpenGL Context
65  void initialize(RenderContextHandle renderContextHandle = nullptr) override;
66 
67  /// @}
68  /// @name Rendering
69  /// @{
70 
71  /// Create GLSL program based on an input shader
72  /// @param shader Input HwShader
73  void createProgram(ShaderPtr shader) override;
74 
75  /// Create GLSL program based on shader stage source code.
76  /// @param stages Map of name and source code for the shader stages.
77  void createProgram(const StageMap& stages) override;
78 
79  /// Validate inputs for the program
80  void validateInputs() override;
81 
82  /// Update the program with value of the uniform.
83  void updateUniform(const string& name, ConstValuePtr value) override;
84 
85  /// Set the size of the rendered image
86  void setSize(unsigned int width, unsigned int height) override;
87 
88  /// Render the current program to an offscreen buffer.
89  void render() override;
90 
91  /// Render the current program in texture space to an off-screen buffer.
92  void renderTextureSpace(const Vector2& uvMin, const Vector2& uvMax);
93 
94  /// @}
95  /// @name Utilities
96  /// @{
97 
98  /// Capture the current contents of the off-screen hardware buffer as an image.
99  ImagePtr captureImage(ImagePtr image = nullptr) override;
100 
101  /// Return the GL frame buffer.
103  {
104  return _framebuffer;
105  }
106 
107  /// Return the GLSL program.
109  {
110  return _program;
111  }
112 
113  /// Submit geometry for a screen-space quad.
114  void drawScreenSpaceQuad(const Vector2& uvMin = Vector2(0.0f), const Vector2& uvMax = Vector2(1.0f));
115 
116  /// Set the screen background color.
117  void setScreenColor(const Color3& screenColor)
118  {
119  _screenColor = screenColor;
120  }
121 
122  /// Return the screen background color.
124  {
125  return _screenColor;
126  }
127 
128  /// @}
129 
130  protected:
131  GlslRenderer(unsigned int width, unsigned int height, Image::BaseType baseType);
132 
133  private:
134  GlslProgramPtr _program;
135  GLFramebufferPtr _framebuffer;
136 
137  bool _initialized;
138 
139  SimpleWindowPtr _window;
140  GLContextPtr _context;
141  Color3 _screenColor;
142 };
143 
145 
146 #endif
virtual void updateUniform(const string &name, ConstValuePtr value)
Update the program with value of the uniform.
GlslProgramPtr getProgram()
Return the GLSL program.
Definition: GlslRenderer.h:108
virtual void validateInputs()
Validate inputs for the program.
GLbitfield stages
Definition: glcorearb.h:1931
std::shared_ptr< GLFramebuffer > GLFramebufferPtr
Shared pointer to a GLFramebuffer.
Definition: GLFramebuffer.h:21
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
void * RenderContextHandle
shared_ptr< const Value > ConstValuePtr
A shared pointer to a const Value.
Definition: Value.h:31
Color3 getScreenColor() const
Return the screen background color.
Definition: GlslRenderer.h:123
static ImageHandlerPtr create(ImageLoaderPtr imageLoader)
GLenum GLenum GLsizei void * image
Definition: glad.h:5132
GLFramebufferPtr getFramebuffer() const
Return the GL frame buffer.
Definition: GlslRenderer.h:102
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
BaseType
Definition: Image.h:48
GLfloat f
Definition: glcorearb.h:1926
std::shared_ptr< class GlslProgram > GlslProgramPtr
Definition: GlslProgram.h:24
std::shared_ptr< class SimpleWindow > SimpleWindowPtr
SimpleWindow shared pointer.
Definition: GLContext.h:34
std::shared_ptr< ImageHandler > ImageHandlerPtr
Shared pointer to an ImageHandler.
Definition: ImageHandler.h:32
virtual void createProgram(ShaderPtr shader)
Create program based on an input shader.
virtual void setSize(unsigned int width, unsigned int height)
Set the size of the rendered image.
std::shared_ptr< ImageLoader > ImageLoaderPtr
Shared pointer to an ImageLoader.
Definition: ImageHandler.h:35
Definition: Types.h:285
GLuint const GLchar * name
Definition: glcorearb.h:786
virtual ~GlslRenderer()
Destructor.
Definition: GlslRenderer.h:55
#define MX_RENDERGLSL_API
Definition: Export.h:18
virtual void initialize(RenderContextHandle=nullptr)
Initialize the renderer.
GLuint shader
Definition: glcorearb.h:785
std::shared_ptr< class GlslRenderer > GlslRendererPtr
Shared pointer to a GlslRenderer.
Definition: GlslRenderer.h:26
ImageHandlerPtr createImageHandler(ImageLoaderPtr imageLoader)
Create a texture handler for OpenGL textures.
Definition: GlslRenderer.h:49
void setScreenColor(const Color3 &screenColor)
Set the screen background color.
Definition: GlslRenderer.h:117
GLint GLsizei width
Definition: glcorearb.h:103
shared_ptr< Shader > ShaderPtr
Shared pointer to a Shader.
Definition: Library.h:34
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.
std::shared_ptr< class GLContext > GLContextPtr
GLContext shared pointer.
Definition: GLContext.h:37