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