Skip to content

Commit 8c94b0c

Browse files
committed
Added some spirv intrinsics
1 parent b3fba14 commit 8c94b0c

File tree

3 files changed

+48
-20
lines changed

3 files changed

+48
-20
lines changed

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <nbl/builtin/hlsl/type_traits.hlsl>
66
#include <nbl/builtin/hlsl/vector_utils/vector_traits.hlsl>
77
#include <nbl/builtin/hlsl/array_accessors.hlsl>
8+
#include <nbl/builtin/hlsl/spirv_intrinsics/GLSL.std.450.hlsl>
89

910
#ifndef __HLSL_VERSION
1011
#include <algorithm>
@@ -18,7 +19,7 @@ namespace hlsl
1819
{
1920

2021
template<typename Integer>
21-
int bitCount(const Integer& val)
22+
int bitCount(NBL_CONST_REF_ARG(Integer) val)
2223
{
2324
#ifdef __HLSL_VERSION
2425

@@ -117,7 +118,7 @@ template<typename Integer>
117118
int findLSB(NBL_CONST_REF_ARG(Integer) val)
118119
{
119120
#ifdef __HLSL_VERSION
120-
121+
return spirv::findILsb(val);
121122
#else
122123
if (is_signed_v<Integer>)
123124
{
@@ -140,7 +141,14 @@ template<typename Integer>
140141
int findMSB(NBL_CONST_REF_ARG(Integer) val)
141142
{
142143
#ifdef __HLSL_VERSION
143-
144+
if (is_signed_v<Integer>)
145+
{
146+
return spirv::findSMsb(val);
147+
}
148+
else
149+
{
150+
return spirv::findUMsb(val);
151+
}
144152
#else
145153
if (is_signed_v<Integer>)
146154
{
@@ -164,7 +172,7 @@ template<typename T> //requires ::nbl::hlsl::is_floating_point_v<T>
164172
inline T floor(NBL_CONST_REF_ARG(T) val)
165173
{
166174
#ifdef __HLSL_VERSION
167-
175+
return spirv::Floor(val);
168176
#else
169177
return glm::floor(val);
170178
#endif
@@ -221,7 +229,7 @@ template<typename T>
221229
inline T min(NBL_CONST_REF_ARG(T) a, NBL_CONST_REF_ARG(T) b)
222230
{
223231
#ifdef __HLSL_VERSION
224-
232+
min(a, b);
225233
#else
226234
return std::min(a, b);
227235
#endif
@@ -237,7 +245,7 @@ template<typename FloatingPoint>
237245
inline FloatingPoint isnan(NBL_CONST_REF_ARG(FloatingPoint) val)
238246
{
239247
#ifdef __HLSL_VERSION
240-
return spirv::isnan(val);
248+
return spirv::IsNan(val);
241249
#else
242250
return std::isnan(val);
243251
#endif
@@ -247,7 +255,7 @@ template<typename FloatingPoint>
247255
inline FloatingPoint isinf(NBL_CONST_REF_ARG(FloatingPoint) val)
248256
{
249257
#ifdef __HLSL_VERSION
250-
return spirv::isinf(val);
258+
return spirv::IsInf(val);
251259
#else
252260
return std::isinf(val);
253261
#endif
@@ -257,17 +265,17 @@ template<typename T>
257265
inline T exp2(NBL_CONST_REF_ARG(T) val)
258266
{
259267
#ifdef __HLSL_VERSION
260-
268+
return spirv::Exp2(val);
261269
#else
262270
return std::exp2(val);
263271
#endif
264272
}
265273

266-
template<typename T>
267-
inline T rsqrt(T x)
274+
template<typename FloatingPoint>
275+
inline FloatingPoint rsqrt(FloatingPoint x)
268276
{
269277
#ifdef __HLSL_VERSION
270-
278+
return spirv::InverseSqrt(x);
271279
#else
272280
return 1.0f / std::sqrt(x);
273281
#endif

include/nbl/builtin/hlsl/spirv_intrinsics/core.hlsl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,14 @@ template<typename Integral>
250250
[[vk::ext_instruction( spv::OpBitReverse )]]
251251
enable_if_t<is_integral_v<Integral>, Integral> bitFieldReverse( Integral base );
252252

253+
template<typename FloatingPoint>
254+
[[vk::ext_instruction( spv::OpIsNan )]]
255+
enable_if_t<is_floating_point_v<FloatingPoint>, bool> IsNan(FloatingPoint val);
256+
257+
template<typename FloatingPoint>
258+
[[vk::ext_instruction( spv::OpIsInf )]]
259+
enable_if_t<is_floating_point_v<FloatingPoint>, bool> IsInf(FloatingPoint val);
260+
253261
}
254262

255263
#endif

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,29 @@ namespace spirv
1414

1515
// Find MSB and LSB restricted to 32-bit width component types https://registry.khronos.org/SPIR-V/specs/unified1/GLSL.std.450.html
1616

17-
template<typename Integral NBL_FUNC_REQUIRES(is_integral_v<Integral> && (sizeof(scalar_type_t<Integral>) == 4))
18-
[[vk::ext_instruction(GLSLstd450::GLSLstd450FindILsb, "GLSL.std.450")]]
19-
Integral findILsb(Integral value);
17+
template<typename Integral>
18+
[[vk::ext_instruction(GLSLstd450::GLSLstd450FindILsb, "GLSL.std.450")]]
19+
enable_if_t<is_integral_v<Integral> && (sizeof(scalar_type_t<Integral>) == 4), Integral> findILsb(Integral value);
2020

21-
template<typename Integral NBL_FUNC_REQUIRES(is_integral_v<Integral> && (sizeof(scalar_type_t<Integral>) == 4))
22-
[[vk::ext_instruction(GLSLstd450::GLSLstd450FindSMsb, "GLSL.std.450")]]
23-
Integral findSMsb(Integral value);
21+
template<typename Integral>
22+
[[vk::ext_instruction(GLSLstd450::GLSLstd450FindSMsb, "GLSL.std.450")]]
23+
enable_if_t<is_integral_v<Integral> && (sizeof(scalar_type_t<Integral>) == 4), Integral> findSMsb(Integral value);
2424

25-
template<typename Integral NBL_FUNC_REQUIRES(is_integral_v<Integral> && (sizeof(scalar_type_t<Integral>) == 4))
26-
[[vk::ext_instruction(GLSLstd450::GLSLstd450FindUMsb, "GLSL.std.450")]]
27-
Integral findUMsb(Integral value);
25+
template<typename Integral>
26+
[[vk::ext_instruction(GLSLstd450::GLSLstd450FindUMsb, "GLSL.std.450")]]
27+
enable_if_t<is_integral_v<Integral> && (sizeof(scalar_type_t<Integral>) == 4), Integral> findUMsb(Integral value);
28+
29+
template<typename T>
30+
[[vk::ext_instruction(GLSLstd450::GLSLstd450Exp2, "GLSL.std.450")]]
31+
enable_if_t<is_fundamental<T>::value, T> Exp2(T val);
32+
33+
template<typename FloatingPoint>
34+
[[vk::ext_instruction(GLSLstd450::GLSLstd450InverseSqrt, "GLSL.std.450")]]
35+
enable_if_t<is_floating_point_v<FloatingPoint>, FloatingPoint> InverseSqrt(FloatingPoint val);
36+
37+
template<typename FloatingPoint>
38+
[[vk::ext_instruction(GLSLstd450::GLSLstd450Floor, "GLSL.std.450")]]
39+
enable_if_t<is_floating_point_v<FloatingPoint>, FloatingPoint> Floor(FloatingPoint val);
2840

2941
}
3042
}

0 commit comments

Comments
 (0)