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