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