-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRayGen.hlsl
90 lines (73 loc) · 3.25 KB
/
RayGen.hlsl
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
#include "Common.hlsl"
// Raytracing output texture, accessed as a UAV
RWTexture2D< float4 > gOutput : register(u0);
// Raytracing acceleration structure, accessed as a SRV
RaytracingAccelerationStructure SceneBVH : register(t0);
// #DXR Extra: Perspective Camera
cbuffer CameraParams : register(b0)
{
float4x4 view;
float4x4 projection;
float4x4 viewI;
float4x4 projectionI;
}
[shader("raygeneration")]
void RayGen() {
// Initialize the ray payload
HitInfo payload;
payload.colorAndDistance = float4(0.0f, 0.0f, 0.0f, 0.0f);
// Get the location within the dispatched 2D grid of work items
// (often maps to pixels, so this could represent a pixel coordinate).
uint2 launchIndex = DispatchRaysIndex().xy;
float2 dims = float2(DispatchRaysDimensions().xy);
float2 d = (((launchIndex.xy + 0.5f) / dims.xy) * 2.0f - 1.0f);
// #DXR Extra: Perspective Camera
float aspectRatio = dims.x / dims.y;
RayDesc ray;
ray.Origin = mul(viewI, float4(0.0f, 0.0f, 0.0f, 1.0f));
float4 target = mul(projectionI, float4(d.x, -d.y, 1.0f, 1.0f));
ray.Direction = mul(viewI, float4(target.xyz, 0.0f));
ray.TMin = 0.0f;
ray.TMax = MAX_RAY_T;
// Trace the ray
TraceRay(
// Parameter name: AccelerationStructure
// Acceleration structure
SceneBVH,
// Parameter name: RayFlags
// Flags can be used to specify the behavior upon hitting a surface
RAY_FLAG_NONE,
// Parameter name: InstanceInclusionMask
// Instance inclusion mask, which can be used to mask out some geometry to this ray by
// and-ing the mask with a geometry mask. The 0xFF flag then indicates no geometry will be
// masked
0xFF,
// Parameter name: RayContributionToHitGroupIndex
// Depending on the type of ray, a given object can have several hit groups attached
// (ie. what to do when hitting to compute regular shading, and what to do when hitting
// to compute shadows). Those hit groups are specified sequentially in the SBT, so the value
// below indicates which offset (on 4 bits) to apply to the hit groups for this ray. In this
// sample we only have one hit group per object, hence an offset of 0.
0,
// Parameter name: MultiplierForGeometryContributionToHitGroupIndex
// The offsets in the SBT can be computed from the object ID, its instance ID, but also simply
// by the order the objects have been pushed in the acceleration structure. This allows the
// application to group shaders in the SBT in the same order as they are added in the AS, in
// which case the value below represents the stride (4 bits representing the number of hit
// groups) between two consecutive objects.
0,
// Parameter name: MissShaderIndex
// Index of the miss shader to use in case several consecutive miss shaders are present in the
// SBT. This allows to change the behavior of the program when no geometry have been hit, for
// example one to return a sky color for regular rendering, and another returning a full
// visibility value for shadow rays. This sample has only one miss shader, hence an index 0
0,
// Parameter name: Ray
// Ray information to trace
ray,
// Parameter name: Payload
// Payload associated to the ray, which will be used to communicate between the hit/miss
// shaders and the raygen
payload);
gOutput[launchIndex] = float4(payload.colorAndDistance.rgb, 1.f);
}