-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
250 lines (221 loc) · 7.71 KB
/
main.cpp
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Local headers
#include "D3DContext.h"
#include "DCompContext.h"
#include "GraphicContents.h"
// OS headers
#include <Windows.h>
// C++ stl
#include <memory>
#include <mutex>
#if defined(USE_DX12)
class TriangleGraphicContents : public GraphicContents {
private:
int width = 0, height = 0;
public:
void updateLayout(int width, int height) override {
this->width = width; this->height = height;
// Uncomment this fake resizing load here to see how the app handles it
// 100ms is a huge time pretty enough to recalculate even a very complicated layout
//
// Sleep(100);
}
std::vector<RGBAVertex> getVertices() override {
float aspect = (float) width / (float) height;
float k = 760.f / (float) width;
float sin60 = sqrtf(3.f) / 2;
float d = 0.3f;
std::vector<RGBAVertex> vertices = {
{0.0f * k, 0.5f * sin60 * aspect * k, 0.0f, 0.5f, 0.0f, 0.5f},
{0.5f * k, -0.5f * sin60 * aspect * k, 0.0f, 0.5f + d, 1.0f, 0.5f},
{-0.5f * k, -0.5f * sin60 * aspect * k, 0.0f, 0.5f - d, 1.0f, 0.5f}
};
return vertices;
}
// std::string getShader() override {
// return {
// "Texture2D txDiffuse : register( t0 );\n"
// "SamplerState samLinear : register( s0 );\n"
// "\n"
// "struct VSInput {\n"
// " float4 position : POSITION;\n"
// " float2 Tex : TEXCOORD0;\n"
// "};\n"
// "struct PSInput {\n"
// " float4 position : SV_POSITION;\n"
// " float2 Tex : TEXCOORD0;\n"
// "};\n"
// "PSInput VSMain(VSInput input) {\n"
// " PSInput output;\n"
// " output.position = input.position;\n"
// " output.Tex = input.Tex;\n"
// " return output;\n"
// "}\n"
// "float4 PSMain(PSInput input) : SV_TARGET {\n"
// " return txDiffuse.Sample( samLinear, input.Tex );\n"
// "}\n"
// };
// }
std::string getShader() override {
return {
"struct PSInput {\n"
" float4 position : SV_POSITION;\n"
" float4 color : COLOR;\n"
"};\n"
"PSInput VSMain(float4 position : POSITION0, float4 color : COLOR0) {\n"
" PSInput result;\n"
" result.position = position;\n"
" result.color = color;\n"
" return result;\n"
"}\n"
"float4 PSMain(PSInput input) : SV_TARGET {\n"
" return input.color;\n"
"}\n"
};
}
};
#elif defined(USE_DX11)
class FullScreenImageGraphicContents : public GraphicContents {
private:
int width = 0, height = 0;
public:
void updateLayout(int width, int height) override {
this->width = width; this->height = height;
// Uncomment this fake resizing load here to see how the app handles it
// 100ms is a huge time pretty enough to recalculate even a very complicated layout
//
//Sleep(100);
}
std::vector<TextureVertex> getVertices() override {
float aspect = (float) width / (float) height;
float k = 1;//760.f / (float) width;
//float sin60 = sqrtf(3.f) / 2;
std::vector<TextureVertex> vertices = {
{-1.0f * k, 1.0f * k, 0.0f, 0.0f, 0.0f},
{ 1.0f * k, 1.0f * k, 0.0f, 1.0f, 0.0f},
{ 1.0f * k, -1.0f * k, 0.0f, 1.0f, 1.0f},
{-1.0f * k, -1.0f * k, 0.0f, 0.0f, 1.0f},
{ 1.0f * k, -1.0f * k, 0.0f, 1.0f, 1.0f},
{-1.0f * k, 1.0f * k, 0.0f, 0.0f, 0.0f},
};
return vertices;
}
std::string getShader() override {
return {
"Texture2D txDiffuse : register( t0 );\n"
"SamplerState samLinear : register( s0 );\n"
"\n"
"struct VSInput {\n"
" float4 position : POSITION;\n"
" float2 Tex : TEXCOORD0;\n"
"};\n"
"struct PSInput {\n"
" float4 position : SV_POSITION;\n"
" float2 Tex : TEXCOORD0;\n"
"};\n"
"PSInput VSMain(VSInput input) {\n"
" PSInput output;\n"
" output.position = input.position;\n"
" output.Tex = input.Tex;\n"
" return output;\n"
"}\n"
"float4 PSMain(PSInput input) : SV_TARGET {\n"
" return txDiffuse.Sample( samLinear, input.Tex );\n"
"}\n"
};
}
};
#else
#error "You should set either USE_DX11 or USE_DX12"
#endif
// Global declarations
std::shared_ptr<D3DContext> context;
std::shared_ptr<DCompContext> dcompContext;
std::shared_ptr<GraphicContents> contents;
bool exitPending;
// Passthrough (t) if truthy. Crash otherwise.
template<class T> T win32_check(T t)
{
if (t) return t;
// Debuggers are better at displaying HRESULTs than the raw DWORD returned by GetLastError().
HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
while (true) __debugbreak();
}
// Win32 message handler.
LRESULT window_proc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
std::mutex draw_mutex;
switch (message)
{
case WM_DESTROY: {
// Destroy the DirectComposition context properly,
// so that the window fades away beautifully.
dcompContext->unbind();
}
case WM_CLOSE: {
exitPending = true;
return 0;
}
case WM_NCCALCSIZE: {
// Use the result of DefWindowProc's WM_NCCALCSIZE handler to get the upcoming client rect.
// Technically, when wparam is TRUE, lparam points to NCCALCSIZE_PARAMS, but its first
// member is a RECT with the same meaning as the one lparam points to when wparam is FALSE.
DefWindowProc(hwnd, message, wparam, lparam);
if (RECT *rect = (RECT *) lparam; rect->right > rect->left && rect->bottom > rect->top) {
contents->updateLayout(rect->right - rect->left, rect->bottom - rect->top);
context->reposition(*rect);
}
// We're never preserving the client area, so we always return 0.
return 0;
}
default:
return DefWindowProc(hwnd, message, wparam, lparam);
}
}
// The app entry point.
int WinMain(HINSTANCE hinstance, HINSTANCE, LPSTR, int)
{
#if defined(USE_DX12)
contents = std::make_shared<TriangleGraphicContents>();
#elif defined(USE_DX11)
contents = std::make_shared<FullScreenImageGraphicContents>();
#else
#error "You should set either USE_DX11 or USE_DX12"
#endif
context = std::make_shared<D3DContext>(contents);
// Register the window class.
WNDCLASS wc = {};
wc.lpfnWndProc = window_proc;
wc.hInstance = hinstance;
wc.hCursor = win32_check(LoadCursor(nullptr, IDC_ARROW));
wc.lpszClassName = TEXT("D3DWindow");
wc.cbClsExtra = sizeof(void*); // Extra pointter
win32_check(RegisterClass(&wc));
std::wstring windowTitle = L"A Never Flickering DirectX Window";
#if defined(USE_DX11)
windowTitle += L" [Direct3D 11]";
#elif defined(USE_DX12)
windowTitle += L" [Direct3D 12]";
#else
#error "Either USE_DX11 or USE_DX12 should be chosen"
#endif
// Create the window. We can use WS_EX_NOREDIRECTIONBITMAP
// since all our presentation is happening through DirectComposition.
HWND hwnd = win32_check(CreateWindowEx(
WS_EX_NOREDIRECTIONBITMAP, wc.lpszClassName, windowTitle.c_str(),
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, hinstance, nullptr));
// The DCompContext creation/destruction is fundamentally asymmetric.
// We are cleaning up the resources in WM_DESTROY, but should NOT create the object in WM_CREATE.
// Instead, we create it here between construction of the window and showing it
dcompContext = std::make_shared<DCompContext>(hwnd, context);
// Show the window and enter the message loop.
ShowWindow(hwnd, SW_SHOWNORMAL);
exitPending = false;
while (!exitPending)
{
MSG msg;
win32_check(GetMessage(&msg, nullptr, 0, 0) > 0);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}