diff options
author | sanine-a <sanine.not@pm.me> | 2020-10-27 16:24:36 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2020-10-27 16:24:36 -0500 |
commit | 31e715cc8a2849c059cc46cf51b301f663a953b1 (patch) | |
tree | eea24cf268ead8e6cb99e3070b8ee31e456a6f1c /src | |
parent | f04b0943c0134da93512fa7eef589047984d592b (diff) |
refactor cglm bindings to not allocate memory
Diffstat (limited to 'src')
-rw-r--r-- | src/cglm_bindings.c | 184 | ||||
-rw-r--r-- | src/cglm_bindings.h | 66 | ||||
-rw-r--r-- | src/common.h | 1 |
3 files changed, 110 insertions, 141 deletions
diff --git a/src/cglm_bindings.c b/src/cglm_bindings.c index 300b85b..8123d7a 100644 --- a/src/cglm_bindings.c +++ b/src/cglm_bindings.c @@ -236,80 +236,62 @@ int honey_cglm_vec4_norm(lua_State* L) int honey_cglm_vec4_add(lua_State* L) { - if (!honey_lua_validate_types(L, 2, HONEY_USERDATA, HONEY_USERDATA)) - lua_error(L); - - float* a = lua_touserdata(L, 1); - float* b = lua_touserdata(L, 2); - - float* dest = lua_newuserdata(L, 4*sizeof(float)); - + float* a, *b, *dest; + honey_lua_parse_arguments(L, 3, + HONEY_USERDATA, &a, + HONEY_USERDATA, &b, + HONEY_USERDATA, &dest); glm_vec4_add(a, b, dest); - - return 1; + return 0; } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ int honey_cglm_vec4_adds(lua_State* L) { - if (!honey_lua_validate_types(L, 2, HONEY_NUMBER, HONEY_USERDATA)) - lua_error(L); - - float a = lua_tonumber(L, 1); - float* v = lua_touserdata(L, 2); - - float* dest = lua_newuserdata(L, 4*sizeof(float)); - + float a, *v, *dest; + honey_lua_parse_arguments(L, 3, + HONEY_NUMBER, &a, + HONEY_USERDATA, &v, + HONEY_USERDATA, &dest); glm_vec4_adds(v, a, dest); - - return 1; + return 0; } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ int honey_cglm_vec4_mul(lua_State* L) { - if (!honey_lua_validate_types(L, 2, HONEY_USERDATA, HONEY_USERDATA)) - lua_error(L); - - float* a = lua_touserdata(L, 1); - float* b = lua_touserdata(L, 2); - - float* dest = lua_newuserdata(L, 4*sizeof(float)); - + float *a, *b, *dest; + honey_lua_parse_arguments(L, 3, + HONEY_USERDATA, &a, + HONEY_USERDATA, &b, + HONEY_USERDATA, &dest); glm_vec4_mul(a, b, dest); - - return 1; + return 0; } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ int honey_cglm_vec4_muls(lua_State* L) { - if (!honey_lua_validate_types(L, 2, HONEY_NUMBER, HONEY_USERDATA)) - lua_error(L); - - float a = lua_tonumber(L, 1); - float* v = lua_touserdata(L, 2); - - float* dest = lua_newuserdata(L, 4*sizeof(float)); - + float a, *v, *dest; + honey_lua_parse_arguments(L, 3, + HONEY_NUMBER, &a, + HONEY_USERDATA, &v, + HONEY_USERDATA, &dest); glm_vec4_scale(v, a, dest); - - return 1; + return 0; } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ int honey_cglm_vec4_normalize(lua_State* L) { - if (!honey_lua_validate_types(L, 1, HONEY_USERDATA)) - lua_error(L); + float* a; + honey_lua_parse_arguments(L, 1, HONEY_USERDATA, &a); - float* v = lua_touserdata(L, 1); - - glm_vec4_normalize(v); + glm_vec4_normalize(a); return 0; } @@ -318,11 +300,10 @@ int honey_cglm_vec4_normalize(lua_State* L) int honey_cglm_vec4_distance(lua_State* L) { - if (!honey_lua_validate_types(L, 2, HONEY_USERDATA, HONEY_USERDATA)) - lua_error(L); - - float* a = lua_touserdata(L, 1); - float* b = lua_touserdata(L, 2); + float* a, *b; + honey_lua_parse_arguments(L, 2, + HONEY_USERDATA, &a, + HONEY_USERDATA, &b); float distance = glm_vec4_distance(a, b); lua_pushnumber(L, distance); @@ -334,18 +315,15 @@ int honey_cglm_vec4_distance(lua_State* L) int honey_cglm_vec4_lerp(lua_State* L) { - if (!honey_lua_validate_types(L, 3, HONEY_USERDATA, HONEY_USERDATA, HONEY_NUMBER)) - lua_error(L); - - float* a = lua_touserdata(L, 1); - float* b = lua_touserdata(L, 2); - float s = lua_tonumber(L, 3); - - float* dest = lua_newuserdata(L, 4*sizeof(float)); - + float* a, *b, s, *dest; + honey_lua_parse_arguments(L, 4, + HONEY_USERDATA, &a, + HONEY_USERDATA, &b, + HONEY_NUMBER, &s, + HONEY_USERDATA, &dest); glm_vec4_lerp(a, b, s, dest); - return 1; + return 0; } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -369,64 +347,52 @@ int honey_cglm_mat4_identity(lua_State* L) int honey_cglm_mat4_pick3(lua_State* L) { - if (!honey_lua_validate_types(L, 1, HONEY_USERDATA)) - lua_error(L); - - float* matrix = lua_touserdata(L, 1); - - float* dest = lua_newuserdata(L, 9*sizeof(float)); + float* matrix, *dest; + honey_lua_parse_arguments(L, 2, + HONEY_USERDATA, &matrix, + HONEY_USERDATA, &dest); glm_mat4_pick3(matrix, dest); - return 1; + return 0; } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ int honey_cglm_mat4_mul(lua_State* L) { - if (!honey_lua_validate_types(L, 2, HONEY_USERDATA)) - lua_error(L); - - float* A = lua_touserdata(L, 1); - float* B = lua_touserdata(L, 2); - - float* dest = lua_newuserdata(L, 16*sizeof(float)); - + float* A, *B, *dest; + honey_lua_parse_arguments(L, 3, + HONEY_USERDATA, &A, + HONEY_USERDATA, &B, + HONEY_USERDATA, &dest); glm_mat4_mul(A, B, dest); - - return 1; + return 0; } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ int honey_cglm_mat4_muls(lua_State* L) { - if (!honey_lua_validate_types(L, 2, HONEY_NUMBER, HONEY_USERDATA)) - lua_error(L); - - float a = lua_tonumber(L, 1); - float* M = lua_touserdata(L, 2); - - float* dest = lua_newuserdata(L, 16*sizeof(float)); + float a; + float* M; + honey_lua_parse_arguments(L, 2, + HONEY_NUMBER, &a, + HONEY_USERDATA, &M); glm_mat4_scale(M, a); - return 1; + return 0; } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ int honey_cglm_mat4_mulv(lua_State* L) { - if (!honey_lua_validate_types(L, 2, HONEY_USERDATA)) - lua_error(L); - - float* M = lua_touserdata(L, 1); - float* v = lua_touserdata(L, 2); - - float* dest = lua_newuserdata(L, 4*sizeof(float)); - + float* M, *v, *dest; + honey_lua_parse_arguments(L, 3, + HONEY_USERDATA, &M, + HONEY_USERDATA, &v, + HONEY_USERDATA, &dest); glm_mat4_mulv(M, v, dest); - - return 1; + return 0; } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ @@ -477,32 +443,24 @@ int honey_cglm_mat4_trace(lua_State* L) int honey_cglm_mat4_inv(lua_State* L) { - if (!honey_lua_validate_types(L, 1, HONEY_USERDATA)) - lua_error(L); - - float* M = lua_touserdata(L, 1); - - float* dest = lua_newuserdata(L, 16*sizeof(float)); - + float* M, *dest; + honey_lua_parse_arguments(L, 2, + HONEY_USERDATA, &M, + HONEY_USERDATA, &dest); glm_mat4_inv(M, dest); - - return 1; + return 0; } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ int honey_cglm_mat4_inv_fast(lua_State* L) { - if (!honey_lua_validate_types(L, 1, HONEY_USERDATA)) - lua_error(L); - - float* M = lua_touserdata(L, 1); - - float* dest = lua_newuserdata(L, 16*sizeof(float)); - + float* M, *dest; + honey_lua_parse_arguments(L, 2, + HONEY_USERDATA, &M, + HONEY_USERDATA, &dest); glm_mat4_inv_fast(M, dest); - - return 1; + return 0; } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/src/cglm_bindings.h b/src/cglm_bindings.h index 01e6792..e8edf2f 100644 --- a/src/cglm_bindings.h +++ b/src/cglm_bindings.h @@ -136,8 +136,9 @@ int honey_cglm_vec4_norm(lua_State* L); * * @param[in] a The first vector. * @param[in] b The second vector. + * @param[out] dest vec4 to fill with a + b. * - * @returns a + b + * @returns Nothing. */ int honey_cglm_vec4_add(lua_State* L); @@ -145,32 +146,35 @@ int honey_cglm_vec4_add(lua_State* L); * * @param[in] a The scalar. * @param[in] v The vector. + * @param[out] dest vec4 to fill with a + v * - * @returns a + v. + * @returns Nothing. */ int honey_cglm_vec4_adds(lua_State* L); /** @param Component-wise multiply two vectors together. * - * @param a The first vector. - * @param b The second vector. + * @param[in] a The first vector. + * @param[in] b The second vector. + * @param[out] dest vec4 to fill with [ a.x*b.x, a.y*b.y, a.z*b.z, a.w*b.w ] * - * @returns [ a.x*b.x, a.y*b.y, a.z*b.z, a.w*b.w ] + * @returns Nothing. */ int honey_cglm_vec4_mul(lua_State* L); /** @brief Multiply a vector by a scalar. * - * @param a The scalar. - * @param v The vector. + * @param[in] a The scalar. + * @param[in] v The vector. + * @param[out] dest vec4 to fill with a*v. * - * @returns a*v. + * @returns Nothing. */ int honey_cglm_vec4_muls(lua_State* L); /** @brief Normalize a vector. * - * @param[inout] v The vector. + * @param[in,out] v The vector. * * @returns Nothing. */ @@ -187,11 +191,12 @@ int honey_cglm_vec4_distance(lua_State* L); /** @brief Linearly interpolate between two values. * - * @param a The first vector. - * @param b The second vector. - * @param s A scalar. + * @param[in] a The first vector. + * @param[in] b The second vector. + * @param[in] s A scalar. + * @param[out] dest vec4 to fill with a + s*(b-s). * - * @returns a + s*(b-s) + * @returns Nothing. */ int honey_cglm_vec4_lerp(lua_State* L); @@ -204,7 +209,7 @@ int honey_cglm_vec4_lerp(lua_State* L); /** @brief Set a matrix to be the identity matrix. * - * @param matrix The matrix to set to the identity. + * @param[out] matrix The matrix to set to the identity. * * @returns Nothing. */ @@ -212,27 +217,29 @@ int honey_cglm_mat4_identity(lua_State* L); /** @brief Get the upper left of a matrix as a mat3. * - * @param matrix The matrix to extract. + * @param[in] matrix The matrix to extract. + * @param[out] dest The 3x3 matrix to fill. * - * @returns A new matrix containing the upper left 3x3 section of matrix. + * @returns Nothing. */ int honey_cglm_mat4_pick3(lua_State* L); /** @brief Multiply two mat4s together. * - * @param A The first matrix. - * @param B The second matrix. + * @param[in] A The first matrix. + * @param[in] B The second matrix. + * @param[out] dest mat4 to fill with A*B. * - * @returns A*B. + * @returns Nothing. */ int honey_cglm_mat4_mul(lua_State* L); /** @brief Multiply a matrix by a scalar. * * @param[in] a The scalar. - * @param[in] M The matrix. + * @param[in,out] M The matrix. * - * @returns Matrix containing a*M. + * @returns Nothing. */ int honey_cglm_mat4_muls(lua_State* L); @@ -240,14 +247,15 @@ int honey_cglm_mat4_muls(lua_State* L); * * @param[in] M The matrix. * @param[in] v The column vector. + * @param[out] dest Matrix to fill with M*v. * - * @returns Matrix containing M*v. + * @returns Nothing. */ int honey_cglm_mat4_mulv(lua_State* L); /** @brief Transpose a matrix. * - * @param[inout] M The matrix to transpose. + * @param[in,out] M The matrix to transpose. * * @returns Nothing. */ @@ -275,8 +283,9 @@ int honey_cglm_mat4_trace(lua_State* L); * for a faster but less precise version. * * @param[in] M The matrix to invert. + * @param[out] dest Matrix to fill with inv(M). * - * @returns inv(M). + * @returns Nothing. */ int honey_cglm_mat4_inv(lua_State* L); @@ -286,8 +295,9 @@ int honey_cglm_mat4_inv(lua_State* L); * for a slower but more precise version. * * @param[in] M The matrix to invert. + * @param[out] dest Matrix to fill with inv(M). * - * @returns inv(M). + * @returns Nothing. */ int honey_cglm_mat4_inv_fast(lua_State* L); @@ -302,7 +312,7 @@ int honey_cglm_mat4_inv_fast(lua_State* L); * * This function modifies the matrix in place. * - * @param[inout] matrix The mat4 to translate. + * @param[in,out] matrix The mat4 to translate. * @param[in] vector The vec3 to translate by. * * @returns Nothing. @@ -311,7 +321,7 @@ int honey_cglm_translate(lua_State* L); /** @brief Scale a matrix by a vector. * - * @param[inout] matrix The mat4 to scale. + * @param[in,out] matrix The mat4 to scale. * @param[in] vector The vec3 to scale by. * * @returns Nothing. @@ -320,7 +330,7 @@ int honey_cglm_scale(lua_State* L); /** @brief Rotate a matrix about a given axis. * - * @param[inout] matrix The mat4 to rotate. + * @param[in,out] matrix The mat4 to rotate. * @param[in] center The vec3 center of rotation. * @param[in] axis The vec3 axis of rotation. * @param[in] angle The angle to rotate by. diff --git a/src/common.h b/src/common.h index e74e824..556304f 100644 --- a/src/common.h +++ b/src/common.h @@ -22,6 +22,7 @@ #include <GLFW/glfw3.h> // c opengl mathematics function +#define CGLM_ALL_UNALIGNED #include <cglm/cglm.h> #include <cglm/call.h> |