]> git.cworth.org Git - apitrace/blob - retrace/d3d9retrace.py
retrace: Implement glxCopySubBufferMESA
[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 import sys
31 from dllretrace import DllRetracer as Retracer
32 from specs.stdapi import API
33
34
35 class D3DRetracer(Retracer):
36
37     def retraceApi(self, api):
38         print '// Swizzling mapping for lock addresses'
39         print 'static std::map<void *, void *> _maps;'
40         print
41
42         Retracer.retraceApi(self, api)
43
44     def invokeFunction(self, function):
45         if function.name in ('Direct3DCreate9', 'Direct3DCreate9Ex'):
46             print 'if (retrace::debug && !g_szD3D9DllName) {'
47             print '    /* '
48             print '     * XXX: D3D9D only works for simple things, it often introduces errors'
49             print '     * on complex traces, or traces which use unofficial D3D9 features.'
50             print '     */'
51             print '    if (0) {'
52             print '        g_szD3D9DllName = "d3d9d.dll";'
53             print '    }'
54             print '}'
55
56         Retracer.invokeFunction(self, function)
57
58     def invokeInterfaceMethod(self, interface, method):
59         # keep track of the last used device for state dumping
60         if interface.name in ('IDirect3DDevice9', 'IDirect3DDevice9Ex'):
61             if method.name == 'Release':
62                 print r'    if (call.ret->toUInt() == 0) {'
63                 print r'        d3d9Dumper.unbindDevice(_this);'
64                 print r'    }'
65             else:
66                 print r'    d3d9Dumper.bindDevice(_this);'
67         if interface.name in ('IDirect3DDevice8', 'IDirect3DDevice8Ex'):
68             if method.name == 'Release':
69                 print r'    if (call.ret->toUInt() == 0) {'
70                 print r'        d3d8Dumper.unbindDevice(_this);'
71                 print r'    }'
72             else:
73                 print r'    d3d8Dumper.bindDevice(_this);'
74
75         # create windows as neccessary
76         if method.name in ('CreateDevice', 'CreateDeviceEx', 'CreateAdditionalSwapChain'):
77             print r'    HWND hWnd = d3dretrace::createWindow(pPresentationParameters->BackBufferWidth, pPresentationParameters->BackBufferHeight);'
78             print r'    pPresentationParameters->hDeviceWindow = hWnd;'
79             if 'hFocusWindow' in method.argNames():
80                 print r'    hFocusWindow = hWnd;'
81         
82         if method.name in ('CreateDevice', 'CreateDeviceEx'):
83             print r'    switch (retrace::driver) {'
84             print r'    case retrace::DRIVER_HARDWARE:'
85             print r'        DeviceType = D3DDEVTYPE_HAL;'
86             print r'        break;'
87             print r'    case retrace::DRIVER_SOFTWARE:'
88             print r'    case retrace::DRIVER_REFERENCE:'
89             print r'        DeviceType = D3DDEVTYPE_REF;'
90             print r'        break;'
91             print r'    case retrace::DRIVER_NULL:'
92             if interface.name.startswith('IDirect3D9'):
93                 print r'        DeviceType = D3DDEVTYPE_NULLREF;'
94             else:
95                 print r'        retrace::warning(call) << "null driver not supported\n";'
96             print r'        break;'
97             print r'    case retrace::DRIVER_MODULE:'
98             print r'        retrace::warning(call) << "driver module not supported\n";'
99             print r'        break;'
100             print r'    default:'
101             print r'        assert(0);'
102             print r'        /* fall-through */'
103             print r'    case retrace::DRIVER_DEFAULT:'
104             print r'        break;'
105             print r'    }'
106
107         if method.name in ('Reset', 'ResetEx'):
108             print r'    if (pPresentationParameters->Windowed) {'
109             print r'        d3dretrace::resizeWindow(pPresentationParameters->hDeviceWindow, pPresentationParameters->BackBufferWidth, pPresentationParameters->BackBufferHeight);'
110             print r'    }'
111
112         # notify frame has been completed
113         if method.name in ('Present', 'PresentEx'):
114             print r'    retrace::frameComplete(call);'
115             print r'    hDestWindowOverride = NULL;'
116
117         if 'pSharedHandle' in method.argNames():
118             print r'    if (pSharedHandle) {'
119             print r'        retrace::warning(call) << "shared surfaces unsupported\n";'
120             print r'        pSharedHandle = NULL;'
121             print r'    }'
122
123         Retracer.invokeInterfaceMethod(self, interface, method)
124
125         # process events after presents
126         if method.name == 'Present':
127             print r'    d3dretrace::processEvents();'
128
129         if method.name in ('Lock', 'LockRect', 'LockBox'):
130             print '    VOID *_pbData = NULL;'
131             print '    size_t _MappedSize = 0;'
132             print '    _getMapInfo(_this, %s, _pbData, _MappedSize);' % ', '.join(method.argNames()[:-1])
133             print '    if (_MappedSize) {'
134             print '        _maps[_this] = _pbData;'
135             print '    } else {'
136             print '        return;'
137             print '    }'
138         
139         if method.name in ('Unlock', 'UnlockRect', 'UnlockBox'):
140             print '    VOID *_pbData = 0;'
141             print '    _pbData = _maps[_this];'
142             print '    if (_pbData) {'
143             print '        retrace::delRegionByPointer(_pbData);'
144             print '        _maps[_this] = 0;'
145             print '    }'
146
147
148 def main():
149     print r'#include <string.h>'
150     print
151     print r'#include <iostream>'
152     print
153     print r'#include "d3dretrace.hpp"'
154     print
155
156     moduleName = sys.argv[1]
157     support = int(sys.argv[2])
158
159     api = API()
160     
161     if support:
162         if moduleName == 'd3d9':
163             from specs.d3d9 import d3d9
164             print r'#include "d3d9imports.hpp"'
165             print r'#include "d3d9size.hpp"'
166             api.addModule(d3d9)
167             print
168             print '''static d3dretrace::D3DDumper<IDirect3DDevice9> d3d9Dumper;'''
169             print
170         elif moduleName == 'd3d8':
171             from specs.d3d8 import d3d8
172             print r'#include "d3d8imports.hpp"'
173             print r'#include "d3d8size.hpp"'
174             api.addModule(d3d8)
175             print
176             print '''static d3dretrace::D3DDumper<IDirect3DDevice8> d3d8Dumper;'''
177             print
178         else:
179             assert False
180
181     retracer = D3DRetracer()
182     retracer.table_name = 'd3dretrace::%s_callbacks' % moduleName
183     retracer.retraceApi(api)
184
185
186 if __name__ == '__main__':
187     main()