HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OslRenderer.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_OSLRENDERER_H
7 #define MATERIALX_OSLRENDERER_H
8 
9 /// @file
10 /// OSL code renderer
11 
13 
16 
18 
19 // Shared pointer to an OslRenderer
20 using OslRendererPtr = std::shared_ptr<class OslRenderer>;
21 
22 /// @class OslRenderer
23 /// Helper class for rendering generated OSL code to produce images.
24 ///
25 /// The main services provided are:
26 /// - Source code validation: Use of "oslc" to compile and test output results
27 /// - Introspection check: None at this time.
28 /// - Binding: None at this time.
29 /// - Render validation: Use of "testrender" to output rendered images. Assumes source compliation was success
30 /// as it depends on the existence of corresponding .oso files.
31 ///
33 {
34  public:
35  /// Create an OSL renderer instance
36  static OslRendererPtr create(unsigned int width = 512, unsigned int height = 512, Image::BaseType baseType = Image::BaseType::UINT8);
37 
38  /// Destructor
39  virtual ~OslRenderer();
40 
41  /// Color closure OSL string
42  static string OSL_CLOSURE_COLOR_STRING;
43 
44  /// @name Setup
45  /// @{
46 
47  /// Internal initialization required for program validation and rendering.
48  /// An exception is thrown on failure.
49  /// The exception will contain a list of initialization errors.
50  void initialize(RenderContextHandle renderContextHandle = nullptr) override;
51 
52  /// @}
53  /// @name Rendering
54  /// @{
55 
56  /// Create OSL program based on an input shader
57  ///
58  /// A valid executable and include path must be specified before calling this method.
59  /// setOslCompilerExecutable(), and setOslIncludePath().
60  ///
61  /// Additionally setOslOutputFilePath() should be set to allow for output of .osl and .oso
62  /// files to the appropriate path location to be used as input for render validation.
63  ///
64  /// If render validation is not required, then the same temporary name will be used for
65  /// all shaders validated using this method.
66  /// @param shader Input shader
67  void createProgram(ShaderPtr shader) override;
68 
69  /// Create OSL program based on shader stage source code.
70  /// @param stages Map of name and source code for the shader stages.
71  void createProgram(const StageMap& stages) override;
72 
73  /// Validate inputs for the compiled OSL program.
74  /// Note: Currently no validation has been implemented.
75  void validateInputs() override;
76 
77  /// Set the size for rendered image
78  void setSize(unsigned int width, unsigned int height) override;
79 
80  /// Render OSL program to disk.
81  /// This is done by using either "testshade" or "testrender".
82  /// Currently only "testshade" is supported.
83  ///
84  /// Usage of both executables requires compiled source (.oso) files as input.
85  /// A shader output must be set before running this test via the setOslOutputName() method to
86  /// ensure that the appropriate .oso files can be located.
87  void render() override;
88 
89  /// @}
90  /// @name Utilities
91  /// @{
92 
93  /// Capture the current rendered output as an image.
94  ImagePtr captureImage(ImagePtr image = nullptr) override;
95 
96  /// @}
97  /// @name Compilation settings
98  /// @{
99 
100  /// Set the path to the OSL executable. Note that it is assumed that this
101  /// references the location of the oslc executable.
102  /// @param executableFilePath Path to OSL compiler executable
103  void setOslCompilerExecutable(const FilePath& executableFilePath)
104  {
105  _oslCompilerExecutable = executableFilePath;
106  }
107 
108  /// Set the search locations for OSL include files.
109  /// @param dirPath Include path(s) for the OSL compiler. This should include the
110  /// path to stdosl.h.
111  void setOslIncludePath(const FileSearchPath& dirPath)
112  {
113  _oslIncludePath = dirPath;
114  }
115 
116  /// Set the location where compiled OSL files will reside.
117  /// @param dirPath Path to output location
118  void setOslOutputFilePath(const FilePath& dirPath)
119  {
120  _oslOutputFilePath = dirPath;
121  }
122 
123  /// Set shader parameter strings to be added to the scene XML file. These
124  /// strings will set parameter overrides for the shader.
125  void setShaderParameterOverrides(const StringVec& parameterOverrides)
126  {
127  _oslShaderParameterOverrides = parameterOverrides;
128  }
129 
130  /// Set shader parameter strings to be added to the scene XML file. These
131  /// strings will set parameter overrides for the shader.
132  void setEnvShaderParameterOverrides(const StringVec& parameterOverrides)
133  {
134  _envOslShaderParameterOverrides = parameterOverrides;
135  }
136 
137  /// Set the OSL shader output.
138  /// This is used during render validation if "testshade" or "testrender" is executed.
139  /// For testrender this value is used to replace the %shader_output% token in the
140  /// input scene file.
141  /// @param outputName Name of shader output
142  /// @param outputType The MaterialX type of the output
143  void setOslShaderOutput(const string& outputName, const string& outputType)
144  {
145  _oslShaderOutputName = outputName;
146  _oslShaderOutputType = outputType;
147  }
148 
149  /// Set the path to the OSL shading tester. Note that it is assumed that this
150  /// references the location of the "testshade" executable.
151  /// @param executableFilePath Path to OSL "testshade" executable
152  void setOslTestShadeExecutable(const FilePath& executableFilePath)
153  {
154  _oslTestShadeExecutable = executableFilePath;
155  }
156 
157  /// Set the path to the OSL rendering tester. Note that it is assumed that this
158  /// references the location of the "testrender" executable.
159  /// @param executableFilePath Path to OSL "testrender" executable
160  void setOslTestRenderExecutable(const FilePath& executableFilePath)
161  {
162  _oslTestRenderExecutable = executableFilePath;
163  }
164 
165  /// Set the XML scene file to use for testrender. This is a template file
166  /// with the following tokens for replacement:
167  /// - %shader% : which will be replaced with the name of the shader to use
168  /// - %shader_output% : which will be replace with the name of the shader output to use
169  /// @param templateFilePath Scene file name
170  void setOslTestRenderSceneTemplateFile(const FilePath& templateFilePath)
171  {
172  _oslTestRenderSceneTemplateFile = templateFilePath;
173  }
174 
175  /// Set the name of the shader to be used for the input XML scene file.
176  /// The value is used to replace the %shader% token in the file.
177  /// @param shaderName Name of shader
178  void setOslShaderName(const string& shaderName)
179  {
180  _oslShaderName = shaderName;
181  }
182 
183  /// Set the search path for dependent shaders (.oso files) which are used
184  /// when rendering with testrender.
185  /// @param dirPath Path to location containing .oso files.
186  void setOslUtilityOSOPath(const FilePath& dirPath)
187  {
188  _oslUtilityOSOPath = dirPath;
189  }
190 
191  /// Used to toggle to either use testrender or testshade during render validation
192  /// By default testshade is used.
193  /// @param useTestRender Indicate whether to use testrender.
194  void useTestRender(bool useTestRender)
195  {
196  _useTestRender = useTestRender;
197  }
198 
199  /// Set the number of rays per pixel to be used for lit surfaces.
200  void setRaysPerPixelLit(int rays)
201  {
202  _raysPerPixelLit = rays;
203  }
204 
205  /// Set the number of rays per pixel to be used for unlit surfaces.
206  void setRaysPerPixelUnlit(int rays)
207  {
208  _raysPerPixelUnlit = rays;
209  }
210 
211  ///
212  /// Compile OSL code stored in a file. Will throw an exception if an error occurs.
213  /// @param oslFilePath OSL file path.
214  void compileOSL(const FilePath& oslFilePath);
215 
216  /// @}
217 
218  protected:
219  ///
220  /// Shade using OSO input file. Will throw an exception if an error occurs.
221  /// @param dirPath Path to location containing input .oso file.
222  /// @param shaderName Name of OSL shader. A corresponding .oso file is assumed to exist in the output path folder.
223  /// @param outputName Name of OSL shader output to use.
224  void shadeOSL(const FilePath& dirPath, const string& shaderName, const string& outputName);
225 
226  ///
227  /// Render using OSO input file. Will throw an exception if an error occurs.
228  /// @param dirPath Path to location containing input .oso file.
229  /// @param shaderName Name of OSL shader. A corresponding .oso file is assumed to exist in the output path folder.
230  /// @param outputName Name of OSL shader output to use.
231  void renderOSL(const FilePath& dirPath, const string& shaderName, const string& outputName);
232 
233  /// Constructor
234  OslRenderer(unsigned int width, unsigned int height, Image::BaseType baseType);
235 
236  private:
237  FilePath _oslCompilerExecutable;
238  FileSearchPath _oslIncludePath;
239  FilePath _oslOutputFilePath;
240  FilePath _oslOutputFileName;
241 
242  FilePath _oslTestShadeExecutable;
243  FilePath _oslTestRenderExecutable;
244  FilePath _oslTestRenderSceneTemplateFile;
245  string _oslShaderName;
246  StringVec _oslShaderParameterOverrides;
247  StringVec _envOslShaderParameterOverrides;
248  string _oslShaderOutputName;
249  string _oslShaderOutputType;
250  FilePath _oslUtilityOSOPath;
251  bool _useTestRender;
252  int _raysPerPixelLit;
253  int _raysPerPixelUnlit;
254 };
255 
257 
258 #endif
void setShaderParameterOverrides(const StringVec &parameterOverrides)
Definition: OslRenderer.h:125
void setOslOutputFilePath(const FilePath &dirPath)
Definition: OslRenderer.h:118
void setOslUtilityOSOPath(const FilePath &dirPath)
Definition: OslRenderer.h:186
virtual void validateInputs()
Validate inputs for the program.
Definition: File.h:26
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
void * RenderContextHandle
GLenum GLenum GLsizei void * image
Definition: glad.h:5132
void setOslIncludePath(const FileSearchPath &dirPath)
Definition: OslRenderer.h:111
void setOslTestShadeExecutable(const FilePath &executableFilePath)
Definition: OslRenderer.h:152
void setRaysPerPixelLit(int rays)
Set the number of rays per pixel to be used for lit surfaces.
Definition: OslRenderer.h:200
void setOslTestRenderSceneTemplateFile(const FilePath &templateFilePath)
Definition: OslRenderer.h:170
std::shared_ptr< class OslRenderer > OslRendererPtr
Definition: OslRenderer.h:20
StringMap StageMap
A map with name and source code for each shader stage.
void setEnvShaderParameterOverrides(const StringVec &parameterOverrides)
Definition: OslRenderer.h:132
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
BaseType
Definition: Image.h:48
void setOslShaderOutput(const string &outputName, const string &outputType)
Definition: OslRenderer.h:143
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.
void setRaysPerPixelUnlit(int rays)
Set the number of rays per pixel to be used for unlit surfaces.
Definition: OslRenderer.h:206
void useTestRender(bool useTestRender)
Definition: OslRenderer.h:194
void setOslCompilerExecutable(const FilePath &executableFilePath)
Definition: OslRenderer.h:103
virtual void initialize(RenderContextHandle=nullptr)
Initialize the renderer.
GLuint shader
Definition: glcorearb.h:785
static string OSL_CLOSURE_COLOR_STRING
Color closure OSL string.
Definition: OslRenderer.h:42
#define MX_RENDEROSL_API
Definition: Export.h:18
void setOslTestRenderExecutable(const FilePath &executableFilePath)
Definition: OslRenderer.h:160
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.
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
shared_ptr< Image > ImagePtr
A shared pointer to an image.
Definition: Image.h:23
void setOslShaderName(const string &shaderName)
Definition: OslRenderer.h:178
virtual void render()
Render the current program to produce an image.