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