rtDeclareVariable float array

Hi,

I wish to declare a floating array to set in the mesh_intersect program and then access the data in the material closest_hit program. I have tried using rtBuffer but it has not worked. The array is only to be used for internal data passing between the mesh intersect and material program and does not need any data transfer to/from the host.

A sample mesh.cu code may look like:

rtDeclareVariable(float, x[20], attribute x[20], );

RT_PROGRAM void mesh_intersect( int triangleIndex )
{
  if( intersect_triangle( ray, p0, p1, p2, n, t, beta, gamma ) ) {

    if(  rtPotentialIntersection( t ) ) {

        for (unsigned int i = 0; i < 20; ++i) {
            x[i] = i;
        }
        rtReportIntersection(material_buffer[triangleIndex]);

    }
  }
}

and the matl.cu code:

rtDeclareVariable(float, x[20], attribute x[20], );

RT_PROGRAM void closest_hit_radiance()
{
    float a = x[2];
}

what is the best way to do this?

Thanks

Right, buffers will not work for that case, because OptiX needs to store the attributes of the closest hits and that only works for variables with the attribute semantic.

For attributes with some complex layout, you should define a structure and use that.
In your case something like this:

// Just an example filling in the barycentric coordinates. You would use a simple float2 for that normally.

// Writing complex attribute structure inside the intersection program:
// Put this structure declaration into a header and include in intersection and anyhit/closesthit programs. 
struct MyAttributes
{
  float x[2];
};

rtDeclareVariable(MyAttributes, attr, attribute MyAttributesSemanticName, );

...
   if (rtPotentialIntersection(t))
    {
      attr.x[0] = beta;
      attr.x[1] = gamma;

      rtReportIntersection(0);
    }
...

// Access inside the closest hit program:
struct MyAttributes
{
  float x[2];
};

rtDeclareVariable(MyAttributes, attr, attribute MyAttributesSemanticName, );

RT_PROGRAM void closesthit()
{
  const float alpha = 1.0f - attr.x[0] -  attr.x[1];
  thePrd.radiance = make_float3(alpha, attr.x[0], attr.x[1]);
}

In general you should try to keep the number of attributes as low as possible.
Read this post for potential optimizations for attribute calculations:
https://devtalk.nvidia.com/default/topic/1037810/?comment=5272924