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