forked from microsoft/Xbox-ATG-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleLighting12.h
114 lines (88 loc) · 4 KB
/
SimpleLighting12.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
//--------------------------------------------------------------------------------------
// SimpleLighting12.h
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#pragma once
#include "DeviceResources.h"
#include "StepTimer.h"
// A basic sample implementation that creates a D3D12 device and
// provides a render loop.
class Sample
{
public:
Sample();
// Initialization and management
void Initialize(IUnknown* window);
// Basic Sample loop
void Tick();
// Messages
void OnSuspending();
void OnResuming();
private:
struct ConstantBuffer
{
DirectX::XMMATRIX worldMatrix;
DirectX::XMMATRIX viewMatrix;
DirectX::XMMATRIX projectionMatrix;
DirectX::XMVECTOR lightDir[2];
DirectX::XMVECTOR lightColor[2];
DirectX::XMVECTOR outputColor;
};
// We'll allocate space for several of these and they will need to be padded for alignment.
static_assert(sizeof(ConstantBuffer) == 272, "Checking the size here.");
// D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT < 272 < 2 * D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT
// Create a union with the correct size and enough room for one ConstantBuffer
union PaddedConstantBuffer
{
ConstantBuffer constants;
uint8_t bytes[2 * D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT];
};
// Check the exact size of the PaddedConstantBuffer to make sure it will align properly
static_assert(sizeof(PaddedConstantBuffer) == 2 * D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT, "PaddedConstantBuffer is not aligned properly");
void Update(DX::StepTimer const& timer);
void Render();
void Clear();
void CreateDeviceDependentResources();
void CreateWindowSizeDependentResources();
// Device resources.
std::unique_ptr<DX::DeviceResources> m_deviceResources;
// Rendering loop timer.
uint64_t m_frame;
DX::StepTimer m_timer;
// Input devices.
std::unique_ptr<DirectX::GamePad> m_gamePad;
DirectX::GamePad::ButtonStateTracker m_gamePadButtons;
// DirectXTK objects.
Microsoft::WRL::ComPtr<ID3D12RootSignature> m_rootSignature;
Microsoft::WRL::ComPtr<ID3D12PipelineState> m_lambertPipelineState;
Microsoft::WRL::ComPtr<ID3D12PipelineState> m_solidColorPipelineState;
Microsoft::WRL::ComPtr<ID3D12Resource> m_vertexBuffer;
Microsoft::WRL::ComPtr<ID3D12Resource> m_indexBuffer;
Microsoft::WRL::ComPtr<ID3D12Resource> m_perFrameConstants;
PaddedConstantBuffer* m_mappedConstantData;
D3D12_GPU_VIRTUAL_ADDRESS m_constantDataGpuAddr;
D3D12_VERTEX_BUFFER_VIEW m_vertexBufferView;
D3D12_INDEX_BUFFER_VIEW m_indexBufferView;
// In this simple sample, we know that there are three draw calls
// and we will update the scene constants for each draw call.
static const unsigned int c_numDrawCalls = 3;
// A synchronization fence and an event. These members will be used
// to synchronize the CPU with the GPU so that there will be no
// contention for the constant buffers.
Microsoft::WRL::ComPtr<ID3D12Fence> m_fence;
Microsoft::WRL::Wrappers::Event m_fenceEvent;
// Index in the root parameter table
static const UINT c_rootParameterCB = 0;
// Scene constants, updated per-frame
float m_curRotationAngleRad;
// These computed values will be loaded into a ConstantBuffer
// during Render
DirectX::XMFLOAT4X4 m_worldMatrix;
DirectX::XMFLOAT4X4 m_viewMatrix;
DirectX::XMFLOAT4X4 m_projectionMatrix;
DirectX::XMFLOAT4 m_lightDirs[2];
DirectX::XMFLOAT4 m_lightColors[2];
DirectX::XMFLOAT4 m_outputColor;
};