Skip to content

Commit 644c1db

Browse files
committed
Merge branch 'cpp_compat_intrinsic_functions' into new_tgmath
2 parents 821a5ed + 0c91969 commit 644c1db

File tree

6 files changed

+284
-5
lines changed

6 files changed

+284
-5
lines changed

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

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,85 @@ 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+
#ifdef __HLSL_VERSION
235+
#define NAMESPACE spirv
236+
#else
237+
#define NAMESPACE glm
238+
#endif
239+
240+
template<typename T NBL_FUNC_REQUIRES(is_same_v<T, float32_t4>)
241+
inline int32_t packSnorm4x8(T vec)
242+
{
243+
return NAMESPACE::packSnorm4x8(vec);
244+
}
245+
246+
template<typename T NBL_FUNC_REQUIRES(is_same_v<T, float32_t4>)
247+
inline int32_t packUnorm4x8(T vec)
248+
{
249+
return NAMESPACE::packUnorm4x8(vec);
250+
}
251+
252+
template<typename T NBL_FUNC_REQUIRES(is_same_v<T, float32_t2>)
253+
inline int32_t packSnorm2x16(T vec)
254+
{
255+
return NAMESPACE::packSnorm2x16(vec);
256+
}
257+
258+
template<typename T NBL_FUNC_REQUIRES(is_same_v<T, float32_t2>)
259+
inline int32_t packUnorm2x16(T vec)
260+
{
261+
return NAMESPACE::packUnorm2x16(vec);
262+
}
263+
264+
template<typename T NBL_FUNC_REQUIRES(is_same_v<T, float32_t2>)
265+
inline int32_t packHalf2x16(T vec)
266+
{
267+
return NAMESPACE::packHalf2x16(vec);
268+
}
269+
270+
template<typename T NBL_FUNC_REQUIRES(is_same_v<T, int32_t2>)
271+
inline float64_t packDouble2x32(T vec)
272+
{
273+
return NAMESPACE::packDouble2x32(vec);
274+
}
275+
276+
template<typename T NBL_FUNC_REQUIRES(is_same_v<T, int32_t>)
277+
inline float32_t2 unpackSnorm2x16(T val)
278+
{
279+
return NAMESPACE::unpackSnorm2x16(val);
280+
}
281+
282+
template<typename T NBL_FUNC_REQUIRES(is_same_v<T, int32_t>)
283+
inline float32_t2 unpackUnorm2x16(T val)
284+
{
285+
return NAMESPACE::unpackUnorm2x16(val);
286+
}
287+
288+
template<typename T NBL_FUNC_REQUIRES(is_same_v<T, int32_t>)
289+
inline float32_t2 unpackHalf2x16(T val)
290+
{
291+
return NAMESPACE::unpackHalf2x16(val);
292+
}
293+
294+
template<typename T NBL_FUNC_REQUIRES(is_same_v<T, int32_t>)
295+
inline float32_t4 unpackSnorm4x8(T val)
296+
{
297+
return NAMESPACE::unpackSnorm4x8(val);
298+
}
299+
300+
template<typename T NBL_FUNC_REQUIRES(is_same_v<T, int32_t>)
301+
inline float32_t4 unpackUnorm4x8(T val)
302+
{
303+
return NAMESPACE::unpackUnorm4x8(val);
304+
}
305+
306+
template<typename T NBL_FUNC_REQUIRES(is_same_v<T, float64_t>)
307+
inline int32_t2 unpackDouble2x32(T val)
308+
{
309+
return NAMESPACE::unpackDouble2x32(val);
310+
}
311+
312+
#undef NAMESPACE
236313

237314
}
238315
}

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

Lines changed: 46 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/tgmath/output_structs.hlsl>
910
#include "spirv/unified1/GLSL.std.450.h"
1011

1112
namespace nbl
@@ -234,6 +235,51 @@ 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+
246+
[[vk::ext_instruction(GLSLstd450PackSnorm4x8, "GLSL.std.450")]]
247+
int32_t packSnorm4x8(float32_t4 vec);
248+
249+
[[vk::ext_instruction(GLSLstd450PackUnorm4x8, "GLSL.std.450")]]
250+
int32_t packUnorm4x8(float32_t4 vec);
251+
252+
[[vk::ext_instruction(GLSLstd450PackSnorm2x16, "GLSL.std.450")]]
253+
int32_t packSnorm2x16(float32_t2 vec);
254+
255+
[[vk::ext_instruction(GLSLstd450PackUnorm2x16, "GLSL.std.450")]]
256+
int32_t packUnorm2x16(float32_t2 vec);
257+
258+
[[vk::ext_instruction(GLSLstd450PackHalf2x16, "GLSL.std.450")]]
259+
int32_t packHalf2x16(float32_t2 vec);
260+
261+
[[vk::ext_instruction(GLSLstd450PackDouble2x32, "GLSL.std.450")]]
262+
float64_t packDouble2x32(int32_t2 vec);
263+
264+
[[vk::ext_instruction(GLSLstd450UnpackSnorm2x16, "GLSL.std.450")]]
265+
float32_t2 unpackSnorm2x16(int32_t vec);
266+
267+
[[vk::ext_instruction(GLSLstd450UnpackUnorm2x16, "GLSL.std.450")]]
268+
float32_t2 unpackUnorm2x16(int32_t vec);
269+
270+
[[vk::ext_instruction(GLSLstd450UnpackHalf2x16, "GLSL.std.450")]]
271+
float32_t2 unpackHalf2x16(int32_t vec);
272+
273+
[[vk::ext_instruction(GLSLstd450UnpackSnorm4x8, "GLSL.std.450")]]
274+
float32_t4 unpackSnorm4x8(int32_t vec);
275+
276+
[[vk::ext_instruction(GLSLstd450UnpackUnorm4x8, "GLSL.std.450")]]
277+
float32_t4 unpackUnorm4x8(int32_t vec);
278+
279+
[[vk::ext_instruction(GLSLstd450UnpackDouble2x32, "GLSL.std.450")]]
280+
int32_t2 unpackDouble2x32(float64_t vec);
281+
282+
237283
}
238284
}
239285
}

