1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:07:45 +00:00

LibGL: Don't repeat ourselves in read_from_vertex_attribute_pointer

This commit is contained in:
Jelle Raaijmakers 2022-10-16 16:52:27 +02:00 committed by Linus Groh
parent 5def168f14
commit e2b151812e

View file

@ -8,6 +8,7 @@
#include <AK/Assertions.h> #include <AK/Assertions.h>
#include <AK/Debug.h> #include <AK/Debug.h>
#include <AK/NumericLimits.h>
#include <LibGL/GLContext.h> #include <LibGL/GLContext.h>
namespace GL { namespace GL {
@ -16,94 +17,45 @@ namespace GL {
static void read_from_vertex_attribute_pointer(VertexAttribPointer const& attrib, int index, float* elements) static void read_from_vertex_attribute_pointer(VertexAttribPointer const& attrib, int index, float* elements)
{ {
auto const* byte_ptr = reinterpret_cast<char const*>(attrib.pointer); auto const* byte_ptr = reinterpret_cast<char const*>(attrib.pointer);
auto normalize = attrib.normalize;
size_t stride = attrib.stride; auto read_values = [&]<typename T>() {
auto const stride = (attrib.stride == 0) ? sizeof(T) * attrib.size : attrib.stride;
for (int i = 0; i < attrib.size; ++i) {
elements[i] = *(reinterpret_cast<T const*>(byte_ptr + stride * index) + i);
if constexpr (IsIntegral<T>) {
if (attrib.normalize)
elements[i] /= NumericLimits<T>::max();
}
}
};
switch (attrib.type) { switch (attrib.type) {
case GL_BYTE: { case GL_BYTE:
if (stride == 0) read_values.operator()<GLbyte>();
stride = sizeof(GLbyte) * attrib.size;
for (int i = 0; i < attrib.size; i++) {
elements[i] = *(reinterpret_cast<GLbyte const*>(byte_ptr + stride * index) + i);
if (normalize)
elements[i] /= 0x80;
}
break; break;
} case GL_UNSIGNED_BYTE:
case GL_UNSIGNED_BYTE: { read_values.operator()<GLubyte>();
if (stride == 0)
stride = sizeof(GLubyte) * attrib.size;
for (int i = 0; i < attrib.size; i++) {
elements[i] = *(reinterpret_cast<GLubyte const*>(byte_ptr + stride * index) + i);
if (normalize)
elements[i] /= 0xff;
}
break; break;
} case GL_SHORT:
case GL_SHORT: { read_values.operator()<GLshort>();
if (stride == 0)
stride = sizeof(GLshort) * attrib.size;
for (int i = 0; i < attrib.size; i++) {
elements[i] = *(reinterpret_cast<GLshort const*>(byte_ptr + stride * index) + i);
if (normalize)
elements[i] /= 0x8000;
}
break; break;
} case GL_UNSIGNED_SHORT:
case GL_UNSIGNED_SHORT: { read_values.operator()<GLushort>();
if (stride == 0)
stride = sizeof(GLushort) * attrib.size;
for (int i = 0; i < attrib.size; i++) {
elements[i] = *(reinterpret_cast<GLushort const*>(byte_ptr + stride * index) + i);
if (normalize)
elements[i] /= 0xffff;
}
break; break;
} case GL_INT:
case GL_INT: { read_values.operator()<GLint>();
if (stride == 0)
stride = sizeof(GLint) * attrib.size;
for (int i = 0; i < attrib.size; i++) {
elements[i] = *(reinterpret_cast<GLint const*>(byte_ptr + stride * index) + i);
if (normalize)
elements[i] /= 0x80000000;
}
break; break;
} case GL_UNSIGNED_INT:
case GL_UNSIGNED_INT: { read_values.operator()<GLuint>();
if (stride == 0)
stride = sizeof(GLuint) * attrib.size;
for (int i = 0; i < attrib.size; i++) {
elements[i] = *(reinterpret_cast<GLuint const*>(byte_ptr + stride * index) + i);
if (normalize)
elements[i] /= 0xffffffff;
}
break; break;
} case GL_FLOAT:
case GL_FLOAT: { read_values.operator()<GLfloat>();
if (stride == 0)
stride = sizeof(GLfloat) * attrib.size;
for (int i = 0; i < attrib.size; i++)
elements[i] = *(reinterpret_cast<GLfloat const*>(byte_ptr + stride * index) + i);
break; break;
} case GL_DOUBLE:
case GL_DOUBLE: { read_values.operator()<GLdouble>();
if (stride == 0)
stride = sizeof(GLdouble) * attrib.size;
for (int i = 0; i < attrib.size; i++)
elements[i] = static_cast<float>(*(reinterpret_cast<GLdouble const*>(byte_ptr + stride * index) + i));
break; break;
} }
} }
}
void GLContext::gl_array_element(GLint i) void GLContext::gl_array_element(GLint i)
{ {