Skip to content

Commit 6fd2e3d

Browse files
Merge pull request #561 from Devsh-Graphics-Programming/new_hlsl_xoroshiro
New hlsl xoroshiro
2 parents e4ab38c + 103835f commit 6fd2e3d

File tree

4 files changed

+142
-1
lines changed

4 files changed

+142
-1
lines changed

include/nbl/builtin/hlsl/bit.hlsl

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#ifndef _NBL_BUILTIN_HLSL_BIT_INCLUDED_
2+
#define _NBL_BUILTIN_HLSL_BIT_INCLUDED_
3+
4+
#include <nbl/builtin/hlsl/cpp_compat.hlsl>
5+
6+
#ifndef __HLSL_VERSION
7+
#include <bit>
8+
9+
namespace nbl::hlsl
10+
{
11+
12+
NBL_ALIAS_TEMPLATE_FUNCTION(std::rotl, rotl);
13+
NBL_ALIAS_TEMPLATE_FUNCTION(std::rotr, rotr);
14+
15+
}
16+
#else
17+
namespace nbl
18+
{
19+
namespace hlsl
20+
{
21+
22+
template<typename T, typename S>
23+
T rotl(T x, S s);
24+
template<typename T, typename S>
25+
T rotr(T x, S s);
26+
27+
template<typename T, typename S>
28+
T rotl(T x, S s)
29+
{
30+
const T N = 32u;
31+
const S r = s % N;
32+
33+
if(r >= 0)
34+
{
35+
return (x << r) | (x >> (N - r));
36+
}
37+
else
38+
{
39+
return (x >> (-r)) | (x << (N - (-r)));
40+
}
41+
}
42+
43+
template<typename T, typename S>
44+
T rotr(T x, S s)
45+
{
46+
const T N = 32u;
47+
const S r = s % N;
48+
49+
if(r >= 0)
50+
{
51+
return (x >> r) | (x << (N - r));
52+
}
53+
else
54+
{
55+
return (x << (-r)) | (x >> (N - (-r)));
56+
}
57+
}
58+
59+
}
60+
}
61+
#endif
62+
63+
#endif

include/nbl/builtin/hlsl/cpp_compat.hlsl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
#define NBL_CONSTEXPR constexpr
1010
#define NBL_CONSTEXPR_STATIC_INLINE constexpr static inline
1111

12+
#define NBL_ALIAS_TEMPLATE_FUNCTION(origFunctionName, functionAlias) \
13+
template<typename... Args> \
14+
inline auto functionAlias(Args&&... args) -> decltype(origFunctionName(std::forward<Args>(args)...)) \
15+
{ \
16+
return origFunctionName(std::forward<Args>(args)...); \
17+
}
18+
1219
namespace nbl::hlsl
1320
{
1421

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
// Copyright (C) 2018-2020 - DevSH Graphics Programming Sp. z O.O.
3+
// This file is part of the "Nabla Engine".
4+
// For conditions of distribution and use, see copyright notice in nabla.h
5+
6+
#ifndef _NBL_BUILTIN_GLSL_RANDOM_XOROSHIRO_HLSL_INCLUDED_
7+
#define _NBL_BUILTIN_GLSL_RANDOM_XOROSHIRO_HLSL_INCLUDED_
8+
9+
#include <nbl/builtin/hlsl/cpp_compat/vector.hlsl>
10+
#include <nbl/builtin/hlsl/bit.hlsl>
11+
12+
namespace nbl
13+
{
14+
namespace hlsl
15+
{
16+
17+
struct Xoroshiro64StateHolder
18+
{
19+
void xoroshiro64_state_advance()
20+
{
21+
state[1] ^= state[0];
22+
state[0] = rotl(state[0], 26u) ^ state[1] ^ (state[1]<<9u); // a, b
23+
state[1] = rotl(state[1], 13u); // c
24+
}
25+
26+
uint32_t2 state;
27+
};
28+
29+
struct Xoroshiro64Star
30+
{
31+
static Xoroshiro64Star construct(NBL_CONST_REF_ARG(uint32_t2) initialState)
32+
{
33+
Xoroshiro64StateHolder stateHolder = {initialState};
34+
return Xoroshiro64Star(stateHolder);
35+
}
36+
37+
uint32_t operator()()
38+
{
39+
const uint32_t result = stateHolder.state[0]*0x9E3779BBu;
40+
stateHolder.xoroshiro64_state_advance();
41+
42+
return result;
43+
}
44+
45+
Xoroshiro64StateHolder stateHolder;
46+
};
47+
48+
struct Xoroshiro64StarStar
49+
{
50+
static Xoroshiro64StarStar construct(NBL_CONST_REF_ARG(uint32_t2) initialState)
51+
{
52+
Xoroshiro64StateHolder stateHolder = {initialState};
53+
return Xoroshiro64StarStar(stateHolder);
54+
}
55+
56+
uint32_t operator()()
57+
{
58+
const uint32_t result = rotl(stateHolder.state[0]*0x9E3779BBu,5u)*5u;
59+
stateHolder.xoroshiro64_state_advance();
60+
61+
return result;
62+
}
63+
64+
Xoroshiro64StateHolder stateHolder;
65+
};
66+
67+
}
68+
}
69+
70+
#endif

src/nbl/builtin/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "glsl/property_pool/transfer.glsl")
105105
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "glsl/property_pool/copy.comp")
106106
# random numbers
107107
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "glsl/random/xoroshiro.glsl")
108+
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/random/xoroshiro.hlsl")
108109
# sampling
109110
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "glsl/sampling/bilinear.glsl")
110111
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "glsl/sampling/box_muller_transform.glsl")
@@ -258,11 +259,11 @@ LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/cpp_compat/vector.hlsl")
258259
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/cpp_compat/promote.hlsl")
259260
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/cpp_compat/intrinsics.h")
260261

262+
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/bit.hlsl")
261263

262264
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/math/quadrature/gauss_legendre/gauss_legendre.hlsl")
263265
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/math/quadrature/gauss_legendre/impl.hlsl")
264266

265-
266267
macro(NBL_ADD_BUILTIN_RESOURCES _TARGET_) # internal & Nabla only, must be added with the macro to properly propagate scope
267268
ADD_CUSTOM_BUILTIN_RESOURCES("${_TARGET_}" NBL_RESOURCES_TO_EMBED "${NBL_ROOT_PATH}/include" "nbl/builtin" "nbl::builtin" "${NBL_ROOT_PATH_BINARY}/include" "${NBL_ROOT_PATH_BINARY}/src" "STATIC" "INTERNAL")
268269
endmacro()

0 commit comments

Comments
 (0)