From 7c4bbdc3983da3ef32fb09ad0491c63a0a4ca1d7 Mon Sep 17 00:00:00 2001 From: Jelle Raaijmakers Date: Sat, 26 Nov 2022 10:50:27 +0100 Subject: [PATCH] LibGL: Simplify transposing input matrices We do not need to templatize the output type - it's always `float`. Also, the input type can be inferred. Finally, use template specialization instead of a conditional to deal with same type input and output matrices. --- Userland/Libraries/LibGL/GLAPI.cpp | 40 ++++++++++++++++-------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/Userland/Libraries/LibGL/GLAPI.cpp b/Userland/Libraries/LibGL/GLAPI.cpp index 293d18e900..c33704af55 100644 --- a/Userland/Libraries/LibGL/GLAPI.cpp +++ b/Userland/Libraries/LibGL/GLAPI.cpp @@ -15,23 +15,12 @@ extern GL::GLContext* g_gl_context; // Transposes input matrices (column-major) to our Matrix (row-major). -template -static constexpr Matrix4x4 transpose_input_matrix(I const* matrix) +template +constexpr FloatMatrix4x4 transpose_input_matrix(I const* matrix) { - if constexpr (IsSame) { - // clang-format off - return { - matrix[0], matrix[4], matrix[8], matrix[12], - matrix[1], matrix[5], matrix[9], matrix[13], - matrix[2], matrix[6], matrix[10], matrix[14], - matrix[3], matrix[7], matrix[11], matrix[15], - }; - // clang-format on - } - - Array elements; + Array elements; for (size_t i = 0; i < 16; ++i) - elements[i] = static_cast(matrix[i]); + elements[i] = static_cast(matrix[i]); // clang-format off return { elements[0], elements[4], elements[8], elements[12], @@ -42,6 +31,19 @@ static constexpr Matrix4x4 transpose_input_matrix(I const* matrix) // clang-format on } +template<> +constexpr FloatMatrix4x4 transpose_input_matrix(float const* matrix) +{ + // clang-format off + return { + matrix[0], matrix[4], matrix[8], matrix[12], + matrix[1], matrix[5], matrix[9], matrix[13], + matrix[2], matrix[6], matrix[10], matrix[14], + matrix[3], matrix[7], matrix[11], matrix[15], + }; + // clang-format on +} + void glActiveTexture(GLenum texture) { g_gl_context->gl_active_texture(texture); @@ -620,12 +622,12 @@ void glLoadIdentity() void glLoadMatrixd(GLdouble const* matrix) { - g_gl_context->gl_load_matrix(transpose_input_matrix(matrix)); + g_gl_context->gl_load_matrix(transpose_input_matrix(matrix)); } void glLoadMatrixf(GLfloat const* matrix) { - g_gl_context->gl_load_matrix(transpose_input_matrix(matrix)); + g_gl_context->gl_load_matrix(transpose_input_matrix(matrix)); } void glMap1d(GLenum target, GLdouble u1, GLdouble u2, GLint stride, GLint order, GLdouble const* points) @@ -738,12 +740,12 @@ void glMultiTexCoord4f(GLenum target, GLfloat s, GLfloat t, GLfloat r, GLfloat q void glMultMatrixd(GLdouble const* matrix) { - g_gl_context->gl_mult_matrix(transpose_input_matrix(matrix)); + g_gl_context->gl_mult_matrix(transpose_input_matrix(matrix)); } void glMultMatrixf(GLfloat const* matrix) { - g_gl_context->gl_mult_matrix(transpose_input_matrix(matrix)); + g_gl_context->gl_mult_matrix(transpose_input_matrix(matrix)); } void glNewList(GLuint list, GLenum mode)