diff options
| author | ladevieq <ladevie.quentin@gmail.com> | 2023-01-12 00:47:59 +0100 |
|---|---|---|
| committer | ladevieq <ladevie.quentin@gmail.com> | 2023-01-12 00:47:59 +0100 |
| commit | 5e3bb454d50091e581d5d1d03af34e38277694b3 (patch) | |
| tree | 06eada2111afb56a24d0ced89685587400041c8d /src/vec3.cpp | |
| parent | b47e177898657e435cb5bc913a4a783fddbe1f3d (diff) | |
WIP new rendering api
Diffstat (limited to 'src/vec3.cpp')
| -rw-r--r-- | src/vec3.cpp | 54 |
1 files changed, 29 insertions, 25 deletions
diff --git a/src/vec3.cpp b/src/vec3.cpp index 1bd068f..0e08f16 100644 --- a/src/vec3.cpp +++ b/src/vec3.cpp @@ -6,38 +6,41 @@ #include "vec3.hpp" vec3::vec3() : v(_mm_setzero_ps()) {} + vec3::vec3(float x) : v(_mm_set1_ps(x)) {} + vec3::vec3(float x, float y, float z) : v(_mm_set_ps(0.f, z, y, x)) {} -vec3::vec3(__m128 &v) : v(v) {} -vec3::vec3(__m128 &&v) : v(v) {} +vec3::vec3(__m128& v) : v(v) {} -vec3::vec3(const vec3 &vec) { v = _mm_load_ps((float *)&vec.v); } +vec3::vec3(__m128&& v) : v(v) {} -vec3::vec3(const vec3 &&vec) { v = _mm_load_ps((float *)&vec.v); } +vec3::vec3(const vec3& vec) { v = _mm_load_ps((float*)&vec.v); } + +vec3::vec3(const vec3&& vec) noexcept { v = _mm_load_ps((float*)&vec.v); } vec3 vec3::operator-() const { return { _mm_xor_ps(v, _mm_set1_ps(-0.0f)) }; } -vec3 &vec3::operator=(const vec3 &vec) { - v = _mm_load_ps((float *)&vec.v); +vec3& vec3::operator=(const vec3& vec) { + v = _mm_load_ps((float*)&vec.v); return *this; } -vec3 &vec3::operator+=(const vec3 &vec) { +vec3& vec3::operator+=(const vec3& vec) { v = _mm_add_ps(v, vec.v); return *this; } -vec3 &vec3::operator*=(float scale) { +vec3& vec3::operator*=(float scale) { __m128 s = _mm_set1_ps(scale); v = _mm_mul_ps(v, s); return *this; } -vec3 &vec3::operator/=(float numerator) { +vec3& vec3::operator/=(float numerator) { __m128 s = _mm_set1_ps(numerator); v = _mm_div_ps(v, s); @@ -45,7 +48,9 @@ vec3 &vec3::operator/=(float numerator) { } float vec3::operator[](const int axis) { - if (axis > 2) { return std::numeric_limits<float>::infinity(); } + if (axis > 2) { + return std::numeric_limits<float>::infinity(); + } return v[axis]; } @@ -57,9 +62,9 @@ float vec3::length() const { float vec3::length_sq() const { return (_mm_dp_ps(v, v, 0b11110001))[0]; } -float vec3::dot(const vec3 &vec) const { return (_mm_dp_ps(v, vec.v, 0b11110001))[0]; } +float vec3::dot(const vec3& vec) const { return (_mm_dp_ps(v, vec.v, 0b11110001))[0]; } -vec3 vec3::cross(const vec3 &vec) const { +vec3 vec3::cross(const vec3& vec) const { __m128 tmp0 = _mm_shuffle_ps(v, v, _MM_SHUFFLE(3, 0, 2, 1)); __m128 tmp1 = _mm_shuffle_ps(vec.v, vec.v, _MM_SHUFFLE(3, 1, 0, 2)); __m128 tmp2 = _mm_mul_ps(tmp0, vec.v); @@ -68,15 +73,15 @@ vec3 vec3::cross(const vec3 &vec) const { return _mm_sub_ps(tmp3, tmp4); } -vec3 vec3::min(const vec3 &vec) const { +vec3 vec3::min(const vec3& vec) const { return { _mm_min_ps(v, vec.v) }; } -vec3 vec3::max(const vec3 &vec) const { +vec3 vec3::max(const vec3& vec) const { return { _mm_max_ps(v, vec.v) }; } -vec3 &vec3::normalize() { +vec3& vec3::normalize() { v = _mm_div_ps(v, _mm_sqrt_ps(_mm_dp_ps(v, v, 0xff))); return *this; @@ -96,36 +101,35 @@ vec3 vec3::random() { return { randd(), randd(), randd() }; } vec3 vec3::random(float min, float max) { return { randd(min, max), randd(min, max), randd(min, max) }; } - // TODO: Use simd instructions -vec3 lerp(const vec3 &u, const vec3 &v, float t) { return (1.f - t) * u + t * v; } +vec3 lerp(const vec3& u, const vec3& v, float t) { return (1.f - t) * u + t * v; } -vec3 operator+(const vec3 &u, const vec3 &v) { return vec3(_mm_add_ps(u.v, v.v)); } +vec3 operator+(const vec3& u, const vec3& v) { return vec3(_mm_add_ps(u.v, v.v)); } -vec3 operator-(const vec3 &u, const vec3 &v) { return vec3(_mm_sub_ps(u.v, v.v)); } +vec3 operator-(const vec3& u, const vec3& v) { return vec3(_mm_sub_ps(u.v, v.v)); } -vec3 operator*(const vec3 &u, const vec3 &v) { return vec3(_mm_mul_ps(u.v, v.v)); } +vec3 operator*(const vec3& u, const vec3& v) { return vec3(_mm_mul_ps(u.v, v.v)); } -vec3 operator*(const vec3 &u, float scale) { +vec3 operator*(const vec3& u, float scale) { __m128 s = _mm_set1_ps(scale); return vec3(_mm_mul_ps(u.v, s)); } -vec3 operator*(float scale, const vec3 &u) { +vec3 operator*(float scale, const vec3& u) { __m128 s = _mm_set1_ps(scale); return vec3(_mm_mul_ps(u.v, s)); } -vec3 operator/(const vec3 &u, float scale) { +vec3 operator/(const vec3& u, float scale) { __m128 s = _mm_set1_ps(scale); return vec3(_mm_div_ps(u.v, s)); } // TODO: Use simd instructions -vec3 reflect(const vec3 &v, const vec3 &n) { return v - 2 * v.dot(n) * n; } +vec3 reflect(const vec3& v, const vec3& n) { return v - 2 * v.dot(n) * n; } // TODO: Use simd instructions -vec3 refract(const vec3 &uv, const vec3 &n, float etai_over_etat) { +vec3 refract(const vec3& uv, const vec3& n, float etai_over_etat) { auto cos_theta = fmin(n.dot(-uv), 1.f); vec3 r_out_perp = etai_over_etat * (uv + cos_theta * n); vec3 r_out_parallel = -sqrt(fabs(1.f - r_out_perp.length_sq())) * n; |
