Per-thread RNGs and RNG bugfix

This commit is contained in:
David 2021-08-30 18:34:14 +02:00
commit 52806e4457
6 changed files with 70 additions and 39 deletions

View file

@ -66,15 +66,15 @@ struct vec3 {
}
// Get a vec3 with random components in the range [0,1)
inline static vec3 random()
inline static vec3 random(int32_t thread_id = 0)
{
return vec3(random_float(), random_float(), random_float());
return vec3(random_float(thread_id), random_float(thread_id), random_float(thread_id));
}
// Get a vec3 with random components in the range [min, max)
inline static vec3 random(float min, float max)
inline static vec3 random(float min, float max, int32_t thread_id = 0)
{
return vec3(random_float(min, max), random_float(min, max), random_float(min, max));
return vec3(random_float(min, max, thread_id), random_float(min, max, thread_id), random_float(min, max, thread_id));
}
// Check if all vector components are near zero
@ -162,12 +162,12 @@ inline vec3 normalize(const vec3 v)
}
// Returns a vec3 of random components between [-1,1) that is inside a unit sphere
vec3 random_in_unit_sphere()
vec3 random_in_unit_sphere(int32_t thread_id)
{
// Iterate until we find a vector with length < 1
while (true)
{
vec3 p = vec3::random(-1,1);
vec3 p = vec3::random(-1,1, thread_id);
if (p.length_squared() >= 1)
continue;
return p;
@ -175,14 +175,14 @@ vec3 random_in_unit_sphere()
}
// Returns a normalized version of the above vector
vec3 random_unit_vector()
vec3 random_unit_vector(int32_t thread_id)
{
return normalize(random_in_unit_sphere());
return normalize(random_in_unit_sphere(thread_id));
}
vec3 random_in_hemisphere(const vec3& normal)
vec3 random_in_hemisphere(const vec3& normal, int32_t thread_id)
{
vec3 in_unit_sphere = random_in_unit_sphere();
vec3 in_unit_sphere = random_in_unit_sphere(thread_id);
if (dot(in_unit_sphere, normal) > 0.0)
return in_unit_sphere;
@ -204,11 +204,11 @@ vec3 refract(const vec3& uv, const vec3& n, float etai_over_etat)
return r_out_perp + r_out_parallel;
}
vec3 random_in_unit_disk()
vec3 random_in_unit_disk(int32_t thread_id)
{
while (true)
{
auto p = vec3(random_float(-1,1), random_float(-1,1), 0);
auto p = vec3(random_float(-1,1,thread_id), random_float(-1,1,thread_id), 0);
if (p.length_squared() >= 1) continue;
return p;
}