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  (vertexShader) \
58  (vertexShaderInjection) \
59  \
60  (surfaceShader) \
61  (displacementShader) \
62  (volumeShader) \
63  ((defVal, "default"))
64 
65 
67 
68 /// \class HioGlslfx
69 ///
70 /// A class representing the config and shader source of a glslfx file.
71 ///
72 /// a HioGlslfx object is constructed by providing the path of a file whose
73 /// contents look something like this:
74 ///
75 /// \code
76 /// -- glslfx version 0.1
77 ///
78 /// -- configuration
79 ///
80 /// {
81 ///
82 /// 'textures' : {
83 /// 'texture_1':{
84 /// 'documentation' : 'a useful texture.',
85 /// },
86 /// 'texture_2':{
87 /// 'documentation' : 'another useful texture.',
88 /// },
89 /// },
90 /// 'parameters': {
91 /// 'param_1' : {
92 /// 'default' : 1.0,
93 /// 'documentation' : 'the first parameter'
94 /// },
95 /// 'param_2' : {
96 /// 'default' : [1.0, 1.0, 1.0],
97 /// 'documentation' : 'a vec3f parameter'
98 /// },
99 /// 'param_3' : {
100 /// 'default' : 2.0
101 /// },
102 /// 'param_4' : {
103 /// 'default' : True
104 /// },
105 /// 'param_5' : {
106 /// 'default' : [1.0, 1.0, 1.0],
107 /// 'role' : 'color'
108 /// 'documentation' : 'specifies a color for use in the shader'
109 /// },
110 /// },
111 /// 'parameterOrder': ['param_1',
112 /// 'param_2',
113 /// 'param_3',
114 /// 'param_4',
115 /// 'param_5'],
116 ///
117 /// 'techniques': {
118 /// 'default': {
119 /// 'fragmentShader': {
120 /// 'source': [ 'MyFragment' ]
121 /// }
122 /// },
123 /// 'metal': {
124 /// 'fragmentShader': {
125 /// 'source': [ 'MyFragment.Metal' ]
126 /// }
127 /// }
128 /// }
129 /// }
130 ///
131 /// -- glsl MyFragment
132 ///
133 /// uniform float param_1;
134 /// uniform float param_2;
135 /// uniform float param_3;
136 /// uniform float param_4;
137 /// uniform float param_5;
138 ///
139 /// void main()
140 /// {
141 /// // ...
142 /// // glsl code which consumes the various uniforms, and perhaps sets
143 /// // gl_FragColor = someOutputColor;
144 /// // ...
145 /// }
146 /// \endcode
147 ///
149 {
150 public:
151  /// Create an invalid glslfx object
152  HIO_API
153  HioGlslfx();
154 
155  /// Create a glslfx object from a file
156  HIO_API
157  HioGlslfx(
158  std::string const & filePath,
159  TfToken const & technique = HioGlslfxTokens->defVal);
160 
161  /// Create a glslfx object from a stream
162  HIO_API
163  HioGlslfx(
164  std::istream &is,
165  TfToken const & technique = HioGlslfxTokens->defVal);
166 
167  /// Return the parameters specified in the configuration
168  HIO_API
170 
171  /// Return the textures specified in the configuration
172  HIO_API
174 
175  /// Return the attributes specified in the configuration
176  HIO_API
178 
179  /// Return the metadata specified in the configuration
180  HIO_API
182 
183  /// Returns true if this is a valid glslfx file
184  HIO_API
185  bool IsValid(std::string *reason=NULL) const;
186 
187  /// \name Access to commonly used shader sources.
188  /// @{
189 
190  /// Get the surface source string
191  HIO_API
193 
194  /// Get the displacement source string
195  HIO_API
197 
198  /// Get the volume source string
199  HIO_API
201 
202  /// @}
203 
204  /// Get the shader source associated with given key
205  HIO_API
206  std::string GetSource(const TfToken &shaderStageKey) const;
207 
208  /// Get the original file name passed to the constructor
209  const std::string &GetFilePath() const { return _globalContext.filename; }
210 
211  /// Return set of all files processed for this glslfx object.
212  /// This includes the original file given to the constructor
213  /// as well as any other files that were imported. This set
214  /// will only contain files that exist.
215  const std::set<std::string>& GetFiles() const { return _seenFiles; }
216 
217  /// Return the computed hash value based on the string
218  size_t GetHash() const { return _hash; }
219 
220  /// Extract imported files from the specified glslfx file. The returned
221  /// paths are as-authored, in the order of declaration, with possible
222  /// duplicates. This function is not recursive -- it only extracts imports
223  /// from the specified \p filename.
224  HIO_API
225  static std::vector<std::string> ExtractImports(const std::string& filename);
226 
227 private:
228  class _ParseContext {
229  public:
230  _ParseContext() { }
231 
232  _ParseContext(std::string const & filePath) :
233  filename(filePath), lineNo(0), version(-1.0) { }
234 
236  int lineNo;
237  double version;
238  std::string currentLine;
239  std::string currentSectionType;
240  std::string currentSectionId;
241  std::vector<std::string> imports;
242  };
243 
244 private:
245  bool _ProcessFile(std::string const & filePath,
246  _ParseContext & context);
247  bool _ProcessInput(std::istream * input,
248  _ParseContext & context);
249  bool _ProcessImport(_ParseContext & context);
250  bool _ParseSectionLine(_ParseContext & context);
251  bool _ParseGLSLSectionLine(std::vector<std::string> const &tokens,
252  _ParseContext & context);
253  bool _ParseVersionLine(std::vector<std::string> const &tokens,
254  _ParseContext & context);
255  bool _ParseConfigurationLine(_ParseContext & context);
256  bool _ComposeConfiguration(std::string *reason);
257  std::string _GetSource(const TfToken &shaderStageKey) const;
258 
259 private:
260  _ParseContext _globalContext;
261 
262  typedef std::map<std::string, std::string> _SourceMap;
263 
264  _SourceMap _sourceMap;
265  _SourceMap _configMap;
266  std::vector<std::string> _configOrder;
267  std::set<std::string> _seenFiles;
268 
269  std::unique_ptr<HioGlslfxConfig> _config;
270 
271  TfToken _technique;
272 
273  bool _valid;
274  std::string _invalidReason; // if _valid is false, reason why
275  size_t _hash;
276 };
277 
278 
280 
281 #endif
282 
GLenum GLenum GLenum input
Definition: glew.h:13879
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)
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:215
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:218
#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:209
GLsizei const GLchar *const * string
Definition: glew.h:1844
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:1346
#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.