blob: c7baaf280b701e66f04b3223d97949fd60acd4b1 [file] [log] [blame]
José Fonseca610942b2012-11-08 10:46:03 +00001##########################################################################
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
José Fonseca6f810332012-11-11 10:05:09 +000030import sys
José Fonseca610942b2012-11-08 10:46:03 +000031from dllretrace import DllRetracer as Retracer
José Fonseca6f810332012-11-11 10:05:09 +000032from specs.stdapi import API
33from specs.dxgi import dxgi
34from specs.d3d10 import d3d10
35from specs.d3d10_1 import d3d10_1
36from specs.d3d11 import d3d11
José Fonseca610942b2012-11-08 10:46:03 +000037
38
39class D3DRetracer(Retracer):
40
José Fonsecae3814852012-11-11 09:45:06 +000041 def retraceApi(self, api):
José Fonseca610942b2012-11-08 10:46:03 +000042 print '// Swizzling mapping for lock addresses'
43 print 'static std::map<void *, void *> _maps;'
44 print
José Fonseca7d042e02012-11-23 17:01:04 +000045 print r'''
46static void
47createWindow(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'''
José Fonseca610942b2012-11-08 10:46:03 +000057
José Fonseca73341c22012-11-24 13:04:42 +000058 self.table_name = 'd3dretrace::dxgi_callbacks'
José Fonseca610942b2012-11-08 10:46:03 +000059
José Fonsecae3814852012-11-11 09:45:06 +000060 Retracer.retraceApi(self, api)
José Fonseca610942b2012-11-08 10:46:03 +000061
José Fonseca4f49d212012-11-14 14:02:35 +000062 createDeviceFunctionNames = [
63 "D3D10CreateDevice",
64 "D3D10CreateDeviceAndSwapChain",
65 "D3D10CreateDevice1",
66 "D3D10CreateDeviceAndSwapChain1",
67 "D3D11CreateDevice",
68 "D3D11CreateDeviceAndSwapChain",
69 ]
José Fonseca610942b2012-11-08 10:46:03 +000070
José Fonseca4f49d212012-11-14 14:02:35 +000071 def invokeFunction(self, function):
72 if function.name in self.createDeviceFunctionNames:
73 # create windows as neccessary
74 if 'pSwapChainDesc' in function.argNames():
José Fonseca7d042e02012-11-23 17:01:04 +000075 print r' createWindow(pSwapChainDesc);'
José Fonseca4f49d212012-11-14 14:02:35 +000076
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' }'
José Fonsecaea799192012-11-13 21:37:24 +000090
José Fonseca610942b2012-11-08 10:46:03 +000091 Retracer.invokeFunction(self, function)
92
93 def invokeInterfaceMethod(self, interface, method):
94 # keep track of the last used device for state dumping
José Fonseca944088e2012-11-20 14:47:03 +000095 if interface.name in ('ID3D10Device', 'ID3D10Device1'):
96 if method.name == 'Release':
97 print r' d3d10Dumper.unbindDevice(_this);'
98 else:
99 print r' d3d10Dumper.bindDevice(_this);'
José Fonseca5773beb2012-11-14 11:46:58 +0000100 if interface.name in ('ID3D11DeviceContext',):
101 if method.name == 'Release':
102 print r' d3d11Dumper.unbindDevice(_this);'
103 else:
104 print r' d3d11Dumper.bindDevice(_this);'
José Fonseca610942b2012-11-08 10:46:03 +0000105
106 # create windows as neccessary
107 if method.name == 'CreateSwapChain':
José Fonseca7d042e02012-11-23 17:01:04 +0000108 print r' createWindow(pDesc);'
José Fonseca610942b2012-11-08 10:46:03 +0000109
110 # notify frame has been completed
111 if method.name == 'Present':
112 print r' retrace::frameComplete(call);'
113
114 if 'pSharedResource' in method.argNames():
115 print r' if (pSharedResource) {'
116 print r' retrace::warning(call) << "shared surfaces unsupported\n";'
117 print r' pSharedResource = NULL;'
118 print r' }'
119
120 Retracer.invokeInterfaceMethod(self, interface, method)
121
122 # process events after presents
123 if method.name == 'Present':
124 print r' d3dretrace::processEvents();'
125
José Fonseca610942b2012-11-08 10:46:03 +0000126 if method.name == 'Map':
127 print ' VOID *_pbData = NULL;'
128 print ' size_t _MappedSize = 0;'
129 print ' _getMapInfo(_this, %s, _pbData, _MappedSize);' % ', '.join(method.argNames())
José Fonseca4f49d212012-11-14 14:02:35 +0000130 print ' if (_MappedSize) {'
131 print ' _maps[_this] = _pbData;'
132 print ' } else {'
133 print ' return;'
134 print ' }'
José Fonseca610942b2012-11-08 10:46:03 +0000135
136 if method.name == 'Unmap':
137 print ' VOID *_pbData = 0;'
138 print ' _pbData = _maps[_this];'
139 print ' if (_pbData) {'
140 print ' retrace::delRegionByPointer(_pbData);'
José Fonseca4f49d212012-11-14 14:02:35 +0000141 print ' _maps[_this] = 0;'
José Fonseca610942b2012-11-08 10:46:03 +0000142 print ' }'
José Fonseca6f810332012-11-11 10:05:09 +0000143
144
145def main():
José Fonseca73341c22012-11-24 13:04:42 +0000146 print r'#include <string.h>'
José Fonseca6f810332012-11-11 10:05:09 +0000147 print
148 print r'#include <iostream>'
149 print
150 print r'#include "d3dretrace.hpp"'
151 print
152
153 moduleNames = sys.argv[1:]
154
155 api = API()
José Fonseca944088e2012-11-20 14:47:03 +0000156
José Fonseca6f810332012-11-11 10:05:09 +0000157 if moduleNames:
158 api.addModule(dxgi)
José Fonseca944088e2012-11-20 14:47:03 +0000159
José Fonseca6f810332012-11-11 10:05:09 +0000160 if 'd3d10' in moduleNames:
161 if 'd3d10_1' in moduleNames:
162 print r'#include "d3d10_1imports.hpp"'
163 # D3D10CreateBlob is duplicated in d3d10 and d3d10_1
164 d3d10_1.functions = [function for function in d3d10_1.functions if function.name != 'D3D10CreateBlob']
165 api.addModule(d3d10_1)
166 else:
167 print r'#include "d3d10imports.hpp"'
168 print r'#include "d3d10size.hpp"'
169 api.addModule(d3d10)
José Fonseca944088e2012-11-20 14:47:03 +0000170 print
171 print '''static d3dretrace::D3DDumper<ID3D10Device> d3d10Dumper;'''
172 print
173
José Fonseca6f810332012-11-11 10:05:09 +0000174 if 'd3d11' in moduleNames:
175 print r'#include "d3d11imports.hpp"'
176 if 'd3d11_1' in moduleNames:
177 print '#include <d3d11_1.h>'
178 import specs.d3d11_1
179 print r'#include "d3d11size.hpp"'
José Fonseca5773beb2012-11-14 11:46:58 +0000180 print r'#include "d3dstate.hpp"'
José Fonseca6f810332012-11-11 10:05:09 +0000181 api.addModule(d3d11)
José Fonseca5773beb2012-11-14 11:46:58 +0000182
183 print
184 print '''static d3dretrace::D3DDumper<ID3D11DeviceContext> d3d11Dumper;'''
185 print
José Fonseca6f810332012-11-11 10:05:09 +0000186
187 retracer = D3DRetracer()
188 retracer.retraceApi(api)
189
190
191if __name__ == '__main__':
192 main()