blob: de722e578ec629c82fdbfceb00d04c9a13654660 [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 Fonsecad7b9abb2020-01-27 11:03:57 +0000102 if function.name in self.createDeviceFunctionNames:
Jose Fonseca13803272019-07-22 19:52:26 +0100103 print(r'''
Robert Tarasov6ccf5bb2020-01-10 12:31:25 -0800104 if (retrace::driver != retrace::DRIVER_DEFAULT &&
105 ppDevice && *ppDevice) {
Jose Fonseca13803272019-07-22 19:52:26 +0100106 com_ptr<IDXGIDevice> pDXGIDevice;
107 HRESULT hr = (*ppDevice)->QueryInterface(IID_IDXGIDevice, (void **)&pDXGIDevice);
108 assert(SUCCEEDED(hr));
109 com_ptr<IDXGIAdapter> pDXGIAdapter;
110 hr = pDXGIDevice->GetAdapter(&pDXGIAdapter);
111 assert(SUCCEEDED(hr));
112 DXGI_ADAPTER_DESC AdapterDesc;
113 hr = pDXGIAdapter->GetDesc(&AdapterDesc);
114 assert(SUCCEEDED(hr));
115 std::wcerr << L"info: using " << AdapterDesc.Description << std::endl;
116 }
117''')
118
Jose Fonsecaf39fd602017-08-23 15:13:39 +0100119 def doInvokeFunction(self, function):
120 Retracer.doInvokeFunction(self, function)
121
122 # Handle missing debug layer. While it's possible to detect whether
123 # the debug layers are present, by creating a null device, and checking
124 # the result. It's simpler to retry.
125 if function.name.startswith('D3D10CreateDevice'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100126 print(r' if ((_result == E_FAIL || _result == DXGI_ERROR_SDK_COMPONENT_MISSING) && (Flags & D3D10_CREATE_DEVICE_DEBUG)) {')
127 print(r' retrace::warning(call) << "Direct3D 10.x SDK Debug Layer (d3d10sdklayers.dll) not available, continuing without debug output\n";')
128 print(r' Flags &= ~D3D10_CREATE_DEVICE_DEBUG;')
Jose Fonsecaf39fd602017-08-23 15:13:39 +0100129 Retracer.doInvokeFunction(self, function)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100130 print(r' }')
Jose Fonsecaf39fd602017-08-23 15:13:39 +0100131 if function.name.startswith('D3D11CreateDevice'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100132 print(r' if ((_result == E_FAIL || _result == DXGI_ERROR_SDK_COMPONENT_MISSING) && (Flags & D3D11_CREATE_DEVICE_DEBUG)) {')
133 print(r' retrace::warning(call) << "Direct3D 11.x SDK Debug Layer (d3d11*sdklayers.dll) not available, continuing without debug output\n";')
134 print(r' Flags &= ~D3D11_CREATE_DEVICE_DEBUG;')
Jose Fonsecaf39fd602017-08-23 15:13:39 +0100135 Retracer.doInvokeFunction(self, function)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100136 print(r' }')
Jose Fonsecaf39fd602017-08-23 15:13:39 +0100137
Jose Fonsecad0094a22017-05-30 12:31:37 +0100138 def handleFailure(self, interface, methodOrFunction):
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100139 # Catch when device is removed, and report the reason.
Jose Fonsecad0094a22017-05-30 12:31:37 +0100140 if interface is not None:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100141 print(r' if (_result == DXGI_ERROR_DEVICE_REMOVED) {')
Jose Fonsecad0094a22017-05-30 12:31:37 +0100142 getDeviceRemovedReasonMethod = interface.getMethodByName("GetDeviceRemovedReason")
143 if getDeviceRemovedReasonMethod is not None:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100144 print(r' HRESULT _reason = _this->GetDeviceRemovedReason();')
145 print(r' retrace::failed(call, _reason);')
Jose Fonsecaa42384c2018-01-12 15:37:52 +0000146 getDeviceMethod = interface.getMethodByName("GetDevice")
147 if getDeviceMethod is not None and len(getDeviceMethod.args) == 1:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100148 print(r' com_ptr<%s> _pDevice;' % getDeviceMethod.args[0].type.type.type)
149 print(r' _this->GetDevice(&_pDevice);')
150 print(r' HRESULT _reason = _pDevice->GetDeviceRemovedReason();')
151 print(r' retrace::failed(call, _reason);')
152 print(r' exit(EXIT_FAILURE);')
153 print(r' }')
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100154
Jose Fonsecad0094a22017-05-30 12:31:37 +0100155 Retracer.handleFailure(self, interface, methodOrFunction)
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100156
José Fonseca42b89fc2012-11-27 12:19:45 +0000157 def forceDriver(self, enum):
José Fonseca82ea89d2012-12-05 19:34:26 +0000158 # This can only work when pAdapter is NULL. For non-NULL pAdapter we
159 # need to override inside the EnumAdapters call below
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100160 print(r' if (pAdapter == NULL) {')
161 print(r' switch (retrace::driver) {')
162 print(r' case retrace::DRIVER_INTEGRATED:')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100163 print(r' case retrace::DRIVER_DISCRETE:')
Jose Fonseca5e6f06b2020-01-27 11:07:13 +0000164 print(r' retrace::warning(call) << "integrated/discrete GPU selection not yet implemented\n";')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100165 print(r' case retrace::DRIVER_HARDWARE:')
166 print(r' DriverType = %s_HARDWARE;' % enum)
167 print(r' Software = NULL;')
168 print(r' break;')
169 print(r' case retrace::DRIVER_SOFTWARE:')
170 print(r' DriverType = %s_WARP;' % enum)
171 print(r' Software = NULL;')
172 print(r' break;')
173 print(r' case retrace::DRIVER_REFERENCE:')
174 print(r' DriverType = %s_REFERENCE;' % enum)
175 print(r' Software = NULL;')
176 print(r' break;')
177 print(r' case retrace::DRIVER_NULL:')
178 print(r' DriverType = %s_NULL;' % enum)
179 print(r' Software = NULL;')
180 print(r' break;')
181 print(r' case retrace::DRIVER_MODULE:')
182 print(r' DriverType = %s_SOFTWARE;' % enum)
183 print(r' Software = LoadLibraryA(retrace::driverModule);')
184 print(r' if (!Software) {')
185 print(r' retrace::warning(call) << "failed to load " << retrace::driverModule << "\n";')
186 print(r' }')
187 print(r' break;')
188 print(r' default:')
189 print(r' assert(0);')
190 print(r' /* fall-through */')
191 print(r' case retrace::DRIVER_DEFAULT:')
192 print(r' if (DriverType == %s_SOFTWARE) {' % enum)
193 print(r' Software = LoadLibraryA("d3d10warp");')
194 print(r' if (!Software) {')
195 print(r' retrace::warning(call) << "failed to load d3d10warp.dll\n";')
196 print(r' }')
197 print(r' }')
198 print(r' break;')
199 print(r' }')
200 print(r' } else {')
201 print(r' Software = NULL;')
202 print(r' }')
José Fonseca42b89fc2012-11-27 12:19:45 +0000203
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100204 def doInvokeInterfaceMethod(self, interface, method):
Jose Fonseca7f95b872020-04-29 19:48:15 +0100205 if interface.name.startswith('IDXGIAdapter') and method.name == 'EnumOutputs':
206 print(r' if (Output != 0) {')
207 print(r' retrace::warning(call) << "ignoring output " << Output << "\n";')
208 print(r' Output = 0;')
209 print(r' }')
210
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100211 Retracer.doInvokeInterfaceMethod(self, interface, method)
212
Jose Fonseca95796502018-11-20 13:11:31 +0000213 # Force driver
214 if interface.name.startswith('IDXGIFactory') and method.name.startswith('EnumAdapters'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100215 print(r' const char *szSoftware = NULL;')
216 print(r' switch (retrace::driver) {')
217 print(r' case retrace::DRIVER_REFERENCE:')
218 print(r' szSoftware = "d3d11ref.dll";')
219 print(r' break;')
220 print(r' case retrace::DRIVER_SOFTWARE:')
221 print(r' szSoftware = "d3d10warp.dll";')
222 print(r' break;')
223 print(r' case retrace::DRIVER_MODULE:')
224 print(r' szSoftware = retrace::driverModule;')
225 print(r' break;')
226 print(r' default:')
227 print(r' break;')
228 print(r' }')
229 print(r' HMODULE hSoftware = NULL;')
230 print(r' if (szSoftware) {')
231 print(r' hSoftware = LoadLibraryA(szSoftware);')
232 print(r' if (!hSoftware) {')
233 print(r' retrace::warning(call) << "failed to load " << szSoftware << "\n";')
234 print(r' }')
235 print(r' }')
236 print(r' if (hSoftware) {')
237 print(r' _result = _this->CreateSoftwareAdapter(hSoftware, reinterpret_cast<IDXGIAdapter **>(ppAdapter));')
238 print(r' } else {')
239 print(r' if (Adapter != 0) {')
240 print(r' retrace::warning(call) << "ignoring non-default adapter " << Adapter << "\n";')
241 print(r' Adapter = 0;')
242 print(r' }')
Jose Fonseca95796502018-11-20 13:11:31 +0000243 Retracer.doInvokeInterfaceMethod(self, interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100244 print(r' }')
Jose Fonseca95796502018-11-20 13:11:31 +0000245 return
246
247 if interface.name.startswith('IDXGIFactory') and method.name == 'CreateSoftwareAdapter':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100248 print(r' const char *szSoftware = NULL;')
249 print(r' switch (retrace::driver) {')
250 print(r' case retrace::DRIVER_REFERENCE:')
251 print(r' szSoftware = "d3d11ref.dll";')
252 print(r' break;')
253 print(r' case retrace::DRIVER_MODULE:')
254 print(r' szSoftware = retrace::driverModule;')
255 print(r' break;')
256 print(r' case retrace::DRIVER_SOFTWARE:')
257 print(r' default:')
258 print(r' szSoftware = "d3d10warp.dll";')
259 print(r' break;')
260 print(r' }')
261 print(r' Module = LoadLibraryA("d3d10warp");')
262 print(r' if (!Module) {')
263 print(r' retrace::warning(call) << "failed to load " << szSoftware << "\n";')
264 print(r' }')
Jose Fonseca95796502018-11-20 13:11:31 +0000265 Retracer.doInvokeInterfaceMethod(self, interface, method)
266
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100267 # Keep retrying ID3D11VideoContext::DecoderBeginFrame when returns E_PENDING
268 if interface.name == 'ID3D11VideoContext' and method.name == 'DecoderBeginFrame':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100269 print(r' while (_result == D3DERR_WASSTILLDRAWING || _result == E_PENDING) {')
270 print(r' Sleep(1);')
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100271 Retracer.doInvokeInterfaceMethod(self, interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100272 print(r' }')
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100273
José Fonseca610942b2012-11-08 10:46:03 +0000274 def invokeInterfaceMethod(self, interface, method):
275 # keep track of the last used device for state dumping
José Fonseca944088e2012-11-20 14:47:03 +0000276 if interface.name in ('ID3D10Device', 'ID3D10Device1'):
277 if method.name == 'Release':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100278 print(r' if (call.ret->toUInt() == 0) {')
279 print(r' d3d10Dumper.unbindDevice(_this);')
280 print(r' }')
José Fonseca944088e2012-11-20 14:47:03 +0000281 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100282 print(r' d3d10Dumper.bindDevice(_this);')
Jose Fonseca4a1c7aa2015-08-10 16:53:49 +0100283 if interface.name.startswith('ID3D11DeviceContext'):
José Fonseca5773beb2012-11-14 11:46:58 +0000284 if method.name == 'Release':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100285 print(r' if (call.ret->toUInt() == 0) {')
286 print(r' d3d11Dumper.unbindDevice(_this);')
287 print(r' }')
José Fonseca5773beb2012-11-14 11:46:58 +0000288 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100289 print(r' d3d11Dumper.bindDevice(_this);')
José Fonseca610942b2012-11-08 10:46:03 +0000290
José Fonseca1e309b42014-09-18 11:08:09 +0100291 # intercept private interfaces
José Fonsecad59843c2014-08-22 16:59:25 +0100292 if method.name == 'QueryInterface':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100293 print(r' if (!d3dretrace::overrideQueryInterface(_this, riid, ppvObj, &_result)) {')
José Fonseca75cbb8c2013-02-12 16:19:23 +0000294 Retracer.invokeInterfaceMethod(self, interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100295 print(r' }')
José Fonseca75cbb8c2013-02-12 16:19:23 +0000296 return
297
José Fonseca610942b2012-11-08 10:46:03 +0000298 # create windows as neccessary
299 if method.name == 'CreateSwapChain':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100300 print(r' d3dretrace::createWindowForSwapChain(pDesc);')
Jose Fonsecadafb4ea2015-08-10 14:40:22 +0100301 if method.name == 'CreateSwapChainForHwnd':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100302 print(r' hWnd = d3dretrace::createWindow(pDesc->Width, pDesc->Height);')
303 print(r' // DXGI_SCALING_NONE is only supported on Win8 and beyond')
304 print(r' if (pDesc->Scaling == DXGI_SCALING_NONE && !IsWindows8OrGreater()) {')
305 print(r' pDesc->Scaling = DXGI_SCALING_STRETCH;')
306 print(r' }')
José Fonsecae23a6be2014-09-14 20:30:00 +0100307 if method.name == 'CreateSwapChainForComposition':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100308 print(r' HWND hWnd = d3dretrace::createWindow(pDesc->Width, pDesc->Height);')
309 print(r' _result = _this->CreateSwapChainForHwnd(pDevice, hWnd, pDesc, NULL, pRestrictToOutput, ppSwapChain);')
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100310 self.checkResult(interface, method)
José Fonsecae23a6be2014-09-14 20:30:00 +0100311 return
Jose Fonsecafeb8f7d2016-02-01 14:14:00 +0000312 if method.name == 'CreateTargetForHwnd':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100313 print(r' hwnd = d3dretrace::createWindow(1024, 768);')
José Fonseca610942b2012-11-08 10:46:03 +0000314
José Fonseca7618cb92014-08-28 16:00:48 +0100315 if method.name == 'SetFullscreenState':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100316 print(r' if (retrace::forceWindowed) {')
317 print(r' DXGI_SWAP_CHAIN_DESC Desc;')
318 print(r' _this->GetDesc(&Desc);')
319 print(r' if (Desc.BufferDesc.Format != DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM) {')
320 print(r' Fullscreen = FALSE;')
321 print(r' pTarget = nullptr;')
322 print(r' }')
323 print(r' }')
José Fonseca7618cb92014-08-28 16:00:48 +0100324
José Fonseca610942b2012-11-08 10:46:03 +0000325 # notify frame has been completed
Jose Fonsecaced9c1c2015-08-10 15:10:37 +0100326 if interface.name.startswith('IDXGISwapChain') and method.name.startswith('Present'):
327 if interface.name.startswith('IDXGISwapChainDWM'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100328 print(r' com_ptr<IDXGISwapChain> pSwapChain;')
329 print(r' if (SUCCEEDED(_this->QueryInterface(IID_IDXGISwapChain, (void **) &pSwapChain))) {')
330 print(r' dxgiDumper.bindDevice(pSwapChain);')
331 print(r' } else {')
332 print(r' assert(0);')
333 print(r' }')
José Fonseca29d4bda2014-02-28 17:50:24 +0000334 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100335 print(r' dxgiDumper.bindDevice(_this);')
Jose Fonseca0f541f42019-07-03 13:46:05 +0100336 print(r' if ((Flags & DXGI_PRESENT_TEST) == 0) {')
337 print(r' retrace::frameComplete(call);')
338 print(r' }')
José Fonseca610942b2012-11-08 10:46:03 +0000339
340 if 'pSharedResource' in method.argNames():
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100341 print(r' if (pSharedResource) {')
342 print(r' retrace::warning(call) << "shared surfaces unsupported\n";')
343 print(r' pSharedResource = NULL;')
344 print(r' }')
José Fonseca610942b2012-11-08 10:46:03 +0000345
José Fonsecae23a6be2014-09-14 20:30:00 +0100346 if interface.name.startswith('ID3D10Device') and method.name.startswith('OpenSharedResource'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100347 print(r' retrace::warning(call) << "replacing shared resource with checker pattern\n";')
348 print(r' _result = d3dretrace::createSharedResource(_this, ReturnedInterface, ppResource);')
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100349 self.checkResult(interface, method)
José Fonseca999284f2013-02-19 13:29:26 +0000350 return
Jose Fonseca559b5f82015-07-17 16:58:50 +0100351 if interface.name.startswith('ID3D11Device') and method.name == 'OpenSharedResource':
352 # Some applications (e.g., video playing in IE11) create shared resources within the same process.
353 # TODO: Generalize to other OpenSharedResource variants
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100354 print(r' retrace::map<HANDLE>::const_iterator it = _shared_handle_map.find(hResource);')
355 print(r' if (it == _shared_handle_map.end()) {')
356 print(r' retrace::warning(call) << "replacing shared resource with checker pattern\n";')
357 print(r' _result = d3dretrace::createSharedResource(_this, ReturnedInterface, ppResource);')
Jose Fonseca559b5f82015-07-17 16:58:50 +0100358 self.checkResult(interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100359 print(r' } else {')
360 print(r' hResource = it->second;')
Jose Fonseca559b5f82015-07-17 16:58:50 +0100361 Retracer.invokeInterfaceMethod(self, interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100362 print(r' }')
Jose Fonseca559b5f82015-07-17 16:58:50 +0100363 return
José Fonseca2b66b932014-07-30 17:31:52 +0100364 if interface.name.startswith('ID3D11Device') and method.name.startswith('OpenSharedResource'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100365 print(r' retrace::warning(call) << "replacing shared resource with checker pattern\n";')
366 print(r' _result = d3dretrace::createSharedResource(_this, ReturnedInterface, ppResource);')
José Fonseca2b66b932014-07-30 17:31:52 +0100367 if method.name == 'OpenSharedResourceByName':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100368 print(r' (void)lpName;')
369 print(r' (void)dwDesiredAccess;')
Jose Fonsecadf913772015-07-17 16:54:47 +0100370 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100371 print(r' (void)hResource;')
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100372 self.checkResult(interface, method)
José Fonseca2b66b932014-07-30 17:31:52 +0100373 return
José Fonseca999284f2013-02-19 13:29:26 +0000374
375 if method.name == 'Map':
376 # Reset _DO_NOT_WAIT flags. Otherwise they may fail, and we have no
377 # way to cope with it (other than retry).
378 mapFlagsArg = method.getArgByName('MapFlags')
379 for flag in mapFlagsArg.type.values:
380 if flag.endswith('_MAP_FLAG_DO_NOT_WAIT'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100381 print(r' MapFlags &= ~%s;' % flag)
José Fonseca999284f2013-02-19 13:29:26 +0000382
José Fonseca7af569a2014-09-04 19:08:45 +0100383 if method.name.startswith('UpdateSubresource'):
José Fonsecac9cc24e2014-06-13 19:06:51 +0100384 # The D3D10 debug layer is buggy (or at least inconsistent with the
385 # runtime), as it seems to estimate and enforce the data size based on the
386 # SrcDepthPitch, even for non 3D textures, but in some traces
387 # SrcDepthPitch is garbagge for non 3D textures.
388 # XXX: It also seems to expect padding bytes at the end of the last
389 # row, but we never record (or allocate) those...
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100390 print(r' if (retrace::debug >= 2 && pDstBox && pDstBox->front == 0 && pDstBox->back == 1) {')
391 print(r' SrcDepthPitch = 0;')
392 print(r' }')
José Fonsecac9cc24e2014-06-13 19:06:51 +0100393
José Fonseca0688d2e2014-08-19 20:43:21 +0100394 if method.name == 'SetGammaControl':
395 # This method is only supported while in full-screen mode
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100396 print(r' if (retrace::forceWindowed) {')
397 print(r' return;')
398 print(r' }')
José Fonseca0688d2e2014-08-19 20:43:21 +0100399
Jose Fonseca60e65142017-06-09 13:06:32 +0100400 if method.name == 'GetData':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100401 print(r' pData = _allocator.alloc(DataSize);')
402 print(r' do {')
Jose Fonseca60e65142017-06-09 13:06:32 +0100403 self.doInvokeInterfaceMethod(interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100404 print(r' GetDataFlags = 0; // Prevent infinite loop')
405 print(r' } while (_result == S_FALSE);')
Jose Fonseca60e65142017-06-09 13:06:32 +0100406 self.checkResult(interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100407 print(r' return;')
Jose Fonseca60e65142017-06-09 13:06:32 +0100408
Jose Fonsecabfdf93e2019-06-26 13:03:27 +0100409 if method.name in ('CreateTexture1D', 'CreateTexture2D', 'CreateTexture3D', 'CreateBuffer'):
Jose Fonseca3d2b3fe2019-06-28 14:32:14 +0100410 # We don't capture multiple processes, so ignore keyed mutexes to avoid deadlocks
411 print(r' if (pDesc->MiscFlags & D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX) {')
412 print(r' pDesc->MiscFlags &= ~D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;')
413 print(r' pDesc->MiscFlags |= D3D11_RESOURCE_MISC_SHARED;')
414 print(r' }')
Jose Fonsecabfdf93e2019-06-26 13:03:27 +0100415
Jose Fonseca7da30bb2019-07-29 14:17:14 +0100416 if method.name == 'ReleaseSync':
417 # We must flush the device that used this surface, as per
418 # https://docs.microsoft.com/en-us/windows/win32/api/d3d10/nf-d3d10-id3d10device-opensharedresource
419 print(r'''
420 com_ptr<ID3D11DeviceChild> pDeviceChild;
421 com_ptr<ID3D11Device> pDevice;
422 com_ptr<ID3D11DeviceContext> pDeviceContext;
423 HRESULT hr = _this->QueryInterface(IID_ID3D11DeviceChild, (void **)&pDeviceChild);
424 if (SUCCEEDED(hr)) {
425 pDeviceChild->GetDevice(&pDevice);
426 pDevice->GetImmediateContext(&pDeviceContext);
427 pDeviceContext->Flush();
428 } else {
429 retrace::warning(call) << "ReleaseSync without D3D11 device\n";
430 }
431 (void)Key;
432 (void)_result;
433''')
434 return
José Fonseca610942b2012-11-08 10:46:03 +0000435
Jose Fonseca7da30bb2019-07-29 14:17:14 +0100436 Retracer.invokeInterfaceMethod(self, interface, method)
Jose Fonseca4d5cfa42015-07-17 16:21:05 +0100437
José Fonseca610942b2012-11-08 10:46:03 +0000438 # process events after presents
Jose Fonsecaced9c1c2015-08-10 15:10:37 +0100439 if interface.name.startswith('IDXGISwapChain') and method.name.startswith('Present'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100440 print(r' d3dretrace::processEvents();')
José Fonseca610942b2012-11-08 10:46:03 +0000441
José Fonseca6ca20082014-10-07 21:39:43 +0100442 if method.name in ('Map', 'Unmap'):
443 if interface.name.startswith('ID3D11DeviceContext'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100444 print(' void * & _pbData = g_Maps[_this][SubresourceKey(pResource, Subresource)];')
José Fonseca6ca20082014-10-07 21:39:43 +0100445 else:
446 subresourceArg = method.getArgByName('Subresource')
447 if subresourceArg is None:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100448 print(' UINT Subresource = 0;')
449 print(' void * & _pbData = g_Maps[0][SubresourceKey(_this, Subresource)];')
José Fonseca6ca20082014-10-07 21:39:43 +0100450
José Fonseca610942b2012-11-08 10:46:03 +0000451 if method.name == 'Map':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100452 print(' _MAP_DESC _MapDesc;')
453 print(' _getMapDesc(_this, %s, _MapDesc);' % ', '.join(method.argNames()))
454 print(' size_t _MappedSize = _MapDesc.Size;')
455 print(' if (_MapDesc.Size) {')
456 print(' _pbData = _MapDesc.pData;')
Jeff Muizelaar97244f92013-11-19 15:17:45 -0500457 if interface.name.startswith('ID3D11DeviceContext'):
Jose Fonseca1e3c0f72015-07-20 21:50:42 +0100458 # Prevent false warnings on 1D and 2D resources, since the
459 # pitches are often junk there...
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100460 print(' _normalizeMap(pResource, pMappedResource);')
Jeff Muizelaar97244f92013-11-19 15:17:45 -0500461 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100462 print(' _pbData = _MapDesc.pData;')
463 print(' } else {')
464 print(' return;')
465 print(' }')
Zack Rusin1cb7b772018-02-09 11:36:11 -0500466
José Fonseca610942b2012-11-08 10:46:03 +0000467 if method.name == 'Unmap':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100468 print(' if (_pbData) {')
469 print(' retrace::delRegionByPointer(_pbData);')
470 print(' _pbData = 0;')
471 print(' }')
José Fonseca6f810332012-11-11 10:05:09 +0000472
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100473 if interface.name.startswith('ID3D11VideoContext'):
474 if method.name == 'GetDecoderBuffer':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100475 print(' if (*ppBuffer && *pBufferSize) {')
476 print(' g_Maps[nullptr][SubresourceKey(_this, Type)] = *ppBuffer;')
477 print(' }')
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100478 if method.name == 'ReleaseDecoderBuffer':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100479 print(' SubresourceKey _mappingKey(_this, Type);')
480 print(' void *_pBuffer = g_Maps[nullptr][_mappingKey];')
481 print(' if (_pBuffer) {')
482 print(' retrace::delRegionByPointer(_pBuffer);')
483 print(' g_Maps[nullptr][_mappingKey] = 0;')
484 print(' }')
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100485
José Fonseca1d4fd142012-11-28 15:06:22 +0000486 # Attach shader byte code for lookup
487 if 'pShaderBytecode' in method.argNames():
488 ppShader = method.args[-1]
489 assert ppShader.output
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100490 print(r' if (retrace::dumpingState && SUCCEEDED(_result)) {')
491 print(r' (*%s)->SetPrivateData(d3dstate::GUID_D3DSTATE, BytecodeLength, pShaderBytecode);' % ppShader.name)
492 print(r' }')
José Fonseca1d4fd142012-11-28 15:06:22 +0000493
Zack Rusin1cb7b772018-02-09 11:36:11 -0500494 if method.name == 'CreateBuffer':
495 ppBuffer = method.args[-1]
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100496 print(r' if (retrace::dumpingState && SUCCEEDED(_result)) {')
497 print(r' char label[32];')
498 print(r' _snprintf(label, sizeof label, "0x%%llx", call.arg(%u).toArray()->values[0]->toUIntPtr());' % ppBuffer.index)
499 print(r' (*%s)->SetPrivateData(WKPDID_D3DDebugObjectName, strlen(label)+1, label);' % ppBuffer.name)
500 print(r' }')
Zack Rusin1cb7b772018-02-09 11:36:11 -0500501
Jose Fonseca37f769f2017-06-16 17:57:39 +0100502 def retraceInterfaceMethodBody(self, interface, method):
503 Retracer.retraceInterfaceMethodBody(self, interface, method)
504
505 # Add pitch swizzling information to the region
Jose Fonsecab2754af2017-06-19 14:22:55 +0100506 if method.name == 'Map' and interface.name not in ('ID3D10Buffer', 'ID3D10Texture1D'):
507 if interface.name.startswith('ID3D11DeviceContext'):
508 outArg = method.getArgByName('pMappedResource')
509 memberNames = ('pData', 'RowPitch', 'DepthPitch')
510 elif interface.name.startswith('ID3D10'):
511 outArg = method.args[-1]
512 memberNames = ('pData', 'RowPitch', 'DepthPitch')
513 elif interface.name == 'IDXGISurface':
514 outArg = method.getArgByName('pLockedRect')
515 memberNames = ('pBits', 'Pitch', None)
516 else:
517 raise NotImplementedError
518 struct = outArg.type.type
519 dataMemberName, rowPitchMemberName, depthPitchMemberName = memberNames
520 dataMemberIndex = struct.getMemberByName(dataMemberName)
521 rowPitchMemberIndex = struct.getMemberByName(rowPitchMemberName)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100522 print(r' if (_pbData && %s->%s != 0) {' % (outArg.name, rowPitchMemberName))
523 print(r' const trace::Array *_%s = call.arg(%u).toArray();' % (outArg.name, outArg.index))
524 print(r' if (%s) {' % outArg.name)
525 print(r' const trace::Struct *_struct = _%s->values[0]->toStruct();' % (outArg.name))
526 print(r' if (_struct) {')
527 print(r' unsigned long long traceAddress = _struct->members[%u]->toUIntPtr();' % dataMemberIndex)
528 print(r' int traceRowPitch = _struct->members[%u]->toSInt();' % rowPitchMemberIndex)
529 print(r' int realRowPitch = %s->%s;' % (outArg.name, rowPitchMemberName))
530 print(r' if (realRowPitch && traceRowPitch != realRowPitch) {')
531 print(r' retrace::setRegionPitch(traceAddress, 2, traceRowPitch, realRowPitch);')
532 print(r' }')
Jose Fonsecab2754af2017-06-19 14:22:55 +0100533 try:
534 depthPitchMemberIndex = struct.getMemberByName(depthPitchMemberName)
535 except ValueError:
536 assert len(struct.members) < 3
537 pass
538 else:
539 assert depthPitchMemberName == 'DepthPitch'
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100540 print(r' if (%s->DepthPitch) {' % outArg.name)
541 print(r' retrace::checkMismatch(call, "DepthPitch", _struct->members[%u], %s->DepthPitch);' % (struct.getMemberByName('DepthPitch'), outArg.name))
542 print(r' }')
543 print(r' }')
544 print(r' }')
545 print(r' }')
Jose Fonseca37f769f2017-06-16 17:57:39 +0100546
547
José Fonseca3be2c672015-02-06 15:36:40 +0000548 def extractArg(self, function, arg, arg_type, lvalue, rvalue):
549 # Set object names
550 if function.name == 'SetPrivateData' and arg.name == 'pData':
551 iid = function.args[0].name
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100552 print(r' if (%s != WKPDID_D3DDebugObjectName) {' % iid)
553 print(r' return;')
554 print(r' }')
José Fonseca3be2c672015-02-06 15:36:40 +0000555 # Interpret argument as string
556 Retracer.extractArg(self, function, arg, LPCSTR, lvalue, rvalue)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100557 print(r' if (!pData) {')
558 print(r' return;')
559 print(r' }')
560 print(r' assert(DataSize >= strlen((const char *)pData));')
561 print(r' // Some applications include the trailing zero terminator in the data')
562 print(r' DataSize = strlen((const char *)pData);')
José Fonseca3be2c672015-02-06 15:36:40 +0000563 return
564
565 Retracer.extractArg(self, function, arg, arg_type, lvalue, rvalue)
566
José Fonseca6f810332012-11-11 10:05:09 +0000567
568def main():
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100569 print(r'#define INITGUID')
570 print()
571 print(r'#include <string.h>')
572 print()
573 print(r'#include <iostream>')
574 print()
575 print(r'#include "d3dretrace.hpp"')
576 print(r'#include "os_version.hpp"')
577 print()
578 print(r'#include "d3dretrace_dxgi.hpp"')
579 print(r'#include "d3d10imports.hpp"')
580 print(r'#include "d3d10size.hpp"')
581 print(r'#include "d3d10state.hpp"')
582 print(r'#include "d3d11imports.hpp"')
583 print(r'#include "d3d11size.hpp"')
584 print(r'#include "dcompimports.hpp"')
585 print(r'#include "d3dstate.hpp"')
586 print(r'#include "d3d9imports.hpp" // D3DERR_WASSTILLDRAWING')
587 print()
588 print('''static d3dretrace::D3DDumper<IDXGISwapChain> dxgiDumper;''')
589 print('''static d3dretrace::D3DDumper<ID3D10Device> d3d10Dumper;''')
590 print('''static d3dretrace::D3DDumper<ID3D11DeviceContext> d3d11Dumper;''')
591 print()
José Fonseca6f810332012-11-11 10:05:09 +0000592
593 api = API()
José Fonseca50bee952015-02-07 22:32:19 +0000594 api.addModule(dxgi)
595 api.addModule(d3d10)
596 api.addModule(d3d10_1)
597 api.addModule(d3d11)
Jose Fonsecafeb8f7d2016-02-01 14:14:00 +0000598 api.addModule(dcomp)
José Fonsecafc58d052014-06-13 12:47:19 +0100599
José Fonseca6f810332012-11-11 10:05:09 +0000600 retracer = D3DRetracer()
601 retracer.retraceApi(api)
602
603
604if __name__ == '__main__':
605 main()