]> git.cworth.org Git - apitrace/blob - retrace/dxgiretrace.py
d3dretrace: Use consistent filenames.
[apitrace] / retrace / dxgiretrace.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 from specs.dxgi import dxgi
34 from specs.d3d10 import d3d10
35 from specs.d3d10_1 import d3d10_1
36 from specs.d3d11 import d3d11
37
38
39 class D3DRetracer(Retracer):
40
41     def retraceApi(self, api):
42         print '// Swizzling mapping for lock addresses'
43         print 'static std::map<void *, void *> _maps;'
44         print
45         print r'''
46 static void 
47 createWindow(DXGI_SWAP_CHAIN_DESC *pSwapChainDesc) {
48     if (pSwapChainDesc->Windowed) {
49         UINT Width  = pSwapChainDesc->BufferDesc.Width;
50         UINT Height = pSwapChainDesc->BufferDesc.Height;
51         if (!Width)  Width = 1024;
52         if (!Height) Height = 768;
53         pSwapChainDesc->OutputWindow = d3dretrace::createWindow(Width, Height);
54     }
55 }
56 '''
57
58         self.table_name = 'd3dretrace::d3d10_callbacks'
59
60         Retracer.retraceApi(self, api)
61
62     createDeviceFunctionNames = [
63         "D3D10CreateDevice",
64         "D3D10CreateDeviceAndSwapChain",
65         "D3D10CreateDevice1",
66         "D3D10CreateDeviceAndSwapChain1",
67         "D3D11CreateDevice",
68         "D3D11CreateDeviceAndSwapChain",
69     ]
70
71     def invokeFunction(self, function):
72         if function.name in self.createDeviceFunctionNames:
73             # create windows as neccessary
74             if 'pSwapChainDesc' in function.argNames():
75                 print r'    createWindow(pSwapChainDesc);'
76
77             # Compensate for the fact we don't trace the software renderer
78             # module LoadLibrary call
79             if 'Software' in function.argNames():
80                 print r'    if (Software) {'
81                 print r'        retrace::warning(call) << "using WARP for software device\n";'
82                 print r'        Software = LoadLibraryA("d3d10warp");'
83                 print r'    }'
84
85             # Compensate for the fact we don't trace DXGI object creation
86             if function.name.startswith('D3D11CreateDevice'):
87                 print r'    if (DriverType == D3D_DRIVER_TYPE_UNKNOWN && !pAdapter) {'
88                 print r'        DriverType = D3D_DRIVER_TYPE_HARDWARE;'
89                 print r'    }'
90
91         Retracer.invokeFunction(self, function)
92
93
94     def invokeInterfaceMethod(self, interface, method):
95         # keep track of the last used device for state dumping
96         if interface.name in ('ID3D10Device', 'ID3D10Device1'):
97             if method.name == 'Release':
98                 print r'    d3d10Dumper.unbindDevice(_this);'
99             else:
100                 print r'    d3d10Dumper.bindDevice(_this);'
101         if interface.name in ('ID3D11DeviceContext',):
102             if method.name == 'Release':
103                 print r'    d3d11Dumper.unbindDevice(_this);'
104             else:
105                 print r'    d3d11Dumper.bindDevice(_this);'
106
107         # create windows as neccessary
108         if method.name == 'CreateSwapChain':
109             print r'    createWindow(pDesc);'
110
111         # notify frame has been completed
112         if method.name == 'Present':
113             print r'    retrace::frameComplete(call);'
114
115         if 'pSharedResource' in method.argNames():
116             print r'    if (pSharedResource) {'
117             print r'        retrace::warning(call) << "shared surfaces unsupported\n";'
118             print r'        pSharedResource = NULL;'
119             print r'    }'
120
121         Retracer.invokeInterfaceMethod(self, interface, method)
122
123         # process events after presents
124         if method.name == 'Present':
125             print r'    d3dretrace::processEvents();'
126
127         if method.name == 'Map':
128             print '    VOID *_pbData = NULL;'
129             print '    size_t _MappedSize = 0;'
130             print '    _getMapInfo(_this, %s, _pbData, _MappedSize);' % ', '.join(method.argNames())
131             print '    if (_MappedSize) {'
132             print '        _maps[_this] = _pbData;'
133             print '    } else {'
134             print '        return;'
135             print '    }'
136         
137         if method.name == 'Unmap':
138             print '    VOID *_pbData = 0;'
139             print '    _pbData = _maps[_this];'
140             print '    if (_pbData) {'
141             print '        retrace::delRegionByPointer(_pbData);'
142             print '        _maps[_this] = 0;'
143             print '    }'
144
145
146 def main():
147     print r'''#include <string.h>'''
148     print
149     print r'#include <iostream>'
150     print
151     print r'#include "d3dretrace.hpp"'
152     print
153
154     moduleNames = sys.argv[1:]
155
156     api = API()
157     
158     if moduleNames:
159         api.addModule(dxgi)
160     
161     if 'd3d10' in moduleNames:
162         if 'd3d10_1' in moduleNames:
163             print r'#include "d3d10_1imports.hpp"'
164             # D3D10CreateBlob is duplicated in d3d10 and d3d10_1
165             d3d10_1.functions = [function for function in d3d10_1.functions if function.name != 'D3D10CreateBlob']
166             api.addModule(d3d10_1)
167         else:
168             print r'#include "d3d10imports.hpp"'
169         print r'#include "d3d10size.hpp"'
170         api.addModule(d3d10)
171         print
172         print '''static d3dretrace::D3DDumper<ID3D10Device> d3d10Dumper;'''
173         print
174
175     if 'd3d11' in moduleNames:
176         print r'#include "d3d11imports.hpp"'
177         if 'd3d11_1' in moduleNames:
178             print '#include <d3d11_1.h>'
179             import specs.d3d11_1
180         print r'#include "d3d11size.hpp"'
181         print r'#include "d3dstate.hpp"'
182         api.addModule(d3d11)
183         
184         print
185         print '''static d3dretrace::D3DDumper<ID3D11DeviceContext> d3d11Dumper;'''
186         print
187
188     retracer = D3DRetracer()
189     retracer.retraceApi(api)
190
191
192 if __name__ == '__main__':
193     main()