-
Notifications
You must be signed in to change notification settings - Fork 226
/
SimpleRaytracing.hlsl
49 lines (41 loc) · 1.62 KB
/
SimpleRaytracing.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
RaytracingAccelerationStructure Scene : register(t0);
RWTexture2D<float4> RenderTarget : register(u0);
typedef BuiltInTriangleIntersectionAttributes MyAttributes;
struct RayPayload
{
float4 color;
};
[shader("raygeneration")]
void RayGenerationShader()
{
float2 lerpValues = (float2)DispatchRaysIndex() / (float2)DispatchRaysDimensions();
// Orthographic projection since we're raytracing in screen space.
float3 rayDir = float3(0, 0, 1);
float3 origin = float3(lerp(-1.0f, 1.0f, lerpValues.x), lerp(-1.0f, 1.0f, lerpValues.y), 0.0f);
// Trace the ray.
// Set the ray's extents.
RayDesc ray;
ray.Origin = origin;
ray.Direction = rayDir;
// Set TMin to a non-zero small value to avoid aliasing issues due to floating - point errors.
// TMin should be kept small to prevent missing geometry at close contact areas.
ray.TMin = 0.001;
ray.TMax = 10000.0;
RayPayload payload = { float4(0, 0, 0, 0) };
TraceRay(Scene, RAY_FLAG_CULL_BACK_FACING_TRIANGLES, ~0, 0, 1, 0, ray, payload);
// Write the raytraced color to the output texture.
RenderTarget[DispatchRaysIndex().xy] = payload.color;
}
[shader("closesthit")]
void ClosestHitShader(inout RayPayload payload, in MyAttributes attr)
{
float3 barycentrics = float3(1 - attr.barycentrics.x - attr.barycentrics.y, attr.barycentrics.x, attr.barycentrics.y);
payload.color = float4(barycentrics, 1);
}
[shader("miss")]
void MissShader(inout RayPayload payload)
{
payload.color = float4(0, 0, 0, 1);
}