Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame^] | 1 | #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 | |
| 10 | namespace android { |
| 11 | namespace dvr { |
| 12 | |
| 13 | namespace 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 |
| 19 | template<typename T> GLint GetSize(); |
| 20 | template<> inline GLint GetSize<vec2>() { return 2; } |
| 21 | template<> inline GLint GetSize<vec3>() { return 3; } |
| 22 | template<> inline GLint GetSize<vec4>() { return 4; } |
| 23 | |
| 24 | template<typename T> GLenum GetType(); |
| 25 | template<> inline GLenum GetType<vec2>() { return GL_FLOAT; } |
| 26 | template<> inline GLenum GetType<vec3>() { return GL_FLOAT; } |
| 27 | template<> inline GLenum GetType<vec4>() { return GL_FLOAT; } |
| 28 | // clang-format on |
| 29 | |
| 30 | template <typename T> |
| 31 | void 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. |
| 38 | template <int index, typename... Ts> |
| 39 | struct 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. |
| 50 | template <typename... Ts> |
| 51 | struct 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_ |