summaryrefslogtreecommitdiffstats
path: root/tests/vkray/raygen.slang
blob: 653435a65d0c2ef737fb34119e97bb88f7bc15be (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
//TEST:SIMPLE(filecheck=CHECK_SPV): -emit-spirv-directly -stage raygeneration -entry main -target spirv-assembly
//TEST:SIMPLE(filecheck=CHECK_HLSL): -stage raygeneration -entry main -target hlsl
//TEST:SIMPLE(filecheck=CHECK_GLSL): -stage raygeneration -entry main -target glsl

#define TRACING_EPSILON 1e-6

Texture2D   samplerPosition;
Texture2D   samplerNormal;
SamplerState sampler;

struct Light {
    float4 position;
    float4 color;
};

struct Uniforms
{
    Light       light;
    float4      viewPos;
    float4x4    view;
    float4x4    model;    
};
ConstantBuffer<Uniforms> ubo;


layout(rgba32f)
RWTexture2D<float4> outputImage;

RaytracingAccelerationStructure as;

struct ShadowRay
{
    float hitDistance;
};

struct ReflectionRay
{
    float color;
};

#define gl_LaunchIDNV DispatchRaysIndex()
#define gl_LaunchSizeNV DispatchRaysDimensions()

void main() 
{
    float2 inUV = float2(
        (float(gl_LaunchIDNV.x) + 0.5f) / float(gl_LaunchSizeNV.x),
        (float(gl_LaunchIDNV.y) + 0.5f) / float(gl_LaunchSizeNV.y)
    );

    float3 P = samplerPosition.SampleLevel(sampler, inUV, 0).rgb;
    float3 N = samplerNormal.SampleLevel(sampler, inUV, 0).rgb * 2.0 - 1.0;

    float3 lightPos = ubo.light.position.xyz;
    float3 lightDelta = lightPos - P;
    float lightDist = length(lightDelta);
    float3 L = normalize(lightDelta);
    float atten = 1.0f / (lightDist*lightDist);

    RayDesc ray;
    ray.Origin = P;
    ray.TMin = TRACING_EPSILON;
    ray.Direction = lightDelta;
    ray.TMax = lightDist;

    {
        ShadowRay shadowRay;
        shadowRay.hitDistance = 0;

        TraceRay(as,
                // ray flags
                1,
                // cull mask
                0xff,
                // sbt record offset
                0,
                // sbt record stride
                0,
                // missIndex
                2,
                // ray
                ray,
                // payload
                shadowRay);
        
        if (shadowRay.hitDistance < lightDist)
        {
            atten = 0.f;
        }
    }

    float3 color = ubo.light.color.xyz * saturate(dot(N,L)) * atten;


    {
        ReflectionRay reflectionRay;
        TraceRay(as,
                // ray flags
                1,
                // cull mask
                0xff,
                // sbt record offset
                0,
                // sbt record stride
                0,
                // missIndex
                2,
                // ray
                ray,
                // payload
                reflectionRay);


        color = color + reflectionRay.color;
    }

    outputImage[int2(gl_LaunchIDNV.xy)] = float4(color, 1.0);
}

// CHECK_SPV: OpCapability RayTracingKHR
// CHECK_SPV-NOT: OpCapability RayQueryKHR
// CHECK_SPV: OpExtension "SPV_KHR_ray_tracing"
// CHECK_SPV-NOT: OpExtension "SPV_KHR_ray_query"
// CHECK_SPV: %{{.*}} = OpVariable %_ptr_RayPayload{{NV|KHR}}_ReflectionRay{{.*}} RayPayload
// CHECK_SPV: OpTraceRayKHR
// CHECK_SPV: OpTraceRayKHR

// CHECK_HLSL: TraceRay
// CHECK_HLSL: TraceRay

// CHECK_GLSL: traceRayEXT(
// CHECK_GLSL: traceRayEXT(