summaryrefslogtreecommitdiff
path: root/tests/cuda/optix-ser.slang
blob: 54f30070654ead7a8bb194536d281d65a1cfbba2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// optix-ser.slang


//TEST:SIMPLE(filecheck=CHECK): -target cuda -entry rayGenerationMain -stage raygeneration

//TEST_INPUT: set scene = AccelerationStructure
uniform RaytracingAccelerationStructure scene;

//TEST_INPUT:set outputBuffer = out ubuffer(data=[0, 0, 0, 0], stride=4)
RWStructuredBuffer<uint> outputBuffer;

struct SomeValues
{
    int a;
    float b;
};

uint calcValue(HitObject hit)
{
    uint r = 0;
    
    if (hit.IsHit())
    {
        uint instanceIndex = hit.GetInstanceIndex();
        uint instanceID = hit.GetInstanceID();
        uint geometryIndex = hit.GetGeometryIndex();
        uint primitiveIndex = hit.GetPrimitiveIndex();
        int clusterID = hit.GetClusterID();
        uint shaderTableIndex = hit.GetShaderTableIndex();
        // spriv and glsl lack these methods
        uint setShaderTableIndex = hit.SetShaderTableIndex(0);
        uint ialbedo = hit.LoadLocalRootTableConstant(0);
        SomeValues objSomeValues = hit.GetAttributes<SomeValues>();
        
        r += instanceIndex;
        r += instanceID;
        r += geometryIndex;
        r += primitiveIndex;
        r += objSomeValues.a;
        r += clusterID;
        r += shaderTableIndex;
        r += setShaderTableIndex;
        r += ialbedo;
    }
    
    return r;
}

void rayGenerationMain() 
{
    int2 launchID = int2(DispatchRaysIndex().xy);
    int2 launchSize = int2(DispatchRaysDimensions().xy);

    int idx = launchID.x;
    
    SomeValues someValues = { idx, idx * 2.0f };

    RayDesc ray;  
    ray.Origin = float3(idx, 0, 0);
    ray.TMin = 0.01f;
    ray.Direction = float3(0, 1, 0);
    ray.TMax = 1e4f;
    
    RAY_FLAG rayFlags = RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH | RAY_FLAG_CULL_BACK_FACING_TRIANGLES;
    uint instanceInclusionMask = 0xff;
    uint rayContributionToHitGroupIndex = 0;
    uint multiplierForGeometryContributionToHitGroupIndex = 4;
    uint missShaderIndex = 0;
    // SPIRV: OpHitObjectTraceRayNV
    // CHECK: optixTraverse
    HitObject hit = HitObject::TraceRay(scene, 
        rayFlags, 
        instanceInclusionMask, 
        rayContributionToHitGroupIndex, 
        multiplierForGeometryContributionToHitGroupIndex, 
        missShaderIndex, 
        ray, 
        someValues);
    
    ReorderThread( hit );
    ReorderThread(hit, uint(idx & 3), 2);
    ReorderThread(uint(idx & 1), 1);

    outputBuffer[idx] = calcValue(hit);
    HitObject miss[2];
    miss[0] = HitObject::MakeMiss(0u, ray);
    miss[1] = HitObject::MakeMotionMiss(0u, ray, 1.f);

    uint hitGroupRecordIndex = 0;
    uint instanceIndex = 0xff;
    uint geometryIndex = 0;
    uint primitiveIndex = 0;
    uint hitKind = 0;
    BuiltInTriangleIntersectionAttributes attr = {0.01f, 0.2f};

    HitObject hitObj = HitObject::MakeHit(hitGroupRecordIndex, scene,
        instanceIndex,
        geometryIndex,
        primitiveIndex,
        hitKind,
        ray,
        attr);
    HitObject nopObj = HitObject::MakeNop();
    outputBuffer[idx] = uint(nopObj.IsNop());

    outputBuffer[idx] += calcValue(hit);
    outputBuffer[idx] += calcValue(miss[0]);
    outputBuffer[idx] += calcValue(miss[1]);
    outputBuffer[idx] += calcValue(hitObj);
    outputBuffer[idx] += calcValue(nopObj);

    // Change the payload
    SomeValues otherValues = { idx * -2, idx * 8.0f };

    HitObject::Invoke( scene, hit, otherValues );
    HitObject motionHitObj[2];
    motionHitObj[0] = HitObject::MakeMotionHit(
        scene,
        instanceIndex,
        geometryIndex,
        primitiveIndex,
        hitKind,
        rayContributionToHitGroupIndex,
        multiplierForGeometryContributionToHitGroupIndex,
        ray,
        0.f,
        attr);
    motionHitObj[1] = HitObject::MakeMotionHit(
        hitGroupRecordIndex,
        scene,
        instanceIndex,
        geometryIndex,
        primitiveIndex,
        hitKind,
        ray,
        0.f,
        attr);
    outputBuffer[idx] += calcValue(motionHitObj[0]);
    outputBuffer[idx] += calcValue(motionHitObj[1]);

    RayDesc rayD = hit.GetRayDesc();

    outputBuffer[idx] += uint(rayD.TMin > 0);
    outputBuffer[idx] += uint(rayD.TMax < ray.TMin);

}