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