1 | #include <cstring>
|
---|
2 |
|
---|
3 | #include <d3d9.h>
|
---|
4 | #include <d3dcompiler.h>
|
---|
5 |
|
---|
6 | #include "../test_utils.h"
|
---|
7 | #include "test_d3d9_nv12.yuv.h"
|
---|
8 |
|
---|
9 | using namespace dxvk;
|
---|
10 |
|
---|
11 | struct Extent2D {
|
---|
12 | uint32_t w, h;
|
---|
13 | };
|
---|
14 |
|
---|
15 | const std::string g_vertexShaderCode = R"(
|
---|
16 |
|
---|
17 | struct VS_INPUT {
|
---|
18 | float3 Position : POSITION;
|
---|
19 | };
|
---|
20 |
|
---|
21 | struct VS_OUTPUT {
|
---|
22 | float4 Position : POSITION;
|
---|
23 | float2 Texcoord : TEXCOORD0;
|
---|
24 | };
|
---|
25 |
|
---|
26 | VS_OUTPUT main( VS_INPUT IN ) {
|
---|
27 | VS_OUTPUT OUT;
|
---|
28 | OUT.Position = float4(IN.Position, 0.6f);
|
---|
29 | OUT.Texcoord = IN.Position.xy + float2(0.5, 0.5);
|
---|
30 | OUT.Texcoord.y = 1.0 - OUT.Texcoord.y;
|
---|
31 |
|
---|
32 | return OUT;
|
---|
33 | }
|
---|
34 |
|
---|
35 | )";
|
---|
36 |
|
---|
37 | const std::string g_pixelShaderCode = R"(
|
---|
38 |
|
---|
39 | struct VS_OUTPUT {
|
---|
40 | float4 Position : POSITION;
|
---|
41 | float2 Texcoord : TEXCOORD0;
|
---|
42 | };
|
---|
43 |
|
---|
44 | struct PS_OUTPUT {
|
---|
45 | float4 Colour : COLOR;
|
---|
46 | };
|
---|
47 |
|
---|
48 | sampler g_frogTex : register( s0 );
|
---|
49 |
|
---|
50 | PS_OUTPUT main( VS_OUTPUT IN ) {
|
---|
51 | PS_OUTPUT OUT;
|
---|
52 |
|
---|
53 | OUT.Colour = tex2D(g_frogTex, IN.Texcoord);
|
---|
54 |
|
---|
55 | return OUT;
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | )";
|
---|
60 |
|
---|
61 | Logger Logger::s_instance("triangle.log");
|
---|
62 |
|
---|
63 | class TriangleApp {
|
---|
64 |
|
---|
65 | public:
|
---|
66 |
|
---|
67 | TriangleApp(HINSTANCE instance, HWND window)
|
---|
68 | : m_window(window) {
|
---|
69 | HRESULT status = Direct3DCreate9Ex(D3D_SDK_VERSION, &m_d3d);
|
---|
70 |
|
---|
71 | if (FAILED(status))
|
---|
72 | throw DxvkError("Failed to create D3D9 interface");
|
---|
73 |
|
---|
74 | UINT adapter = D3DADAPTER_DEFAULT;
|
---|
75 |
|
---|
76 | D3DADAPTER_IDENTIFIER9 adapterId;
|
---|
77 | m_d3d->GetAdapterIdentifier(adapter, 0, &adapterId);
|
---|
78 |
|
---|
79 | Logger::info(str::format("Using adapter: ", adapterId.Description));
|
---|
80 |
|
---|
81 | D3DPRESENT_PARAMETERS params;
|
---|
82 | getPresentParams(params);
|
---|
83 |
|
---|
84 | status = m_d3d->CreateDeviceEx(
|
---|
85 | adapter,
|
---|
86 | D3DDEVTYPE_HAL,
|
---|
87 | m_window,
|
---|
88 | D3DCREATE_HARDWARE_VERTEXPROCESSING,
|
---|
89 | ¶ms,
|
---|
90 | nullptr,
|
---|
91 | &m_device);
|
---|
92 |
|
---|
93 | if (FAILED(status))
|
---|
94 | throw DxvkError("Failed to create D3D9 device");
|
---|
95 |
|
---|
96 |
|
---|
97 | // Vertex Shader
|
---|
98 | {
|
---|
99 | Com<ID3DBlob> blob;
|
---|
100 |
|
---|
101 | status = D3DCompile(
|
---|
102 | g_vertexShaderCode.data(),
|
---|
103 | g_vertexShaderCode.length(),
|
---|
104 | nullptr, nullptr, nullptr,
|
---|
105 | "main",
|
---|
106 | "vs_2_0",
|
---|
107 | 0, 0, &blob,
|
---|
108 | nullptr);
|
---|
109 |
|
---|
110 | if (FAILED(status))
|
---|
111 | throw DxvkError("Failed to compile vertex shader");
|
---|
112 |
|
---|
113 | status = m_device->CreateVertexShader(reinterpret_cast<const DWORD*>(blob->GetBufferPointer()), &m_vs);
|
---|
114 |
|
---|
115 | if (FAILED(status))
|
---|
116 | throw DxvkError("Failed to create vertex shader");
|
---|
117 | }
|
---|
118 |
|
---|
119 | // Pixel Shader
|
---|
120 | {
|
---|
121 | Com<ID3DBlob> blob;
|
---|
122 |
|
---|
123 | status = D3DCompile(
|
---|
124 | g_pixelShaderCode.data(),
|
---|
125 | g_pixelShaderCode.length(),
|
---|
126 | nullptr, nullptr, nullptr,
|
---|
127 | "main",
|
---|
128 | "ps_2_0",
|
---|
129 | 0, 0, &blob,
|
---|
130 | nullptr);
|
---|
131 |
|
---|
132 | if (FAILED(status))
|
---|
133 | throw DxvkError("Failed to compile pixel shader");
|
---|
134 |
|
---|
135 | status = m_device->CreatePixelShader(reinterpret_cast<const DWORD*>(blob->GetBufferPointer()), &m_ps);
|
---|
136 |
|
---|
137 | if (FAILED(status))
|
---|
138 | throw DxvkError("Failed to create pixel shader");
|
---|
139 | }
|
---|
140 |
|
---|
141 | m_device->SetVertexShader(m_vs.ptr());
|
---|
142 | m_device->SetPixelShader(m_ps.ptr());
|
---|
143 |
|
---|
144 | m_device->AddRef();
|
---|
145 |
|
---|
146 |
|
---|
147 | std::array<float, 9> vertices = {
|
---|
148 | 0.0f, 0.5f, 0.0f,
|
---|
149 | 0.5f, -0.5f, 0.0f,
|
---|
150 | -0.5f, -0.5f, 0.0f,
|
---|
151 | };
|
---|
152 |
|
---|
153 | const size_t vbSize = vertices.size() * sizeof(float);
|
---|
154 |
|
---|
155 | status = m_device->CreateVertexBuffer(vbSize, 0, 0, D3DPOOL_DEFAULT, &m_vb, nullptr);
|
---|
156 | if (FAILED(status))
|
---|
157 | throw DxvkError("Failed to create vertex buffer");
|
---|
158 |
|
---|
159 | void* data = nullptr;
|
---|
160 | status = m_vb->Lock(0, 0, &data, 0);
|
---|
161 | if (FAILED(status))
|
---|
162 | throw DxvkError("Failed to lock vertex buffer");
|
---|
163 |
|
---|
164 | std::memcpy(data, vertices.data(), vbSize);
|
---|
165 |
|
---|
166 | status = m_vb->Unlock();
|
---|
167 | if (FAILED(status))
|
---|
168 | throw DxvkError("Failed to unlock vertex buffer");
|
---|
169 |
|
---|
170 | m_device->SetStreamSource(0, m_vb.ptr(), 0, 3 * sizeof(float));
|
---|
171 |
|
---|
172 | std::array<D3DVERTEXELEMENT9, 2> elements;
|
---|
173 |
|
---|
174 | elements[0].Method = 0;
|
---|
175 | elements[0].Offset = 0;
|
---|
176 | elements[0].Stream = 0;
|
---|
177 | elements[0].Type = D3DDECLTYPE_FLOAT3;
|
---|
178 | elements[0].Usage = D3DDECLUSAGE_POSITION;
|
---|
179 | elements[0].UsageIndex = 0;
|
---|
180 |
|
---|
181 | elements[1] = D3DDECL_END();
|
---|
182 |
|
---|
183 | HRESULT result = m_device->CreateVertexDeclaration(elements.data(), &m_decl);
|
---|
184 | if (FAILED(result))
|
---|
185 | throw DxvkError("Failed to create vertex decl");
|
---|
186 |
|
---|
187 | m_device->SetVertexDeclaration(m_decl.ptr());
|
---|
188 |
|
---|
189 | const uint32_t imageSize = 320;
|
---|
190 |
|
---|
191 | Com<IDirect3DTexture9> texture;
|
---|
192 | Com<IDirect3DSurface9> texSurf;
|
---|
193 | status = m_device->CreateTexture(imageSize, imageSize, 1, D3DUSAGE_RENDERTARGET, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &texture, nullptr);
|
---|
194 | status = texture->GetSurfaceLevel(0, &texSurf);
|
---|
195 |
|
---|
196 | Com<IDirect3DSurface9> nv12Surf;
|
---|
197 | status = m_device->CreateOffscreenPlainSurface(imageSize, imageSize, (D3DFORMAT)MAKEFOURCC('N', 'V', '1', '2'), D3DPOOL_DEFAULT, &nv12Surf, nullptr);
|
---|
198 | D3DLOCKED_RECT rect;
|
---|
199 | nv12Surf->LockRect(&rect, nullptr, 0);
|
---|
200 | char* dst = (char*)rect.pBits;
|
---|
201 | char* src = (char*)test_d3d9_nv12_yuv;
|
---|
202 | for (uint32_t i = 0; i < imageSize; i++)
|
---|
203 | {
|
---|
204 | std::memcpy(dst, src, imageSize);
|
---|
205 | src += imageSize;
|
---|
206 | dst += rect.Pitch;
|
---|
207 | }
|
---|
208 |
|
---|
209 | for (uint32_t i = 0; i < imageSize / 2; i++)
|
---|
210 | {
|
---|
211 | std::memcpy(dst, src, imageSize);
|
---|
212 | src += imageSize;
|
---|
213 | dst += rect.Pitch;
|
---|
214 | }
|
---|
215 | nv12Surf->UnlockRect();
|
---|
216 | status = m_device->StretchRect(nv12Surf.ptr(), nullptr, texSurf.ptr(), nullptr, D3DTEXF_LINEAR);
|
---|
217 | m_device->SetTexture(0, texture.ptr());
|
---|
218 | }
|
---|
219 |
|
---|
220 | void run() {
|
---|
221 | this->adjustBackBuffer();
|
---|
222 |
|
---|
223 | m_device->BeginScene();
|
---|
224 |
|
---|
225 | m_device->Clear(
|
---|
226 | 0,
|
---|
227 | nullptr,
|
---|
228 | D3DCLEAR_TARGET,
|
---|
229 | D3DCOLOR_RGBA(44, 62, 80, 0),
|
---|
230 | 0,
|
---|
231 | 0);
|
---|
232 |
|
---|
233 | m_device->Clear(
|
---|
234 | 0,
|
---|
235 | nullptr,
|
---|
236 | D3DCLEAR_ZBUFFER,
|
---|
237 | 0,
|
---|
238 | 0.5f,
|
---|
239 | 0);
|
---|
240 |
|
---|
241 | m_device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
|
---|
242 |
|
---|
243 | m_device->EndScene();
|
---|
244 |
|
---|
245 | m_device->PresentEx(
|
---|
246 | nullptr,
|
---|
247 | nullptr,
|
---|
248 | nullptr,
|
---|
249 | nullptr,
|
---|
250 | 0);
|
---|
251 | }
|
---|
252 |
|
---|
253 | void adjustBackBuffer() {
|
---|
254 | RECT windowRect = { 0, 0, 1024, 600 };
|
---|
255 | GetClientRect(m_window, &windowRect);
|
---|
256 |
|
---|
257 | Extent2D newSize = {
|
---|
258 | static_cast<uint32_t>(windowRect.right - windowRect.left),
|
---|
259 | static_cast<uint32_t>(windowRect.bottom - windowRect.top),
|
---|
260 | };
|
---|
261 |
|
---|
262 | if (m_windowSize.w != newSize.w
|
---|
263 | || m_windowSize.h != newSize.h) {
|
---|
264 | m_windowSize = newSize;
|
---|
265 |
|
---|
266 | D3DPRESENT_PARAMETERS params;
|
---|
267 | getPresentParams(params);
|
---|
268 | HRESULT status = m_device->ResetEx(¶ms, nullptr);
|
---|
269 |
|
---|
270 | if (FAILED(status))
|
---|
271 | throw DxvkError("Device reset failed");
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | void getPresentParams(D3DPRESENT_PARAMETERS& params) {
|
---|
276 | params.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
|
---|
277 | params.BackBufferCount = 1;
|
---|
278 | params.BackBufferFormat = D3DFMT_X8R8G8B8;
|
---|
279 | params.BackBufferWidth = m_windowSize.w;
|
---|
280 | params.BackBufferHeight = m_windowSize.h;
|
---|
281 | params.EnableAutoDepthStencil = 0;
|
---|
282 | params.Flags = 0;
|
---|
283 | params.FullScreen_RefreshRateInHz = 0;
|
---|
284 | params.hDeviceWindow = m_window;
|
---|
285 | params.MultiSampleQuality = 0;
|
---|
286 | params.MultiSampleType = D3DMULTISAMPLE_NONE;
|
---|
287 | params.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
|
---|
288 | params.SwapEffect = D3DSWAPEFFECT_DISCARD;
|
---|
289 | params.Windowed = TRUE;
|
---|
290 | }
|
---|
291 |
|
---|
292 | private:
|
---|
293 |
|
---|
294 | HWND m_window;
|
---|
295 | Extent2D m_windowSize = { 1024, 600 };
|
---|
296 |
|
---|
297 | Com<IDirect3D9Ex> m_d3d;
|
---|
298 | Com<IDirect3DDevice9Ex> m_device;
|
---|
299 |
|
---|
300 | Com<IDirect3DVertexShader9> m_vs;
|
---|
301 | Com<IDirect3DPixelShader9> m_ps;
|
---|
302 | Com<IDirect3DVertexBuffer9> m_vb;
|
---|
303 | Com<IDirect3DVertexDeclaration9> m_decl;
|
---|
304 |
|
---|
305 | };
|
---|
306 |
|
---|
307 | LRESULT CALLBACK WindowProc(HWND hWnd,
|
---|
308 | UINT message,
|
---|
309 | WPARAM wParam,
|
---|
310 | LPARAM lParam);
|
---|
311 |
|
---|
312 | int WINAPI WinMain(HINSTANCE hInstance,
|
---|
313 | HINSTANCE hPrevInstance,
|
---|
314 | LPSTR lpCmdLine,
|
---|
315 | int nCmdShow) {
|
---|
316 | HWND hWnd;
|
---|
317 | WNDCLASSEXW wc;
|
---|
318 | ZeroMemory(&wc, sizeof(WNDCLASSEX));
|
---|
319 | wc.cbSize = sizeof(WNDCLASSEX);
|
---|
320 | wc.style = CS_HREDRAW | CS_VREDRAW;
|
---|
321 | wc.lpfnWndProc = WindowProc;
|
---|
322 | wc.hInstance = hInstance;
|
---|
323 | wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
|
---|
324 | wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
|
---|
325 | wc.lpszClassName = L"WindowClass1";
|
---|
326 | RegisterClassExW(&wc);
|
---|
327 |
|
---|
328 | hWnd = CreateWindowExW(0,
|
---|
329 | L"WindowClass1",
|
---|
330 | L"Our First Windowed Program",
|
---|
331 | WS_OVERLAPPEDWINDOW,
|
---|
332 | 300, 300,
|
---|
333 | 640, 480,
|
---|
334 | nullptr,
|
---|
335 | nullptr,
|
---|
336 | hInstance,
|
---|
337 | nullptr);
|
---|
338 | ShowWindow(hWnd, nCmdShow);
|
---|
339 |
|
---|
340 | MSG msg;
|
---|
341 |
|
---|
342 | try {
|
---|
343 | TriangleApp app(hInstance, hWnd);
|
---|
344 |
|
---|
345 | while (true) {
|
---|
346 | if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) {
|
---|
347 | TranslateMessage(&msg);
|
---|
348 | DispatchMessage(&msg);
|
---|
349 |
|
---|
350 | if (msg.message == WM_QUIT)
|
---|
351 | return msg.wParam;
|
---|
352 | } else {
|
---|
353 | app.run();
|
---|
354 | }
|
---|
355 | }
|
---|
356 | } catch (const dxvk::DxvkError& e) {
|
---|
357 | std::cerr << e.message() << std::endl;
|
---|
358 | return msg.wParam;
|
---|
359 | }
|
---|
360 | }
|
---|
361 |
|
---|
362 | LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
|
---|
363 | switch (message) {
|
---|
364 | case WM_CLOSE:
|
---|
365 | PostQuitMessage(0);
|
---|
366 | return 0;
|
---|
367 | }
|
---|
368 |
|
---|
369 | return DefWindowProc(hWnd, message, wParam, lParam);
|
---|
370 | }
|
---|