1 #ifndef OGLPLUS_BASIC_GL_API_HPP
9 #define OGLPLUS_BASIC_GL_API_HPP
11 #include "gl_api/api.hpp"
12 #include "gl_api/api_traits.hpp"
13 #include "gl_api/constants.hpp"
20 namespace eagine::oglp {
26 template <
typename ApiTraits>
34 using combined_result =
typename ApiTraits::template combined_result<R>;
41 : ApiTraits{std::move(traits)}
44 *
static_cast<ApiTraits*
>(
this),
92 constexpr
auto true_or_false(
bool b)
const noexcept ->
true_false {
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();
103 auto shdr_src_blk{prog_src_blk.shader_source(i)};
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);
112 return this->link_program(prog);
117 -> combined_result<void> {
118 return build_program(prog, program_source_block(prog_src_blk));
123 template <
typename ProgramUniformFunc,
typename UniformFunc,
typename T>
125 ProgramUniformFunc& program_uniform_func,
126 UniformFunc& uniform_func,
129 T&& value)
const -> combined_result<void> {
130 if(program_uniform_func) {
131 return program_uniform_func(prog, loc, std::forward<T>(value));
133 if(
auto use_res{this->use_program(prog)}) {
134 return uniform_func(loc, std::forward<T>(value));
141 template <
typename ProgramUniformFunc,
typename UniformFunc,
typename T>
142 auto _set_uniform_matrix(
143 ProgramUniformFunc& program_uniform_func,
144 UniformFunc& uniform_func,
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));
153 if(
auto use_res{this->use_program(prog)}) {
154 return uniform_func(loc,
transpose, std::forward<T>(value));
165 -> combined_result<void> {
167 this->program_uniform1i, this->uniform1i, prog, loc, value);
173 span<const int_type> value,
174 type_identity<int_type[1]>) const -> combined_result<
void> {
176 this->program_uniform1iv, this->uniform1iv, prog, loc, value);
182 span<const int_type> value,
183 type_identity<int_type[2]>) const -> combined_result<
void> {
185 this->program_uniform2iv, this->uniform2iv, prog, loc, value);
191 span<const int_type> value,
192 type_identity<int_type[3]>) const -> combined_result<
void> {
194 this->program_uniform3iv, this->uniform3iv, prog, loc, value);
200 span<const int_type> value,
201 type_identity<int_type[4]>) const -> combined_result<
void> {
203 this->program_uniform4iv, this->uniform4iv, prog, loc, value);
209 -> combined_result<void> {
211 this->program_uniform1f, this->uniform1f, prog, loc, value);
217 span<const float_type> value,
218 type_identity<float_type[1]>) const -> combined_result<
void> {
220 this->program_uniform1fv, this->uniform1fv, prog, loc, value);
226 span<const float_type> value,
227 type_identity<float_type[2]>) const -> combined_result<
void> {
229 this->program_uniform2fv, this->uniform2fv, prog, loc, value);
235 span<const float_type> value,
236 type_identity<float_type[3]>) const -> combined_result<
void> {
238 this->program_uniform3fv, this->uniform3fv, prog, loc, value);
244 span<const float_type> value,
245 type_identity<float_type[4]>) const -> combined_result<
void> {
247 this->program_uniform4fv, this->uniform4fv, prog, loc, value);
250 template <
typename T>
253 -> std::enable_if_t<is_known_vector_type_v<T>, combined_result<void>> {
255 prog, loc, element_view(value), canonical_compound_type<T>());
259 auto set_uniform_matrix(
262 span<const float_type> value,
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,
274 auto set_uniform_matrix(
277 span<const float_type> value,
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,
289 auto set_uniform_matrix(
292 span<const float_type> value,
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,
304 auto set_uniform_matrix(
307 span<const float_type> value,
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,
319 auto set_uniform_matrix(
322 span<const float_type> value,
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,
334 auto set_uniform_matrix(
337 span<const float_type> value,
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,
349 auto set_uniform_matrix(
352 span<const float_type> value,
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,
364 auto set_uniform_matrix(
367 span<const float_type> value,
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,
379 auto set_uniform_matrix(
382 span<const float_type> value,
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,
394 template <
typename T>
397 -> std::enable_if_t<is_known_matrix_type_v<T>, combined_result<void>> {
398 return set_uniform_matrix(
402 true_or_false(math::is_row_major_v<T>),
403 canonical_compound_type<T>());
407 auto spec_tex_image1d(
411 const image_spec& image) -> combined_result<void> {
412 return this->tex_image1d(
415 image.internal_format(),
423 auto spec_tex_image2d(
427 const image_spec& image) -> combined_result<void> {
428 return this->tex_image2d(
431 image.internal_format(),
440 auto spec_tex_image3d(
444 const image_spec& image) -> combined_result<void> {
445 return this->tex_image3d(
448 image.internal_format(),
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& {
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& {
471 template <
typename A>
472 auto translate(
const basic_gl_api<A>& api,
bool value) noexcept -> true_false {
484 template <
typename ApiTraits>
485 struct tuple_size<
eagine::oglp::basic_gl_api<ApiTraits>>
486 :
public std::integral_constant<std::size_t, 2> {};
488 template <
typename ApiTraits>
489 struct tuple_element<0,
eagine::oglp::basic_gl_api<ApiTraits>> {
493 template <
typename ApiTraits>
494 struct tuple_element<1,
eagine::oglp::basic_gl_api<ApiTraits>> {
500 #endif // OGLPLUS_BASIC_GL_API_HPP