forked from microsoft/Xbox-ATG-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleRaytracing.hlsl
62 lines (52 loc) · 2.05 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
50
51
52
53
54
55
56
57
58
59
60
61
62
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
#ifndef SIMPLERAYTRACING_HLSL
#define SIMPLERAYTRACING_HLSL
RaytracingAccelerationStructure Scene : register(t0, space0);
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);
}
#endif // SIMPLERAYTRACING_HLSL