StapleGL
Header-only C++20 OpenGL wrapper
Loading...
Searching...
No Matches
shader_data_type.hpp
Go to the documentation of this file.
1
17#pragma once
18
19#include "gl_functions.hpp"
20
21#include <cstddef>
22#include <cstdint>
23#include <exception>
24
25#ifdef STAPLEGL_DEBUG
26#include <cstdio>
27#endif // STAPLEGL_DEBUG
28
30
38enum class shader_array_type : std::uint8_t {
45};
46
59enum class u_type : std::uint8_t {
60
61 float32,
62 vec2,
63 vec3,
64 vec4,
65 mat3,
66 mat4,
67};
68
76constexpr static auto size(u_type type) -> std::size_t
77{
78 switch (type) {
79 case u_type::float32:
80 return sizeof(float);
81 case u_type::vec2:
82 return sizeof(float) * 2;
83 case u_type::vec3:
84 return sizeof(float) * 3;
85 case u_type::vec4:
86 return sizeof(float) * 4;
87 case u_type::mat3: // internally padded to use 3 vec4s
88 return size(u_type::vec4) * 3;
89 case u_type::mat4:
90 return size(u_type::vec4) * 4;
91 default:
92#ifdef STAPLEGL_DEBUG
93 std::fprintf(stderr, STAPLEGL_LINEINFO ", invalid shader enum %d\n",
94 static_cast<int>(type));
95#endif // STAPLEGL_DEBUG
96 std::terminate();
97 }
98}
99
107constexpr static auto to_opengl_type(u_type type) -> std::uint32_t
108{
109 switch (type) {
110 case u_type::vec2:
111 return GL_FLOAT_VEC2;
112 case u_type::vec3:
113 return GL_FLOAT_VEC3;
114 case u_type::vec4:
115 return GL_FLOAT_VEC4;
116 case u_type::mat3:
117 return GL_FLOAT_MAT3;
118 case u_type::mat4:
119 return GL_FLOAT_MAT4;
120 case u_type::float32:
121 return GL_FLOAT;
122 default:
123
124#ifdef STAPLEGL_DEBUG
125 std::fprintf(stderr, STAPLEGL_LINEINFO ", invalid shader enum %d\n",
126 static_cast<int>(type));
127#endif // STAPLEGL_DEBUG
128 std::terminate();
129 }
130}
131
142constexpr static auto to_opengl_underlying_type(u_type type) -> std::uint32_t
143{
144 switch (type) {
145 case u_type::vec2:
146 case u_type::vec3:
147 case u_type::vec4:
148 case u_type::mat3:
149 case u_type::mat4:
150 case u_type::float32:
151 return GL_FLOAT;
152 default:
153#ifdef STAPLEGL_DEBUG
154 std::fprintf(stderr, STAPLEGL_LINEINFO ", invalid shader enum %d\n",
155 static_cast<int>(type));
156#endif // STAPLEGL_DEBUG
157 std::terminate();
158 }
159}
160
169constexpr static auto component_count(u_type type) -> std::uint16_t
170{
171 switch (type) {
172 case u_type::vec2:
173 return 2;
174 case u_type::vec3:
175 return 3;
176 case u_type::vec4:
177 return 4;
178 case u_type::mat3:
179 return 12;
180 case u_type::mat4:
181 return 16;
182 case u_type::float32:
183 return 1;
184 default:
185#ifdef STAPLEGL_DEBUG
186 std::fprintf(stderr, STAPLEGL_LINEINFO ", invalid shader enum %d\n",
187 static_cast<int>(type));
188#endif // STAPLEGL_DEBUG
189 std::terminate();
190 }
191}
192};
Loads OpenGL functions.
static constexpr auto component_count(u_type type) -> std::uint16_t
Get the number of components in the shader data type, useful for vector types.
static constexpr auto to_opengl_type(u_type type) -> std::uint32_t
Convert the shader data type to an OpenGL type.
u_type
Enumerator that represents a data type.
static constexpr auto size(u_type type) -> std::size_t
Get the size of the shader data type.
static constexpr auto to_opengl_underlying_type(u_type type) -> std::uint32_t
Obtains the OpenGL type of the underlying type of the shader data type.
shader_array_type
Enumerator that represents an array of a given data type.