Having some problems getting ray querying working

I know the rayQuery feature is working, but it’s not hitting anything.
I checked with the shader loggers and it’s logging in the while(rayQueryProceedEXT(rayQuery)){} area.
I’ve also checked the Acceleration Structure and the mesh is there.

It’s working fine in the full raytracer.

vec3 CalcNormalDirLight(Vertex vertex, MaterialProperties material, mat3 TBN, vec3 normal, vec2 uv, int index)
{
    vec3 LightPos = DLight[index].directionalLight.direction;
    vec3 ViewPos = sceneData.CameraPos;
    vec3 FragPos2 = vertex.Position;
    if (material.NormalMapID != 0)
    {
        LightPos = TBN * DLight[index].directionalLight.direction;
        ViewPos = TBN * sceneData.CameraPos;
        FragPos2 = TBN *  vertex.Position;;
    }
    vec3 ViewDir = normalize(ViewPos - FragPos2);

    const vec3 lightDir = normalize(-LightPos);
    const float diff = max(dot(normal, lightDir), 0.0);

    const vec3 halfwayDir = normalize(lightDir + ViewDir);
    const float spec = pow(max(dot(normal, halfwayDir), 0.0), material.Shininess);

    vec3 ambient = DLight[index].directionalLight.ambient * material.Diffuse.rgb;
    vec3 diffuse = DLight[index].directionalLight.diffuse * diff * material.Diffuse.rgb;
    vec3 specular = DLight[index].directionalLight.specular * spec * material.Specular;
    if (material.DiffuseMapID != 0)
    {
        ambient = DLight[index].directionalLight.ambient * vec3(texture(TextureMap[material.DiffuseMapID], uv));
        diffuse = DLight[index].directionalLight.diffuse * diff * vec3(texture(TextureMap[material.DiffuseMapID], uv));
    }
    if (material.SpecularMapID != 0)
    {
        specular = DLight[index].directionalLight.specular * spec * vec3(texture(TextureMap[material.SpecularMapID], uv));
    }

    float LightDistance = length(LightPos - FragPos2);
    vec3 result = ambient;

         float tmin = 0.001;
	    float tmax = LightDistance;
	    vec3 origin = gl_WorldRayOriginEXT + gl_WorldRayDirectionEXT * gl_HitTEXT;
	    shadowed = true;  
	    traceRayEXT(topLevelAS, gl_RayFlagsSkipClosestHitShaderEXT, 0xFF, 1, 0, 1, origin, tmin, lightDir, tmax, 1);
	    if (shadowed) 
        {
            result += (diffuse + specular) * 0.3f;
	    }
        else
        {
           result += (diffuse + specular) ;
        }

    return result;
}

But when I’m trying with ray querying in the raster shader, it’s getting no hits.

vec3 CalcNormalDirLight(MaterialProperties material, mat3 TBN, vec3 normal, vec2 uv, int index)
{
    vec3 LightPos = DLight[index].directionalLight.direction;

    vec3 ViewPos = sceneData.CameraPos;
    vec3 FragPos2 = FragPos;
    if (material.NormalMapID != 0)
    {
        LightPos = TBN * DLight[index].directionalLight.direction;
        ViewPos = TBN * sceneData.CameraPos;
        FragPos2 = TBN * FragPos;
    }
    vec3 ViewDir = normalize(ViewPos - FragPos2);

    const vec3 lightDir = normalize(-LightPos);
    const float diff = max(dot(normal, lightDir), 0.0);

    const vec3 halfwayDir = normalize(lightDir + ViewDir);
    const float spec = pow(max(dot(normal, halfwayDir), 0.0), material.Shininess);

    vec3 ambient = DLight[index].directionalLight.ambient * material.Diffuse.rgb;
    vec3 diffuse = DLight[index].directionalLight.diffuse * diff * material.Diffuse.rgb;
    vec3 specular = DLight[index].directionalLight.specular * spec * material.Specular;
    if (material.DiffuseMapID != 0)
    {
        ambient = DLight[index].directionalLight.ambient * vec3(texture(TextureMap[material.DiffuseMapID], uv));
        diffuse = DLight[index].directionalLight.diffuse * diff * vec3(texture(TextureMap[material.DiffuseMapID], uv));
    }
    if (material.SpecularMapID != 0)
    {
        specular = DLight[index].directionalLight.specular * spec * vec3(texture(TextureMap[material.SpecularMapID], uv));
    }

    float LightDistance = length(LightPos - FragPos2);

      vec3 result = ambient;
      vec3  origin    = FragPos;
      vec3  direction = normalize(FragPos - LightPos); 
      float tMin      = 0.001f;
      float tMax      = LightDistance ;

      rayQueryEXT rayQuery;
      rayQueryInitializeEXT(rayQuery, topLevelAS, gl_RayFlagsTerminateOnFirstHitEXT, 0xFF, origin, tMin, direction, tMax);

      while(rayQueryProceedEXT(rayQuery))
      {
      }

      if(rayQueryGetIntersectionTypeEXT(rayQuery, true) != gl_RayQueryCommittedIntersectionNoneEXT)
      {
         result += (diffuse + specular) * 0.3f;
      }
      else
      {
         result += (diffuse + specular);
      }

   return result;
}

Do you get any validation errors from standard validation?

If the “full raytracer” is working, set result, origin, direction, tMin, tMax to hard-coded values, then see if you get the same result for both. If yes, try to gradually change some of the parameters until the “ray querying” approach starts to fail.

Nope, no validation errors.
Though I did try putting the raster ray query settings into the raytracer and the shadows look a lot more jagged. Looks like I might be better off doing ray tracing a shadow map and do shadows that way.

Is there a way to get the value of how far away the ray had to travel to hit?
Looks like I’ll have to do a raytraced depth map to get it to work like i want it too.

Yeah, gl_RayTmaxEXT should contain that information.
Citing the specification of GLSL_EXT_ray_tracing.txt:

    Add the following description for gl_RayTminEXT and gl_RayTmaxEXT:

    The variables gl_RayTminEXT and gl_RayTmaxEXT are available in the intersection,
    any-hit, closest-hit, and miss shaders to specify the parametric <Tmin>
    and <Tmax> values of the ray being processed. The values are independent
    of the space in which the ray and origin exist.

    The <Tmin> value remains constant for the duration of the ray query, while
    the <Tmax> value changes throughout the lifetime of the ray query that
    produced the intersection. In the closest-hit shader, the value reflects
    the closest distance to the intersected primitive. In the any-hit shader,
    it reflects the distance to the primitive currently being intersected. In
    the intersection shader, it reflects the distance to the closest primitive
    intersected so far. The value can change in the intersection shader after
    calling reportIntersectionEXT() if the corresponding any-hit shader does not
    ignore the intersection. In a miss shader, the value is identical to the
    parameter passed into traceRayEXT().

    Add the following description for gl_IncomingRayFlagsEXT:

    gl_IncomingRayFlagsEXT is available in intersection, closest-hit, any-hit, and miss,
    shaders to specify the current ray's flags as passed via the 'rayflags' argument of
    traceRayEXT() call resulting in invocation of current shader stage.

    Add the following description for gl_HitTEXT:

    gl_HitTEXT is available only in the any-hit and closest-hit shaders. This is an
    alias of gl_RayTmaxEXT added to closest-hit and any-hit shaders for convenience.