I’m having some trouble using raytracing on a simple scene, when tracing a ray, some objects will intersect fine, and some other objects will behave as if they are assigned to the wrong hit group, even though I only have one and all objects are assigned to it.
I’m running a very simple shader, there is only a closest hit shader in the hit group and it always returns white. So every object assigned to this hit group should be white on the screen, and red if it’s a miss. Here are the shaders
struct RayPayload
{
float4 color;
};
RaytracingAccelerationStructure Scene : register(t0, space2);
RWTexture2D<float4> AlbedoMap : register(u0);
cbuffer cb0 : register(b0)
{
float4x4 InvViewProj;
float4 Eye;
};
[shader("raygeneration")]
void RayGen()
{
float2 texcoords = (DispatchRaysIndex().xy + 0.5f) / (float2) DispatchRaysDimensions();
texcoords = texcoords * float2(2.f, -2.f) - float2(1.f, -1.f);
float4 target = mul(InvViewProj, float4(texcoords.xy, 0.5f, 1.f));
target /= target.w;
RayDesc ray;
ray.Origin = Eye.xyz;
ray.Direction = normalize(target.xyz - Eye.xyz);
ray.TMin = 1.f;
ray.TMax = 1e5f;
RayPayload payload = { float4(0, 0, 0, 0) };
TraceRay(Scene, RAY_FLAG_NONE, ~0, 0, 1, 0, ray, payload);
AlbedoMap[DispatchRaysIndex().xy] = payload.color;
}
[shader("miss")]
void Miss(inout RayPayload payload)
{
payload.color = float4(1.f, 0.f, 0.f, 0.f);
}
[shader("closesthit")]
void ClosestHitDefault(inout RayPayload payload, BuiltInTriangleIntersectionAttributes attr)
{
payload.color = 1.f;
}
In a pix capture, I can verify during the TLAS creation that every instance has InstanceContributionToHitGroupIndex = 0, which to my understanding, garantees that the instance is associated with the first hit group in the shader table.
It is clear that the pixels in black come from rays that intersect the scene but they don’t seem to execute ClosestHitDefault.