summaryrefslogtreecommitdiffstats
path: root/tests/cuda/optix-ser.slang
blob: d28db60ef932b85106bc3a5ed95fe92281c8d8d4 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// optix-ser.slang


//TEST:SIMPLE(filecheck=CHECK): -target cuda -entry rayGenerationMain -stage raygeneration
//TEST:SIMPLE(filecheck=CHECK-PTX): -target ptx -Xnvrtc -I"./external/optix-dev/include/" -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;

    // CHECK: slangOptixHitObjectIsHit
    if (hit.IsHit())
    {
        // CHECK: slangOptixHitObjectGetInstanceIndex
        uint instanceIndex = hit.GetInstanceIndex();
        // CHECK: slangOptixHitObjectGetInstanceId
        uint instanceID = hit.GetInstanceID();
        // CHECK: slangOptixHitObjectGetSbtGASIndex
        uint geometryIndex = hit.GetGeometryIndex();

        // CHECK: slangOptixHitObjectGetPrimitiveIndex
        uint primitiveIndex = hit.GetPrimitiveIndex();
        // CHECK: slangOptixHitObjectGetClusterId
        int clusterID = hit.GetClusterID();
        // CHECK: slangOptixHitObjectGetSbtRecordIndex
        uint shaderTableIndex = hit.GetShaderTableIndex();
        // spriv and glsl lack these methods
        // CHECK: slangOptixHitObjectSetSbtRecordIndex({{.*}}0U)
        uint setShaderTableIndex = hit.SetShaderTableIndex(0);

        // CHECK: optixHitObjectGetSbtDataPointer()+(0U)
        uint ialbedo = hit.LoadLocalRootTableConstant(0);

        // CHECK: optixHitObjectGetAttribute
        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()
{
    // CHECK: optixGetLaunchIndex
    int2 launchID = int2(DispatchRaysIndex().xy);
    // CHECK: optixGetLaunchDimensions
    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;
    // CHECK: optixTraverse
    // CHECK-PTX:_optix_hitobject_traverse
    HitObject hit = HitObject::TraceRay(
        scene,
        rayFlags,
        instanceInclusionMask,
        rayContributionToHitGroupIndex,
        multiplierForGeometryContributionToHitGroupIndex,
        missShaderIndex,
        ray,
        someValues);

    // CHECK-DAG: optixReorder();
    // CHECK-DAG: optixReorder((uint(idx_0 & int(3))), (2U));
    // CHECK-DAG: optixReorder(uint(idx_0 & int(1)), 1U);
    ReorderThread(hit);
    ReorderThread(hit, uint(idx & 3), 2);
    ReorderThread(uint(idx & 1), 1);

    outputBuffer[idx] = calcValue(hit);
    HitObject miss[2];
    // CHECK: optixMakeMissHitObject(0U, ray_0, &miss_0[int(0)]);
    miss[0] = HitObject::MakeMiss(0u, ray);
    // CHECK: optixMakeMissHitObject(0U, ray_0, 1.0f, &miss_0[int(1)]);
    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 };

    // CHECK: optixMakeHitObject
    HitObject hitObj = HitObject::MakeHit(
        hitGroupRecordIndex,
        scene,
        instanceIndex,
        geometryIndex,
        primitiveIndex,
        hitKind,
        ray,
        attr);
    // CHECK: slangOptixMakeNopHitObject
    HitObject nopObj = HitObject::MakeNop();
    // CHECK: slangOptixHitObjectIsNop
    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 };

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

    // CHECK: optixHitObjectGetRayDesc
    RayDesc rayD = hit.GetRayDesc();

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