|
raytracer
Parallel Ray Tracer written from scratch (no graphics API).
|
A renderer has no reliable expected output: for any non-trivial scene, there is no independent way to know what the correct image looks like, short of writing a second renderer. This is the test oracle problem. Metamorphic testing sidesteps it by not checking a single output against a known answer, but instead checking how the output should change (or stay the same) when the input is transformed in a known way. For example, moving the entire scene and the camera by the same offset should not change the image, whatever that image is. The approach is well established for graphics software. GraphicsFuzz, for instance, applies it to find bugs in shader compilers.
The ray tracer adds a second difficulty: it is a Monte Carlo renderer, so every image contains sampling noise. A useful test therefore has to distinguish a real change in the picture from a different arrangement of noise.
Images are compared in memory, on the floating-point colour values produced by the renderer, before they are quantized to 8-bit BMP output. Quantization would both hide small real differences and exaggerate tiny ones that happen to cross a colour bucket boundary.
The similarity metric (common/include/common/image_similarity.h) splits both images into square blocks (size 4x4 by default), averages the colour of each block, and returns the mean Euclidean distance between corresponding block averages, weighted by block area so that partial blocks at the image edges count proportionally. Averaging over blocks acts as a low-pass filter: per-pixel sampling noise largely cancels out, while structural differences, such as a missing or displaced object, remain. A downsampling factor of 1 means plain per-pixel mean distance.
Each relation is checked in one of two ways, depending on what it can guarantee:
The tolerance threshold was measured experimentally. Rendering the same scene with different random number streams gives a number between 0.009-0.010 at 4 rays per pixel and a block size of 4. The tolerance was set to twice that (0.02). To confirm that the threshold still has detection power, the rotation test was deliberately broken by rotating only the camera. The difference rose to 0.089, about 4.5x the tolerance.
The random number generator is seeded with a fixed value in tests. This makes failures reproducible, and it is what makes the exact check possible at all: with the same seed and thread count, rendering is fully deterministic.
Ray tracer (raytracer/test/test_renderer.cpp):
| Relation | Check |
|---|---|
| Rotating the scene and the camera together by 90 degrees around the vertical axis | Within tolerance |
| Translating the scene and the camera together by a large offset (100000) | Within tolerance |
| Replacing a diffuse material with a textured one of the same constant colour | Exact |
| Adding an object fully enclosed by another opaque object | Exact |
| Reordering the scene's object list | Exact |
| Adding an object behind the camera | Within tolerance |
| Rendering with 1, 2, 4 and 8 parallel threads | Within tolerance |
Mandelbrot set (mandelbrot/test/test_renderer.cpp): rendering a region as two halves, or as four quadrants at twice the zoom, and stitching the parts together must reproduce the full render exactly.