1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "Direct3D9.hpp"
16 #include "Direct3D9Ex.hpp"
17 
18 #include "Debug.hpp"
19 
20 #include "resource.h"
21 
22 #include <stdio.h>
23 #include <assert.h>
24 
25 namespace D3D9
26 {
27 	class Direct3DShaderValidator9
28 	{
29 	public:
30 		Direct3DShaderValidator9();
31 
32 		virtual ~Direct3DShaderValidator9();
33 
34 		virtual int __stdcall ValidateShader(long *shader, long *shader1, long *shader2, long *shader3);
35 		virtual int __stdcall ValidateShader2(long *shader);
36 		virtual int __stdcall ValidateShader3(long *shader, long *shader1, long *shader2, long *shader3);
37 		virtual int __stdcall ValidateShader4(long *shader, long *shader1, long *shader2, long *shader3);
38 		virtual int __stdcall ValidateShader5(long *shader, long *shader1, long *shader2);
39 		virtual int __stdcall ValidateShader6(long *shader, long *shader1, long *shader2, long *shader3);
40 	};
41 
42 	Direct3DShaderValidator9::Direct3DShaderValidator9()
43 	{
44 	}
45 
46 	Direct3DShaderValidator9::~Direct3DShaderValidator9()
47 	{
48 	}
49 
50 	int __stdcall Direct3DShaderValidator9::ValidateShader(long *shader, long *shader1, long *shader2, long *shader3)   // FIXME
51 	{
52 		TRACE("");
53 
54 		UNIMPLEMENTED();
55 
56 		return true;   // FIXME
57 	}
58 
59 	int __stdcall Direct3DShaderValidator9::ValidateShader2(long *shader)   // FIXME
60 	{
61 		TRACE("");
62 
63 		UNIMPLEMENTED();
64 
65 		return true;   // FIXME
66 	}
67 
68 	int __stdcall Direct3DShaderValidator9::ValidateShader3(long *shader, long *shader1, long *shader2, long *shader3)
69 	{
70 		TRACE("");
71 
72 		UNIMPLEMENTED();
73 
74 		return true;   // FIXME
75 	}
76 
77 	int __stdcall Direct3DShaderValidator9::ValidateShader4(long *shader, long *shader1, long *shader2, long *shader3)
78 	{
79 		TRACE("");
80 
81 		UNIMPLEMENTED();
82 
83 		return true;   // FIXME
84 	}
85 
86 	int __stdcall Direct3DShaderValidator9::ValidateShader5(long *shader, long *shader1, long *shader2)   // FIXME
87 	{
88 		TRACE("");
89 
90 		UNIMPLEMENTED();
91 
92 		return true;   // FIXME
93 	}
94 
95 	int __stdcall Direct3DShaderValidator9::ValidateShader6(long *shader, long *shader1, long *shader2, long *shader3)   // FIXME
96 	{
97 		TRACE("");
98 
99 		UNIMPLEMENTED();
100 
101 		return true;   // FIXME
102 	}
103 }
104 
105 static INT_PTR CALLBACK DebuggerWaitDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
106 {
107 	RECT rect;
108 
109 	switch(uMsg)
110 	{
111 	case WM_INITDIALOG:
112 		GetWindowRect(GetDesktopWindow(), &rect);
113 		SetWindowPos(hwnd, HWND_TOP, rect.right / 2, rect.bottom / 2, 0, 0, SWP_NOSIZE);
114 		SetTimer(hwnd, 1, 100, NULL);
115 		return TRUE;
116 	case WM_COMMAND:
117 		if(LOWORD(wParam) == IDCANCEL)
118 		{
119 			EndDialog(hwnd, 0);
120 		}
121 		break;
122 	case WM_TIMER:
123 		if(IsDebuggerPresent())
124 		{
125 			EndDialog(hwnd, 0);
126 		}
127 	}
128 
129 	return FALSE;
130 }
131 
132 static void WaitForDebugger(HINSTANCE instance)
133 {
134 	if(!IsDebuggerPresent())
135 	{
136 		HRSRC dialog = FindResource(instance, MAKEINTRESOURCE(IDD_DIALOG1), RT_DIALOG);
137 		DLGTEMPLATE *dialogTemplate = (DLGTEMPLATE*)LoadResource(instance, dialog);
138 		DialogBoxIndirect(instance, dialogTemplate, NULL, DebuggerWaitDialogProc);
139 	}
140 }
141 
142 using namespace D3D9;
143 
144 extern "C"
145 {
146 	HINSTANCE dllInstance = 0;
147 
148 	int	__stdcall DllMain(HINSTANCE instance, unsigned long reason, void *reserved)
149 	{
150 		#ifndef NDEBUG
151 			if(dllInstance == 0)
152 			{
153 				FILE *file = fopen("debug.txt", "w");   // Clear debug log
154 				if(file) fclose(file);
155 			}
156 		#endif
157 
158 		GTRACE("HINSTANCE instance = 0x%0.8p, unsigned long reason = %d, void *reserved = 0x%0.8p", instance, reason, reserved);
159 
160 		dllInstance	= instance;
161 
162 		switch(reason)
163 		{
164 		case DLL_PROCESS_DETACH:
165 			break;
166 		case DLL_PROCESS_ATTACH:
167 			#ifndef NDEBUG
168 				WaitForDebugger(instance);
169 			#endif
170 			DisableThreadLibraryCalls(instance);
171 			break;
172 		case DLL_THREAD_ATTACH:
173 			break;
174 		case DLL_THREAD_DETACH:
175 			break;
176 		default:
177 			SetLastError(ERROR_INVALID_PARAMETER);
178 			return FALSE;
179 		}
180 
181 		return TRUE;
182 	}
183 
184 	IDirect3D9 *__stdcall Direct3DCreate9(unsigned int version)
185 	{
186 		GTRACE("");
187 
188 		// D3D_SDK_VERSION check
189 		if(version != (31 | 0x80000000) && // 9.0a/b DEBUG_INFO
190 		   version != 31 &&                // 9.0a/b
191 		   version != (32 | 0x80000000) && // 9.0c DEBUG_INFO
192 		   version != 32)                  // 9.0c
193 		{
194 			return 0;
195 		}
196 
197 		IDirect3D9 *device = new D3D9::Direct3D9(version, dllInstance);
198 
199 		if(device)
200 		{
201 			device->AddRef();
202 		}
203 
204 		return device;
205 	}
206 
207 	HRESULT __stdcall Direct3DCreate9Ex(unsigned int version, IDirect3D9Ex **device)
208 	{
209 		// D3D_SDK_VERSION check
210 		if(version != (31 | 0x80000000) && // 9.0a/b DEBUG_INFO
211 		   version != 31 &&                // 9.0a/b
212 		   version != (32 | 0x80000000) && // 9.0c DEBUG_INFO
213 		   version != 32)                  // 9.0c
214 		{
215 			return NOTAVAILABLE();
216 		}
217 
218 		*device = new D3D9::Direct3D9Ex(version, dllInstance);
219 
220 		if(device)
221 		{
222 			(*device)->AddRef();
223 		}
224 		else
225 		{
226 			return OUTOFMEMORY();
227 		}
228 
229 		return D3D_OK;
230 	}
231 
232 	int __stdcall CheckFullscreen()
233 	{
234 		GTRACE("");
235 
236 		UNIMPLEMENTED();
237 
238 		return FALSE;
239 	}
240 
241 	int __stdcall D3DPERF_BeginEvent(D3DCOLOR color, const wchar_t *name)
242 	{
243 		GTRACE("");
244 
245 	//	UNIMPLEMENTED();   // PIX unsupported
246 
247 		return -1;
248 	}
249 
250 	int __stdcall D3DPERF_EndEvent()
251 	{
252 		GTRACE("");
253 
254 	//	UNIMPLEMENTED();   // PIX unsupported
255 
256 		return -1;
257 	}
258 
259 	unsigned long __stdcall D3DPERF_GetStatus()
260 	{
261 		GTRACE("");
262 
263 	//	UNIMPLEMENTED();   // PIX unsupported
264 
265 		return 0;
266 	}
267 
268 	int __stdcall D3DPERF_QueryRepeatFrame()
269 	{
270 		GTRACE("");
271 
272 	//	UNIMPLEMENTED();   // PIX unsupported
273 
274 		return FALSE;
275 	}
276 
277 	void __stdcall D3DPERF_SetMarker(D3DCOLOR color, const wchar_t *name)
278 	{
279 		GTRACE("");
280 
281 	//	UNIMPLEMENTED();   // PIX unsupported
282 	}
283 
284 	void __stdcall D3DPERF_SetOptions(unsigned long options)
285 	{
286 		GTRACE("");
287 
288 	//	UNIMPLEMENTED();   // PIX unsupported
289 	}
290 
291 	void __stdcall D3DPERF_SetRegion(D3DCOLOR color, const wchar_t *name)
292 	{
293 		GTRACE("");
294 
295 	//	UNIMPLEMENTED();   // PIX unsupported
296 	}
297 
298 	void __cdecl DebugSetLevel(long level)
299 	{
300 		GTRACE("long level = %d", level);
301 
302 	//	UNIMPLEMENTED();   // Debug output unsupported
303 	}
304 
305 	void __cdecl DebugSetMute(long mute)
306 	{
307 		GTRACE("long mute = %d", mute);
308 
309 	//	UNIMPLEMENTED();   // Debug output unsupported
310 	}
311 
312 	void *__stdcall Direct3DShaderValidatorCreate9()
313 	{
314 		GTRACE("");
315 
316 	//	UNIMPLEMENTED();
317 
318 		return 0;
319 
320 	//	return new D3D9::Direct3DShaderValidator9();
321 	}
322 
323 	void __stdcall PSGPError()
324 	{
325 		GTRACE("");
326 
327 		UNIMPLEMENTED();
328 	}
329 
330 	void __stdcall PSGPSampleTexture()
331 	{
332 		GTRACE("");
333 
334 		UNIMPLEMENTED();
335 	}
336 }
337