José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 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 | |
José Fonseca | 6f81033 | 2012-11-11 10:05:09 +0000 | [diff] [blame] | 30 | import sys |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 31 | from dllretrace import DllRetracer as Retracer |
José Fonseca | 6f81033 | 2012-11-11 10:05:09 +0000 | [diff] [blame] | 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 |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 37 | |
| 38 | |
| 39 | class D3DRetracer(Retracer): |
| 40 | |
José Fonseca | e381485 | 2012-11-11 09:45:06 +0000 | [diff] [blame] | 41 | def retraceApi(self, api): |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 42 | print '// Swizzling mapping for lock addresses' |
| 43 | print 'static std::map<void *, void *> _maps;' |
| 44 | print |
| 45 | |
José Fonseca | fbcb5d9 | 2012-11-13 09:28:23 +0000 | [diff] [blame] | 46 | self.table_name = 'd3dretrace::d3d10_callbacks' |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 47 | |
José Fonseca | e381485 | 2012-11-11 09:45:06 +0000 | [diff] [blame] | 48 | Retracer.retraceApi(self, api) |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 49 | |
José Fonseca | 4f49d21 | 2012-11-14 14:02:35 +0000 | [diff] [blame^] | 50 | createDeviceFunctionNames = [ |
| 51 | "D3D10CreateDevice", |
| 52 | "D3D10CreateDeviceAndSwapChain", |
| 53 | "D3D10CreateDevice1", |
| 54 | "D3D10CreateDeviceAndSwapChain1", |
| 55 | "D3D11CreateDevice", |
| 56 | "D3D11CreateDeviceAndSwapChain", |
| 57 | ] |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 58 | |
José Fonseca | 4f49d21 | 2012-11-14 14:02:35 +0000 | [diff] [blame^] | 59 | def invokeFunction(self, function): |
| 60 | if function.name in self.createDeviceFunctionNames: |
| 61 | # create windows as neccessary |
| 62 | if 'pSwapChainDesc' in function.argNames(): |
| 63 | print r' pSwapChainDesc->OutputWindow = d3dretrace::createWindow(512, 512);' |
| 64 | |
| 65 | # Compensate for the fact we don't trace the software renderer |
| 66 | # module LoadLibrary call |
| 67 | if 'Software' in function.argNames(): |
| 68 | print r' if (Software) {' |
| 69 | print r' retrace::warning(call) << "using WARP for software device\n";' |
| 70 | print r' Software = LoadLibraryA("d3d10warp");' |
| 71 | print r' }' |
| 72 | |
| 73 | # Compensate for the fact we don't trace DXGI object creation |
| 74 | if function.name.startswith('D3D11CreateDevice'): |
| 75 | print r' if (DriverType == D3D_DRIVER_TYPE_UNKNOWN && !pAdapter) {' |
| 76 | print r' DriverType = D3D_DRIVER_TYPE_HARDWARE;' |
| 77 | print r' }' |
José Fonseca | ea79919 | 2012-11-13 21:37:24 +0000 | [diff] [blame] | 78 | |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 79 | Retracer.invokeFunction(self, function) |
| 80 | |
José Fonseca | ea79919 | 2012-11-13 21:37:24 +0000 | [diff] [blame] | 81 | |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 82 | def invokeInterfaceMethod(self, interface, method): |
| 83 | # keep track of the last used device for state dumping |
José Fonseca | 5773beb | 2012-11-14 11:46:58 +0000 | [diff] [blame] | 84 | if interface.name in ('ID3D11DeviceContext',): |
| 85 | if method.name == 'Release': |
| 86 | print r' d3d11Dumper.unbindDevice(_this);' |
| 87 | else: |
| 88 | print r' d3d11Dumper.bindDevice(_this);' |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 89 | |
| 90 | # create windows as neccessary |
| 91 | if method.name == 'CreateSwapChain': |
| 92 | print r' pDesc->OutputWindow = d3dretrace::createWindow(512, 512);' |
| 93 | |
| 94 | # notify frame has been completed |
| 95 | if method.name == 'Present': |
| 96 | print r' retrace::frameComplete(call);' |
| 97 | |
| 98 | if 'pSharedResource' in method.argNames(): |
| 99 | print r' if (pSharedResource) {' |
| 100 | print r' retrace::warning(call) << "shared surfaces unsupported\n";' |
| 101 | print r' pSharedResource = NULL;' |
| 102 | print r' }' |
| 103 | |
| 104 | Retracer.invokeInterfaceMethod(self, interface, method) |
| 105 | |
| 106 | # process events after presents |
| 107 | if method.name == 'Present': |
| 108 | print r' d3dretrace::processEvents();' |
| 109 | |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 110 | if method.name == 'Map': |
| 111 | print ' VOID *_pbData = NULL;' |
| 112 | print ' size_t _MappedSize = 0;' |
| 113 | print ' _getMapInfo(_this, %s, _pbData, _MappedSize);' % ', '.join(method.argNames()) |
José Fonseca | 4f49d21 | 2012-11-14 14:02:35 +0000 | [diff] [blame^] | 114 | print ' if (_MappedSize) {' |
| 115 | print ' _maps[_this] = _pbData;' |
| 116 | print ' } else {' |
| 117 | print ' return;' |
| 118 | print ' }' |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 119 | |
| 120 | if method.name == 'Unmap': |
| 121 | print ' VOID *_pbData = 0;' |
| 122 | print ' _pbData = _maps[_this];' |
| 123 | print ' if (_pbData) {' |
| 124 | print ' retrace::delRegionByPointer(_pbData);' |
José Fonseca | 4f49d21 | 2012-11-14 14:02:35 +0000 | [diff] [blame^] | 125 | print ' _maps[_this] = 0;' |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 126 | print ' }' |
José Fonseca | 6f81033 | 2012-11-11 10:05:09 +0000 | [diff] [blame] | 127 | |
| 128 | |
| 129 | def main(): |
| 130 | print r'''#include <string.h>''' |
| 131 | print |
| 132 | print r'#include <iostream>' |
| 133 | print |
| 134 | print r'#include "d3dretrace.hpp"' |
| 135 | print |
| 136 | |
| 137 | moduleNames = sys.argv[1:] |
| 138 | |
| 139 | api = API() |
| 140 | if moduleNames: |
| 141 | api.addModule(dxgi) |
| 142 | if 'd3d10' in moduleNames: |
| 143 | if 'd3d10_1' in moduleNames: |
| 144 | print r'#include "d3d10_1imports.hpp"' |
| 145 | # D3D10CreateBlob is duplicated in d3d10 and d3d10_1 |
| 146 | d3d10_1.functions = [function for function in d3d10_1.functions if function.name != 'D3D10CreateBlob'] |
| 147 | api.addModule(d3d10_1) |
| 148 | else: |
| 149 | print r'#include "d3d10imports.hpp"' |
| 150 | print r'#include "d3d10size.hpp"' |
| 151 | api.addModule(d3d10) |
| 152 | if 'd3d11' in moduleNames: |
| 153 | print r'#include "d3d11imports.hpp"' |
| 154 | if 'd3d11_1' in moduleNames: |
| 155 | print '#include <d3d11_1.h>' |
| 156 | import specs.d3d11_1 |
| 157 | print r'#include "d3d11size.hpp"' |
José Fonseca | 5773beb | 2012-11-14 11:46:58 +0000 | [diff] [blame] | 158 | print r'#include "d3dstate.hpp"' |
José Fonseca | 6f81033 | 2012-11-11 10:05:09 +0000 | [diff] [blame] | 159 | api.addModule(d3d11) |
José Fonseca | 5773beb | 2012-11-14 11:46:58 +0000 | [diff] [blame] | 160 | |
| 161 | print |
| 162 | print '''static d3dretrace::D3DDumper<ID3D11DeviceContext> d3d11Dumper;''' |
| 163 | print |
José Fonseca | 6f81033 | 2012-11-11 10:05:09 +0000 | [diff] [blame] | 164 | |
| 165 | retracer = D3DRetracer() |
| 166 | retracer.retraceApi(api) |
| 167 | |
| 168 | |
| 169 | if __name__ == '__main__': |
| 170 | main() |