blob: d627ec3e0a070e449160084ee41498901a4f125f [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é Fonseca75cbb8c2013-02-12 16:19:23 +000032import specs.dxgi
José Fonseca6f810332012-11-11 10:05:09 +000033from specs.stdapi import API
José Fonseca3be2c672015-02-06 15:36:40 +000034from specs.winapi import LPCSTR
José Fonseca6f810332012-11-11 10:05:09 +000035from specs.dxgi import dxgi
Jose Fonsecaa92e0c42015-03-14 10:49:23 +000036from specs.d3d10 import d3d10, d3d10_1
José Fonseca6f810332012-11-11 10:05:09 +000037from specs.d3d11 import d3d11
Jose Fonsecafeb8f7d2016-02-01 14:14:00 +000038from specs.dcomp import dcomp
José Fonseca610942b2012-11-08 10:46:03 +000039
40
41class D3DRetracer(Retracer):
42
José Fonsecae3814852012-11-11 09:45:06 +000043 def retraceApi(self, api):
José Fonseca6ca20082014-10-07 21:39:43 +010044 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é Fonseca610942b2012-11-08 10:46:03 +000047 print
José Fonseca73341c22012-11-24 13:04:42 +000048 self.table_name = 'd3dretrace::dxgi_callbacks'
José Fonseca610942b2012-11-08 10:46:03 +000049
José Fonsecae3814852012-11-11 09:45:06 +000050 Retracer.retraceApi(self, api)
José Fonseca610942b2012-11-08 10:46:03 +000051
José Fonseca4f49d212012-11-14 14:02:35 +000052 createDeviceFunctionNames = [
53 "D3D10CreateDevice",
54 "D3D10CreateDeviceAndSwapChain",
55 "D3D10CreateDevice1",
56 "D3D10CreateDeviceAndSwapChain1",
57 "D3D11CreateDevice",
58 "D3D11CreateDeviceAndSwapChain",
59 ]
José Fonseca610942b2012-11-08 10:46:03 +000060
José Fonseca4f49d212012-11-14 14:02:35 +000061 def invokeFunction(self, function):
62 if function.name in self.createDeviceFunctionNames:
63 # create windows as neccessary
64 if 'pSwapChainDesc' in function.argNames():
José Fonseca1e309b42014-09-18 11:08:09 +010065 print r' d3dretrace::createWindowForSwapChain(pSwapChainDesc);'
José Fonseca4f49d212012-11-14 14:02:35 +000066
José Fonseca4f49d212012-11-14 14:02:35 +000067 # 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é Fonsecaea799192012-11-13 21:37:24 +000072
José Fonseca42b89fc2012-11-27 12:19:45 +000073 if function.name.startswith('D3D10CreateDevice'):
José Fonseca7bcc96c2012-12-05 19:28:22 +000074 # Toggle debugging
José Fonseca7bcc96c2012-12-05 19:28:22 +000075 print r' if (retrace::debug) {'
Jose Fonsecaf39fd602017-08-23 15:13:39 +010076 print r' Flags |= D3D10_CREATE_DEVICE_DEBUG;'
77 print r' } else {'
78 print r' Flags &= ~D3D10_CREATE_DEVICE_DEBUG;'
José Fonseca7bcc96c2012-12-05 19:28:22 +000079 print r' }'
80
81 # Force driver
José Fonseca42b89fc2012-11-27 12:19:45 +000082 self.forceDriver('D3D10_DRIVER_TYPE')
José Fonseca7bcc96c2012-12-05 19:28:22 +000083
José Fonseca42b89fc2012-11-27 12:19:45 +000084 if function.name.startswith('D3D11CreateDevice'):
José Fonseca7bcc96c2012-12-05 19:28:22 +000085 # Toggle debugging
José Fonseca7bcc96c2012-12-05 19:28:22 +000086 print r' if (retrace::debug) {'
Jose Fonsecaf39fd602017-08-23 15:13:39 +010087 print r' Flags |= D3D11_CREATE_DEVICE_DEBUG;'
88 print r' } else {'
89 print r' Flags &= ~D3D11_CREATE_DEVICE_DEBUG;'
José Fonseca7bcc96c2012-12-05 19:28:22 +000090 print r' }'
91
92 # Force driver
José Fonseca42b89fc2012-11-27 12:19:45 +000093 self.forceDriver('D3D_DRIVER_TYPE')
94
José Fonseca610942b2012-11-08 10:46:03 +000095 Retracer.invokeFunction(self, function)
96
Jose Fonsecaf39fd602017-08-23 15:13:39 +010097 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 Fonsecad0094a22017-05-30 12:31:37 +0100116 def handleFailure(self, interface, methodOrFunction):
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100117 # Catch when device is removed, and report the reason.
Jose Fonsecad0094a22017-05-30 12:31:37 +0100118 if interface is not None:
Jose Fonsecaa42384c2018-01-12 15:37:52 +0000119 print r' if (_result == DXGI_ERROR_DEVICE_REMOVED) {'
Jose Fonsecad0094a22017-05-30 12:31:37 +0100120 getDeviceRemovedReasonMethod = interface.getMethodByName("GetDeviceRemovedReason")
121 if getDeviceRemovedReasonMethod is not None:
Jose Fonsecad0094a22017-05-30 12:31:37 +0100122 print r' HRESULT _reason = _this->GetDeviceRemovedReason();'
123 print r' retrace::failed(call, _reason);'
Jose Fonsecaa42384c2018-01-12 15:37:52 +0000124 getDeviceMethod = interface.getMethodByName("GetDevice")
125 if getDeviceMethod is not None and len(getDeviceMethod.args) == 1:
126 print r' com_ptr<%s> _pDevice;' % getDeviceMethod.args[0].type.type.type
127 print r' _this->GetDevice(&_pDevice);'
128 print r' HRESULT _reason = _pDevice->GetDeviceRemovedReason();'
129 print r' retrace::failed(call, _reason);'
130 print r' exit(EXIT_FAILURE);'
131 print r' }'
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100132
Jose Fonsecad0094a22017-05-30 12:31:37 +0100133 Retracer.handleFailure(self, interface, methodOrFunction)
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100134
José Fonseca42b89fc2012-11-27 12:19:45 +0000135 def forceDriver(self, enum):
José Fonseca82ea89d2012-12-05 19:34:26 +0000136 # This can only work when pAdapter is NULL. For non-NULL pAdapter we
137 # need to override inside the EnumAdapters call below
138 print r' if (pAdapter == NULL) {'
139 print r' switch (retrace::driver) {'
140 print r' case retrace::DRIVER_HARDWARE:'
141 print r' DriverType = %s_HARDWARE;' % enum
142 print r' Software = NULL;'
143 print r' break;'
144 print r' case retrace::DRIVER_SOFTWARE:'
145 print r' DriverType = %s_WARP;' % enum
146 print r' Software = NULL;'
147 print r' break;'
148 print r' case retrace::DRIVER_REFERENCE:'
149 print r' DriverType = %s_REFERENCE;' % enum
150 print r' Software = NULL;'
151 print r' break;'
152 print r' case retrace::DRIVER_NULL:'
153 print r' DriverType = %s_NULL;' % enum
154 print r' Software = NULL;'
155 print r' break;'
156 print r' case retrace::DRIVER_MODULE:'
157 print r' DriverType = %s_SOFTWARE;' % enum
158 print r' Software = LoadLibraryA(retrace::driverModule);'
José Fonseca42b89fc2012-11-27 12:19:45 +0000159 print r' if (!Software) {'
José Fonseca82ea89d2012-12-05 19:34:26 +0000160 print r' retrace::warning(call) << "failed to load " << retrace::driverModule << "\n";'
José Fonseca42b89fc2012-11-27 12:19:45 +0000161 print r' }'
José Fonseca82ea89d2012-12-05 19:34:26 +0000162 print r' break;'
163 print r' default:'
164 print r' assert(0);'
165 print r' /* fall-through */'
166 print r' case retrace::DRIVER_DEFAULT:'
167 print r' if (DriverType == %s_SOFTWARE) {' % enum
168 print r' Software = LoadLibraryA("d3d10warp");'
169 print r' if (!Software) {'
170 print r' retrace::warning(call) << "failed to load d3d10warp.dll\n";'
171 print r' }'
172 print r' }'
173 print r' break;'
José Fonseca42b89fc2012-11-27 12:19:45 +0000174 print r' }'
José Fonseca82ea89d2012-12-05 19:34:26 +0000175 print r' } else {'
176 print r' Software = NULL;'
José Fonseca42b89fc2012-11-27 12:19:45 +0000177 print r' }'
178
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100179 def doInvokeInterfaceMethod(self, interface, method):
180 Retracer.doInvokeInterfaceMethod(self, interface, method)
181
182 # Keep retrying ID3D11VideoContext::DecoderBeginFrame when returns E_PENDING
183 if interface.name == 'ID3D11VideoContext' and method.name == 'DecoderBeginFrame':
184 print r' while (_result == D3DERR_WASSTILLDRAWING || _result == E_PENDING) {'
185 print r' Sleep(1);'
186 Retracer.doInvokeInterfaceMethod(self, interface, method)
187 print r' }'
188
José Fonseca610942b2012-11-08 10:46:03 +0000189 def invokeInterfaceMethod(self, interface, method):
190 # keep track of the last used device for state dumping
José Fonseca944088e2012-11-20 14:47:03 +0000191 if interface.name in ('ID3D10Device', 'ID3D10Device1'):
192 if method.name == 'Release':
José Fonsecadb62f0f2013-06-27 16:15:01 +0100193 print r' if (call.ret->toUInt() == 0) {'
194 print r' d3d10Dumper.unbindDevice(_this);'
195 print r' }'
José Fonseca944088e2012-11-20 14:47:03 +0000196 else:
197 print r' d3d10Dumper.bindDevice(_this);'
Jose Fonseca4a1c7aa2015-08-10 16:53:49 +0100198 if interface.name.startswith('ID3D11DeviceContext'):
José Fonseca5773beb2012-11-14 11:46:58 +0000199 if method.name == 'Release':
José Fonsecadb62f0f2013-06-27 16:15:01 +0100200 print r' if (call.ret->toUInt() == 0) {'
201 print r' d3d11Dumper.unbindDevice(_this);'
202 print r' }'
José Fonseca5773beb2012-11-14 11:46:58 +0000203 else:
Jose Fonsecab059dd82017-07-20 13:13:44 +0100204 print r' d3d11Dumper.bindDevice(_this);'
José Fonseca610942b2012-11-08 10:46:03 +0000205
José Fonseca1e309b42014-09-18 11:08:09 +0100206 # intercept private interfaces
José Fonsecad59843c2014-08-22 16:59:25 +0100207 if method.name == 'QueryInterface':
José Fonsecac44c62c2014-09-22 13:42:42 +0100208 print r' if (!d3dretrace::overrideQueryInterface(_this, riid, ppvObj, &_result)) {'
José Fonseca75cbb8c2013-02-12 16:19:23 +0000209 Retracer.invokeInterfaceMethod(self, interface, method)
210 print r' }'
211 return
212
José Fonseca610942b2012-11-08 10:46:03 +0000213 # create windows as neccessary
214 if method.name == 'CreateSwapChain':
José Fonseca1e309b42014-09-18 11:08:09 +0100215 print r' d3dretrace::createWindowForSwapChain(pDesc);'
Jose Fonsecadafb4ea2015-08-10 14:40:22 +0100216 if method.name == 'CreateSwapChainForHwnd':
217 print r' WindowHandle = d3dretrace::createWindow(pDesc->Width, pDesc->Height);'
218 print r' // DXGI_SCALING_NONE is only supported on Win8 and beyond'
219 print r' if (pDesc->Scaling == DXGI_SCALING_NONE && !IsWindows8OrGreater()) {'
220 print r' pDesc->Scaling = DXGI_SCALING_STRETCH;'
221 print r' }'
José Fonsecae23a6be2014-09-14 20:30:00 +0100222 if method.name == 'CreateSwapChainForComposition':
223 print r' HWND hWnd = d3dretrace::createWindow(pDesc->Width, pDesc->Height);'
224 print r' _result = _this->CreateSwapChainForHwnd(pDevice, hWnd, pDesc, NULL, pRestrictToOutput, ppSwapChain);'
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100225 self.checkResult(interface, method)
José Fonsecae23a6be2014-09-14 20:30:00 +0100226 return
Jose Fonsecafeb8f7d2016-02-01 14:14:00 +0000227 if method.name == 'CreateTargetForHwnd':
228 print r' hwnd = d3dretrace::createWindow(1024, 768);'
José Fonseca610942b2012-11-08 10:46:03 +0000229
José Fonseca7618cb92014-08-28 16:00:48 +0100230 if method.name == 'SetFullscreenState':
231 print r' if (retrace::forceWindowed) {'
Jose Fonseca7b3403e2018-01-19 13:05:40 +0000232 print r' DXGI_SWAP_CHAIN_DESC Desc;'
233 print r' _this->GetDesc(&Desc);'
234 print r' if (Desc.BufferDesc.Format != DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM) {'
235 print r' Fullscreen = FALSE;'
236 print r' pTarget = nullptr;'
237 print r' }'
José Fonseca7618cb92014-08-28 16:00:48 +0100238 print r' }'
239
José Fonseca610942b2012-11-08 10:46:03 +0000240 # notify frame has been completed
Jose Fonsecaced9c1c2015-08-10 15:10:37 +0100241 if interface.name.startswith('IDXGISwapChain') and method.name.startswith('Present'):
242 if interface.name.startswith('IDXGISwapChainDWM'):
José Fonseca1e309b42014-09-18 11:08:09 +0100243 print r' com_ptr<IDXGISwapChain> pSwapChain;'
244 print r' if (SUCCEEDED(_this->QueryInterface(IID_IDXGISwapChain, (void **) &pSwapChain))) {'
245 print r' dxgiDumper.bindDevice(pSwapChain);'
246 print r' } else {'
247 print r' assert(0);'
248 print r' }'
José Fonseca29d4bda2014-02-28 17:50:24 +0000249 else:
250 print r' dxgiDumper.bindDevice(_this);'
José Fonseca610942b2012-11-08 10:46:03 +0000251 print r' retrace::frameComplete(call);'
252
253 if 'pSharedResource' in method.argNames():
254 print r' if (pSharedResource) {'
255 print r' retrace::warning(call) << "shared surfaces unsupported\n";'
256 print r' pSharedResource = NULL;'
257 print r' }'
258
José Fonseca82ea89d2012-12-05 19:34:26 +0000259 # Force driver
José Fonsecaad6f60f2013-09-16 14:58:34 +0100260 if interface.name.startswith('IDXGIFactory') and method.name.startswith('EnumAdapters'):
José Fonseca82ea89d2012-12-05 19:34:26 +0000261 print r' const char *szSoftware = NULL;'
262 print r' switch (retrace::driver) {'
263 print r' case retrace::DRIVER_REFERENCE:'
264 print r' case retrace::DRIVER_SOFTWARE:'
265 print r' szSoftware = "d3d10warp.dll";'
266 print r' break;'
267 print r' case retrace::DRIVER_MODULE:'
268 print r' szSoftware = retrace::driverModule;'
269 print r' break;'
270 print r' default:'
271 print r' break;'
272 print r' }'
273 print r' HMODULE hSoftware = NULL;'
274 print r' if (szSoftware) {'
275 print r' hSoftware = LoadLibraryA(szSoftware);'
276 print r' if (!hSoftware) {'
277 print r' retrace::warning(call) << "failed to load " << szSoftware << "\n";'
278 print r' }'
279 print r' }'
280 print r' if (hSoftware) {'
José Fonsecaad6f60f2013-09-16 14:58:34 +0100281 print r' _result = _this->CreateSoftwareAdapter(hSoftware, reinterpret_cast<IDXGIAdapter **>(ppAdapter));'
José Fonseca82ea89d2012-12-05 19:34:26 +0000282 print r' } else {'
283 Retracer.invokeInterfaceMethod(self, interface, method)
284 print r' }'
285 return
286
José Fonsecae23a6be2014-09-14 20:30:00 +0100287 if interface.name.startswith('ID3D10Device') and method.name.startswith('OpenSharedResource'):
José Fonseca75cbb8c2013-02-12 16:19:23 +0000288 print r' retrace::warning(call) << "replacing shared resource with checker pattern\n";'
Jose Fonsecadf913772015-07-17 16:54:47 +0100289 print r' _result = d3dretrace::createSharedResource(_this, ReturnedInterface, ppResource);'
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100290 self.checkResult(interface, method)
José Fonseca999284f2013-02-19 13:29:26 +0000291 return
Jose Fonseca559b5f82015-07-17 16:58:50 +0100292 if interface.name.startswith('ID3D11Device') and method.name == 'OpenSharedResource':
293 # Some applications (e.g., video playing in IE11) create shared resources within the same process.
294 # TODO: Generalize to other OpenSharedResource variants
295 print r' retrace::map<HANDLE>::const_iterator it = _shared_handle_map.find(hResource);'
296 print r' if (it == _shared_handle_map.end()) {'
297 print r' retrace::warning(call) << "replacing shared resource with checker pattern\n";'
298 print r' _result = d3dretrace::createSharedResource(_this, ReturnedInterface, ppResource);'
299 self.checkResult(interface, method)
300 print r' } else {'
301 print r' hResource = it->second;'
302 Retracer.invokeInterfaceMethod(self, interface, method)
303 print r' }'
304 return
José Fonseca2b66b932014-07-30 17:31:52 +0100305 if interface.name.startswith('ID3D11Device') and method.name.startswith('OpenSharedResource'):
306 print r' retrace::warning(call) << "replacing shared resource with checker pattern\n";'
Jose Fonsecadf913772015-07-17 16:54:47 +0100307 print r' _result = d3dretrace::createSharedResource(_this, ReturnedInterface, ppResource);'
José Fonseca2b66b932014-07-30 17:31:52 +0100308 if method.name == 'OpenSharedResourceByName':
309 print r' (void)lpName;'
310 print r' (void)dwDesiredAccess;'
Jose Fonsecadf913772015-07-17 16:54:47 +0100311 else:
312 print r' (void)hResource;'
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100313 self.checkResult(interface, method)
José Fonseca2b66b932014-07-30 17:31:52 +0100314 return
José Fonseca999284f2013-02-19 13:29:26 +0000315
316 if method.name == 'Map':
317 # Reset _DO_NOT_WAIT flags. Otherwise they may fail, and we have no
318 # way to cope with it (other than retry).
319 mapFlagsArg = method.getArgByName('MapFlags')
320 for flag in mapFlagsArg.type.values:
321 if flag.endswith('_MAP_FLAG_DO_NOT_WAIT'):
322 print r' MapFlags &= ~%s;' % flag
323
José Fonseca7af569a2014-09-04 19:08:45 +0100324 if method.name.startswith('UpdateSubresource'):
José Fonsecac9cc24e2014-06-13 19:06:51 +0100325 # The D3D10 debug layer is buggy (or at least inconsistent with the
326 # runtime), as it seems to estimate and enforce the data size based on the
327 # SrcDepthPitch, even for non 3D textures, but in some traces
328 # SrcDepthPitch is garbagge for non 3D textures.
329 # XXX: It also seems to expect padding bytes at the end of the last
330 # row, but we never record (or allocate) those...
331 print r' if (retrace::debug && pDstBox && pDstBox->front == 0 && pDstBox->back == 1) {'
332 print r' SrcDepthPitch = 0;'
333 print r' }'
334
José Fonseca0688d2e2014-08-19 20:43:21 +0100335 if method.name == 'SetGammaControl':
336 # This method is only supported while in full-screen mode
337 print r' if (retrace::forceWindowed) {'
338 print r' return;'
339 print r' }'
340
Jose Fonseca60e65142017-06-09 13:06:32 +0100341 if method.name == 'GetData':
342 print r' pData = _allocator.alloc(DataSize);'
343 print r' do {'
344 self.doInvokeInterfaceMethod(interface, method)
345 print r' GetDataFlags = 0; // Prevent infinite loop'
346 print r' } while (_result == S_FALSE);'
347 self.checkResult(interface, method)
348 print r' return;'
349
José Fonseca999284f2013-02-19 13:29:26 +0000350 Retracer.invokeInterfaceMethod(self, interface, method)
José Fonseca610942b2012-11-08 10:46:03 +0000351
Jose Fonseca4d5cfa42015-07-17 16:21:05 +0100352 if method.name in ('AcquireSync', 'ReleaseSync'):
353 print r' if (SUCCEEDED(_result) && _result != S_OK) {'
354 print r' retrace::warning(call) << " returned " << _result << "\n";'
355 print r' }'
356
José Fonseca610942b2012-11-08 10:46:03 +0000357 # process events after presents
Jose Fonsecaced9c1c2015-08-10 15:10:37 +0100358 if interface.name.startswith('IDXGISwapChain') and method.name.startswith('Present'):
José Fonseca610942b2012-11-08 10:46:03 +0000359 print r' d3dretrace::processEvents();'
360
José Fonseca6ca20082014-10-07 21:39:43 +0100361 if method.name in ('Map', 'Unmap'):
362 if interface.name.startswith('ID3D11DeviceContext'):
363 print ' void * & _pbData = g_Maps[_this][SubresourceKey(pResource, Subresource)];'
364 else:
365 subresourceArg = method.getArgByName('Subresource')
366 if subresourceArg is None:
367 print ' UINT Subresource = 0;'
368 print ' void * & _pbData = g_Maps[0][SubresourceKey(_this, Subresource)];'
369
José Fonseca610942b2012-11-08 10:46:03 +0000370 if method.name == 'Map':
José Fonseca003b8be2013-05-29 19:59:40 +0100371 print ' _MAP_DESC _MapDesc;'
372 print ' _getMapDesc(_this, %s, _MapDesc);' % ', '.join(method.argNames())
373 print ' size_t _MappedSize = _MapDesc.Size;'
374 print ' if (_MapDesc.Size) {'
José Fonseca6ca20082014-10-07 21:39:43 +0100375 print ' _pbData = _MapDesc.pData;'
Jeff Muizelaar97244f92013-11-19 15:17:45 -0500376 if interface.name.startswith('ID3D11DeviceContext'):
Jose Fonseca1e3c0f72015-07-20 21:50:42 +0100377 # Prevent false warnings on 1D and 2D resources, since the
378 # pitches are often junk there...
379 print ' _normalizeMap(pResource, pMappedResource);'
Jeff Muizelaar97244f92013-11-19 15:17:45 -0500380 else:
José Fonseca6ca20082014-10-07 21:39:43 +0100381 print ' _pbData = _MapDesc.pData;'
José Fonseca4f49d212012-11-14 14:02:35 +0000382 print ' } else {'
383 print ' return;'
384 print ' }'
Zack Rusin1cb7b772018-02-09 11:36:11 -0500385
José Fonseca610942b2012-11-08 10:46:03 +0000386 if method.name == 'Unmap':
José Fonseca6ca20082014-10-07 21:39:43 +0100387 print ' if (_pbData) {'
388 print ' retrace::delRegionByPointer(_pbData);'
389 print ' _pbData = 0;'
390 print ' }'
José Fonseca6f810332012-11-11 10:05:09 +0000391
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100392 if interface.name.startswith('ID3D11VideoContext'):
393 if method.name == 'GetDecoderBuffer':
394 print ' if (*ppBuffer && *pBufferSize) {'
395 print ' g_Maps[nullptr][SubresourceKey(_this, Type)] = *ppBuffer;'
396 print ' }'
397 if method.name == 'ReleaseDecoderBuffer':
398 print ' SubresourceKey _mappingKey(_this, Type);'
399 print ' void *_pBuffer = g_Maps[nullptr][_mappingKey];'
400 print ' if (_pBuffer) {'
401 print ' retrace::delRegionByPointer(_pBuffer);'
402 print ' g_Maps[nullptr][_mappingKey] = 0;'
403 print ' }'
404
José Fonseca1d4fd142012-11-28 15:06:22 +0000405 # Attach shader byte code for lookup
406 if 'pShaderBytecode' in method.argNames():
407 ppShader = method.args[-1]
408 assert ppShader.output
409 print r' if (retrace::dumpingState && SUCCEEDED(_result)) {'
410 print r' (*%s)->SetPrivateData(d3dstate::GUID_D3DSTATE, BytecodeLength, pShaderBytecode);' % ppShader.name
411 print r' }'
412
Zack Rusin1cb7b772018-02-09 11:36:11 -0500413 if method.name == 'CreateBuffer':
414 ppBuffer = method.args[-1]
415 print r' if (retrace::dumpingState && SUCCEEDED(_result)) {'
416 print r' char label[32];'
417 print r' _snprintf(label, sizeof label, "0x%%llx", call.arg(%u).toArray()->values[0]->toUIntPtr());' % ppBuffer.index
418 print r' (*%s)->SetPrivateData(WKPDID_D3DDebugObjectName, strlen(label)+1, label);' % ppBuffer.name
419 print r' }'
420
Jose Fonseca37f769f2017-06-16 17:57:39 +0100421 def retraceInterfaceMethodBody(self, interface, method):
422 Retracer.retraceInterfaceMethodBody(self, interface, method)
423
424 # Add pitch swizzling information to the region
Jose Fonsecab2754af2017-06-19 14:22:55 +0100425 if method.name == 'Map' and interface.name not in ('ID3D10Buffer', 'ID3D10Texture1D'):
426 if interface.name.startswith('ID3D11DeviceContext'):
427 outArg = method.getArgByName('pMappedResource')
428 memberNames = ('pData', 'RowPitch', 'DepthPitch')
429 elif interface.name.startswith('ID3D10'):
430 outArg = method.args[-1]
431 memberNames = ('pData', 'RowPitch', 'DepthPitch')
432 elif interface.name == 'IDXGISurface':
433 outArg = method.getArgByName('pLockedRect')
434 memberNames = ('pBits', 'Pitch', None)
435 else:
436 raise NotImplementedError
437 struct = outArg.type.type
438 dataMemberName, rowPitchMemberName, depthPitchMemberName = memberNames
439 dataMemberIndex = struct.getMemberByName(dataMemberName)
440 rowPitchMemberIndex = struct.getMemberByName(rowPitchMemberName)
441 print r' if (_pbData && %s->%s != 0) {' % (outArg.name, rowPitchMemberName)
Jose Fonseca37f769f2017-06-16 17:57:39 +0100442 print r' const trace::Array *_%s = call.arg(%u).toArray();' % (outArg.name, outArg.index)
443 print r' if (%s) {' % outArg.name
444 print r' const trace::Struct *_struct = _%s->values[0]->toStruct();' % (outArg.name)
445 print r' if (_struct) {'
Jose Fonsecab2754af2017-06-19 14:22:55 +0100446 print r' unsigned long long traceAddress = _struct->members[%u]->toUIntPtr();' % dataMemberIndex
447 print r' int traceRowPitch = _struct->members[%u]->toSInt();' % rowPitchMemberIndex
448 print r' int realRowPitch = %s->%s;' % (outArg.name, rowPitchMemberName)
Jose Fonseca37f769f2017-06-16 17:57:39 +0100449 print r' if (realRowPitch && traceRowPitch != realRowPitch) {'
450 print r' retrace::setRegionPitch(traceAddress, 2, traceRowPitch, realRowPitch);'
Jose Fonseca37f769f2017-06-16 17:57:39 +0100451 print r' }'
Jose Fonsecab2754af2017-06-19 14:22:55 +0100452 try:
453 depthPitchMemberIndex = struct.getMemberByName(depthPitchMemberName)
454 except ValueError:
455 assert len(struct.members) < 3
456 pass
457 else:
458 assert depthPitchMemberName == 'DepthPitch'
459 print r' if (%s->DepthPitch) {' % outArg.name
460 print r' retrace::checkMismatch(call, "DepthPitch", _struct->members[%u], %s->DepthPitch);' % (struct.getMemberByName('DepthPitch'), outArg.name)
461 print r' }'
Jose Fonseca37f769f2017-06-16 17:57:39 +0100462 print r' }'
463 print r' }'
464 print r' }'
465
466
José Fonseca3be2c672015-02-06 15:36:40 +0000467 def extractArg(self, function, arg, arg_type, lvalue, rvalue):
468 # Set object names
469 if function.name == 'SetPrivateData' and arg.name == 'pData':
470 iid = function.args[0].name
471 print r' if (%s != WKPDID_D3DDebugObjectName) {' % iid
472 print r' return;'
473 print r' }'
474 # Interpret argument as string
475 Retracer.extractArg(self, function, arg, LPCSTR, lvalue, rvalue)
Józef Kuciae5be0832017-12-03 20:03:24 +0100476 print r' if (!pData) {'
477 print r' return;'
478 print r' }'
Jose Fonseca571c2fa2015-07-30 15:08:49 +0100479 print r' assert(DataSize >= strlen((const char *)pData));'
480 print r' // Some applications include the trailing zero terminator in the data'
481 print r' DataSize = strlen((const char *)pData);'
José Fonseca3be2c672015-02-06 15:36:40 +0000482 return
483
484 Retracer.extractArg(self, function, arg, arg_type, lvalue, rvalue)
485
José Fonseca6f810332012-11-11 10:05:09 +0000486
487def main():
José Fonseca75cbb8c2013-02-12 16:19:23 +0000488 print r'#define INITGUID'
489 print
José Fonseca73341c22012-11-24 13:04:42 +0000490 print r'#include <string.h>'
José Fonseca6f810332012-11-11 10:05:09 +0000491 print
492 print r'#include <iostream>'
493 print
494 print r'#include "d3dretrace.hpp"'
José Fonseca89b697f2014-06-17 20:26:38 +0100495 print r'#include "os_version.hpp"'
José Fonseca6f810332012-11-11 10:05:09 +0000496 print
José Fonseca50bee952015-02-07 22:32:19 +0000497 print r'#include "d3dretrace_dxgi.hpp"'
498 print r'#include "d3d10imports.hpp"'
499 print r'#include "d3d10size.hpp"'
500 print r'#include "d3d10state.hpp"'
501 print r'#include "d3d11imports.hpp"'
502 print r'#include "d3d11size.hpp"'
Jose Fonsecafeb8f7d2016-02-01 14:14:00 +0000503 print r'#include "dcompimports.hpp"'
José Fonseca50bee952015-02-07 22:32:19 +0000504 print r'#include "d3dstate.hpp"'
Jose Fonseca1e579c42017-03-22 14:08:19 +0000505 print r'#include "d3d9imports.hpp" // D3DERR_WASSTILLDRAWING'
José Fonseca50bee952015-02-07 22:32:19 +0000506 print
507 print '''static d3dretrace::D3DDumper<IDXGISwapChain> dxgiDumper;'''
508 print '''static d3dretrace::D3DDumper<ID3D10Device> d3d10Dumper;'''
509 print '''static d3dretrace::D3DDumper<ID3D11DeviceContext> d3d11Dumper;'''
510 print
José Fonseca6f810332012-11-11 10:05:09 +0000511
512 api = API()
José Fonseca50bee952015-02-07 22:32:19 +0000513 api.addModule(dxgi)
514 api.addModule(d3d10)
515 api.addModule(d3d10_1)
516 api.addModule(d3d11)
Jose Fonsecafeb8f7d2016-02-01 14:14:00 +0000517 api.addModule(dcomp)
José Fonsecafc58d052014-06-13 12:47:19 +0100518
José Fonseca6f810332012-11-11 10:05:09 +0000519 retracer = D3DRetracer()
520 retracer.retraceApi(api)
521
522
523if __name__ == '__main__':
524 main()