-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetaBallRaytracing.h
154 lines (134 loc) · 6.43 KB
/
MetaBallRaytracing.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
//*********************************************************
//
// 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.
//
//*********************************************************
#pragma once
#include "DXSample.h"
#include "StepTimer.h"
#include "RaytracingSceneDefines.h"
#include "DirectXRaytracingHelper.h"
#include "PerformanceTimers.h"
class MetaBallRaytracing : public DXSample
{
public:
MetaBallRaytracing(UINT width, UINT height, std::wstring name);
// IDeviceNotify
virtual void OnDeviceLost() override;
virtual void OnDeviceRestored() override;
// Messages
virtual void OnInit();
virtual void OnKeyDown(UINT8 key);
virtual void OnUpdate();
virtual void OnRender();
virtual void OnSizeChanged(UINT width, UINT height, bool minimized);
virtual void OnDestroy();
virtual IDXGISwapChain* GetSwapchain() { return m_deviceResources->GetSwapChain(); }
private:
static const UINT FrameCount = 3;
// Constants.
const UINT NUM_BLAS = 3; // Triangle + AABB bottom-level AS + MetaBall BLAS.
const float c_aabbWidth = 2; // AABB width.
const float c_aabbDistance = 2; // Distance between AABBs.
// DirectX Raytracing (DXR) attributes
ComPtr<ID3D12Device5> m_dxrDevice;
ComPtr<ID3D12GraphicsCommandList5> m_dxrCommandList;
ComPtr<ID3D12StateObject> m_dxrStateObject;
// Root signatures
ComPtr<ID3D12RootSignature> m_raytracingGlobalRootSignature;
ComPtr<ID3D12RootSignature> m_raytracingLocalRootSignature[LocalRootSignature::Type::Count];
// Descriptors
ComPtr<ID3D12DescriptorHeap> m_descriptorHeap;
UINT m_descriptorsAllocated;
UINT m_descriptorSize;
// Raytracing scene
ConstantBuffer<SceneConstantBuffer> m_sceneCB;
StructuredBuffer<PrimitiveInstancePerFrameBuffer> m_aabbPrimitiveAttributeBuffer;
StructuredBuffer<MetaBall> m_metaBallBuffer;
std::vector<D3D12_RAYTRACING_AABB> m_aabbs;
std::vector<D3D12_RAYTRACING_AABB> m_metaBall_aabbs;
// Root constants
PrimitiveConstantBuffer m_planeMaterialCB;
PrimitiveConstantBuffer m_metaBallMeterialCB;
PrimitiveConstantBuffer m_aabbMaterialCB[IntersectionShaderType::TotalPrimitiveCount];
// Geometry
D3DBuffer m_indexBuffer;
D3DBuffer m_vertexBuffer;
D3DBuffer m_aabbBuffer;
D3DBuffer m_metaBall_aabbBuffer;
std::vector<MetaBall> m_metaBalls;
// Acceleration structure
ComPtr<ID3D12Resource> m_bottomLevelAS[BottomLevelASType::Count];
ComPtr<ID3D12Resource> m_topLevelAS;
// Raytracing output
ComPtr<ID3D12Resource> m_raytracingOutput;
D3D12_GPU_DESCRIPTOR_HANDLE m_raytracingOutputResourceUAVGpuDescriptor;
UINT m_raytracingOutputResourceUAVDescriptorHeapIndex;
// Shader tables
static const wchar_t* c_hitGroupNames_TriangleGeometry[RayType::Count];
static const wchar_t* c_hitGroupNames_AABBGeometry[IntersectionShaderType::Count][RayType::Count];
static const wchar_t* c_hitGroupNames_MetaBallGeometry[RayType::Count];
static const wchar_t* c_raygenShaderName;
static const wchar_t* c_intersectionShaderNames[IntersectionShaderType::Count];
static const wchar_t* c_intersectionShaderMetaballName;
static const wchar_t* c_anyhitShaderMetaBallName;
static const wchar_t* c_closestHitShaderNames[GeometryType::Count];
static const wchar_t* c_missShaderNames[RayType::Count];
ComPtr<ID3D12Resource> m_missShaderTable;
UINT m_missShaderTableStrideInBytes;
ComPtr<ID3D12Resource> m_hitGroupShaderTable;
UINT m_hitGroupShaderTableStrideInBytes;
ComPtr<ID3D12Resource> m_rayGenShaderTable;
// Application state
DX::GPUTimer m_gpuTimers[GpuTimers::Count];
StepTimer m_timer;
float m_animateGeometryTime;
bool m_animateGeometry;
bool m_animateCamera;
bool m_animateLight;
XMVECTOR m_eye;
XMVECTOR m_at;
XMVECTOR m_up;
void UpdateCameraMatrices();
void UpdateAABBPrimitiveAttributes(float animationTime);
void InitializeScene();
void RecreateD3D();
void DoRaytracing();
void CreateConstantBuffers();
void CreateAABBPrimitiveAttributesBuffers();
void CreateDeviceDependentResources();
void CreateWindowSizeDependentResources();
void ReleaseDeviceDependentResources();
void ReleaseWindowSizeDependentResources();
void CreateRaytracingInterfaces();
void SerializeAndCreateRaytracingRootSignature(D3D12_ROOT_SIGNATURE_DESC& desc, ComPtr<ID3D12RootSignature>* rootSig);
void CreateRootSignatures();
void CreateDxilLibrarySubobject(CD3DX12_STATE_OBJECT_DESC* raytracingPipeline);
void CreateHitGroupSubobjects(CD3DX12_STATE_OBJECT_DESC* raytracingPipeline);
void CreateLocalRootSignatureSubobjects(CD3DX12_STATE_OBJECT_DESC* raytracingPipeline);
void CreateRaytracingPipelineStateObject();
void CreateAuxilaryDeviceResources();
void CreateDescriptorHeap();
void CreateRaytracingOutputResource();
void BuildProceduralGeometryAABBs();
void BuildMetaBalls();
void BuildGeometry();
void BuildPlaneGeometry();
void BuildGeometryDescsForBottomLevelAS(std::array<std::vector<D3D12_RAYTRACING_GEOMETRY_DESC>, BottomLevelASType::Count>& geometryDescs);
template <class InstanceDescType, class BLASPtrType>
void BuildBotomLevelASInstanceDescs(BLASPtrType *bottomLevelASaddresses, ComPtr<ID3D12Resource>* instanceDescsResource);
AccelerationStructureBuffers BuildBottomLevelAS(const std::vector<D3D12_RAYTRACING_GEOMETRY_DESC>& geometryDesc, D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAGS buildFlags = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PREFER_FAST_TRACE);
AccelerationStructureBuffers BuildTopLevelAS(AccelerationStructureBuffers bottomLevelAS[BottomLevelASType::Count], D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAGS buildFlags = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PREFER_FAST_TRACE);
void BuildAccelerationStructures();
void BuildShaderTables();
void UpdateForSizeChange(UINT clientWidth, UINT clientHeight);
void CopyRaytracingOutputToBackbuffer();
void CalculateFrameStats();
UINT AllocateDescriptor(D3D12_CPU_DESCRIPTOR_HANDLE* cpuDescriptor, UINT descriptorIndexToUse = UINT_MAX);
UINT CreateBufferSRV(D3DBuffer* buffer, UINT numElements, UINT elementSize);
};