Some rays seems to execute the wrong hit group on a basic raytracing test

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;

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      =;
    ray.Direction   = normalize( -;
    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;

void Miss(inout RayPayload payload)
    payload.color = float4(1.f, 0.f, 0.f, 0.f);

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.

Hi @martin.gerard936!

Sorry to see that you didn’t get a response yet. Sadly my own Ray tracing knowledge is rudimentary at best, I can’t help directly. But I can try to get some more experienced eyes to take a look.

Meanwhile, in what environment are you testing this? Unreal Engine? If so, which version and how did you configure ray tracing support?

Hopefully we will manage to get you some suggestions.


I’m working with a in-house engine, I managed to get a proper behaviour by replacing the TraceRay line in the shader by this one

TraceRay(Scene, RAY_FLAG_NONE, ~0, 0, 0, 0, ray, payload);

I don’t understand why I still get different hit groups with a stride of 0 instead of 1 but at least it works