Basic RNG
This commit is contained in:
18
shader/_test-rng-xoroshiro128plus.slang
Normal file
18
shader/_test-rng-xoroshiro128plus.slang
Normal file
@ -0,0 +1,18 @@
|
||||
//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type
|
||||
//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type
|
||||
//TEST(compute):COMPARE_COMPUTE_EX:-cpu -compute -shaderobj -output-using-type
|
||||
|
||||
import rng;
|
||||
|
||||
//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0 0 0 0 0], stride=4):out,name=outputBuffer
|
||||
RWStructuredBuffer<uint64_t> outputBuffer;
|
||||
|
||||
[numthreads(1, 1, 1)]
|
||||
void computeMain(int3 dispatchThreadID : SV_DispatchThreadID)
|
||||
{
|
||||
Xoroshiro128Plus rng = {1, 2};
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
outputBuffer[i] = rng.next();
|
||||
}
|
||||
}
|
6
shader/_test-rng-xoroshiro128plus.slang.expected.txt
Normal file
6
shader/_test-rng-xoroshiro128plus.slang.expected.txt
Normal file
@ -0,0 +1,6 @@
|
||||
type: uint64_t
|
||||
3
|
||||
412333834243
|
||||
2360170716294286339
|
||||
9295852285959843169
|
||||
2797080929874688578
|
@ -2,10 +2,15 @@ StructuredBuffer<float> buffer0;
|
||||
StructuredBuffer<float> buffer1;
|
||||
RWStructuredBuffer<float> result;
|
||||
|
||||
import rng;
|
||||
|
||||
[shader("compute")]
|
||||
[numthreads(1,1,1)]
|
||||
void computeMain(uint3 threadId : SV_DispatchThreadID)
|
||||
{
|
||||
uint index = threadId.x;
|
||||
result[index] = buffer0[index] + buffer1[index];
|
||||
}
|
||||
Xoroshiro128Plus generator = {1, 2};
|
||||
for (var i = 0; i < 10; i++)
|
||||
{
|
||||
result[i] = generator.next();
|
||||
}
|
||||
}
|
44
shader/rng.slang
Normal file
44
shader/rng.slang
Normal file
@ -0,0 +1,44 @@
|
||||
module rng;
|
||||
|
||||
public interface IRngCore
|
||||
{
|
||||
[mutating]
|
||||
public uint64_t next();
|
||||
|
||||
[mutating]
|
||||
public float next_float();
|
||||
}
|
||||
|
||||
uint64_t rotl(uint64_t x, int k)
|
||||
{
|
||||
return (x << k) | (x >> (64 - k));
|
||||
}
|
||||
|
||||
float u64_to_float(uint64_t x)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public struct Xoroshiro128Plus : IRngCore
|
||||
{
|
||||
internal uint64_t s0;
|
||||
internal uint64_t s1;
|
||||
|
||||
[mutating]
|
||||
public uint64_t next()
|
||||
{
|
||||
var result = s0 + s1;
|
||||
|
||||
s1 ^= s0;
|
||||
s0 = rotl(s0, 24) ^ s1 ^ (s1 << 16);
|
||||
s1 = rotl(s1, 37);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
[mutating]
|
||||
public float next_float()
|
||||
{
|
||||
return u64_to_float(next());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user