Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 361
Samuel Avatar asked Jan 06 '21 14:01

Samuel


People also ask

Should I turn on ray tracing?

Ray tracing brings down your frame rate even if you're running the latest high-end hardware. Sometimes causing a drop from 100 to 60 fps just by turning it on. While ray tracing makes for magnificent visuals and images, the boost in quality it provides in gaming isn't that significant.

What is ray tracing in GPU?

Ray tracing is a method of graphics rendering that simulates the physical behavior of light. Thought to be decades away from reality, NVIDIA has made real-time ray tracing possible with NVIDIA RTX™ the first-ever real-time ray-tracing GPU—and has continued to pioneer the technology since.

Is ray tracing only for RTX?

Only the RTX 20-series cards were capable of supporting ray tracing and DLSS.


Video Answer


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

like image 124
Dr.Neo Avatar answered Nov 14 '22 02:11

Dr.Neo