include/nbl/builtin/hlsl/tgmath.hlsl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#define _NBL_BUILTIN_HLSL_TGMATH_INCLUDED_
66

77
#include <nbl/builtin/hlsl/spirv_intrinsics/glsl.std.450.hlsl>
8-
#include <nbl/builtin/hlsl/impl/tgmath_impl.hlsl>
8+
#include <nbl/builtin/hlsl/tgmath/impl.hlsl>
99
#include <nbl/builtin/hlsl/type_traits.hlsl>
1010
#include <nbl/builtin/hlsl/vector_utils/vector_traits.hlsl>
1111
#include <nbl/builtin/hlsl/cpp_compat.hlsl>
@@ -162,6 +162,18 @@ inline T ldexp(NBL_CONST_REF_ARG(T) arg, NBL_CONST_REF_ARG(U) exp)
162162
return tgmath_impl::ldexp_helper<T, U>::__call(arg, exp);
163163
}
164164

165+
template<typename T>
166+
inline ModfOutput<T> modfStruct(NBL_CONST_REF_ARG(T) val)
167+
{
168+
return tgmath_impl::modfStruct_helper<T>::__call(val);
169+
}
170+
171+
template<typename T>
172+
inline FrexpOutput<T> frexpStruct(NBL_CONST_REF_ARG(T) val)
173+
{
174+
return tgmath_impl::frexpStruct_helper<T>::__call(val);
175+
}
176+
165177
}
166178
}
167179

include/nbl/builtin/hlsl/impl/tgmath_impl.hlsl renamed to include/nbl/builtin/hlsl/tgmath/impl.hlsl

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <nbl/builtin/hlsl/spirv_intrinsics/core.hlsl>
88
#include <nbl/builtin/hlsl/spirv_intrinsics/glsl.std.450.hlsl>
99
#include <nbl/builtin/hlsl/ieee754.hlsl>
10+
#include <nbl/builtin/hlsl/tgmath/output_structs.hlsl>
1011

1112
// C++ includes
1213
#ifndef __HLSL_VERSION
@@ -79,6 +80,10 @@ template<typename T NBL_STRUCT_CONSTRAINABLE>
7980
struct fma_helper;
8081
template<typename T, typename U NBL_STRUCT_CONSTRAINABLE>
8182
struct ldexp_helper;
83+
template<typename T NBL_STRUCT_CONSTRAINABLE>
84+
struct modfStruct_helper;
85+
template<typename T NBL_STRUCT_CONSTRAINABLE>
86+
struct frexpStruct_helper;
8287

8388
#ifdef __HLSL_VERSION
8489

@@ -118,6 +123,8 @@ template<typename T> AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(ceil_helper, ceil, (T),
118123
template<typename T> AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(pow_helper, pow, (T), (T)(T), T)
119124
template<typename T> AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(fma_helper, fma, (T), (T)(T)(T), T)
120125
template<typename T, typename U> AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(ldexp_helper, ldexp, (T)(U), (T)(U), T)
126+
template<typename T> AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(modfStruct_helper, modfStruct, (T), (T), ModfOutput<T>)
127+
template<typename T> AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(frexpStruct_helper, frexpStruct, (T), (T), FrexpOutput<T>)
121128

122129
#define ISINF_AND_ISNAN_RETURN_TYPE conditional_t<is_vector_v<T>, vector<bool, vector_traits<T>::Dimension>, bool>
123130
template<typename T> AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(isinf_helper, isInf, (T), (T), ISINF_AND_ISNAN_RETURN_TYPE)
@@ -327,6 +334,34 @@ struct ldexp_helper<T, U NBL_PARTIAL_REQ_BOT(concepts::FloatingPointScalar<T> &&
327334
}
328335
};
329336

