]> git.cworth.org Git - apitrace/blob - retrace/d3dretrace.py
Don't retrace IDirect3DQuery9::GetData.
[apitrace] / retrace / d3dretrace.py
1 ##########################################################################
2 #
3 # Copyright 2011 Jose Fonseca
4 # All Rights Reserved.
5 #
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:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
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
22 # THE SOFTWARE.
23 #
24 ##########################################################################/
25
26
27 """D3D retracer generator."""
28
29
30 from dllretrace import DllRetracer as Retracer
31 import specs.stdapi as stdapi
32 from specs.d3d9 import *
33
34
35 class D3DRetracer(Retracer):
36
37     table_name = 'd3dretrace::d3d9_callbacks'
38
39     bufferInterfaceNames = [
40         'IDirect3DVertexBuffer9',
41         'IDirect3DIndexBuffer9',
42     ]
43
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;'
48
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;'
54
55         # notify frame has been completed
56         if method.name == 'Present':
57             print r'    retrace::frameComplete(call);'
58
59         Retracer.invokeInterfaceMethod(self, interface, method)
60
61         # check errors
62         if str(method.type) == 'HRESULT':
63             print r'    if (FAILED(_result)) {'
64             print r'        retrace::warning(call) << "failed\n";'
65             print r'    }'
66
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;'
76             else:
77                 raise NotImplementedError
78             print '        _this->SetPrivateData(GUID_APITRACE, &_pbData, sizeof _pbData, 0);'
79         
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);'
86             print '        }'
87
88
89 if __name__ == '__main__':
90     print r'''
91 #define INITGUID
92
93 #include <string.h>
94
95 #include <iostream>
96
97 #include "d3d9imports.hpp"
98 #include "d3dsize.hpp"
99 #include "d3dretrace.hpp"
100
101
102 // XXX: Don't duplicate this code.
103
104 static LRESULT CALLBACK
105 WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
106 {
107     MINMAXINFO *pMMI;
108     switch (uMsg) {
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;
116         break;
117     default:
118         break;
119     }
120
121     return DefWindowProc(hWnd, uMsg, wParam, lParam);
122 }
123
124
125 DEFINE_GUID(GUID_APITRACE,0X7D71CAC9,0X7F58,0X432C,0XA9,0X75,0XA1,0X9F,0XCF,0XCE,0XFD,0X14);
126
127
128 static HWND
129 createWindow(int width, int height) {
130     static bool first = TRUE;
131     RECT rect;
132
133     if (first) {
134         WNDCLASS wc;
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;
142         RegisterClass(&wc);
143         first = FALSE;
144     }
145
146     DWORD dwExStyle;
147     DWORD dwStyle;
148     HWND hWnd;
149
150     dwExStyle = 0;
151     dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
152
153     int x = 0, y = 0;
154
155     rect.left = x;
156     rect.top = y;
157     rect.right = rect.left + width;
158     rect.bottom = rect.top + height;
159
160     AdjustWindowRectEx(&rect, dwStyle, FALSE, dwExStyle);
161
162     hWnd = CreateWindowEx(dwExStyle,
163                           "d3dretrace", /* wc.lpszClassName */
164                           NULL,
165                           dwStyle,
166                           0, /* x */
167                           0, /* y */
168                           rect.right - rect.left, /* width */
169                           rect.bottom - rect.top, /* height */
170                           NULL,
171                           NULL,
172                           NULL,
173                           NULL);
174     ShowWindow(hWnd, SW_SHOW);
175     return hWnd;
176 }
177
178
179
180 typedef HRESULT
181 (WINAPI *PD3DXASSEMBLESHADER)(
182     LPCSTR pSrcData,
183     UINT SrcDataLen,
184     const D3DXMACRO *pDefines,
185     LPD3DXINCLUDE pInclude,
186     DWORD Flags,
187     LPD3DXBUFFER *ppShader,
188     LPD3DXBUFFER *ppErrorMsgs
189 );
190
191 DWORD *
192 extractShader(LPCSTR pSrcData)
193 {
194     static BOOL firsttime = TRUE;
195     static HMODULE hD3DXModule = NULL;
196     static PD3DXASSEMBLESHADER pfnD3DXAssembleShader = NULL;
197
198     if (firsttime) {
199         if (!hD3DXModule) {
200             unsigned release;
201             int version;
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) {
205                     char filename[256];
206                     _snprintf(filename, sizeof(filename),
207                               "d3dx9%s%s%u.dll", release ? "" : "d", version ? "_" : "", version);
208                     hD3DXModule = LoadLibraryA(filename);
209                     if (hD3DXModule)
210                         goto found;
211                 }
212             }
213 found:
214             ;
215         }
216
217         if (hD3DXModule) {
218             if (!pfnD3DXAssembleShader) {
219                 pfnD3DXAssembleShader = (PD3DXASSEMBLESHADER)GetProcAddress(hD3DXModule, "D3DXAssembleShader");
220             }
221         }
222
223         firsttime = FALSE;
224     }
225
226     if (pfnD3DXAssembleShader) {
227         LPD3DXBUFFER pTokens = NULL;
228         HRESULT hr;
229
230         hr = pfnD3DXAssembleShader(pSrcData, strlen(pSrcData), NULL, NULL, 0, &pTokens, NULL);
231         if (SUCCEEDED(hr)) {
232             return (DWORD *)pTokens->GetBufferPointer();
233         }
234
235         // FIXME: Don't leak pTokens
236     }
237
238     return NULL;
239 }
240 '''
241
242     retracer = D3DRetracer()
243     retracer.retraceApi(d3d9)