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 # notify frame has been completed
56 if method.name == 'Present':
57 print r' retrace::frameComplete(call);'
59 Retracer.invokeInterfaceMethod(self, interface, method)
62 if str(method.type) == 'HRESULT':
63 print r' if (FAILED(_result)) {'
64 print r' retrace::warning(call) << "failed\n";'
67 if method.name in ('Lock', 'LockRect', 'LockBox'):
68 print ' size_t _LockedSize = _getLockSize(_this, %s);' % ', '.join(method.argNames()[:-1])
69 if method.name == 'Lock':
70 # FIXME: handle recursive locks
71 print ' VOID *_pbData = *ppbData;'
72 elif method.name == 'LockRect':
73 print ' VOID *_pbData = pLockedRect->pBits;'
74 elif method.name == 'LockBox':
75 print ' VOID *_pbData = pLockedVolume->pBits;'
77 raise NotImplementedError
78 print ' _this->SetPrivateData(GUID_APITRACE, &_pbData, sizeof _pbData, 0);'
80 if method.name in ('Unlock', 'UnlockRect', 'UnlockBox'):
81 print ' VOID *_pbData = 0;'
82 print ' DWORD dwSizeOfData = sizeof _pbData;'
83 print ' _this->GetPrivateData(GUID_APITRACE, &_pbData, &dwSizeOfData);'
84 print ' if (_pbData) {'
85 print ' retrace::delRegionByPointer(_pbData);'
89 if __name__ == '__main__':
97 #include "d3d9imports.hpp"
98 #include "d3dsize.hpp"
99 #include "d3dretrace.hpp"
102 // XXX: Don't duplicate this code.
104 static LRESULT CALLBACK
105 WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
109 case WM_GETMINMAXINFO:
110 // Allow to create a window bigger than the desktop
111 pMMI = (MINMAXINFO *)lParam;
112 pMMI->ptMaxSize.x = 60000;
113 pMMI->ptMaxSize.y = 60000;
114 pMMI->ptMaxTrackSize.x = 60000;
115 pMMI->ptMaxTrackSize.y = 60000;
121 return DefWindowProc(hWnd, uMsg, wParam, lParam);
125 DEFINE_GUID(GUID_APITRACE,0X7D71CAC9,0X7F58,0X432C,0XA9,0X75,0XA1,0X9F,0XCF,0XCE,0XFD,0X14);
129 createWindow(int width, int height) {
130 static bool first = TRUE;
135 memset(&wc, 0, sizeof wc);
136 wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
137 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
138 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
139 wc.lpfnWndProc = WndProc;
140 wc.lpszClassName = "d3dretrace";
141 wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
151 dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
157 rect.right = rect.left + width;
158 rect.bottom = rect.top + height;
160 AdjustWindowRectEx(&rect, dwStyle, FALSE, dwExStyle);
162 hWnd = CreateWindowEx(dwExStyle,
163 "d3dretrace", /* wc.lpszClassName */
168 rect.right - rect.left, /* width */
169 rect.bottom - rect.top, /* height */
174 ShowWindow(hWnd, SW_SHOW);
181 (WINAPI *PD3DXASSEMBLESHADER)(
184 const D3DXMACRO *pDefines,
185 LPD3DXINCLUDE pInclude,
187 LPD3DXBUFFER *ppShader,
188 LPD3DXBUFFER *ppErrorMsgs
192 extractShader(LPCSTR pSrcData)
194 static BOOL firsttime = TRUE;
195 static HMODULE hD3DXModule = NULL;
196 static PD3DXASSEMBLESHADER pfnD3DXAssembleShader = NULL;
202 for (release = 0; release <= 1; ++release) {
203 /* Version 41 corresponds to Mar 2009 version of DirectX Runtime / SDK */
204 for (version = 41; version >= 0; --version) {
206 _snprintf(filename, sizeof(filename),
207 "d3dx9%s%s%u.dll", release ? "" : "d", version ? "_" : "", version);
208 hD3DXModule = LoadLibraryA(filename);
218 if (!pfnD3DXAssembleShader) {
219 pfnD3DXAssembleShader = (PD3DXASSEMBLESHADER)GetProcAddress(hD3DXModule, "D3DXAssembleShader");
226 if (pfnD3DXAssembleShader) {
227 LPD3DXBUFFER pTokens = NULL;
230 hr = pfnD3DXAssembleShader(pSrcData, strlen(pSrcData), NULL, NULL, 0, &pTokens, NULL);
232 return (DWORD *)pTokens->GetBufferPointer();
235 // FIXME: Don't leak pTokens
242 retracer = D3DRetracer()
243 retracer.retraceApi(d3d9)