OGLplus (0.52.0) a C++ wrapper for OpenGL

uniform_subroutines.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_UNIFORM_SUBROUTINE_1107121519_HPP
14 #define OGLPLUS_UNIFORM_SUBROUTINE_1107121519_HPP
15 
16 #include <oglplus/glfunc.hpp>
17 #include <oglplus/string/ref.hpp>
18 #include <oglplus/error/object.hpp>
21 #include <oglplus/prog_var/set_ops.hpp>
23 #include <oglplus/shader_type.hpp>
24 
25 #include <cassert>
26 
27 namespace oglplus {
28 
29 #if OGLPLUS_DOCUMENTATION_ONLY || GL_VERSION_4_0 || GL_ARB_shader_subroutine
30 
31 template <>
32 class ProgVarLocOps<tag::Subroutine>
33 {
34 private:
35  ShaderType _stage;
36  static const char* MsgGettingInactive(void);
37 protected:
38  static const char* MsgUsingInactive(void);
39  ProgVarLocOps(ShaderType stage)
40  : _stage(stage)
41  { }
42 public:
44 
52  static GLint GetLocation(
53  ProgramName program,
54  ShaderType stage,
55  StrCRef identifier,
56  bool active_only
57  )
58  {
59  GLint result = OGLPLUS_GLFUNC(GetSubroutineIndex)(
60  GetGLName(program),
61  GLenum(stage),
62  identifier.c_str()
63  );
64  OGLPLUS_CHECK(
65  GetSubroutineIndex,
66  ProgVarError,
67  Program(program).
68  Identifier(identifier).
69  EnumParam(stage)
70  );
71  OGLPLUS_HANDLE_ERROR_IF(
72  active_only && (result < 0),
73  GL_INVALID_OPERATION,
74  MsgGettingInactive(),
75  ProgVarError,
76  Program(program).
77  Identifier(identifier).
78  EnumParam(stage)
79  );
80  return result;
81  }
82 
84 
91  GLint GetLocation(
92  ProgramName program,
93  StrCRef identifier,
94  bool active_only
95  )
96  {
97  return GetLocation(
98  program,
99  this->_stage,
100  identifier,
101  active_only
102  );
103  }
104 
106  ShaderType Stage(void) const
107  {
108  return _stage;
109  }
110 };
111 
113 template <>
114 class ProgVar<tag::ImplicitSel, tag::Subroutine, tag::NoTypecheck, void>
115  : public ProgVarLoc<tag::Subroutine>
116 {
117 public:
120  : ProgVarLoc<tag::Subroutine>(pvloc)
121  { }
122 
124  ProgVar(ProgramName program, ShaderType stage, StrCRef identifier)
125  : ProgVarLoc<tag::Subroutine>(stage, program, identifier)
126  { }
127 
130  ProgramName program,
131  ShaderType stage,
132  StrCRef identifier,
133  bool active_only
134  ): ProgVarLoc<tag::Subroutine>(stage, program, identifier, active_only)
135  { }
136 };
137 
139 typedef ProgVar<tag::ImplicitSel, tag::Subroutine, tag::NoTypecheck, void>
141 
142 template <>
143 class ProgVarLocOps<tag::SubroutineUniform>
144 {
145 private:
146  ShaderType _stage;
147  static const char* MsgGettingInactive(void);
148 protected:
149  static const char* MsgUsingInactive(void);
150  ProgVarLocOps(ShaderType stage)
151  : _stage(stage)
152  { }
153 public:
155 
162  static GLint GetLocation(
163  ProgramName program,
164  ShaderType stage,
165  StrCRef identifier,
166  bool active_only
167  )
168  {
169  GLint result = OGLPLUS_GLFUNC(GetSubroutineUniformLocation)(
170  GetGLName(program),
171  GLenum(stage),
172  identifier.c_str()
173  );
174  OGLPLUS_CHECK(
175  GetSubroutineUniformLocation,
176  ProgVarError,
177  Program(program).
178  Identifier(identifier).
179  EnumParam(stage)
180  );
181  OGLPLUS_HANDLE_ERROR_IF(
182  active_only && (result < 0),
183  GL_INVALID_OPERATION,
184  MsgGettingInactive(),
185  ProgVarError,
186  Program(program).
187  Identifier(identifier).
188  EnumParam(stage)
189  );
190  return result;
191  }
192 
194 
201  GLint GetLocation(
202  ProgramName program,
203  StrCRef identifier,
204  bool active_only
205  )
206  {
207  return GetLocation(
208  program,
209  this->_stage,
210  identifier,
211  active_only
212  );
213  }
214 
216  ShaderType Stage(void) const
217  {
218  return _stage;
219  }
220 };
221 
223 template <>
224 class ProgVar<tag::ImplicitSel, tag::SubroutineUniform, tag::NoTypecheck, void>
225  : public ProgVarLoc<tag::SubroutineUniform>
226 {
227 public:
230  : ProgVarLoc<tag::SubroutineUniform>(pvloc)
231  { }
232 
234  ProgVar(ProgramName program, ShaderType stage, StrCRef identifier)
235  : ProgVarLoc<tag::SubroutineUniform>(stage, program, identifier)
236  { }
237 
240  ProgramName program,
241  ShaderType stage,
242  StrCRef identifier,
243  bool active_only
244  ): ProgVarLoc<tag::SubroutineUniform>(
245  stage,
246  program,
247  identifier,
248  active_only
249  ){ }
250 };
251 
253 typedef ProgVar<tag::ImplicitSel, tag::SubroutineUniform, tag::NoTypecheck, void>
255 
257 
263 {
264 private:
265  ProgramName _program;
266  ShaderType _stage;
267  std::vector<GLuint> _indices;
268 
269  static GLsizei _get_location_count(
270  ProgramName program,
271  ShaderType stage
272  )
273  {
274  GLint result;
275  OGLPLUS_GLFUNC(GetProgramStageiv)(
276  GetGLName(program),
277  GLenum(stage),
278  GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS,
279  &result
280  );
281  OGLPLUS_VERIFY(
282  GetProgramStageiv,
283  ObjectError,
284  Object(program).
285  EnumParam(stage)
286  );
287  return result;
288  }
289 
290  std::vector<GLuint>& _get_indices(void)
291  {
292  if(_indices.empty())
293  {
294  _indices.resize(
295  _get_location_count(
296  _program,
297  _stage
298  ), 0
299  );
300  }
301  return _indices;
302  }
303 public:
306  ProgramName program,
307  ShaderType stage
308  ): _program(program)
309  , _stage(stage)
310  { }
311 
313 
321  const SubroutineUniform& uniform,
322  const Subroutine& subroutine
323  )
324  {
325  assert(uniform.Program() == _program);
326  assert(subroutine.Program() == _program);
327  assert(uniform.Stage() == ShaderType(_stage));
328  assert(subroutine.Stage() == ShaderType(_stage));
329  assert(uniform.IsActive());
330  assert(uniform.Location() <= GLint(_get_indices().size()));
331 
332  _get_indices()[uniform.Location()] = subroutine.Location();
333  return *this;
334  }
335 
337 
347  class Preset
348  {
349  private:
350  friend class UniformSubroutines;
351 
352  const std::vector<GLuint> _indices;
353 #ifndef NDEBUG
354  ProgramName _program;
355  ShaderType _stage;
356 #endif
357 
358  Preset(
359 #ifndef NDEBUG
360  ProgramName program,
361  ShaderType stage,
362 #endif
363  const std::vector<GLuint>& indices
364  ): _indices(indices)
365 #ifndef NDEBUG
366  , _program(program)
367  , _stage(stage)
368 #endif
369  { }
370  public:
371  Preset(Preset&& tmp)
372  : _indices(std::move(tmp._indices))
373 #ifndef NDEBUG
374  , _program(tmp._program)
375  , _stage(tmp._stage)
376 #endif
377  { }
378  };
379 
381 
386  Preset Save(void)
387  {
388  return Preset(
389 #ifndef NDEBUG
390  _program,
391  _stage,
392 #endif
393  _get_indices()
394  );
395  }
396 
398 
405  void Load(const Preset& preset)
406  {
407  assert(_program == preset._program);
408  assert(_stage == preset._stage);
409  assert(_get_indices().size() == preset._indices.size());
410  _get_indices() = preset._indices;
411  }
412 
414 
421  void Apply(const Preset& preset)
422  {
423  assert(_program == preset._program);
424  assert(_stage == preset._stage);
425  assert(_get_indices().size() == preset._indices.size());
426 
427  OGLPLUS_GLFUNC(UniformSubroutinesuiv)(
428  GLenum(_stage),
429  GLsizei(preset._indices.size()),
430  preset._indices.data()
431  );
432  OGLPLUS_CHECK(
433  UniformSubroutinesuiv,
434  Error,
435  EnumParam(_stage)
436  );
437  }
438 
440 
443  void Apply(void)
444  {
445  OGLPLUS_GLFUNC(UniformSubroutinesuiv)(
446  GLenum(_stage),
447  GLsizei(_get_indices().size()),
448  _get_indices().data()
449  );
450  OGLPLUS_CHECK(
451  UniformSubroutinesuiv,
452  Error,
453  EnumParam(_stage)
454  );
455  }
456 };
457 
458 #endif // GL_ARB_shader_subroutine
459 
460 } // namespace oglplus
461 
462 #if !OGLPLUS_LINK_LIBRARY || defined(OGLPLUS_IMPLEMENTING_LIBRARY)
463 #include <oglplus/uniform_subroutines.ipp>
464 #endif // OGLPLUS_LINK_LIBRARY
465 
466 #endif // include guard
ProgVar< tag::ImplicitSel, tag::Subroutine, tag::NoTypecheck, void > Subroutine
Subroutine.
Definition: uniform_subroutines.hpp:140
Specialization of ProgVar for subroutine uniformss.
Definition: uniform_subroutines.hpp:224
const Char * c_str(void) const
Returns the null-terminated c-string.
Definition: ref_tpl.hpp:200
ShaderType
The type of a Shader.
Definition: shader_type.hpp:26
Program variable wrapper.
UniformSubroutines(ProgramName program, ShaderType stage)
Constructs a uniform subroutine setter for a stage of a program.
Definition: uniform_subroutines.hpp:305
Program variable location wrapper.
ProgVar(ProgramName program, ShaderType stage, StrCRef identifier, bool active_only)
Sub.uniform with the specified identifier in stage of program.
Definition: uniform_subroutines.hpp:239
Shader type enumeration.
void Apply(void)
Applies all changes made by Assign.
Definition: uniform_subroutines.hpp:443
Wrapper encapsulating program variable location/index.
Definition: fwd.hpp:191
Declaration of OGLplus object-related error.
UniformSubroutines & Assign(const SubroutineUniform &uniform, const Subroutine &subroutine)
Assigns the subroutine to the subroutine uniform.
Definition: uniform_subroutines.hpp:320
ProgVar(ProgramName program, ShaderType stage, StrCRef identifier, bool active_only)
Subroutine with the specified identifier in stage of program.
Definition: uniform_subroutines.hpp:129
GLuint GetGLName(ObjectName< ObjTag > named)
Returns the GLuint OpenGL name assigned to named object.
Definition: name.hpp:38
String reference.
void Apply(const Preset &preset)
Applies the setting from a preset without changing the current setting.
Definition: uniform_subroutines.hpp:421
Specialization of ProgVar for subroutines.
Definition: uniform_subroutines.hpp:114
This type stores a setting of the whole set of subroutine uniforms.
Definition: uniform_subroutines.hpp:347
String const reference wrapper template.
Definition: ref_tpl.hpp:72
ProgVar(SubroutineLoc pvloc)
Subroutine with the specified location.
Definition: uniform_subroutines.hpp:119
Helper macro for optional checking of availability of GL function.
Exception class for GL program-variable-related errors.
Definition: prog_var.hpp:22
bool IsActive(void) const
Returns true if the variable is active.
Definition: location.hpp:159
ProgVar(ProgramName program, ShaderType stage, StrCRef identifier)
Sub.uniform with the specified identifier in stage of program.
Definition: uniform_subroutines.hpp:234
void Load(const Preset &preset)
Loads the setting of subroutine uniforms from a preset.
Definition: uniform_subroutines.hpp:405
ProgVar< tag::ImplicitSel, tag::SubroutineUniform, tag::NoTypecheck, void > SubroutineUniform
SubroutineUniform.
Definition: uniform_subroutines.hpp:254
Preset Save(void)
Saves the current setting of subroutine uniforms into a preset.
Definition: uniform_subroutines.hpp:386
Exception class for general OpenGL errors.
Definition: basic.hpp:43
Exception class for GL object-related errors.
Definition: object.hpp:24
Program variable (vertex attrib / uniform ) wrapper.
Definition: fwd.hpp:222
Encapsulates the uniform subroutine setting operations.
Definition: uniform_subroutines.hpp:262
ProgVar(ProgramName program, ShaderType stage, StrCRef identifier)
Subroutine with the specified identifier in stage of program.
Definition: uniform_subroutines.hpp:124
ProgVar(SubroutineUniformLoc pvloc)
Subroutine uniform with the specified location.
Definition: uniform_subroutines.hpp:229
Declaration of OGLplus program-variable-related error.
GLint Location(void) const
Returns the location of the variable.
Definition: location.hpp:152
ProgramName Program(void) const
The program the variable belongs to.
Definition: location.hpp:145

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).