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