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 invokeInterfaceMethod(self, interface, method):
45 # keep track of the last used device for state dumping
46 if interface.name in ('IDirect3DDevice9', 'IDirect3DDevice9Ex'):
47 print r' d3dretrace::pLastDirect3DDevice9 = _this;'
49 # create windows as neccessary
50 if method.name in ('CreateDevice', 'CreateDeviceEx'):
51 print r' HWND hWnd = createWindow(pPresentationParameters->BackBufferWidth, pPresentationParameters->BackBufferHeight);'
52 print r' hFocusWindow = hWnd;'
53 print r' pPresentationParameters->hDeviceWindow = hWnd;'
55 Retracer.invokeInterfaceMethod(self, interface, method)
58 if str(method.type) == 'HRESULT':
59 print r' if (FAILED(_result)) {'
60 print r' retrace::warning(call) << "failed\n";'
63 if method.name in ('Lock', 'LockRect'):
64 print ' size_t _LockedSize = _getLockSize(_this, %s);' % ', '.join(method.argNames()[:-1])
67 if __name__ == '__main__':
73 #include "d3d9imports.hpp"
74 #include "d3dsize.hpp"
75 #include "d3dretrace.hpp"
78 // XXX: Don't duplicate this code.
80 static LRESULT CALLBACK
81 WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
85 case WM_GETMINMAXINFO:
86 // Allow to create a window bigger than the desktop
87 pMMI = (MINMAXINFO *)lParam;
88 pMMI->ptMaxSize.x = 60000;
89 pMMI->ptMaxSize.y = 60000;
90 pMMI->ptMaxTrackSize.x = 60000;
91 pMMI->ptMaxTrackSize.y = 60000;
97 return DefWindowProc(hWnd, uMsg, wParam, lParam);
102 createWindow(int width, int height) {
103 static bool first = TRUE;
108 memset(&wc, 0, sizeof wc);
109 wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
110 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
111 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
112 wc.lpfnWndProc = WndProc;
113 wc.lpszClassName = "d3dretrace";
114 wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
124 dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
130 rect.right = rect.left + width;
131 rect.bottom = rect.top + height;
133 AdjustWindowRectEx(&rect, dwStyle, FALSE, dwExStyle);
135 hWnd = CreateWindowEx(dwExStyle,
136 "d3dretrace", /* wc.lpszClassName */
141 rect.right - rect.left, /* width */
142 rect.bottom - rect.top, /* height */
147 ShowWindow(hWnd, SW_SHOW);
154 (WINAPI *PD3DXASSEMBLESHADER)(
157 const D3DXMACRO *pDefines,
158 LPD3DXINCLUDE pInclude,
160 LPD3DXBUFFER *ppShader,
161 LPD3DXBUFFER *ppErrorMsgs
165 extractShader(LPCSTR pSrcData)
167 static BOOL firsttime = TRUE;
168 static HMODULE hD3DXModule = NULL;
169 static PD3DXASSEMBLESHADER pfnD3DXAssembleShader = NULL;
175 for (release = 0; release <= 1; ++release) {
176 /* Version 41 corresponds to Mar 2009 version of DirectX Runtime / SDK */
177 for (version = 41; version >= 0; --version) {
179 _snprintf(filename, sizeof(filename),
180 "d3dx9%s%s%u.dll", release ? "" : "d", version ? "_" : "", version);
181 hD3DXModule = LoadLibraryA(filename);
191 if (!pfnD3DXAssembleShader) {
192 pfnD3DXAssembleShader = (PD3DXASSEMBLESHADER)GetProcAddress(hD3DXModule, "D3DXAssembleShader");
199 if (pfnD3DXAssembleShader) {
200 LPD3DXBUFFER pTokens = NULL;
203 hr = pfnD3DXAssembleShader(pSrcData, strlen(pSrcData), NULL, NULL, 0, &pTokens, NULL);
205 return (DWORD *)pTokens->GetBufferPointer();
208 // FIXME: Don't leak pTokens
215 retracer = D3DRetracer()
216 retracer.retraceApi(d3d9)