Skip to content

Commit 81e7214

Browse files
committed
Implemented modfStruct and frexpStruct
1 parent 2b1701a commit 81e7214

File tree

5 files changed

+163
-3
lines changed

5 files changed

+163
-3
lines changed

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

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <boost/preprocessor/comparison/not_equal.hpp>
1616
#include <boost/preprocessor/punctuation/comma_if.hpp>
1717
#include <boost/preprocessor/seq/for_each_i.hpp>
18+
#include <nbl/builtin/hlsl/spirv_intrinsics/output_structs.hlsl>
1819

1920
namespace nbl
2021
{
@@ -81,6 +82,10 @@ template<typename T NBL_STRUCT_CONSTRAINABLE>
8182
struct reflect_helper;
8283
template<typename T, typename U NBL_STRUCT_CONSTRAINABLE>
8384
struct refract_helper;
85+
template<typename T NBL_STRUCT_CONSTRAINABLE>
86+
struct modfStruct_helper;
87+
template<typename T NBL_STRUCT_CONSTRAINABLE>
88+
struct frexpStruct_helper;
8489

8590
#ifdef __HLSL_VERSION // HLSL only specializations
8691

@@ -136,6 +141,8 @@ template<typename T> AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(clamp_helper, sClamp, (
136141
template<typename T> AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(smoothStep_helper, smoothStep, (T), (T)(T)(T), T)
137142
template<typename T> AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(faceForward_helper, faceForward, (T), (T)(T)(T), T)
138143
template<typename T, typename U> AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(refract_helper, refract, (T)(U), (T)(T)(U), T)
144+
template<typename T> AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(modfStruct_helper, modfStruct, (T), (T), ModfOutput<T>)
145+
template<typename T> AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(frexpStruct_helper, frexpStruct, (T), (T), FrexpOutput<T>)
139146

140147
#define BITCOUNT_HELPER_RETRUN_TYPE conditional_t<is_vector_v<T>, vector<int32_t, vector_traits<T>::Dimension>, int32_t>
141148
template<typename T> AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(bitCount_helper, bitCount, (T), (T), BITCOUNT_HELPER_RETRUN_TYPE)
@@ -510,6 +517,34 @@ struct refract_helper<T, U>
510517
}
511518
};
512519

520+
template<typename T>
521+
requires concepts::FloatingPointScalar<T>
522+
struct modfStruct_helper<T>
523+
{
524+
using return_t = ModfOutput<T>;
525+
static inline return_t __call(const T val)
526+
{
527+
return_t output;
528+
output.fractionalPart = std::modf(val, &output.wholeNumberPart);
529+
530+
return output;
531+
}
532+
};
533+
534+
template<typename T>
535+
requires concepts::FloatingPointScalar<T>
536+
struct frexpStruct_helper<T>
537+
{
538+
using return_t = FrexpOutput<T>;
539+
static inline return_t __call(const T val)
540+
{
541+
return_t output;
542+
output.significand = std::frexp(val, &output.exponent);
543+
544+
return output;
545+
}
546+
};
547+
513548
#endif // C++ only specializations
514549

515550
// C++ and HLSL specializations
@@ -765,6 +800,67 @@ struct smoothStep_helper<T NBL_PARTIAL_REQ_BOT(VECTOR_SPECIALIZATION_CONCEPT) >
765800
}
766801
};
767802

803+
template<typename T>
804+
NBL_PARTIAL_REQ_TOP(VECTOR_SPECIALIZATION_CONCEPT)
805+
struct modfStruct_helper<T NBL_PARTIAL_REQ_BOT(VECTOR_SPECIALIZATION_CONCEPT) >
806+
{
807+
using return_t = ModfOutput<T>;
808+
static return_t __call(NBL_CONST_REF_ARG(T) x)
809+
{
810+
using traits = hlsl::vector_traits<T>;
811+
array_get<T, typename traits::scalar_type> getter;
812+
array_set<T, typename traits::scalar_type> setter;
813+
814+
T fracPartOut;
815+
T intPartOut;
816+
for (uint32_t i = 0; i < traits::Dimension; ++i)
817+
{
818+
using component_return_t = ModfOutput<typename vector_traits<T>::scalar_type>;
819+
component_return_t result = modfStruct_helper<typename traits::scalar_type>::__call(getter(x, i));
820+
821+
setter(fracPartOut, i, result.fractionalPart);
822+
setter(intPartOut, i, result.wholeNumberPart);
823+
}
824+
825+
return_t output;
826+
output.fractionalPart = fracPartOut;
827+
output.wholeNumberPart = intPartOut;
828+
829+
return output;
830+
}
831+
};
832+
833+
template<typename T>
834+
NBL_PARTIAL_REQ_TOP(VECTOR_SPECIALIZATION_CONCEPT)
835+
struct frexpStruct_helper<T NBL_PARTIAL_REQ_BOT(VECTOR_SPECIALIZATION_CONCEPT) >
836+
{
837+
using return_t = FrexpOutput<T>;
838+
static return_t __call(NBL_CONST_REF_ARG(T) x)
839+
{
840+
using traits = hlsl::vector_traits<T>;
841+
array_get<T, typename traits::scalar_type> getter;
842+
array_set<T, typename traits::scalar_type> significandSetter;
843+
array_set<T, typename traits::scalar_type> exponentSetter;
844+
845+
T significandOut;
846+
T exponentOut;
847+
for (uint32_t i = 0; i < traits::Dimension; ++i)
848+
{
849+
using component_return_t = FrexpOutput<typename vector_traits<T>::scalar_type>;
850+
component_return_t result = frexpStruct_helper<typename traits::scalar_type>::__call(getter(x, i));
851+
852+
significandSetter(significandOut, i, result.significand);
853+
exponentSetter(exponentOut, i, result.exponent);
854+
}
855+
856+
return_t output;
857+
output.significand = significandOut;
858+
output.exponent = exponentOut;
859+
860+
return output;
861+
}
862+
};
863+
768864
}
769865
}
770866
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,17 @@ inline T refract(NBL_CONST_REF_ARG(T) I, NBL_CONST_REF_ARG(T) N, NBL_CONST_REF_A
231231
return cpp_compat_intrinsics_impl::refract_helper<T, U>::__call(I, N, eta);
232232
}
233233

