Loading...
Searching...
No Matches
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
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
25PXR_NAMESPACE_OPEN_SCOPE
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
51TF_DECLARE_PUBLIC_TOKENS(HioGlslfxTokens, HIO_API, HIO_GLSLFX_TOKENS);
52
134{
135public:
137 HIO_API
139
141 HIO_API
143 std::string const & filePath,
144 TfToken const & technique = HioGlslfxTokens->defVal);
145
147 HIO_API
149 std::istream &is,
150 TfToken const & technique = HioGlslfxTokens->defVal);
151
153 HIO_API
154 HioGlslfxConfig::Parameters GetParameters() const;
155
157 HIO_API
158 HioGlslfxConfig::Textures GetTextures() const;
159
161 HIO_API
162 HioGlslfxConfig::Attributes GetAttributes() const;
163
165 HIO_API
167
169 HIO_API
170 bool IsValid(std::string *reason=NULL) const;
171
174
176 HIO_API
177 std::string GetSurfaceSource() const;
178
180 HIO_API
181 std::string GetDisplacementSource() const;
182
184 HIO_API
185 std::string GetVolumeSource() const;
186
188
192 HIO_API
194 std::string *errorStr) const;
195
197 HIO_API
198 std::string GetSource(const TfToken &shaderStageKey) const;
199
201 const std::string &GetFilePath() const { return _globalContext.filename; }
202
207 const std::set<std::string>& GetFiles() const { return _seenFiles; }
208
210 size_t GetHash() const { return _hash; }
211
216 HIO_API
217 static std::vector<std::string> ExtractImports(const std::string& filename);
218
219private:
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
236private:
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
257 std::string _GetLayoutAsString(const TfTokenVector &shaderStageKeys) const;
258
259private:
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
280PXR_NAMESPACE_CLOSE_SCOPE
281
282#endif
283
A class representing the config and shader source of a glslfx file.
Definition: glslfx.h:134
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 std::string GetDisplacementSource() const
Get the displacement source string.
HIO_API std::string GetVolumeSource() const
Get the volume source string.
HIO_API HioGlslfxConfig::Textures GetTextures() const
Return the textures specified in the configuration.
HIO_API HioGlslfx()
Create an invalid glslfx object.
const std::set< std::string > & GetFiles() const
Return set of all files processed for this glslfx object.
Definition: glslfx.h:207
HIO_API HioGlslfx(std::istream &is, TfToken const &technique=HioGlslfxTokens->defVal)
Create a glslfx object from a stream.
HIO_API HioGlslfxConfig::MetadataDictionary GetMetadata() const
Return the metadata specified in the configuration.
HIO_API HioGlslfxConfig::Parameters GetParameters() const
Return the parameters specified in the configuration.
HIO_API bool IsValid(std::string *reason=NULL) const
Returns true if this is a valid glslfx file.
HIO_API HioGlslfx(std::string const &filePath, TfToken const &technique=HioGlslfxTokens->defVal)
Create a glslfx object from a file.
size_t GetHash() const
Return the computed hash value based on the string.
Definition: glslfx.h:210
HIO_API std::string GetSource(const TfToken &shaderStageKey) const
Get the shader source associated with given key.
HIO_API std::string GetSurfaceSource() const
Get the surface source string.
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:201
static HIO_API std::vector< std::string > ExtractImports(const std::string &filename)
Extract imported files from the specified glslfx file.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
A map with string keys and VtValue values.
Definition: dictionary.h:43
This file defines some macros that are useful for declaring and using static TfTokens.
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.
Definition: staticTokens.h:92
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440