-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscene.hpp
213 lines (182 loc) · 5.23 KB
/
scene.hpp
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#ifndef _SCENE_HPP
#define _SCENE_HPP
#include <map>
#include <string>
#include <vector>
#include <d3d12.h>
#include <D3Dcompiler.h>
#include <DirectXMath.h>
#include "d3dx12.h"
#include <glm/glm.hpp>
#include <wrl/client.h>
#include <dxgi1_4.h>
#include "chunk.hpp"
#include "chunkindex.hpp"
#include "camera.hpp"
#include "sprite.hpp"
#include "textrender.hpp"
#include "util.hpp"
#include <ft2build.h>
#include FT_FREETYPE_H
class Scene {
public:
virtual void Render() = 0;
virtual void Update(float secs) = 0;
};
class DX12ClearScreenScene : public Scene {
public:
DX12ClearScreenScene();
void Render() override;
void Update(float secs) override;
private:
void InitPipelineAndCommandList();
ID3D12CommandAllocator* command_allocator;
ID3D12GraphicsCommandList* command_list;
ID3DBlob* VS, * PS;
ID3D12RootSignature* root_signature;
ID3D12PipelineState* pipeline_state;
};
class DX12HelloTriangleScene : public Scene {
public:
struct Vertex {
DirectX::XMFLOAT3 position;
DirectX::XMFLOAT4 color;
};
DX12HelloTriangleScene();
void Render() override;
void Update(float secs) override;
static constexpr int FRAME_COUNT = 2;
private:
void InitPipelineAndCommandList();
void InitResources();
ID3D12Resource* vertex_buffer;
ID3D12CommandAllocator* command_allocator;
ID3D12GraphicsCommandList* command_list;
ID3DBlob* VS, *PS;
ID3D12RootSignature* root_signature;
ID3D12PipelineState* pipeline_state;
D3D12_VERTEX_BUFFER_VIEW vertex_buffer_view;
ID3D12DescriptorHeap* cbv_heap;
int cbv_descriptor_size;
ID3D12Resource* cbvs; // CBV resource, N copies of the CBV for N triangles.
};
struct PerSceneCB {
DirectX::XMVECTOR dir_light;
DirectX::XMMATRIX lightPV;
DirectX::XMVECTOR cam_pos;
};
class DX12ChunksScene : public Scene {
public:
DX12ChunksScene();
void Render() override;
void Update(float secs) override;
static constexpr int FRAME_COUNT = 2;
private:
void InitCommandList();
void InitResources();
ID3D12CommandAllocator* command_allocator;
ID3D12GraphicsCommandList* command_list;
Chunk* chunk;
ChunkIndex* chunk_index;
ChunkPass *chunk_pass_depth, *chunk_pass_normal;
ChunkSprite* chunk_sprite;
float total_secs;
// CB's heap, resource, view and descriptor
ID3D12DescriptorHeap* cbv_heap;
PerSceneCB h_per_scene_cb;
ID3D12Resource* d_per_scene_cb;
int cbv_descriptor_size;
Camera* camera;
DirectionalLight* dir_light;
void UpdatePerSceneCB(const DirectX::XMVECTOR* dir_light, const DirectX::XMMATRIX* lightPV, const DirectX::XMVECTOR* camPos);
DirectX::XMMATRIX projection_matrix;
// DSV's heap
ID3D12DescriptorHeap* dsv_heap;
int dsv_descriptor_size;
ID3D12Resource* depth_buffer;
// Rendertarget
ID3D12Resource* gbuffer;
ID3D12DescriptorHeap* rtv_heap; // For GBuffer
// Empty shadow map
ID3D12Resource* shadow_map;
// Backdrop
ID3D12Resource* backdrop_vert_buf;
D3D12_VERTEX_BUFFER_VIEW backdrop_vbv;
};
class DX12TextScene : public Scene {
public:
DX12TextScene();
void Render() override;
void Update(float secs) override;
static constexpr int FRAME_COUNT = 2;
void AddText(const std::wstring& t, glm::vec2 pos);
// Texture atlas 中的Character
struct Character_D3D12 {
ID3D12Resource* texture;
int offset_in_srv_heap; // Offset in the number of descriptors
glm::ivec2 size, bearing;
uint32_t advance;
};
// 要显示的Character
struct CharacterToDisplay {
Character_D3D12* character;
D3D12_VERTEX_BUFFER_VIEW vbv;
};
private:
void InitCommandList();
void InitResources();
void InitFreetype();
void ClearCharactersToDisplay();
Character_D3D12* CreateOrGetChar(wchar_t ch);
ID3D12CommandAllocator* command_allocator;
ID3D12GraphicsCommandList* command_list;
ID3DBlob* VS, * PS;
ID3D12RootSignature* root_signature_text_render;
ID3D12PipelineState* pipeline_state_text_render;
std::vector<CharacterToDisplay> characters_to_display;
std::wstring text_to_display;
glm::vec2 text_pos;
std::vector<ID3D12Resource*> constant_buffers;
ID3D12DescriptorHeap* cbv_heap;
std::map<wchar_t, Character_D3D12> characters_d3d12;
ID3D12Resource* vertex_buffers;
ID3D12DescriptorHeap* srv_heap;
int srv_descriptor_size;
FT_Face face;
// Testing text pass
TextPass* text_pass;
};
class DX12LightScatterScene : public Scene {
public:
struct ConstantBufferDataDrawLight {
float WIN_W, WIN_H;
float light_x, light_y, light_r;
DirectX::XMVECTOR light_color;
float global_alpha;
};
struct VertexUV {
float x, y, z, u, v;
};
DX12LightScatterScene();
void Render() override;
void Update(float secs) override;
ID3D12CommandAllocator* command_allocator;
ID3D12GraphicsCommandList* command_list;
ID3DBlob* ps_drawlight, * vs_drawlight;
ID3D12RootSignature* root_signature;
ID3D12PipelineState* pipeline_state_drawlight;
ID3D12PipelineState* pipeline_state_combine;
ID3D12DescriptorHeap* srv_heap;
int srv_descriptor_size;
ID3D12DescriptorHeap* rtv_heap;
int rtv_descriptor_size;
ID3D12Resource* lightmask;
ID3D12Resource* vb_fsquad;
D3D12_VERTEX_BUFFER_VIEW vbv_fsquad;
ID3D12Resource* cb_drawlight;
ConstantBufferDataDrawLight h_cb_drawlight;
float elapsed_secs;
ID3DBlob* ps_combine, * vs_combine;
ID3D12Resource* main_canvas;
};
#endif