Skip to content
Merged
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
198 changes: 85 additions & 113 deletions include/nbl/builtin/hlsl/emulated/vector_t.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,6 @@ NBL_CONSTEXPR_FUNC this_t operator OP() NBL_CONST_MEMBER_FUNC \
}

#define NBL_EMULATED_VECTOR_ARITHMETIC_OPERATOR(OP)\
NBL_CONSTEXPR_FUNC this_t operator OP (component_t val) NBL_CONST_MEMBER_FUNC \
{\
this_t output;\
[[unroll]]\
for (uint32_t i = 0u; i < CRTP::Dimension; ++i)\
output.setComponent(i, this_t::getComponent(i) OP val);\
return output;\
}\
NBL_CONSTEXPR_FUNC this_t operator OP (this_t other) NBL_CONST_MEMBER_FUNC \
{\
this_t output;\
Expand Down Expand Up @@ -183,6 +175,14 @@ NBL_CONSTEXPR_FUNC vector<bool, CRTP::Dimension> operator OP (vector<component_t
#define NBL_EMULATED_VECTOR_CREATION_AND_COMPONENT_SUM \
using this_t = emulated_vector<ComponentType, CRTP>;\
using component_t = ComponentType;\
template<typename T>\
NBL_CONSTEXPR_STATIC this_t create(vector<T, CRTP::Dimension> other)\
Comment on lines +178 to +179

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs NBL_FUNC_REQUIRES(impl::assignable_v<component_t,T>) or something to check that you can assign

{\
this_t output;\
for (uint32_t i = 0u; i < CRTP::Dimension; ++i)\
output.setComponent(i, component_t::create(other[i]));\
return output;\
}\
NBL_CONSTEXPR_STATIC this_t create(this_t other)\
{\
CRTP output;\
Expand All @@ -209,6 +209,43 @@ NBL_CONSTEXPR_STATIC this_t create(vector<component_t, CRTP::Dimension> other)\
return output;\
}

#define DEFINE_OPERATORS_FOR_TYPE(...)\
NBL_CONSTEXPR_FUNC this_t operator+(__VA_ARGS__ val) NBL_CONST_MEMBER_FUNC \
{\
this_t output;\
for (uint32_t i = 0u; i < CRTP::Dimension; ++i)\
output.setComponent(i, CRTP::getComponent(i) + _static_cast<component_t>(val));\
\
return output;\
}\
\
NBL_CONSTEXPR_FUNC this_t operator-(__VA_ARGS__ val) NBL_CONST_MEMBER_FUNC \
{\
this_t output;\
for (uint32_t i = 0u; i < CRTP::Dimension; ++i)\
output.setComponent(i, CRTP::getComponent(i) - _static_cast<component_t>(val));\
\
return output;\
}\
\
NBL_CONSTEXPR_FUNC this_t operator*(__VA_ARGS__ val) NBL_CONST_MEMBER_FUNC \
{\
this_t output;\
for (uint32_t i = 0u; i < CRTP::Dimension; ++i)\
output.setComponent(i, CRTP::getComponent(i) * _static_cast<component_t>(val));\
\
return output;\
}\
\
NBL_CONSTEXPR_FUNC this_t operator/(__VA_ARGS__ val) NBL_CONST_MEMBER_FUNC \
{\
this_t output;\
for (uint32_t i = 0u; i < CRTP::Dimension; ++i)\
output.setComponent(i, CRTP::getComponent(i) / _static_cast<component_t>(val));\
\
return output;\
}\

// Fundamental, integral
template <typename ComponentType, typename CRTP> NBL_PARTIAL_REQ_TOP(is_fundamental_v<ComponentType> && concepts::IntegralLikeScalar<ComponentType>)
struct emulated_vector<ComponentType, CRTP NBL_PARTIAL_REQ_BOT(is_fundamental_v<ComponentType>&& concepts::IntegralLikeScalar<ComponentType>) > : CRTP
Expand All @@ -232,6 +269,15 @@ struct emulated_vector<ComponentType, CRTP NBL_PARTIAL_REQ_BOT(is_fundamental_v<
NBL_EMULATED_FUNDAMENTAL_TYPE_VECTOR_COMPARISON_OPERATOR(<=)
NBL_EMULATED_FUNDAMENTAL_TYPE_VECTOR_COMPARISON_OPERATOR(>)
NBL_EMULATED_FUNDAMENTAL_TYPE_VECTOR_COMPARISON_OPERATOR(>=)

DEFINE_OPERATORS_FOR_TYPE(emulated_uint64_t)
DEFINE_OPERATORS_FOR_TYPE(emulated_int64_t)
DEFINE_OPERATORS_FOR_TYPE(uint16_t)
DEFINE_OPERATORS_FOR_TYPE(uint32_t)
DEFINE_OPERATORS_FOR_TYPE(uint64_t)
DEFINE_OPERATORS_FOR_TYPE(int16_t)
DEFINE_OPERATORS_FOR_TYPE(int32_t)
DEFINE_OPERATORS_FOR_TYPE(int64_t)
};

// Fundamental, not integral
Expand All @@ -253,6 +299,15 @@ struct emulated_vector<ComponentType, CRTP NBL_PARTIAL_REQ_BOT(is_fundamental_v<
NBL_EMULATED_FUNDAMENTAL_TYPE_VECTOR_COMPARISON_OPERATOR(<=)
NBL_EMULATED_FUNDAMENTAL_TYPE_VECTOR_COMPARISON_OPERATOR(>)
NBL_EMULATED_FUNDAMENTAL_TYPE_VECTOR_COMPARISON_OPERATOR(>=)

DEFINE_OPERATORS_FOR_TYPE(emulated_uint64_t)
DEFINE_OPERATORS_FOR_TYPE(emulated_int64_t)
DEFINE_OPERATORS_FOR_TYPE(uint16_t)
DEFINE_OPERATORS_FOR_TYPE(uint32_t)
DEFINE_OPERATORS_FOR_TYPE(uint64_t)
DEFINE_OPERATORS_FOR_TYPE(int16_t)
DEFINE_OPERATORS_FOR_TYPE(int32_t)
DEFINE_OPERATORS_FOR_TYPE(int64_t)
};

// Not fundamental, integral
Expand All @@ -278,6 +333,20 @@ struct emulated_vector<ComponentType, CRTP NBL_PARTIAL_REQ_BOT(!is_fundamental_v
NBL_EMULATED_VECTOR_COMPARISON_OPERATOR(<=)
NBL_EMULATED_VECTOR_COMPARISON_OPERATOR(>)
NBL_EMULATED_VECTOR_COMPARISON_OPERATOR(>=)

DEFINE_OPERATORS_FOR_TYPE(emulated_float64_t<true, true>)
DEFINE_OPERATORS_FOR_TYPE(emulated_float64_t<true, false>)
DEFINE_OPERATORS_FOR_TYPE(emulated_float64_t<false, true>)
DEFINE_OPERATORS_FOR_TYPE(emulated_float64_t<false, false>)
DEFINE_OPERATORS_FOR_TYPE(float16_t)
DEFINE_OPERATORS_FOR_TYPE(float32_t)
DEFINE_OPERATORS_FOR_TYPE(float64_t)
DEFINE_OPERATORS_FOR_TYPE(uint16_t)
DEFINE_OPERATORS_FOR_TYPE(uint32_t)
DEFINE_OPERATORS_FOR_TYPE(uint64_t)
DEFINE_OPERATORS_FOR_TYPE(int16_t)
DEFINE_OPERATORS_FOR_TYPE(int32_t)
DEFINE_OPERATORS_FOR_TYPE(int64_t)
};

// Not fundamental, not integral
Expand All @@ -299,107 +368,12 @@ struct emulated_vector<ComponentType, CRTP NBL_PARTIAL_REQ_BOT(!is_fundamental_v
NBL_EMULATED_VECTOR_COMPARISON_OPERATOR(<=)
NBL_EMULATED_VECTOR_COMPARISON_OPERATOR(>)
NBL_EMULATED_VECTOR_COMPARISON_OPERATOR(>=)
};

#undef NBL_EMULATED_FUNDAMENTAL_TYPE_VECTOR_CREATION_AND_COMPONENT_SUM
#undef NBL_EMULATED_VECTOR_CREATION_AND_COMPONENT_SUM
#undef NBL_EMULATED_FUNDAMENTAL_TYPE_VECTOR_COMPARISON_OPERATOR
#undef NBL_EMULATED_VECTOR_COMPARISON_OPERATOR
#undef NBL_EMULATED_FUNDAMENTAL_TYPE_VECTOR_ARITHMETIC_OPERATOR
#undef NBL_EMULATED_VECTOR_ARITHMETIC_OPERATOR
#undef NBL_EMULATED_VECTOR_UNARY_OPERATOR

// ----------------------------------------------------- EMULATED FLOAT SPECIALIZATION --------------------------------------------------------------------

#define DEFINE_OPERATORS_FOR_TYPE(...)\
NBL_CONSTEXPR_FUNC this_t operator+(__VA_ARGS__ val) NBL_CONST_MEMBER_FUNC \
{\
this_t output;\
for (uint32_t i = 0u; i < CRTP::Dimension; ++i)\
output.setComponent(i, CRTP::getComponent(i) + component_t::create(val));\
\
return output;\
}\
\
NBL_CONSTEXPR_FUNC this_t operator-(__VA_ARGS__ val) NBL_CONST_MEMBER_FUNC \
{\
this_t output;\
for (uint32_t i = 0u; i < CRTP::Dimension; ++i)\
output.setComponent(i, CRTP::getComponent(i) - component_t::create(val));\
\
return output;\
}\
\
NBL_CONSTEXPR_FUNC this_t operator*(__VA_ARGS__ val) NBL_CONST_MEMBER_FUNC \
{\
this_t output;\
for (uint32_t i = 0u; i < CRTP::Dimension; ++i)\
output.setComponent(i, CRTP::getComponent(i) * component_t::create(val));\
\
return output;\
}\
\


template <bool FastMath, bool FlushDenormToZero, typename CRTP>
struct emulated_vector<emulated_float64_t<FastMath, FlushDenormToZero>, CRTP> : CRTP
{
using component_t = emulated_float64_t<FastMath, FlushDenormToZero>;
using this_t = emulated_vector<component_t, CRTP>;

NBL_CONSTEXPR_STATIC this_t create(this_t other)
{
this_t output;

for (uint32_t i = 0u; i < CRTP::Dimension; ++i)
output.setComponent(i, other.getComponent(i));

return output;
}

template<typename T>
NBL_CONSTEXPR_STATIC this_t create(vector<T, CRTP::Dimension> other)
{
this_t output;

for (uint32_t i = 0u; i < CRTP::Dimension; ++i)
output.setComponent(i, component_t::create(other[i]));

return output;
}

NBL_CONSTEXPR_FUNC this_t operator+(this_t other) NBL_CONST_MEMBER_FUNC
{
this_t output;

for (uint32_t i = 0u; i < CRTP::Dimension; ++i)
output.setComponent(i, CRTP::getComponent(i) + other.getComponent(i));

return output;
}
NBL_CONSTEXPR_FUNC this_t operator-(this_t other) NBL_CONST_MEMBER_FUNC
{
this_t output;

for (uint32_t i = 0u; i < CRTP::Dimension; ++i)
output.setComponent(i, CRTP::getComponent(i) - other.getComponent(i));

return output;
}
NBL_CONSTEXPR_FUNC this_t operator*(this_t other) NBL_CONST_MEMBER_FUNC
{
this_t output;

for (uint32_t i = 0u; i < CRTP::Dimension; ++i)
output.setComponent(i, CRTP::getComponent(i) * other.getComponent(i));

return output;
}

DEFINE_OPERATORS_FOR_TYPE(emulated_float64_t<true, true>)
DEFINE_OPERATORS_FOR_TYPE(emulated_float64_t<true, false>)
DEFINE_OPERATORS_FOR_TYPE(emulated_float64_t<false, true>)
DEFINE_OPERATORS_FOR_TYPE(emulated_float64_t<false, false>)
DEFINE_OPERATORS_FOR_TYPE(float16_t)
DEFINE_OPERATORS_FOR_TYPE(float32_t)
DEFINE_OPERATORS_FOR_TYPE(float64_t)
DEFINE_OPERATORS_FOR_TYPE(uint16_t)
Expand All @@ -408,17 +382,15 @@ struct emulated_vector<emulated_float64_t<FastMath, FlushDenormToZero>, CRTP> :
DEFINE_OPERATORS_FOR_TYPE(int16_t)
DEFINE_OPERATORS_FOR_TYPE(int32_t)
DEFINE_OPERATORS_FOR_TYPE(int64_t)

NBL_CONSTEXPR_FUNC component_t calcComponentSum() NBL_CONST_MEMBER_FUNC
{
component_t sum = component_t::create(0);
for (uint32_t i = 0u; i < CRTP::Dimension; ++i)
sum = sum + CRTP::getComponent(i);

return sum;
}
};

#undef NBL_EMULATED_FUNDAMENTAL_TYPE_VECTOR_CREATION_AND_COMPONENT_SUM
#undef NBL_EMULATED_VECTOR_CREATION_AND_COMPONENT_SUM
#undef NBL_EMULATED_FUNDAMENTAL_TYPE_VECTOR_COMPARISON_OPERATOR
#undef NBL_EMULATED_VECTOR_COMPARISON_OPERATOR
#undef NBL_EMULATED_FUNDAMENTAL_TYPE_VECTOR_ARITHMETIC_OPERATOR
#undef NBL_EMULATED_VECTOR_ARITHMETIC_OPERATOR
#undef NBL_EMULATED_VECTOR_UNARY_OPERATOR
#undef DEFINE_OPERATORS_FOR_TYPE

template<typename T, uint32_t N>
Expand Down
Loading