blob: 7a4a58ea8a9e6cd02bc636bef461f3122f7011e2 [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);')
313 print(r' retrace::frameComplete(call);')
José Fonseca610942b2012-11-08 10:46:03 +0000314
315 if 'pSharedResource' in method.argNames():
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100316 print(r' if (pSharedResource) {')
317 print(r' retrace::warning(call) << "shared surfaces unsupported\n";')
318 print(r' pSharedResource = NULL;')
319 print(r' }')
José Fonseca610942b2012-11-08 10:46:03 +0000320
José Fonsecae23a6be2014-09-14 20:30:00 +0100321 if interface.name.startswith('ID3D10Device') and method.name.startswith('OpenSharedResource'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100322 print(r' retrace::warning(call) << "replacing shared resource with checker pattern\n";')
323 print(r' _result = d3dretrace::createSharedResource(_this, ReturnedInterface, ppResource);')
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100324 self.checkResult(interface, method)
José Fonseca999284f2013-02-19 13:29:26 +0000325 return
Jose Fonseca559b5f82015-07-17 16:58:50 +0100326 if interface.name.startswith('ID3D11Device') and method.name == 'OpenSharedResource':
327 # Some applications (e.g., video playing in IE11) create shared resources within the same process.
328 # TODO: Generalize to other OpenSharedResource variants
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100329 print(r' retrace::map<HANDLE>::const_iterator it = _shared_handle_map.find(hResource);')
330 print(r' if (it == _shared_handle_map.end()) {')
331 print(r' retrace::warning(call) << "replacing shared resource with checker pattern\n";')
332 print(r' _result = d3dretrace::createSharedResource(_this, ReturnedInterface, ppResource);')
Jose Fonseca559b5f82015-07-17 16:58:50 +0100333 self.checkResult(interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100334 print(r' } else {')
335 print(r' hResource = it->second;')
Jose Fonseca559b5f82015-07-17 16:58:50 +0100336 Retracer.invokeInterfaceMethod(self, interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100337 print(r' }')
Jose Fonseca559b5f82015-07-17 16:58:50 +0100338 return
José Fonseca2b66b932014-07-30 17:31:52 +0100339 if interface.name.startswith('ID3D11Device') and method.name.startswith('OpenSharedResource'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100340 print(r' retrace::warning(call) << "replacing shared resource with checker pattern\n";')
341 print(r' _result = d3dretrace::createSharedResource(_this, ReturnedInterface, ppResource);')
José Fonseca2b66b932014-07-30 17:31:52 +0100342 if method.name == 'OpenSharedResourceByName':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100343 print(r' (void)lpName;')
344 print(r' (void)dwDesiredAccess;')
Jose Fonsecadf913772015-07-17 16:54:47 +0100345 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100346 print(r' (void)hResource;')
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100347 self.checkResult(interface, method)
José Fonseca2b66b932014-07-30 17:31:52 +0100348 return
José Fonseca999284f2013-02-19 13:29:26 +0000349
350 if method.name == 'Map':
351 # Reset _DO_NOT_WAIT flags. Otherwise they may fail, and we have no
352 # way to cope with it (other than retry).
353 mapFlagsArg = method.getArgByName('MapFlags')
354 for flag in mapFlagsArg.type.values:
355 if flag.endswith('_MAP_FLAG_DO_NOT_WAIT'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100356 print(r' MapFlags &= ~%s;' % flag)
José Fonseca999284f2013-02-19 13:29:26 +0000357
José Fonseca7af569a2014-09-04 19:08:45 +0100358 if method.name.startswith('UpdateSubresource'):
José Fonsecac9cc24e2014-06-13 19:06:51 +0100359 # The D3D10 debug layer is buggy (or at least inconsistent with the
360 # runtime), as it seems to estimate and enforce the data size based on the
361 # SrcDepthPitch, even for non 3D textures, but in some traces
362 # SrcDepthPitch is garbagge for non 3D textures.
363 # XXX: It also seems to expect padding bytes at the end of the last
364 # row, but we never record (or allocate) those...
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100365 print(r' if (retrace::debug >= 2 && pDstBox && pDstBox->front == 0 && pDstBox->back == 1) {')
366 print(r' SrcDepthPitch = 0;')
367 print(r' }')
José Fonsecac9cc24e2014-06-13 19:06:51 +0100368
José Fonseca0688d2e2014-08-19 20:43:21 +0100369 if method.name == 'SetGammaControl':
370 # This method is only supported while in full-screen mode
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100371 print(r' if (retrace::forceWindowed) {')
372 print(r' return;')
373 print(r' }')
José Fonseca0688d2e2014-08-19 20:43:21 +0100374
Jose Fonseca60e65142017-06-09 13:06:32 +0100375 if method.name == 'GetData':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100376 print(r' pData = _allocator.alloc(DataSize);')
377 print(r' do {')
Jose Fonseca60e65142017-06-09 13:06:32 +0100378 self.doInvokeInterfaceMethod(interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100379 print(r' GetDataFlags = 0; // Prevent infinite loop')
380 print(r' } while (_result == S_FALSE);')
Jose Fonseca60e65142017-06-09 13:06:32 +0100381 self.checkResult(interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100382 print(r' return;')
Jose Fonseca60e65142017-06-09 13:06:32 +0100383
Jose Fonsecabfdf93e2019-06-26 13:03:27 +0100384 if method.name in ('CreateTexture1D', 'CreateTexture2D', 'CreateTexture3D', 'CreateBuffer'):
385 print(r' pDesc->MiscFlags &= ~D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;')
386
José Fonseca999284f2013-02-19 13:29:26 +0000387 Retracer.invokeInterfaceMethod(self, interface, method)
José Fonseca610942b2012-11-08 10:46:03 +0000388
Jose Fonseca4d5cfa42015-07-17 16:21:05 +0100389 if method.name in ('AcquireSync', 'ReleaseSync'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100390 print(r' if (SUCCEEDED(_result) && _result != S_OK) {')
391 print(r' retrace::warning(call) << " returned " << _result << "\n";')
392 print(r' }')
Jose Fonseca4d5cfa42015-07-17 16:21:05 +0100393
José Fonseca610942b2012-11-08 10:46:03 +0000394 # process events after presents
Jose Fonsecaced9c1c2015-08-10 15:10:37 +0100395 if interface.name.startswith('IDXGISwapChain') and method.name.startswith('Present'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100396 print(r' d3dretrace::processEvents();')
José Fonseca610942b2012-11-08 10:46:03 +0000397
José Fonseca6ca20082014-10-07 21:39:43 +0100398 if method.name in ('Map', 'Unmap'):
399 if interface.name.startswith('ID3D11DeviceContext'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100400 print(' void * & _pbData = g_Maps[_this][SubresourceKey(pResource, Subresource)];')
José Fonseca6ca20082014-10-07 21:39:43 +0100401 else:
402 subresourceArg = method.getArgByName('Subresource')
403 if subresourceArg is None:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100404 print(' UINT Subresource = 0;')
405 print(' void * & _pbData = g_Maps[0][SubresourceKey(_this, Subresource)];')
José Fonseca6ca20082014-10-07 21:39:43 +0100406
José Fonseca610942b2012-11-08 10:46:03 +0000407 if method.name == 'Map':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100408 print(' _MAP_DESC _MapDesc;')
409 print(' _getMapDesc(_this, %s, _MapDesc);' % ', '.join(method.argNames()))
410 print(' size_t _MappedSize = _MapDesc.Size;')
411 print(' if (_MapDesc.Size) {')
412 print(' _pbData = _MapDesc.pData;')
Jeff Muizelaar97244f92013-11-19 15:17:45 -0500413 if interface.name.startswith('ID3D11DeviceContext'):
Jose Fonseca1e3c0f72015-07-20 21:50:42 +0100414 # Prevent false warnings on 1D and 2D resources, since the
415 # pitches are often junk there...
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100416 print(' _normalizeMap(pResource, pMappedResource);')
Jeff Muizelaar97244f92013-11-19 15:17:45 -0500417 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100418 print(' _pbData = _MapDesc.pData;')
419 print(' } else {')
420 print(' return;')
421 print(' }')
Zack Rusin1cb7b772018-02-09 11:36:11 -0500422
José Fonseca610942b2012-11-08 10:46:03 +0000423 if method.name == 'Unmap':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100424 print(' if (_pbData) {')
425 print(' retrace::delRegionByPointer(_pbData);')
426 print(' _pbData = 0;')
427 print(' }')
José Fonseca6f810332012-11-11 10:05:09 +0000428
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100429 if interface.name.startswith('ID3D11VideoContext'):
430 if method.name == 'GetDecoderBuffer':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100431 print(' if (*ppBuffer && *pBufferSize) {')
432 print(' g_Maps[nullptr][SubresourceKey(_this, Type)] = *ppBuffer;')
433 print(' }')
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100434 if method.name == 'ReleaseDecoderBuffer':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100435 print(' SubresourceKey _mappingKey(_this, Type);')
436 print(' void *_pBuffer = g_Maps[nullptr][_mappingKey];')
437 print(' if (_pBuffer) {')
438 print(' retrace::delRegionByPointer(_pBuffer);')
439 print(' g_Maps[nullptr][_mappingKey] = 0;')
440 print(' }')
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100441
José Fonseca1d4fd142012-11-28 15:06:22 +0000442 # Attach shader byte code for lookup
443 if 'pShaderBytecode' in method.argNames():
444 ppShader = method.args[-1]
445 assert ppShader.output
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100446 print(r' if (retrace::dumpingState && SUCCEEDED(_result)) {')
447 print(r' (*%s)->SetPrivateData(d3dstate::GUID_D3DSTATE, BytecodeLength, pShaderBytecode);' % ppShader.name)
448 print(r' }')
José Fonseca1d4fd142012-11-28 15:06:22 +0000449
Zack Rusin1cb7b772018-02-09 11:36:11 -0500450 if method.name == 'CreateBuffer':
451 ppBuffer = method.args[-1]
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100452 print(r' if (retrace::dumpingState && SUCCEEDED(_result)) {')
453 print(r' char label[32];')
454 print(r' _snprintf(label, sizeof label, "0x%%llx", call.arg(%u).toArray()->values[0]->toUIntPtr());' % ppBuffer.index)
455 print(r' (*%s)->SetPrivateData(WKPDID_D3DDebugObjectName, strlen(label)+1, label);' % ppBuffer.name)
456 print(r' }')
Zack Rusin1cb7b772018-02-09 11:36:11 -0500457
Jose Fonseca37f769f2017-06-16 17:57:39 +0100458 def retraceInterfaceMethodBody(self, interface, method):
459 Retracer.retraceInterfaceMethodBody(self, interface, method)
460
461 # Add pitch swizzling information to the region
Jose Fonsecab2754af2017-06-19 14:22:55 +0100462 if method.name == 'Map' and interface.name not in ('ID3D10Buffer', 'ID3D10Texture1D'):
463 if interface.name.startswith('ID3D11DeviceContext'):
464 outArg = method.getArgByName('pMappedResource')
465 memberNames = ('pData', 'RowPitch', 'DepthPitch')
466 elif interface.name.startswith('ID3D10'):
467 outArg = method.args[-1]
468 memberNames = ('pData', 'RowPitch', 'DepthPitch')
469 elif interface.name == 'IDXGISurface':
470 outArg = method.getArgByName('pLockedRect')
471 memberNames = ('pBits', 'Pitch', None)
472 else:
473 raise NotImplementedError
474 struct = outArg.type.type
475 dataMemberName, rowPitchMemberName, depthPitchMemberName = memberNames
476 dataMemberIndex = struct.getMemberByName(dataMemberName)
477 rowPitchMemberIndex = struct.getMemberByName(rowPitchMemberName)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100478 print(r' if (_pbData && %s->%s != 0) {' % (outArg.name, rowPitchMemberName))
479 print(r' const trace::Array *_%s = call.arg(%u).toArray();' % (outArg.name, outArg.index))
480 print(r' if (%s) {' % outArg.name)
481 print(r' const trace::Struct *_struct = _%s->values[0]->toStruct();' % (outArg.name))
482 print(r' if (_struct) {')
483 print(r' unsigned long long traceAddress = _struct->members[%u]->toUIntPtr();' % dataMemberIndex)
484 print(r' int traceRowPitch = _struct->members[%u]->toSInt();' % rowPitchMemberIndex)
485 print(r' int realRowPitch = %s->%s;' % (outArg.name, rowPitchMemberName))
486 print(r' if (realRowPitch && traceRowPitch != realRowPitch) {')
487 print(r' retrace::setRegionPitch(traceAddress, 2, traceRowPitch, realRowPitch);')
488 print(r' }')
Jose Fonsecab2754af2017-06-19 14:22:55 +0100489 try:
490 depthPitchMemberIndex = struct.getMemberByName(depthPitchMemberName)
491 except ValueError:
492 assert len(struct.members) < 3
493 pass
494 else:
495 assert depthPitchMemberName == 'DepthPitch'
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100496 print(r' if (%s->DepthPitch) {' % outArg.name)
497 print(r' retrace::checkMismatch(call, "DepthPitch", _struct->members[%u], %s->DepthPitch);' % (struct.getMemberByName('DepthPitch'), outArg.name))
498 print(r' }')
499 print(r' }')
500 print(r' }')
501 print(r' }')
Jose Fonseca37f769f2017-06-16 17:57:39 +0100502
503
José Fonseca3be2c672015-02-06 15:36:40 +0000504 def extractArg(self, function, arg, arg_type, lvalue, rvalue):
505 # Set object names
506 if function.name == 'SetPrivateData' and arg.name == 'pData':
507 iid = function.args[0].name
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100508 print(r' if (%s != WKPDID_D3DDebugObjectName) {' % iid)
509 print(r' return;')
510 print(r' }')
José Fonseca3be2c672015-02-06 15:36:40 +0000511 # Interpret argument as string
512 Retracer.extractArg(self, function, arg, LPCSTR, lvalue, rvalue)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100513 print(r' if (!pData) {')
514 print(r' return;')
515 print(r' }')
516 print(r' assert(DataSize >= strlen((const char *)pData));')
517 print(r' // Some applications include the trailing zero terminator in the data')
518 print(r' DataSize = strlen((const char *)pData);')
José Fonseca3be2c672015-02-06 15:36:40 +0000519 return
520
521 Retracer.extractArg(self, function, arg, arg_type, lvalue, rvalue)
522
José Fonseca6f810332012-11-11 10:05:09 +0000523
524def main():
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100525 print(r'#define INITGUID')
526 print()
527 print(r'#include <string.h>')
528 print()
529 print(r'#include <iostream>')
530 print()
531 print(r'#include "d3dretrace.hpp"')
532 print(r'#include "os_version.hpp"')
533 print()
534 print(r'#include "d3dretrace_dxgi.hpp"')
535 print(r'#include "d3d10imports.hpp"')
536 print(r'#include "d3d10size.hpp"')
537 print(r'#include "d3d10state.hpp"')
538 print(r'#include "d3d11imports.hpp"')
539 print(r'#include "d3d11size.hpp"')
540 print(r'#include "dcompimports.hpp"')
541 print(r'#include "d3dstate.hpp"')
542 print(r'#include "d3d9imports.hpp" // D3DERR_WASSTILLDRAWING')
543 print()
544 print('''static d3dretrace::D3DDumper<IDXGISwapChain> dxgiDumper;''')
545 print('''static d3dretrace::D3DDumper<ID3D10Device> d3d10Dumper;''')
546 print('''static d3dretrace::D3DDumper<ID3D11DeviceContext> d3d11Dumper;''')
547 print()
José Fonseca6f810332012-11-11 10:05:09 +0000548
549 api = API()
José Fonseca50bee952015-02-07 22:32:19 +0000550 api.addModule(dxgi)
551 api.addModule(d3d10)
552 api.addModule(d3d10_1)
553 api.addModule(d3d11)
Jose Fonsecafeb8f7d2016-02-01 14:14:00 +0000554 api.addModule(dcomp)
José Fonsecafc58d052014-06-13 12:47:19 +0100555
José Fonseca6f810332012-11-11 10:05:09 +0000556 retracer = D3DRetracer()
557 retracer.retraceApi(api)
558
559
560if __name__ == '__main__':
561 main()