-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathraygen.rgen
64 lines (51 loc) · 1.64 KB
/
raygen.rgen
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
// Copyright 2020 Google LLC
RaytracingAccelerationStructure rs : register(t0);
RWTexture2D<float4> image : register(u1);
struct CameraProperties
{
float4x4 viewInverse;
float4x4 projInverse;
float4 lightPos;
};
cbuffer cam : register(b2) { CameraProperties cam; };
struct RayPayload {
float3 color;
float distance;
float3 normal;
float reflector;
};
// Max. number of recursion is passed via a specialization constant
[[vk::constant_id(0)]] const int MAX_RECURSION = 0;
[shader("raygeneration")]
void main()
{
uint3 LaunchID = DispatchRaysIndex();
uint3 LaunchSize = DispatchRaysDimensions();
const float2 pixelCenter = float2(LaunchID.xy) + float2(0.5, 0.5);
const float2 inUV = pixelCenter/float2(LaunchSize.xy);
float2 d = inUV * 2.0 - 1.0;
float4 target = mul(cam.projInverse, float4(d.x, d.y, 1, 1));
RayDesc rayDesc;
rayDesc.Origin = mul(cam.viewInverse, float4(0,0,0,1)).xyz;
rayDesc.Direction = mul(cam.viewInverse, float4(normalize(target.xyz), 0)).xyz;
rayDesc.TMin = 0.001;
rayDesc.TMax = 10000.0;
float3 color = float3(0.0, 0.0, 0.0);
for (int i = 0; i < MAX_RECURSION; i++) {
RayPayload rayPayload;
TraceRay(rs, RAY_FLAG_FORCE_OPAQUE, 0xff, 0, 0, 0, rayDesc, rayPayload);
float3 hitColor = rayPayload.color;
if (rayPayload.distance < 0.0f) {
color += hitColor;
break;
} else if (rayPayload.reflector == 1.0f) {
const float3 hitPos = rayDesc.Origin + rayDesc.Direction * rayPayload.distance;
rayDesc.Origin = hitPos + rayPayload.normal * 0.001f;
rayDesc.Direction = reflect(rayDesc.Direction, rayPayload.normal);
} else {
color += hitColor;
break;
}
}
image[int2(LaunchID.xy)] = float4(color, 0.0);
}