1 | #include <SDL2/SDL.h>
|
---|
2 | #include <SDL2/SDL_vulkan.h>
|
---|
3 |
|
---|
4 | #include <cstring>
|
---|
5 |
|
---|
6 | #include <d3d9.h>
|
---|
7 |
|
---|
8 | #include <wsi/native_wsi.h>
|
---|
9 |
|
---|
10 | #include "../test_utils.h"
|
---|
11 |
|
---|
12 | using namespace dxvk;
|
---|
13 |
|
---|
14 | /*
|
---|
15 | struct VS_INPUT {
|
---|
16 | float3 Position : POSITION;
|
---|
17 | };
|
---|
18 |
|
---|
19 | struct VS_OUTPUT {
|
---|
20 | float4 Position : POSITION;
|
---|
21 | };
|
---|
22 |
|
---|
23 | VS_OUTPUT main( VS_INPUT IN ) {
|
---|
24 | VS_OUTPUT OUT;
|
---|
25 | OUT.Position = float4(IN.Position, 0.6f);
|
---|
26 |
|
---|
27 | return OUT;
|
---|
28 | }
|
---|
29 | */
|
---|
30 |
|
---|
31 | const std::array<uint8_t, 148> g_vertexShaderCode = {{
|
---|
32 | 0x00, 0x02, 0xfe, 0xff, 0xfe, 0xff, 0x14, 0x00, 0x43, 0x54, 0x41, 0x42,
|
---|
33 | 0x1c, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x00, 0x02, 0xfe, 0xff,
|
---|
34 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
|
---|
35 | 0x1c, 0x00, 0x00, 0x00, 0x76, 0x73, 0x5f, 0x32, 0x5f, 0x30, 0x00, 0x4d,
|
---|
36 | 0x69, 0x63, 0x72, 0x6f, 0x73, 0x6f, 0x66, 0x74, 0x20, 0x28, 0x52, 0x29,
|
---|
37 | 0x20, 0x48, 0x4c, 0x53, 0x4c, 0x20, 0x53, 0x68, 0x61, 0x64, 0x65, 0x72,
|
---|
38 | 0x20, 0x43, 0x6f, 0x6d, 0x70, 0x69, 0x6c, 0x65, 0x72, 0x20, 0x31, 0x30,
|
---|
39 | 0x2e, 0x31, 0x00, 0xab, 0x51, 0x00, 0x00, 0x05, 0x00, 0x00, 0x0f, 0xa0,
|
---|
40 | 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x9a, 0x99, 0x19, 0x3f,
|
---|
41 | 0x00, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x80,
|
---|
42 | 0x00, 0x00, 0x0f, 0x90, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x0f, 0xc0,
|
---|
43 | 0x00, 0x00, 0x24, 0x90, 0x00, 0x00, 0x40, 0xa0, 0x00, 0x00, 0x95, 0xa0,
|
---|
44 | 0xff, 0xff, 0x00, 0x00
|
---|
45 | }};
|
---|
46 |
|
---|
47 | /*
|
---|
48 | struct VS_OUTPUT {
|
---|
49 | float4 Position : POSITION;
|
---|
50 | };
|
---|
51 |
|
---|
52 | struct PS_OUTPUT {
|
---|
53 | float4 Colour : COLOR;
|
---|
54 | };
|
---|
55 |
|
---|
56 | sampler g_texDepth : register( s0 );
|
---|
57 |
|
---|
58 | PS_OUTPUT main( VS_OUTPUT IN ) {
|
---|
59 | PS_OUTPUT OUT;
|
---|
60 |
|
---|
61 | OUT.Colour = tex2D(g_texDepth, float2(0, 0));
|
---|
62 | OUT.Colour = 1.0;
|
---|
63 |
|
---|
64 | return OUT;
|
---|
65 | }
|
---|
66 | */
|
---|
67 |
|
---|
68 | const std::array<uint8_t, 140> g_pixelShaderCode = {{
|
---|
69 | 0x00, 0x02, 0xff, 0xff, 0xfe, 0xff, 0x14, 0x00, 0x43, 0x54, 0x41, 0x42,
|
---|
70 | 0x1c, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xff,
|
---|
71 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
|
---|
72 | 0x1c, 0x00, 0x00, 0x00, 0x70, 0x73, 0x5f, 0x32, 0x5f, 0x30, 0x00, 0x4d,
|
---|
73 | 0x69, 0x63, 0x72, 0x6f, 0x73, 0x6f, 0x66, 0x74, 0x20, 0x28, 0x52, 0x29,
|
---|
74 | 0x20, 0x48, 0x4c, 0x53, 0x4c, 0x20, 0x53, 0x68, 0x61, 0x64, 0x65, 0x72,
|
---|
75 | 0x20, 0x43, 0x6f, 0x6d, 0x70, 0x69, 0x6c, 0x65, 0x72, 0x20, 0x31, 0x30,
|
---|
76 | 0x2e, 0x31, 0x00, 0xab, 0x51, 0x00, 0x00, 0x05, 0x00, 0x00, 0x0f, 0xa0,
|
---|
77 | 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
78 | 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x0f, 0x80,
|
---|
79 | 0x00, 0x00, 0x00, 0xa0, 0x01, 0x00, 0x00, 0x02, 0x00, 0x08, 0x0f, 0x80,
|
---|
80 | 0x00, 0x00, 0xe4, 0x80, 0xff, 0xff, 0x00, 0x00
|
---|
81 | }};
|
---|
82 |
|
---|
83 | Logger Logger::s_instance("triangle.log");
|
---|
84 |
|
---|
85 | class TriangleApp {
|
---|
86 |
|
---|
87 | public:
|
---|
88 |
|
---|
89 | TriangleApp(SDL_Window* window)
|
---|
90 | : m_window(window) {
|
---|
91 | if (FAILED(Direct3DCreate9Ex(D3D_SDK_VERSION, &m_d3d)))
|
---|
92 | throw DxvkError("Failed to create D3D9 interface");
|
---|
93 |
|
---|
94 | D3DPRESENT_PARAMETERS params;
|
---|
95 | getPresentParams(params);
|
---|
96 |
|
---|
97 | if (FAILED(m_d3d->CreateDeviceEx(
|
---|
98 | D3DADAPTER_DEFAULT,
|
---|
99 | D3DDEVTYPE_HAL,
|
---|
100 | wsi::toHwnd(m_window),
|
---|
101 | D3DCREATE_HARDWARE_VERTEXPROCESSING,
|
---|
102 | ¶ms,
|
---|
103 | nullptr,
|
---|
104 | &m_device)))
|
---|
105 | throw DxvkError("Failed to create D3D9 device");
|
---|
106 |
|
---|
107 | if (FAILED(m_device->CreateVertexShader(reinterpret_cast<const DWORD*>(g_vertexShaderCode.data()), &m_vs)))
|
---|
108 | throw DxvkError("Failed to create vertex shader");
|
---|
109 | m_device->SetVertexShader(m_vs.ptr());
|
---|
110 |
|
---|
111 | if (FAILED(m_device->CreatePixelShader(reinterpret_cast<const DWORD*>(g_pixelShaderCode.data()), &m_ps)))
|
---|
112 | throw DxvkError("Failed to create pixel shader");
|
---|
113 | m_device->SetPixelShader(m_ps.ptr());
|
---|
114 |
|
---|
115 | std::array<float, 9> vertices = {
|
---|
116 | 0.0f, 0.5f, 0.0f,
|
---|
117 | 0.5f, -0.5f, 0.0f,
|
---|
118 | -0.5f, -0.5f, 0.0f,
|
---|
119 | };
|
---|
120 |
|
---|
121 | const size_t vbSize = vertices.size() * sizeof(float);
|
---|
122 |
|
---|
123 | if (FAILED(m_device->CreateVertexBuffer(vbSize, 0, 0, D3DPOOL_DEFAULT, &m_vb, nullptr)))
|
---|
124 | throw DxvkError("Failed to create vertex buffer");
|
---|
125 |
|
---|
126 | void* data = nullptr;
|
---|
127 | if (FAILED(m_vb->Lock(0, 0, &data, 0)))
|
---|
128 | throw DxvkError("Failed to lock vertex buffer");
|
---|
129 |
|
---|
130 | std::memcpy(data, vertices.data(), vbSize);
|
---|
131 |
|
---|
132 | if (FAILED(m_vb->Unlock()))
|
---|
133 | throw DxvkError("Failed to unlock vertex buffer");
|
---|
134 |
|
---|
135 | m_device->SetStreamSource(0, m_vb.ptr(), 0, 3 * sizeof(float));
|
---|
136 |
|
---|
137 | std::array<D3DVERTEXELEMENT9, 2> elements;
|
---|
138 |
|
---|
139 | elements[0].Method = 0;
|
---|
140 | elements[0].Offset = 0;
|
---|
141 | elements[0].Stream = 0;
|
---|
142 | elements[0].Type = D3DDECLTYPE_FLOAT3;
|
---|
143 | elements[0].Usage = D3DDECLUSAGE_POSITION;
|
---|
144 | elements[0].UsageIndex = 0;
|
---|
145 |
|
---|
146 | elements[1] = D3DDECL_END();
|
---|
147 |
|
---|
148 | if (FAILED(m_device->CreateVertexDeclaration(elements.data(), &m_decl)))
|
---|
149 | throw DxvkError("Failed to create vertex decl");
|
---|
150 |
|
---|
151 | m_device->SetVertexDeclaration(m_decl.ptr());
|
---|
152 | }
|
---|
153 |
|
---|
154 | void run() {
|
---|
155 | this->adjustBackBuffer();
|
---|
156 |
|
---|
157 | m_device->BeginScene();
|
---|
158 |
|
---|
159 | m_device->Clear(
|
---|
160 | 0, nullptr,
|
---|
161 | D3DCLEAR_TARGET,
|
---|
162 | D3DCOLOR_RGBA(44, 62, 80, 0),
|
---|
163 | 0, 0);
|
---|
164 |
|
---|
165 | m_device->Clear(
|
---|
166 | 0, nullptr,
|
---|
167 | D3DCLEAR_ZBUFFER,
|
---|
168 | 0, 0.5f, 0);
|
---|
169 |
|
---|
170 | m_device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
|
---|
171 |
|
---|
172 | m_device->EndScene();
|
---|
173 |
|
---|
174 | m_device->PresentEx(
|
---|
175 | nullptr,nullptr,
|
---|
176 | nullptr, nullptr,
|
---|
177 | 0);
|
---|
178 | }
|
---|
179 |
|
---|
180 | void adjustBackBuffer() {
|
---|
181 | int32_t w, h;
|
---|
182 | SDL_GetWindowSize(m_window, &w, &h);
|
---|
183 |
|
---|
184 | uint32_t newWindowSizeW = uint32_t(w);
|
---|
185 | uint32_t newWindowSizeH = uint32_t(h);
|
---|
186 |
|
---|
187 | if (m_windowSizeW != newWindowSizeW
|
---|
188 | || m_windowSizeH != newWindowSizeH) {
|
---|
189 | m_windowSizeW = newWindowSizeW;
|
---|
190 | m_windowSizeH = newWindowSizeH;
|
---|
191 |
|
---|
192 | D3DPRESENT_PARAMETERS params;
|
---|
193 | getPresentParams(params);
|
---|
194 | HRESULT status = m_device->ResetEx(¶ms, nullptr);
|
---|
195 |
|
---|
196 | if (FAILED(status))
|
---|
197 | throw DxvkError("Device reset failed");
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | void getPresentParams(D3DPRESENT_PARAMETERS& params) {
|
---|
202 | params.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
|
---|
203 | params.BackBufferCount = 1;
|
---|
204 | params.BackBufferFormat = D3DFMT_X8R8G8B8;
|
---|
205 | params.BackBufferWidth = m_windowSizeW;
|
---|
206 | params.BackBufferHeight = m_windowSizeH;
|
---|
207 | params.EnableAutoDepthStencil = 0;
|
---|
208 | params.Flags = 0;
|
---|
209 | params.FullScreen_RefreshRateInHz = 0;
|
---|
210 | params.hDeviceWindow = m_window;
|
---|
211 | params.MultiSampleQuality = 0;
|
---|
212 | params.MultiSampleType = D3DMULTISAMPLE_NONE;
|
---|
213 | params.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
|
---|
214 | params.SwapEffect = D3DSWAPEFFECT_DISCARD;
|
---|
215 | params.Windowed = TRUE;
|
---|
216 | }
|
---|
217 |
|
---|
218 | private:
|
---|
219 |
|
---|
220 | SDL_Window* m_window;
|
---|
221 | uint32_t m_windowSizeW = 1024;
|
---|
222 | uint32_t m_windowSizeH = 600;
|
---|
223 |
|
---|
224 | Com<IDirect3D9Ex> m_d3d;
|
---|
225 | Com<IDirect3DDevice9Ex> m_device;
|
---|
226 |
|
---|
227 | Com<IDirect3DVertexShader9> m_vs;
|
---|
228 | Com<IDirect3DPixelShader9> m_ps;
|
---|
229 | Com<IDirect3DVertexBuffer9> m_vb;
|
---|
230 | Com<IDirect3DVertexDeclaration9> m_decl;
|
---|
231 |
|
---|
232 | };
|
---|
233 |
|
---|
234 | int main(int argc, char** argv) {
|
---|
235 | if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) {
|
---|
236 | std::cerr << "Failed to init SDL" << std::endl;
|
---|
237 | return 1;
|
---|
238 | }
|
---|
239 |
|
---|
240 | SDL_Window* window = SDL_CreateWindow(
|
---|
241 | "DXVK Native Triangle! - D3D9",
|
---|
242 | SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
---|
243 | 1024, 600,
|
---|
244 | SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE);
|
---|
245 | if (!window) {
|
---|
246 | std::cerr << "Failed to create SDL window" << std::endl;
|
---|
247 | return 1;
|
---|
248 | }
|
---|
249 |
|
---|
250 | TriangleApp app(window);
|
---|
251 |
|
---|
252 | bool running = true;
|
---|
253 | while (running) {
|
---|
254 | SDL_Event event;
|
---|
255 | while (SDL_PollEvent(&event)) {
|
---|
256 | switch (event.type) {
|
---|
257 | case SDL_QUIT:
|
---|
258 | running = false;
|
---|
259 | break;
|
---|
260 | default:
|
---|
261 | break;
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|
265 | app.run();
|
---|
266 | }
|
---|
267 |
|
---|
268 | return 0;
|
---|
269 | }
|
---|
270 |
|
---|