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