Attributes passing from intersection

Hi, experts.
I found a strange problem passing the attributes from the closest intersection programme. The shading is total wrong as the attributes are unreasonable. (The intersection is right because the projection of the scene is right). I simplified the problem code as follows:

// mesh file
#include <optix_world.h>

using namespace optix;
struct Vertex
{
float3 position;
float3 normal;
float2 tex;
};

rtBuffer vertex_buffer;
rtBuffer index_buffer;

rtBuffer material_buffer; // per-face material index
rtDeclareVariable(float3, texcoord, attribute texcoord, );
rtDeclareVariable(float3, geometric_normal, attribute geometric_normal, );
rtDeclareVariable(float3, shading_normal, attribute shading_normal, );
rtDeclareVariable(optix::Ray, ray, rtCurrentRay, );

RT_PROGRAM void mesh_intersect(int primIdx)
{
int3 v_idx = index_buffer[primIdx];

float3 p0 = vertex_buffer[v_idx.x].position;
float3 p1 = vertex_buffer[v_idx.y].position;
float3 p2 = vertex_buffer[v_idx.z].position;

// Intersect ray with triangle
float3 n;
float  t, beta, gamma;
if (intersect_triangle(ray, p0, p1, p2, n, t, beta, gamma))
{
		shading_normal = make_float3(1.0f, 0.0f, 0.0f);
		geometric_normal = make_float3(1, 0, 0);
		texcoord = make_float3(1, 0, 0);
		rtReportIntersection(material_buffer[primIdx]);
}

}

RT_PROGRAM void mesh_bounds(int primIdx, optix::Aabb* aabb)
{
const int3 v_idx = index_buffer[primIdx];

const float3 v0 = vertex_buffer[v_idx.x].position;
const float3 v1 = vertex_buffer[v_idx.y].position;
const float3 v2 = vertex_buffer[v_idx.z].position;
const float  area = length(cross(v1 - v0, v2 - v0));

if (area > 0.0f && !isinf(area)) 
{
	aabb->m_min = fminf(fminf(v0, v1), v2);
	aabb->m_max = fmaxf(fmaxf(v0, v1), v2);
}
else {
	aabb->invalidate();
}

}

// shading file:

#include <optix.h>
#include <optixu/optixu_math_namespace.h>

using namespace optix;

rtDeclareVariable(float, scene_epsilon, , );
rtDeclareVariable(rtObject, reflectors, , );
rtDeclareVariable(uint, max_depth, , );

rtDeclareVariable(optix::Ray, ray, rtCurrentRay, );
rtDeclareVariable(float, t_hit, rtIntersectionDistance, );

device float myfmax(float a, float b) {
return ((a) > (b) ? a : b);
}
struct PerRayData_shadow
{
float3 attenuation;
float t_hit;
};
rtDeclareVariable(PerRayData_shadow, prd_shadow, rtPayload, );

rtDeclareVariable(float3, texcoord, attribute texcoord, );
rtDeclareVariable(float3, geometric_normal, attribute geometric_normal, );
rtDeclareVariable(float3, shading_normal, attribute shading_normal, );

RT_PROGRAM void closest_hit_radiance()
{
prd_shadow.attenuation = shading_normal;
}

RT_PROGRAM void any_hit_shadow()
{
prd_shadow.attenuation = make_float3(0, 0, 0);
rtTerminateRay();
}

The result image is like a irreasonable random image.
If I change the name of the attribute, runtime programe raises a error " Attribute “shading_normal1” is referenced in a hit program but is not produced in attached intersection program" , which means the attribute is actually attached. However, the value is wrong.
Could anyone guild me a direction to solve this problem?

The best

I know attributes passing has something to do with rtPotentialIntersection
So the simplifed intersection code is :

if (intersect_triangle(ray, p0, p1, p2, n, t, beta, gamma))
{
if (rtPotentialIntersection(t))
{
shading_normal = make_float3(1.0f, 0.0f, 0.0f);
rtReportIntersection(material_buffer[primIdx]);
}
}

However, the problem remains.

I use a single plane as the test. And rtprintf tells me primIdx is 0 and t is a positive distance value. All seems reasonable except the shading_normal cannot pass out.

I guess there is some compile things, but I donot know how.

What do you mean by ‘the shading attributes are unreasonable’? Have you visualized the shading normals returned via attribute to see if they look correct? Are there transforms in your scene? If so, do you use rtTransformNormal to transform them from object space into world space?

your intersection code looks fine.

Thanks for your reply. I have visulized it by directly output it onto screen. It is blinking and has many little blocks on screen.

Can you write a solid color (instead of the normal) into your per-ray data struct’s output field within your closest-hit program and see if that works? You might be getting stack overflows or something else unrelated to your attributes.

I tried. If I output a const color value, the shading closest-hit programe is right. But it is not true with attribute variable.

Well, the blockiness sounds like uninitialized memory. Have you tried printing optix exceptions?

You could zip up your code and send it to optix-help@nvidia.com and I could take a quick look. You will have to rename your archive to .piz or something since an actual .zip extension will cause it to get blocked.

Also, are you intending to write into a PerRayData_shadow struct from the closest_hit function?

I have emailed to optix-help@nvidia.com with a sample code.

That doesn’t seem to have made it through, yet.
If you attached a *.zip archive file, the mail is going to be blocked by our SPAM filters.
In that case please rename the extension to *.zi_ and resend again

The reason is that I set both any-hit function and closet-hit function to one certain type of ray. I thought it was compatible. However, it is not.
Thanks for the experts.