summaryrefslogtreecommitdiff
path: root/lib/gl_traits.h
blob: b3c69097648302ea346e13f0d3556101bafb6b23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#pragma once

#include <array>
#include <glad/gl.h>
#include <glm/common.hpp>
#include <glm/fwd.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <span>

template<typename T> struct gl_traits;

struct gl_traits_base {
	static constexpr GLint size {1};
};

struct gl_traits_float : public gl_traits_base {
	static constexpr auto vertexAttribFunc {
			[](GLuint index, GLint size, GLenum type, GLsizei stride, const void * pointer) -> GLuint {
				glVertexAttribPointer(index, size, type, GL_FALSE, stride, pointer);
				return 1;
			}};
};

struct gl_traits_longfloat : public gl_traits_base {
	static constexpr auto vertexAttribFunc {
			[](GLuint index, GLint size, GLenum type, GLsizei stride, const void * pointer) -> GLuint {
				glVertexAttribLPointer(index, size, type, stride, pointer);
				return 1;
			}};
};

struct gl_traits_integer : public gl_traits_base {
	static constexpr auto vertexAttribFunc {
			[](GLuint index, GLint size, GLenum type, GLsizei stride, const void * pointer) -> GLuint {
				glVertexAttribIPointer(index, size, type, stride, pointer);
				return 1;
			}};
};

template<> struct gl_traits<glm::f32> : public gl_traits_float {
	static constexpr GLenum type {GL_FLOAT};
	static constexpr auto glUniformFunc {&glUniform1f};
	static constexpr std::array glUniformvFunc {&glUniform1fv, &glUniform2fv, &glUniform3fv, &glUniform4fv};
	static constexpr std::array glUniformmFunc {&glUniformMatrix2fv, &glUniformMatrix3fv, &glUniformMatrix4fv};
	static constexpr auto glTexParameterFunc {&glTexParameterf};
	static constexpr auto glTexParameterfFunc {&glTexParameterfv};
};

template<> struct gl_traits<glm::f64> : public gl_traits_longfloat {
	static constexpr GLenum type {GL_DOUBLE};
};

template<> struct gl_traits<glm::int8> : public gl_traits_integer {
	static constexpr GLenum type {GL_BYTE};
};

template<> struct gl_traits<glm::int16> : public gl_traits_integer {
	static constexpr GLenum type {GL_SHORT};
};

template<> struct gl_traits<glm::int32> : public gl_traits_integer {
	static constexpr GLenum type {GL_INT};
	static constexpr auto glUniformFunc {&glUniform1i};
	static constexpr std::array glUniformvFunc {&glUniform1iv, &glUniform2iv, &glUniform3iv, &glUniform4iv};
	static constexpr auto glTexParameterFunc {&glTexParameteri};
	static constexpr auto glTexParameterfFunc {&glTexParameteriv};
};

template<> struct gl_traits<glm::uint8> : public gl_traits_integer {
	static constexpr GLenum type {GL_UNSIGNED_BYTE};
};

template<> struct gl_traits<glm::uint16> : public gl_traits_integer {
	static constexpr GLenum type {GL_UNSIGNED_SHORT};
};

template<> struct gl_traits<glm::uint32> : public gl_traits_integer {
	static constexpr GLenum type {GL_UNSIGNED_INT};
	static constexpr auto glUniformFunc {&glUniform1ui};
	static constexpr std::array<decltype(&glUniform1uiv), 5> glUniformvFunc {
			&glUniform1uiv, &glUniform2uiv, &glUniform3uiv, &glUniform4uiv};
};

template<typename T, std::size_t S> struct gl_traits<std::array<T, S>> : public gl_traits<T> {
	static constexpr GLint size {S * gl_traits<T>::size};
	static constexpr auto vertexAttribFunc {
			[](GLuint index, GLint, GLenum type, GLsizei stride, const void * pointer) -> GLuint {
				const auto base = static_cast<const T *>(pointer);
				for (GLuint e = 0; e < S; e++) {
					glVertexAttribPointer(index + e, gl_traits<T>::size, type, GL_FALSE, stride, base + e);
				}
				return S;
			}};
};

template<glm::length_t L, typename T, glm::qualifier Q> struct gl_traits<glm::vec<L, T, Q>> : public gl_traits<T> {
	static constexpr GLint size {L};
};

template<glm::length_t C, glm::length_t R, typename T, glm::qualifier Q>
struct gl_traits<glm::mat<C, R, T, Q>> : public gl_traits<T> {
	static constexpr GLint size {C * R};
	static constexpr auto vertexAttribFunc {
			[](GLuint index, GLint, GLenum type, GLsizei stride, const void * pointer) -> GLuint {
				const auto base = static_cast<const T *>(pointer);
				for (GLuint r = 0; r < R; r++) {
					glVertexAttribPointer(index + r, C, type, GL_FALSE, stride, base + (r * C));
				}
				return R;
			}};
};

template<typename T>
concept has_glUniform1 = requires { gl_traits<T>::glUniformFunc; };
template<typename T>
concept has_glUniformNv = requires { gl_traits<T>::glUniformvFunc; };
template<typename T>
concept has_glUniformMatrixNv = requires { gl_traits<T>::glUniformmFunc; };
template<typename T>
concept has_glTexParameter = requires { gl_traits<T>::glTexParameterFunc; };
template<typename T>
concept has_glTexParameterf = requires { gl_traits<T>::glTexParameterfFunc; };

template<has_glUniform1 T>
void
glUniform(GLint location, T v)
{
	(*gl_traits<T>::glUniformFunc)(location, v);
}

template<glm::length_t L, has_glUniformNv T, glm::qualifier Q>
void
glUniform(GLint location, const glm::vec<L, T, Q> & v)
{
	(*gl_traits<T>::glUniformvFunc[L - 1])(location, 1, glm::value_ptr(v));
}

template<glm::length_t L, has_glUniformNv T, glm::qualifier Q>
void
glUniform(GLint location, std::span<const glm::vec<L, T, Q>> v)
{
	(*gl_traits<T>::glUniformvFunc[L - 1])(location, static_cast<GLsizei>(v.size()), glm::value_ptr(v.front()));
}

template<has_glUniformNv T>
void
glUniform(GLint location, std::span<const T> v)
{
	(*gl_traits<T>::glUniformvFunc.front())(location, static_cast<GLsizei>(v.size()), v.data());
}

template<glm::length_t L, has_glUniformMatrixNv T, glm::qualifier Q>
void
glUniform(GLint location, const glm::mat<L, L, T, Q> & v)
{
	(*gl_traits<T>::glUniformmFunc[L - 2])(location, 1, GL_FALSE, glm::value_ptr(v));
}

template<glm::length_t L, has_glUniformMatrixNv T, glm::qualifier Q>
void
glUniform(GLint location, std::span<const glm::mat<L, L, T, Q>> v)
{
	(*gl_traits<T>::glUniformmFunc[L - 2])(
			location, static_cast<GLsizei>(v.size()), GL_FALSE, glm::value_ptr(v.front()));
}

template<has_glTexParameter T>
void
glTexParameter(GLenum target, GLenum pname, T param)
{
	(*gl_traits<T>::glTexParameterFunc)(target, pname, param);
}

template<glm::length_t L, has_glTexParameterf T, glm::qualifier Q>
void
glTexParameter(GLenum target, GLenum pname, const glm::vec<L, T, Q> & param)
{
	(*gl_traits<T>::glTexParameterfFunc)(target, pname, glm::value_ptr(param));
}