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
|
//TEST:CROSS_COMPILE: -profile glsl_460+GL_EXT_ray_tracing -stage raygeneration -entry main -target spirv-assembly
#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(rgba8);
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.Sample(sampler, inUV).rgb;
float3 N = samplerNormal.Sample(sampler, inUV).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);
}
|