blob: 8126c615dbc6beaecdd4e26293f0d6e0cbc9b8e8 [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:
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 Fonseca5c487ec2015-06-02 22:16:52 +0100126
Jose Fonsecad0094a22017-05-30 12:31:37 +0100127 Retracer.handleFailure(self, interface, methodOrFunction)
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100128
José Fonseca42b89fc2012-11-27 12:19:45 +0000129 def forceDriver(self, enum):
José Fonseca82ea89d2012-12-05 19:34:26 +0000130 # 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é Fonseca42b89fc2012-11-27 12:19:45 +0000153 print r' if (!Software) {'
José Fonseca82ea89d2012-12-05 19:34:26 +0000154 print r' retrace::warning(call) << "failed to load " << retrace::driverModule << "\n";'
José Fonseca42b89fc2012-11-27 12:19:45 +0000155 print r' }'
José Fonseca82ea89d2012-12-05 19:34:26 +0000156 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é Fonseca42b89fc2012-11-27 12:19:45 +0000168 print r' }'
José Fonseca82ea89d2012-12-05 19:34:26 +0000169 print r' } else {'
170 print r' Software = NULL;'
José Fonseca42b89fc2012-11-27 12:19:45 +0000171 print r' }'
172
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100173 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é Fonseca610942b2012-11-08 10:46:03 +0000183 def invokeInterfaceMethod(self, interface, method):
184 # keep track of the last used device for state dumping
José Fonseca944088e2012-11-20 14:47:03 +0000185 if interface.name in ('ID3D10Device', 'ID3D10Device1'):
186 if method.name == 'Release':
José Fonsecadb62f0f2013-06-27 16:15:01 +0100187 print r' if (call.ret->toUInt() == 0) {'
188 print r' d3d10Dumper.unbindDevice(_this);'
189 print r' }'
José Fonseca944088e2012-11-20 14:47:03 +0000190 else:
191 print r' d3d10Dumper.bindDevice(_this);'
Jose Fonseca4a1c7aa2015-08-10 16:53:49 +0100192 if interface.name.startswith('ID3D11DeviceContext'):
José Fonseca5773beb2012-11-14 11:46:58 +0000193 if method.name == 'Release':
José Fonsecadb62f0f2013-06-27 16:15:01 +0100194 print r' if (call.ret->toUInt() == 0) {'
195 print r' d3d11Dumper.unbindDevice(_this);'
196 print r' }'
José Fonseca5773beb2012-11-14 11:46:58 +0000197 else:
Jose Fonsecab059dd82017-07-20 13:13:44 +0100198 print r' d3d11Dumper.bindDevice(_this);'
José Fonseca610942b2012-11-08 10:46:03 +0000199
José Fonseca1e309b42014-09-18 11:08:09 +0100200 # intercept private interfaces
José Fonsecad59843c2014-08-22 16:59:25 +0100201 if method.name == 'QueryInterface':
José Fonsecac44c62c2014-09-22 13:42:42 +0100202 print r' if (!d3dretrace::overrideQueryInterface(_this, riid, ppvObj, &_result)) {'
José Fonseca75cbb8c2013-02-12 16:19:23 +0000203 Retracer.invokeInterfaceMethod(self, interface, method)
204 print r' }'
205 return
206
José Fonseca610942b2012-11-08 10:46:03 +0000207 # create windows as neccessary
208 if method.name == 'CreateSwapChain':
José Fonseca1e309b42014-09-18 11:08:09 +0100209 print r' d3dretrace::createWindowForSwapChain(pDesc);'
Jose Fonsecadafb4ea2015-08-10 14:40:22 +0100210 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é Fonsecae23a6be2014-09-14 20:30:00 +0100216 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 Fonseca5c487ec2015-06-02 22:16:52 +0100219 self.checkResult(interface, method)
José Fonsecae23a6be2014-09-14 20:30:00 +0100220 return
Jose Fonsecafeb8f7d2016-02-01 14:14:00 +0000221 if method.name == 'CreateTargetForHwnd':
222 print r' hwnd = d3dretrace::createWindow(1024, 768);'
José Fonseca610942b2012-11-08 10:46:03 +0000223
José Fonseca7618cb92014-08-28 16:00:48 +0100224 if method.name == 'SetFullscreenState':
225 print r' if (retrace::forceWindowed) {'
226 print r' Fullscreen = FALSE;'
José Fonseca693382a2014-10-03 14:27:59 +0100227 print r' pTarget = NULL;'
José Fonseca7618cb92014-08-28 16:00:48 +0100228 print r' }'
229
José Fonseca610942b2012-11-08 10:46:03 +0000230 # notify frame has been completed
Jose Fonsecaced9c1c2015-08-10 15:10:37 +0100231 if interface.name.startswith('IDXGISwapChain') and method.name.startswith('Present'):
232 if interface.name.startswith('IDXGISwapChainDWM'):
José Fonseca1e309b42014-09-18 11:08:09 +0100233 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é Fonseca29d4bda2014-02-28 17:50:24 +0000239 else:
240 print r' dxgiDumper.bindDevice(_this);'
José Fonseca610942b2012-11-08 10:46:03 +0000241 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é Fonseca82ea89d2012-12-05 19:34:26 +0000249 # Force driver
José Fonsecaad6f60f2013-09-16 14:58:34 +0100250 if interface.name.startswith('IDXGIFactory') and method.name.startswith('EnumAdapters'):
José Fonseca82ea89d2012-12-05 19:34:26 +0000251 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é Fonsecaad6f60f2013-09-16 14:58:34 +0100271 print r' _result = _this->CreateSoftwareAdapter(hSoftware, reinterpret_cast<IDXGIAdapter **>(ppAdapter));'
José Fonseca82ea89d2012-12-05 19:34:26 +0000272 print r' } else {'
273 Retracer.invokeInterfaceMethod(self, interface, method)
274 print r' }'
275 return
276
José Fonsecae23a6be2014-09-14 20:30:00 +0100277 if interface.name.startswith('ID3D10Device') and method.name.startswith('OpenSharedResource'):
José Fonseca75cbb8c2013-02-12 16:19:23 +0000278 print r' retrace::warning(call) << "replacing shared resource with checker pattern\n";'
Jose Fonsecadf913772015-07-17 16:54:47 +0100279 print r' _result = d3dretrace::createSharedResource(_this, ReturnedInterface, ppResource);'
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100280 self.checkResult(interface, method)
José Fonseca999284f2013-02-19 13:29:26 +0000281 return
Jose Fonseca559b5f82015-07-17 16:58:50 +0100282 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é Fonseca2b66b932014-07-30 17:31:52 +0100295 if interface.name.startswith('ID3D11Device') and method.name.startswith('OpenSharedResource'):
296 print r' retrace::warning(call) << "replacing shared resource with checker pattern\n";'
Jose Fonsecadf913772015-07-17 16:54:47 +0100297 print r' _result = d3dretrace::createSharedResource(_this, ReturnedInterface, ppResource);'
José Fonseca2b66b932014-07-30 17:31:52 +0100298 if method.name == 'OpenSharedResourceByName':
299 print r' (void)lpName;'
300 print r' (void)dwDesiredAccess;'
Jose Fonsecadf913772015-07-17 16:54:47 +0100301 else:
302 print r' (void)hResource;'
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100303 self.checkResult(interface, method)
José Fonseca2b66b932014-07-30 17:31:52 +0100304 return
José Fonseca999284f2013-02-19 13:29:26 +0000305
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é Fonseca7af569a2014-09-04 19:08:45 +0100314 if method.name.startswith('UpdateSubresource'):
José Fonsecac9cc24e2014-06-13 19:06:51 +0100315 # 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é Fonseca0688d2e2014-08-19 20:43:21 +0100325 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 Fonseca60e65142017-06-09 13:06:32 +0100331 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é Fonseca999284f2013-02-19 13:29:26 +0000340 Retracer.invokeInterfaceMethod(self, interface, method)
José Fonseca610942b2012-11-08 10:46:03 +0000341
Jose Fonseca4d5cfa42015-07-17 16:21:05 +0100342 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é Fonseca610942b2012-11-08 10:46:03 +0000347 # process events after presents
Jose Fonsecaced9c1c2015-08-10 15:10:37 +0100348 if interface.name.startswith('IDXGISwapChain') and method.name.startswith('Present'):
José Fonseca610942b2012-11-08 10:46:03 +0000349 print r' d3dretrace::processEvents();'
350
José Fonseca6ca20082014-10-07 21:39:43 +0100351 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é Fonseca610942b2012-11-08 10:46:03 +0000360 if method.name == 'Map':
José Fonseca003b8be2013-05-29 19:59:40 +0100361 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é Fonseca6ca20082014-10-07 21:39:43 +0100365 print ' _pbData = _MapDesc.pData;'
Jeff Muizelaar97244f92013-11-19 15:17:45 -0500366 if interface.name.startswith('ID3D11DeviceContext'):
Jose Fonseca1e3c0f72015-07-20 21:50:42 +0100367 # Prevent false warnings on 1D and 2D resources, since the
368 # pitches are often junk there...
369 print ' _normalizeMap(pResource, pMappedResource);'
Jeff Muizelaar97244f92013-11-19 15:17:45 -0500370 else:
José Fonseca6ca20082014-10-07 21:39:43 +0100371 print ' _pbData = _MapDesc.pData;'
José Fonseca4f49d212012-11-14 14:02:35 +0000372 print ' } else {'
373 print ' return;'
374 print ' }'
José Fonseca610942b2012-11-08 10:46:03 +0000375
376 if method.name == 'Unmap':
José Fonseca6ca20082014-10-07 21:39:43 +0100377 print ' if (_pbData) {'
378 print ' retrace::delRegionByPointer(_pbData);'
379 print ' _pbData = 0;'
380 print ' }'
José Fonseca6f810332012-11-11 10:05:09 +0000381
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100382 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é Fonseca1d4fd142012-11-28 15:06:22 +0000395 # 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 Fonseca37f769f2017-06-16 17:57:39 +0100403 def retraceInterfaceMethodBody(self, interface, method):
404 Retracer.retraceInterfaceMethodBody(self, interface, method)
405
406 # Add pitch swizzling information to the region
Jose Fonsecab2754af2017-06-19 14:22:55 +0100407 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 Fonseca37f769f2017-06-16 17:57:39 +0100424 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 Fonsecab2754af2017-06-19 14:22:55 +0100428 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 Fonseca37f769f2017-06-16 17:57:39 +0100431 print r' if (realRowPitch && traceRowPitch != realRowPitch) {'
432 print r' retrace::setRegionPitch(traceAddress, 2, traceRowPitch, realRowPitch);'
Jose Fonseca37f769f2017-06-16 17:57:39 +0100433 print r' }'
Jose Fonsecab2754af2017-06-19 14:22:55 +0100434 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 Fonseca37f769f2017-06-16 17:57:39 +0100444 print r' }'
445 print r' }'
446 print r' }'
447
448
José Fonseca3be2c672015-02-06 15:36:40 +0000449 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 Fonseca571c2fa2015-07-30 15:08:49 +0100459 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é Fonseca3be2c672015-02-06 15:36:40 +0000462 return
463
464 Retracer.extractArg(self, function, arg, arg_type, lvalue, rvalue)
465
José Fonseca6f810332012-11-11 10:05:09 +0000466
467def main():
José Fonseca75cbb8c2013-02-12 16:19:23 +0000468 print r'#define INITGUID'
469 print
José Fonseca73341c22012-11-24 13:04:42 +0000470 print r'#include <string.h>'
José Fonseca6f810332012-11-11 10:05:09 +0000471 print
472 print r'#include <iostream>'
473 print
474 print r'#include "d3dretrace.hpp"'
José Fonseca89b697f2014-06-17 20:26:38 +0100475 print r'#include "os_version.hpp"'
José Fonseca6f810332012-11-11 10:05:09 +0000476 print
José Fonseca50bee952015-02-07 22:32:19 +0000477 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 Fonsecafeb8f7d2016-02-01 14:14:00 +0000483 print r'#include "dcompimports.hpp"'
José Fonseca50bee952015-02-07 22:32:19 +0000484 print r'#include "d3dstate.hpp"'
Jose Fonseca1e579c42017-03-22 14:08:19 +0000485 print r'#include "d3d9imports.hpp" // D3DERR_WASSTILLDRAWING'
José Fonseca50bee952015-02-07 22:32:19 +0000486 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é Fonseca6f810332012-11-11 10:05:09 +0000491
492 api = API()
José Fonseca50bee952015-02-07 22:32:19 +0000493 api.addModule(dxgi)
494 api.addModule(d3d10)
495 api.addModule(d3d10_1)
496 api.addModule(d3d11)
Jose Fonsecafeb8f7d2016-02-01 14:14:00 +0000497 api.addModule(dcomp)
José Fonsecafc58d052014-06-13 12:47:19 +0100498
José Fonseca6f810332012-11-11 10:05:09 +0000499 retracer = D3DRetracer()
500 retracer.retraceApi(api)
501
502
503if __name__ == '__main__':
504 main()