337+
template<typename T>
338+
requires concepts::FloatingPointScalar<T>
339+
struct modfStruct_helper<T>
340+
{
341+
using return_t = ModfOutput<T>;
342+
static inline return_t __call(const T val)
343+
{
344+
return_t output;
345+
output.fractionalPart = std::modf(val, &output.wholeNumberPart);
346+
347+
return output;
348+
}
349+
};
350+
351+
template<typename T>
352+
requires concepts::FloatingPointScalar<T>
353+
struct frexpStruct_helper<T>
354+
{
355+
using return_t = FrexpOutput<T>;
356+
static inline return_t __call(const T val)
357+
{
358+
return_t output;
359+
output.significand = std::frexp(val, &output.exponent);
360+
361+
return output;
362+
}
363+
};
364+
330365
#endif // C++ only specializations
331366

332367
// C++ and HLSL specializations
@@ -480,6 +515,67 @@ struct ldexp_helper<T, U NBL_PARTIAL_REQ_BOT(VECTOR_SPECIALIZATION_CONCEPT && (v
480515
}
481516
};
482517

518+
template<typename T>
519+
NBL_PARTIAL_REQ_TOP(VECTOR_SPECIALIZATION_CONCEPT)
520+
struct modfStruct_helper<T NBL_PARTIAL_REQ_BOT(VECTOR_SPECIALIZATION_CONCEPT) >
521+
{
522+
using return_t = ModfOutput<T>;
523+
static return_t __call(NBL_CONST_REF_ARG(T) x)
524+
{
525+
using traits = hlsl::vector_traits<T>;
526+
array_get<T, typename traits::scalar_type> getter;
527+
array_set<T, typename traits::scalar_type> setter;
528+
529+
T fracPartOut;
530+
T intPartOut;
531+
for (uint32_t i = 0; i < traits::Dimension; ++i)
532+
{
533+
using component_return_t = ModfOutput<typename vector_traits<T>::scalar_type>;
534+
component_return_t result = modfStruct_helper<typename traits::scalar_type>::__call(getter(x, i));
535+
536+
setter(fracPartOut, i, result.fractionalPart);
537+
setter(intPartOut, i, result.wholeNumberPart);
538+
}
539+
540+
return_t output;
541+
output.fractionalPart = fracPartOut;
542+
output.wholeNumberPart = intPartOut;
543+
544+
return output;
545+
}
546+
};
547+
548+
template<typename T>
549+
NBL_PARTIAL_REQ_TOP(VECTOR_SPECIALIZATION_CONCEPT)
550+
struct frexpStruct_helper<T NBL_PARTIAL_REQ_BOT(VECTOR_SPECIALIZATION_CONCEPT) >
551+
{
552+
using return_t = FrexpOutput<T>;
553+
static return_t __call(NBL_CONST_REF_ARG(T) x)
554+
{
555+
using traits = hlsl::vector_traits<T>;
556+
array_get<T, typename traits::scalar_type> getter;
557+
array_set<T, typename traits::scalar_type> significandSetter;
558+
array_set<T, typename traits::scalar_type> exponentSetter;
559+
560+
T significandOut;
561+
T exponentOut;
562+
for (uint32_t i = 0; i < traits::Dimension; ++i)
563+
{
564+
using component_return_t = FrexpOutput<typename vector_traits<T>::scalar_type>;
565+
component_return_t result = frexpStruct_helper<typename traits::scalar_type>::__call(getter(x, i));
566+
567+
significandSetter(significandOut, i, result.significand);
568+
exponentSetter(exponentOut, i, result.exponent);
569+
}
570+
571+
return_t output;
572+
output.significand = significandOut;
573+
output.exponent = exponentOut;
574+
575+
return output;
576+
}
577+
};
578+
483579
#undef VECTOR_SPECIALIZATION_CONCEPT
484580

485581
}
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

src/nbl/builtin/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,6 @@ LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/ieee754/impl.hlsl")
226226
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/array_accessors.hlsl")
227227
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/vector_utils/vector_traits.hlsl")
228228
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/matrix_utils/matrix_traits.hlsl")
229-
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/tgmath.hlsl")
230-
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/impl/tgmath_impl.hlsl")
231229

232230
#spirv intrinsics
233231
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/spirv_intrinsics/core.hlsl")
@@ -357,5 +355,9 @@ LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/concepts/accessors/loadable_i
357355
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/concepts/accessors/mip_mapped.hlsl")
358356
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/concepts/accessors/storable_image.hlsl")
359357
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/concepts/accessors/fft.hlsl")
358+
#tgmath
359+
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/tgmath.hlsl")
360+
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/tgmath/impl.hlsl")
361+
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/tgmath/output_structs.hlsl")
360362

361363
ADD_CUSTOM_BUILTIN_RESOURCES(nblBuiltinResourceData NBL_RESOURCES_TO_EMBED "${NBL_ROOT_PATH}/include" "nbl/builtin" "nbl::builtin" "${NBL_ROOT_PATH_BINARY}/include" "${NBL_ROOT_PATH_BINARY}/src" "STATIC" "INTERNAL")

0 commit comments

Comments
 (0)