]> git.cworth.org Git - apitrace/blob - retrace/d3d9retrace.py
9884d92d4f8fe602f31e662e802a6e8f2fc21da5
[apitrace] / retrace / d3d9retrace.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 from specs.stdapi import API
32 from specs.d3d9 import *
33
34
35 class D3DRetracer(Retracer):
36
37     def retraceApi(self, api):
38         print '''
39
40 class D3D9Dumper : public retrace::Dumper {
41 public:
42     IDirect3DDevice9 *pLastDirect3DDevice9;
43
44     D3D9Dumper() :
45         pLastDirect3DDevice9(NULL)
46     {}
47
48     image::Image *
49     getSnapshot(void) {
50         if (!pLastDirect3DDevice9) {
51             return NULL;
52         }
53         return d3dstate::getRenderTargetImage(pLastDirect3DDevice9);
54     }
55
56     bool
57     dumpState(std::ostream &os) {
58         if (!pLastDirect3DDevice9) {
59             return false;
60         }
61         d3dstate::dumpDevice(os, pLastDirect3DDevice9);
62         return true;
63     }
64
65     inline void
66     bindDevice(IDirect3DDevice9 *pDevice) {
67         pLastDirect3DDevice9 = pDevice;
68         retrace::dumper = this;
69     }
70     
71     inline void
72     unbindDevice(IDirect3DDevice9 *pDevice) {
73         if (pLastDirect3DDevice9 == pDevice) {
74             pLastDirect3DDevice9 = NULL;
75         }
76     }
77 };
78
79 static D3D9Dumper d3d9Dumper;
80 '''
81
82         print '// Swizzling mapping for lock addresses'
83         print 'static std::map<void *, void *> _maps;'
84         print
85
86         self.table_name = 'd3dretrace::d3d9_callbacks'
87
88         Retracer.retraceApi(self, api)
89
90     def invokeFunction(self, function):
91         if function.name in ('Direct3DCreate9', 'Direct3DCreate9Ex'):
92             print 'if (retrace::debug && !g_szD3D9DllName) {'
93             print '    /* '
94             print '     * XXX: D3D9D only works for simple things, it often introduces errors'
95             print '     * on complex traces, or traces which use unofficial D3D9 features.'
96             print '     */'
97             print '    if (0) {'
98             print '        g_szD3D9DllName = "d3d9d.dll";'
99             print '    }'
100             print '}'
101
102         Retracer.invokeFunction(self, function)
103
104     def invokeInterfaceMethod(self, interface, method):
105         # keep track of the last used device for state dumping
106         if interface.name in ('IDirect3DDevice9', 'IDirect3DDevice9Ex'):
107             if method.name == 'Release':
108                 print r'    d3d9Dumper.unbindDevice(_this);'
109             else:
110                 print r'    d3d9Dumper.bindDevice(_this);'
111
112         # create windows as neccessary
113         if method.name in ('CreateDevice', 'CreateDeviceEx', 'CreateAdditionalSwapChain'):
114             print r'    HWND hWnd = d3dretrace::createWindow(pPresentationParameters->BackBufferWidth, pPresentationParameters->BackBufferHeight);'
115             print r'    pPresentationParameters->hDeviceWindow = hWnd;'
116             if 'hFocusWindow' in method.argNames():
117                 print r'    hFocusWindow = hWnd;'
118
119         if method.name in ('Reset', 'ResetEx'):
120             print r'    if (pPresentationParameters->Windowed) {'
121             print r'        d3dretrace::resizeWindow(pPresentationParameters->hDeviceWindow, pPresentationParameters->BackBufferWidth, pPresentationParameters->BackBufferHeight);'
122             print r'    }'
123
124         # notify frame has been completed
125         if method.name == 'Present':
126             print r'    retrace::frameComplete(call);'
127             print r'    hDestWindowOverride = NULL;'
128
129         if 'pSharedHandle' in method.argNames():
130             print r'    if (pSharedHandle) {'
131             print r'        retrace::warning(call) << "shared surfaces unsupported\n";'
132             print r'        pSharedHandle = NULL;'
133             print r'    }'
134
135         Retracer.invokeInterfaceMethod(self, interface, method)
136
137         # process events after presents
138         if method.name == 'Present':
139             print r'    d3dretrace::processEvents();'
140
141         # check errors
142         if str(method.type) == 'HRESULT':
143             print r'    if (FAILED(_result)) {'
144             print r'        retrace::warning(call) << "failed\n";'
145             print r'    }'
146
147         if method.name in ('Lock', 'LockRect', 'LockBox'):
148             print '    VOID *_pbData = NULL;'
149             print '    size_t _MappedSize = 0;'
150             print '    _getMapInfo(_this, %s, _pbData, _MappedSize);' % ', '.join(method.argNames()[:-1])
151             print '    _maps[_this] = _pbData;'
152         
153         if method.name in ('Unlock', 'UnlockRect', 'UnlockBox'):
154             print '    VOID *_pbData = 0;'
155             print '    _pbData = _maps[_this];'
156             print '    if (_pbData) {'
157             print '        retrace::delRegionByPointer(_pbData);'
158             print '    }'
159
160
161 if __name__ == '__main__':
162     print r'''
163 #include <string.h>
164
165 #include <iostream>
166
167 #include "d3d9imports.hpp"
168 #include "d3d9size.hpp"
169 #include "d3dretrace.hpp"
170 #include "d3d9state.hpp"
171
172 '''
173
174     api = API()
175     api.addModule(d3d9)
176     retracer = D3DRetracer()
177     retracer.retraceApi(api)