blob: dac5b64e6a252795d06b8604cc17e13df9895f4b [file] [log] [blame]
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001#ifndef ANDROID_DVR_GRAPHICS_VERTEX_ATTRIBUTES_H_
2#define ANDROID_DVR_GRAPHICS_VERTEX_ATTRIBUTES_H_
3
4#include <private/dvr/types.h>
5
6#include <EGL/egl.h>
7#include <GLES3/gl3.h>
8#include <tuple>
9
10namespace android {
11namespace dvr {
12
13namespace Details {
14
15// Set up the vertex attributes by iterating over the variadic template
16// parameters. The supported attributes are the GetSize and GetType
17// specializations.
18// clang-format off
19template<typename T> GLint GetSize();
20template<> inline GLint GetSize<vec2>() { return 2; }
21template<> inline GLint GetSize<vec3>() { return 3; }
22template<> inline GLint GetSize<vec4>() { return 4; }
23
24template<typename T> GLenum GetType();
25template<> inline GLenum GetType<vec2>() { return GL_FLOAT; }
26template<> inline GLenum GetType<vec3>() { return GL_FLOAT; }
27template<> inline GLenum GetType<vec4>() { return GL_FLOAT; }
28// clang-format on
29
30template <typename T>
31void VertexAttrib(GLuint index, GLsizei stride, const GLvoid* pointer) {
32 glVertexAttribPointer(index, GetSize<T>(), GetType<T>(), GL_FALSE, stride,
33 pointer);
34 glEnableVertexAttribArray(index);
35}
36
37// Recursion variadic template parameter iterator.
38template <int index, typename... Ts>
39struct VertexAttribHelper {
40 using tuple = std::tuple<Ts...>;
41 size_t operator()() {
42 size_t offset = VertexAttribHelper<index - 1, Ts...>{}();
43 using type = typename std::tuple_element<index, tuple>::type;
44 VertexAttrib<type>(index, sizeof(tuple), reinterpret_cast<void*>(offset));
45 return offset + sizeof(type);
46 }
47};
48
49// Recursion stop point.
50template <typename... Ts>
51struct VertexAttribHelper<0, Ts...> {
52 using tuple = std::tuple<Ts...>;
53 size_t operator()() {
54 using type = typename std::tuple_element<0, tuple>::type;
55 VertexAttrib<type>(0, sizeof(tuple), nullptr);
56 return sizeof(type);
57 }
58};
59} // namespace Details
60
61} // namespace dvr
62} // namespace android
63
64#endif // ANDROID_DVR_GRAPHICS_VERTEX_ATTRIBUTES_H_