Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
uniformholder.h
Go to the documentation of this file.
1 
18 #ifndef ION_GFX_UNIFORMHOLDER_H_
19 #define ION_GFX_UNIFORMHOLDER_H_
20 
21 #include <string>
22 
23 #include "ion/base/allocatable.h"
24 #include "ion/base/invalid.h"
26 #include "ion/gfx/uniform.h"
27 
28 namespace ion {
29 namespace gfx {
30 
39 class ION_API UniformHolder {
40  public:
46  size_t AddUniform(const Uniform& uniform) {
47  if (uniform.IsValid()) {
48  uniforms_.push_back(uniform);
49  return uniforms_.size() - 1U;
50  } else {
51  return base::kInvalidIndex;
52  }
53  }
54 
57  bool ReplaceUniform(size_t index, const Uniform& uniform) {
58  if (uniform.IsValid() && index < uniforms_.size()) {
59  uniforms_[index] = uniform;
60  return true;
61  } else {
62  return false;
63  }
64  }
65 
69  bool RemoveUniformByName(const std::string& name) {
70  const size_t index = GetUniformIndex(name);
71  if (index == base::kInvalidIndex)
72  return false;
73  uniforms_.erase(uniforms_.begin() + index);
74  return true;
75  }
76 
78  void ClearUniforms() { uniforms_.clear(); }
79 
81  const base::AllocVector<Uniform>& GetUniforms() const { return uniforms_; }
82 
86  template <typename T> bool SetUniformValue(size_t index, const T& value) {
87  if (index < uniforms_.size())
88  return uniforms_[index].SetValue(value);
89  return false;
90  }
91 
95  template <typename T> bool SetUniformValueAt(size_t index,
96  size_t array_index,
97  const T& value) {
98  if (index < uniforms_.size())
99  return uniforms_[index].SetValueAt(array_index, value);
100  return false;
101  }
102 
108  size_t GetUniformIndex(const std::string& name) const;
109 
113  template <typename T> bool SetUniformByName(const std::string& name,
114  const T& value) {
115  const size_t index = GetUniformIndex(name);
116  return index == base::kInvalidIndex ? false :
117  SetUniformValue<T>(index, value);
118  }
119 
125  template <typename T> bool SetUniformByNameAt(const std::string& name,
126  size_t array_index,
127  const T& value) {
128  const size_t index = GetUniformIndex(name);
129  return (index == base::kInvalidIndex || index >= uniforms_.size()) ?
130  false :
131  uniforms_[index].SetValueAt(array_index, value);
132  }
133 
137  void Enable(bool enable) { is_enabled_ = enable; }
138  bool IsEnabled() const { return is_enabled_; }
139 
140  protected:
143  explicit UniformHolder(const base::AllocatorPtr& alloc);
144 
147  virtual ~UniformHolder();
148 
149  private:
150  bool is_enabled_;
151  base::AllocVector<Uniform> uniforms_;
152 };
153 
154 } // namespace gfx
155 } // namespace ion
156 
157 #endif // ION_GFX_UNIFORMHOLDER_H_
size_t AddUniform(const Uniform &uniform)
Adds a uniform to this and returns an index that can be used to refer to the uniform.
Definition: uniformholder.h:46
const size_t kInvalidIndex
kInvalidIndex is a size_t value that is very unlikely to be a valid index.
Definition: invalid.cc:23
A UniformHolder is a base class for an object that holds Uniforms.
Definition: uniformholder.h:39
double value
bool SetUniformValueAt(size_t index, size_t array_index, const T &value)
Sets the value of the array uniform at an index if the index is valid.
Definition: uniformholder.h:95
A Uniform instance represents a uniform shader argument.
Definition: uniform.h:76
const base::AllocVector< Uniform > & GetUniforms() const
Gets the vector of uniforms.
Definition: uniformholder.h:81
void Enable(bool enable)
Enables or disables the UniformHolder.
bool SetUniformByNameAt(const std::string &name, size_t array_index, const T &value)
Convenience function to set the value of an element of an array uniform designated by name...
bool ReplaceUniform(size_t index, const Uniform &uniform)
Replaces the uniform at an index with the passed value, if the index is valid.
Definition: uniformholder.h:57
std::string name
Definition: printer.cc:324
void ClearUniforms()
Clears the vector of uniforms in this.
Definition: uniformholder.h:78
bool SetUniformByName(const std::string &name, const T &value)
Convenience function to set the value of a uniform specified by name.
bool SetUniformValue(size_t index, const T &value)
Sets the value of the uniform at an index if the index is valid.
Definition: uniformholder.h:86
bool RemoveUniformByName(const std::string &name)
Removes the uniform with the passed name if it exists.
Definition: uniformholder.h:69
bool IsValid() const
Returns true if this is a valid instance created by a ShaderInputRegistry.
Definition: shaderinput.h:59
This class can be used in place of std::vector to allow an Ion Allocator to be used for memory alloca...
Definition: allocvector.h:50