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 | 75cbb8c | 2013-02-12 16:19:23 +0000 | [diff] [blame] | 32 | import specs.dxgi |
José Fonseca | 6f81033 | 2012-11-11 10:05:09 +0000 | [diff] [blame] | 33 | from specs.stdapi import API |
José Fonseca | 3be2c67 | 2015-02-06 15:36:40 +0000 | [diff] [blame] | 34 | from specs.winapi import LPCSTR |
José Fonseca | 6f81033 | 2012-11-11 10:05:09 +0000 | [diff] [blame] | 35 | from specs.dxgi import dxgi |
Jose Fonseca | a92e0c4 | 2015-03-14 10:49:23 +0000 | [diff] [blame] | 36 | from specs.d3d10 import d3d10, d3d10_1 |
José Fonseca | 6f81033 | 2012-11-11 10:05:09 +0000 | [diff] [blame] | 37 | from specs.d3d11 import d3d11 |
Jose Fonseca | feb8f7d | 2016-02-01 14:14:00 +0000 | [diff] [blame] | 38 | from specs.dcomp import dcomp |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 39 | |
| 40 | |
| 41 | class D3DRetracer(Retracer): |
| 42 | |
José Fonseca | e381485 | 2012-11-11 09:45:06 +0000 | [diff] [blame] | 43 | def retraceApi(self, api): |
José Fonseca | 6ca2008 | 2014-10-07 21:39:43 +0100 | [diff] [blame] | 44 | print '// Swizzling mapping for lock addresses, mapping a (pDeviceContext, pResource, Subresource) -> void *' |
| 45 | print 'typedef std::pair< IUnknown *, UINT > SubresourceKey;' |
| 46 | print 'static std::map< IUnknown *, std::map< SubresourceKey, void * > > g_Maps;' |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 47 | print |
José Fonseca | 73341c2 | 2012-11-24 13:04:42 +0000 | [diff] [blame] | 48 | self.table_name = 'd3dretrace::dxgi_callbacks' |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 49 | |
José Fonseca | e381485 | 2012-11-11 09:45:06 +0000 | [diff] [blame] | 50 | Retracer.retraceApi(self, api) |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 51 | |
José Fonseca | 4f49d21 | 2012-11-14 14:02:35 +0000 | [diff] [blame] | 52 | createDeviceFunctionNames = [ |
| 53 | "D3D10CreateDevice", |
| 54 | "D3D10CreateDeviceAndSwapChain", |
| 55 | "D3D10CreateDevice1", |
| 56 | "D3D10CreateDeviceAndSwapChain1", |
| 57 | "D3D11CreateDevice", |
| 58 | "D3D11CreateDeviceAndSwapChain", |
| 59 | ] |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 60 | |
José Fonseca | 4f49d21 | 2012-11-14 14:02:35 +0000 | [diff] [blame] | 61 | def invokeFunction(self, function): |
| 62 | if function.name in self.createDeviceFunctionNames: |
| 63 | # create windows as neccessary |
| 64 | if 'pSwapChainDesc' in function.argNames(): |
José Fonseca | 1e309b4 | 2014-09-18 11:08:09 +0100 | [diff] [blame] | 65 | print r' d3dretrace::createWindowForSwapChain(pSwapChainDesc);' |
José Fonseca | 4f49d21 | 2012-11-14 14:02:35 +0000 | [diff] [blame] | 66 | |
José Fonseca | 4f49d21 | 2012-11-14 14:02:35 +0000 | [diff] [blame] | 67 | # Compensate for the fact we don't trace DXGI object creation |
| 68 | if function.name.startswith('D3D11CreateDevice'): |
| 69 | print r' if (DriverType == D3D_DRIVER_TYPE_UNKNOWN && !pAdapter) {' |
| 70 | print r' DriverType = D3D_DRIVER_TYPE_HARDWARE;' |
| 71 | print r' }' |
José Fonseca | ea79919 | 2012-11-13 21:37:24 +0000 | [diff] [blame] | 72 | |
José Fonseca | 42b89fc | 2012-11-27 12:19:45 +0000 | [diff] [blame] | 73 | if function.name.startswith('D3D10CreateDevice'): |
José Fonseca | 7bcc96c | 2012-12-05 19:28:22 +0000 | [diff] [blame] | 74 | # Toggle debugging |
José Fonseca | 7bcc96c | 2012-12-05 19:28:22 +0000 | [diff] [blame] | 75 | print r' if (retrace::debug) {' |
Jose Fonseca | f39fd60 | 2017-08-23 15:13:39 +0100 | [diff] [blame^] | 76 | print r' Flags |= D3D10_CREATE_DEVICE_DEBUG;' |
| 77 | print r' } else {' |
| 78 | print r' Flags &= ~D3D10_CREATE_DEVICE_DEBUG;' |
José Fonseca | 7bcc96c | 2012-12-05 19:28:22 +0000 | [diff] [blame] | 79 | print r' }' |
| 80 | |
| 81 | # Force driver |
José Fonseca | 42b89fc | 2012-11-27 12:19:45 +0000 | [diff] [blame] | 82 | self.forceDriver('D3D10_DRIVER_TYPE') |
José Fonseca | 7bcc96c | 2012-12-05 19:28:22 +0000 | [diff] [blame] | 83 | |
José Fonseca | 42b89fc | 2012-11-27 12:19:45 +0000 | [diff] [blame] | 84 | if function.name.startswith('D3D11CreateDevice'): |
José Fonseca | 7bcc96c | 2012-12-05 19:28:22 +0000 | [diff] [blame] | 85 | # Toggle debugging |
José Fonseca | 7bcc96c | 2012-12-05 19:28:22 +0000 | [diff] [blame] | 86 | print r' if (retrace::debug) {' |
Jose Fonseca | f39fd60 | 2017-08-23 15:13:39 +0100 | [diff] [blame^] | 87 | print r' Flags |= D3D11_CREATE_DEVICE_DEBUG;' |
| 88 | print r' } else {' |
| 89 | print r' Flags &= ~D3D11_CREATE_DEVICE_DEBUG;' |
José Fonseca | 7bcc96c | 2012-12-05 19:28:22 +0000 | [diff] [blame] | 90 | print r' }' |
| 91 | |
| 92 | # Force driver |
José Fonseca | 42b89fc | 2012-11-27 12:19:45 +0000 | [diff] [blame] | 93 | self.forceDriver('D3D_DRIVER_TYPE') |
| 94 | |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 95 | Retracer.invokeFunction(self, function) |
| 96 | |
Jose Fonseca | f39fd60 | 2017-08-23 15:13:39 +0100 | [diff] [blame^] | 97 | def doInvokeFunction(self, function): |
| 98 | Retracer.doInvokeFunction(self, function) |
| 99 | |
| 100 | # Handle missing debug layer. While it's possible to detect whether |
| 101 | # the debug layers are present, by creating a null device, and checking |
| 102 | # the result. It's simpler to retry. |
| 103 | if function.name.startswith('D3D10CreateDevice'): |
| 104 | print r' if (_result == E_FAIL && (Flags & D3D10_CREATE_DEVICE_DEBUG)) {' |
| 105 | print r' retrace::warning(call) << "Direct3D 10.x SDK Debug Layer (d3d10sdklayers.dll) not available, continuing without debug output\n";' |
| 106 | print r' Flags &= ~D3D10_CREATE_DEVICE_DEBUG;' |
| 107 | Retracer.doInvokeFunction(self, function) |
| 108 | print r' }' |
| 109 | if function.name.startswith('D3D11CreateDevice'): |
| 110 | print r' if (_result == E_FAIL && (Flags & D3D11_CREATE_DEVICE_DEBUG)) {' |
| 111 | print r' retrace::warning(call) << "Direct3D 11.x SDK Debug Layer (d3d11*sdklayers.dll) not available, continuing without debug output\n";' |
| 112 | print r' Flags &= ~D3D11_CREATE_DEVICE_DEBUG;' |
| 113 | Retracer.doInvokeFunction(self, function) |
| 114 | print r' }' |
| 115 | |
Jose Fonseca | d0094a2 | 2017-05-30 12:31:37 +0100 | [diff] [blame] | 116 | def handleFailure(self, interface, methodOrFunction): |
Jose Fonseca | 5c487ec | 2015-06-02 22:16:52 +0100 | [diff] [blame] | 117 | # Catch when device is removed, and report the reason. |
Jose Fonseca | d0094a2 | 2017-05-30 12:31:37 +0100 | [diff] [blame] | 118 | if interface is not None: |
| 119 | getDeviceRemovedReasonMethod = interface.getMethodByName("GetDeviceRemovedReason") |
| 120 | if getDeviceRemovedReasonMethod is not None: |
| 121 | print r' if (_result == DXGI_ERROR_DEVICE_REMOVED) {' |
| 122 | print r' HRESULT _reason = _this->GetDeviceRemovedReason();' |
| 123 | print r' retrace::failed(call, _reason);' |
| 124 | print r' exit(EXIT_FAILURE);' |
| 125 | print r' }' |
Jose Fonseca | 5c487ec | 2015-06-02 22:16:52 +0100 | [diff] [blame] | 126 | |
Jose Fonseca | d0094a2 | 2017-05-30 12:31:37 +0100 | [diff] [blame] | 127 | Retracer.handleFailure(self, interface, methodOrFunction) |
Jose Fonseca | 5c487ec | 2015-06-02 22:16:52 +0100 | [diff] [blame] | 128 | |
José Fonseca | 42b89fc | 2012-11-27 12:19:45 +0000 | [diff] [blame] | 129 | def forceDriver(self, enum): |
José Fonseca | 82ea89d | 2012-12-05 19:34:26 +0000 | [diff] [blame] | 130 | # This can only work when pAdapter is NULL. For non-NULL pAdapter we |
| 131 | # need to override inside the EnumAdapters call below |
| 132 | print r' if (pAdapter == NULL) {' |
| 133 | print r' switch (retrace::driver) {' |
| 134 | print r' case retrace::DRIVER_HARDWARE:' |
| 135 | print r' DriverType = %s_HARDWARE;' % enum |
| 136 | print r' Software = NULL;' |
| 137 | print r' break;' |
| 138 | print r' case retrace::DRIVER_SOFTWARE:' |
| 139 | print r' DriverType = %s_WARP;' % enum |
| 140 | print r' Software = NULL;' |
| 141 | print r' break;' |
| 142 | print r' case retrace::DRIVER_REFERENCE:' |
| 143 | print r' DriverType = %s_REFERENCE;' % enum |
| 144 | print r' Software = NULL;' |
| 145 | print r' break;' |
| 146 | print r' case retrace::DRIVER_NULL:' |
| 147 | print r' DriverType = %s_NULL;' % enum |
| 148 | print r' Software = NULL;' |
| 149 | print r' break;' |
| 150 | print r' case retrace::DRIVER_MODULE:' |
| 151 | print r' DriverType = %s_SOFTWARE;' % enum |
| 152 | print r' Software = LoadLibraryA(retrace::driverModule);' |
José Fonseca | 42b89fc | 2012-11-27 12:19:45 +0000 | [diff] [blame] | 153 | print r' if (!Software) {' |
José Fonseca | 82ea89d | 2012-12-05 19:34:26 +0000 | [diff] [blame] | 154 | print r' retrace::warning(call) << "failed to load " << retrace::driverModule << "\n";' |
José Fonseca | 42b89fc | 2012-11-27 12:19:45 +0000 | [diff] [blame] | 155 | print r' }' |
José Fonseca | 82ea89d | 2012-12-05 19:34:26 +0000 | [diff] [blame] | 156 | print r' break;' |
| 157 | print r' default:' |
| 158 | print r' assert(0);' |
| 159 | print r' /* fall-through */' |
| 160 | print r' case retrace::DRIVER_DEFAULT:' |
| 161 | print r' if (DriverType == %s_SOFTWARE) {' % enum |
| 162 | print r' Software = LoadLibraryA("d3d10warp");' |
| 163 | print r' if (!Software) {' |
| 164 | print r' retrace::warning(call) << "failed to load d3d10warp.dll\n";' |
| 165 | print r' }' |
| 166 | print r' }' |
| 167 | print r' break;' |
José Fonseca | 42b89fc | 2012-11-27 12:19:45 +0000 | [diff] [blame] | 168 | print r' }' |
José Fonseca | 82ea89d | 2012-12-05 19:34:26 +0000 | [diff] [blame] | 169 | print r' } else {' |
| 170 | print r' Software = NULL;' |
José Fonseca | 42b89fc | 2012-11-27 12:19:45 +0000 | [diff] [blame] | 171 | print r' }' |
| 172 | |
Jose Fonseca | 2d78bef | 2016-05-19 15:10:23 +0100 | [diff] [blame] | 173 | def doInvokeInterfaceMethod(self, interface, method): |
| 174 | Retracer.doInvokeInterfaceMethod(self, interface, method) |
| 175 | |
| 176 | # Keep retrying ID3D11VideoContext::DecoderBeginFrame when returns E_PENDING |
| 177 | if interface.name == 'ID3D11VideoContext' and method.name == 'DecoderBeginFrame': |
| 178 | print r' while (_result == D3DERR_WASSTILLDRAWING || _result == E_PENDING) {' |
| 179 | print r' Sleep(1);' |
| 180 | Retracer.doInvokeInterfaceMethod(self, interface, method) |
| 181 | print r' }' |
| 182 | |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 183 | def invokeInterfaceMethod(self, interface, method): |
| 184 | # keep track of the last used device for state dumping |
José Fonseca | 944088e | 2012-11-20 14:47:03 +0000 | [diff] [blame] | 185 | if interface.name in ('ID3D10Device', 'ID3D10Device1'): |
| 186 | if method.name == 'Release': |
José Fonseca | db62f0f | 2013-06-27 16:15:01 +0100 | [diff] [blame] | 187 | print r' if (call.ret->toUInt() == 0) {' |
| 188 | print r' d3d10Dumper.unbindDevice(_this);' |
| 189 | print r' }' |
José Fonseca | 944088e | 2012-11-20 14:47:03 +0000 | [diff] [blame] | 190 | else: |
| 191 | print r' d3d10Dumper.bindDevice(_this);' |
Jose Fonseca | 4a1c7aa | 2015-08-10 16:53:49 +0100 | [diff] [blame] | 192 | if interface.name.startswith('ID3D11DeviceContext'): |
José Fonseca | 5773beb | 2012-11-14 11:46:58 +0000 | [diff] [blame] | 193 | if method.name == 'Release': |
José Fonseca | db62f0f | 2013-06-27 16:15:01 +0100 | [diff] [blame] | 194 | print r' if (call.ret->toUInt() == 0) {' |
| 195 | print r' d3d11Dumper.unbindDevice(_this);' |
| 196 | print r' }' |
José Fonseca | 5773beb | 2012-11-14 11:46:58 +0000 | [diff] [blame] | 197 | else: |
Jose Fonseca | b059dd8 | 2017-07-20 13:13:44 +0100 | [diff] [blame] | 198 | print r' d3d11Dumper.bindDevice(_this);' |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 199 | |
José Fonseca | 1e309b4 | 2014-09-18 11:08:09 +0100 | [diff] [blame] | 200 | # intercept private interfaces |
José Fonseca | d59843c | 2014-08-22 16:59:25 +0100 | [diff] [blame] | 201 | if method.name == 'QueryInterface': |
José Fonseca | c44c62c | 2014-09-22 13:42:42 +0100 | [diff] [blame] | 202 | print r' if (!d3dretrace::overrideQueryInterface(_this, riid, ppvObj, &_result)) {' |
José Fonseca | 75cbb8c | 2013-02-12 16:19:23 +0000 | [diff] [blame] | 203 | Retracer.invokeInterfaceMethod(self, interface, method) |
| 204 | print r' }' |
| 205 | return |
| 206 | |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 207 | # create windows as neccessary |
| 208 | if method.name == 'CreateSwapChain': |
José Fonseca | 1e309b4 | 2014-09-18 11:08:09 +0100 | [diff] [blame] | 209 | print r' d3dretrace::createWindowForSwapChain(pDesc);' |
Jose Fonseca | dafb4ea | 2015-08-10 14:40:22 +0100 | [diff] [blame] | 210 | if method.name == 'CreateSwapChainForHwnd': |
| 211 | print r' WindowHandle = d3dretrace::createWindow(pDesc->Width, pDesc->Height);' |
| 212 | print r' // DXGI_SCALING_NONE is only supported on Win8 and beyond' |
| 213 | print r' if (pDesc->Scaling == DXGI_SCALING_NONE && !IsWindows8OrGreater()) {' |
| 214 | print r' pDesc->Scaling = DXGI_SCALING_STRETCH;' |
| 215 | print r' }' |
José Fonseca | e23a6be | 2014-09-14 20:30:00 +0100 | [diff] [blame] | 216 | if method.name == 'CreateSwapChainForComposition': |
| 217 | print r' HWND hWnd = d3dretrace::createWindow(pDesc->Width, pDesc->Height);' |
| 218 | print r' _result = _this->CreateSwapChainForHwnd(pDevice, hWnd, pDesc, NULL, pRestrictToOutput, ppSwapChain);' |
Jose Fonseca | 5c487ec | 2015-06-02 22:16:52 +0100 | [diff] [blame] | 219 | self.checkResult(interface, method) |
José Fonseca | e23a6be | 2014-09-14 20:30:00 +0100 | [diff] [blame] | 220 | return |
Jose Fonseca | feb8f7d | 2016-02-01 14:14:00 +0000 | [diff] [blame] | 221 | if method.name == 'CreateTargetForHwnd': |
| 222 | print r' hwnd = d3dretrace::createWindow(1024, 768);' |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 223 | |
José Fonseca | 7618cb9 | 2014-08-28 16:00:48 +0100 | [diff] [blame] | 224 | if method.name == 'SetFullscreenState': |
| 225 | print r' if (retrace::forceWindowed) {' |
| 226 | print r' Fullscreen = FALSE;' |
José Fonseca | 693382a | 2014-10-03 14:27:59 +0100 | [diff] [blame] | 227 | print r' pTarget = NULL;' |
José Fonseca | 7618cb9 | 2014-08-28 16:00:48 +0100 | [diff] [blame] | 228 | print r' }' |
| 229 | |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 230 | # notify frame has been completed |
Jose Fonseca | ced9c1c | 2015-08-10 15:10:37 +0100 | [diff] [blame] | 231 | if interface.name.startswith('IDXGISwapChain') and method.name.startswith('Present'): |
| 232 | if interface.name.startswith('IDXGISwapChainDWM'): |
José Fonseca | 1e309b4 | 2014-09-18 11:08:09 +0100 | [diff] [blame] | 233 | print r' com_ptr<IDXGISwapChain> pSwapChain;' |
| 234 | print r' if (SUCCEEDED(_this->QueryInterface(IID_IDXGISwapChain, (void **) &pSwapChain))) {' |
| 235 | print r' dxgiDumper.bindDevice(pSwapChain);' |
| 236 | print r' } else {' |
| 237 | print r' assert(0);' |
| 238 | print r' }' |
José Fonseca | 29d4bda | 2014-02-28 17:50:24 +0000 | [diff] [blame] | 239 | else: |
| 240 | print r' dxgiDumper.bindDevice(_this);' |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 241 | print r' retrace::frameComplete(call);' |
| 242 | |
| 243 | if 'pSharedResource' in method.argNames(): |
| 244 | print r' if (pSharedResource) {' |
| 245 | print r' retrace::warning(call) << "shared surfaces unsupported\n";' |
| 246 | print r' pSharedResource = NULL;' |
| 247 | print r' }' |
| 248 | |
José Fonseca | 82ea89d | 2012-12-05 19:34:26 +0000 | [diff] [blame] | 249 | # Force driver |
José Fonseca | ad6f60f | 2013-09-16 14:58:34 +0100 | [diff] [blame] | 250 | if interface.name.startswith('IDXGIFactory') and method.name.startswith('EnumAdapters'): |
José Fonseca | 82ea89d | 2012-12-05 19:34:26 +0000 | [diff] [blame] | 251 | print r' const char *szSoftware = NULL;' |
| 252 | print r' switch (retrace::driver) {' |
| 253 | print r' case retrace::DRIVER_REFERENCE:' |
| 254 | print r' case retrace::DRIVER_SOFTWARE:' |
| 255 | print r' szSoftware = "d3d10warp.dll";' |
| 256 | print r' break;' |
| 257 | print r' case retrace::DRIVER_MODULE:' |
| 258 | print r' szSoftware = retrace::driverModule;' |
| 259 | print r' break;' |
| 260 | print r' default:' |
| 261 | print r' break;' |
| 262 | print r' }' |
| 263 | print r' HMODULE hSoftware = NULL;' |
| 264 | print r' if (szSoftware) {' |
| 265 | print r' hSoftware = LoadLibraryA(szSoftware);' |
| 266 | print r' if (!hSoftware) {' |
| 267 | print r' retrace::warning(call) << "failed to load " << szSoftware << "\n";' |
| 268 | print r' }' |
| 269 | print r' }' |
| 270 | print r' if (hSoftware) {' |
José Fonseca | ad6f60f | 2013-09-16 14:58:34 +0100 | [diff] [blame] | 271 | print r' _result = _this->CreateSoftwareAdapter(hSoftware, reinterpret_cast<IDXGIAdapter **>(ppAdapter));' |
José Fonseca | 82ea89d | 2012-12-05 19:34:26 +0000 | [diff] [blame] | 272 | print r' } else {' |
| 273 | Retracer.invokeInterfaceMethod(self, interface, method) |
| 274 | print r' }' |
| 275 | return |
| 276 | |
José Fonseca | e23a6be | 2014-09-14 20:30:00 +0100 | [diff] [blame] | 277 | if interface.name.startswith('ID3D10Device') and method.name.startswith('OpenSharedResource'): |
José Fonseca | 75cbb8c | 2013-02-12 16:19:23 +0000 | [diff] [blame] | 278 | print r' retrace::warning(call) << "replacing shared resource with checker pattern\n";' |
Jose Fonseca | df91377 | 2015-07-17 16:54:47 +0100 | [diff] [blame] | 279 | print r' _result = d3dretrace::createSharedResource(_this, ReturnedInterface, ppResource);' |
Jose Fonseca | 5c487ec | 2015-06-02 22:16:52 +0100 | [diff] [blame] | 280 | self.checkResult(interface, method) |
José Fonseca | 999284f | 2013-02-19 13:29:26 +0000 | [diff] [blame] | 281 | return |
Jose Fonseca | 559b5f8 | 2015-07-17 16:58:50 +0100 | [diff] [blame] | 282 | if interface.name.startswith('ID3D11Device') and method.name == 'OpenSharedResource': |
| 283 | # Some applications (e.g., video playing in IE11) create shared resources within the same process. |
| 284 | # TODO: Generalize to other OpenSharedResource variants |
| 285 | print r' retrace::map<HANDLE>::const_iterator it = _shared_handle_map.find(hResource);' |
| 286 | print r' if (it == _shared_handle_map.end()) {' |
| 287 | print r' retrace::warning(call) << "replacing shared resource with checker pattern\n";' |
| 288 | print r' _result = d3dretrace::createSharedResource(_this, ReturnedInterface, ppResource);' |
| 289 | self.checkResult(interface, method) |
| 290 | print r' } else {' |
| 291 | print r' hResource = it->second;' |
| 292 | Retracer.invokeInterfaceMethod(self, interface, method) |
| 293 | print r' }' |
| 294 | return |
José Fonseca | 2b66b93 | 2014-07-30 17:31:52 +0100 | [diff] [blame] | 295 | if interface.name.startswith('ID3D11Device') and method.name.startswith('OpenSharedResource'): |
| 296 | print r' retrace::warning(call) << "replacing shared resource with checker pattern\n";' |
Jose Fonseca | df91377 | 2015-07-17 16:54:47 +0100 | [diff] [blame] | 297 | print r' _result = d3dretrace::createSharedResource(_this, ReturnedInterface, ppResource);' |
José Fonseca | 2b66b93 | 2014-07-30 17:31:52 +0100 | [diff] [blame] | 298 | if method.name == 'OpenSharedResourceByName': |
| 299 | print r' (void)lpName;' |
| 300 | print r' (void)dwDesiredAccess;' |
Jose Fonseca | df91377 | 2015-07-17 16:54:47 +0100 | [diff] [blame] | 301 | else: |
| 302 | print r' (void)hResource;' |
Jose Fonseca | 5c487ec | 2015-06-02 22:16:52 +0100 | [diff] [blame] | 303 | self.checkResult(interface, method) |
José Fonseca | 2b66b93 | 2014-07-30 17:31:52 +0100 | [diff] [blame] | 304 | return |
José Fonseca | 999284f | 2013-02-19 13:29:26 +0000 | [diff] [blame] | 305 | |
| 306 | if method.name == 'Map': |
| 307 | # Reset _DO_NOT_WAIT flags. Otherwise they may fail, and we have no |
| 308 | # way to cope with it (other than retry). |
| 309 | mapFlagsArg = method.getArgByName('MapFlags') |
| 310 | for flag in mapFlagsArg.type.values: |
| 311 | if flag.endswith('_MAP_FLAG_DO_NOT_WAIT'): |
| 312 | print r' MapFlags &= ~%s;' % flag |
| 313 | |
José Fonseca | 7af569a | 2014-09-04 19:08:45 +0100 | [diff] [blame] | 314 | if method.name.startswith('UpdateSubresource'): |
José Fonseca | c9cc24e | 2014-06-13 19:06:51 +0100 | [diff] [blame] | 315 | # The D3D10 debug layer is buggy (or at least inconsistent with the |
| 316 | # runtime), as it seems to estimate and enforce the data size based on the |
| 317 | # SrcDepthPitch, even for non 3D textures, but in some traces |
| 318 | # SrcDepthPitch is garbagge for non 3D textures. |
| 319 | # XXX: It also seems to expect padding bytes at the end of the last |
| 320 | # row, but we never record (or allocate) those... |
| 321 | print r' if (retrace::debug && pDstBox && pDstBox->front == 0 && pDstBox->back == 1) {' |
| 322 | print r' SrcDepthPitch = 0;' |
| 323 | print r' }' |
| 324 | |
José Fonseca | 0688d2e | 2014-08-19 20:43:21 +0100 | [diff] [blame] | 325 | if method.name == 'SetGammaControl': |
| 326 | # This method is only supported while in full-screen mode |
| 327 | print r' if (retrace::forceWindowed) {' |
| 328 | print r' return;' |
| 329 | print r' }' |
| 330 | |
Jose Fonseca | 60e6514 | 2017-06-09 13:06:32 +0100 | [diff] [blame] | 331 | if method.name == 'GetData': |
| 332 | print r' pData = _allocator.alloc(DataSize);' |
| 333 | print r' do {' |
| 334 | self.doInvokeInterfaceMethod(interface, method) |
| 335 | print r' GetDataFlags = 0; // Prevent infinite loop' |
| 336 | print r' } while (_result == S_FALSE);' |
| 337 | self.checkResult(interface, method) |
| 338 | print r' return;' |
| 339 | |
José Fonseca | 999284f | 2013-02-19 13:29:26 +0000 | [diff] [blame] | 340 | Retracer.invokeInterfaceMethod(self, interface, method) |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 341 | |
Jose Fonseca | 4d5cfa4 | 2015-07-17 16:21:05 +0100 | [diff] [blame] | 342 | if method.name in ('AcquireSync', 'ReleaseSync'): |
| 343 | print r' if (SUCCEEDED(_result) && _result != S_OK) {' |
| 344 | print r' retrace::warning(call) << " returned " << _result << "\n";' |
| 345 | print r' }' |
| 346 | |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 347 | # process events after presents |
Jose Fonseca | ced9c1c | 2015-08-10 15:10:37 +0100 | [diff] [blame] | 348 | if interface.name.startswith('IDXGISwapChain') and method.name.startswith('Present'): |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 349 | print r' d3dretrace::processEvents();' |
| 350 | |
José Fonseca | 6ca2008 | 2014-10-07 21:39:43 +0100 | [diff] [blame] | 351 | if method.name in ('Map', 'Unmap'): |
| 352 | if interface.name.startswith('ID3D11DeviceContext'): |
| 353 | print ' void * & _pbData = g_Maps[_this][SubresourceKey(pResource, Subresource)];' |
| 354 | else: |
| 355 | subresourceArg = method.getArgByName('Subresource') |
| 356 | if subresourceArg is None: |
| 357 | print ' UINT Subresource = 0;' |
| 358 | print ' void * & _pbData = g_Maps[0][SubresourceKey(_this, Subresource)];' |
| 359 | |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 360 | if method.name == 'Map': |
José Fonseca | 003b8be | 2013-05-29 19:59:40 +0100 | [diff] [blame] | 361 | print ' _MAP_DESC _MapDesc;' |
| 362 | print ' _getMapDesc(_this, %s, _MapDesc);' % ', '.join(method.argNames()) |
| 363 | print ' size_t _MappedSize = _MapDesc.Size;' |
| 364 | print ' if (_MapDesc.Size) {' |
José Fonseca | 6ca2008 | 2014-10-07 21:39:43 +0100 | [diff] [blame] | 365 | print ' _pbData = _MapDesc.pData;' |
Jeff Muizelaar | 97244f9 | 2013-11-19 15:17:45 -0500 | [diff] [blame] | 366 | if interface.name.startswith('ID3D11DeviceContext'): |
Jose Fonseca | 1e3c0f7 | 2015-07-20 21:50:42 +0100 | [diff] [blame] | 367 | # Prevent false warnings on 1D and 2D resources, since the |
| 368 | # pitches are often junk there... |
| 369 | print ' _normalizeMap(pResource, pMappedResource);' |
Jeff Muizelaar | 97244f9 | 2013-11-19 15:17:45 -0500 | [diff] [blame] | 370 | else: |
José Fonseca | 6ca2008 | 2014-10-07 21:39:43 +0100 | [diff] [blame] | 371 | print ' _pbData = _MapDesc.pData;' |
José Fonseca | 4f49d21 | 2012-11-14 14:02:35 +0000 | [diff] [blame] | 372 | print ' } else {' |
| 373 | print ' return;' |
| 374 | print ' }' |
José Fonseca | 610942b | 2012-11-08 10:46:03 +0000 | [diff] [blame] | 375 | |
| 376 | if method.name == 'Unmap': |
José Fonseca | 6ca2008 | 2014-10-07 21:39:43 +0100 | [diff] [blame] | 377 | print ' if (_pbData) {' |
| 378 | print ' retrace::delRegionByPointer(_pbData);' |
| 379 | print ' _pbData = 0;' |
| 380 | print ' }' |
José Fonseca | 6f81033 | 2012-11-11 10:05:09 +0000 | [diff] [blame] | 381 | |
Jose Fonseca | 2d78bef | 2016-05-19 15:10:23 +0100 | [diff] [blame] | 382 | if interface.name.startswith('ID3D11VideoContext'): |
| 383 | if method.name == 'GetDecoderBuffer': |
| 384 | print ' if (*ppBuffer && *pBufferSize) {' |
| 385 | print ' g_Maps[nullptr][SubresourceKey(_this, Type)] = *ppBuffer;' |
| 386 | print ' }' |
| 387 | if method.name == 'ReleaseDecoderBuffer': |
| 388 | print ' SubresourceKey _mappingKey(_this, Type);' |
| 389 | print ' void *_pBuffer = g_Maps[nullptr][_mappingKey];' |
| 390 | print ' if (_pBuffer) {' |
| 391 | print ' retrace::delRegionByPointer(_pBuffer);' |
| 392 | print ' g_Maps[nullptr][_mappingKey] = 0;' |
| 393 | print ' }' |
| 394 | |
José Fonseca | 1d4fd14 | 2012-11-28 15:06:22 +0000 | [diff] [blame] | 395 | # Attach shader byte code for lookup |
| 396 | if 'pShaderBytecode' in method.argNames(): |
| 397 | ppShader = method.args[-1] |
| 398 | assert ppShader.output |
| 399 | print r' if (retrace::dumpingState && SUCCEEDED(_result)) {' |
| 400 | print r' (*%s)->SetPrivateData(d3dstate::GUID_D3DSTATE, BytecodeLength, pShaderBytecode);' % ppShader.name |
| 401 | print r' }' |
| 402 | |
Jose Fonseca | 37f769f | 2017-06-16 17:57:39 +0100 | [diff] [blame] | 403 | def retraceInterfaceMethodBody(self, interface, method): |
| 404 | Retracer.retraceInterfaceMethodBody(self, interface, method) |
| 405 | |
| 406 | # Add pitch swizzling information to the region |
Jose Fonseca | b2754af | 2017-06-19 14:22:55 +0100 | [diff] [blame] | 407 | if method.name == 'Map' and interface.name not in ('ID3D10Buffer', 'ID3D10Texture1D'): |
| 408 | if interface.name.startswith('ID3D11DeviceContext'): |
| 409 | outArg = method.getArgByName('pMappedResource') |
| 410 | memberNames = ('pData', 'RowPitch', 'DepthPitch') |
| 411 | elif interface.name.startswith('ID3D10'): |
| 412 | outArg = method.args[-1] |
| 413 | memberNames = ('pData', 'RowPitch', 'DepthPitch') |
| 414 | elif interface.name == 'IDXGISurface': |
| 415 | outArg = method.getArgByName('pLockedRect') |
| 416 | memberNames = ('pBits', 'Pitch', None) |
| 417 | else: |
| 418 | raise NotImplementedError |
| 419 | struct = outArg.type.type |
| 420 | dataMemberName, rowPitchMemberName, depthPitchMemberName = memberNames |
| 421 | dataMemberIndex = struct.getMemberByName(dataMemberName) |
| 422 | rowPitchMemberIndex = struct.getMemberByName(rowPitchMemberName) |
| 423 | print r' if (_pbData && %s->%s != 0) {' % (outArg.name, rowPitchMemberName) |
Jose Fonseca | 37f769f | 2017-06-16 17:57:39 +0100 | [diff] [blame] | 424 | print r' const trace::Array *_%s = call.arg(%u).toArray();' % (outArg.name, outArg.index) |
| 425 | print r' if (%s) {' % outArg.name |
| 426 | print r' const trace::Struct *_struct = _%s->values[0]->toStruct();' % (outArg.name) |
| 427 | print r' if (_struct) {' |
Jose Fonseca | b2754af | 2017-06-19 14:22:55 +0100 | [diff] [blame] | 428 | print r' unsigned long long traceAddress = _struct->members[%u]->toUIntPtr();' % dataMemberIndex |
| 429 | print r' int traceRowPitch = _struct->members[%u]->toSInt();' % rowPitchMemberIndex |
| 430 | print r' int realRowPitch = %s->%s;' % (outArg.name, rowPitchMemberName) |
Jose Fonseca | 37f769f | 2017-06-16 17:57:39 +0100 | [diff] [blame] | 431 | print r' if (realRowPitch && traceRowPitch != realRowPitch) {' |
| 432 | print r' retrace::setRegionPitch(traceAddress, 2, traceRowPitch, realRowPitch);' |
Jose Fonseca | 37f769f | 2017-06-16 17:57:39 +0100 | [diff] [blame] | 433 | print r' }' |
Jose Fonseca | b2754af | 2017-06-19 14:22:55 +0100 | [diff] [blame] | 434 | try: |
| 435 | depthPitchMemberIndex = struct.getMemberByName(depthPitchMemberName) |
| 436 | except ValueError: |
| 437 | assert len(struct.members) < 3 |
| 438 | pass |
| 439 | else: |
| 440 | assert depthPitchMemberName == 'DepthPitch' |
| 441 | print r' if (%s->DepthPitch) {' % outArg.name |
| 442 | print r' retrace::checkMismatch(call, "DepthPitch", _struct->members[%u], %s->DepthPitch);' % (struct.getMemberByName('DepthPitch'), outArg.name) |
| 443 | print r' }' |
Jose Fonseca | 37f769f | 2017-06-16 17:57:39 +0100 | [diff] [blame] | 444 | print r' }' |
| 445 | print r' }' |
| 446 | print r' }' |
| 447 | |
| 448 | |
José Fonseca | 3be2c67 | 2015-02-06 15:36:40 +0000 | [diff] [blame] | 449 | def extractArg(self, function, arg, arg_type, lvalue, rvalue): |
| 450 | # Set object names |
| 451 | if function.name == 'SetPrivateData' and arg.name == 'pData': |
| 452 | iid = function.args[0].name |
| 453 | print r' if (%s != WKPDID_D3DDebugObjectName) {' % iid |
| 454 | print r' return;' |
| 455 | print r' }' |
| 456 | # Interpret argument as string |
| 457 | Retracer.extractArg(self, function, arg, LPCSTR, lvalue, rvalue) |
| 458 | print r' assert(pData);' |
Jose Fonseca | 571c2fa | 2015-07-30 15:08:49 +0100 | [diff] [blame] | 459 | print r' assert(DataSize >= strlen((const char *)pData));' |
| 460 | print r' // Some applications include the trailing zero terminator in the data' |
| 461 | print r' DataSize = strlen((const char *)pData);' |
José Fonseca | 3be2c67 | 2015-02-06 15:36:40 +0000 | [diff] [blame] | 462 | return |
| 463 | |
| 464 | Retracer.extractArg(self, function, arg, arg_type, lvalue, rvalue) |
| 465 | |
José Fonseca | 6f81033 | 2012-11-11 10:05:09 +0000 | [diff] [blame] | 466 | |
| 467 | def main(): |
José Fonseca | 75cbb8c | 2013-02-12 16:19:23 +0000 | [diff] [blame] | 468 | print r'#define INITGUID' |
| 469 | print |
José Fonseca | 73341c2 | 2012-11-24 13:04:42 +0000 | [diff] [blame] | 470 | print r'#include <string.h>' |
José Fonseca | 6f81033 | 2012-11-11 10:05:09 +0000 | [diff] [blame] | 471 | print |
| 472 | print r'#include <iostream>' |
| 473 | print |
| 474 | print r'#include "d3dretrace.hpp"' |
José Fonseca | 89b697f | 2014-06-17 20:26:38 +0100 | [diff] [blame] | 475 | print r'#include "os_version.hpp"' |
José Fonseca | 6f81033 | 2012-11-11 10:05:09 +0000 | [diff] [blame] | 476 | print |
José Fonseca | 50bee95 | 2015-02-07 22:32:19 +0000 | [diff] [blame] | 477 | print r'#include "d3dretrace_dxgi.hpp"' |
| 478 | print r'#include "d3d10imports.hpp"' |
| 479 | print r'#include "d3d10size.hpp"' |
| 480 | print r'#include "d3d10state.hpp"' |
| 481 | print r'#include "d3d11imports.hpp"' |
| 482 | print r'#include "d3d11size.hpp"' |
Jose Fonseca | feb8f7d | 2016-02-01 14:14:00 +0000 | [diff] [blame] | 483 | print r'#include "dcompimports.hpp"' |
José Fonseca | 50bee95 | 2015-02-07 22:32:19 +0000 | [diff] [blame] | 484 | print r'#include "d3dstate.hpp"' |
Jose Fonseca | 1e579c4 | 2017-03-22 14:08:19 +0000 | [diff] [blame] | 485 | print r'#include "d3d9imports.hpp" // D3DERR_WASSTILLDRAWING' |
José Fonseca | 50bee95 | 2015-02-07 22:32:19 +0000 | [diff] [blame] | 486 | print |
| 487 | print '''static d3dretrace::D3DDumper<IDXGISwapChain> dxgiDumper;''' |
| 488 | print '''static d3dretrace::D3DDumper<ID3D10Device> d3d10Dumper;''' |
| 489 | print '''static d3dretrace::D3DDumper<ID3D11DeviceContext> d3d11Dumper;''' |
| 490 | print |
José Fonseca | 6f81033 | 2012-11-11 10:05:09 +0000 | [diff] [blame] | 491 | |
| 492 | api = API() |
José Fonseca | 50bee95 | 2015-02-07 22:32:19 +0000 | [diff] [blame] | 493 | api.addModule(dxgi) |
| 494 | api.addModule(d3d10) |
| 495 | api.addModule(d3d10_1) |
| 496 | api.addModule(d3d11) |
Jose Fonseca | feb8f7d | 2016-02-01 14:14:00 +0000 | [diff] [blame] | 497 | api.addModule(dcomp) |
José Fonseca | fc58d05 | 2014-06-13 12:47:19 +0100 | [diff] [blame] | 498 | |
José Fonseca | 6f81033 | 2012-11-11 10:05:09 +0000 | [diff] [blame] | 499 | retracer = D3DRetracer() |
| 500 | retracer.retraceApi(api) |
| 501 | |
| 502 | |
| 503 | if __name__ == '__main__': |
| 504 | main() |