Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
shaderinput.h
Go to the documentation of this file.
1 
18 #ifndef ION_GFX_SHADERINPUT_H_
19 #define ION_GFX_SHADERINPUT_H_
20 
21 #include <cstring> // For size_t.
22 
23 #include "base/integral_types.h"
24 #include "ion/base/allocator.h"
25 
26 namespace ion {
27 namespace gfx {
28 
29 class ShaderInputRegistry;
30 
32  public:
35  enum Tag {
38  };
39 
42 
43  protected:
45  static uint64 GetNewStamp();
46 };
47 
49 template <typename ValueHolderType, typename ValueEnumType>
50 class ShaderInput : public ShaderInputBase {
51  public:
52  typedef ValueHolderType HolderType;
53  typedef ValueEnumType ValueType;
54 
56 
59  bool IsValid() const { return registry_ != NULL; }
60 
63  const ShaderInputRegistry& GetRegistry() const { return *registry_; }
64 
67  size_t GetIndexInRegistry() const { return index_in_registry_; }
68 
71  size_t GetRegistryId() const { return registry_id_; }
72 
76  size_t GetArrayIndex() const { return array_index_; }
77 
80  ValueType GetType() const { return type_; }
81 
85  template <typename T> const T& GetValue() const {
86  return value_.template Get<T>();
87  }
88 
93  template <typename T> const T& GetValueAt(size_t i) const {
94  return value_.template GetValueAt<T>(i);
95  }
96 
99  size_t GetCount() const {
100  return value_.GetCount();
101  }
102 
105  template <typename T> bool Is() const {
106  return value_.template Is<T>();
107  }
108 
111  template <typename T> bool IsArrayOf() const {
112  return value_.template IsArrayOf<T>();
113  }
114 
118  template <typename T> bool SetValue(const T& value) {
119  if (value_.template IsAssignableTo<T>()) {
120  SetNewStamp();
121  value_.Set(value);
122  return true;
123  } else {
124  return false;
125  }
126  }
127 
131  template <typename T> bool SetValueAt(size_t i, const T& value) {
132  if (value_.template ElementsAssignableTo<T>()) {
133  SetNewStamp();
134  value_.SetValueAt(i, value);
135  return true;
136  } else {
137  return false;
138  }
139  }
140 
145  uint64 GetStamp() const { return stamp_; }
146 
147  protected:
152  : ShaderInputBase(),
153  registry_(NULL),
154  index_in_registry_(0),
155  registry_id_(0),
156  type_(static_cast<ValueType>(0)),
157  stamp_(0U),
158  array_index_(0) {}
159 
161  void SetNewStamp() { stamp_ = GetNewStamp(); }
162 
169  template <typename T>
170  void Init(const ShaderInputRegistry& registry, size_t registry_id,
171  size_t index_in_registry, size_t array_index, ValueType type,
172  const T& value) {
173  registry_ = &registry;
174  registry_id_ = registry_id;
175  index_in_registry_ = index_in_registry;
176  array_index_ = array_index;
177  type_ = type;
178  value_.Set(value);
179  SetNewStamp();
180  }
181 
188  template <typename T>
189  void InitArray(const ShaderInputRegistry& registry, size_t registry_id,
190  size_t index_in_registry, size_t array_index, ValueType type,
191  const T* values, size_t count,
192  const base::AllocatorPtr& allocator) {
193  registry_ = &registry;
194  registry_id_ = registry_id;
195  index_in_registry_ = index_in_registry;
196  array_index_ = array_index;
197  type_ = type;
198  value_.template InitArray<T>(allocator, count);
199  if (values) {
200  for (size_t i = 0; i < count; ++i)
201  value_.SetValueAt(i, values[i]);
202  }
203  SetNewStamp();
204  }
205 
208  return value_.GetArrayAllocator();
209  }
210 
211  private:
213  const ShaderInputRegistry* registry_;
214  size_t index_in_registry_;
215  size_t registry_id_;
216  ValueType type_;
217  ValueHolderType value_;
219  uint64 stamp_;
221  size_t array_index_;
222 
223  friend class ShaderInputRegistry;
224 };
225 
226 } // namespace gfx
227 } // namespace ion
228 
229 #endif // ION_GFX_SHADERINPUT_H_
bool IsArrayOf() const
If this instance contains an array of values of type T, this returns true, otherwise it returns false...
Definition: shaderinput.h:111
size_t GetRegistryId() const
Returns the id of the owning registry.
Definition: shaderinput.h:71
bool SetValue(const T &value)
If this instance contains a value of type T, this changes it to the new value.
Definition: shaderinput.h:118
std::string type
Definition: printer.cc:353
ValueEnumType ValueType
Definition: shaderinput.h:53
double value
const ShaderInputRegistry & GetRegistry() const
Returns the ShaderInputRegistry the shader input is defined in.
Definition: shaderinput.h:63
Tag
This is only used to determine the type of a ShaderInputRegistry::Spec since Attributes and Uniforms ...
Definition: shaderinput.h:35
size_t GetArrayIndex() const
Returns the array index of this input; by default this is 0.
Definition: shaderinput.h:76
void SetNewStamp()
Assigns a new stamp to this Input.
Definition: shaderinput.h:161
bool Is() const
If this instance contains a value of type T, this returns true, otherwise it returns false...
Definition: shaderinput.h:105
void InitArray(const ShaderInputRegistry &registry, size_t registry_id, size_t index_in_registry, size_t array_index, ValueType type, const T *values, size_t count, const base::AllocatorPtr &allocator)
Initializes the ShaderInput to a valid state.
Definition: shaderinput.h:189
void Init(const ShaderInputRegistry &registry, size_t registry_id, size_t index_in_registry, size_t array_index, ValueType type, const T &value)
Initializes the ShaderInput to a valid state.
Definition: shaderinput.h:170
size_t GetCount() const
Returns the number of elements in the held type.
Definition: shaderinput.h:99
A ShaderInputRegistry is used to manage a collection of shader inputs to a specific ShaderProgram (bo...
const base::AllocatorPtr & GetArrayAllocator() const
Returns the allocator used to make array allocations.
Definition: shaderinput.h:207
uint64 GetStamp() const
Returns the stamp of the input.
Definition: shaderinput.h:145
size_t GetIndexInRegistry() const
Returns the index of the shader input within the registry.
Definition: shaderinput.h:67
ShaderInput()
The default constructor creates an invalid ShaderInput instance, which should never be used as is...
Definition: shaderinput.h:151
static uint64 GetNewStamp()
Returns atomically post-incremented stamp.
Definition: shaderinput.cc:24
const T & GetValue() const
If this instance contains a value of type T, this returns a const reference to it.
Definition: shaderinput.h:85
A ShaderInput instance represents a general shader input.
Definition: shaderinput.h:50
bool IsValid() const
Returns true if this is a valid instance created by a ShaderInputRegistry.
Definition: shaderinput.h:59
const T & GetValueAt(size_t i) const
If this instance contains an array of values of type T with a length smaller than the passed index...
Definition: shaderinput.h:93
bool SetValueAt(size_t i, const T &value)
If this instance contains a array of values of type T with a length larger than i, this changes the element at i to the new value.
Definition: shaderinput.h:131
ValueHolderType HolderType
Definition: shaderinput.h:52
ValueType GetType() const
Returns the type of the shader input.
Definition: shaderinput.h:80
UniformType
The UniformType enum defines all supported uniform shader argument types.
Definition: uniform.h:37