All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
GLStateTypes.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Types.h"
6 #include "Graphics.h"
7 
8 #ifdef EEGEO_WIN
9 #define EEGEO_GL_API_CALL __stdcall
10 #else
11 #define EEGEO_GL_API_CALL
12 #endif
13 
14 namespace Eegeo
15 {
16  namespace Rendering
17  {
19  {
20  public:
21  GLStateBase()
22  : m_numOfActualSets(0)
23  , m_stateValid(false)
24  , m_stateFilteringEnabled(true)
25  , m_trySetAttempted(false)
26  , m_numOfAttemptedSets(0)
27  {
28  }
29 
30  GLStateBase(bool enableStateFiltering)
31  : m_stateValid(false)
32  , m_stateFilteringEnabled(enableStateFiltering)
33  {
34  }
35 
36  inline void Invalidate()
37  {
38  m_stateValid = false;
39  }
40 
41  inline int GetNumOfAttemptedSets() const
42  {
43  return m_numOfAttemptedSets;
44  }
45 
46  inline int GetNumOfActualSets() const
47  {
48  return m_numOfActualSets;
49  }
50 
51  inline void ResetCounters()
52  {
53  m_numOfAttemptedSets = 0;
54  m_numOfActualSets = 0;
55  }
56 
57  inline void SetTrySetAttempted()
58  {
59  m_trySetAttempted = true;
60  ++m_numOfAttemptedSets;
61  }
62 
63  inline void ClearTrySetAttempted()
64  {
65  m_trySetAttempted = false;
66  }
67 
68  inline bool TrySetAttempted() const
69  {
70  return m_trySetAttempted;
71  }
72 
73  inline bool IsStateValid() const
74  {
75  return m_stateValid;
76  }
77 
78  protected:
79  int m_numOfActualSets;
80 
81  inline void SetIsValid()
82  {
83  m_stateValid = m_stateFilteringEnabled;
84  }
85 
86  private:
87  bool m_stateValid;
88  bool m_stateFilteringEnabled;
89  bool m_trySetAttempted;
90  int m_numOfAttemptedSets;
91  };
92 
93  template<GLenum T_cap>
94  class GLBoolState : public GLStateBase
95  {
96  public:
97  GLBoolState(const bool value)
98  : m_value(value)
99  {
100  }
101 
102  inline void Enable()
103  {
104  TrySet(true);
105  }
106 
107  inline void Disable()
108  {
109  TrySet(false);
110  }
111 
112  inline void operator () (bool enable)
113  {
114  TrySet(enable);
115  }
116 
117  inline bool TrySet(bool value)
118  {
119  SetTrySetAttempted();
120 
121  if (!IsStateValid() || value != m_value)
122  {
123  ++m_numOfActualSets;
124 
125  m_value = value;
126  SetIsValid();
127  if (value)
128  {
129  Eegeo_GL(glEnable(T_cap));
130  }
131  else
132  {
133  Eegeo_GL(glDisable(T_cap));
134  }
135  return true;
136  }
137  return false;
138  }
139 
140  inline bool TrySet(const GLBoolState& stateToSet)
141  {
142  if(stateToSet.IsStateValid())
143  {
144  return TrySet(stateToSet.m_value);
145  }
146 
147  Eegeo_ASSERT(IsStateValid() == false, "Can't set a valid from an invalid state");
148  return false;
149  }
150 
151  inline bool TrySetIfNotAttempted(const GLBoolState& stateToSet)
152  {
153  if(!TrySetAttempted())
154  {
155  return TrySet(stateToSet);
156  }
157 
158  return false;
159  }
160 
161 
162  private:
163  bool m_value;
164  };
165 
166  template<typename T_paramA>
167  class GLStateFunc1 : public GLStateBase
168  {
169  public:
170  GLStateFunc1(void (EEGEO_GL_API_CALL *func)(T_paramA))
171  : m_func(func)
172  {
173  }
174 
175  inline void operator () (T_paramA paramA)
176  {
177  TrySet(paramA);
178  }
179 
180  inline bool TrySet(T_paramA paramA)
181  {
182  SetTrySetAttempted();
183 
184  if (!IsStateValid() ||
185  paramA != m_paramA)
186  {
187  ++m_numOfActualSets;
188  m_paramA = paramA;
189  SetIsValid();
190  Eegeo_GL(m_func(paramA));
191  return true;
192  }
193  return false;
194  }
195 
196  inline bool TrySet(const GLStateFunc1& stateToSet)
197  {
198  if(stateToSet.IsStateValid())
199  {
200  return TrySet(stateToSet.m_paramA);
201  }
202 
203  Eegeo_ASSERT(IsStateValid() == false, "Can't set a valid from an invalid state");
204  return false;
205  }
206 
207  inline bool TrySetIfNotAttempted(const GLStateFunc1& stateToSet)
208  {
209  if(!TrySetAttempted())
210  {
211  return TrySet(stateToSet);
212  }
213 
214  return false;
215  }
216 
217 
218  inline T_paramA GetValue() const
219  {
220  return m_paramA;
221  }
222 
223  private:
224  void (EEGEO_GL_API_CALL *m_func)(T_paramA);
225  T_paramA m_paramA;
226  };
227 
228  template<typename T_paramA, typename T_paramB>
229  class GLStateFunc2 : public GLStateBase
230  {
231  public:
232  GLStateFunc2(void (EEGEO_GL_API_CALL *func)(T_paramA, T_paramB))
233  : m_func(func)
234  {
235  }
236 
237  inline void operator () (T_paramA paramA, T_paramB paramB)
238  {
239  TrySet(paramA, paramB);
240  }
241 
242  inline bool TrySet(T_paramA paramA, T_paramB paramB)
243  {
244  SetTrySetAttempted();
245 
246  if (!IsStateValid() ||
247  (paramA != m_paramA) ||
248  (paramB != m_paramB))
249  {
250  ++m_numOfActualSets;
251  #pragma clang diagnostic push
252  #pragma clang diagnostic ignored "-Wunknown-pragmas"
253  #pragma GCC diagnostic push
254  #ifndef __clang__
255  #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
256  #endif
257  m_paramA = paramA;
258  m_paramB = paramB;
259  SetIsValid();
260  Eegeo_GL(m_func(paramA, paramB));
261  #pragma GCC diagnostic pop
262  #pragma clang diagnostic pop
263  return true;
264  }
265  return false;
266  }
267 
268  inline bool TrySet(const GLStateFunc2& stateToSet)
269  {
270  if(stateToSet.IsStateValid())
271  {
272  return TrySet(stateToSet.m_paramA, stateToSet.m_paramB);
273  }
274 
275  Eegeo_ASSERT(IsStateValid() == false, "Can't set a valid from an invalid state");
276  return false;
277  }
278 
279 
280  inline bool TrySetIfNotAttempted(const GLStateFunc2& stateToSet)
281  {
282  if(!TrySetAttempted())
283  {
284  return TrySet(stateToSet);
285  }
286 
287  return false;
288  }
289 
290 
291  private:
292  void (EEGEO_GL_API_CALL *m_func)(T_paramA, T_paramB);
293  T_paramA m_paramA;
294  T_paramB m_paramB;
295  };
296 
297  template<typename T_paramA, typename T_paramB, typename T_paramC>
298  class GLStateFunc3 : public GLStateBase
299  {
300  public:
301  GLStateFunc3(void (EEGEO_GL_API_CALL *func)(T_paramA, T_paramB, T_paramC))
302  : m_func(func)
303  {
304  }
305 
306  inline void operator () (T_paramA paramA, T_paramB paramB, T_paramC paramC)
307  {
308  TrySet(paramA, paramB, paramC);
309  }
310 
311  inline bool TrySet(T_paramA paramA, T_paramB paramB, T_paramC paramC)
312  {
313  SetTrySetAttempted();
314 
315  if (!IsStateValid() ||
316  (paramA != m_paramA) ||
317  (paramB != m_paramB) ||
318  (paramC != m_paramC))
319  {
320  ++m_numOfActualSets;
321 
322  m_paramA = paramA;
323  m_paramB = paramB;
324  m_paramC = paramC;
325  SetIsValid();
326  Eegeo_GL(m_func(paramA, paramB, paramC));
327  return true;
328  }
329  return false;
330  }
331 
332  inline bool TrySet(const GLStateFunc3& stateToSet)
333  {
334  if(stateToSet.IsStateValid())
335  {
336  return TrySet(stateToSet.m_paramA, stateToSet.m_paramB, stateToSet.m_paramC);
337  }
338 
339  Eegeo_ASSERT(IsStateValid() == false, "Can't set a valid from an invalid state");
340  return false;
341  }
342 
343  inline bool TrySetIfNotAttempted(const GLStateFunc3& stateToSet)
344  {
345  if(!TrySetAttempted())
346  {
347  return TrySet(stateToSet);
348  }
349 
350  return false;
351  }
352 
353 
354  private:
355  void (EEGEO_GL_API_CALL *m_func)(T_paramA, T_paramB, T_paramC);
356  T_paramA m_paramA;
357  T_paramB m_paramB;
358  T_paramC m_paramC;
359  };
360 
361  template<typename T_paramA, typename T_paramB, typename T_paramC, typename T_paramD>
362  class GLStateFunc4 : public GLStateBase
363  {
364  public:
365  GLStateFunc4(void (EEGEO_GL_API_CALL *func)(T_paramA, T_paramB, T_paramC, T_paramD))
366  : m_func(func)
367  {
368  }
369 
370  inline void operator () (T_paramA paramA, T_paramB paramB, T_paramC paramC, T_paramD paramD)
371  {
372  TrySet(paramA, paramB, paramC, paramD);
373  }
374 
375  inline bool TrySet(T_paramA paramA, T_paramB paramB, T_paramC paramC, T_paramD paramD)
376  {
377  SetTrySetAttempted();
378 
379  if (!IsStateValid() ||
380  (paramA != m_paramA) ||
381  (paramB != m_paramB) ||
382  (paramC != m_paramC) ||
383  (paramD != m_paramD))
384  {
385  ++m_numOfActualSets;
386 
387  m_paramA = paramA;
388  m_paramB = paramB;
389  m_paramC = paramC;
390  m_paramD = paramD;
391  SetIsValid();
392  Eegeo_GL(m_func(paramA, paramB, paramC, paramD));
393  return true;
394  }
395  return false;
396  }
397 
398  inline bool TrySet(const GLStateFunc4& stateToSet)
399  {
400  if(stateToSet.IsStateValid())
401  {
402  return TrySet(stateToSet.m_paramA, stateToSet.m_paramB, stateToSet.m_paramC, stateToSet.m_paramD);
403  }
404 
405  Eegeo_ASSERT(IsStateValid() == false, "Can't set a valid from an invalid state");
406  return false;
407  }
408 
409  inline bool TrySetIfNotAttempted(const GLStateFunc4& stateToSet)
410  {
411  if(!TrySetAttempted())
412  {
413  return TrySet(stateToSet);
414  }
415 
416  return false;
417  }
418 
419  private:
420  void (EEGEO_GL_API_CALL *m_func)(T_paramA, T_paramB, T_paramC, T_paramD);
421  T_paramA m_paramA;
422  T_paramB m_paramB;
423  T_paramC m_paramC;
424  T_paramD m_paramD;
425  };
426 
427 
428  template<GLenum T_glEnum, typename T_paramA>
430  {
431  public:
432  GLStateEnumFunc1(void (EEGEO_GL_API_CALL *func)(GLenum, T_paramA))
433  : m_func(func)
434  {
435  }
436 
438  : m_func(NULL)
439  {
440  }
441 
443  : GLStateBase(other)
444  , m_func(other.m_func)
445  {
446  }
447 
448  GLStateEnumFunc1(void (EEGEO_GL_API_CALL *func)(GLenum, T_paramA), bool enableStateFiltering)
449  : GLStateBase(enableStateFiltering)
450  , m_func(func)
451  {
452  }
453 
454  inline void operator () (T_paramA paramA)
455  {
456  TrySet(paramA);
457  }
458 
459  inline bool TrySet(T_paramA paramA)
460  {
461  SetTrySetAttempted();
462 
463  if (!IsStateValid() ||
464  paramA != m_paramA)
465  {
466  ++m_numOfActualSets;
467 
468  m_paramA = paramA;
469  SetIsValid();
470  Eegeo_GL(m_func(T_glEnum, paramA));
471  return true;
472  }
473  return false;
474  }
475 
476  inline bool TrySet(const GLStateEnumFunc1& stateToSet)
477  {
478  if(stateToSet.IsStateValid())
479  {
480  return TrySet(stateToSet.m_paramA);
481  }
482 
483  Eegeo_ASSERT(IsStateValid() == false, "Can't set a valid from an invalid state");
484  return false;
485  }
486 
487  inline bool TrySetIfNotAttempted(const GLStateEnumFunc1& stateToSet)
488  {
489  if(!TrySetAttempted())
490  {
491  return TrySet(stateToSet);
492  }
493 
494  return false;
495  }
496 
497  private:
498  void (EEGEO_GL_API_CALL *m_func)(GLenum, T_paramA);
499  T_paramA m_paramA;
500  };
501  }
502 }