From 9cd86a3110499b35c16ba39306765b68908ad2ff Mon Sep 17 00:00:00 2001 From: Przemog1 Date: Tue, 21 Jul 2026 19:19:32 +0200 Subject: [PATCH] Implemented matrix accessors --- include/nbl/builtin/hlsl/array_accessors.hlsl | 50 +++++++++++++++++++ .../nbl/builtin/hlsl/emulated/matrix_t.hlsl | 34 +++++++++++++ 2 files changed, 84 insertions(+) diff --git a/include/nbl/builtin/hlsl/array_accessors.hlsl b/include/nbl/builtin/hlsl/array_accessors.hlsl index 73a4b83102..885306cf48 100644 --- a/include/nbl/builtin/hlsl/array_accessors.hlsl +++ b/include/nbl/builtin/hlsl/array_accessors.hlsl @@ -2,6 +2,8 @@ #define _NBL_BUILTIN_HLSL_ARRAY_ACCESSORS_HLSL_INCLUDED_ #include +#include +#include namespace nbl { @@ -25,6 +27,54 @@ struct array_set } }; +namespace impl +{ + +template +struct MatrixComponentSetterHelper; +template +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::IsMatrix && nbl::hlsl::is_scalar_v::scalar_type> + +template +NBL_PARTIAL_REQ_TOP(MATRIX_COMPONENT_IS_NATIVE_TYPE) +struct MatrixComponentSetterHelper +{ + using ComponentType = typename nbl::hlsl::matrix_traits::scalar_type; + static void __call(NBL_REF_ARG(MatrixType) mat, uint16_t row, uint16_t column, ComponentType value) + { + mat[row][column] = value; + } +}; +template +NBL_PARTIAL_REQ_TOP(MATRIX_COMPONENT_IS_NATIVE_TYPE) +struct MatrixComponentGetterHelper +{ + using ComponentType = typename nbl::hlsl::matrix_traits::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 +void matrix_component_set(NBL_REF_ARG(MatrixType) mat, uint16_t row, uint16_t column, typename nbl::hlsl::matrix_traits::scalar_type value) +{ + impl::MatrixComponentSetterHelper::__call(mat, row, column, value); +} + +template +typename nbl::hlsl::matrix_traits::scalar_type matrix_component_get(NBL_REF_ARG(MatrixType) mat, uint16_t row, uint16_t column) +{ + return impl::MatrixComponentGetterHelper::__call(mat, row, column); +} + } } diff --git a/include/nbl/builtin/hlsl/emulated/matrix_t.hlsl b/include/nbl/builtin/hlsl/emulated/matrix_t.hlsl index d605538e36..7bc0efee79 100644 --- a/include/nbl/builtin/hlsl/emulated/matrix_t.hlsl +++ b/include/nbl/builtin/hlsl/emulated/matrix_t.hlsl @@ -4,6 +4,7 @@ #include #include #include +#include namespace nbl { @@ -143,6 +144,39 @@ struct mul_helper, 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::IsMatrix && !nbl::hlsl::is_scalar_v::scalar_type> && (nbl::hlsl::concepts::FloatingPointLikeScalar::scalar_type> || nbl::hlsl::concepts::IntegralLikeScalar::scalar_type>) + +template +NBL_PARTIAL_REQ_TOP(MATRIX_COMPONENT_IS_EMULATED) +struct MatrixComponentSetterHelper +{ + using ComponentType = typename nbl::hlsl::matrix_traits::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 +NBL_PARTIAL_REQ_TOP(MATRIX_COMPONENT_IS_EMULATED) +struct MatrixComponentGetterHelper +{ + using ComponentType = typename nbl::hlsl::matrix_traits::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