Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question How to do [TraceRayInline] in closest hit shader?

Discussion in 'Graphics Experimental Previews' started by SanYue-TechArt, Apr 30, 2024.

  1. SanYue-TechArt

    SanYue-TechArt

    Joined:
    Sep 27, 2021
    Posts:
    2
    I wanna learn about DDGI implementation in URP

    After placing probes, i trace some rays from each probe, when ray intersect with any geometry, i need to trace ray again from intersection point to the directional light (so i can know this point is in shadow or not)

    Code (CSharp):
    1. // When this shader is called, that means ray from probe has hit geometry
    2. [shader("closesthit")]
    3. void ClosestHit(inout DDGIPayload payload : SV_RayPayload)
    4. {
    5.     // I need to trace shadow ray from hit point
    6.    // In order to know if the hit point is in shadow.
    7.     bool visibility = TraceDirectionalShadowRay(mainLight, hitPosWS);
    8.     if(!visibility) return;
    9.  
    10.     // If hit point is not in shadow, we can compute its radiance use Lambert Diffuse Law
    11.     // If hit point is in shadow, radiance will keep to zero.
    12.     float3 radiance = ComputeRadiance(...);
    13.     payload.radiance = radiance;
    14. }
    Code (CSharp):
    1.             bool TraceShadowRay(RayDesc rayDesc)
    2.             {
    3.                 RayQuery<RAY_FLAG_CULL_NON_OPAQUE | RAY_FLAG_SKIP_PROCEDURAL_PRIMITIVES | RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH> q;
    4.  
    5.                 q.TraceRayInline(_AccelerationStructure, RAY_FLAG_NONE, 0xFF, rayDesc);
    6.                 while (q.Proceed())
    7.                 {
    8.                     switch (q.CandidateType())
    9.                     {
    10.                         case CANDIDATE_NON_OPAQUE_TRIANGLE:
    11.                         {
    12.                             q.CommitNonOpaqueTriangleHit();
    13.                             break;
    14.                         }
    15.                     }
    16.                 }
    17.                 return q.CommittedStatus() != COMMITTED_TRIANGLE_HIT;
    18.             }
    19.  
    20.             bool TraceDirectionalShadowRay(Light light, float3 worldPos)
    21.             {
    22.                 RayDesc rayDesc;
    23.                 rayDesc.Origin      = worldPos;
    24.                 rayDesc.Direction   = light.direction;
    25.                 rayDesc.TMin        = 1e-1f;
    26.                 rayDesc.TMax        = FLT_MAX;
    27.  
    28.                 return TraceShadowRay(rayDesc);
    29.             }
    I found some implementations use TraceRayInline to trace shadow ray from intersection point, i tried to use the same way in Unity, but in URP (with Unity 2022.3.17f1), i got some errors:
    [ Opcode AllocateRayQuery not valid in shader model lib_6_3(closesthit) ]
    [ Opcode RayQuery_TraceRayInline not valid in shader model lib_6_3(closesthit) ]
     
  2. INedelcu

    INedelcu

    Unity Technologies

    Joined:
    Jul 14, 2015
    Posts:
    185
    Hi! Ray tracing shaders don't support ray queries, so you still have to use TraceRay for shadow rays. Be sure to set the #pragma max_recursion_depth 2 in your raytrace file if you shoot a shadow ray from a closest hit shader, otherwise you'll crash the GPU.
    Inline ray tracing (ray queries) support in rasterization and compute shaders was added in 2023.2.
     
  3. SanYue-TechArt

    SanYue-TechArt

    Joined:
    Sep 27, 2021
    Posts:
    2
    Thanks a lot for replying and useful tips! I will try to use TraceRay instead later