Skip to content
Merged
63 changes: 63 additions & 0 deletions include/nbl/builtin/hlsl/bit.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#ifndef _NBL_BUILTIN_HLSL_BIT_INCLUDED_
#define _NBL_BUILTIN_HLSL_BIT_INCLUDED_

#include <nbl/builtin/hlsl/cpp_compat.hlsl>

#ifndef __HLSL_VERSION
#include <bit>

namespace nbl::hlsl
{

NBL_ALIAS_TEMPLATE_FUNCTION(std::rotl, rotl);
NBL_ALIAS_TEMPLATE_FUNCTION(std::rotr, rotr);

}
#else
namespace nbl
{
namespace hlsl
{

template<typename T, typename S>
T rotl(T x, S s);
template<typename T, typename S>
T rotr(T x, S s);

template<typename T, typename S>
T rotl(T x, S s)
{
const T N = 32u;
const S r = s % N;

if(r >= 0)
{
return (x << r) | (x >> (N - r));
}
else
{
return (x >> (-r)) | (x << (N - (-r)));
}
}

template<typename T, typename S>
T rotr(T x, S s)
{
const T N = 32u;
const S r = s % N;

if(r >= 0)
{
return (x >> r) | (x << (N - r));
}
else
{
return (x << (-r)) | (x >> (N - (-r)));
}
}

}
}
#endif

#endif
7 changes: 7 additions & 0 deletions include/nbl/builtin/hlsl/cpp_compat.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
#define NBL_CONSTEXPR constexpr
#define NBL_CONSTEXPR_STATIC_INLINE constexpr static inline

#define NBL_ALIAS_TEMPLATE_FUNCTION(origFunctionName, functionAlias) \
template<typename... Args> \
inline auto functionAlias(Args&&... args) -> decltype(origFunctionName(std::forward<Args>(args)...)) \
{ \
return origFunctionName(std::forward<Args>(args)...); \
}

namespace nbl::hlsl
{

Expand Down
65 changes: 44 additions & 21 deletions include/nbl/builtin/hlsl/random/xoroshiro.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,63 @@
#ifndef _NBL_BUILTIN_GLSL_RANDOM_XOROSHIRO_HLSL_INCLUDED_
#define _NBL_BUILTIN_GLSL_RANDOM_XOROSHIRO_HLSL_INCLUDED_

#include <nbl/builtin/hlsl/math/functions.hlsl>

#include <nbl/builtin/hlsl/cpp_compat/vector.hlsl>
#include <nbl/builtin/hlsl/bit.hlsl>

namespace nbl
{
namespace hlsl
{

#define xoroshiro64star_state_t uint2
#define xoroshiro64starstar_state_t uint2

void xoroshiro64_state_advance(inout uint2 state)
struct Xoroshiro64StateHolder
{
state[1] ^= state[0];
state[0] = math::rotl(state[0], 26u) ^ state[1] ^ (state[1]<<9u); // a, b
state[1] = math::rotl(state[1], 13u); // c
}
void xoroshiro64_state_advance()
{
state[1] ^= state[0];
state[0] = rotl(state[0], 26u) ^ state[1] ^ (state[1]<<9u); // a, b
state[1] = rotl(state[1], 13u); // c
}

uint32_t2 state;
};

uint xoroshiro64star(inout xoroshiro64starstar_state_t state)
struct Xoroshiro64Star
{
const uint result = state[0]*0x9E3779BBu;
static Xoroshiro64Star construct(NBL_CONST_REF_ARG(uint32_t2) initialState)
{
Xoroshiro64StateHolder stateHolder = {initialState};
return Xoroshiro64Star(stateHolder);
}

uint32_t operator()()
{
const uint32_t result = stateHolder.state[0]*0x9E3779BBu;
stateHolder.xoroshiro64_state_advance();

xoroshiro64_state_advance(state);
return result;
}

Xoroshiro64StateHolder stateHolder;
};

return result;
}
uint xoroshiro64starstar(inout xoroshiro64starstar_state_t state)
struct Xoroshiro64StarStar
{
const uint result = math::rotl(state[0]*0x9E3779BBu,5u)*5u;

xoroshiro64_state_advance(state);
static Xoroshiro64StarStar construct(NBL_CONST_REF_ARG(uint32_t2) initialState)
{
Xoroshiro64StateHolder stateHolder = {initialState};
return Xoroshiro64StarStar(stateHolder);
}

uint32_t operator()()
{
const uint32_t result = rotl(stateHolder.state[0]*0x9E3779BBu,5u)*5u;
stateHolder.xoroshiro64_state_advance();

return result;
}

return result;
}
Xoroshiro64StateHolder stateHolder;
};

}
}
Expand Down
3 changes: 2 additions & 1 deletion src/nbl/builtin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "glsl/property_pool/transfer.glsl")
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "glsl/property_pool/copy.comp")
# random numbers
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "glsl/random/xoroshiro.glsl")
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/random/xoroshiro.hlsl")
# sampling
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "glsl/sampling/bilinear.glsl")
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "glsl/sampling/box_muller_transform.glsl")
Expand Down Expand Up @@ -258,11 +259,11 @@ LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/cpp_compat/vector.hlsl")
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/cpp_compat/promote.hlsl")
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/cpp_compat/intrinsics.h")

LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/bit.hlsl")

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


macro(NBL_ADD_BUILTIN_RESOURCES _TARGET_) # internal & Nabla only, must be added with the macro to properly propagate scope
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")
endmacro()
Expand Down