OGLplus (0.52.0) a C++ wrapper for OpenGL

uniform.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_UNIFORM_1107121519_HPP
14 #define OGLPLUS_UNIFORM_1107121519_HPP
15 
16 #include <oglplus/glfunc.hpp>
17 #include <oglplus/string/ref.hpp>
20 #include <oglplus/prog_var/varpara_fns.hpp>
21 #include <oglplus/prog_var/set_ops.hpp>
23 #include <type_traits>
24 
25 namespace oglplus {
26 
27 template <>
28 class ProgVarLocOps<tag::Uniform>
29 {
30 private:
31  static const char* MsgGettingInactive(void);
32 protected:
33  static const char* MsgUsingInactive(void);
34 public:
36 
43  static GLint GetLocation(
44  ProgramName program,
45  StrCRef identifier,
46  bool active_only
47  )
48  {
49  GLint result = OGLPLUS_GLFUNC(GetUniformLocation)(
50  GetGLName(program),
51  identifier.c_str()
52  );
53  OGLPLUS_CHECK(
54  GetUniformLocation,
55  ProgVarError,
56  Program(program).
57  Identifier(identifier)
58  );
59  OGLPLUS_HANDLE_ERROR_IF(
60  active_only && (result < 0),
61  GL_INVALID_OPERATION,
62  MsgGettingInactive(),
63  ProgVarError,
64  Program(program).
65  Identifier(identifier)
66  );
67  return result;
68  }
69 };
70 
71 // collection of Uniform setter functions for basic types
72 template <>
73 class ProgVarSetters<tag::ImplicitSel, tag::Uniform, tag::NativeTypes>
74 {
75 protected:
76  OGLPLUS_ERROR_CONTEXT(Uniform, Uniform)
77 
78  OGLPLUS_AUX_VARPARA_FNS(Uniform, ui, t, GLuint)
79  OGLPLUS_AUX_VARPARA_FNS(Uniform, i, t, GLint)
80 #if GL_ARB_bindless_texture
81  OGLPLUS_AUX_VARPARA_FNC(UniformHandle, ui64ARB, t, GLuint64, 1)
82 #elif GL_NV_shader_buffer_load
83  OGLPLUS_AUX_VARPARA_FNC(Uniform, ui64NV, t, GLuint64EXT, 1)
84 #endif
85  OGLPLUS_AUX_VARPARA_FNS(Uniform, f, t, GLfloat)
86 #if GL_VERSION_3_3 || GL_ARB_gpu_shader_fp64
87  OGLPLUS_AUX_VARPARA_FNS(Uniform, d, t, GLdouble)
88 #endif
89 
90  OGLPLUS_AUX_VARPARA_FNS(Uniform, iv, v, GLint)
91 #if GL_ARB_bindless_texture
92  OGLPLUS_AUX_VARPARA_FNC(UniformHandle, ui64vARB, v, GLuint64, 1)
93 #endif
94  OGLPLUS_AUX_VARPARA_FNS(Uniform, fv, v, GLfloat)
95 #if GL_VERSION_3_3 || GL_ARB_gpu_shader_fp64
96  OGLPLUS_AUX_VARPARA_FNS(Uniform, dv, v, GLdouble)
97 #endif
98 };
99 
100 // collection of Uniform setter function for matrices
101 template <>
102 class ProgVarSetters<tag::ImplicitSel, tag::Uniform, tag::MatrixTypes>
103 {
104 protected:
105  OGLPLUS_ERROR_CONTEXT(UniformMatrix, Uniform)
106 
107  OGLPLUS_AUX_VARPARA_MAT_FNS(UniformMatrix, fv, v, GLfloat)
108 #if GL_VERSION_3_3 || GL_ARB_gpu_shader_fp64
109  OGLPLUS_AUX_VARPARA_MAT_FNS(UniformMatrix, dv, v, GLdouble)
110 #endif
111 };
112 
114 
117 template <typename OpsTag, typename T>
118 class ProgVarGetSetOps<OpsTag, tag::Uniform, T>
119  : public ProgVarCommonOps<tag::Uniform>
120  , public ProgVarBaseSetOps<OpsTag, tag::Uniform, tag::NativeTypes, T, 16>
121 {
122 protected:
123  ProgVarGetSetOps(UniformLoc uloc)
124  : ProgVarCommonOps<tag::Uniform>(uloc)
125  { }
126 public:
128 
133  void SetValue(T value)
134  {
135  this->_do_set(
136  this->_program,
137  this->_location,
138  value
139  );
140  }
141 
143  void SetValues(std::size_t n, const T* values)
144  {
145  this->template _do_set_many<1>(
146  this->_program,
147  this->_location,
148  GLsizei(n),
149  values
150  );
151  }
152 };
153 
154 template <typename OpsTag, typename T, std::size_t N>
155 class ProgVarGetSetOps<OpsTag, tag::Uniform, Vector<T, N>>
156  : public ProgVarCommonOps<tag::Uniform>
157  , public ProgVarBaseSetOps<OpsTag, tag::Uniform, tag::NativeTypes, T, 4>
158 {
159 protected:
160  ProgVarGetSetOps(UniformLoc uloc)
161  : ProgVarCommonOps<tag::Uniform>(uloc)
162  { }
163 public:
164  void SetValue(const Vector<T, N>& value)
165  {
166  this->template _do_set<N>(_program, _location, Data(value));
167  }
168 
169  void SetValues(std::size_t n, const T* values)
170  {
171  assert(n % N == 0);
172  this->template _do_set_many<N>(
173  this->_program,
174  this->_location,
175  GLsizei(n),
176  values
177  );
178  }
179 
180  void SetValues(std::size_t n, const Vector<T, N>* values, std::true_type)
181  {
182  const T* temp = (const T*)(values);
183  SetValues(n*N, temp);
184  }
185 
186  void SetValues(std::size_t n, const Vector<T, N>* values,std::false_type)
187  {
188  std::vector<T> temp;
189  temp.reserve(n*N);
190  for(std::size_t i=0; i!=n; ++i)
191  {
192  temp.insert(temp.end(), Data(values), Data(values)+N);
193  }
194  SetValues(temp.size(), temp.data());
195  }
196 
197  void SetValues(std::size_t n, const Vector<T, N>* values)
198  {
199  SetValues(
200  n, values,
201  std::integral_constant<
202  bool,
203  sizeof(Vector<T, N>[4]) == sizeof(T[N*4])
204  >()
205  );
206  }
207 };
208 
209 template <typename OpsTag, typename T, std::size_t R, std::size_t C>
210 class ProgVarGetSetOps<OpsTag, tag::Uniform, Matrix<T, R, C>>
211  : public ProgVarCommonOps<tag::Uniform>
212  , public ProgVarBaseSetOps<OpsTag, tag::Uniform, tag::MatrixTypes, T, 16>
213 {
214 protected:
215  ProgVarGetSetOps(UniformLoc uloc)
216  : ProgVarCommonOps<tag::Uniform>(uloc)
217  { }
218 public:
219  void SetValue(const Matrix<T, R, C>& value)
220  {
221  this->template _do_set_mat<C, R>(
222  this->_program,
223  this->_location,
224  1,
225  true,
226  Data(value)
227  );
228  }
229 
230  void SetValues(std::size_t n, bool row_major, const T* values)
231  {
232  assert(n % R*C == 0);
233  this->template _do_set_mat<C, R>(
234  this->_program,
235  this->_location,
236  GLsizei(n/(R*C)),
237  row_major,
238  values
239  );
240  }
241 
242  void SetValues(
243  std::size_t n,
244  const Matrix<T, R, C>* values,
245  std::true_type
246  )
247  {
248  const T* temp = (const T*)(values);
249  SetValues(n*R*C, true, temp);
250  }
251 
252  void SetValues(
253  std::size_t n,
254  const Matrix<T, R, C>* values,
255  std::false_type
256  )
257  {
258  std::vector<T> temp;
259  temp.reserve(n*R*C);
260  for(std::size_t i=0; i!=n; ++i)
261  {
262  temp.insert(temp.end(), Data(values), Data(values)+R*C);
263  }
264  SetValues(temp.size(), temp.data());
265  }
266 
267  void SetValues(std::size_t n, const Matrix<T, R, C>* values)
268  {
269  SetValues(
270  n, values,
271  std::integral_constant<
272  bool,
273  sizeof(Matrix<T, R, C>[4]) == sizeof(T[R*C*4])
274  >()
275  );
276  }
277 };
278 
280  Uniform,
281  tag::ImplicitSel,
282  tag::Uniform,
283  tag::NoTypecheck
284 )
285 
286 typedef Uniform<GLint> UniformSampler;
288 
289 // typeless uniform
290 template <typename OpsTag>
291 class ProgVar<OpsTag, tag::Uniform, tag::NoTypecheck, void>
292  : public ProgVarCommonOps<tag::Uniform>
293 {
294 private:
295  typedef ProgVarCommonOps<tag::Uniform> Base;
296 public:
297  ProgVar(ProgramName program, GLuint location)
298  : Base(UniformLoc(program, location))
299  { }
300 
301  ProgVar(ProgramName program, StrCRef identifier)
302  : Base(UniformLoc(program, identifier))
303  { }
304 
305  template <typename T>
306  void Set(T value)
307  {
308  ProgVar<OpsTag, tag::Uniform, tag::NoTypecheck, T>(*this).Set(value);
309  }
310 
311  template <typename T>
312  ProgVar& operator = (T value)
313  {
314  Set(value);
315  return *this;
316  }
317 };
318 
319 typedef ProgVar<tag::ImplicitSel, tag::Uniform, tag::NoTypecheck, void> UntypedUniform;
320 
321 inline UntypedUniform
322 operator / (ProgramName program, StrCRef identifier)
323 {
324  return UntypedUniform(program, identifier);
325 }
326 
327 } // namespace oglplus
328 
329 #if !OGLPLUS_LINK_LIBRARY || defined(OGLPLUS_IMPLEMENTING_LIBRARY)
330 #include <oglplus/uniform.ipp>
331 #endif
332 
333 #endif // include guard
const Char * c_str(void) const
Returns the null-terminated c-string.
Definition: ref_tpl.hpp:200
Program variable wrapper.
Program variable location wrapper.
OGLPLUS_DECLARE_PROG_VAR(Uniform, tag::ImplicitSel, tag::Uniform, tag::NoTypecheck) typedef Uniform< GLint > UniformSampler
Uniform sampler.
GLuint GetGLName(ObjectName< ObjTag > named)
Returns the GLuint OpenGL name assigned to named object.
Definition: name.hpp:38
String reference.
void SetValue(T value)
Set the value of the uniform.
Definition: uniform.hpp:133
String const reference wrapper template.
Definition: ref_tpl.hpp:72
Helper macro for optional checking of availability of GL function.
Declaration of OGLplus program-variable-related error.
void SetValues(std::size_t n, const T *values)
Set multiple consecutive values.
Definition: uniform.hpp:143

Copyright © 2010-2014 Matúš Chochlík, University of Žilina, Žilina, Slovakia.
<matus.chochlik -at- fri.uniza.sk>
<chochlik -at -gmail.com>
Documentation generated on Mon Sep 22 2014 by Doxygen (version 1.8.6).