OGLplus (0.52.0) a C++ wrapper for OpenGL

shader.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_SHADER_1107121519_HPP
14 #define OGLPLUS_SHADER_1107121519_HPP
15 
16 #include <oglplus/glfunc.hpp>
18 #include <oglplus/object/array.hpp>
21 #include <oglplus/shader_type.hpp>
22 #include <oglplus/glsl_source.hpp>
23 
24 #include <array>
25 #include <vector>
26 #include <cassert>
27 
28 namespace oglplus {
29 
31 
38 template <>
39 class ObjGenDelOps<tag::Shader>
40 {
41 protected:
42  static void Gen(tag::Create, GLsizei count, GLuint* names, GLenum type)
43  {
44  assert(names != nullptr);
45  for(GLsizei i=0; i<count; ++i)
46  {
47  names[i] = OGLPLUS_GLFUNC(CreateShader)(type);
48  OGLPLUS_CHECK_SIMPLE(CreateShader);
49  }
50  }
51 
52  GLenum _type;
53 
54  void Gen(tag::Create create, GLsizei count, GLuint* names) const
55  {
56  Gen(create, count, names, _type);
57  }
58 
59  static void Delete(GLsizei count, GLuint* names)
60  {
61  assert(names != nullptr);
62  for(GLsizei i=0; i<count; ++i)
63  {
64  OGLPLUS_GLFUNC(DeleteShader)(names[i]);
65  OGLPLUS_VERIFY_SIMPLE(DeleteShader);
66  }
67  }
68 
69  static GLboolean IsA(GLuint name)
70  {
71  assert(name != 0);
72  GLboolean result = OGLPLUS_GLFUNC(IsShader)(name);
73  OGLPLUS_VERIFY_SIMPLE(IsShader);
74  return result;
75  }
76 };
77 
78 template <>
79 struct ObjGenTag<tag::DirectState, tag::Shader>
80 {
81  typedef tag::Create Type;
82 };
83 
85 
87 template <>
88 class ObjCommonOps<tag::Shader>
89  : public ShaderName
90 {
91 protected:
92  ObjCommonOps(void) { }
93 
94 #if OGLPLUS_DOCUMENTATION_ONLY || \
95  GL_ES_VERSION_3_0 || \
96  GL_VERSION_4_1 || \
97  GL_ARB_ES2_compatibility
98 
104  static void PrecisionFormat(
105  ShaderType shader_type,
106  PrecisionType precision_type,
107  GLint* range_log_2,
108  GLint* precision_log_2
109  )
110  {
111  OGLPLUS_GLFUNC(GetShaderPrecisionFormat)(
112  GLenum(shader_type),
113  GLenum(precision_type),
114  range_log_2,
115  precision_log_2
116  );
117  OGLPLUS_VERIFY_SIMPLE(GetShaderPrecisionFormat);
118  }
119 #endif
120 };
121 
123 
125 template <>
126 class ObjectOps<tag::DirectState, tag::Shader>
127  : public ObjCommonOps<tag::Shader>
128 {
129 protected:
130  ObjectOps(void){ }
131 public:
133  struct Property
134  {
136  typedef ShaderType Type;
137  };
138 
140 
145  ShaderType Type(void) const
146  {
147  GLint result = 0;
148  OGLPLUS_GLFUNC(GetShaderiv)(
149  _name,
150  GL_SHADER_TYPE,
151  &result
152  );
153  OGLPLUS_VERIFY(
154  GetShaderiv,
155  ObjectError,
156  Object(*this)
157  );
158  return ShaderType(result);
159  }
160 
162 
167  const GLsizei count,
168  const GLchar* const * srcs,
169  const GLint* lens
170  )
171  {
172  assert(_name != 0);
173  OGLPLUS_GLFUNC(ShaderSource)(
174  _name,
175  count,
176  const_cast<const GLchar**>(srcs),
177  lens
178  );
179  OGLPLUS_VERIFY(
180  ShaderSource,
181  ObjectError,
182  Object(*this)
183  );
184  return *this;
185  }
186 
187  template <typename Src>
188  ObjectOps& SourceTpl(const Src& src)
189  {
190  return Source(src.Count(), src.Parts(), src.Lengths());
191  }
192 
194 
199  {
200  return SourceTpl(source);
201  }
202 
204 
209  {
210  return SourceTpl(source);
211  }
212 
214 
218  ObjectOps& Source(const GLSLSource& glsl_source)
219  {
220  return SourceTpl(glsl_source);
221  }
222 
224 
231  bool IsCompiled(void) const
232  {
233  assert(_name != 0);
234  int status;
235  OGLPLUS_GLFUNC(GetShaderiv)(_name, GL_COMPILE_STATUS, &status);
236  OGLPLUS_VERIFY(
237  GetShaderiv,
238  ObjectError,
239  Object(*this).
240  EnumParam(Type())
241  );
242  return status == GL_TRUE;
243  }
244 
246 
254  String GetInfoLog(void) const;
255 
257 
265  ObjectOps& Compile(void);
266 
267 
268 #if OGLPLUS_DOCUMENTATION_ONLY || \
269  GL_ARB_shading_language_include
270 
272 
281  ObjectOps& CompileInclude(
282  GLsizei count,
283  const GLchar* const* paths,
284  const GLint* lengths
285  );
286 
288 
298  {
299  return CompileInclude(
300  incl.Count(),
301  incl.Parts(),
302  incl.Lengths()
303  );
304  }
305 
306  ObjectOps& CompileInclude(GLSLStrings&& incl)
307  {
308  return CompileInclude(
309  incl.Count(),
310  incl.Parts(),
311  incl.Lengths()
312  );
313  }
314 
315  ObjectOps& CompileInclude(const GLSLSource& incl)
316  {
317  return CompileInclude(
318  incl.Count(),
319  incl.Parts(),
320  incl.Lengths()
321  );
322  }
323 #endif
324 
325 #if OGLPLUS_DOCUMENTATION_ONLY || \
326  GL_ES_VERSION_3_0 || \
327  GL_VERSION_4_1 || \
328  GL_ARB_ES2_compatibility
329 
335  static void ReleaseCompiler(void)
336  {
337  OGLPLUS_GLFUNC(ReleaseShaderCompiler)();
338  OGLPLUS_VERIFY_SIMPLE(ReleaseShaderCompiler);
339  }
340 #endif
341 };
342 
344 typedef ObjectOps<tag::DirectState, tag::Shader>
346 
347 template <>
348 struct ObjectSubtype<tag::Shader>
349 {
350  typedef ShaderType Type;
351 };
352 
354 
363 class Shader
364  : public Object<ShaderOps>
365 {
366 private:
367  Shader(const Shader&); // = delete;
368 protected:
369  using Object<ShaderOps>::Uninitialized_;
370 
372  Shader(Uninitialized_ u)
373  : Object<ShaderOps>(u)
374  { }
375 public:
378  : Object<ShaderOps>(type)
379  { }
380 
382  Shader(ShaderType type, ObjectDesc&& description)
383  : Object<ShaderOps>(type, std::move(description))
384  { }
385 
388  ShaderType type,
389  GLSLString&& glsl_source
390  ): Object<ShaderOps>(type)
391  {
392  this->Source(std::move(glsl_source));
393  this->Compile();
394  }
395 
398  ShaderType type,
399  ObjectDesc&& description,
400  GLSLString&& glsl_source
401  ): Object<ShaderOps>(type, std::move(description))
402  {
403  this->Source(std::move(glsl_source));
404  this->Compile();
405  }
406 
409  ShaderType type,
410  GLSLStrings&& glsl_source
411  ): Object<ShaderOps>(type)
412  {
413  this->Source(std::move(glsl_source));
414  this->Compile();
415  }
416 
419  ShaderType type,
420  ObjectDesc&& description,
421  GLSLStrings&& glsl_source
422  ): Object<ShaderOps>(type, std::move(description))
423  {
424  this->Source(std::move(glsl_source));
425  this->Compile();
426  }
427 
430  ShaderType type,
431  const GLSLSource& glsl_source
432  ): Object<ShaderOps>(type)
433  {
434  this->Source(glsl_source);
435  this->Compile();
436  }
437 
440  ShaderType type,
441  ObjectDesc&& description,
442  const GLSLSource& glsl_source
443  ): Object<ShaderOps>(type, std::move(description))
444  {
445  this->Source(glsl_source);
446  this->Compile();
447  }
448 
450  Shader(Shader&& temp)
451  : Object<ShaderOps>(static_cast<Object<ShaderOps>&&>(temp))
452  { }
453 
454  Shader& operator = (Shader&& temp)
455  {
456  Object<ShaderOps>::operator = (std::move(temp));
457  return *this;
458  }
459 };
460 
461 template <>
462 struct Classify<Shader>
463  : Classify<Object<ShaderOps>>
464 { };
465 
466 template <>
467 class Array<Shader>
468  : public Array<ObjectOps<tag::DirectState, tag::Shader>>
469 {
470 public:
471  Array(GLsizei n, ShaderType type)
472  : Array<ObjectOps<tag::DirectState, tag::Shader>>(n, type)
473  { }
474 };
475 
477 template <ShaderType ShType>
479  : public Shader
480 {
481 private:
482  SpecShader(const SpecShader&); // = delete;
483 public:
486  : Shader(ShType)
487  { }
488 
490  SpecShader(ObjectDesc&& description)
491  : Shader(ShType, std::move(description))
492  { }
493 
495  SpecShader(GLSLString&& glsl_source)
496  : Shader(ShType, std::move(glsl_source))
497  { }
498 
501  ObjectDesc&& description,
502  GLSLString&& glsl_source
503  ): Shader(ShType, std::move(description), std::move(glsl_source))
504  { }
505 
507  SpecShader(GLSLStrings&& glsl_source)
508  : Shader(ShType, std::move(glsl_source))
509  { }
510 
513  ObjectDesc&& description,
514  GLSLStrings&& glsl_source
515  ): Shader(ShType, std::move(description), std::move(glsl_source))
516  { }
517 
519  SpecShader(const GLSLSource& glsl_source)
520  : Shader(ShType, glsl_source)
521  { }
522 
525  ObjectDesc&& description,
526  const GLSLSource& glsl_source
527  ): Shader(ShType, std::move(description), glsl_source)
528  { }
529 
530  SpecShader(SpecShader&& temp)
531  : Shader(static_cast<Shader&&>(temp))
532  { }
533 };
534 
536 
542 
543 #if OGLPLUS_DOCUMENTATION_ONLY || GL_GEOMETRY_SHADER
544 
551 #endif
552 
554 
560 
561 #if OGLPLUS_DOCUMENTATION_ONLY || GL_TESS_CONTROL_SHADER
562 
569 #endif
570 
571 #if OGLPLUS_DOCUMENTATION_ONLY || GL_TESS_EVALUATION_SHADER
572 
579 #endif
580 
581 #if OGLPLUS_DOCUMENTATION_ONLY || GL_COMPUTE_SHADER
582 
589 #endif
590 
591 } // namespace oglplus
592 
593 #if !OGLPLUS_LINK_LIBRARY || defined(OGLPLUS_IMPLEMENTING_LIBRARY)
594 #include <oglplus/shader.ipp>
595 #endif // OGLPLUS_LINK_LIBRARY
596 
597 #endif // include guard
ShaderType
The type of a Shader.
Definition: shader_type.hpp:26
ObjectOps< tag::DirectState, tag::Shader > ShaderOps
Shader operations (with direct state access)
Definition: shader.hpp:345
Shader(ShaderType type, ObjectDesc &&description, GLSLStrings &&glsl_source)
Construction with type, description and source code wrapper.
Definition: shader.hpp:418
OpenGL precision type-related declarations.
Shader(ShaderType type, GLSLString &&glsl_source)
Construction with type and source code wrapper.
Definition: shader.hpp:387
Shader(ShaderType type, ObjectDesc &&description)
Construction with type specifier and textual descriptor.
Definition: shader.hpp:382
ObjectOps & Source(GLSLStrings &&source)
Set the source code of the shader.
Definition: shader.hpp:208
Shader type enumeration.
SpecShader(GLSLStrings &&glsl_source)
Construction with a source code wrapper.
Definition: shader.hpp:507
SpecShader< ShaderType::Geometry > GeometryShader
Geometry shader wrapper.
Definition: shader.hpp:550
SpecShader< ShaderType::Vertex > VertexShader
Vertex shader wrapper.
Definition: shader.hpp:541
Shader(ShaderType type, ObjectDesc &&description, GLSLString &&glsl_source)
Construction with type, description and source code wrapper.
Definition: shader.hpp:397
Class storing source code in GLSL.
Definition: glsl_source.hpp:22
SpecShader< ShaderType::Fragment > FragmentShader
Fragment shader wrapper.
Definition: shader.hpp:559
Program errors.
Generic OpenGL object wrapper.
Shader(ShaderType type, GLSLStrings &&glsl_source)
Construction with type and source code wrapper.
Definition: shader.hpp:408
SpecShader< ShaderType::Compute > ComputeShader
Compute shader wrapper.
Definition: shader.hpp:588
Implements operations applicable to named (non-zero) objects.
Definition: wrap_tpl.hpp:45
SpecShader(GLSLString &&glsl_source)
Construction with a source code wrapper.
Definition: shader.hpp:495
Shader(ShaderType type, const GLSLSource &glsl_source)
Construction with type and source code wrapper.
Definition: shader.hpp:429
SpecShader< ShaderType::TessControl > TessControlShader
Tesselation control shader wrapper.
Definition: shader.hpp:568
SpecShader(ObjectDesc &&description)
Construction with a textual descriptor.
Definition: shader.hpp:490
ShaderType Type
The type of a Shader.
Definition: shader.hpp:136
Class for passing a set of strings as a Source to a Shader.
Definition: glsl_string.hpp:94
PrecisionType
Precision type enumeration.
Definition: precision_type.hpp:27
bool IsCompiled(void) const
Returns true if the shader is already compiled, returns false otherwise.
Definition: shader.hpp:231
Helper macro for optional checking of availability of GL function.
SpecShader(ObjectDesc &&description, const GLSLSource &glsl_source)
Construction with description and source code wrapper.
Definition: shader.hpp:524
Array data structure.
ObjectOps & CompileInclude(GLSLString &&incl)
Compiles the shader using the specified include paths.
Definition: shader.hpp:297
ObjectOps & Source(GLSLString &&source)
Set the source code of the shader.
Definition: shader.hpp:198
ShaderType Type(void) const
Get the type of the shader.
Definition: shader.hpp:145
Shader(Uninitialized_ u)
Uninitialized construction.
Definition: shader.hpp:372
::std::basic_string< GLchar > String
String class.
Definition: def.hpp:36
ObjectOps & Source(const GLsizei count, const GLchar *const *srcs, const GLint *lens)
Set the source code of the shader.
Definition: shader.hpp:166
An object encasulating the shader object functionality.
Definition: shader.hpp:363
Object< SourceOps > Source
SpecShader(ObjectDesc &&description, GLSLStrings &&glsl_source)
Construction with description and source code wrapper.
Definition: shader.hpp:512
ObjectOps & Source(const GLSLSource &glsl_source)
Set the source code of the shader.
Definition: shader.hpp:218
SpecShader< ShaderType::TessEvaluation > TessEvaluationShader
Tesselation evaluation shader wrapper.
Definition: shader.hpp:578
SpecShader(ObjectDesc &&description, GLSLString &&glsl_source)
Construction with description and source code wrapper.
Definition: shader.hpp:500
Exception class for GL object-related errors.
Definition: object.hpp:24
static void ReleaseCompiler(void)
Indicate that the resources associated with the compiler can be freed.
Definition: shader.hpp:335
Base template for specialized shader types.
Definition: shader.hpp:478
Helper class storing source code in GLSL.
Shader(ShaderType type)
Construction with shader type specifier.
Definition: shader.hpp:377
static void PrecisionFormat(ShaderType shader_type, PrecisionType precision_type, GLint *range_log_2, GLint *precision_log_2)
Get the shader precision format.
Definition: shader.hpp:104
Implements operations applicable to any object and any operation kind.
Definition: fwd.hpp:151
Class for passing a single string as a Source to a Shader.
Definition: glsl_string.hpp:31
SpecShader(const GLSLSource &glsl_source)
Construction with a source code wrapper.
Definition: shader.hpp:519
A common template for "named" objects like textures, buffers, etc.
Definition: fwd.hpp:136
SpecShader(void)
Default construction.
Definition: shader.hpp:485
Class wrapping shader functions (with direct state access)
Definition: shader.hpp:126
Shader(Shader &&temp)
Shaders are movable.
Definition: shader.hpp:450
Shader(ShaderType type, ObjectDesc &&description, const GLSLSource &glsl_source)
Construction with type, description and source code wrapper.
Definition: shader.hpp:439

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