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