VirtualBox

source: vbox/trunk/src/libs/dxvk-native-1.9.2a/tests/d3d9/test_d3d9_l6v5u5.cpp@ 103698

最後變更 在這個檔案從103698是 96497,由 vboxsync 提交於 3 年 前

libs/dxvk-native-1.9.2a: export to OSE

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette