VVISF & VVGL
ISFDoc.hpp
1 #ifndef ISFDoc_hpp
2 #define ISFDoc_hpp
3 
4 #include <vector>
5 #include "VVISF_Base.hpp"
6 #include "ISFAttr.hpp"
7 
8 
9 
10 
11 namespace VVISF
12 {
13 
14 
15 
16 
17 using namespace std;
18 
19 class ISFScene;
20 
21 
22 
23 
35 class VVISF_EXPORT ISFDoc {
36  private:
37  recursive_mutex _propLock;
38 
39  string *_path = nullptr; // full path to the loaded file
40  string *_name = nullptr; // just the file name (including its extension)
41  string *_description = nullptr; // description of whatever the file does
42  string *_credit = nullptr; // credit
43  string *_vsn = nullptr;
45  bool _throwExcept = true;
46 
47  vector<string> _categories; // array of strings of the category names this doc should be listed under
48  vector<ISFAttrRef> _inputs; // array of ISFAttrRef instances for the various inputs
49  vector<ISFAttrRef> _imageInputs; // array of ISFAttrRef instances for the image inputs (the image inputs are stored in two arrays).
50  vector<ISFAttrRef> _audioInputs; // array of ISFAttrRef instances for the audio inputs
51  vector<ISFAttrRef> _imageImports; // array of ISFAttrRef instances that describe imported images. attrib's 'attribName' is the name of the sampler, attrib's 'description' is the path to the file.
52 
53  //bool bufferRequiresEval = false; // NO by default, set to YES during file open if any of the buffers require evaluation (faster than checking every single buffer every pass)
54  vector<ISFPassTargetRef> _persistentPassTargets;
55  vector<ISFPassTargetRef> _tempPassTargets;
56  vector<string> _renderPasses;
57 
58  string *_jsonSourceString = nullptr; // the JSON string from the source *including the comments and any linebreaks before/after it*
59  string *_jsonString = nullptr; // the JSON string copied from the source- doesn't include any comments before/after it
60  string *_vertShaderSource = nullptr; // the raw vert shader source before being find-and-replaced
61  string *_fragShaderSource = nullptr; // the raw frag shader source before being find-and-replaced
62 
63  ISFScene *_parentScene = nullptr; // nil by default, weak ref to the scene that "owns" me. only non-nil when an ISFScene is using the doc to render.
64 
65  public:
66 
70 
73 
79  ISFDoc(const string & inPath, ISFScene * inParentScene=nullptr, const bool & inThrowExcept=true) noexcept(false);
80 
82  /*
83  \param inFSContents A string containing the fragment shader portion of the ISF file. The JSON blob that defines the ISF file must be contained in here.
84  \param inVSContents A string containing the vertex shader portion of the ISF file. If you don't have a vertex shader to pass, VVISF defines a static string "ISFVertPassthru_GL2", which should work as a "passthru" vertex shader for most purposes.
85  \param inImportsDir A string containing the path to the directory that will contain any related media files (used for IMPORT/etc).
86  \param inParentScene The scene that will be used to render this ISFDoc, or null if no scene is to be used.
87  \param inThrowExcept Whether or not the ISFDoc should throw any exceptions if it encounters errors parsing anything.
88  Throws an ISFErr if there is a problem of some sort parsing the JSON blob from the frag shader string.
89  */
90  ISFDoc(const string & inFSContents, const string & inVSContents, const string & importsDir, ISFScene * inParentScene=nullptr, const bool & inThrowExcept=true);
91 
93 
94 
95  ~ISFDoc();
96 
97 
101 
104  string path() const { return (_path==nullptr) ? string("") : string(*_path); }
106  string name() const { return (_name==nullptr) ? string("") : string(*_name); }
108  string description() const { return (_description==nullptr) ? string("") : string(*_description); }
110  string credit() const { return (_credit==nullptr) ? string("") : string(*_credit); }
112  string vsn() const { return (_vsn==nullptr) ? string("") : string(*_vsn); }
114  ISFFileType type() const { return _type; }
116  vector<string> & categories() { return _categories; }
117 
119 
120 
124 
127  vector<ISFAttrRef> & inputs() { return _inputs; }
129  vector<ISFAttrRef> & imageInputs() { return _imageInputs; }
131  vector<ISFAttrRef> & audioInputs() { return _audioInputs; }
133  vector<ISFAttrRef> & imageImports() { return _imageImports; }
135  vector<ISFAttrRef> inputsOfType(const ISFValType & inInputType);
137  ISFAttrRef input(const string & inAttrName);
138 
140 
141 
145 
148  vector<ISFPassTargetRef> persistentPassTargets() const { return _persistentPassTargets; }
150  vector<ISFPassTargetRef> tempPassTargets() const { return _tempPassTargets; }
152  vector<string> & renderPasses() { return _renderPasses; }
154  const VVGL::GLBufferRef getBufferForKey(const string & n);
156  const VVGL::GLBufferRef getPersistentBufferForKey(const string & n);
158  const VVGL::GLBufferRef getTempBufferForKey(const string & n);
160  const ISFPassTargetRef passTargetForKey(const string & n);
162  const ISFPassTargetRef persistentPassTargetForKey(const string & n);
164  const ISFPassTargetRef tempPassTargetForKey(const string & n);
165 
167 
168 
172 
175  string * jsonSourceString() const { return _jsonSourceString; }
177  string * jsonString() const { return _jsonString; }
179  string * vertShaderSource() const { return _vertShaderSource; }
181  string * fragShaderSource() const { return _fragShaderSource; }
183  void jsonSourceString(string & outStr);
184 
186 
187  void setParentScene(ISFScene * n) { _parentScene=n; }
188  ISFScene * parentScene() { return _parentScene; }
189 
190  // returns a string describing the type of the expected texture samplers ("2" for 2D, "R" for Rect, "C" for Cube). save this, if it changes in a later pass the shader source must be generated again.
191  string generateTextureTypeString();
199  bool generateShaderSource(string * outFragSrc, string * outVertSrc, VVGL::GLVersion & inGLVers, const bool & inVarsAsUBO=false);
200  // this method must be called before rendering (passes/etc may have expressions that require the render dims to be evaluated)
201  void evalBufferDimensionsWithRenderSize(const VVGL::Size & inSize);
202 
203  VVISF_EXPORT friend ostream & operator<<(ostream & os, const ISFDoc & n);
204 
205  protected:
206  // used so we can have two constructors without duplicating code
207  void _initWithRawFragShaderString(const string & inRawFile);
208  // returns a true if successful. populates a string with variable declarations for a frag shader
209  bool _assembleShaderSource_VarDeclarations(string * outVSString, string * outFSString, VVGL::GLVersion & inGLVers, const bool & inVarsAsUBO=false);
210  // returns a true if successful. populates a map with string/value pairs that will be used to evaluate variable names in strings
211  bool _assembleSubstitutionMap(map<string,double*> * outMap);
212 };
213 
214 
215 
216 
218 
225 inline ISFDocRef CreateISFDocRef(const string & inPath, ISFScene * inParentScene=nullptr, const bool & inThrowExcept=true) noexcept(false) { return make_shared<ISFDoc>(inPath,inParentScene,inThrowExcept); }
226 
228 
236 inline ISFDocRef CreateISFDocRefWith(const string & inFSContents, const string & inImportsDir=string("/"), const string & inVSContents=string(ISFVertPassthru_GL2), ISFScene * inParentScene=nullptr, const bool & inThrowExcept=true) { return make_shared<ISFDoc>(inFSContents,inVSContents,inImportsDir,inParentScene,inThrowExcept); }
237 
238 
239 
240 }
241 
242 
243 
244 
245 #endif /* ISFDoc_hpp */
Subclass of GLScene- the primary interface for rendering and interacting with an ISF file...
Definition: ISFScene.hpp:28
vector< ISFPassTargetRef > tempPassTargets() const
Returns a vector of ISFPassTargetRef instances describing every pass that doesn&#39;t have a persistent b...
Definition: ISFDoc.hpp:150
The file is a "source"- it generates images.
Definition: VVISF_Base.hpp:72
vector< string > & renderPasses()
Returns a vector of std::string instances describing the names of the render passes, in order. If the names were not specified properly in the JSON blob, this array will be incomplete or inaccurate and rendering won&#39;t work!
Definition: ISFDoc.hpp:152
STL namespace.
string * jsonString() const
Returns the JSON string copied from the source- doesn&#39;t include any comments before/after it...
Definition: ISFDoc.hpp:177
string * vertShaderSource() const
Returns the raw vert shader source before being find-and-replaced.
Definition: ISFDoc.hpp:179
shared_ptr< ISFDoc > ISFDocRef
ISFDocRef is a shared pointer around an ISFDoc instance.
Definition: VVISF_Base.hpp:51
Definition: ISFAttr.hpp:12
Basic struct for 2D size.
Definition: VVGL_Geom.hpp:46
vector< ISFAttrRef > & imageImports()
Returns a vector of ISFAttrRef instances describing only the receiver&#39;s audioFFT inputs.
Definition: ISFDoc.hpp:133
string credit() const
Returns the receiver&#39;s "credit" string, as defined in its JSON blob ("CREDIT").
Definition: ISFDoc.hpp:110
ISFValType
Enumerates the different kinds of ISF values.
Definition: ISFVal.hpp:25
ISFFileType type() const
Returns the receiver&#39;s file type.
Definition: ISFDoc.hpp:114
string name() const
Returns the name of the receiver&#39;s ISF file (the file name, minus the extension). ...
Definition: ISFDoc.hpp:106
Describes an "ISF file"- requires an actual file on disk which is parsed. Capable of generating GLSL ...
Definition: ISFDoc.hpp:35
ISFDocRef CreateISFDocRef(const string &inPath, ISFScene *inParentScene=nullptr, const bool &inThrowExcept=true) noexcept(false)
Constructs an ISFDoc instance from a passed file on disk.
Definition: ISFDoc.hpp:225
vector< ISFAttrRef > & audioInputs()
Returns a vector of ISFAttrRef instances describing only the receiver&#39;s audio inputs.
Definition: ISFDoc.hpp:131
string * fragShaderSource() const
Returns the raw frag shader source before being find-and-replaced.
Definition: ISFDoc.hpp:181
ISFDocRef CreateISFDocRefWith(const string &inFSContents, const string &inImportsDir=string("/"), const string &inVSContents=string(ISFVertPassthru_GL2), ISFScene *inParentScene=nullptr, const bool &inThrowExcept=true)
Constructs an ISFDoc instance from shader strings.
Definition: ISFDoc.hpp:236
string vsn() const
Returns the receiver&#39;s "vsn" string, as defined in its JSON blob ("VSN")
Definition: ISFDoc.hpp:112
GLVersion
This enum is used to describe the GL environment of a GL context, which is determined at runtime...
Definition: VVGL_Base.hpp:94
string description() const
Returns the receiver&#39;s "description" string, as defined in its JSON blob ("DESCRIPTION").
Definition: ISFDoc.hpp:108
vector< ISFAttrRef > & inputs()
Returns a vector of ISFAttrRef instances describing all of the receiver&#39;s inputs. ...
Definition: ISFDoc.hpp:127
string path() const
Returns the path of the ISF file for the receiver. This is probably the path to the frag shader...
Definition: ISFDoc.hpp:104
string * jsonSourceString() const
Returns the JSON string from the source including the comments and any linebreaks before/after it ...
Definition: ISFDoc.hpp:175
ISFFileType
Definition: VVISF_Base.hpp:70
vector< ISFAttrRef > & imageInputs()
Returns a vector of ISFAttrRef instances describing only the receiver&#39;s image inputs.
Definition: ISFDoc.hpp:129
vector< string > & categories()
Returns a vector containing strings listing the receiver&#39;s categories.
Definition: ISFDoc.hpp:116
vector< ISFPassTargetRef > persistentPassTargets() const
Returns a vector of ISFPassTargetRef instances describing every pass that has a persistent buffer...
Definition: ISFDoc.hpp:148