HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
glslfx.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_IMAGING_HIO_GLSLFX_H
25 #define PXR_IMAGING_HIO_GLSLFX_H
26 
27 /// \file hio/glslfx.h
28 
29 #include "pxr/pxr.h"
30 #include "pxr/imaging/hio/api.h"
32 
33 #include "pxr/base/tf/token.h"
35 
36 #include <string>
37 #include <vector>
38 #include <set>
39 #include <map>
40 #include <memory>
41 
43 
44 // Version 1 - Added HioGlslfx::ExtractImports
45 //
46 #define HIO_GLSLFX_API_VERSION 1
47 
48 #define HIO_GLSLFX_TOKENS \
49  (glslfx) \
50  \
51  (fragmentShader) \
52  (geometryShader) \
53  (geometryShaderInjection) \
54  (preamble) \
55  (tessControlShader) \
56  (tessEvalShader) \
57  (postTessControlShader) \
58  (postTessVertexShader) \
59  (vertexShader) \
60  (vertexShaderInjection) \
61  \
62  (surfaceShader) \
63  (displacementShader) \
64  (volumeShader) \
65  ((defVal, "default"))
66 
67 
69 
70 /// \class HioGlslfx
71 ///
72 /// A class representing the config and shader source of a glslfx file.
73 ///
74 /// a HioGlslfx object is constructed by providing the path of a file whose
75 /// contents look something like this:
76 ///
77 /// \code
78 /// -- glslfx version 0.1
79 ///
80 /// -- configuration
81 ///
82 /// {
83 ///
84 /// 'textures' : {
85 /// 'texture_1':{
86 /// 'documentation' : 'a useful texture.',
87 /// },
88 /// 'texture_2':{
89 /// 'documentation' : 'another useful texture.',
90 /// },
91 /// },
92 /// 'parameters': {
93 /// 'param_1' : {
94 /// 'default' : 1.0,
95 /// 'documentation' : 'the first parameter'
96 /// },
97 /// 'param_2' : {
98 /// 'default' : [1.0, 1.0, 1.0],
99 /// 'documentation' : 'a vec3f parameter'
100 /// },
101 /// 'param_3' : {
102 /// 'default' : 2.0
103 /// },
104 /// 'param_4' : {
105 /// 'default' : True
106 /// },
107 /// 'param_5' : {
108 /// 'default' : [1.0, 1.0, 1.0],
109 /// 'role' : 'color'
110 /// 'documentation' : 'specifies a color for use in the shader'
111 /// },
112 /// },
113 /// 'parameterOrder': ['param_1',
114 /// 'param_2',
115 /// 'param_3',
116 /// 'param_4',
117 /// 'param_5'],
118 ///
119 /// 'techniques': {
120 /// 'default': {
121 /// 'fragmentShader': {
122 /// 'source': [ 'MyFragment' ]
123 /// }
124 /// },
125 /// 'metal': {
126 /// 'fragmentShader': {
127 /// 'source': [ 'MyFragment.Metal' ]
128 /// }
129 /// }
130 /// }
131 /// }
132 ///
133 /// -- glsl MyFragment
134 ///
135 /// uniform float param_1;
136 /// uniform float param_2;
137 /// uniform float param_3;
138 /// uniform float param_4;
139 /// uniform float param_5;
140 ///
141 /// void main()
142 /// {
143 /// // ...
144 /// // glsl code which consumes the various uniforms, and perhaps sets
145 /// // gl_FragColor = someOutputColor;
146 /// // ...
147 /// }
148 /// \endcode
149 ///
151 {
152 public:
153  /// Create an invalid glslfx object
154  HIO_API
155  HioGlslfx();
156 
157  /// Create a glslfx object from a file
158  HIO_API
159  HioGlslfx(
160  std::string const & filePath,
161  TfToken const & technique = HioGlslfxTokens->defVal);
162 
163  /// Create a glslfx object from a stream
164  HIO_API
165  HioGlslfx(
166  std::istream &is,
167  TfToken const & technique = HioGlslfxTokens->defVal);
168 
169  /// Return the parameters specified in the configuration
170  HIO_API
172 
173  /// Return the textures specified in the configuration
174  HIO_API
176 
177  /// Return the attributes specified in the configuration
178  HIO_API
180 
181  /// Return the metadata specified in the configuration
182  HIO_API
184 
185  /// Returns true if this is a valid glslfx file
186  HIO_API
187  bool IsValid(std::string *reason=NULL) const;
188 
189  /// \name Access to commonly used shader sources.
190  /// @{
191 
192  /// Get the surface source string
193  HIO_API
195 
196  /// Get the displacement source string
197  HIO_API
199 
200  /// Get the volume source string
201  HIO_API
203 
204  /// @}
205 
206  /// Get the layout config as a VtDictionary parsed from the JSON
207  /// layout config corresponding to the shader source associated
208  /// with the given keys.
209  HIO_API
210  VtDictionary GetLayoutAsDictionary(const TfTokenVector &shaderStageKeys,
211  std::string *errorStr) const;
212 
213  /// Get the shader source associated with given key
214  HIO_API
215  std::string GetSource(const TfToken &shaderStageKey) const;
216 
217  /// Get the original file name passed to the constructor
218  const std::string &GetFilePath() const { return _globalContext.filename; }
219 
220  /// Return set of all files processed for this glslfx object.
221  /// This includes the original file given to the constructor
222  /// as well as any other files that were imported. This set
223  /// will only contain files that exist.
224  const std::set<std::string>& GetFiles() const { return _seenFiles; }
225 
226  /// Return the computed hash value based on the string
227  size_t GetHash() const { return _hash; }
228 
229  /// Extract imported files from the specified glslfx file. The returned
230  /// paths are as-authored, in the order of declaration, with possible
231  /// duplicates. This function is not recursive -- it only extracts imports
232  /// from the specified \p filename.
233  HIO_API
234  static std::vector<std::string> ExtractImports(const std::string& filename);
235 
236 private:
237  class _ParseContext {
238  public:
239  _ParseContext() { }
240 
241  _ParseContext(std::string const & filePath) :
242  filename(filePath), lineNo(0), version(-1.0) { }
243 
245  int lineNo;
246  double version;
247  std::string currentLine;
248  std::string currentSectionType;
249  std::string currentSectionId;
250  std::vector<std::string> imports;
251  };
252 
253 private:
254  bool _ProcessFile(std::string const & filePath,
255  _ParseContext & context);
256  bool _ProcessInput(std::istream * input,
257  _ParseContext & context);
258  bool _ProcessImport(_ParseContext & context);
259  bool _ParseSectionLine(_ParseContext & context);
260  bool _ParseGLSLSectionLine(std::vector<std::string> const &tokens,
261  _ParseContext & context);
262  bool _ParseLayoutSectionLine(std::vector<std::string> const &tokens,
263  _ParseContext & context);
264  bool _ParseVersionLine(std::vector<std::string> const &tokens,
265  _ParseContext & context);
266  bool _ParseConfigurationLine(_ParseContext & context);
267  bool _ComposeConfiguration(std::string *reason);
268 
269  std::string _GetLayout(const TfToken &shaderStageKey) const;
270  std::string _GetSource(const TfToken &shaderStageKey) const;
271 
272  /// Get the layout config as a string formatted as JSON corresponding
273  /// to the shader source associated with the given keys.
274  std::string _GetLayoutAsString(const TfTokenVector &shaderStageKeys) const;
275 
276 private:
277  _ParseContext _globalContext;
278 
279  typedef std::map<std::string, std::string> _SourceMap;
280 
281  _SourceMap _sourceMap;
282  _SourceMap _layoutMap;
283  _SourceMap _configMap;
284  std::vector<std::string> _configOrder;
285  std::set<std::string> _seenFiles;
286 
287  std::unique_ptr<HioGlslfxConfig> _config;
288 
289  TfToken _technique;
290 
291  bool _valid;
292  std::string _invalidReason; // if _valid is false, reason why
293  size_t _hash;
294 };
295 
296 
298 
299 #endif
300 
GT_API const UT_StringHolder filename
std::vector< Attribute > Attributes
Definition: glslfxConfig.h:122
HIO_API HioGlslfxConfig::Parameters GetParameters() const
Return the parameters specified in the configuration.
static HIO_API std::vector< std::string > ExtractImports(const std::string &filename)
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
HIO_API HioGlslfxConfig::Attributes GetAttributes() const
Return the attributes specified in the configuration.
#define HIO_API
Definition: api.h:40
HIO_API bool IsValid(std::string *reason=NULL) const
Returns true if this is a valid glslfx file.
HIO_API std::string GetSurfaceSource() const
Get the surface source string.
const std::set< std::string > & GetFiles() const
Definition: glslfx.h:224
Definition: token.h:87
std::vector< Parameter > Parameters
Definition: glslfxConfig.h:82
size_t GetHash() const
Return the computed hash value based on the string.
Definition: glslfx.h:227
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
#define HIO_GLSLFX_TOKENS
Definition: glslfx.h:48
GT_API const UT_StringHolder version
const std::string & GetFilePath() const
Get the original file name passed to the constructor.
Definition: glslfx.h:218
HIO_API VtDictionary GetLayoutAsDictionary(const TfTokenVector &shaderStageKeys, std::string *errorStr) const
HIO_API HioGlslfxConfig::MetadataDictionary GetMetadata() const
Return the metadata specified in the configuration.
HIO_API HioGlslfxConfig::Textures GetTextures() const
Return the textures specified in the configuration.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
HIO_API std::string GetVolumeSource() const
Get the volume source string.
TF_DECLARE_PUBLIC_TOKENS(HioGlslfxTokens, HIO_API, HIO_GLSLFX_TOKENS)
std::vector< Texture > Textures
Definition: glslfxConfig.h:102
HIO_API HioGlslfx()
Create an invalid glslfx object.
HIO_API std::string GetSource(const TfToken &shaderStageKey) const
Get the shader source associated with given key.
HIO_API std::string GetDisplacementSource() const
Get the displacement source string.