OGLplus  (0.59.0) a C++ wrapper for rendering APIs

basic_gl_api.hpp
Go to the documentation of this file.
1 #ifndef OGLPLUS_BASIC_GL_API_HPP
9 #define OGLPLUS_BASIC_GL_API_HPP
10 
11 #include "gl_api/api.hpp"
12 #include "gl_api/api_traits.hpp"
13 #include "gl_api/constants.hpp"
14 #include <eagine/integer_range.hpp>
15 #include <eagine/math/tmat.hpp>
16 //
17 #include "utils/image_file.hpp"
18 #include "utils/program_file.hpp"
19 
20 namespace eagine::oglp {
21 //------------------------------------------------------------------------------
26 template <typename ApiTraits>
28  : protected ApiTraits
29  , public basic_gl_operations<ApiTraits>
30  , public basic_gl_constants<ApiTraits> {
31 
32 public:
33  template <typename R>
34  using combined_result = typename ApiTraits::template combined_result<R>;
35 
36  using int_type = typename gl_types::int_type;
37  using float_type = typename gl_types::float_type;
38 
40  basic_gl_api(ApiTraits traits)
41  : ApiTraits{std::move(traits)}
42  , basic_gl_operations<ApiTraits>{*static_cast<ApiTraits*>(this)}
44  *static_cast<ApiTraits*>(this),
45  *static_cast<basic_gl_operations<ApiTraits>*>(this)} {}
46 
49  : basic_gl_api{ApiTraits{}} {}
50 
52  auto operations() noexcept -> basic_gl_operations<ApiTraits>& {
53  return *this;
54  }
55 
57  auto operations() const noexcept -> const basic_gl_operations<ApiTraits>& {
58  return *this;
59  }
60 
62  auto operator()() noexcept -> basic_gl_operations<ApiTraits>& {
63  return *this;
64  }
65 
67  auto operator()() const noexcept -> const basic_gl_operations<ApiTraits>& {
68  return *this;
69  }
70 
72  auto constants() noexcept -> basic_gl_constants<ApiTraits>& {
73  return *this;
74  }
75 
77  auto constants() const noexcept -> const basic_gl_constants<ApiTraits>& {
78  return *this;
79  }
80 
82  auto operator~() noexcept -> basic_gl_constants<ApiTraits>& {
83  return *this;
84  }
85 
87  auto operator~() const noexcept -> const basic_gl_constants<ApiTraits>& {
88  return *this;
89  }
90 
91  // convenience functions
92  constexpr auto true_or_false(bool b) const noexcept -> true_false {
93  return b ? true_false(this->true_) : true_false(this->false_);
94  }
95 
98  program_name prog,
99  const program_source_block& prog_src_blk) const -> combined_result<void> {
100  if(prog_src_blk.is_valid()) {
101  const span_size_t n = prog_src_blk.shader_source_count();
102  for(auto i : integer_range(n)) {
103  auto shdr_src_blk{prog_src_blk.shader_source(i)};
104  owned_shader_name shdr;
105  this->create_shader(shdr_src_blk.type()) >> shdr;
106  auto cleanup = this->delete_shader.raii(shdr);
107  this->shader_source(shdr, shdr_src_blk);
108  this->compile_shader(shdr);
109  this->attach_shader(prog, shdr);
110  }
111  }
112  return this->link_program(prog);
113  }
114 
116  auto build_program(program_name prog, memory::const_block prog_src_blk) const
117  -> combined_result<void> {
118  return build_program(prog, program_source_block(prog_src_blk));
119  }
120 
121  // set_uniform
122 private:
123  template <typename ProgramUniformFunc, typename UniformFunc, typename T>
124  auto _set_uniform(
125  ProgramUniformFunc& program_uniform_func,
126  UniformFunc& uniform_func,
127  program_name prog,
128  uniform_location loc,
129  T&& value) const -> combined_result<void> {
130  if(program_uniform_func) {
131  return program_uniform_func(prog, loc, std::forward<T>(value));
132  } else {
133  if(auto use_res{this->use_program(prog)}) {
134  return uniform_func(loc, std::forward<T>(value));
135  } else {
136  return use_res;
137  }
138  }
139  }
140 
141  template <typename ProgramUniformFunc, typename UniformFunc, typename T>
142  auto _set_uniform_matrix(
143  ProgramUniformFunc& program_uniform_func,
144  UniformFunc& uniform_func,
145  program_name prog,
146  uniform_location loc,
147  T&& value,
148  true_false transpose) const -> combined_result<void> {
149  if(program_uniform_func) {
150  return program_uniform_func(
151  prog, loc, transpose, std::forward<T>(value));
152  } else {
153  if(auto use_res{this->use_program(prog)}) {
154  return uniform_func(loc, transpose, std::forward<T>(value));
155  } else {
156  return use_res;
157  }
158  }
159  }
160 
161 public:
162  // int
163  auto
164  set_uniform(program_name prog, uniform_location loc, int_type value) const
165  -> combined_result<void> {
166  return _set_uniform(
167  this->program_uniform1i, this->uniform1i, prog, loc, value);
168  }
169 
170  auto set_uniform(
171  program_name prog,
172  uniform_location loc,
173  span<const int_type> value,
174  type_identity<int_type[1]>) const -> combined_result<void> {
175  return _set_uniform(
176  this->program_uniform1iv, this->uniform1iv, prog, loc, value);
177  }
178 
179  auto set_uniform(
180  program_name prog,
181  uniform_location loc,
182  span<const int_type> value,
183  type_identity<int_type[2]>) const -> combined_result<void> {
184  return _set_uniform(
185  this->program_uniform2iv, this->uniform2iv, prog, loc, value);
186  }
187 
188  auto set_uniform(
189  program_name prog,
190  uniform_location loc,
191  span<const int_type> value,
192  type_identity<int_type[3]>) const -> combined_result<void> {
193  return _set_uniform(
194  this->program_uniform3iv, this->uniform3iv, prog, loc, value);
195  }
196 
197  auto set_uniform(
198  program_name prog,
199  uniform_location loc,
200  span<const int_type> value,
201  type_identity<int_type[4]>) const -> combined_result<void> {
202  return _set_uniform(
203  this->program_uniform4iv, this->uniform4iv, prog, loc, value);
204  }
205 
206  // float
207  auto
208  set_uniform(program_name prog, uniform_location loc, float_type value) const
209  -> combined_result<void> {
210  return _set_uniform(
211  this->program_uniform1f, this->uniform1f, prog, loc, value);
212  }
213 
214  auto set_uniform(
215  program_name prog,
216  uniform_location loc,
217  span<const float_type> value,
218  type_identity<float_type[1]>) const -> combined_result<void> {
219  return _set_uniform(
220  this->program_uniform1fv, this->uniform1fv, prog, loc, value);
221  }
222 
223  auto set_uniform(
224  program_name prog,
225  uniform_location loc,
226  span<const float_type> value,
227  type_identity<float_type[2]>) const -> combined_result<void> {
228  return _set_uniform(
229  this->program_uniform2fv, this->uniform2fv, prog, loc, value);
230  }
231 
232  auto set_uniform(
233  program_name prog,
234  uniform_location loc,
235  span<const float_type> value,
236  type_identity<float_type[3]>) const -> combined_result<void> {
237  return _set_uniform(
238  this->program_uniform3fv, this->uniform3fv, prog, loc, value);
239  }
240 
241  auto set_uniform(
242  program_name prog,
243  uniform_location loc,
244  span<const float_type> value,
245  type_identity<float_type[4]>) const -> combined_result<void> {
246  return _set_uniform(
247  this->program_uniform4fv, this->uniform4fv, prog, loc, value);
248  }
249 
250  template <typename T>
251  auto
252  set_uniform(program_name prog, uniform_location loc, const T& value) const
253  -> std::enable_if_t<is_known_vector_type_v<T>, combined_result<void>> {
254  return set_uniform(
255  prog, loc, element_view(value), canonical_compound_type<T>());
256  }
257 
258  // uniform matrix
259  auto set_uniform_matrix(
260  program_name prog,
261  uniform_location loc,
262  span<const float_type> value,
263  true_false transpose,
264  type_identity<float_type[2][2]>) const -> combined_result<void> {
265  return _set_uniform_matrix(
266  this->program_uniform_matrix2fv,
267  this->uniform_matrix2fv,
268  prog,
269  loc,
270  value,
271  transpose);
272  }
273 
274  auto set_uniform_matrix(
275  program_name prog,
276  uniform_location loc,
277  span<const float_type> value,
278  true_false transpose,
279  type_identity<float_type[2][3]>) const -> combined_result<void> {
280  return _set_uniform_matrix(
281  this->program_uniform_matrix2x3fv,
282  this->uniform_matrix2x3fv,
283  prog,
284  loc,
285  value,
286  transpose);
287  }
288 
289  auto set_uniform_matrix(
290  program_name prog,
291  uniform_location loc,
292  span<const float_type> value,
293  true_false transpose,
294  type_identity<float_type[2][4]>) const -> combined_result<void> {
295  return _set_uniform_matrix(
296  this->program_uniform_matrix2x4fv,
297  this->uniform_matrix2x4fv,
298  prog,
299  loc,
300  value,
301  transpose);
302  }
303 
304  auto set_uniform_matrix(
305  program_name prog,
306  uniform_location loc,
307  span<const float_type> value,
308  true_false transpose,
309  type_identity<float_type[3][2]>) const -> combined_result<void> {
310  return _set_uniform_matrix(
311  this->program_uniform_matrix3x2fv,
312  this->uniform_matrix3x2fv,
313  prog,
314  loc,
315  value,
316  transpose);
317  }
318 
319  auto set_uniform_matrix(
320  program_name prog,
321  uniform_location loc,
322  span<const float_type> value,
323  true_false transpose,
324  type_identity<float_type[3][3]>) const -> combined_result<void> {
325  return _set_uniform_matrix(
326  this->program_uniform_matrix3fv,
327  this->uniform_matrix3fv,
328  prog,
329  loc,
330  value,
331  transpose);
332  }
333 
334  auto set_uniform_matrix(
335  program_name prog,
336  uniform_location loc,
337  span<const float_type> value,
338  true_false transpose,
339  type_identity<float_type[3][4]>) const -> combined_result<void> {
340  return _set_uniform_matrix(
341  this->program_uniform_matrix3x4fv,
342  this->uniform_matrix3x4fv,
343  prog,
344  loc,
345  value,
346  transpose);
347  }
348 
349  auto set_uniform_matrix(
350  program_name prog,
351  uniform_location loc,
352  span<const float_type> value,
353  true_false transpose,
354  type_identity<float_type[4][2]>) const -> combined_result<void> {
355  return _set_uniform_matrix(
356  this->program_uniform_matrix4x2fv,
357  this->uniform_matrix4x2fv,
358  prog,
359  loc,
360  value,
361  transpose);
362  }
363 
364  auto set_uniform_matrix(
365  program_name prog,
366  uniform_location loc,
367  span<const float_type> value,
368  true_false transpose,
369  type_identity<float_type[4][3]>) const -> combined_result<void> {
370  return _set_uniform_matrix(
371  this->program_uniform_matrix4x3fv,
372  this->uniform_matrix4x3fv,
373  prog,
374  loc,
375  value,
376  transpose);
377  }
378 
379  auto set_uniform_matrix(
380  program_name prog,
381  uniform_location loc,
382  span<const float_type> value,
383  true_false transpose,
384  type_identity<float_type[4][4]>) const -> combined_result<void> {
385  return _set_uniform_matrix(
386  this->program_uniform_matrix4fv,
387  this->uniform_matrix4fv,
388  prog,
389  loc,
390  value,
391  transpose);
392  }
393 
394  template <typename T>
395  auto
396  set_uniform(program_name prog, uniform_location loc, const T& value) const
397  -> std::enable_if_t<is_known_matrix_type_v<T>, combined_result<void>> {
398  return set_uniform_matrix(
399  prog,
400  loc,
401  element_view(value),
402  true_or_false(math::is_row_major_v<T>),
403  canonical_compound_type<T>());
404  }
405 
406  // texture image
407  auto spec_tex_image1d(
408  texture_target tex_tgt,
409  gl_types::int_type level,
410  gl_types::int_type border,
411  const image_spec& image) -> combined_result<void> {
412  return this->tex_image1d(
413  tex_tgt,
414  level,
415  image.internal_format(),
416  image.width(),
417  border,
418  image.format(),
419  image.type(),
420  image.data());
421  }
422 
423  auto spec_tex_image2d(
424  texture_target tex_tgt,
425  gl_types::int_type level,
426  gl_types::int_type border,
427  const image_spec& image) -> combined_result<void> {
428  return this->tex_image2d(
429  tex_tgt,
430  level,
431  image.internal_format(),
432  image.width(),
433  image.height(),
434  border,
435  image.format(),
436  image.type(),
437  image.data());
438  }
439 
440  auto spec_tex_image3d(
441  texture_target tex_tgt,
442  gl_types::int_type level,
443  gl_types::int_type border,
444  const image_spec& image) -> combined_result<void> {
445  return this->tex_image3d(
446  tex_tgt,
447  level,
448  image.internal_format(),
449  image.width(),
450  image.height(),
451  image.depth(),
452  border,
453  image.format(),
454  image.type(),
455  image.data());
456  }
457 };
458 
459 template <std::size_t I, typename ApiTraits>
460 auto get(basic_gl_api<ApiTraits>& x) noexcept ->
461  typename std::tuple_element<I, basic_gl_api<ApiTraits>>::type& {
462  return x;
463 }
464 
465 template <std::size_t I, typename ApiTraits>
466 auto get(const basic_gl_api<ApiTraits>& x) noexcept -> const
467  typename std::tuple_element<I, basic_gl_api<ApiTraits>>::type& {
468  return x;
469 }
470 //------------------------------------------------------------------------------
471 template <typename A>
472 auto translate(const basic_gl_api<A>& api, bool value) noexcept -> true_false {
473  if(value) {
474  return api.true_;
475  }
476  return api.false_;
477 }
478 //------------------------------------------------------------------------------
479 } // namespace eagine::oglp
480 
481 // NOLINTNEXTLINE(cert-dcl58-cpp)
482 namespace std {
483 //------------------------------------------------------------------------------
484 template <typename ApiTraits>
485 struct tuple_size<eagine::oglp::basic_gl_api<ApiTraits>>
486  : public std::integral_constant<std::size_t, 2> {};
487 
488 template <typename ApiTraits>
489 struct tuple_element<0, eagine::oglp::basic_gl_api<ApiTraits>> {
491 };
492 
493 template <typename ApiTraits>
494 struct tuple_element<1, eagine::oglp::basic_gl_api<ApiTraits>> {
496 };
497 //------------------------------------------------------------------------------
498 } // namespace std
499 
500 #endif // OGLPLUS_BASIC_GL_API_HPP
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
prog_var_location< EAGINE_ID_V(Uniform)> uniform_location
Alias for shader program uniform location wrapper.
Definition: prog_var_loc.hpp:115
Common code is placed in this namespace.
Definition: eagine.hpp:21
opt_c_api_constant< mp_list< oglp::true_false >, bool_type_i > false_
Definition: constants.hpp:234
auto operator()() const noexcept -> const basic_gl_operations< ApiTraits > &
Returns a reference to the wrapped operations.
Definition: basic_gl_api.hpp:67
GLint int_type
Signed integer type.
Definition: config.hpp:70
auto build_program(program_name prog, memory::const_block prog_src_blk) const -> combined_result< void >
Builds a shader program from the sources in a baked memory block.
Definition: basic_gl_api.hpp:116
auto operator~() noexcept -> basic_gl_constants< ApiTraits > &
Returns a reference to the wrapped constants.
Definition: basic_gl_api.hpp:82
gl_object_name< program_tag > program_name
Alias for GL program object handle.
Definition: object_name.hpp:128
Wrapper for true, false GL enums.
Definition: enum_types.hpp:17
auto constants() const noexcept -> const basic_gl_constants< ApiTraits > &
Returns a reference to the wrapped constants.
Definition: basic_gl_api.hpp:77
Non-owning wrapper for C-API opaque handle types.
Definition: handle.hpp:26
auto operations() const noexcept -> const basic_gl_operations< ApiTraits > &
Returns a reference to the wrapped operations.
Definition: basic_gl_api.hpp:57
auto build_program(program_name prog, const program_source_block &prog_src_blk) const -> combined_result< void >
Builds a shader program from the specified sources.
Definition: basic_gl_api.hpp:97
Class wrapping the functions from the GL API.
Definition: api.hpp:32
opt_c_api_constant< mp_list< texture_parameter >, enum_type_i, oglp::texture_target > texture_target
Definition: constants.hpp:3085
Non-owning view of a contiguous range of memory with ValueType elements.
Definition: flatten_fwd.hpp:16
auto operator~() const noexcept -> const basic_gl_constants< ApiTraits > &
Returns a reference to the wrapped constants.
Definition: basic_gl_api.hpp:87
opt_c_api_constant< mp_list< oglp::true_false >, bool_type_i > true_
Definition: constants.hpp:223
GLfloat float_type
Floating-point type.
Definition: config.hpp:82
basic_gl_api()
Default constructor.
Definition: basic_gl_api.hpp:48
Class wrapping the constants from the GL API.
Definition: constants.hpp:26
auto constants() noexcept -> basic_gl_constants< ApiTraits > &
Returns a reference to the wrapped constants.
Definition: basic_gl_api.hpp:72
Combined wrapper for the GL API operations and constants.
Definition: basic_gl_api.hpp:27
Owning wrapper for C-API opaque handle types.
Definition: handle.hpp:98
integer_range(B, E) -> integer_range< std::common_type_t< B, E >>
Deduction guide for integer_range.
basic_gl_api(ApiTraits traits)
Constructor using API traits..
Definition: basic_gl_api.hpp:40
auto operator()() noexcept -> basic_gl_operations< ApiTraits > &
Returns a reference to the wrapped operations.
Definition: basic_gl_api.hpp:62
static auto transpose(const matrix< T, C, R, RM, V > &m) noexcept -> matrix< T, R, C, RM, V >
Transposes a matrix.
Definition: matrix.hpp:292
auto operations() noexcept -> basic_gl_operations< ApiTraits > &
Returns a reference to the wrapped operations.
Definition: basic_gl_api.hpp:52

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).