-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathRaytracing.hlsl
83 lines (71 loc) · 2.7 KB
/
Raytracing.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
//*********************************************************
//
// 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 RAYTRACING_HLSL
#define RAYTRACING_HLSL
#include "RaytracingHlslCompat.h"
RaytracingAccelerationStructure Scene : register(t0, space0);
RWTexture2D<float4> RenderTarget : register(u0);
ConstantBuffer<RayGenConstantBuffer> g_rayGenCB : register(b0);
typedef BuiltInTriangleIntersectionAttributes MyAttributes;
struct RayPayload
{
float4 color;
};
bool IsInsideViewport(float2 p, Viewport viewport)
{
return (p.x >= viewport.left && p.x <= viewport.right)
&& (p.y >= viewport.top && p.y <= viewport.bottom);
}
[shader("raygeneration")]
void MyRaygenShader()
{
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(g_rayGenCB.viewport.left, g_rayGenCB.viewport.right, lerpValues.x),
lerp(g_rayGenCB.viewport.top, g_rayGenCB.viewport.bottom, lerpValues.y),
0.0f);
if (IsInsideViewport(origin.xy, g_rayGenCB.stencil))
{
// 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;
}
else
{
// Render interpolated DispatchRaysIndex outside the stencil window
RenderTarget[DispatchRaysIndex().xy] = float4(lerpValues, 0, 1);
}
}
[shader("closesthit")]
void MyClosestHitShader(inout RayPayload payload, in MyAttributes attr)
{
//float3 barycentrics = float3(1 - attr.barycentrics.x - attr.barycentrics.y, attr.barycentrics.x, attr.barycentrics.y);
float3 barycentrics = float3(1, 0, 1);
payload.color = float4(barycentrics, 1);
}
[shader("miss")]
void MyMissShader(inout RayPayload payload)
{
payload.color = float4(0, 0, 0, 1);
}
#endif // RAYTRACING_HLSL