Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions include/nbl/builtin/hlsl/array_accessors.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define _NBL_BUILTIN_HLSL_ARRAY_ACCESSORS_HLSL_INCLUDED_

#include <nbl/builtin/hlsl/cpp_compat/basic.h>
#include <nbl/builtin/hlsl/concepts.hlsl>
#include <nbl/builtin/hlsl/matrix_utils/matrix_traits.hlsl>

namespace nbl
{
Expand All @@ -25,6 +27,54 @@ struct array_set
}
};

namespace impl
{

template<typename T NBL_STRUCT_CONSTRAINABLE>
struct MatrixComponentSetterHelper;
template<typename T NBL_STRUCT_CONSTRAINABLE>
struct MatrixComponentGetterHelper;

// this concept will check whether MatrixType is a matrix and whether its component is a native type scalar, should work only for HLSL matrices
#define MATRIX_COMPONENT_IS_NATIVE_TYPE nbl::hlsl::matrix_traits<MatrixType>::IsMatrix && nbl::hlsl::is_scalar_v<typename nbl::hlsl::matrix_traits<MatrixType>::scalar_type>

template<typename MatrixType>
NBL_PARTIAL_REQ_TOP(MATRIX_COMPONENT_IS_NATIVE_TYPE)
struct MatrixComponentSetterHelper<MatrixType NBL_PARTIAL_REQ_BOT(MATRIX_COMPONENT_IS_NATIVE_TYPE) >
{
using ComponentType = typename nbl::hlsl::matrix_traits<MatrixType>::scalar_type;
static void __call(NBL_REF_ARG(MatrixType) mat, uint16_t row, uint16_t column, ComponentType value)
{
mat[row][column] = value;
}
};
template<typename MatrixType>
NBL_PARTIAL_REQ_TOP(MATRIX_COMPONENT_IS_NATIVE_TYPE)
struct MatrixComponentGetterHelper<MatrixType NBL_PARTIAL_REQ_BOT(MATRIX_COMPONENT_IS_NATIVE_TYPE) >
{
using ComponentType = typename nbl::hlsl::matrix_traits<MatrixType>::scalar_type;
static ComponentType __call(NBL_REF_ARG(MatrixType) mat, uint16_t row, uint16_t column)
{
return mat[row][column];
}
};

#undef MATRIX_COMPONENT_IS_NATIVE_TYPE

}

template<typename MatrixType>
void matrix_component_set(NBL_REF_ARG(MatrixType) mat, uint16_t row, uint16_t column, typename nbl::hlsl::matrix_traits<MatrixType>::scalar_type value)
{
impl::MatrixComponentSetterHelper<MatrixType>::__call(mat, row, column, value);
}

template<typename MatrixType>
typename nbl::hlsl::matrix_traits<MatrixType>::scalar_type matrix_component_get(NBL_REF_ARG(MatrixType) mat, uint16_t row, uint16_t column)
{
return impl::MatrixComponentGetterHelper<MatrixType>::__call(mat, row, column);
}

}
}

Expand Down
34 changes: 34 additions & 0 deletions include/nbl/builtin/hlsl/emulated/matrix_t.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <nbl/builtin/hlsl/portable/float64_t.hlsl>
#include <nbl/builtin/hlsl/emulated/vector_t.hlsl>
#include <nbl/builtin/hlsl/matrix_utils/matrix_traits.hlsl>
#include <nbl/builtin/hlsl/array_accessors.hlsl>

namespace nbl
{
Expand Down Expand Up @@ -143,6 +144,39 @@ struct mul_helper<emulated_matrix<ComponentT, RowCount, ColumnCount>, emulated_v
};
}

namespace impl
{

// this concept will check whether MatrixType is a matrix and whether its component is not a native type scalar, should work only for emulated matrices
#define MATRIX_COMPONENT_IS_EMULATED nbl::hlsl::matrix_traits<MatrixType>::IsMatrix && !nbl::hlsl::is_scalar_v<typename nbl::hlsl::matrix_traits<MatrixType>::scalar_type> && (nbl::hlsl::concepts::FloatingPointLikeScalar<typename nbl::hlsl::matrix_traits<MatrixType>::scalar_type> || nbl::hlsl::concepts::IntegralLikeScalar<typename nbl::hlsl::matrix_traits<MatrixType>::scalar_type>)

template<typename MatrixType>
NBL_PARTIAL_REQ_TOP(MATRIX_COMPONENT_IS_EMULATED)
struct MatrixComponentSetterHelper<MatrixType NBL_PARTIAL_REQ_BOT(MATRIX_COMPONENT_IS_EMULATED) >
{
using ComponentType = typename nbl::hlsl::matrix_traits<MatrixType>::scalar_type;
static void __call(NBL_REF_ARG(MatrixType) mat, uint16_t row, uint16_t column, ComponentType value)
{
mat.rows[row].setComponent(column, value);
}
};

template<typename MatrixType>
NBL_PARTIAL_REQ_TOP(MATRIX_COMPONENT_IS_EMULATED)
struct MatrixComponentGetterHelper<MatrixType NBL_PARTIAL_REQ_BOT(MATRIX_COMPONENT_IS_EMULATED) >
{
using ComponentType = typename nbl::hlsl::matrix_traits<MatrixType>::scalar_type;
static ComponentType __call(NBL_REF_ARG(MatrixType) mat, uint16_t row, uint16_t column)
{
return mat.rows[row].getComponent(column);
}
};

#undef MATRIX_COMPONENT_IS_EMULATED

}


}
}
#endif
Loading