SLProject  4.3.020
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLRay.cpp File Reference
#include <atomic>
#include <cmath>
#include <ctime>
#include <random>
#include <SLRay.h>
#include <SLSceneView.h>
#include <SLSkybox.h>
Include dependency graph for SLRay.cpp:

Go to the source code of this file.

Functions

SLfloat rnd01 ()
 

Detailed Description

Date
July 2014
Authors
Marcus Hudritsch
Remarks
Please use clangformat to format the code. See more code style on https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style

Definition in file SLRay.cpp.

Function Documentation

◆ rnd01()

SLfloat rnd01 ( )

Uniform random number generator for numbers between 0 and 1 that is used in SLRay, SLLightRect and SLPathtracer.

Remarks
The engine state is thread_local and must stay that way. The ray tracer and the path tracer call rnd01 concurrently from all worker threads (see SLPathtracer::render). A single shared std::mt19937 would be a data race on its 624 word state plus its position index. That is undefined behaviour, and in practice the racing threads hand each other torn and repeated values, so their samples are no longer independent and the noise no longer averages out with 1/sqrt(N). Each thread seeds its own engine from the current time mixed with a shared atomic counter, so that threads created within the same second still get different sequences.

Definition at line 50 of file SLRay.cpp.

51 {
52  static std::atomic<SLuint> seedCounter{0};
53 
54  thread_local std::mt19937 engine((SLuint)std::time(nullptr) * 2654435761u +
55  seedCounter.fetch_add(1u) * 40503u + 1u);
56 
57  thread_local std::uniform_real_distribution<SLfloat> dist(0.0f, 1.0f);
58 
59  return dist(engine);
60 }
unsigned int SLuint
analog to GLuint
Definition: SL.h:198