blob: f5c2a2954651f182863811204899df36fc7428f9 [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):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +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;')
47 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():
Danylo Piliaieve0bfba62019-06-06 13:36:08 +030065 print(r' if (pSwapChainDesc) {')
66 print(r' d3dretrace::createWindowForSwapChain(pSwapChainDesc);')
67 print(r' }')
José Fonseca4f49d212012-11-14 14:02:35 +000068
José Fonseca4f49d212012-11-14 14:02:35 +000069 # Compensate for the fact we don't trace DXGI object creation
70 if function.name.startswith('D3D11CreateDevice'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010071 print(r' if (DriverType == D3D_DRIVER_TYPE_UNKNOWN && !pAdapter) {')
72 print(r' DriverType = D3D_DRIVER_TYPE_HARDWARE;')
73 print(r' }')
José Fonsecaea799192012-11-13 21:37:24 +000074
José Fonseca42b89fc2012-11-27 12:19:45 +000075 if function.name.startswith('D3D10CreateDevice'):
José Fonseca7bcc96c2012-12-05 19:28:22 +000076 # Toggle debugging
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010077 print(r' if (retrace::debug >= 2) {')
78 print(r' Flags |= D3D10_CREATE_DEVICE_DEBUG;')
79 print(r' } else if (retrace::debug < 0) {')
80 print(r' Flags &= ~D3D10_CREATE_DEVICE_DEBUG;')
81 print(r' }')
José Fonseca7bcc96c2012-12-05 19:28:22 +000082
83 # Force driver
José Fonseca42b89fc2012-11-27 12:19:45 +000084 self.forceDriver('D3D10_DRIVER_TYPE')
José Fonseca7bcc96c2012-12-05 19:28:22 +000085
José Fonseca42b89fc2012-11-27 12:19:45 +000086 if function.name.startswith('D3D11CreateDevice'):
José Fonseca7bcc96c2012-12-05 19:28:22 +000087 # Toggle debugging
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010088 print(r' if (retrace::debug >= 2) {')
89 print(r' Flags |= D3D11_CREATE_DEVICE_DEBUG;')
90 print(r' } else if (retrace::debug < 0) {')
91 print(r' Flags &= ~D3D11_CREATE_DEVICE_DEBUG;')
92 print(r' }')
93 print(r' if (IsWindows8OrGreater()) {')
94 print(r' Flags |= D3D11_CREATE_DEVICE_DISABLE_GPU_TIMEOUT;')
95 print(r' }')
José Fonseca7bcc96c2012-12-05 19:28:22 +000096
97 # Force driver
José Fonseca42b89fc2012-11-27 12:19:45 +000098 self.forceDriver('D3D_DRIVER_TYPE')
99
José Fonseca610942b2012-11-08 10:46:03 +0000100 Retracer.invokeFunction(self, function)
101
Jose Fonsecaf39fd602017-08-23 15:13:39 +0100102 def doInvokeFunction(self, function):
103 Retracer.doInvokeFunction(self, function)
104
105 # Handle missing debug layer. While it's possible to detect whether
106 # the debug layers are present, by creating a null device, and checking
107 # the result. It's simpler to retry.
108 if function.name.startswith('D3D10CreateDevice'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100109 print(r' if ((_result == E_FAIL || _result == DXGI_ERROR_SDK_COMPONENT_MISSING) && (Flags & D3D10_CREATE_DEVICE_DEBUG)) {')
110 print(r' retrace::warning(call) << "Direct3D 10.x SDK Debug Layer (d3d10sdklayers.dll) not available, continuing without debug output\n";')
111 print(r' Flags &= ~D3D10_CREATE_DEVICE_DEBUG;')
Jose Fonsecaf39fd602017-08-23 15:13:39 +0100112 Retracer.doInvokeFunction(self, function)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100113 print(r' }')
Jose Fonsecaf39fd602017-08-23 15:13:39 +0100114 if function.name.startswith('D3D11CreateDevice'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100115 print(r' if ((_result == E_FAIL || _result == DXGI_ERROR_SDK_COMPONENT_MISSING) && (Flags & D3D11_CREATE_DEVICE_DEBUG)) {')
116 print(r' retrace::warning(call) << "Direct3D 11.x SDK Debug Layer (d3d11*sdklayers.dll) not available, continuing without debug output\n";')
117 print(r' Flags &= ~D3D11_CREATE_DEVICE_DEBUG;')
Jose Fonsecaf39fd602017-08-23 15:13:39 +0100118 Retracer.doInvokeFunction(self, function)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100119 print(r' }')
Jose Fonsecaf39fd602017-08-23 15:13:39 +0100120
Jose Fonsecad0094a22017-05-30 12:31:37 +0100121 def handleFailure(self, interface, methodOrFunction):
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100122 # Catch when device is removed, and report the reason.
Jose Fonsecad0094a22017-05-30 12:31:37 +0100123 if interface is not None:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100124 print(r' if (_result == DXGI_ERROR_DEVICE_REMOVED) {')
Jose Fonsecad0094a22017-05-30 12:31:37 +0100125 getDeviceRemovedReasonMethod = interface.getMethodByName("GetDeviceRemovedReason")
126 if getDeviceRemovedReasonMethod is not None:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100127 print(r' HRESULT _reason = _this->GetDeviceRemovedReason();')
128 print(r' retrace::failed(call, _reason);')
Jose Fonsecaa42384c2018-01-12 15:37:52 +0000129 getDeviceMethod = interface.getMethodByName("GetDevice")
130 if getDeviceMethod is not None and len(getDeviceMethod.args) == 1:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100131 print(r' com_ptr<%s> _pDevice;' % getDeviceMethod.args[0].type.type.type)
132 print(r' _this->GetDevice(&_pDevice);')
133 print(r' HRESULT _reason = _pDevice->GetDeviceRemovedReason();')
134 print(r' retrace::failed(call, _reason);')
135 print(r' exit(EXIT_FAILURE);')
136 print(r' }')
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100137
Jose Fonsecad0094a22017-05-30 12:31:37 +0100138 Retracer.handleFailure(self, interface, methodOrFunction)
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100139
José Fonseca42b89fc2012-11-27 12:19:45 +0000140 def forceDriver(self, enum):
José Fonseca82ea89d2012-12-05 19:34:26 +0000141 # This can only work when pAdapter is NULL. For non-NULL pAdapter we
142 # need to override inside the EnumAdapters call below
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100143 print(r' if (pAdapter == NULL) {')
144 print(r' switch (retrace::driver) {')
145 print(r' case retrace::DRIVER_INTEGRATED:')
146 print(r' retrace::warning(call) << "integrated gpu selection not yet implemented\n";')
147 print(r' case retrace::DRIVER_DISCRETE:')
148 print(r' case retrace::DRIVER_HARDWARE:')
149 print(r' DriverType = %s_HARDWARE;' % enum)
150 print(r' Software = NULL;')
151 print(r' break;')
152 print(r' case retrace::DRIVER_SOFTWARE:')
153 print(r' DriverType = %s_WARP;' % enum)
154 print(r' Software = NULL;')
155 print(r' break;')
156 print(r' case retrace::DRIVER_REFERENCE:')
157 print(r' DriverType = %s_REFERENCE;' % enum)
158 print(r' Software = NULL;')
159 print(r' break;')
160 print(r' case retrace::DRIVER_NULL:')
161 print(r' DriverType = %s_NULL;' % enum)
162 print(r' Software = NULL;')
163 print(r' break;')
164 print(r' case retrace::DRIVER_MODULE:')
165 print(r' DriverType = %s_SOFTWARE;' % enum)
166 print(r' Software = LoadLibraryA(retrace::driverModule);')
167 print(r' if (!Software) {')
168 print(r' retrace::warning(call) << "failed to load " << retrace::driverModule << "\n";')
169 print(r' }')
170 print(r' break;')
171 print(r' default:')
172 print(r' assert(0);')
173 print(r' /* fall-through */')
174 print(r' case retrace::DRIVER_DEFAULT:')
175 print(r' if (DriverType == %s_SOFTWARE) {' % enum)
176 print(r' Software = LoadLibraryA("d3d10warp");')
177 print(r' if (!Software) {')
178 print(r' retrace::warning(call) << "failed to load d3d10warp.dll\n";')
179 print(r' }')
180 print(r' }')
181 print(r' break;')
182 print(r' }')
183 print(r' } else {')
184 print(r' Software = NULL;')
185 print(r' }')
José Fonseca42b89fc2012-11-27 12:19:45 +0000186
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100187 def doInvokeInterfaceMethod(self, interface, method):
188 Retracer.doInvokeInterfaceMethod(self, interface, method)
189
Jose Fonseca95796502018-11-20 13:11:31 +0000190 # Force driver
191 if interface.name.startswith('IDXGIFactory') and method.name.startswith('EnumAdapters'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100192 print(r' const char *szSoftware = NULL;')
193 print(r' switch (retrace::driver) {')
194 print(r' case retrace::DRIVER_REFERENCE:')
195 print(r' szSoftware = "d3d11ref.dll";')
196 print(r' break;')
197 print(r' case retrace::DRIVER_SOFTWARE:')
198 print(r' szSoftware = "d3d10warp.dll";')
199 print(r' break;')
200 print(r' case retrace::DRIVER_MODULE:')
201 print(r' szSoftware = retrace::driverModule;')
202 print(r' break;')
203 print(r' default:')
204 print(r' break;')
205 print(r' }')
206 print(r' HMODULE hSoftware = NULL;')
207 print(r' if (szSoftware) {')
208 print(r' hSoftware = LoadLibraryA(szSoftware);')
209 print(r' if (!hSoftware) {')
210 print(r' retrace::warning(call) << "failed to load " << szSoftware << "\n";')
211 print(r' }')
212 print(r' }')
213 print(r' if (hSoftware) {')
214 print(r' _result = _this->CreateSoftwareAdapter(hSoftware, reinterpret_cast<IDXGIAdapter **>(ppAdapter));')
215 print(r' } else {')
216 print(r' if (Adapter != 0) {')
217 print(r' retrace::warning(call) << "ignoring non-default adapter " << Adapter << "\n";')
218 print(r' Adapter = 0;')
219 print(r' }')
Jose Fonseca95796502018-11-20 13:11:31 +0000220 Retracer.doInvokeInterfaceMethod(self, interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100221 print(r' }')
Jose Fonseca95796502018-11-20 13:11:31 +0000222 return
223
224 if interface.name.startswith('IDXGIFactory') and method.name == 'CreateSoftwareAdapter':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100225 print(r' const char *szSoftware = NULL;')
226 print(r' switch (retrace::driver) {')
227 print(r' case retrace::DRIVER_REFERENCE:')
228 print(r' szSoftware = "d3d11ref.dll";')
229 print(r' break;')
230 print(r' case retrace::DRIVER_MODULE:')
231 print(r' szSoftware = retrace::driverModule;')
232 print(r' break;')
233 print(r' case retrace::DRIVER_SOFTWARE:')
234 print(r' default:')
235 print(r' szSoftware = "d3d10warp.dll";')
236 print(r' break;')
237 print(r' }')
238 print(r' Module = LoadLibraryA("d3d10warp");')
239 print(r' if (!Module) {')
240 print(r' retrace::warning(call) << "failed to load " << szSoftware << "\n";')
241 print(r' }')
Jose Fonseca95796502018-11-20 13:11:31 +0000242 Retracer.doInvokeInterfaceMethod(self, interface, method)
243
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100244 # Keep retrying ID3D11VideoContext::DecoderBeginFrame when returns E_PENDING
245 if interface.name == 'ID3D11VideoContext' and method.name == 'DecoderBeginFrame':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100246 print(r' while (_result == D3DERR_WASSTILLDRAWING || _result == E_PENDING) {')
247 print(r' Sleep(1);')
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100248 Retracer.doInvokeInterfaceMethod(self, interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100249 print(r' }')
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100250
José Fonseca610942b2012-11-08 10:46:03 +0000251 def invokeInterfaceMethod(self, interface, method):
252 # keep track of the last used device for state dumping
José Fonseca944088e2012-11-20 14:47:03 +0000253 if interface.name in ('ID3D10Device', 'ID3D10Device1'):
254 if method.name == 'Release':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100255 print(r' if (call.ret->toUInt() == 0) {')
256 print(r' d3d10Dumper.unbindDevice(_this);')
257 print(r' }')
José Fonseca944088e2012-11-20 14:47:03 +0000258 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100259 print(r' d3d10Dumper.bindDevice(_this);')
Jose Fonseca4a1c7aa2015-08-10 16:53:49 +0100260 if interface.name.startswith('ID3D11DeviceContext'):
José Fonseca5773beb2012-11-14 11:46:58 +0000261 if method.name == 'Release':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100262 print(r' if (call.ret->toUInt() == 0) {')
263 print(r' d3d11Dumper.unbindDevice(_this);')
264 print(r' }')
José Fonseca5773beb2012-11-14 11:46:58 +0000265 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100266 print(r' d3d11Dumper.bindDevice(_this);')
José Fonseca610942b2012-11-08 10:46:03 +0000267
José Fonseca1e309b42014-09-18 11:08:09 +0100268 # intercept private interfaces
José Fonsecad59843c2014-08-22 16:59:25 +0100269 if method.name == 'QueryInterface':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100270 print(r' if (!d3dretrace::overrideQueryInterface(_this, riid, ppvObj, &_result)) {')
José Fonseca75cbb8c2013-02-12 16:19:23 +0000271 Retracer.invokeInterfaceMethod(self, interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100272 print(r' }')
José Fonseca75cbb8c2013-02-12 16:19:23 +0000273 return
274
José Fonseca610942b2012-11-08 10:46:03 +0000275 # create windows as neccessary
276 if method.name == 'CreateSwapChain':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100277 print(r' d3dretrace::createWindowForSwapChain(pDesc);')
Jose Fonsecadafb4ea2015-08-10 14:40:22 +0100278 if method.name == 'CreateSwapChainForHwnd':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100279 print(r' hWnd = d3dretrace::createWindow(pDesc->Width, pDesc->Height);')
280 print(r' // DXGI_SCALING_NONE is only supported on Win8 and beyond')
281 print(r' if (pDesc->Scaling == DXGI_SCALING_NONE && !IsWindows8OrGreater()) {')
282 print(r' pDesc->Scaling = DXGI_SCALING_STRETCH;')
283 print(r' }')
José Fonsecae23a6be2014-09-14 20:30:00 +0100284 if method.name == 'CreateSwapChainForComposition':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100285 print(r' HWND hWnd = d3dretrace::createWindow(pDesc->Width, pDesc->Height);')
286 print(r' _result = _this->CreateSwapChainForHwnd(pDevice, hWnd, pDesc, NULL, pRestrictToOutput, ppSwapChain);')
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100287 self.checkResult(interface, method)
José Fonsecae23a6be2014-09-14 20:30:00 +0100288 return
Jose Fonsecafeb8f7d2016-02-01 14:14:00 +0000289 if method.name == 'CreateTargetForHwnd':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100290 print(r' hwnd = d3dretrace::createWindow(1024, 768);')
José Fonseca610942b2012-11-08 10:46:03 +0000291
José Fonseca7618cb92014-08-28 16:00:48 +0100292 if method.name == 'SetFullscreenState':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100293 print(r' if (retrace::forceWindowed) {')
294 print(r' DXGI_SWAP_CHAIN_DESC Desc;')
295 print(r' _this->GetDesc(&Desc);')
296 print(r' if (Desc.BufferDesc.Format != DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM) {')
297 print(r' Fullscreen = FALSE;')
298 print(r' pTarget = nullptr;')
299 print(r' }')
300 print(r' }')
José Fonseca7618cb92014-08-28 16:00:48 +0100301
José Fonseca610942b2012-11-08 10:46:03 +0000302 # notify frame has been completed
Jose Fonsecaced9c1c2015-08-10 15:10:37 +0100303 if interface.name.startswith('IDXGISwapChain') and method.name.startswith('Present'):
304 if interface.name.startswith('IDXGISwapChainDWM'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100305 print(r' com_ptr<IDXGISwapChain> pSwapChain;')
306 print(r' if (SUCCEEDED(_this->QueryInterface(IID_IDXGISwapChain, (void **) &pSwapChain))) {')
307 print(r' dxgiDumper.bindDevice(pSwapChain);')
308 print(r' } else {')
309 print(r' assert(0);')
310 print(r' }')
José Fonseca29d4bda2014-02-28 17:50:24 +0000311 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100312 print(r' dxgiDumper.bindDevice(_this);')
Jose Fonseca0f541f42019-07-03 13:46:05 +0100313 print(r' if ((Flags & DXGI_PRESENT_TEST) == 0) {')
314 print(r' retrace::frameComplete(call);')
315 print(r' }')
José Fonseca610942b2012-11-08 10:46:03 +0000316
317 if 'pSharedResource' in method.argNames():
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100318 print(r' if (pSharedResource) {')
319 print(r' retrace::warning(call) << "shared surfaces unsupported\n";')
320 print(r' pSharedResource = NULL;')
321 print(r' }')
José Fonseca610942b2012-11-08 10:46:03 +0000322
José Fonsecae23a6be2014-09-14 20:30:00 +0100323 if interface.name.startswith('ID3D10Device') and method.name.startswith('OpenSharedResource'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100324 print(r' retrace::warning(call) << "replacing shared resource with checker pattern\n";')
325 print(r' _result = d3dretrace::createSharedResource(_this, ReturnedInterface, ppResource);')
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100326 self.checkResult(interface, method)
José Fonseca999284f2013-02-19 13:29:26 +0000327 return
Jose Fonseca559b5f82015-07-17 16:58:50 +0100328 if interface.name.startswith('ID3D11Device') and method.name == 'OpenSharedResource':
329 # Some applications (e.g., video playing in IE11) create shared resources within the same process.
330 # TODO: Generalize to other OpenSharedResource variants
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100331 print(r' retrace::map<HANDLE>::const_iterator it = _shared_handle_map.find(hResource);')
332 print(r' if (it == _shared_handle_map.end()) {')
333 print(r' retrace::warning(call) << "replacing shared resource with checker pattern\n";')
334 print(r' _result = d3dretrace::createSharedResource(_this, ReturnedInterface, ppResource);')
Jose Fonseca559b5f82015-07-17 16:58:50 +0100335 self.checkResult(interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100336 print(r' } else {')
337 print(r' hResource = it->second;')
Jose Fonseca559b5f82015-07-17 16:58:50 +0100338 Retracer.invokeInterfaceMethod(self, interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100339 print(r' }')
Jose Fonseca559b5f82015-07-17 16:58:50 +0100340 return
José Fonseca2b66b932014-07-30 17:31:52 +0100341 if interface.name.startswith('ID3D11Device') and method.name.startswith('OpenSharedResource'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100342 print(r' retrace::warning(call) << "replacing shared resource with checker pattern\n";')
343 print(r' _result = d3dretrace::createSharedResource(_this, ReturnedInterface, ppResource);')
José Fonseca2b66b932014-07-30 17:31:52 +0100344 if method.name == 'OpenSharedResourceByName':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100345 print(r' (void)lpName;')
346 print(r' (void)dwDesiredAccess;')
Jose Fonsecadf913772015-07-17 16:54:47 +0100347 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100348 print(r' (void)hResource;')
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100349 self.checkResult(interface, method)
José Fonseca2b66b932014-07-30 17:31:52 +0100350 return
José Fonseca999284f2013-02-19 13:29:26 +0000351
352 if method.name == 'Map':
353 # Reset _DO_NOT_WAIT flags. Otherwise they may fail, and we have no
354 # way to cope with it (other than retry).
355 mapFlagsArg = method.getArgByName('MapFlags')
356 for flag in mapFlagsArg.type.values:
357 if flag.endswith('_MAP_FLAG_DO_NOT_WAIT'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100358 print(r' MapFlags &= ~%s;' % flag)
José Fonseca999284f2013-02-19 13:29:26 +0000359
José Fonseca7af569a2014-09-04 19:08:45 +0100360 if method.name.startswith('UpdateSubresource'):
José Fonsecac9cc24e2014-06-13 19:06:51 +0100361 # The D3D10 debug layer is buggy (or at least inconsistent with the
362 # runtime), as it seems to estimate and enforce the data size based on the
363 # SrcDepthPitch, even for non 3D textures, but in some traces
364 # SrcDepthPitch is garbagge for non 3D textures.
365 # XXX: It also seems to expect padding bytes at the end of the last
366 # row, but we never record (or allocate) those...
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100367 print(r' if (retrace::debug >= 2 && pDstBox && pDstBox->front == 0 && pDstBox->back == 1) {')
368 print(r' SrcDepthPitch = 0;')
369 print(r' }')
José Fonsecac9cc24e2014-06-13 19:06:51 +0100370
José Fonseca0688d2e2014-08-19 20:43:21 +0100371 if method.name == 'SetGammaControl':
372 # This method is only supported while in full-screen mode
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100373 print(r' if (retrace::forceWindowed) {')
374 print(r' return;')
375 print(r' }')
José Fonseca0688d2e2014-08-19 20:43:21 +0100376
Jose Fonseca60e65142017-06-09 13:06:32 +0100377 if method.name == 'GetData':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100378 print(r' pData = _allocator.alloc(DataSize);')
379 print(r' do {')
Jose Fonseca60e65142017-06-09 13:06:32 +0100380 self.doInvokeInterfaceMethod(interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100381 print(r' GetDataFlags = 0; // Prevent infinite loop')
382 print(r' } while (_result == S_FALSE);')
Jose Fonseca60e65142017-06-09 13:06:32 +0100383 self.checkResult(interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100384 print(r' return;')
Jose Fonseca60e65142017-06-09 13:06:32 +0100385
Jose Fonsecabfdf93e2019-06-26 13:03:27 +0100386 if method.name in ('CreateTexture1D', 'CreateTexture2D', 'CreateTexture3D', 'CreateBuffer'):
Jose Fonseca3d2b3fe2019-06-28 14:32:14 +0100387 # We don't capture multiple processes, so ignore keyed mutexes to avoid deadlocks
388 print(r' if (pDesc->MiscFlags & D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX) {')
389 print(r' pDesc->MiscFlags &= ~D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;')
390 print(r' pDesc->MiscFlags |= D3D11_RESOURCE_MISC_SHARED;')
391 print(r' }')
Jose Fonsecabfdf93e2019-06-26 13:03:27 +0100392
José Fonseca999284f2013-02-19 13:29:26 +0000393 Retracer.invokeInterfaceMethod(self, interface, method)
José Fonseca610942b2012-11-08 10:46:03 +0000394
Jose Fonseca4d5cfa42015-07-17 16:21:05 +0100395 if method.name in ('AcquireSync', 'ReleaseSync'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100396 print(r' if (SUCCEEDED(_result) && _result != S_OK) {')
397 print(r' retrace::warning(call) << " returned " << _result << "\n";')
398 print(r' }')
Jose Fonseca4d5cfa42015-07-17 16:21:05 +0100399
José Fonseca610942b2012-11-08 10:46:03 +0000400 # process events after presents
Jose Fonsecaced9c1c2015-08-10 15:10:37 +0100401 if interface.name.startswith('IDXGISwapChain') and method.name.startswith('Present'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100402 print(r' d3dretrace::processEvents();')
José Fonseca610942b2012-11-08 10:46:03 +0000403
José Fonseca6ca20082014-10-07 21:39:43 +0100404 if method.name in ('Map', 'Unmap'):
405 if interface.name.startswith('ID3D11DeviceContext'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100406 print(' void * & _pbData = g_Maps[_this][SubresourceKey(pResource, Subresource)];')
José Fonseca6ca20082014-10-07 21:39:43 +0100407 else:
408 subresourceArg = method.getArgByName('Subresource')
409 if subresourceArg is None:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100410 print(' UINT Subresource = 0;')
411 print(' void * & _pbData = g_Maps[0][SubresourceKey(_this, Subresource)];')
José Fonseca6ca20082014-10-07 21:39:43 +0100412
José Fonseca610942b2012-11-08 10:46:03 +0000413 if method.name == 'Map':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100414 print(' _MAP_DESC _MapDesc;')
415 print(' _getMapDesc(_this, %s, _MapDesc);' % ', '.join(method.argNames()))
416 print(' size_t _MappedSize = _MapDesc.Size;')
417 print(' if (_MapDesc.Size) {')
418 print(' _pbData = _MapDesc.pData;')
Jeff Muizelaar97244f92013-11-19 15:17:45 -0500419 if interface.name.startswith('ID3D11DeviceContext'):
Jose Fonseca1e3c0f72015-07-20 21:50:42 +0100420 # Prevent false warnings on 1D and 2D resources, since the
421 # pitches are often junk there...
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100422 print(' _normalizeMap(pResource, pMappedResource);')
Jeff Muizelaar97244f92013-11-19 15:17:45 -0500423 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100424 print(' _pbData = _MapDesc.pData;')
425 print(' } else {')
426 print(' return;')
427 print(' }')
Zack Rusin1cb7b772018-02-09 11:36:11 -0500428
José Fonseca610942b2012-11-08 10:46:03 +0000429 if method.name == 'Unmap':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100430 print(' if (_pbData) {')
431 print(' retrace::delRegionByPointer(_pbData);')
432 print(' _pbData = 0;')
433 print(' }')
José Fonseca6f810332012-11-11 10:05:09 +0000434
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100435 if interface.name.startswith('ID3D11VideoContext'):
436 if method.name == 'GetDecoderBuffer':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100437 print(' if (*ppBuffer && *pBufferSize) {')
438 print(' g_Maps[nullptr][SubresourceKey(_this, Type)] = *ppBuffer;')
439 print(' }')
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100440 if method.name == 'ReleaseDecoderBuffer':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100441 print(' SubresourceKey _mappingKey(_this, Type);')
442 print(' void *_pBuffer = g_Maps[nullptr][_mappingKey];')
443 print(' if (_pBuffer) {')
444 print(' retrace::delRegionByPointer(_pBuffer);')
445 print(' g_Maps[nullptr][_mappingKey] = 0;')
446 print(' }')
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100447
José Fonseca1d4fd142012-11-28 15:06:22 +0000448 # Attach shader byte code for lookup
449 if 'pShaderBytecode' in method.argNames():
450 ppShader = method.args[-1]
451 assert ppShader.output
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100452 print(r' if (retrace::dumpingState && SUCCEEDED(_result)) {')
453 print(r' (*%s)->SetPrivateData(d3dstate::GUID_D3DSTATE, BytecodeLength, pShaderBytecode);' % ppShader.name)
454 print(r' }')
José Fonseca1d4fd142012-11-28 15:06:22 +0000455
Zack Rusin1cb7b772018-02-09 11:36:11 -0500456 if method.name == 'CreateBuffer':
457 ppBuffer = method.args[-1]
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100458 print(r' if (retrace::dumpingState && SUCCEEDED(_result)) {')
459 print(r' char label[32];')
460 print(r' _snprintf(label, sizeof label, "0x%%llx", call.arg(%u).toArray()->values[0]->toUIntPtr());' % ppBuffer.index)
461 print(r' (*%s)->SetPrivateData(WKPDID_D3DDebugObjectName, strlen(label)+1, label);' % ppBuffer.name)
462 print(r' }')
Zack Rusin1cb7b772018-02-09 11:36:11 -0500463
Jose Fonseca37f769f2017-06-16 17:57:39 +0100464 def retraceInterfaceMethodBody(self, interface, method):
465 Retracer.retraceInterfaceMethodBody(self, interface, method)
466
467 # Add pitch swizzling information to the region
Jose Fonsecab2754af2017-06-19 14:22:55 +0100468 if method.name == 'Map' and interface.name not in ('ID3D10Buffer', 'ID3D10Texture1D'):
469 if interface.name.startswith('ID3D11DeviceContext'):
470 outArg = method.getArgByName('pMappedResource')
471 memberNames = ('pData', 'RowPitch', 'DepthPitch')
472 elif interface.name.startswith('ID3D10'):
473 outArg = method.args[-1]
474 memberNames = ('pData', 'RowPitch', 'DepthPitch')
475 elif interface.name == 'IDXGISurface':
476 outArg = method.getArgByName('pLockedRect')
477 memberNames = ('pBits', 'Pitch', None)
478 else:
479 raise NotImplementedError
480 struct = outArg.type.type
481 dataMemberName, rowPitchMemberName, depthPitchMemberName = memberNames
482 dataMemberIndex = struct.getMemberByName(dataMemberName)
483 rowPitchMemberIndex = struct.getMemberByName(rowPitchMemberName)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100484 print(r' if (_pbData && %s->%s != 0) {' % (outArg.name, rowPitchMemberName))
485 print(r' const trace::Array *_%s = call.arg(%u).toArray();' % (outArg.name, outArg.index))
486 print(r' if (%s) {' % outArg.name)
487 print(r' const trace::Struct *_struct = _%s->values[0]->toStruct();' % (outArg.name))
488 print(r' if (_struct) {')
489 print(r' unsigned long long traceAddress = _struct->members[%u]->toUIntPtr();' % dataMemberIndex)
490 print(r' int traceRowPitch = _struct->members[%u]->toSInt();' % rowPitchMemberIndex)
491 print(r' int realRowPitch = %s->%s;' % (outArg.name, rowPitchMemberName))
492 print(r' if (realRowPitch && traceRowPitch != realRowPitch) {')
493 print(r' retrace::setRegionPitch(traceAddress, 2, traceRowPitch, realRowPitch);')
494 print(r' }')
Jose Fonsecab2754af2017-06-19 14:22:55 +0100495 try:
496 depthPitchMemberIndex = struct.getMemberByName(depthPitchMemberName)
497 except ValueError:
498 assert len(struct.members) < 3
499 pass
500 else:
501 assert depthPitchMemberName == 'DepthPitch'
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100502 print(r' if (%s->DepthPitch) {' % outArg.name)
503 print(r' retrace::checkMismatch(call, "DepthPitch", _struct->members[%u], %s->DepthPitch);' % (struct.getMemberByName('DepthPitch'), outArg.name))
504 print(r' }')
505 print(r' }')
506 print(r' }')
507 print(r' }')
Jose Fonseca37f769f2017-06-16 17:57:39 +0100508
509
José Fonseca3be2c672015-02-06 15:36:40 +0000510 def extractArg(self, function, arg, arg_type, lvalue, rvalue):
511 # Set object names
512 if function.name == 'SetPrivateData' and arg.name == 'pData':
513 iid = function.args[0].name
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100514 print(r' if (%s != WKPDID_D3DDebugObjectName) {' % iid)
515 print(r' return;')
516 print(r' }')
José Fonseca3be2c672015-02-06 15:36:40 +0000517 # Interpret argument as string
518 Retracer.extractArg(self, function, arg, LPCSTR, lvalue, rvalue)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100519 print(r' if (!pData) {')
520 print(r' return;')
521 print(r' }')
522 print(r' assert(DataSize >= strlen((const char *)pData));')
523 print(r' // Some applications include the trailing zero terminator in the data')
524 print(r' DataSize = strlen((const char *)pData);')
José Fonseca3be2c672015-02-06 15:36:40 +0000525 return
526
527 Retracer.extractArg(self, function, arg, arg_type, lvalue, rvalue)
528
José Fonseca6f810332012-11-11 10:05:09 +0000529
530def main():
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100531 print(r'#define INITGUID')
532 print()
533 print(r'#include <string.h>')
534 print()
535 print(r'#include <iostream>')
536 print()
537 print(r'#include "d3dretrace.hpp"')
538 print(r'#include "os_version.hpp"')
539 print()
540 print(r'#include "d3dretrace_dxgi.hpp"')
541 print(r'#include "d3d10imports.hpp"')
542 print(r'#include "d3d10size.hpp"')
543 print(r'#include "d3d10state.hpp"')
544 print(r'#include "d3d11imports.hpp"')
545 print(r'#include "d3d11size.hpp"')
546 print(r'#include "dcompimports.hpp"')
547 print(r'#include "d3dstate.hpp"')
548 print(r'#include "d3d9imports.hpp" // D3DERR_WASSTILLDRAWING')
549 print()
550 print('''static d3dretrace::D3DDumper<IDXGISwapChain> dxgiDumper;''')
551 print('''static d3dretrace::D3DDumper<ID3D10Device> d3d10Dumper;''')
552 print('''static d3dretrace::D3DDumper<ID3D11DeviceContext> d3d11Dumper;''')
553 print()
José Fonseca6f810332012-11-11 10:05:09 +0000554
555 api = API()
José Fonseca50bee952015-02-07 22:32:19 +0000556 api.addModule(dxgi)
557 api.addModule(d3d10)
558 api.addModule(d3d10_1)
559 api.addModule(d3d11)
Jose Fonsecafeb8f7d2016-02-01 14:14:00 +0000560 api.addModule(dcomp)
José Fonsecafc58d052014-06-13 12:47:19 +0100561
José Fonseca6f810332012-11-11 10:05:09 +0000562 retracer = D3DRetracer()
563 retracer.retraceApi(api)
564
565
566if __name__ == '__main__':
567 main()