Is this ray tracing function that runs on the GPU, GPU safe?

Viewed 196

I have tried to write a simple ray tracer in the fragment shader. I have this function that is supposed to create a diffuse sphere as follows :

enter image description here

Here is the function :

vec3 GetRayColor(Ray ray)
{
Ray new_ray = ray;
vec3 FinalColor = vec3(1.0f);

bool IntersectionFound = false;
int hit_times = 0;

for (int i = 0; i < RAY_BOUNCE_LIMIT; i++)
{
    RayHitRecord ClosestSphere = IntersectSceneSpheres(new_ray, 0.001f, MAX_RAY_HIT_DISTANCE);

    if (ClosestSphere.Hit == true)
    {
        // Get the final ray direction

        vec3 R;
        R.x = nextFloat(RNG_SEED, -1.0f, 1.0f); 
        R.y = nextFloat(RNG_SEED, -1.0f, 1.0f);
        R.z = nextFloat(RNG_SEED, -1.0f, 1.0f);

        vec3 S = normalize(ClosestSphere.Normal) + normalize(R);
        S = normalize(S);

        new_ray.Origin = ClosestSphere.Point;
        new_ray.Direction = S;

        hit_times += 1;
        IntersectionFound = true;
    }

    else
    {
        FinalColor = GetGradientColorAtRay(new_ray);
        break;
    }
}

if (IntersectionFound)
{
    FinalColor /= 2.0f; // Lambertian diffuse only absorbs half the light
    FinalColor = FinalColor / float(hit_times);
}

return FinalColor;
}

For some reason, it seems like hit_times is constant. This exact same code works on the CPU and produced the attached screenshot.

I'm not sure if this something to do with the GPU. But I've tested all the other functions and they work as expected.

  1. The normals are fine and identical
  2. The random number generator works fine and has been visualized

Here is the Normal + RandomVec or S visualized :

enter image description here

And it is the exact same when done on the CPU.

Here is the hit_times visualised on the CPU

enter image description here

But on the GPU, all three spheres are white. Here is the full fragment shader : https://pastebin.com/3xeA6LtT Here is the code that works on the CPU : https://pastebin.com/eyLnHYzr

1 Answers

All spheres are white most likely because ClosestSphere.Hit in GetRayColor function is always true.

I think the problem is in your IntersectSceneSpheres function.

In CPU code you return HitAnything which is defaulted to false. At the same time in the fragment shader you return struct ClosestRecord that remains uninitialized if nothing was hit.

Explicitly adding ClosestRecord.Hit = HitAnything; in the end of IntersectSceneSpheres function should fix it

Related