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