Skip to content

Commit 3fd2e14

Browse files
author
kevyuu
committed
Add tea, lcg and pcg into nb::hlsl::random
1 parent c35d01a commit 3fd2e14

File tree

5 files changed

+112
-1
lines changed

5 files changed

+112
-1
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (C) 2018-2025 - 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+
5+
#ifndef _NBL_BUILTIN_GLSL_RANDOM_LCG_HLSL_INCLUDED_
6+
#define _NBL_BUILTIN_GLSL_RANDOM_LCG_HLSL_INCLUDED_
7+
8+
namespace nbl
9+
{
10+
namespace hlsl
11+
{
12+
13+
struct Lcg
14+
{
15+
static Lcg construct(NBL_CONST_REF_ARG(uint32_t) state)
16+
{
17+
return Lcg(state);
18+
}
19+
20+
uint32_t2 operator()()
21+
{
22+
uint32_t LCG_A = 1664525u;
23+
uint32_t LCG_C = 1013904223u;
24+
state = (LCG_A * state + LCG_C);
25+
state &= 0x00FFFFFF;
26+
return state;
27+
}
28+
29+
uint32_t state;
30+
};
31+
32+
}
33+
}
34+
#endif
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (C) 2018-2025 - 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+
5+
#ifndef _NBL_BUILTIN_GLSL_RANDOM_PCG_HLSL_INCLUDED_
6+
#define _NBL_BUILTIN_GLSL_RANDOM_PCG_HLSL_INCLUDED_
7+
8+
namespace nbl
9+
{
10+
namespace hlsl
11+
{
12+
13+
struct Pcg
14+
{
15+
static Pcg construct(NBL_CONST_REF_ARG(uint32_t) initialState)
16+
{
17+
uint32_t state = {initialState};
18+
return Pcg(state);
19+
}
20+
21+
uint32_t operator()()
22+
{
23+
const uint32_t tmp = state * 747796405u + 2891336453u;
24+
const uint32_t word = ((tmp >> ((tmp >> 28u) + 4u)) ^ tmp) * 277803737u;
25+
state = (word >> 22u) ^ word;
26+
return state;
27+
}
28+
29+
uint32_t state;
30+
};
31+
32+
}
33+
}
34+
#endif
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (C) 2018-2025 - 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+
5+
#ifndef _NBL_BUILTIN_GLSL_RANDOM_TEA_HLSL_INCLUDED_
6+
#define _NBL_BUILTIN_GLSL_RANDOM_TEA_HLSL_INCLUDED_
7+
8+
namespace nbl
9+
{
10+
namespace hlsl
11+
{
12+
13+
struct Tea
14+
{
15+
static Tea construct()
16+
{
17+
Tea tea = {};
18+
return tea;
19+
}
20+
21+
uint32_t2 operator()(uint32_t stream, uint32_t sequence, uint32_t roundCount)
22+
{
23+
uint32_t sum = 0;
24+
uint32_t v0 = stream;
25+
uint32_t v1 = sequence;
26+
for (uint32_t n = 0; n < roundCount; n++)
27+
{
28+
sum += 0x9e3779b9;
29+
v0 += ((v1 << 4) + 0xa341316c) ^ (v1 + sum) ^ ((v1 >> 5) + 0xc8013ea4);
30+
v1 += ((v0 << 4) + 0xad90777d) ^ (v0 + sum) ^ ((v0 >> 5) + 0x7e95761e);
31+
}
32+
33+
return uint32_t2(v0, v1);
34+
}
35+
36+
};
37+
38+
}
39+
}
40+
#endif

src/nbl/builtin/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ 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")
108108
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/random/xoroshiro.hlsl")
109+
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/random/pcg.hlsl")
110+
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/random/lcg.hlsl")
111+
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/random/tea.hlsl")
109112
# sampling
110113
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "glsl/sampling/bilinear.glsl")
111114
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "glsl/sampling/box_muller_transform.glsl")

0 commit comments

Comments
 (0)