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