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 compilation 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  void setDataLibraryOSOPath(const FilePath& dirPath)
191  {
192  _dataLibraryOSOPath = dirPath;
193  }
194 
195  /// Used to toggle to either use testrender or testshade during render validation
196  /// By default testshade is used.
197  /// @param useTestRender Indicate whether to use testrender.
198  void useTestRender(bool useTestRender)
199  {
200  _useTestRender = useTestRender;
201  }
202 
203  /// Used to switch between testing oso files and osl command strings
204  void useOslCommandString(bool useOslCmdstr)
205  {
206  _useOSLCmdStr = useOslCmdstr;
207  }
208 
209  /// Set the testrender `-aa N` value used for lit (closure-color) outputs.
210  /// Note: testrender treats this as the linear AA dimension and traces
211  /// `N * N` samples per pixel, so cost scales quadratically.
212  void setAaLit(int aa)
213  {
214  _aaLit = aa;
215  }
216 
217  /// Set the testrender `-aa N` value used for unlit (non-closure) outputs.
218  /// Same `N * N` semantics as setAaLit.
219  void setAaUnlit(int aa)
220  {
221  _aaUnlit = aa;
222  }
223 
224  /// Set the osl command string that is to be tested
225  void setOSLCmdStr(const string& oslCmd)
226  {
227  _oslCmdStr = oslCmd;
228  }
229  ///
230  /// Compile OSL code stored in a file. Will throw an exception if an error occurs.
231  /// @param oslFilePath OSL file path.
232  void compileOSL(const FilePath& oslFilePath);
233 
234  /// @}
235 
236  protected:
237  ///
238  /// Shade using OSO input file. Will throw an exception if an error occurs.
239  /// @param dirPath Path to location containing input .oso file.
240  /// @param shaderName Name of OSL shader. A corresponding .oso file is assumed to exist in the output path folder.
241  /// @param outputName Name of OSL shader output to use.
242  void shadeOSL(const FilePath& dirPath, const string& shaderName, const string& outputName);
243 
244  ///
245  /// Render using OSO input file. Will throw an exception if an error occurs.
246  /// @param dirPath Path to location containing input .oso file.
247  /// @param shaderName Name of OSL shader. A corresponding .oso file is assumed to exist in the output path folder.
248  /// @param outputName Name of OSL shader output to use.
249  void renderOSL(const FilePath& dirPath, const string& shaderName, const string& outputName);
250 
251  /// Render using OSL command string. Will throw an exception if an error occurs.
252  /// @param dirPath Path to location containing input .oso file.
253  /// @param shaderName Name of OSL shader. A corresponding .oso file is assumed to exist in the output path folder.
254  void renderOSLNetwork(const FilePath& dirPath, const string& shaderName);
255 
256  /// Constructor
257  OslRenderer(unsigned int width, unsigned int height, Image::BaseType baseType);
258 
259  private:
260  FilePath _oslCompilerExecutable;
261  FileSearchPath _oslIncludePath;
262  FilePath _oslOutputFilePath;
263  FilePath _oslOutputFileName;
264 
265  FilePath _oslTestShadeExecutable;
266  FilePath _oslTestRenderExecutable;
267  FilePath _oslTestRenderSceneTemplateFile;
268  string _oslShaderName;
269  StringVec _oslShaderParameterOverrides;
270  StringVec _envOslShaderParameterOverrides;
271  string _oslShaderOutputName;
272  string _oslShaderOutputType;
273  FilePath _oslUtilityOSOPath;
274  FilePath _dataLibraryOSOPath;
275  bool _useTestRender;
276  bool _useOSLCmdStr;
277  int _aaLit;
278  int _aaUnlit;
279  string _oslCmdStr;
280 };
281 
283 
284 #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
void setAaUnlit(int aa)
Definition: OslRenderer.h:219
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
vector< string > StringVec
A vector of strings.
Definition: Library.h:61
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 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 setDataLibraryOSOPath(const FilePath &dirPath)
Definition: OslRenderer.h:190
void setOslShaderOutput(const string &outputName, const string &outputType)
Definition: OslRenderer.h:143
virtual ImagePtr captureImage(ImagePtr=nullptr)
Capture the current rendered output as an image.
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 useTestRender(bool useTestRender)
Definition: OslRenderer.h:198
void setOSLCmdStr(const string &oslCmd)
Set the osl command string that is to be tested.
Definition: OslRenderer.h:225
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:33
void setAaLit(int aa)
Definition: OslRenderer.h:212
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
void useOslCommandString(bool useOslCmdstr)
Used to switch between testing oso files and osl command strings.
Definition: OslRenderer.h:204
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.