Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
shadersourcecomposer.h
Go to the documentation of this file.
1 
18 #ifndef ION_GFXUTILS_SHADERSOURCECOMPOSER_H_
19 #define ION_GFXUTILS_SHADERSOURCECOMPOSER_H_
20 
21 #include <chrono> // NOLINT
22 #include <functional>
23 #include <memory>
24 #include <string>
25 #include <vector>
26 
27 #include "ion/base/referent.h"
28 
29 namespace ion {
30 namespace gfxutils {
31 
54 
59 
60 class ION_API ShaderSourceComposer : public base::Referent {
61  public:
63  virtual const std::string GetSource() = 0;
66  virtual bool DependsOn(const std::string& dependency) const = 0;
68  virtual const std::string GetDependencySource(
69  const std::string& dependency) const = 0;
72  virtual bool SetDependencySource(const std::string& dependency,
73  const std::string& source) = 0;
77  virtual const std::string GetDependencyName(unsigned int id) const = 0;
80  virtual const std::vector<std::string> GetDependencyNames() const = 0;
85  virtual const std::vector<std::string> GetChangedDependencies() = 0;
86 
87  protected:
91  ~ShaderSourceComposer() override;
92 };
94 
96 
101 
102 class ION_API StringComposer : public ShaderSourceComposer {
103  public:
104  explicit StringComposer(const std::string& dependency_name,
105  const std::string& source)
106  : dependency_(dependency_name),
107  source_(source) {}
108 
109  const std::string GetSource() override { return source_; }
110  bool DependsOn(const std::string& dependency) const override {
111  return dependency == dependency_;
112  }
113  const std::string GetDependencySource(
114  const std::string& dependency) const override {
115  return dependency == dependency_ ? source_ : std::string();
116  }
117  bool SetDependencySource(const std::string& dependency,
118  const std::string& source) override {
119  if (dependency == dependency_) {
120  source_ = source;
121  return true;
122  } else {
123  return false;
124  }
125  }
126  const std::string GetDependencyName(unsigned int id) const override {
127  return dependency_;
128  }
129  const std::vector<std::string> GetDependencyNames() const override {
130  std::vector<std::string> names;
131  names.push_back(dependency_);
132  return names;
133  }
134  const std::vector<std::string> GetChangedDependencies() override {
135  return std::vector<std::string>();
136  }
137 
138  protected:
140  ~StringComposer() override {}
141 
142  private:
143  std::string dependency_;
144  std::string source_;
145 };
147 
149 
155 
156 class ION_API FilterComposer : public ShaderSourceComposer {
157  public:
158  typedef std::function<std::string(const std::string)> StringFilter;
160  const StringFilter& transformer)
161  : base_(base),
162  transformer_(transformer) {}
163  const std::string GetSource() override {
164  return transformer_(base_->GetSource());
165  }
166  bool DependsOn(const std::string& dependency) const override {
167  return base_->DependsOn(dependency);
168  }
169  const std::string GetDependencySource(
170  const std::string& dependency) const override {
171  return base_->GetDependencySource(dependency);
172  }
173  bool SetDependencySource(const std::string& dependency,
174  const std::string& source) override {
175  return base_->SetDependencySource(dependency, source);
176  }
177  const std::string GetDependencyName(unsigned int id) const override {
178  return base_->GetDependencyName(id);
179  }
180  const std::vector<std::string> GetDependencyNames() const override {
181  return base_->GetDependencyNames();
182  }
183  const std::vector<std::string> GetChangedDependencies() override {
184  return base_->GetChangedDependencies();
185  }
186 
187  private:
189  StringFilter transformer_;
190 };
192 
194 
205 
206 class ION_API IncludeComposer : public ShaderSourceComposer {
207  public:
209  typedef std::function<const std::string(const std::string& name)>
213  typedef std::function<bool(const std::string& name, // NOLINT
214  const std::string& source)> SourceSaver;
216  typedef std::function<bool(const std::string& filename,
217  std::chrono::system_clock::time_point* timestamp)>
219 
224  IncludeComposer(const std::string& filename,
225  const SourceLoader& source_loader,
226  const SourceSaver& source_saver,
227  const SourceModificationTime& source_time,
228  bool insert_line_directives);
231  void SetBasePath(const std::string& path);
232 
233  const std::string GetSource() override;
234  bool DependsOn(const std::string& dependency) const override;
235  const std::string GetDependencySource(
236  const std::string& dependency) const override;
237  bool SetDependencySource(const std::string& dependency,
238  const std::string& source) override;
239  const std::string GetDependencyName(unsigned int id) const override;
240  const std::vector<std::string> GetDependencyNames() const override;
241  const std::vector<std::string> GetChangedDependencies() override;
242 
243  protected:
245  ~IncludeComposer() override;
246 
247  private:
248  class IncludeComposerHelper;
249  std::unique_ptr<IncludeComposerHelper> helper_;
250 };
252 
254 
260 
261 class ION_API ZipAssetComposer : public IncludeComposer {
262  public:
263  ZipAssetComposer(const std::string& filename, bool insert_line_directives);
264 
265  protected:
267  ~ZipAssetComposer() override;
268 };
270 
271 } // namespace gfxutils
272 } // namespace ion
273 
274 #endif // ION_GFXUTILS_SHADERSOURCECOMPOSER_H_
std::function< bool(const std::string &name, const std::string &source)> SourceSaver
A function that saves a string source given a filename.
const std::string GetDependencySource(const std::string &dependency) const override
Returns the source of the passed dependency.
const std::vector< std::string > GetDependencyNames() const override
Returns a vector containing all names that this composer depends on, or an empty vector if there are ...
Loads a shader source from zip asset resources that may $input other zip assets.
const std::vector< std::string > GetDependencyNames() const override
Returns a vector containing all names that this composer depends on, or an empty vector if there are ...
bool DependsOn(const std::string &dependency) const override
Returns whether this composer depends on the named dependency, which might be a filename or some othe...
FilterComposer(const ShaderSourceComposerPtr &base, const StringFilter &transformer)
std::function< bool(const std::string &filename, std::chrono::system_clock::time_point *timestamp)> SourceModificationTime
A function that returns the last time the source in filename was modified.
Simple composer that just returns the string passed to its constructor.
bool SetDependencySource(const std::string &dependency, const std::string &source) override
Requests that the composer set the source of the dependency.
base::ReferentPtr< FilterComposer >::Type FilterComposerPtr
const std::string GetSource() override
Returns the source string of a shader.
Thread-safe abstract base class.
Definition: referent.h:49
StringComposer(const std::string &dependency_name, const std::string &source)
base::ReferentPtr< StringComposer >::Type StringComposerPtr
const std::string GetSource() override
Returns the source string of a shader.
base::ReferentPtr< ZipAssetComposer >::Type ZipAssetComposerPtr
std::string name
Definition: printer.cc:324
const std::string GetDependencyName(unsigned int id) const override
Returns the name of a dependency identified by the passed id.
~StringComposer() override
The destructor is protected since this is derived from base::Referent.
std::function< const std::string(const std::string &name)> SourceLoader
A function that returns a string source given a filename.
std::function< std::string(const std::string)> StringFilter
bool SetDependencySource(const std::string &dependency, const std::string &source) override
Requests that the composer set the source of the dependency.
const std::string GetDependencySource(const std::string &dependency) const override
Returns the source of the passed dependency.
Applies a fixed transformation to the output of another composer.
bool DependsOn(const std::string &dependency) const override
Returns whether this composer depends on the named dependency, which might be a filename or some othe...
Loads a shader source from a resource that may include other resources using the special directive '$...
const std::vector< std::string > GetChangedDependencies() override
Determines if any dependencies have changed (e.g., if a file has changed on disk since the last call ...
const std::string GetDependencyName(unsigned int id) const override
Returns the name of a dependency identified by the passed id.
A ShaderSourceComposer is a generic interface for constructing a shader source string.
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
Definition: sharedptr.h:60
base::ReferentPtr< ShaderSourceComposer >::Type ShaderSourceComposerPtr
const std::vector< std::string > GetChangedDependencies() override
Determines if any dependencies have changed (e.g., if a file has changed on disk since the last call ...
base::ReferentPtr< IncludeComposer >::Type IncludeComposerPtr