1 ##########################################################################
3 # Copyright 2011 Jose Fonseca
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 ##########################################################################/
27 """D3D retracer generator."""
30 from dllretrace import DllRetracer as Retracer
31 import specs.stdapi as stdapi
32 from specs.d3d9 import *
35 class D3DRetracer(Retracer):
37 table_name = 'd3dretrace::d3d9_callbacks'
39 bufferInterfaceNames = [
40 'IDirect3DVertexBuffer9',
41 'IDirect3DIndexBuffer9',
44 def extractArg(self, function, arg, arg_type, lvalue, rvalue):
45 if arg.type is D3DSHADER9:
46 print r' %s = extractShader((%s).toString());' % (lvalue, rvalue)
47 print r' if (!%s) {' % lvalue
48 print r' retrace::warning(call) << "failed to assemble shader\n";'
53 Retracer.extractArg(self, function, arg, arg_type, lvalue, rvalue)
55 def invokeInterfaceMethod(self, interface, method):
56 if method.name in ('CreateDevice', 'CreateDeviceEx'):
57 print r' HWND hWnd = createWindow(pPresentationParameters->BackBufferWidth, pPresentationParameters->BackBufferHeight);'
58 print r' hFocusWindow = hWnd;'
59 print r' pPresentationParameters->hDeviceWindow = hWnd;'
61 Retracer.invokeInterfaceMethod(self, interface, method)
63 if str(method.type) == 'HRESULT':
64 print r' if (_result != S_OK) {'
65 print r' retrace::warning(call) << "failed\n";'
68 if interface.name in self.bufferInterfaceNames and method.name == 'Lock':
69 getDescMethod = interface.getMethodByName('GetDesc')
70 descArg = getDescMethod.args[0]
72 descType = getDescMethod.args[0].type.type
74 print ' if (!SizeToLock) {'
75 print ' %s Desc;' % descType
76 print ' _this->GetDesc(&Desc);'
77 print ' SizeToLock = Desc.Size;'
81 if __name__ == '__main__':
87 #include "d3d9imports.hpp"
88 #include "d3dretrace.hpp"
91 // XXX: Don't duplicate this code.
93 static LRESULT CALLBACK
94 WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
98 case WM_GETMINMAXINFO:
99 // Allow to create a window bigger than the desktop
100 pMMI = (MINMAXINFO *)lParam;
101 pMMI->ptMaxSize.x = 60000;
102 pMMI->ptMaxSize.y = 60000;
103 pMMI->ptMaxTrackSize.x = 60000;
104 pMMI->ptMaxTrackSize.y = 60000;
110 return DefWindowProc(hWnd, uMsg, wParam, lParam);
115 createWindow(int width, int height) {
116 static bool first = TRUE;
121 memset(&wc, 0, sizeof wc);
122 wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
123 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
124 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
125 wc.lpfnWndProc = WndProc;
126 wc.lpszClassName = "d3dretrace";
127 wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
137 dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
143 rect.right = rect.left + width;
144 rect.bottom = rect.top + height;
146 AdjustWindowRectEx(&rect, dwStyle, FALSE, dwExStyle);
148 hWnd = CreateWindowEx(dwExStyle,
149 "d3dretrace", /* wc.lpszClassName */
154 rect.right - rect.left, /* width */
155 rect.bottom - rect.top, /* height */
160 ShowWindow(hWnd, SW_SHOW);
167 (WINAPI *PD3DXASSEMBLESHADER)(
170 const D3DXMACRO *pDefines,
171 LPD3DXINCLUDE pInclude,
173 LPD3DXBUFFER *ppShader,
174 LPD3DXBUFFER *ppErrorMsgs
178 extractShader(LPCSTR pSrcData)
180 static BOOL firsttime = TRUE;
181 static HMODULE hD3DXModule = NULL;
182 static PD3DXASSEMBLESHADER pfnD3DXAssembleShader = NULL;
188 for (release = 0; release <= 1; ++release) {
189 /* Version 41 corresponds to Mar 2009 version of DirectX Runtime / SDK */
190 for (version = 41; version >= 0; --version) {
192 _snprintf(filename, sizeof(filename),
193 "d3dx9%s%s%u.dll", release ? "" : "d", version ? "_" : "", version);
194 hD3DXModule = LoadLibraryA(filename);
204 if (!pfnD3DXAssembleShader) {
205 pfnD3DXAssembleShader = (PD3DXASSEMBLESHADER)GetProcAddress(hD3DXModule, "D3DXAssembleShader");
212 if (pfnD3DXAssembleShader) {
213 LPD3DXBUFFER pTokens = NULL;
216 hr = pfnD3DXAssembleShader(pSrcData, strlen(pSrcData), NULL, NULL, 0, &pTokens, NULL);
218 return (DWORD *)pTokens->GetBufferPointer();
221 // FIXME: Don't leak pTokens
229 retracer = D3DRetracer()
230 retracer.retraceApi(d3d9)