Blender V5.0
lcg.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include "util/hash.h"
8
10
11/* Linear Congruential Generator */
12
13/* This is templated to handle multiple address spaces on Metal. */
14template<class T> ccl_device uint lcg_step_uint(T rng)
15{
16 /* implicit mod 2^32 */
17 *rng = (1103515245 * (*rng) + 12345);
18 return *rng;
19}
20
21/* This is templated to handle multiple address spaces on Metal. */
22template<class T> ccl_device float lcg_step_float(T rng)
23{
24 /* implicit mod 2^32 */
25 *rng = (1103515245 * (*rng) + 12345);
26 return (float)*rng * (1.0f / (float)0xFFFFFFFF);
27}
28
29template<class T> ccl_device float3 lcg_step_float3(T rng)
30{
31 /* Make sure the random numbers are evaluated in order. */
32 const float rand_x = lcg_step_float(rng);
33 const float rand_y = lcg_step_float(rng);
34 const float rand_z = lcg_step_float(rng);
35 return make_float3(rand_x, rand_y, rand_z);
36}
37
39{
40 uint rng = seed;
41 lcg_step_uint(&rng);
42 return rng;
43}
44
46 const uint rng_offset,
47 const uint sample,
48 const uint scramble)
49{
50 return hash_uint3(rng_hash ^ scramble, rng_offset, sample);
51}
52
unsigned int uint
static unsigned long seed
Definition btSoftBody.h:39
nullptr float
#define ccl_device_inline
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
ccl_device_inline uint hash_uint3(const uint kx, const uint ky, const uint kz)
Definition hash.h:153
CCL_NAMESPACE_BEGIN ccl_device uint lcg_step_uint(T rng)
Definition lcg.h:14
ccl_device_inline uint lcg_state_init(const uint rng_hash, const uint rng_offset, const uint sample, const uint scramble)
Definition lcg.h:45
ccl_device uint lcg_init(const uint seed)
Definition lcg.h:38
ccl_device float3 lcg_step_float3(T rng)
Definition lcg.h:29
ccl_device float lcg_step_float(T rng)
Definition lcg.h:22
#define T
#define ccl_device