precompute random numbers to avoid memory corruption with multi-threading

This commit is contained in:
Axel Kohlmeyer
2024-08-15 00:25:28 -04:00
parent 97026569a7
commit 68b808d3b5

View File

@ -959,6 +959,10 @@ void Image::compute_SSAO()
int pixelstart = static_cast<int> (1.0*me/nprocs * npixels); int pixelstart = static_cast<int> (1.0*me/nprocs * npixels);
int pixelstop = static_cast<int> (1.0*(me+1)/nprocs * npixels); int pixelstop = static_cast<int> (1.0*(me+1)/nprocs * npixels);
// file buffer with random numbers to avoid race conditions
double *uniform = new double[pixelstop - pixelstart];
for (int i = 0; i < pixelstop - pixelstart; ++i) uniform[i] = random->uniform();
#if defined(_OPENMP) #if defined(_OPENMP)
#pragma omp parallel for #pragma omp parallel for
#endif #endif
@ -973,7 +977,7 @@ void Image::compute_SSAO()
double sy = surfaceBuffer[index * 2 + 1]; double sy = surfaceBuffer[index * 2 + 1];
double sin_t = -sqrt(sx*sx + sy*sy); double sin_t = -sqrt(sx*sx + sy*sy);
double mytheta = random->uniform() * SSAOJitter; double mytheta = uniform[index - pixelstart] * SSAOJitter;
double ao = 0.0; double ao = 0.0;
for (int s = 0; s < SSAOSamples; s ++) { for (int s = 0; s < SSAOSamples; s ++) {
@ -1063,6 +1067,7 @@ void Image::compute_SSAO()
imageBuffer[index * 3 + 1] = (int) c[1]; imageBuffer[index * 3 + 1] = (int) c[1];
imageBuffer[index * 3 + 2] = (int) c[2]; imageBuffer[index * 3 + 2] = (int) c[2];
} }
delete[] uniform;
} }
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */