]> git.cworth.org Git - apitrace/blob - retrace/d3dretrace.py
Refactor d3d9 lock tracking code further.
[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         if 'pSharedHandle' in method.argNames():
60             print r'    if (pSharedHandle) {'
61             print r'        retrace::warning(call) << "shared surfaces unsupported\n";'
62             print r'        pSharedHandle = NULL;'
63             print r'    }'
64
65         Retracer.invokeInterfaceMethod(self, interface, method)
66
67         # check errors
68         if str(method.type) == 'HRESULT':
69             print r'    if (FAILED(_result)) {'
70             print r'        retrace::warning(call) << "failed\n";'
71             print r'    }'
72
73         if method.name in ('Lock', 'LockRect', 'LockBox'):
74             print '    VOID *_pbData = NULL;'
75             print '    size_t _LockedSize = 0;'
76             print '    _getLockInfo(_this, %s, _pbData, _LockedSize);' % ', '.join(method.argNames()[:-1])
77             print '    _this->SetPrivateData(GUID_APITRACE, &_pbData, sizeof _pbData, 0);'
78         
79         if method.name in ('Unlock', 'UnlockRect', 'UnlockBox'):
80             print '    VOID *_pbData = 0;'
81             print '    DWORD dwSizeOfData = sizeof _pbData;'
82             print '    _this->GetPrivateData(GUID_APITRACE, &_pbData, &dwSizeOfData);'
83             print '    if (_pbData) {'
84             print '        retrace::delRegionByPointer(_pbData);'
85             print '    }'
86
87
88 if __name__ == '__main__':
89     print r'''
90 #define INITGUID
91
92 #include <string.h>
93
94 #include <iostream>
95
96 #include "d3d9imports.hpp"
97 #include "d3dsize.hpp"
98 #include "d3dretrace.hpp"
99
100
101 // XXX: Don't duplicate this code.
102
103 static LRESULT CALLBACK
104 WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
105 {
106     MINMAXINFO *pMMI;
107     switch (uMsg) {
108     case WM_GETMINMAXINFO:
109         // Allow to create a window bigger than the desktop
110         pMMI = (MINMAXINFO *)lParam;
111         pMMI->ptMaxSize.x = 60000;
112         pMMI->ptMaxSize.y = 60000;
113         pMMI->ptMaxTrackSize.x = 60000;
114         pMMI->ptMaxTrackSize.y = 60000;
115         break;
116     default:
117         break;
118     }
119
120     return DefWindowProc(hWnd, uMsg, wParam, lParam);
121 }
122
123
124 DEFINE_GUID(GUID_APITRACE,0X7D71CAC9,0X7F58,0X432C,0XA9,0X75,0XA1,0X9F,0XCF,0XCE,0XFD,0X14);
125
126
127 static HWND
128 createWindow(int width, int height) {
129     static bool first = TRUE;
130     RECT rect;
131
132     if (first) {
133         WNDCLASS wc;
134         memset(&wc, 0, sizeof wc);
135         wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
136         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
137         wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
138         wc.lpfnWndProc = WndProc;
139         wc.lpszClassName = "d3dretrace";
140         wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
141         RegisterClass(&wc);
142         first = FALSE;
143     }
144
145     DWORD dwExStyle;
146     DWORD dwStyle;
147     HWND hWnd;
148
149     dwExStyle = 0;
150     dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
151
152     int x = 0, y = 0;
153
154     rect.left = x;
155     rect.top = y;
156     rect.right = rect.left + width;
157     rect.bottom = rect.top + height;
158
159     AdjustWindowRectEx(&rect, dwStyle, FALSE, dwExStyle);
160
161     hWnd = CreateWindowEx(dwExStyle,
162                           "d3dretrace", /* wc.lpszClassName */
163                           NULL,
164                           dwStyle,
165                           0, /* x */
166                           0, /* y */
167                           rect.right - rect.left, /* width */
168                           rect.bottom - rect.top, /* height */
169                           NULL,
170                           NULL,
171                           NULL,
172                           NULL);
173     ShowWindow(hWnd, SW_SHOW);
174     return hWnd;
175 }
176
177
178
179 typedef HRESULT
180 (WINAPI *PD3DXASSEMBLESHADER)(
181     LPCSTR pSrcData,
182     UINT SrcDataLen,
183     const D3DXMACRO *pDefines,
184     LPD3DXINCLUDE pInclude,
185     DWORD Flags,
186     LPD3DXBUFFER *ppShader,
187     LPD3DXBUFFER *ppErrorMsgs
188 );
189
190 DWORD *
191 extractShader(LPCSTR pSrcData)
192 {
193     static BOOL firsttime = TRUE;
194     static HMODULE hD3DXModule = NULL;
195     static PD3DXASSEMBLESHADER pfnD3DXAssembleShader = NULL;
196
197     if (firsttime) {
198         if (!hD3DXModule) {
199             unsigned release;
200             int version;
201             for (release = 0; release <= 1; ++release) {
202                 /* Version 41 corresponds to Mar 2009 version of DirectX Runtime / SDK */
203                 for (version = 41; version >= 0; --version) {
204                     char filename[256];
205                     _snprintf(filename, sizeof(filename),
206                               "d3dx9%s%s%u.dll", release ? "" : "d", version ? "_" : "", version);
207                     hD3DXModule = LoadLibraryA(filename);
208                     if (hD3DXModule)
209                         goto found;
210                 }
211             }
212 found:
213             ;
214         }
215
216         if (hD3DXModule) {
217             if (!pfnD3DXAssembleShader) {
218                 pfnD3DXAssembleShader = (PD3DXASSEMBLESHADER)GetProcAddress(hD3DXModule, "D3DXAssembleShader");
219             }
220         }
221
222         firsttime = FALSE;
223     }
224
225     if (pfnD3DXAssembleShader) {
226         LPD3DXBUFFER pTokens = NULL;
227         HRESULT hr;
228
229         hr = pfnD3DXAssembleShader(pSrcData, strlen(pSrcData), NULL, NULL, 0, &pTokens, NULL);
230         if (SUCCEEDED(hr)) {
231             return (DWORD *)pTokens->GetBufferPointer();
232         }
233
234         // FIXME: Don't leak pTokens
235     }
236
237     return NULL;
238 }
239 '''
240
241     retracer = D3DRetracer()
242     retracer.retraceApi(d3d9)