74 int Alignment = (((NumCol * NumRow) % 4 == 0) ? 4 : 1) *
sizeof(T)>
82 static constexpr int min_dim = (NumRow < NumCol) ? NumRow : NumCol;
83 static constexpr int col_len = NumCol;
84 static constexpr int row_len = NumRow;
89#define BLI_ENABLE_IF_MAT(_size, _test) int S = _size, BLI_ENABLE_IF((S _test))
104 template<BLI_ENABLE_IF_MAT(NumCol, == 4)>
115 template<
typename U,
int OtherNumCol,
int OtherNumRow>
118 if constexpr ((OtherNumRow >= NumRow) && (OtherNumCol >= NumCol)) {
119 unroll<NumCol>([&](
auto i) { (*this)[i] =
col_type(other[i]); });
123 unroll<NumCol>([&](
auto i) {
124 unroll<NumRow>([&](
auto j) {
125 if (i < OtherNumCol && j < OtherNumRow) {
126 (*this)[i][j] = other[i][j];
129 (*this)[i][j] =
T(1);
132 (*this)[i][j] =
T(0);
139#undef BLI_ENABLE_IF_MAT
145 unroll<NumCol>([&](
auto i) { (*this)[i] =
reinterpret_cast<const col_type *
>(
ptr)[i]; });
148 template<
typename U, BLI_ENABLE_IF((std::is_convertible_v<U, T>))>
explicit MatBase(
const U *
ptr)
150 unroll<NumCol>([&](
auto i) { (*this)[i] =
ptr[i]; });
159 unroll<NumCol>([&](
auto i) { (*this)[i] =
col_type(vec[i]); });
169 return *
reinterpret_cast<const c_style_mat *
>(
this);
181 return reinterpret_cast<const T *
>(
this);
187 return reinterpret_cast<T *
>(
this);
192 template<
int ViewNumCol = NumCol,
193 int ViewNumRow = NumRow,
203 template<
int ViewNumCol = NumCol,
204 int ViewNumRow = NumRow,
226 return reinterpret_cast<const col_type *
>(
this)[index];
233 return reinterpret_cast<col_type *
>(
this)[index];
242 return *
reinterpret_cast<vec3_type *
>(&(*this)[0]);
249 return *
reinterpret_cast<vec3_type *
>(&(*this)[1]);
256 return *
reinterpret_cast<vec3_type *
>(&(*this)[2]);
263 return *
reinterpret_cast<loc_type *
>(&(*this)[NumCol - 1]);
270 return *
reinterpret_cast<const vec3_type *
>(&(*this)[0]);
277 return *
reinterpret_cast<const vec3_type *
>(&(*this)[1]);
284 return *
reinterpret_cast<const vec3_type *
>(&(*this)[2]);
291 return *
reinterpret_cast<const loc_type *
>(&(*this)[NumCol - 1]);
299 unroll<NumCol>([&](
auto i) { result[i] = a[i] +
b[i]; });
306 unroll<NumCol>([&](
auto i) { result[i] = a[i] +
b; });
317 unroll<NumCol>([&](
auto i) { (*this)[i] +=
b[i]; });
323 unroll<NumCol>([&](
auto i) { (*this)[i] +=
b; });
330 unroll<NumCol>([&](
auto i) { result[i] = -a[i]; });
337 unroll<NumCol>([&](
auto i) { result[i] = a[i] -
b[i]; });
344 unroll<NumCol>([&](
auto i) { result[i] = a[i] -
b; });
351 unroll<NumCol>([&](
auto i) { result[i] = a -
b[i]; });
357 unroll<NumCol>([&](
auto i) { (*this)[i] -=
b[i]; });
363 unroll<NumCol>([&](
auto i) { (*this)[i] -=
b; });
371 unroll<NumCol>([&](
auto i) { result[i] = a[i] *
b; });
392 unroll<NumCol>([&](
auto i) { (*this)[i] *=
b; });
403 unroll<NumCol>([&](
auto c) { result +=
b[c] * a[c]; });
413 unroll<NumCol>([&](
auto c) { unroll<NumRow>([&](
auto r) { result[c] +=
b[c][r] * a[r]; }); });
421 for (
int i = 0; i < NumCol; i++) {
439 unroll<min_dim>([&](
auto i) { result[i][i] = value; });
446 unroll<NumCol>([&](
auto i) { result[i] =
col_type(value); });
463 unroll<NumCol * NumRow>([&](
auto i) {
464 T value = (
reinterpret_cast<const T *
>(
this))[i];
473 for (
int i = 0; i < NumRow; i++) {
475 for (
int j = 0; j < NumCol; j++) {
478 if (j < NumCol - 1) {
483 if (i < NumRow - 1) {
518 BLI_STATIC_ASSERT(SrcStartCol >= 0,
"View does not fit source matrix dimensions");
519 BLI_STATIC_ASSERT(SrcStartRow >= 0,
"View does not fit source matrix dimensions");
521 "View does not fit source matrix dimensions");
523 "View does not fit source matrix dimensions");
527 explicit MatView(
const float (*src)[SrcNumRow])
536 return *
reinterpret_cast<const col_type *
>(&mat[index + SrcStartCol][SrcStartRow]);
544 unroll<NumCol>([&](
auto c) { mat[c] = (*this)[c]; });
553 unroll<NumCol>([&](
auto i) { result[i] = a[i] +
b; });
565 unroll<NumCol>([&](
auto i) { result[i] = -a[i]; });
569 template<
int OtherSrcNumCol,
571 int OtherSrcStartCol,
572 int OtherSrcStartRow,
573 int OtherSrcAlignment>
582 OtherSrcAlignment> &
b)
585 unroll<NumCol>([&](
auto i) { result[i] = a[i] -
b[i]; });
594 template<
int OtherSrcNumCol,
596 int OtherSrcStartCol,
597 int OtherSrcStartRow,
598 int OtherSrcAlignment>
606 OtherSrcAlignment> &a,
610 unroll<NumCol>([&](
auto i) { result[i] = a[i] -
b[i]; });
622 unroll<NumCol>([&](
auto i) { result[i] = a[i] -
b; });
629 unroll<NumCol>([&](
auto i) { result[i] = a -
b[i]; });
635 return *
this *
b.view();
642 unroll<NumCol>([&](
auto i) { result[i] = a[i] *
b; });
659 unroll<NumCol>([&](
auto c) { result +=
b[c] * a[c]; });
669 unroll<NumCol>([&](
auto c) { unroll<NumRow>([&](
auto r) { result[c] +=
b[c][r] * a[r]; }); });
677 for (
int i = 0; i < NumCol; i++) {
694 return stream << mat->
mat;
711 :
MatView<T, NumCol, NumRow, SrcNumCol, SrcNumRow, SrcStartCol, SrcStartRow, SrcAlignment> {
745 template<
int OtherSrcNumCol,
747 int OtherSrcStartCol,
748 int OtherSrcStartRow,
749 int OtherSrcAlignment>
757 OtherSrcAlignment> &other)
760 (
reinterpret_cast<const void *
>(&other.mat[0][0]) !=
761 reinterpret_cast<const void *
>(&this->mat[0][0])) ||
763 ((OtherSrcStartCol > SrcStartCol) || (OtherSrcStartCol + NumCol <= SrcStartCol) ||
764 (OtherSrcStartRow > SrcStartRow + NumRow) ||
765 (OtherSrcStartRow + NumRow <= SrcStartRow)),
766 "Operation is undefined if views overlap.");
767 unroll<NumCol>([&](
auto i) { (*this)[i] = other[i]; });
773 *
this = other.view();
779 template<
int OtherSrcNumCol,
781 int OtherSrcStartCol,
782 int OtherSrcStartRow,
783 int OtherSrcAlignment>
791 OtherSrcAlignment> &
b)
793 unroll<NumCol>([&](
auto i) { (*this)[i] +=
b[i]; });
799 return *
this +=
b.view();
804 unroll<NumCol>([&](
auto i) { (*this)[i] +=
b; });
808 template<
int OtherSrcNumCol,
810 int OtherSrcStartCol,
811 int OtherSrcStartRow,
812 int OtherSrcAlignment>
820 OtherSrcAlignment> &
b)
822 unroll<NumCol>([&](
auto i) { (*this)[i] -=
b[i]; });
828 return *
this -=
b.view();
833 unroll<NumCol>([&](
auto i) { (*this)[i] -=
b; });
838 template<
int OtherSrcNumCol,
840 int OtherSrcStartCol,
841 int OtherSrcStartRow,
842 int OtherSrcAlignment>
850 OtherSrcAlignment> &
b)
852 *
this = *
static_cast<MatViewT *
>(
this) *
b;
858 return *
this *=
b.view();
864 unroll<NumCol>([&](
auto i) { (*this)[i] *=
b; });
875 unroll<NumCol>([&](
auto c) { result +=
b[c] * a[c]; });
885 unroll<NumCol>([&](
auto c) { unroll<NumRow>([&](
auto r) { result[c] +=
b[c][r] * a[r]; }); });
902 static_assert(A_NumCol == B_NumRow);
906 unroll<B_NumCol>([&](
auto j) {
907 unroll<A_NumRow>([&](
auto i) {
909 unroll<A_NumCol>([&](
auto k) { result[j][i] += a[k][i] *
b[j][k]; });
917template<
typename T,
int A_NumCol,
int A_NumRow,
int B_NumCol,
int B_NumRow>
921 return detail::matrix_mul_impl<T, A_NumCol, A_NumRow, B_NumCol, B_NumRow>(a,
b);
956 return detail::matrix_mul_impl<T, A_NumCol, A_NumRow, B_NumCol, B_NumRow>(a,
b);
978 return detail::matrix_mul_impl<T, A_NumCol, A_NumRow, B_NumCol, B_NumRow>(a,
b);
1001 return detail::matrix_mul_impl<T, A_NumCol, A_NumRow, B_NumCol, B_NumRow>(a,
b);
#define BLI_STATIC_ASSERT(a, msg)
#define BLI_assert_msg(a, msg)
local_group_size(16, 16) .push_constant(Type b
draw_view in_light_buf[] float
ccl_device_inline float2 operator*(const float2 a, const float2 b)
MatBase< T, B_NumCol, A_NumRow > matrix_mul_impl(const MatA &a, const MatB &b)
std::conditional_t< sizeof(T)==sizeof(uint8_t), uint8_t, std::conditional_t< sizeof(T)==sizeof(uint16_t), uint16_t, std::conditional_t< sizeof(T)==sizeof(uint32_t), uint32_t, std::conditional_t< sizeof(T)==sizeof(uint64_t), uint64_t, void > > > > as_uint_type
unsigned __int64 uint64_t
friend MatBase operator-(const MatBase &a)
static MatBase diagonal(T value)
friend MatBase operator*(const MatBase &a, T b)
friend MatBase operator-(T a, const MatBase &b)
MatBase & operator+=(const MatBase &b)
friend MatBase operator+(T a, const MatBase &b)
MatBase(col_type _x, col_type _y, col_type _z)
MatBase(const MatBase< U, NumRow, NumCol > &vec)
MatBase(col_type _x, col_type _y, col_type _z, col_type _w)
MatBase & operator*=(T b)
MatBase & operator-=(T b)
MatBase(const T(*ptr)[NumCol])
col_type & operator[](int index)
MatBase & operator-=(const MatBase &b)
T[NumCol][NumRow] c_style_mat
friend col_type operator*(const MatBase &a, const row_type &b)
MutableMatView< T, ViewNumCol, ViewNumRow, NumCol, NumRow, SrcStartCol, SrcStartRow, Alignment > view()
friend MatBase operator+(const MatBase &a, T b)
const vec3_type & z_axis() const
friend MatBase operator*(T a, const MatBase &b)
const vec3_type & x_axis() const
friend MatBase operator-(const MatBase &a, T b)
const col_type & operator[](int index) const
friend row_type operator*(const col_type &a, const MatBase &b)
friend MatBase operator+(const MatBase &a, const MatBase &b)
MatBase & operator*=(const MatBase &b)
friend std::ostream & operator<<(std::ostream &stream, const MatBase &mat)
const c_style_mat & ptr() const
friend MatBase operator-(const MatBase &a, const MatBase &b)
const vec3_type & y_axis() const
MatBase(const MatBase< U, OtherNumCol, OtherNumRow > &other)
friend bool operator!=(const MatBase &a, const MatBase &b)
const MatView< T, ViewNumCol, ViewNumRow, NumCol, NumRow, SrcStartCol, SrcStartRow, Alignment > view() const
friend bool operator==(const MatBase &a, const MatBase &b)
static MatBase all(T value)
MatBase & operator+=(T b)
const loc_type & location() const
const T * base_ptr() const
static MatBase identity()
MatBase(col_type _x, col_type _y)
friend MatT operator-(const MatView &a, const MatView< T, NumCol, NumRow, OtherSrcNumCol, OtherSrcNumRow, OtherSrcStartCol, OtherSrcStartRow, OtherSrcAlignment > &b)
const col_type & operator[](int index) const
friend MatT operator-(const MatT &a, const MatView &b)
MatT operator*(const MatT &b) const
MatView(const SrcMatT &src)
friend MatT operator*(const MatView &a, T b)
friend bool operator!=(const MatView &a, const MatView &b)
friend MatT operator-(const MatView &a, T b)
friend MatT operator-(const MatView &a)
friend std::ostream & operator<<(std::ostream &stream, const MatView &mat)
friend col_type operator*(const MatView &a, const row_type &b)
friend bool operator==(const MatView &a, const MatView &b)
friend MatT operator-(const MatView< T, NumCol, NumRow, OtherSrcNumCol, OtherSrcNumRow, OtherSrcStartCol, OtherSrcStartRow, OtherSrcAlignment > &a, const MatView &b)
friend row_type operator*(const col_type &a, const MatView &b)
friend MatT operator+(const MatView &a, T b)
friend MatT operator+(T a, const MatView &b)
friend MatT operator-(const MatView &a, const MatT &b)
MatView(const float(*src)[SrcNumRow])
friend MatT operator*(T a, const MatView &b)
friend MatView operator-(T a, const MatView &b)
friend col_type operator*(MutableMatView &a, const row_type &b)
friend row_type operator*(const col_type &a, MutableMatView &b)
MutableMatView & operator*=(const MatView< T, NumCol, NumRow, OtherSrcNumCol, OtherSrcNumRow, OtherSrcStartCol, OtherSrcStartRow, OtherSrcAlignment > &b)
MutableMatView & operator+=(const MatView< T, NumCol, NumRow, OtherSrcNumCol, OtherSrcNumRow, OtherSrcStartCol, OtherSrcStartRow, OtherSrcAlignment > &b)
col_type & operator[](int index)
MutableMatView & operator-=(T b)
MutableMatView & operator*=(const MatT &b)
MutableMatView(SrcMatT &src)
MutableMatView & operator=(const MatView< T, NumCol, NumRow, OtherSrcNumCol, OtherSrcNumRow, OtherSrcStartCol, OtherSrcStartRow, OtherSrcAlignment > &other)
MutableMatView(float src[SrcNumCol][SrcNumRow])
MutableMatView & operator*=(T b)
MutableMatView & operator+=(const MatT &b)
MutableMatView & operator=(const MatT &other)
MutableMatView & operator+=(T b)
MutableMatView & operator-=(const MatT &b)
MutableMatView & operator-=(const MatView< T, NumCol, NumRow, OtherSrcNumCol, OtherSrcNumRow, OtherSrcStartCol, OtherSrcStartRow, OtherSrcAlignment > &b)