234-
// TODO: implement
235-
//pack and unpack intrinsics
234+
template<typename T>
235+
inline ModfOutput<T> modfStruct(NBL_CONST_REF_ARG(T) val)
236+
{
237+
return cpp_compat_intrinsics_impl::modfStruct_helper<T>::__call(val);
238+
}
239+
240+
template<typename T>
241+
inline FrexpOutput<T> frexpStruct(NBL_CONST_REF_ARG(T) val)
242+
{
243+
return cpp_compat_intrinsics_impl::frexpStruct_helper<T>::__call(val);
244+
}
236245

237246
}
238247
}

include/nbl/builtin/hlsl/spirv_intrinsics/glsl.std.450.hlsl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <nbl/builtin/hlsl/matrix_utils/matrix_traits.hlsl>
77
#include <nbl/builtin/hlsl/cpp_compat/basic.h>
88
#include <nbl/builtin/hlsl/concepts.hlsl>
9+
#include <nbl/builtin/hlsl/spirv_intrinsics/output_structs.hlsl>
910
#include "spirv/unified1/GLSL.std.450.h"
1011

1112
namespace nbl
@@ -234,6 +235,14 @@ template<typename T, typename U NBL_FUNC_REQUIRES(concepts::FloatingPointVectorO
234235
[[vk::ext_instruction(GLSLstd450Refract, "GLSL.std.450")]]
235236
T refract(T I, T N, U Nref);
236237

238+
template<typename T NBL_FUNC_REQUIRES(concepts::FloatingPointVectorOrScalar<T>)
239+
[[vk::ext_instruction(GLSLstd450ModfStruct, "GLSL.std.450")]]
240+
ModfOutput<T> modfStruct(T val);
241+
242+
template<typename T NBL_FUNC_REQUIRES(concepts::FloatingPointVectorOrScalar<T>)
243+
[[vk::ext_instruction(GLSLstd450FrexpStruct, "GLSL.std.450")]]
244+
FrexpOutput<T> frexpStruct(T val);
245+
237246
}
238247
}
239248
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (C) 2023 - DevSH Graphics Programming Sp. z O.O.
2+
// This file is part of the "Nabla Engine".
3+
// For conditions of distribution and use, see copyright notice in nabla.h
4+
#ifndef _NBL_BUILTIN_HLSL_SPIRV_INTRINSICS_OUTPUT_STRUCTS_INCLUDED_
5+
#define _NBL_BUILTIN_HLSL_SPIRV_INTRINSICS_OUTPUT_STRUCTS_INCLUDED_
6+
7+
#include <nbl/builtin/hlsl/concepts/core.hlsl>
8+
#include <nbl/builtin/hlsl/concepts/vector.hlsl>
9+
#include <nbl/builtin/hlsl/vector_utils/vector_traits.hlsl>
10+
11+
namespace nbl
12+
{
13+
namespace hlsl
14+
{
15+
16+
template<typename T NBL_STRUCT_CONSTRAINABLE>
17+
struct ModfOutput;
18+
19+
template<typename T> NBL_PARTIAL_REQ_TOP(concepts::FloatingPoint<T>)
20+
struct ModfOutput<T NBL_PARTIAL_REQ_BOT(concepts::FloatingPoint<T>) >
21+
{
22+
T fractionalPart;
23+
T wholeNumberPart;
24+
};
25+
26+
template<typename T NBL_STRUCT_CONSTRAINABLE>
27+
struct FrexpOutput;
28+
29+
template<typename T> NBL_PARTIAL_REQ_TOP(concepts::FloatingPointScalar<T>)
30+
struct FrexpOutput<T NBL_PARTIAL_REQ_BOT(concepts::FloatingPointScalar<T>) >
31+
{
32+
T significand;
33+
int exponent;
34+
};
35+
36+
template<typename T> NBL_PARTIAL_REQ_TOP(concepts::FloatingPointVector<T>)
37+
struct FrexpOutput<T NBL_PARTIAL_REQ_BOT(concepts::FloatingPointVector<T>) >
38+
{
39+
T significand;
40+
vector<int, vector_traits<T>::Dimension> exponent;
41+
};
42+
43+
}
44+
}
45+
46+
#endif

0 commit comments

Comments
 (0)