I am implementing a ray tracing program using VS2015 community. The program fills a screen size color buffer (width * height
with element type of Eigen::Vector3f
), then save the buffer content to a ppm file.
The basic loop (Note the output section at the end of the outer loop):
using Vec3f = Eigen::Vector3f;
Vec3f * buffer = new Vec3f[w * h];
// for each pixel
for (int y = 0; y < h; y++) for (int x = 0; x < w; x++)
{
int const i = (h - y - 1) * w + x;
buffer[i] = Vec3f::Zero();
// 2x2 subpixel
for (int sy = 0; sy < 2; sy++) for (int sx = 0; sx < 2; sx++)
{
Vec3f r = Vec3f::Zero();
// sampling
for (int s = 0; s < samps; s++)
{
// do some computation and accumulation to r
// r = ...
}
buffer[i] = buffer[i] + r;
}
buffer[i] = buffer[i] * 0.25f;
// debug with an output section
//if (x % 16 == 0 && y % 16 == 0)
// std::cout << buffer[i] << std::endl;
}
I can get proper result with configurations of:
- Debug, x86 or x64;
- Release, x86;
- Release (optimization = \Od), x64.
However, the buffer is all zero vectors with Release (optimization = \O1 or \O2 or \Ox), x64, and what I got is a black picture.
So with Release (optimization = \O1 or \O2 or \Ox), x64, I uncommented the output section to check the values in the buffer. The strange thing is, each pixel that I checked has a correct value, those not checked remains zero vector. For example, if I check every 16 pixels like the upper code, I will get a picture like this (256*256 black tessellated every 16 pixel):
I googled and read some materials like Surviving the Release Version, but still have no idea. Could anyone provide some experiences dealing with these problems?
Update: The code above is not so detailed, full code is here, depends on Eigen 3.2.6.