IUniform.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include <ionCore.h>
5 #include <ionMath.h>
6 
7 #include "Enums.h"
8 
9 
10 namespace ion
11 {
12  namespace Graphics
13  {
14 
17  enum class EUniformType
18  {
19  Float = 0,
20  Float2 = 1,
21  Float3 = 2,
22  Float4 = 3,
23  Int = 4,
24  Int2 = 5,
25  Int3 = 6,
26  Int4 = 7,
27  Matrix4x4 = 8,
28  FloatArray = 9,
29  Float2Array = 10,
30  Float3Array = 11,
31  Matrix4x4Array = 12,
32  Bool = 13,
33  };
34 
37  string GetUniformTypeString(EUniformType const UniformType);
38 
41  class IUniform
42  {
43 
44  public:
45 
46  virtual void const * GetData() const = 0;
47  virtual EUniformType GetType() const = 0;
48  virtual size_t GetSize() const = 0;
49 
50  };
51 
54  template <typename T>
55  class IUniformTyped : public IUniform
56  {
57 
58  public:
59 
60  EUniformType GetType() const;
61 
62  size_t GetSize() const
63  {
64  return sizeof(T);
65  }
66 
67  };
68 
71  template <typename T>
72  class CUniformReference : public IUniformTyped<T>
73  {
74 
75  public:
76 
77  T const * Value;
78 
80  : Value(0)
81  {}
82 
83  CUniformReference(T const * value)
84  : Value(value)
85  {}
86 
87  void const * GetData() const
88  {
89  return Value;
90  }
91 
92  };
93 
96  template <typename T>
97  class CUniformCallback : public IUniformTyped<T>
98  {
99 
100  public:
101 
102  std::function<T()> Callback;
103 
104  CUniformCallback(std::function<T()> const callback)
105  : Callback(callback)
106  {}
107 
108  void const * GetData() const
109  {
110  Temporary = Callback();
111  return &Temporary;
112  }
113 
114  protected:
115 
116  mutable T Temporary;
117 
118  };
119 
122  template <typename T>
123  class CUniformValue : public IUniformTyped<T>
124  {
125 
126  public:
127 
129 
131  {}
132 
133  CUniformValue(T const & value)
134  : Value(value)
135  {}
136 
137  void const * GetData() const
138  {
139  return & Value;
140  }
141 
142  CUniformValue & operator = (T const & value)
143  {
144  Value = value;
145  return * this;
146  }
147 
148  operator T () const
149  {
150  return Value;
151  }
152 
153  };
154 
157  template <typename T>
158  class CUniform
159  {
160 
161  public:
162 
164 
166  : Uniform(MakeShared<CUniformValue<T>>())
167  {}
168 
169  CUniform(CUniform<T> const & other)
170  : Uniform(MakeShared<CUniformValue<T>>())
171  {
172  Uniform->Value = other.Uniform->Value;
173  }
174 
176  : Uniform(other.Uniform)
177  {
178  other.Uniform = nullptr;
179  }
180 
181  CUniform(T const & value)
182  : Uniform(MakeShared<CUniformValue<T>>())
183  {
184  Uniform->Value = value;
185  }
186 
187  CUniform<T> & operator = (CUniform<T> const & other)
188  {
189  Uniform->Value = other.Uniform->Value;
190  return * this;
191  }
192 
193  CUniform<T> & operator = (CUniform<T> && other)
194  {
195  Uniform = other.Uniform;
196  other.Uniform = nullptr;
197  return * this;
198  }
199 
200  CUniform<T> & operator = (T const & value)
201  {
202  Uniform->Value = value;
203  return * this;
204  }
205 
206  T * operator ->()
207  {
208  return & Uniform->Value;
209  }
210 
211  operator T () const
212  {
213  return Uniform->Value;
214  }
215 
217  {
218  return Uniform;
219  }
220 
221  T & Get()
222  {
223  return Uniform->Value;
224  }
225 
226  CUniform<T> & operator ++ ()
227  {
228  Uniform->Value ++;
229  return * this;
230  }
231 
232  CUniform<T> & operator ++ (int)
233  {
234  Uniform->Value ++;
235  return * this;
236  }
237 
238  CUniform<T> & operator -- ()
239  {
240  Uniform->Value --;
241  return * this;
242  }
243 
244  CUniform<T> & operator -- (int)
245  {
246  Uniform->Value --;
247  return * this;
248  }
249 
250  CUniform<T> & operator += (T const & value)
251  {
252  Uniform->Value += value;
253  return * this;
254  }
255 
256  CUniform<T> & operator -= (T const & value)
257  {
258  Uniform->Value -= value;
259  return * this;
260  }
261 
262  CUniform<T> & operator *= (T const & value)
263  {
264  Uniform->Value *= value;
265  return * this;
266  }
267 
268  CUniform<T> & operator /= (T const & value)
269  {
270  Uniform->Value /= value;
271  return * this;
272  }
273 
274  };
275 
276  }
277 }
CUniformCallback(std::function< T()> const callback)
Definition: IUniform.h:104
CUniform(T const &value)
Definition: IUniform.h:181
CUniformValue(T const &value)
Definition: IUniform.h:133
std::shared_ptr< T > SharedPointer
Definition: ionSmartPointer.h:22
std::function< T()> Callback
Definition: IUniform.h:102
string GetUniformTypeString(EUniformType const UniformType)
Converts uniform enum to equivalent string form.
Definition: IUniform.cpp:10
T Value
Definition: IUniform.h:128
CUniform()
Definition: IUniform.h:165
T & Get()
Definition: IUniform.h:221
CUniformReference()
Definition: IUniform.h:79
Implementation of IUniformTyped that stores a copy of a given value for uniform upload.
Definition: IUniform.h:123
Implementation of IUniformTyped that stores a (non-owning) reference to value.
Definition: IUniform.h:72
Definition: CCatmullRomAdvancedSplineInterpolator.h:7
SharedPointer< CUniformValue< T > > Uniform
Definition: IUniform.h:163
Abstract interface for anything which can be uploaded as a shader uniform.
Definition: IUniform.h:41
CUniformValue()
Definition: IUniform.h:130
EUniformType
Possible types for uniforms.
Definition: IUniform.h:17
void const * GetData() const
Definition: IUniform.h:87
CUniform(CUniform< T > &&other)
Definition: IUniform.h:175
Helper class to easily add a uniform as a member or local variable.
Definition: IUniform.h:158
T const * Value
Definition: IUniform.h:77
void const * GetData() const
Definition: IUniform.h:108
void const * GetData() const
Definition: IUniform.h:137
size_t GetSize() const
Definition: IUniform.h:62
Implementation of IUniformTyped that uses a given lambda callback to provide uniform value...
Definition: IUniform.h:97
CUniformReference(T const *value)
Definition: IUniform.h:83
Abstract interface for IUniform with known type.
Definition: IUniform.h:55
CUniform(CUniform< T > const &other)
Definition: IUniform.h:169
T Temporary
Definition: IUniform.h:116