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 
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 
42 PXR_NAMESPACE_OPEN_SCOPE
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 
68 TF_DECLARE_PUBLIC_TOKENS(HioGlslfxTokens, HIO_API, HIO_GLSLFX_TOKENS);
69 
151 {
152 public:
154  HIO_API
155  HioGlslfx();
156 
158  HIO_API
159  HioGlslfx(
160  std::string const & filePath,
161  TfToken const & technique = HioGlslfxTokens->defVal);
162 
164  HIO_API
165  HioGlslfx(
166  std::istream &is,
167  TfToken const & technique = HioGlslfxTokens->defVal);
168 
170  HIO_API
171  HioGlslfxConfig::Parameters GetParameters() const;
172 
174  HIO_API
175  HioGlslfxConfig::Textures GetTextures() const;
176 
178  HIO_API
179  HioGlslfxConfig::Attributes GetAttributes() const;
180 
182  HIO_API
184 
186  HIO_API
187  bool IsValid(std::string *reason=NULL) const;
188 
191 
193  HIO_API
194  std::string GetSurfaceSource() const;
195 
197  HIO_API
198  std::string GetDisplacementSource() const;
199 
201  HIO_API
202  std::string GetVolumeSource() const;
203 
205 
209  HIO_API
210  VtDictionary GetLayoutAsDictionary(const TfTokenVector &shaderStageKeys,
211  std::string *errorStr) const;
212 
214  HIO_API
215  std::string GetSource(const TfToken &shaderStageKey) const;
216 
218  const std::string &GetFilePath() const { return _globalContext.filename; }
219 
224  const std::set<std::string>& GetFiles() const { return _seenFiles; }
225 
227  size_t GetHash() const { return _hash; }
228 
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 
244  std::string filename;
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 
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 
297 PXR_NAMESPACE_CLOSE_SCOPE
298 
299 #endif
300 
const std::string & GetFilePath() const
Get the original file name passed to the constructor.
Definition: glslfx.h:218
HIO_API HioGlslfxConfig::MetadataDictionary GetMetadata() const
Return the metadata specified in the configuration.
HIO_API VtDictionary GetLayoutAsDictionary(const TfTokenVector &shaderStageKeys, std::string *errorStr) const
Get the layout config as a VtDictionary parsed from the JSON layout config corresponding to the shade...
HIO_API HioGlslfxConfig::Parameters GetParameters() const
Return the parameters specified in the configuration.
A map with string keys and VtValue values.
Definition: dictionary.h:63
HIO_API std::string GetDisplacementSource() const
Get the displacement source string.
HIO_API std::string GetVolumeSource() const
Get the volume source string.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.
Definition: staticTokens.h:118
HIO_API std::string GetSurfaceSource() const
Get the surface source string.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
HIO_API std::string GetSource(const TfToken &shaderStageKey) const
Get the shader source associated with given key.
HIO_API HioGlslfx()
Create an invalid glslfx object.
A class representing the config and shader source of a glslfx file.
Definition: glslfx.h:150
HIO_API bool IsValid(std::string *reason=NULL) const
Returns true if this is a valid glslfx file.
static HIO_API std::vector< std::string > ExtractImports(const std::string &filename)
Extract imported files from the specified glslfx file.
HIO_API HioGlslfxConfig::Textures GetTextures() const
Return the textures specified in the configuration.
This file defines some macros that are useful for declaring and using static TfTokens.
const std::set< std::string > & GetFiles() const
Return set of all files processed for this glslfx object.
Definition: glslfx.h:224
HIO_API HioGlslfxConfig::Attributes GetAttributes() const
Return the attributes specified in the configuration.
size_t GetHash() const
Return the computed hash value based on the string.
Definition: glslfx.h:227
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...