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 
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  (vertexShader) \
58  (vertexShaderInjection) \
59  \
60  (surfaceShader) \
61  (displacementShader) \
62  (volumeShader) \
63  ((defVal, "default"))
64 
65 
66 TF_DECLARE_PUBLIC_TOKENS(HioGlslfxTokens, HIO_API, HIO_GLSLFX_TOKENS);
67 
149 {
150 public:
152  HIO_API
153  HioGlslfx();
154 
156  HIO_API
157  HioGlslfx(
158  std::string const & filePath,
159  TfToken const & technique = HioGlslfxTokens->defVal);
160 
162  HIO_API
163  HioGlslfx(
164  std::istream &is,
165  TfToken const & technique = HioGlslfxTokens->defVal);
166 
168  HIO_API
169  HioGlslfxConfig::Parameters GetParameters() const;
170 
172  HIO_API
173  HioGlslfxConfig::Textures GetTextures() const;
174 
176  HIO_API
177  HioGlslfxConfig::Attributes GetAttributes() const;
178 
180  HIO_API
182 
184  HIO_API
185  bool IsValid(std::string *reason=NULL) const;
186 
189 
191  HIO_API
192  std::string GetSurfaceSource() const;
193 
195  HIO_API
196  std::string GetDisplacementSource() const;
197 
199  HIO_API
200  std::string GetVolumeSource() const;
201 
203 
205  HIO_API
206  std::string GetSource(const TfToken &shaderStageKey) const;
207 
209  const std::string &GetFilePath() const { return _globalContext.filename; }
210 
215  const std::set<std::string>& GetFiles() const { return _seenFiles; }
216 
218  size_t GetHash() const { return _hash; }
219 
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 
235  std::string filename;
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 
279 PXR_NAMESPACE_CLOSE_SCOPE
280 
281 #endif
282 
HIO_API HioGlslfxConfig::MetadataDictionary GetMetadata() const
Return the metadata specified in the configuration.
HIO_API std::string GetDisplacementSource() const
Get the displacement source string.
A map with string keys and VtValue values.
Definition: dictionary.h:63
HIO_API bool IsValid(std::string *reason=NULL) const
Returns true if this is a valid glslfx file.
HIO_API HioGlslfxConfig::Textures GetTextures() const
Return the textures specified in the configuration.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
HIO_API std::string GetSurfaceSource() const
Get the surface source string.
size_t GetHash() const
Return the computed hash value based on the string.
Definition: glslfx.h:218
HIO_API HioGlslfxConfig::Attributes GetAttributes() const
Return the attributes specified in the configuration.
const std::string & GetFilePath() const
Get the original file name passed to the constructor.
Definition: glslfx.h:209
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.
Definition: staticTokens.h:118
HIO_API HioGlslfxConfig::Parameters GetParameters() const
Return the parameters specified in the configuration.
HIO_API std::string GetVolumeSource() const
Get the volume source string.
HIO_API HioGlslfx()
Create an invalid glslfx object.
A class representing the config and shader source of a glslfx file.
Definition: glslfx.h:148
static HIO_API std::vector< std::string > ExtractImports(const std::string &filename)
Extract imported files from the specified glslfx file.
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:215
HIO_API std::string GetSource(const TfToken &shaderStageKey) const
Get the shader source associated with given key.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...