交叉着色器

用于实现自定义交集基元的着色器,用于与关联的边界卷相交的光线 (边界框) 。

交集着色器无权访问光线有效负载,但通过调用 ReportHit 为每个命中定义交集属性。 如果设置了光线标志RAY_FLAG_ACCEPT_FIRST_HIT_\AND_\END_SEARCH,或者从任何命中着色器调用 AcceptHitAndEndSearch,则 ReportHit 的处理可能会提前停止交集着色器。 否则,如果已接受命中,则返回 true;如果命中被拒绝,则返回 false。 这意味着,任何命中着色器(如果存在)都必须执行,然后才能有条件地返回到交集着色器。

着色器类型属性

[shader("intersection")]

示例

struct CustomPrimitiveDef { ... };
struct MyAttributes { ... };
struct CustomIntersectionIterator {...};
void InitCustomIntersectionIterator(CustomIntersectionIterator it) {...}
bool IntersectCustomPrimitiveFrontToBack(
    CustomPrimitiveDef prim,
    inout CustomIntersectionIterator it,
    float3 origin, float3 dir,
    float rayTMin, inout float curT,
    out MyAttributes attr);

[shader("intersection")]
void intersection_main()
{
    float THit = RayTCurrent();
    MyAttributes attr;
    CustomIntersectionIterator it;
    InitCustomIntersectionIterator(it); 
    while(IntersectCustomPrimitiveFrontToBack(
            CustomPrimitiveDefinitions[LocalConstants.PrimitiveIndex],
            it, ObjectRayOrigin(), ObjectRayDirection(), 
            RayTMin(), THit, attr))
    {
        // Exit on the first hit.  Note that if the ray has
        // RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH or an
        // anyhit shader is used and calls AcceptHitAndEndSearch(),
        // that would also fully exit this intersection shader (making
        // the “break” below moot in that case).        
        if (ReportHit(THit, /*hitKind*/ 0, attr) && (RayFlags() &  RAY_FLAG_FORCE_OPAQUE))
            break;
    }
}