vec4.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <lain@obsproject.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #pragma once
  15. #include "math-defs.h"
  16. #include "srgb.h"
  17. #include "../util/sse-intrin.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. struct vec3;
  22. struct matrix4;
  23. struct vec4 {
  24. union {
  25. struct {
  26. float x, y, z, w;
  27. };
  28. float ptr[4];
  29. __m128 m;
  30. };
  31. };
  32. static inline void vec4_zero(struct vec4 *v)
  33. {
  34. v->m = _mm_setzero_ps();
  35. }
  36. static inline void vec4_set(struct vec4 *dst, float x, float y, float z, float w)
  37. {
  38. dst->m = _mm_set_ps(w, z, y, x);
  39. }
  40. static inline void vec4_copy(struct vec4 *dst, const struct vec4 *v)
  41. {
  42. dst->m = v->m;
  43. }
  44. EXPORT void vec4_from_vec3(struct vec4 *dst, const struct vec3 *v);
  45. static inline void vec4_add(struct vec4 *dst, const struct vec4 *v1, const struct vec4 *v2)
  46. {
  47. dst->m = _mm_add_ps(v1->m, v2->m);
  48. }
  49. static inline void vec4_sub(struct vec4 *dst, const struct vec4 *v1, const struct vec4 *v2)
  50. {
  51. dst->m = _mm_sub_ps(v1->m, v2->m);
  52. }
  53. static inline void vec4_mul(struct vec4 *dst, const struct vec4 *v1, const struct vec4 *v2)
  54. {
  55. dst->m = _mm_mul_ps(v1->m, v2->m);
  56. }
  57. static inline void vec4_div(struct vec4 *dst, const struct vec4 *v1, const struct vec4 *v2)
  58. {
  59. dst->m = _mm_div_ps(v1->m, v2->m);
  60. }
  61. static inline void vec4_addf(struct vec4 *dst, const struct vec4 *v, float f)
  62. {
  63. dst->m = _mm_add_ps(v->m, _mm_set1_ps(f));
  64. }
  65. static inline void vec4_subf(struct vec4 *dst, const struct vec4 *v, float f)
  66. {
  67. dst->m = _mm_sub_ps(v->m, _mm_set1_ps(f));
  68. }
  69. static inline void vec4_mulf(struct vec4 *dst, const struct vec4 *v, float f)
  70. {
  71. dst->m = _mm_mul_ps(v->m, _mm_set1_ps(f));
  72. }
  73. static inline void vec4_divf(struct vec4 *dst, const struct vec4 *v, float f)
  74. {
  75. dst->m = _mm_div_ps(v->m, _mm_set1_ps(f));
  76. }
  77. static inline float vec4_dot(const struct vec4 *v1, const struct vec4 *v2)
  78. {
  79. struct vec4 add;
  80. __m128 mul = _mm_mul_ps(v1->m, v2->m);
  81. add.m = _mm_add_ps(_mm_movehl_ps(mul, mul), mul);
  82. add.m = _mm_add_ps(_mm_shuffle_ps(add.m, add.m, 0x55), add.m);
  83. return add.x;
  84. }
  85. static inline void vec4_neg(struct vec4 *dst, const struct vec4 *v)
  86. {
  87. dst->x = -v->x;
  88. dst->y = -v->y;
  89. dst->z = -v->z;
  90. dst->w = -v->w;
  91. }
  92. static inline float vec4_len(const struct vec4 *v)
  93. {
  94. float dot_val = vec4_dot(v, v);
  95. return (dot_val > 0.0f) ? sqrtf(dot_val) : 0.0f;
  96. }
  97. static inline float vec4_dist(const struct vec4 *v1, const struct vec4 *v2)
  98. {
  99. struct vec4 temp;
  100. float dot_val;
  101. vec4_sub(&temp, v1, v2);
  102. dot_val = vec4_dot(&temp, &temp);
  103. return (dot_val > 0.0f) ? sqrtf(dot_val) : 0.0f;
  104. }
  105. static inline void vec4_norm(struct vec4 *dst, const struct vec4 *v)
  106. {
  107. float dot_val = vec4_dot(v, v);
  108. dst->m = (dot_val > 0.0f) ? _mm_mul_ps(v->m, _mm_set1_ps(1.0f / sqrtf(dot_val))) : _mm_setzero_ps();
  109. }
  110. static inline int vec4_close(const struct vec4 *v1, const struct vec4 *v2, float epsilon)
  111. {
  112. struct vec4 test;
  113. vec4_sub(&test, v1, v2);
  114. return test.x < epsilon && test.y < epsilon && test.z < epsilon && test.w < epsilon;
  115. }
  116. static inline void vec4_min(struct vec4 *dst, const struct vec4 *v1, const struct vec4 *v2)
  117. {
  118. dst->m = _mm_min_ps(v1->m, v2->m);
  119. }
  120. static inline void vec4_minf(struct vec4 *dst, const struct vec4 *v, float f)
  121. {
  122. dst->m = _mm_min_ps(v->m, _mm_set1_ps(f));
  123. }
  124. static inline void vec4_max(struct vec4 *dst, const struct vec4 *v1, const struct vec4 *v2)
  125. {
  126. dst->m = _mm_max_ps(v1->m, v2->m);
  127. }
  128. static inline void vec4_maxf(struct vec4 *dst, const struct vec4 *v, float f)
  129. {
  130. dst->m = _mm_max_ps(v->m, _mm_set1_ps(f));
  131. }
  132. static inline void vec4_abs(struct vec4 *dst, const struct vec4 *v)
  133. {
  134. dst->x = fabsf(v->x);
  135. dst->y = fabsf(v->y);
  136. dst->z = fabsf(v->z);
  137. dst->w = fabsf(v->w);
  138. }
  139. static inline void vec4_floor(struct vec4 *dst, const struct vec4 *v)
  140. {
  141. dst->x = floorf(v->x);
  142. dst->y = floorf(v->y);
  143. dst->z = floorf(v->z);
  144. dst->w = floorf(v->w);
  145. }
  146. static inline void vec4_ceil(struct vec4 *dst, const struct vec4 *v)
  147. {
  148. dst->x = ceilf(v->x);
  149. dst->y = ceilf(v->y);
  150. dst->z = ceilf(v->z);
  151. dst->w = ceilf(v->w);
  152. }
  153. static inline uint32_t vec4_to_rgba(const struct vec4 *src)
  154. {
  155. float f[4];
  156. memcpy(f, src->ptr, sizeof(f));
  157. uint8_t u[4];
  158. gs_float4_to_u8x4(u, f);
  159. uint32_t val;
  160. memcpy(&val, u, sizeof(val));
  161. return val;
  162. }
  163. static inline uint32_t vec4_to_bgra(const struct vec4 *src)
  164. {
  165. float f[4];
  166. memcpy(f, src->ptr, sizeof(f));
  167. uint8_t u[4];
  168. gs_float4_to_u8x4(u, f);
  169. uint8_t temp = u[0];
  170. u[0] = u[2];
  171. u[2] = temp;
  172. uint32_t val;
  173. memcpy(&val, u, sizeof(val));
  174. return val;
  175. }
  176. static inline void vec4_from_rgba(struct vec4 *dst, uint32_t rgba)
  177. {
  178. uint8_t u[4];
  179. memcpy(u, &rgba, sizeof(u));
  180. gs_u8x4_to_float4(dst->ptr, u);
  181. }
  182. static inline void vec4_from_bgra(struct vec4 *dst, uint32_t bgra)
  183. {
  184. uint8_t u[4];
  185. memcpy(u, &bgra, sizeof(u));
  186. uint8_t temp = u[0];
  187. u[0] = u[2];
  188. u[2] = temp;
  189. gs_u8x4_to_float4(dst->ptr, u);
  190. }
  191. static inline void vec4_from_rgba_srgb(struct vec4 *dst, uint32_t rgba)
  192. {
  193. vec4_from_rgba(dst, rgba);
  194. gs_float3_srgb_nonlinear_to_linear(dst->ptr);
  195. }
  196. EXPORT void vec4_transform(struct vec4 *dst, const struct vec4 *v, const struct matrix4 *m);
  197. #ifdef __cplusplus
  198. }
  199. #endif