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 terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_IMAGING_HIO_GLSLFX_H
8 #define PXR_IMAGING_HIO_GLSLFX_H
9 
10 /// \file hio/glslfx.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/imaging/hio/api.h"
15 
16 #include "pxr/base/tf/token.h"
18 
19 #include <string>
20 #include <vector>
21 #include <set>
22 #include <map>
23 #include <memory>
24 
26 
27 // Version 1 - Added HioGlslfx::ExtractImports
28 //
29 #define HIO_GLSLFX_API_VERSION 1
30 
31 #define HIO_GLSLFX_TOKENS \
32  (glslfx) \
33  \
34  (fragmentShader) \
35  (geometryShader) \
36  (geometryShaderInjection) \
37  (preamble) \
38  (tessControlShader) \
39  (tessEvalShader) \
40  (postTessControlShader) \
41  (postTessVertexShader) \
42  (vertexShader) \
43  (vertexShaderInjection) \
44  \
45  (surfaceShader) \
46  (displacementShader) \
47  (volumeShader) \
48  ((defVal, "default"))
49 
50 
52 
53 /// \class HioGlslfx
54 ///
55 /// A class representing the config and shader source of a glslfx file.
56 ///
57 /// a HioGlslfx object is constructed by providing the path of a file whose
58 /// contents look something like this:
59 ///
60 /// \code
61 /// -- glslfx version 0.1
62 ///
63 /// -- configuration
64 ///
65 /// {
66 ///
67 /// 'textures' : {
68 /// 'texture_1':{
69 /// 'documentation' : 'a useful texture.',
70 /// },
71 /// 'texture_2':{
72 /// 'documentation' : 'another useful texture.',
73 /// },
74 /// },
75 /// 'parameters': {
76 /// 'param_1' : {
77 /// 'default' : 1.0,
78 /// 'documentation' : 'the first parameter'
79 /// },
80 /// 'param_2' : {
81 /// 'default' : [1.0, 1.0, 1.0],
82 /// 'documentation' : 'a vec3f parameter'
83 /// },
84 /// 'param_3' : {
85 /// 'default' : 2.0
86 /// },
87 /// 'param_4' : {
88 /// 'default' : True
89 /// },
90 /// 'param_5' : {
91 /// 'default' : [1.0, 1.0, 1.0],
92 /// 'role' : 'color'
93 /// 'documentation' : 'specifies a color for use in the shader'
94 /// },
95 /// },
96 /// 'parameterOrder': ['param_1',
97 /// 'param_2',
98 /// 'param_3',
99 /// 'param_4',
100 /// 'param_5'],
101 ///
102 /// 'techniques': {
103 /// 'default': {
104 /// 'fragmentShader': {
105 /// 'source': [ 'MyFragment' ]
106 /// }
107 /// },
108 /// 'metal': {
109 /// 'fragmentShader': {
110 /// 'source': [ 'MyFragment.Metal' ]
111 /// }
112 /// }
113 /// }
114 /// }
115 ///
116 /// -- glsl MyFragment
117 ///
118 /// uniform float param_1;
119 /// uniform float param_2;
120 /// uniform float param_3;
121 /// uniform float param_4;
122 /// uniform float param_5;
123 ///
124 /// void main()
125 /// {
126 /// // ...
127 /// // glsl code which consumes the various uniforms, and perhaps sets
128 /// // gl_FragColor = someOutputColor;
129 /// // ...
130 /// }
131 /// \endcode
132 ///
134 {
135 public:
136  /// Create an invalid glslfx object
137  HIO_API
138  HioGlslfx();
139 
140  /// Create a glslfx object from a file
141  HIO_API
142  HioGlslfx(
143  std::string const & filePath,
144  TfToken const & technique = HioGlslfxTokens->defVal);
145 
146  /// Create a glslfx object from a stream
147  HIO_API
148  HioGlslfx(
149  std::istream &is,
150  TfToken const & technique = HioGlslfxTokens->defVal);
151 
152  /// Return the parameters specified in the configuration
153  HIO_API
155 
156  /// Return the textures specified in the configuration
157  HIO_API
159 
160  /// Return the attributes specified in the configuration
161  HIO_API
163 
164  /// Return the metadata specified in the configuration
165  HIO_API
167 
168  /// Returns true if this is a valid glslfx file
169  HIO_API
170  bool IsValid(std::string *reason=NULL) const;
171 
172  /// \name Access to commonly used shader sources.
173  /// @{
174 
175  /// Get the surface source string
176  HIO_API
177  std::string GetSurfaceSource() const;
178 
179  /// Get the displacement source string
180  HIO_API
181  std::string GetDisplacementSource() const;
182 
183  /// Get the volume source string
184  HIO_API
185  std::string GetVolumeSource() const;
186 
187  /// @}
188 
189  /// Get the layout config as a VtDictionary parsed from the JSON
190  /// layout config corresponding to the shader source associated
191  /// with the given keys.
192  HIO_API
193  VtDictionary GetLayoutAsDictionary(const TfTokenVector &shaderStageKeys,
194  std::string *errorStr) const;
195 
196  /// Get the shader source associated with given key
197  HIO_API
198  std::string GetSource(const TfToken &shaderStageKey) const;
199 
200  /// Get the original file name passed to the constructor
201  const std::string &GetFilePath() const { return _globalContext.filename; }
202 
203  /// Return set of all files processed for this glslfx object.
204  /// This includes the original file given to the constructor
205  /// as well as any other files that were imported. This set
206  /// will only contain files that exist.
207  const std::set<std::string>& GetFiles() const { return _seenFiles; }
208 
209  /// Return the computed hash value based on the string
210  size_t GetHash() const { return _hash; }
211 
212  /// Extract imported files from the specified glslfx file. The returned
213  /// paths are as-authored, in the order of declaration, with possible
214  /// duplicates. This function is not recursive -- it only extracts imports
215  /// from the specified \p filename.
216  HIO_API
217  static std::vector<std::string> ExtractImports(const std::string& filename);
218 
219 private:
220  class _ParseContext {
221  public:
222  _ParseContext() { }
223 
224  _ParseContext(std::string const & filePath) :
225  filename(filePath), lineNo(0), version(-1.0) { }
226 
227  std::string filename;
228  int lineNo;
229  double version;
230  std::string currentLine;
231  std::string currentSectionType;
232  std::string currentSectionId;
233  std::vector<std::string> imports;
234  };
235 
236 private:
237  bool _ProcessFile(std::string const & filePath,
238  _ParseContext & context);
239  bool _ProcessInput(std::istream * input,
240  _ParseContext & context);
241  bool _ProcessImport(_ParseContext & context);
242  bool _ParseSectionLine(_ParseContext & context);
243  bool _ParseGLSLSectionLine(std::vector<std::string> const &tokens,
244  _ParseContext & context);
245  bool _ParseLayoutSectionLine(std::vector<std::string> const &tokens,
246  _ParseContext & context);
247  bool _ParseVersionLine(std::vector<std::string> const &tokens,
248  _ParseContext & context);
249  bool _ParseConfigurationLine(_ParseContext & context);
250  bool _ComposeConfiguration(std::string *reason);
251 
252  std::string _GetLayout(const TfToken &shaderStageKey) const;
253  std::string _GetSource(const TfToken &shaderStageKey) const;
254 
255  /// Get the layout config as a string formatted as JSON corresponding
256  /// to the shader source associated with the given keys.
257  std::string _GetLayoutAsString(const TfTokenVector &shaderStageKeys) const;
258 
259 private:
260  _ParseContext _globalContext;
261 
262  typedef std::map<std::string, std::string> _SourceMap;
263 
264  _SourceMap _sourceMap;
265  _SourceMap _layoutMap;
266  _SourceMap _configMap;
267  std::vector<std::string> _configOrder;
268  std::set<std::string> _seenFiles;
269 
270  std::unique_ptr<HioGlslfxConfig> _config;
271 
272  TfToken _technique;
273 
274  bool _valid;
275  std::string _invalidReason; // if _valid is false, reason why
276  size_t _hash;
277 };
278 
279 
281 
282 #endif
283 
GT_API const UT_StringHolder filename
std::vector< Attribute > Attributes
Definition: glslfxConfig.h:105
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)
HIO_API HioGlslfxConfig::Attributes GetAttributes() const
Return the attributes specified in the configuration.
#define HIO_API
Definition: api.h:23
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:207
Definition: token.h:70
std::vector< Parameter > Parameters
Definition: glslfxConfig.h:65
size_t GetHash() const
Return the computed hash value based on the string.
Definition: glslfx.h:210
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
#define HIO_GLSLFX_TOKENS
Definition: glslfx.h:31
GT_API const UT_StringHolder version
const std::string & GetFilePath() const
Get the original file name passed to the constructor.
Definition: glslfx.h:201
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:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
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:85
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.