Skip to content

Commit 03c9042

Browse files
committed
Created transpose partial spec for emulated matrices
1 parent 20bff2c commit 03c9042

File tree

5 files changed

+69
-11
lines changed

5 files changed

+69
-11
lines changed

include/nbl/builtin/hlsl/cpp_compat/impl/intrinsics_impl.hlsl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define _NBL_BUILTIN_HLSL_CPP_COMPAT_IMPL_INTRINSICS_IMPL_INCLUDED_
33

44
#include <nbl/builtin/hlsl/cpp_compat/basic.h>
5+
#include <nbl/builtin/hlsl/matrix_utils/matrix_traits.hlsl>
56
#include <nbl/builtin/hlsl/concepts.hlsl>
67

78
namespace nbl
@@ -362,6 +363,24 @@ struct lerp_helper<vector<T, N>, vector<bool, N> >
362363
}
363364
};
364365

366+
template<typename Matrix>
367+
struct transpose_helper;
368+
369+
template<typename T, int N, int M>
370+
struct transpose_helper<matrix<T, N, M> >
371+
{
372+
using transposed_t = typename matrix_traits<matrix<T, N, M> >::transposed_type;
373+
374+
static transposed_t transpose(NBL_CONST_REF_ARG(matrix<T, N, M>) m)
375+
{
376+
#ifdef __HLSL_VERSION
377+
return spirv::transpose(m);
378+
#else
379+
return reinterpret_cast<transposed_t&>(glm::transpose(reinterpret_cast<typename matrix<T, N, M>::Base const&>(m)));
380+
#endif
381+
}
382+
};
383+
365384
}
366385
}
367386
}

include/nbl/builtin/hlsl/cpp_compat/intrinsics.hlsl

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <nbl/builtin/hlsl/spirv_intrinsics/core.hlsl>
99
#include <nbl/builtin/hlsl/spirv_intrinsics/glsl.std.450.hlsl>
1010
#include <nbl/builtin/hlsl/cpp_compat/impl/intrinsics_impl.hlsl>
11+
#include <nbl/builtin/hlsl/matrix_utils/matrix_traits.hlsl>
1112

