Merge pull request #2613 from lammps/ssao_bugfix

Fix bug in dump image SSAO depth shading
This commit is contained in:
Axel Kohlmeyer
2021-02-19 18:47:41 -05:00
committed by GitHub
2 changed files with 131 additions and 91 deletions

View File

@ -113,6 +113,11 @@ Image::Image(LAMMPS *lmp, int nmap_caller) : Pointers(lmp)
backLightColor[2] = 0.9;
random = nullptr;
// MPI_Gatherv vectors
recvcounts = nullptr;
displs = nullptr;
}
/* ---------------------------------------------------------------------- */
@ -134,6 +139,9 @@ Image::~Image()
memory->destroy(rgbcopy);
if (random) delete random;
memory->destroy(recvcounts);
memory->destroy(displs);
}
/* ----------------------------------------------------------------------
@ -334,16 +342,37 @@ void Image::merge()
// extra SSAO enhancement
// bcast full image to all procs
// each works on subset of pixels
// gather result back to proc 0
// MPI_Gather() result back to proc 0
// use Gatherv() if subset of pixels is not the same size on every proc
if (ssao) {
MPI_Bcast(imageBuffer,npixels*3,MPI_BYTE,0,world);
MPI_Bcast(surfaceBuffer,npixels*2,MPI_DOUBLE,0,world);
MPI_Bcast(depthBuffer,npixels,MPI_DOUBLE,0,world);
compute_SSAO();
int pixelPart = height/nprocs * width*3;
MPI_Gather(imageBuffer+me*pixelPart,pixelPart,MPI_BYTE,
rgbcopy,pixelPart,MPI_BYTE,0,world);
int pixelstart = 3 * static_cast<int> (1.0*me/nprocs * npixels);
int pixelstop = 3 * static_cast<int> (1.0*(me+1)/nprocs * npixels);
int mypixels = pixelstop - pixelstart;
if (npixels % nprocs == 0) {
MPI_Gather(imageBuffer+pixelstart,mypixels,MPI_BYTE,
rgbcopy,mypixels,MPI_BYTE,0,world);
} else {
if (recvcounts == nullptr) {
memory->create(recvcounts,nprocs,"image:recvcounts");
memory->create(displs,nprocs,"image:displs");
MPI_Allgather(&mypixels,1,MPI_INT,recvcounts,1,MPI_INT,world);
displs[0] = 0;
for (int i = 1; i < nprocs; i++)
displs[i] = displs[i-1] + recvcounts[i-1];
}
MPI_Gatherv(imageBuffer+pixelstart,mypixels,MPI_BYTE,
rgbcopy,recvcounts,displs,MPI_BYTE,0,world);
}
writeBuffer = rgbcopy;
} else {
writeBuffer = imageBuffer;
@ -880,11 +909,19 @@ void Image::compute_SSAO()
-tanPerPixel / zoom;
int pixelRadius = (int) trunc (SSAORadius / pixelWidth + 0.5);
int x,y,s;
int hPart = height / nprocs;
int index = me * hPart * width;
for (y = me * hPart; y < (me + 1) * hPart; y ++) {
for (x = 0; x < width; x ++, index ++) {
// each proc is assigned a subset of contiguous pixels from the full image
// pixels are contiguous in x (columns within a row), then by row
// index = pixels from 0 to npixel-1
// x = column # from 0 to width-1
// y = row # from 0 to height-1
int pixelstart = static_cast<int> (1.0*me/nprocs * npixels);
int pixelstop = static_cast<int> (1.0*(me+1)/nprocs * npixels);
for (int index = pixelstart; index < pixelstop; index++) {
int x = index % width;
int y = index / width;
double cdepth = depthBuffer[index];
if (cdepth < 0) { continue; }
@ -895,7 +932,7 @@ void Image::compute_SSAO()
double mytheta = random->uniform() * SSAOJitter;
double ao = 0.0;
for (s = 0; s < SSAOSamples; s ++) {
for (int s = 0; s < SSAOSamples; s ++) {
double hx = cos(mytheta);
double hy = sin(mytheta);
mytheta += delTheta;
@ -985,7 +1022,6 @@ void Image::compute_SSAO()
imageBuffer[index * 3 + 2] = (int) c[2];
}
}
}
/* ---------------------------------------------------------------------- */

View File

@ -73,6 +73,10 @@ class Image : protected Pointers {
double *depthcopy,*surfacecopy;
unsigned char *imageBuffer,*rgbcopy,*writeBuffer;
// MPI_Gatherv
int *recvcounts,*displs;
// constant view params
double FOV;