forked from microsoft/Xbox-ATG-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleRaytracingTriangle_PC12.h
159 lines (124 loc) · 5.24 KB
/
SimpleRaytracingTriangle_PC12.h
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//--------------------------------------------------------------------------------------
// D3D12RaytracingAO.h
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#pragma once
#include "DeviceResources.h"
#include "GeneralHelper.h"
namespace GlobalRootSignatureParams {
enum Value
{
OutputViewSlot = 0,
AccelerationStructureSlot,
Count
};
}
//Should not be needed...
namespace LocalRootSignatureParams {
enum Value
{
ViewportConstantSlot = 0,
Count
};
}
// A basic sample implementation that creates a D3D12 device and provides a render loop.
class Sample : public DX::IDeviceNotify
{
public:
Sample() noexcept(false);
~Sample();
// Initialization and management
void Initialize(HWND window, int width, int height);
// Basic render loop
void Tick();
// IDeviceNotify
virtual void OnDeviceLost() override;
virtual void OnDeviceRestored() override;
// Messages
void OnActivated();
void OnDeactivated();
void OnSuspending();
void OnResuming();
void OnWindowMoved();
void OnWindowSizeChanged(int width, int height);
// Properties
void GetDefaultSize(int& width, int& height) const;
private:
// Initialization and management
void CreateDeviceDependentResources();
void CreateWindowSizeDependentResources();
// Basic loop
void Update();
void Render();
void Clear();
// Device resources
std::shared_ptr<DX::DeviceResources> m_deviceResources;
// Input devices
std::unique_ptr<DirectX::GamePad> m_gamePad;
std::unique_ptr<DirectX::Keyboard> m_keyboard;
std::unique_ptr<DirectX::Mouse> m_mouse;
DirectX::GamePad::ButtonStateTracker m_gamePadButtons;
DirectX::Keyboard::KeyboardStateTracker m_keyboardButtons;
/////////////////////////////////////////////////////////////////////////////////
// START SAMPLE SPECIFIC
void BuildSceneGeometry();
// Geometry
typedef UINT16 Index;
struct Vertex { float v1, v2, v3; };
Index indices[3] = { 0, 1, 2 };
const float depthValue = 1.0;
const float offset = 0.5f;
// The sample raytraces in screen space coordinates.
// Since DirectX screen space coordinates are right handed (i.e. Y axis points down).
// Define the vertices in counter clockwise order ~ clockwise in left handed.
Vertex vertices[3] = {{ 0, -offset, depthValue }, { -offset, offset, depthValue }, { offset, offset, depthValue }};
Microsoft::WRL::ComPtr<ID3D12Resource> m_indexBuffer;
Microsoft::WRL::ComPtr<ID3D12Resource> m_vertexBuffer;
// END SAMPLE SPECIFIC
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// START RAYTRACING SPECIFIC
//Init
bool CheckRaytracingSupported(IDXGIAdapter1* adapter);
void CreateRaytracingRootSignatures();
void SerializeAndCreateRaytracingRootSignature(D3D12_ROOT_SIGNATURE_DESC& desc, Microsoft::WRL::ComPtr<ID3D12RootSignature>* rootSig);
void CreateRaytracingPipelineStateObject();
void CreateRaytracingLocalRootSignatureSubobjects(CD3D12_STATE_OBJECT_DESC* raytracingPipeline);
void CreateRaytracingDescriptorHeap();
void BuildRaytracingAccelerationStructures();
void BuildRaytracingShaderTables();
void CreateRaytracingOutputResource();
UINT AllocateRaytracingDescriptor(D3D12_CPU_DESCRIPTOR_HANDLE* cpuDescriptor, UINT descriptorIndexToUse = UINT_MAX);
WRAPPED_GPU_POINTER CreateFallbackWrappedPointer(ID3D12Resource* resource, UINT bufferNumElements);
//Runtime
void DoRaytracing();
void CopyRaytracingOutputToBackbuffer();
// Root signatures
Microsoft::WRL::ComPtr<ID3D12RootSignature> m_raytracingGlobalRootSignature;
Microsoft::WRL::ComPtr<ID3D12RootSignature> m_raytracingLocalRootSignature;
// Descriptors
Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> m_raytracingDescriptorHeap;
UINT m_raytracingDescriptorsAllocated;
UINT m_raytracingDescriptorSize;
// Acceleration structure
Microsoft::WRL::ComPtr<ID3D12Resource> m_accelerationStructure;
Microsoft::WRL::ComPtr<ID3D12Resource> m_bottomLevelAccelerationStructure;
Microsoft::WRL::ComPtr<ID3D12Resource> m_topLevelAccelerationStructure;
WRAPPED_GPU_POINTER m_fallbackTopLevelAccelerationStructurePointer;
// Raytracing output
Microsoft::WRL::ComPtr<ID3D12Resource> m_raytracingOutput;
D3D12_GPU_DESCRIPTOR_HANDLE m_raytracingOutputResourceUAVGpuDescriptor;
UINT m_raytracingOutputResourceUAVDescriptorHeapIndex;
// Shader tables
static const wchar_t* c_hitGroupName;
static const wchar_t* c_raygenShaderName;
static const wchar_t* c_closestHitShaderName;
static const wchar_t* c_missShaderName;
Microsoft::WRL::ComPtr<ID3D12Resource> m_missShaderTable;
Microsoft::WRL::ComPtr<ID3D12Resource> m_hitGroupShaderTable;
Microsoft::WRL::ComPtr<ID3D12Resource> m_rayGenShaderTable;
// END RAYTRACING SPECIFIC
/////////////////////////////////////////////////////////////////////////////////
};