Tracking number of closest hits

Hello,

I’m attempting to use OptiX 5.1 for a volumetric ray tracing application. I managed to alter Tutorial 1 to index the number of hits via the ray payload with a member variable (hitCount). I incremented it on each hit, then on the second hit, use the ray length to define the color.

I’m now trying to get something similar to work with the mesh viewer application, and I’m experiencing some weirdness. I started by creating a custom first hit program, which dumps the hitCount variable with rtPrintf. I would expect this number to be 1 at all times, since I’m not launching any new rays, but that is not the case. I frequently observe 2 hits, sometimes as many as 5 hits, from this program!

Why would a first hit program fire multiple times for a given launch index?

Here’s my custom debugging program:

RT_PROGRAM void passthrough_hit()
{
  if(launch_index.x == 512 && launch_index.y == 512)
  {
    float3 hit_point = ray.origin + t_hit * ray.direction;
    optix::Ray newRay( hit_point, ray.direction, radiance_ray_type, scene_epsilon );
    float rayLength = length(hit_point - ray.origin);
    prd_radiance.hitCount++;
    rtPrintf("Hit count %d -- ray.origin %f,%f,%f -- rayLength %f\n",prd_radiance.hitCount, 
         ray.origin.x, ray.origin.y, ray.origin.z, rayLength);     
  }
  // Color my cube red, just to confirm I can see it
  prd_radiance.result = make_float3(0.5f,0.0f,0.0f);

}

Here’s an example block of the dumped text log:

Hit count 1 – ray.origin 3.575440,9.577191,3.157028 – rayLength 10.166735
Hit count 3 – ray.origin 3.575440,9.577191,3.157028 – rayLength 10.166735
Hit count 1 – ray.origin 3.575440,9.577191,3.157028 – rayLength 10.166735
Hit count 1 – ray.origin 3.575440,9.577191,3.157028 – rayLength 10.166735
Hit count 1 – ray.origin 3.575440,9.577191,3.157028 – rayLength 10.166735
Hit count 2 – ray.origin 3.575440,9.577191,3.157028 – rayLength 10.166735
Hit count 1 – ray.origin 3.575440,9.577191,3.157028 – rayLength 10.166735
Hit count 3 – ray.origin 3.575440,9.577191,3.157028 – rayLength 10.166735

Hi dreedy,

OptiX should only call your closest hit shader once per ray cast (per call to rtTrace()). I copied your code snippet into the optixMeshViewer sample (into cuda/phong.cu specifically) and I only see “Hit Count 1”. Are you initializing your hit count to 0 in your raygen program? Are you calling rtTrace() more than once in your raygen program? Is your closest hit program the only place that hitCount is being incremented?

If you check all of those things I mentioned and it’s still happening, can you send me your complete modified code reproducer?


David.

Hi David,

Thanks for the response! It was initialization - I am surprised that it was erroneously low integers, though, rather than occasional random numbers? Is this because OptiX reuses the same memory space for each launch, and was thus accidentally looking like it was actually hitting multiple times?

Dylan

That’s an interesting question, and a good guess, but I don’t think that’s it. With pixel (512,512), the thread is not that likely to end up re-using the same device memory every single time, especially if this is your display GPU and other things are going on.

More likely in this case is that your payload is small enough to fit in registers - which OptiX 5 & 6 will do automatically to your payload whenever it’s less than or equal to 32 bytes in size. I’m guessing in this case what you’ll get is the same register every time, and that register will have leftover contents from some other computation - could be a loop counter or one of the arguments to the kernel or something else in memory that is reliably low-valued. So it’s probably co-incidence that the value is low, but not surprising that the behavior seems deterministic.


David.

Understood. Thanks again for the feedback.