blob: 301754c53cd460694423da91cbe7af8dd6c43c93 [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 Fonseca13803272019-07-22 19:52:26 +0100102 if function.name.startswith('D3D11CreateDevice'):
103 print(r'''
104 if (retrace::driver != retrace::DRIVER_DEFAULT) {
105 com_ptr<IDXGIDevice> pDXGIDevice;
106 HRESULT hr = (*ppDevice)->QueryInterface(IID_IDXGIDevice, (void **)&pDXGIDevice);
107 assert(SUCCEEDED(hr));
108 com_ptr<IDXGIAdapter> pDXGIAdapter;
109 hr = pDXGIDevice->GetAdapter(&pDXGIAdapter);
110 assert(SUCCEEDED(hr));
111 DXGI_ADAPTER_DESC AdapterDesc;
112 hr = pDXGIAdapter->GetDesc(&AdapterDesc);
113 assert(SUCCEEDED(hr));
114 std::wcerr << L"info: using " << AdapterDesc.Description << std::endl;
115 }
116''')
117
Jose Fonsecaf39fd602017-08-23 15:13:39 +0100118 def doInvokeFunction(self, function):
119 Retracer.doInvokeFunction(self, function)
120
121 # Handle missing debug layer. While it's possible to detect whether
122 # the debug layers are present, by creating a null device, and checking
123 # the result. It's simpler to retry.
124 if function.name.startswith('D3D10CreateDevice'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100125 print(r' if ((_result == E_FAIL || _result == DXGI_ERROR_SDK_COMPONENT_MISSING) && (Flags & D3D10_CREATE_DEVICE_DEBUG)) {')
126 print(r' retrace::warning(call) << "Direct3D 10.x SDK Debug Layer (d3d10sdklayers.dll) not available, continuing without debug output\n";')
127 print(r' Flags &= ~D3D10_CREATE_DEVICE_DEBUG;')
Jose Fonsecaf39fd602017-08-23 15:13:39 +0100128 Retracer.doInvokeFunction(self, function)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100129 print(r' }')
Jose Fonsecaf39fd602017-08-23 15:13:39 +0100130 if function.name.startswith('D3D11CreateDevice'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100131 print(r' if ((_result == E_FAIL || _result == DXGI_ERROR_SDK_COMPONENT_MISSING) && (Flags & D3D11_CREATE_DEVICE_DEBUG)) {')
132 print(r' retrace::warning(call) << "Direct3D 11.x SDK Debug Layer (d3d11*sdklayers.dll) not available, continuing without debug output\n";')
133 print(r' Flags &= ~D3D11_CREATE_DEVICE_DEBUG;')
Jose Fonsecaf39fd602017-08-23 15:13:39 +0100134 Retracer.doInvokeFunction(self, function)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100135 print(r' }')
Jose Fonsecaf39fd602017-08-23 15:13:39 +0100136
Jose Fonsecad0094a22017-05-30 12:31:37 +0100137 def handleFailure(self, interface, methodOrFunction):
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100138 # Catch when device is removed, and report the reason.
Jose Fonsecad0094a22017-05-30 12:31:37 +0100139 if interface is not None:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100140 print(r' if (_result == DXGI_ERROR_DEVICE_REMOVED) {')
Jose Fonsecad0094a22017-05-30 12:31:37 +0100141 getDeviceRemovedReasonMethod = interface.getMethodByName("GetDeviceRemovedReason")
142 if getDeviceRemovedReasonMethod is not None:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100143 print(r' HRESULT _reason = _this->GetDeviceRemovedReason();')
144 print(r' retrace::failed(call, _reason);')
Jose Fonsecaa42384c2018-01-12 15:37:52 +0000145 getDeviceMethod = interface.getMethodByName("GetDevice")
146 if getDeviceMethod is not None and len(getDeviceMethod.args) == 1:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100147 print(r' com_ptr<%s> _pDevice;' % getDeviceMethod.args[0].type.type.type)
148 print(r' _this->GetDevice(&_pDevice);')
149 print(r' HRESULT _reason = _pDevice->GetDeviceRemovedReason();')
150 print(r' retrace::failed(call, _reason);')
151 print(r' exit(EXIT_FAILURE);')
152 print(r' }')
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100153
Jose Fonsecad0094a22017-05-30 12:31:37 +0100154 Retracer.handleFailure(self, interface, methodOrFunction)
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100155
José Fonseca42b89fc2012-11-27 12:19:45 +0000156 def forceDriver(self, enum):
José Fonseca82ea89d2012-12-05 19:34:26 +0000157 # This can only work when pAdapter is NULL. For non-NULL pAdapter we
158 # need to override inside the EnumAdapters call below
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100159 print(r' if (pAdapter == NULL) {')
160 print(r' switch (retrace::driver) {')
161 print(r' case retrace::DRIVER_INTEGRATED:')
162 print(r' retrace::warning(call) << "integrated gpu selection not yet implemented\n";')
163 print(r' case retrace::DRIVER_DISCRETE:')
164 print(r' case retrace::DRIVER_HARDWARE:')
165 print(r' DriverType = %s_HARDWARE;' % enum)
166 print(r' Software = NULL;')
167 print(r' break;')
168 print(r' case retrace::DRIVER_SOFTWARE:')
169 print(r' DriverType = %s_WARP;' % enum)
170 print(r' Software = NULL;')
171 print(r' break;')
172 print(r' case retrace::DRIVER_REFERENCE:')
173 print(r' DriverType = %s_REFERENCE;' % enum)
174 print(r' Software = NULL;')
175 print(r' break;')
176 print(r' case retrace::DRIVER_NULL:')
177 print(r' DriverType = %s_NULL;' % enum)
178 print(r' Software = NULL;')
179 print(r' break;')
180 print(r' case retrace::DRIVER_MODULE:')
181 print(r' DriverType = %s_SOFTWARE;' % enum)
182 print(r' Software = LoadLibraryA(retrace::driverModule);')
183 print(r' if (!Software) {')
184 print(r' retrace::warning(call) << "failed to load " << retrace::driverModule << "\n";')
185 print(r' }')
186 print(r' break;')
187 print(r' default:')
188 print(r' assert(0);')
189 print(r' /* fall-through */')
190 print(r' case retrace::DRIVER_DEFAULT:')
191 print(r' if (DriverType == %s_SOFTWARE) {' % enum)
192 print(r' Software = LoadLibraryA("d3d10warp");')
193 print(r' if (!Software) {')
194 print(r' retrace::warning(call) << "failed to load d3d10warp.dll\n";')
195 print(r' }')
196 print(r' }')
197 print(r' break;')
198 print(r' }')
199 print(r' } else {')
200 print(r' Software = NULL;')
201 print(r' }')
José Fonseca42b89fc2012-11-27 12:19:45 +0000202
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100203 def doInvokeInterfaceMethod(self, interface, method):
204 Retracer.doInvokeInterfaceMethod(self, interface, method)
205
Jose Fonseca95796502018-11-20 13:11:31 +0000206 # Force driver
207 if interface.name.startswith('IDXGIFactory') and method.name.startswith('EnumAdapters'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100208 print(r' const char *szSoftware = NULL;')
209 print(r' switch (retrace::driver) {')
210 print(r' case retrace::DRIVER_REFERENCE:')
211 print(r' szSoftware = "d3d11ref.dll";')
212 print(r' break;')
213 print(r' case retrace::DRIVER_SOFTWARE:')
214 print(r' szSoftware = "d3d10warp.dll";')
215 print(r' break;')
216 print(r' case retrace::DRIVER_MODULE:')
217 print(r' szSoftware = retrace::driverModule;')
218 print(r' break;')
219 print(r' default:')
220 print(r' break;')
221 print(r' }')
222 print(r' HMODULE hSoftware = NULL;')
223 print(r' if (szSoftware) {')
224 print(r' hSoftware = LoadLibraryA(szSoftware);')
225 print(r' if (!hSoftware) {')
226 print(r' retrace::warning(call) << "failed to load " << szSoftware << "\n";')
227 print(r' }')
228 print(r' }')
229 print(r' if (hSoftware) {')
230 print(r' _result = _this->CreateSoftwareAdapter(hSoftware, reinterpret_cast<IDXGIAdapter **>(ppAdapter));')
231 print(r' } else {')
232 print(r' if (Adapter != 0) {')
233 print(r' retrace::warning(call) << "ignoring non-default adapter " << Adapter << "\n";')
234 print(r' Adapter = 0;')
235 print(r' }')
Jose Fonseca95796502018-11-20 13:11:31 +0000236 Retracer.doInvokeInterfaceMethod(self, interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100237 print(r' }')
Jose Fonseca95796502018-11-20 13:11:31 +0000238 return
239
240 if interface.name.startswith('IDXGIFactory') and method.name == 'CreateSoftwareAdapter':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100241 print(r' const char *szSoftware = NULL;')
242 print(r' switch (retrace::driver) {')
243 print(r' case retrace::DRIVER_REFERENCE:')
244 print(r' szSoftware = "d3d11ref.dll";')
245 print(r' break;')
246 print(r' case retrace::DRIVER_MODULE:')
247 print(r' szSoftware = retrace::driverModule;')
248 print(r' break;')
249 print(r' case retrace::DRIVER_SOFTWARE:')
250 print(r' default:')
251 print(r' szSoftware = "d3d10warp.dll";')
252 print(r' break;')
253 print(r' }')
254 print(r' Module = LoadLibraryA("d3d10warp");')
255 print(r' if (!Module) {')
256 print(r' retrace::warning(call) << "failed to load " << szSoftware << "\n";')
257 print(r' }')
Jose Fonseca95796502018-11-20 13:11:31 +0000258 Retracer.doInvokeInterfaceMethod(self, interface, method)
259
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100260 # Keep retrying ID3D11VideoContext::DecoderBeginFrame when returns E_PENDING
261 if interface.name == 'ID3D11VideoContext' and method.name == 'DecoderBeginFrame':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100262 print(r' while (_result == D3DERR_WASSTILLDRAWING || _result == E_PENDING) {')
263 print(r' Sleep(1);')
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100264 Retracer.doInvokeInterfaceMethod(self, interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100265 print(r' }')
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100266
José Fonseca610942b2012-11-08 10:46:03 +0000267 def invokeInterfaceMethod(self, interface, method):
268 # keep track of the last used device for state dumping
José Fonseca944088e2012-11-20 14:47:03 +0000269 if interface.name in ('ID3D10Device', 'ID3D10Device1'):
270 if method.name == 'Release':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100271 print(r' if (call.ret->toUInt() == 0) {')
272 print(r' d3d10Dumper.unbindDevice(_this);')
273 print(r' }')
José Fonseca944088e2012-11-20 14:47:03 +0000274 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100275 print(r' d3d10Dumper.bindDevice(_this);')
Jose Fonseca4a1c7aa2015-08-10 16:53:49 +0100276 if interface.name.startswith('ID3D11DeviceContext'):
José Fonseca5773beb2012-11-14 11:46:58 +0000277 if method.name == 'Release':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100278 print(r' if (call.ret->toUInt() == 0) {')
279 print(r' d3d11Dumper.unbindDevice(_this);')
280 print(r' }')
José Fonseca5773beb2012-11-14 11:46:58 +0000281 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100282 print(r' d3d11Dumper.bindDevice(_this);')
José Fonseca610942b2012-11-08 10:46:03 +0000283
José Fonseca1e309b42014-09-18 11:08:09 +0100284 # intercept private interfaces
José Fonsecad59843c2014-08-22 16:59:25 +0100285 if method.name == 'QueryInterface':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100286 print(r' if (!d3dretrace::overrideQueryInterface(_this, riid, ppvObj, &_result)) {')
José Fonseca75cbb8c2013-02-12 16:19:23 +0000287 Retracer.invokeInterfaceMethod(self, interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100288 print(r' }')
José Fonseca75cbb8c2013-02-12 16:19:23 +0000289 return
290
José Fonseca610942b2012-11-08 10:46:03 +0000291 # create windows as neccessary
292 if method.name == 'CreateSwapChain':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100293 print(r' d3dretrace::createWindowForSwapChain(pDesc);')
Jose Fonsecadafb4ea2015-08-10 14:40:22 +0100294 if method.name == 'CreateSwapChainForHwnd':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100295 print(r' hWnd = d3dretrace::createWindow(pDesc->Width, pDesc->Height);')
296 print(r' // DXGI_SCALING_NONE is only supported on Win8 and beyond')
297 print(r' if (pDesc->Scaling == DXGI_SCALING_NONE && !IsWindows8OrGreater()) {')
298 print(r' pDesc->Scaling = DXGI_SCALING_STRETCH;')
299 print(r' }')
José Fonsecae23a6be2014-09-14 20:30:00 +0100300 if method.name == 'CreateSwapChainForComposition':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100301 print(r' HWND hWnd = d3dretrace::createWindow(pDesc->Width, pDesc->Height);')
302 print(r' _result = _this->CreateSwapChainForHwnd(pDevice, hWnd, pDesc, NULL, pRestrictToOutput, ppSwapChain);')
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100303 self.checkResult(interface, method)
José Fonsecae23a6be2014-09-14 20:30:00 +0100304 return
Jose Fonsecafeb8f7d2016-02-01 14:14:00 +0000305 if method.name == 'CreateTargetForHwnd':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100306 print(r' hwnd = d3dretrace::createWindow(1024, 768);')
José Fonseca610942b2012-11-08 10:46:03 +0000307
José Fonseca7618cb92014-08-28 16:00:48 +0100308 if method.name == 'SetFullscreenState':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100309 print(r' if (retrace::forceWindowed) {')
310 print(r' DXGI_SWAP_CHAIN_DESC Desc;')
311 print(r' _this->GetDesc(&Desc);')
312 print(r' if (Desc.BufferDesc.Format != DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM) {')
313 print(r' Fullscreen = FALSE;')
314 print(r' pTarget = nullptr;')
315 print(r' }')
316 print(r' }')
José Fonseca7618cb92014-08-28 16:00:48 +0100317
José Fonseca610942b2012-11-08 10:46:03 +0000318 # notify frame has been completed
Jose Fonsecaced9c1c2015-08-10 15:10:37 +0100319 if interface.name.startswith('IDXGISwapChain') and method.name.startswith('Present'):
320 if interface.name.startswith('IDXGISwapChainDWM'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100321 print(r' com_ptr<IDXGISwapChain> pSwapChain;')
322 print(r' if (SUCCEEDED(_this->QueryInterface(IID_IDXGISwapChain, (void **) &pSwapChain))) {')
323 print(r' dxgiDumper.bindDevice(pSwapChain);')
324 print(r' } else {')
325 print(r' assert(0);')
326 print(r' }')
José Fonseca29d4bda2014-02-28 17:50:24 +0000327 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100328 print(r' dxgiDumper.bindDevice(_this);')
Jose Fonseca0f541f42019-07-03 13:46:05 +0100329 print(r' if ((Flags & DXGI_PRESENT_TEST) == 0) {')
330 print(r' retrace::frameComplete(call);')
331 print(r' }')
José Fonseca610942b2012-11-08 10:46:03 +0000332
333 if 'pSharedResource' in method.argNames():
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100334 print(r' if (pSharedResource) {')
335 print(r' retrace::warning(call) << "shared surfaces unsupported\n";')
336 print(r' pSharedResource = NULL;')
337 print(r' }')
José Fonseca610942b2012-11-08 10:46:03 +0000338
José Fonsecae23a6be2014-09-14 20:30:00 +0100339 if interface.name.startswith('ID3D10Device') 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);')
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100342 self.checkResult(interface, method)
José Fonseca999284f2013-02-19 13:29:26 +0000343 return
Jose Fonseca559b5f82015-07-17 16:58:50 +0100344 if interface.name.startswith('ID3D11Device') and method.name == 'OpenSharedResource':
345 # Some applications (e.g., video playing in IE11) create shared resources within the same process.
346 # TODO: Generalize to other OpenSharedResource variants
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100347 print(r' retrace::map<HANDLE>::const_iterator it = _shared_handle_map.find(hResource);')
348 print(r' if (it == _shared_handle_map.end()) {')
349 print(r' retrace::warning(call) << "replacing shared resource with checker pattern\n";')
350 print(r' _result = d3dretrace::createSharedResource(_this, ReturnedInterface, ppResource);')
Jose Fonseca559b5f82015-07-17 16:58:50 +0100351 self.checkResult(interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100352 print(r' } else {')
353 print(r' hResource = it->second;')
Jose Fonseca559b5f82015-07-17 16:58:50 +0100354 Retracer.invokeInterfaceMethod(self, interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100355 print(r' }')
Jose Fonseca559b5f82015-07-17 16:58:50 +0100356 return
José Fonseca2b66b932014-07-30 17:31:52 +0100357 if interface.name.startswith('ID3D11Device') and method.name.startswith('OpenSharedResource'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100358 print(r' retrace::warning(call) << "replacing shared resource with checker pattern\n";')
359 print(r' _result = d3dretrace::createSharedResource(_this, ReturnedInterface, ppResource);')
José Fonseca2b66b932014-07-30 17:31:52 +0100360 if method.name == 'OpenSharedResourceByName':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100361 print(r' (void)lpName;')
362 print(r' (void)dwDesiredAccess;')
Jose Fonsecadf913772015-07-17 16:54:47 +0100363 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100364 print(r' (void)hResource;')
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100365 self.checkResult(interface, method)
José Fonseca2b66b932014-07-30 17:31:52 +0100366 return
José Fonseca999284f2013-02-19 13:29:26 +0000367
368 if method.name == 'Map':
369 # Reset _DO_NOT_WAIT flags. Otherwise they may fail, and we have no
370 # way to cope with it (other than retry).
371 mapFlagsArg = method.getArgByName('MapFlags')
372 for flag in mapFlagsArg.type.values:
373 if flag.endswith('_MAP_FLAG_DO_NOT_WAIT'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100374 print(r' MapFlags &= ~%s;' % flag)
José Fonseca999284f2013-02-19 13:29:26 +0000375
José Fonseca7af569a2014-09-04 19:08:45 +0100376 if method.name.startswith('UpdateSubresource'):
José Fonsecac9cc24e2014-06-13 19:06:51 +0100377 # The D3D10 debug layer is buggy (or at least inconsistent with the
378 # runtime), as it seems to estimate and enforce the data size based on the
379 # SrcDepthPitch, even for non 3D textures, but in some traces
380 # SrcDepthPitch is garbagge for non 3D textures.
381 # XXX: It also seems to expect padding bytes at the end of the last
382 # row, but we never record (or allocate) those...
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100383 print(r' if (retrace::debug >= 2 && pDstBox && pDstBox->front == 0 && pDstBox->back == 1) {')
384 print(r' SrcDepthPitch = 0;')
385 print(r' }')
José Fonsecac9cc24e2014-06-13 19:06:51 +0100386
José Fonseca0688d2e2014-08-19 20:43:21 +0100387 if method.name == 'SetGammaControl':
388 # This method is only supported while in full-screen mode
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100389 print(r' if (retrace::forceWindowed) {')
390 print(r' return;')
391 print(r' }')
José Fonseca0688d2e2014-08-19 20:43:21 +0100392
Jose Fonseca60e65142017-06-09 13:06:32 +0100393 if method.name == 'GetData':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100394 print(r' pData = _allocator.alloc(DataSize);')
395 print(r' do {')
Jose Fonseca60e65142017-06-09 13:06:32 +0100396 self.doInvokeInterfaceMethod(interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100397 print(r' GetDataFlags = 0; // Prevent infinite loop')
398 print(r' } while (_result == S_FALSE);')
Jose Fonseca60e65142017-06-09 13:06:32 +0100399 self.checkResult(interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100400 print(r' return;')
Jose Fonseca60e65142017-06-09 13:06:32 +0100401
Jose Fonsecabfdf93e2019-06-26 13:03:27 +0100402 if method.name in ('CreateTexture1D', 'CreateTexture2D', 'CreateTexture3D', 'CreateBuffer'):
Jose Fonseca3d2b3fe2019-06-28 14:32:14 +0100403 # We don't capture multiple processes, so ignore keyed mutexes to avoid deadlocks
404 print(r' if (pDesc->MiscFlags & D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX) {')
405 print(r' pDesc->MiscFlags &= ~D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;')
406 print(r' pDesc->MiscFlags |= D3D11_RESOURCE_MISC_SHARED;')
407 print(r' }')
Jose Fonsecabfdf93e2019-06-26 13:03:27 +0100408
Jose Fonseca7da30bb2019-07-29 14:17:14 +0100409 if method.name == 'ReleaseSync':
410 # We must flush the device that used this surface, as per
411 # https://docs.microsoft.com/en-us/windows/win32/api/d3d10/nf-d3d10-id3d10device-opensharedresource
412 print(r'''
413 com_ptr<ID3D11DeviceChild> pDeviceChild;
414 com_ptr<ID3D11Device> pDevice;
415 com_ptr<ID3D11DeviceContext> pDeviceContext;
416 HRESULT hr = _this->QueryInterface(IID_ID3D11DeviceChild, (void **)&pDeviceChild);
417 if (SUCCEEDED(hr)) {
418 pDeviceChild->GetDevice(&pDevice);
419 pDevice->GetImmediateContext(&pDeviceContext);
420 pDeviceContext->Flush();
421 } else {
422 retrace::warning(call) << "ReleaseSync without D3D11 device\n";
423 }
424 (void)Key;
425 (void)_result;
426''')
427 return
José Fonseca610942b2012-11-08 10:46:03 +0000428
Jose Fonseca7da30bb2019-07-29 14:17:14 +0100429 Retracer.invokeInterfaceMethod(self, interface, method)
Jose Fonseca4d5cfa42015-07-17 16:21:05 +0100430
José Fonseca610942b2012-11-08 10:46:03 +0000431 # process events after presents
Jose Fonsecaced9c1c2015-08-10 15:10:37 +0100432 if interface.name.startswith('IDXGISwapChain') and method.name.startswith('Present'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100433 print(r' d3dretrace::processEvents();')
José Fonseca610942b2012-11-08 10:46:03 +0000434
José Fonseca6ca20082014-10-07 21:39:43 +0100435 if method.name in ('Map', 'Unmap'):
436 if interface.name.startswith('ID3D11DeviceContext'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100437 print(' void * & _pbData = g_Maps[_this][SubresourceKey(pResource, Subresource)];')
José Fonseca6ca20082014-10-07 21:39:43 +0100438 else:
439 subresourceArg = method.getArgByName('Subresource')
440 if subresourceArg is None:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100441 print(' UINT Subresource = 0;')
442 print(' void * & _pbData = g_Maps[0][SubresourceKey(_this, Subresource)];')
José Fonseca6ca20082014-10-07 21:39:43 +0100443
José Fonseca610942b2012-11-08 10:46:03 +0000444 if method.name == 'Map':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100445 print(' _MAP_DESC _MapDesc;')
446 print(' _getMapDesc(_this, %s, _MapDesc);' % ', '.join(method.argNames()))
447 print(' size_t _MappedSize = _MapDesc.Size;')
448 print(' if (_MapDesc.Size) {')
449 print(' _pbData = _MapDesc.pData;')
Jeff Muizelaar97244f92013-11-19 15:17:45 -0500450 if interface.name.startswith('ID3D11DeviceContext'):
Jose Fonseca1e3c0f72015-07-20 21:50:42 +0100451 # Prevent false warnings on 1D and 2D resources, since the
452 # pitches are often junk there...
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100453 print(' _normalizeMap(pResource, pMappedResource);')
Jeff Muizelaar97244f92013-11-19 15:17:45 -0500454 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100455 print(' _pbData = _MapDesc.pData;')
456 print(' } else {')
457 print(' return;')
458 print(' }')
Zack Rusin1cb7b772018-02-09 11:36:11 -0500459
José Fonseca610942b2012-11-08 10:46:03 +0000460 if method.name == 'Unmap':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100461 print(' if (_pbData) {')
462 print(' retrace::delRegionByPointer(_pbData);')
463 print(' _pbData = 0;')
464 print(' }')
José Fonseca6f810332012-11-11 10:05:09 +0000465
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100466 if interface.name.startswith('ID3D11VideoContext'):
467 if method.name == 'GetDecoderBuffer':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100468 print(' if (*ppBuffer && *pBufferSize) {')
469 print(' g_Maps[nullptr][SubresourceKey(_this, Type)] = *ppBuffer;')
470 print(' }')
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100471 if method.name == 'ReleaseDecoderBuffer':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100472 print(' SubresourceKey _mappingKey(_this, Type);')
473 print(' void *_pBuffer = g_Maps[nullptr][_mappingKey];')
474 print(' if (_pBuffer) {')
475 print(' retrace::delRegionByPointer(_pBuffer);')
476 print(' g_Maps[nullptr][_mappingKey] = 0;')
477 print(' }')
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100478
José Fonseca1d4fd142012-11-28 15:06:22 +0000479 # Attach shader byte code for lookup
480 if 'pShaderBytecode' in method.argNames():
481 ppShader = method.args[-1]
482 assert ppShader.output
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100483 print(r' if (retrace::dumpingState && SUCCEEDED(_result)) {')
484 print(r' (*%s)->SetPrivateData(d3dstate::GUID_D3DSTATE, BytecodeLength, pShaderBytecode);' % ppShader.name)
485 print(r' }')
José Fonseca1d4fd142012-11-28 15:06:22 +0000486
Zack Rusin1cb7b772018-02-09 11:36:11 -0500487 if method.name == 'CreateBuffer':
488 ppBuffer = method.args[-1]
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100489 print(r' if (retrace::dumpingState && SUCCEEDED(_result)) {')
490 print(r' char label[32];')
491 print(r' _snprintf(label, sizeof label, "0x%%llx", call.arg(%u).toArray()->values[0]->toUIntPtr());' % ppBuffer.index)
492 print(r' (*%s)->SetPrivateData(WKPDID_D3DDebugObjectName, strlen(label)+1, label);' % ppBuffer.name)
493 print(r' }')
Zack Rusin1cb7b772018-02-09 11:36:11 -0500494
Jose Fonseca37f769f2017-06-16 17:57:39 +0100495 def retraceInterfaceMethodBody(self, interface, method):
496 Retracer.retraceInterfaceMethodBody(self, interface, method)
497
498 # Add pitch swizzling information to the region
Jose Fonsecab2754af2017-06-19 14:22:55 +0100499 if method.name == 'Map' and interface.name not in ('ID3D10Buffer', 'ID3D10Texture1D'):
500 if interface.name.startswith('ID3D11DeviceContext'):
501 outArg = method.getArgByName('pMappedResource')
502 memberNames = ('pData', 'RowPitch', 'DepthPitch')
503 elif interface.name.startswith('ID3D10'):
504 outArg = method.args[-1]
505 memberNames = ('pData', 'RowPitch', 'DepthPitch')
506 elif interface.name == 'IDXGISurface':
507 outArg = method.getArgByName('pLockedRect')
508 memberNames = ('pBits', 'Pitch', None)
509 else:
510 raise NotImplementedError
511 struct = outArg.type.type
512 dataMemberName, rowPitchMemberName, depthPitchMemberName = memberNames
513 dataMemberIndex = struct.getMemberByName(dataMemberName)
514 rowPitchMemberIndex = struct.getMemberByName(rowPitchMemberName)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100515 print(r' if (_pbData && %s->%s != 0) {' % (outArg.name, rowPitchMemberName))
516 print(r' const trace::Array *_%s = call.arg(%u).toArray();' % (outArg.name, outArg.index))
517 print(r' if (%s) {' % outArg.name)
518 print(r' const trace::Struct *_struct = _%s->values[0]->toStruct();' % (outArg.name))
519 print(r' if (_struct) {')
520 print(r' unsigned long long traceAddress = _struct->members[%u]->toUIntPtr();' % dataMemberIndex)
521 print(r' int traceRowPitch = _struct->members[%u]->toSInt();' % rowPitchMemberIndex)
522 print(r' int realRowPitch = %s->%s;' % (outArg.name, rowPitchMemberName))
523 print(r' if (realRowPitch && traceRowPitch != realRowPitch) {')
524 print(r' retrace::setRegionPitch(traceAddress, 2, traceRowPitch, realRowPitch);')
525 print(r' }')
Jose Fonsecab2754af2017-06-19 14:22:55 +0100526 try:
527 depthPitchMemberIndex = struct.getMemberByName(depthPitchMemberName)
528 except ValueError:
529 assert len(struct.members) < 3
530 pass
531 else:
532 assert depthPitchMemberName == 'DepthPitch'
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100533 print(r' if (%s->DepthPitch) {' % outArg.name)
534 print(r' retrace::checkMismatch(call, "DepthPitch", _struct->members[%u], %s->DepthPitch);' % (struct.getMemberByName('DepthPitch'), outArg.name))
535 print(r' }')
536 print(r' }')
537 print(r' }')
538 print(r' }')
Jose Fonseca37f769f2017-06-16 17:57:39 +0100539
540
José Fonseca3be2c672015-02-06 15:36:40 +0000541 def extractArg(self, function, arg, arg_type, lvalue, rvalue):
542 # Set object names
543 if function.name == 'SetPrivateData' and arg.name == 'pData':
544 iid = function.args[0].name
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100545 print(r' if (%s != WKPDID_D3DDebugObjectName) {' % iid)
546 print(r' return;')
547 print(r' }')
José Fonseca3be2c672015-02-06 15:36:40 +0000548 # Interpret argument as string
549 Retracer.extractArg(self, function, arg, LPCSTR, lvalue, rvalue)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100550 print(r' if (!pData) {')
551 print(r' return;')
552 print(r' }')
553 print(r' assert(DataSize >= strlen((const char *)pData));')
554 print(r' // Some applications include the trailing zero terminator in the data')
555 print(r' DataSize = strlen((const char *)pData);')
José Fonseca3be2c672015-02-06 15:36:40 +0000556 return
557
558 Retracer.extractArg(self, function, arg, arg_type, lvalue, rvalue)
559
José Fonseca6f810332012-11-11 10:05:09 +0000560
561def main():
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100562 print(r'#define INITGUID')
563 print()
564 print(r'#include <string.h>')
565 print()
566 print(r'#include <iostream>')
567 print()
568 print(r'#include "d3dretrace.hpp"')
569 print(r'#include "os_version.hpp"')
570 print()
571 print(r'#include "d3dretrace_dxgi.hpp"')
572 print(r'#include "d3d10imports.hpp"')
573 print(r'#include "d3d10size.hpp"')
574 print(r'#include "d3d10state.hpp"')
575 print(r'#include "d3d11imports.hpp"')
576 print(r'#include "d3d11size.hpp"')
577 print(r'#include "dcompimports.hpp"')
578 print(r'#include "d3dstate.hpp"')
579 print(r'#include "d3d9imports.hpp" // D3DERR_WASSTILLDRAWING')
580 print()
581 print('''static d3dretrace::D3DDumper<IDXGISwapChain> dxgiDumper;''')
582 print('''static d3dretrace::D3DDumper<ID3D10Device> d3d10Dumper;''')
583 print('''static d3dretrace::D3DDumper<ID3D11DeviceContext> d3d11Dumper;''')
584 print()
José Fonseca6f810332012-11-11 10:05:09 +0000585
586 api = API()
José Fonseca50bee952015-02-07 22:32:19 +0000587 api.addModule(dxgi)
588 api.addModule(d3d10)
589 api.addModule(d3d10_1)
590 api.addModule(d3d11)
Jose Fonsecafeb8f7d2016-02-01 14:14:00 +0000591 api.addModule(dcomp)
José Fonsecafc58d052014-06-13 12:47:19 +0100592
José Fonseca6f810332012-11-11 10:05:09 +0000593 retracer = D3DRetracer()
594 retracer.retraceApi(api)
595
596
597if __name__ == '__main__':
598 main()