JPL Spatial
Sound spatialization and propagation library
Loading...
Searching...
No Matches
Vec3Buffer.h
Go to the documentation of this file.
1//
2// ██╗██████╗ ██╗ ██╗██████╗ ███████╗
3// ██║██╔══██╗ ██║ ██║██╔══██╗██╔════╝ ** JPLSpatial **
4// ██║██████╔╝ ██║ ██║██████╔╝███████╗
5// ██ ██║██╔═══╝ ██║ ██║██╔══██╗╚════██║ https://github.com/Jaytheway/JPLSpatial
6// ╚█████╔╝██║ ███████╗██║██████╔╝███████║
7// ╚════╝ ╚═╝ ╚══════╝╚═╝╚═════╝ ╚══════╝
8//
9// Copyright Jaroslav Pevno, JPLSpatial is offered under the terms of the ISC license:
10//
11// Permission to use, copy, modify, and/or distribute this software for any purpose with or
12// without fee is hereby granted, provided that the above copyright notice and this permission
13// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
14// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
16// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
17// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
18// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20#pragma once
21
22#include <JPLSpatial/Core.h>
27
28#include <concepts>
29#include <cstring>
30#include <span>
31
32namespace JPL
33{
34 namespace Impl
35 {
36 template<class T>
37 concept CSIMDorFloat = std::same_as<T, float> || std::same_as<T, simd>;
38 }
39
40 //======================================================================
41 // Forward declarations
42 template<Impl::CSIMDorFloat T>
43 struct Vec3BufferView;
44
45 template<Impl::CSIMDorFloat T, std::size_t N>
46 struct Vec3Buffer;
47
48 //======================================================================
50 template<std::size_t N> using Vec3SIMDBuffer = Vec3Buffer<simd, N>;
51
53 template<std::size_t N> using Vec3FloatBuffer = Vec3Buffer<float, N>;
54
57
60
61 //======================================================================
64 template<Impl::CSIMDorFloat T, std::size_t N>
66 {
67 T X[N];
68 T Y[N];
69 T Z[N];
70
72 {
73 JPL_ASSERT(count <= N);
74 return Vec3BufferView<T>{ .X = X, .Y = Y , .Z = Z, .Count = count };
75 }
76
77 [[nodiscard]] JPL_INLINE Vec3BufferView<T> MakeView(std::size_t offset, std::size_t count) noexcept
78 {
79 JPL_ASSERT((offset + count) <= N);
80 return Vec3BufferView<T>{ .X = X + offset, .Y = Y + offset, .Z = Z + offset, .Count = count };
81 }
82 };
83
84 //======================================================================
87 template<Impl::CSIMDorFloat T>
89 {
90 T* X;
91 T* Y;
92 T* Z;
93 std::size_t Count = 0;
94
95 [[nodiscard]] JPL_INLINE std::size_t size() const noexcept { return Count; }
96
97 template<CVec3 Vec3Type>
98 JPL_INLINE void Unpack(std::span<Vec3Type> outVec3s) const
99 {
100 if constexpr (std::same_as<T, float>)
101 {
103 for (uint32 i = 0; i < size(); ++i)
104 {
105 Vec3Type& outVec = outVec3s[i];
106 SetX(outVec, X[i]); SetY(outVec, Y[i]); SetZ(outVec, Z[i]);
107 }
108 }
109 else
110 {
112
113 for (uint32 i = 0; i < size(); ++i)
114 {
115 float xs[simd::size()]{}; X[i].store(xs);
116 float ys[simd::size()]{}; Y[i].store(ys);
117 float zs[simd::size()]{}; Z[i].store(zs);
118 for (uint32 ii = 0; ii < simd::size(); ++ii)
119 {
120 Vec3Type& outVec = outVec3s[i * simd::size() + ii];
121 SetX(outVec, xs[ii]); SetY(outVec, ys[ii]); SetZ(outVec, zs[ii]);
122 }
123 }
124 }
125 }
126
128 {
129 JPL_ASSERT(other.size() == size());
130 std::memcpy(other.X, X, sizeof(T) * size());
131 std::memcpy(other.Y, Y, sizeof(T) * size());
132 std::memcpy(other.Z, Z, sizeof(T) * size());
133 }
134 };
135} // namespace JPL
#define JPL_ASSERT(inExpression,...)
Main assert macro, usage: JPL_ASSERT(condition, message) or JPL_ASSERT(condition)
Definition ErrorReporting.h:76
Definition Vec3Buffer.h:37
Definition AcousticMaterial.h:36
JPL_INLINE void SetZ(Vec3Type &v, Vec3FloatType< Vec3Type > value) noexcept
Definition Vec3Traits.h:41
JPL_INLINE void SetY(Vec3Type &v, Vec3FloatType< Vec3Type > value) noexcept
Definition Vec3Traits.h:40
std::uint32_t uint32
Definition Core.h:311
JPL_INLINE void SetX(Vec3Type &v, Vec3FloatType< Vec3Type > value) noexcept
Definition Vec3Traits.h:39
JPL_INLINE simd min(const simd &a, const simd &b) noexcept
Element-wise min.
Definition SIMD.h:1813
Definition Vec3Buffer.h:89
std::size_t Count
Definition Vec3Buffer.h:93
T * Z
Definition Vec3Buffer.h:92
JPL_INLINE std::size_t size() const noexcept
Definition Vec3Buffer.h:95
T * Y
Definition Vec3Buffer.h:91
JPL_INLINE void Unpack(std::span< Vec3Type > outVec3s) const
Definition Vec3Buffer.h:98
JPL_INLINE void CopyTo(Vec3BufferView &other) const
Definition Vec3Buffer.h:127
T * X
Definition Vec3Buffer.h:90
Definition Vec3Buffer.h:66
T Y[N]
Definition Vec3Buffer.h:68
JPL_INLINE Vec3BufferView< T > MakeView(std::size_t offset, std::size_t count) noexcept
Definition Vec3Buffer.h:77
T X[N]
Definition Vec3Buffer.h:67
JPL_INLINE Vec3BufferView< T > MakeView(std::size_t count) noexcept
Definition Vec3Buffer.h:71
T Z[N]
Definition Vec3Buffer.h:69
JPL_INLINE void store(float *mem) const
Store values from simd to provided memory location.
Definition SIMD.h:573
static constexpr std::size_t size() noexcept
Get number of element of the vector.
Definition SIMD.h:97