1213
#ifndef __HLSL_VERSION
1314
#include <algorithm>
@@ -121,14 +122,10 @@ inline T lerp(NBL_CONST_REF_ARG(T) x, NBL_CONST_REF_ARG(T) y, NBL_CONST_REF_ARG(
121122
}
122123

123124
// transpose not defined cause its implemented via hidden friend
124-
template<typename T, uint16_t N, uint16_t M>
125-
inline matrix<T, M, N> transpose(NBL_CONST_REF_ARG(matrix<T, N, M>) m)
125+
template<typename Matrix>
126+
inline typename matrix_traits<Matrix>::transposed_type transpose(NBL_CONST_REF_ARG(Matrix) m)
126127
{
127-
#ifdef __HLSL_VERSION
128-
return spirv::transpose(m);
129-
#else
130-
return reinterpret_cast<matrix<T, M, N>&>(glm::transpose(reinterpret_cast<typename matrix<T, N, M>::Base const&>(m)));
131-
#endif
128+
return cpp_compat_intrinsics_impl::transpose_helper<Matrix>::transpose(m);
132129
}
133130

134131
template<typename T>

include/nbl/builtin/hlsl/emulated/float64_t_impl.hlsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,12 @@ NBL_CONSTEXPR_INLINE_FUNC bool areBothInfinity(uint64_t lhs, uint64_t rhs)
153153

154154
NBL_CONSTEXPR_INLINE_FUNC bool areBothZero(uint64_t lhs, uint64_t rhs)
155155
{
156-
return ((lhs << 1) == 0ull) && ((rhs << 1) == 0ull);
156+
return !bool((lhs | rhs) << 1);
157157
}
158158

159159
NBL_CONSTEXPR_INLINE_FUNC bool areBothSameSignZero(uint64_t lhs, uint64_t rhs)
160160
{
161-
return ((lhs << 1) == 0ull) && (lhs == rhs);
161+
return !bool((lhs) << 1) && (lhs == rhs);
162162
}
163163

164164
enum OperatorType

include/nbl/builtin/hlsl/emulated/matrix_t.hlsl

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define _NBL_BUILTIN_HLSL_EMULATED_MATRIX_T_HLSL_INCLUDED_
33

44
#include <nbl/builtin/hlsl/portable/float64_t.hlsl>
5+
#include <nbl/builtin/hlsl/matrix_utils/matrix_traits.hlsl>
56

67
namespace nbl
78
{
@@ -20,7 +21,7 @@ struct emulated_matrix
2021

2122
vec_t rows[RowCount];
2223

23-
transposed_t getTransposed()
24+
transposed_t getTransposed() NBL_CONST_MEMBER_FUNC
2425
{
2526
static nbl::hlsl::array_get<typename this_t::vec_t, T> getter;
2627
static nbl::hlsl::array_set<typename transposed_t::vec_t, T> setter;
@@ -47,8 +48,29 @@ using emulated_matrix_t4x4 = emulated_matrix<EmulatedType, 4, 4>;
4748
template<typename EmulatedType>
4849
using emulated_matrix_t3x4 = emulated_matrix<EmulatedType, 3, 4>;
4950

51+
// i choose to implement it this way because of this DXC bug: https://github.com/microsoft/DirectXShaderCompiler/issues/7007
52+
#define DEFINE_MATRIX_TRAITS_TEMPLATE_SPECIALIZATION(ROW_COUNT, COLUMN_COUNT) \
53+
template<typename T> \
54+
struct matrix_traits<emulated_matrix<T, ROW_COUNT, COLUMN_COUNT> > \
55+
{ \
56+
using scalar_type = T; \
57+
using row_type = vector<T, COLUMN_COUNT>; \
58+
using transposed_type = emulated_matrix<T, COLUMN_COUNT, ROW_COUNT>; \
59+
NBL_CONSTEXPR_STATIC_INLINE uint32_t RowCount = ROW_COUNT; \
60+
NBL_CONSTEXPR_STATIC_INLINE uint32_t ColumnCount = COLUMN_COUNT; \
61+
NBL_CONSTEXPR_STATIC_INLINE bool Square = RowCount == ColumnCount; \
62+
};
63+
64+
DEFINE_MATRIX_TRAITS_TEMPLATE_SPECIALIZATION(2, 2)
65+
DEFINE_MATRIX_TRAITS_TEMPLATE_SPECIALIZATION(3, 3)
66+
DEFINE_MATRIX_TRAITS_TEMPLATE_SPECIALIZATION(4, 4)
67+
DEFINE_MATRIX_TRAITS_TEMPLATE_SPECIALIZATION(3, 4)
68+
69+
#undef DEFINE_MATRIX_TRAITS_TEMPLATE_SPECIALIZATION
70+
5071
namespace emulated_matrix_impl
5172
{
73+
// TODO: move to cpp_compat/impl/intrinsics_impl.hlsl
5274
template<typename LhsT, typename RhsT>
5375
struct mul_helper
5476
{
@@ -77,8 +99,24 @@ struct mul_helper<emulated_matrix<ComponentT, RowCount, ColumnCount>, emulated_v
7799
return output;
78100
}
79101
};
102+
103+
}
104+
105+
namespace cpp_compat_intrinsics_impl
106+
{
107+
template<typename T, int N, int M>
108+
struct transpose_helper<emulated_matrix<T, N, M> >
109+
{
110+
using transposed_t = typename matrix_traits<emulated_matrix<T, N, M> >::transposed_type;
111+
112+
static transposed_t transpose(NBL_CONST_REF_ARG(emulated_matrix<T, N, M>) m)
113+
{
114+
return m.getTransposed();
115+
}
116+
};
80117
}
81118

119+
// TODO: move to cpp_compat/intrinsics.hlsl
82120
// TODO: concepts, to ensure that LhsT is a matrix and RhsT is a vector type
83121
template<typename MatT, typename VecT>
84122
VecT mul(MatT mat, VecT vec)

include/nbl/builtin/hlsl/matrix_utils/matrix_traits.hlsl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#ifndef _NBL_BUILTIN_HLSL_MATRIX_UTILS_MATRIX_TRAITS_INCLUDED_
22
#define _NBL_BUILTIN_HLSL_MATRIX_UTILS_MATRIX_TRAITS_INCLUDED_
33

4-
#include <nbl/builtin/hlsl/cpp_compat.hlsl>
4+
#include <nbl/builtin/hlsl/cpp_compat/basic.h>
5+
#include <nbl/builtin/hlsl/cpp_compat/matrix.hlsl>
56

67
namespace nbl
78
{
@@ -18,6 +19,7 @@ struct matrix_traits<matrix<T, ROW_COUNT, COLUMN_COUNT> > \
1819
{ \
1920
using scalar_type = T; \
2021
using row_type = vector<T, COLUMN_COUNT>; \
22+
using transposed_type = matrix<T, COLUMN_COUNT, ROW_COUNT>; \
2123
NBL_CONSTEXPR_STATIC_INLINE uint32_t RowCount = ROW_COUNT; \
2224
NBL_CONSTEXPR_STATIC_INLINE uint32_t ColumnCount = COLUMN_COUNT; \
2325
NBL_CONSTEXPR_STATIC_INLINE bool Square = RowCount == ColumnCount; \
@@ -28,6 +30,8 @@ DEFINE_MATRIX_TRAITS_TEMPLATE_SPECIALIZATION(3, 3)
2830
DEFINE_MATRIX_TRAITS_TEMPLATE_SPECIALIZATION(4, 4)
2931
DEFINE_MATRIX_TRAITS_TEMPLATE_SPECIALIZATION(3, 4)
3032

33+
#undef DEFINE_MATRIX_TRAITS_TEMPLATE_SPECIALIZATION
34+
3135
// TODO: when this bug: https://github.com/microsoft/DirectXShaderCompiler/issues/7007 is fixed, uncomment and delete template specializations
3236
/*template<typename T, uint32_t N, uint32_t M>
3337
struct matrix_traits<matrix<T,N,M> >

0 commit comments

Comments
 (0)