blob: f4b5ecdeae07dce2e1c8d290bf4dfe99b0d072bd [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 Fonseca33eb5f32020-04-29 19:49:21 +0100211 # GPU counters are vendor specific and likely to fail, so use a
212 # timestamp query instead, which is guaranteed to succeed
213 if method.name == 'CreateCounter':
214 if interface.name.startswith('ID3D10'):
215 print(r' D3D10_QUERY_DESC _queryDesc;')
216 print(r' _queryDesc.Query = D3D10_QUERY_TIMESTAMP;')
217 print(r' _queryDesc.MiscFlags = 0;')
218 print(r' _result = _this->CreateQuery(&_queryDesc, reinterpret_cast<ID3D10Query **>(ppCounter));')
219 return
220 if interface.name.startswith('ID3D11'):
221 print(r' D3D11_QUERY_DESC _queryDesc;')
222 print(r' _queryDesc.Query = D3D11_QUERY_TIMESTAMP;')
223 print(r' _queryDesc.MiscFlags = 0;')
224 print(r' _result = _this->CreateQuery(&_queryDesc, reinterpret_cast<ID3D11Query **>(ppCounter));')
225 return
226
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100227 Retracer.doInvokeInterfaceMethod(self, interface, method)
228
Jose Fonseca95796502018-11-20 13:11:31 +0000229 # Force driver
230 if interface.name.startswith('IDXGIFactory') and method.name.startswith('EnumAdapters'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100231 print(r' const char *szSoftware = NULL;')
232 print(r' switch (retrace::driver) {')
233 print(r' case retrace::DRIVER_REFERENCE:')
234 print(r' szSoftware = "d3d11ref.dll";')
235 print(r' break;')
236 print(r' case retrace::DRIVER_SOFTWARE:')
237 print(r' szSoftware = "d3d10warp.dll";')
238 print(r' break;')
239 print(r' case retrace::DRIVER_MODULE:')
240 print(r' szSoftware = retrace::driverModule;')
241 print(r' break;')
242 print(r' default:')
243 print(r' break;')
244 print(r' }')
245 print(r' HMODULE hSoftware = NULL;')
246 print(r' if (szSoftware) {')
247 print(r' hSoftware = LoadLibraryA(szSoftware);')
248 print(r' if (!hSoftware) {')
249 print(r' retrace::warning(call) << "failed to load " << szSoftware << "\n";')
250 print(r' }')
251 print(r' }')
252 print(r' if (hSoftware) {')
253 print(r' _result = _this->CreateSoftwareAdapter(hSoftware, reinterpret_cast<IDXGIAdapter **>(ppAdapter));')
254 print(r' } else {')
255 print(r' if (Adapter != 0) {')
256 print(r' retrace::warning(call) << "ignoring non-default adapter " << Adapter << "\n";')
257 print(r' Adapter = 0;')
258 print(r' }')
Jose Fonseca95796502018-11-20 13:11:31 +0000259 Retracer.doInvokeInterfaceMethod(self, interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100260 print(r' }')
Jose Fonseca95796502018-11-20 13:11:31 +0000261 return
262
263 if interface.name.startswith('IDXGIFactory') and method.name == 'CreateSoftwareAdapter':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100264 print(r' const char *szSoftware = NULL;')
265 print(r' switch (retrace::driver) {')
266 print(r' case retrace::DRIVER_REFERENCE:')
267 print(r' szSoftware = "d3d11ref.dll";')
268 print(r' break;')
269 print(r' case retrace::DRIVER_MODULE:')
270 print(r' szSoftware = retrace::driverModule;')
271 print(r' break;')
272 print(r' case retrace::DRIVER_SOFTWARE:')
273 print(r' default:')
274 print(r' szSoftware = "d3d10warp.dll";')
275 print(r' break;')
276 print(r' }')
277 print(r' Module = LoadLibraryA("d3d10warp");')
278 print(r' if (!Module) {')
279 print(r' retrace::warning(call) << "failed to load " << szSoftware << "\n";')
280 print(r' }')
Jose Fonseca95796502018-11-20 13:11:31 +0000281 Retracer.doInvokeInterfaceMethod(self, interface, method)
282
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100283 # Keep retrying ID3D11VideoContext::DecoderBeginFrame when returns E_PENDING
284 if interface.name == 'ID3D11VideoContext' and method.name == 'DecoderBeginFrame':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100285 print(r' while (_result == D3DERR_WASSTILLDRAWING || _result == E_PENDING) {')
286 print(r' Sleep(1);')
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100287 Retracer.doInvokeInterfaceMethod(self, interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100288 print(r' }')
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100289
José Fonseca610942b2012-11-08 10:46:03 +0000290 def invokeInterfaceMethod(self, interface, method):
291 # keep track of the last used device for state dumping
José Fonseca944088e2012-11-20 14:47:03 +0000292 if interface.name in ('ID3D10Device', 'ID3D10Device1'):
293 if method.name == 'Release':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100294 print(r' if (call.ret->toUInt() == 0) {')
295 print(r' d3d10Dumper.unbindDevice(_this);')
296 print(r' }')
José Fonseca944088e2012-11-20 14:47:03 +0000297 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100298 print(r' d3d10Dumper.bindDevice(_this);')
Jose Fonseca4a1c7aa2015-08-10 16:53:49 +0100299 if interface.name.startswith('ID3D11DeviceContext'):
José Fonseca5773beb2012-11-14 11:46:58 +0000300 if method.name == 'Release':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100301 print(r' if (call.ret->toUInt() == 0) {')
302 print(r' d3d11Dumper.unbindDevice(_this);')
303 print(r' }')
José Fonseca5773beb2012-11-14 11:46:58 +0000304 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100305 print(r' d3d11Dumper.bindDevice(_this);')
José Fonseca610942b2012-11-08 10:46:03 +0000306
José Fonseca1e309b42014-09-18 11:08:09 +0100307 # intercept private interfaces
José Fonsecad59843c2014-08-22 16:59:25 +0100308 if method.name == 'QueryInterface':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100309 print(r' if (!d3dretrace::overrideQueryInterface(_this, riid, ppvObj, &_result)) {')
José Fonseca75cbb8c2013-02-12 16:19:23 +0000310 Retracer.invokeInterfaceMethod(self, interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100311 print(r' }')
José Fonseca75cbb8c2013-02-12 16:19:23 +0000312 return
313
José Fonseca610942b2012-11-08 10:46:03 +0000314 # create windows as neccessary
315 if method.name == 'CreateSwapChain':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100316 print(r' d3dretrace::createWindowForSwapChain(pDesc);')
Jose Fonsecadafb4ea2015-08-10 14:40:22 +0100317 if method.name == 'CreateSwapChainForHwnd':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100318 print(r' hWnd = d3dretrace::createWindow(pDesc->Width, pDesc->Height);')
319 print(r' // DXGI_SCALING_NONE is only supported on Win8 and beyond')
320 print(r' if (pDesc->Scaling == DXGI_SCALING_NONE && !IsWindows8OrGreater()) {')
321 print(r' pDesc->Scaling = DXGI_SCALING_STRETCH;')
322 print(r' }')
José Fonsecae23a6be2014-09-14 20:30:00 +0100323 if method.name == 'CreateSwapChainForComposition':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100324 print(r' HWND hWnd = d3dretrace::createWindow(pDesc->Width, pDesc->Height);')
325 print(r' _result = _this->CreateSwapChainForHwnd(pDevice, hWnd, pDesc, NULL, pRestrictToOutput, ppSwapChain);')
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100326 self.checkResult(interface, method)
José Fonsecae23a6be2014-09-14 20:30:00 +0100327 return
Jose Fonsecafeb8f7d2016-02-01 14:14:00 +0000328 if method.name == 'CreateTargetForHwnd':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100329 print(r' hwnd = d3dretrace::createWindow(1024, 768);')
José Fonseca610942b2012-11-08 10:46:03 +0000330
José Fonseca7618cb92014-08-28 16:00:48 +0100331 if method.name == 'SetFullscreenState':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100332 print(r' if (retrace::forceWindowed) {')
333 print(r' DXGI_SWAP_CHAIN_DESC Desc;')
334 print(r' _this->GetDesc(&Desc);')
335 print(r' if (Desc.BufferDesc.Format != DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM) {')
336 print(r' Fullscreen = FALSE;')
337 print(r' pTarget = nullptr;')
338 print(r' }')
339 print(r' }')
José Fonseca7618cb92014-08-28 16:00:48 +0100340
José Fonseca610942b2012-11-08 10:46:03 +0000341 # notify frame has been completed
Jose Fonsecaced9c1c2015-08-10 15:10:37 +0100342 if interface.name.startswith('IDXGISwapChain') and method.name.startswith('Present'):
343 if interface.name.startswith('IDXGISwapChainDWM'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100344 print(r' com_ptr<IDXGISwapChain> pSwapChain;')
345 print(r' if (SUCCEEDED(_this->QueryInterface(IID_IDXGISwapChain, (void **) &pSwapChain))) {')
346 print(r' dxgiDumper.bindDevice(pSwapChain);')
347 print(r' } else {')
348 print(r' assert(0);')
349 print(r' }')
José Fonseca29d4bda2014-02-28 17:50:24 +0000350 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100351 print(r' dxgiDumper.bindDevice(_this);')
Jose Fonseca0f541f42019-07-03 13:46:05 +0100352 print(r' if ((Flags & DXGI_PRESENT_TEST) == 0) {')
353 print(r' retrace::frameComplete(call);')
354 print(r' }')
José Fonseca610942b2012-11-08 10:46:03 +0000355
356 if 'pSharedResource' in method.argNames():
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100357 print(r' if (pSharedResource) {')
358 print(r' retrace::warning(call) << "shared surfaces unsupported\n";')
359 print(r' pSharedResource = NULL;')
360 print(r' }')
José Fonseca610942b2012-11-08 10:46:03 +0000361
José Fonsecae23a6be2014-09-14 20:30:00 +0100362 if interface.name.startswith('ID3D10Device') and method.name.startswith('OpenSharedResource'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100363 print(r' retrace::warning(call) << "replacing shared resource with checker pattern\n";')
364 print(r' _result = d3dretrace::createSharedResource(_this, ReturnedInterface, ppResource);')
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100365 self.checkResult(interface, method)
José Fonseca999284f2013-02-19 13:29:26 +0000366 return
Jose Fonseca559b5f82015-07-17 16:58:50 +0100367 if interface.name.startswith('ID3D11Device') and method.name == 'OpenSharedResource':
368 # Some applications (e.g., video playing in IE11) create shared resources within the same process.
369 # TODO: Generalize to other OpenSharedResource variants
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100370 print(r' retrace::map<HANDLE>::const_iterator it = _shared_handle_map.find(hResource);')
371 print(r' if (it == _shared_handle_map.end()) {')
372 print(r' retrace::warning(call) << "replacing shared resource with checker pattern\n";')
373 print(r' _result = d3dretrace::createSharedResource(_this, ReturnedInterface, ppResource);')
Jose Fonseca559b5f82015-07-17 16:58:50 +0100374 self.checkResult(interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100375 print(r' } else {')
376 print(r' hResource = it->second;')
Jose Fonseca559b5f82015-07-17 16:58:50 +0100377 Retracer.invokeInterfaceMethod(self, interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100378 print(r' }')
Jose Fonseca559b5f82015-07-17 16:58:50 +0100379 return
José Fonseca2b66b932014-07-30 17:31:52 +0100380 if interface.name.startswith('ID3D11Device') and method.name.startswith('OpenSharedResource'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100381 print(r' retrace::warning(call) << "replacing shared resource with checker pattern\n";')
382 print(r' _result = d3dretrace::createSharedResource(_this, ReturnedInterface, ppResource);')
José Fonseca2b66b932014-07-30 17:31:52 +0100383 if method.name == 'OpenSharedResourceByName':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100384 print(r' (void)lpName;')
385 print(r' (void)dwDesiredAccess;')
Jose Fonsecadf913772015-07-17 16:54:47 +0100386 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100387 print(r' (void)hResource;')
Jose Fonseca5c487ec2015-06-02 22:16:52 +0100388 self.checkResult(interface, method)
José Fonseca2b66b932014-07-30 17:31:52 +0100389 return
José Fonseca999284f2013-02-19 13:29:26 +0000390
391 if method.name == 'Map':
392 # Reset _DO_NOT_WAIT flags. Otherwise they may fail, and we have no
393 # way to cope with it (other than retry).
394 mapFlagsArg = method.getArgByName('MapFlags')
395 for flag in mapFlagsArg.type.values:
396 if flag.endswith('_MAP_FLAG_DO_NOT_WAIT'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100397 print(r' MapFlags &= ~%s;' % flag)
José Fonseca999284f2013-02-19 13:29:26 +0000398
José Fonseca7af569a2014-09-04 19:08:45 +0100399 if method.name.startswith('UpdateSubresource'):
José Fonsecac9cc24e2014-06-13 19:06:51 +0100400 # The D3D10 debug layer is buggy (or at least inconsistent with the
401 # runtime), as it seems to estimate and enforce the data size based on the
402 # SrcDepthPitch, even for non 3D textures, but in some traces
403 # SrcDepthPitch is garbagge for non 3D textures.
404 # XXX: It also seems to expect padding bytes at the end of the last
405 # row, but we never record (or allocate) those...
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100406 print(r' if (retrace::debug >= 2 && pDstBox && pDstBox->front == 0 && pDstBox->back == 1) {')
407 print(r' SrcDepthPitch = 0;')
408 print(r' }')
José Fonsecac9cc24e2014-06-13 19:06:51 +0100409
José Fonseca0688d2e2014-08-19 20:43:21 +0100410 if method.name == 'SetGammaControl':
411 # This method is only supported while in full-screen mode
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100412 print(r' if (retrace::forceWindowed) {')
413 print(r' return;')
414 print(r' }')
José Fonseca0688d2e2014-08-19 20:43:21 +0100415
Jose Fonseca60e65142017-06-09 13:06:32 +0100416 if method.name == 'GetData':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100417 print(r' pData = _allocator.alloc(DataSize);')
418 print(r' do {')
Jose Fonseca60e65142017-06-09 13:06:32 +0100419 self.doInvokeInterfaceMethod(interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100420 print(r' GetDataFlags = 0; // Prevent infinite loop')
421 print(r' } while (_result == S_FALSE);')
Jose Fonseca60e65142017-06-09 13:06:32 +0100422 self.checkResult(interface, method)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100423 print(r' return;')
Jose Fonseca60e65142017-06-09 13:06:32 +0100424
Jose Fonsecabfdf93e2019-06-26 13:03:27 +0100425 if method.name in ('CreateTexture1D', 'CreateTexture2D', 'CreateTexture3D', 'CreateBuffer'):
Jose Fonseca3d2b3fe2019-06-28 14:32:14 +0100426 # We don't capture multiple processes, so ignore keyed mutexes to avoid deadlocks
427 print(r' if (pDesc->MiscFlags & D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX) {')
428 print(r' pDesc->MiscFlags &= ~D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;')
429 print(r' pDesc->MiscFlags |= D3D11_RESOURCE_MISC_SHARED;')
430 print(r' }')
Jose Fonsecabfdf93e2019-06-26 13:03:27 +0100431
Jose Fonseca7da30bb2019-07-29 14:17:14 +0100432 if method.name == 'ReleaseSync':
433 # We must flush the device that used this surface, as per
434 # https://docs.microsoft.com/en-us/windows/win32/api/d3d10/nf-d3d10-id3d10device-opensharedresource
435 print(r'''
436 com_ptr<ID3D11DeviceChild> pDeviceChild;
437 com_ptr<ID3D11Device> pDevice;
438 com_ptr<ID3D11DeviceContext> pDeviceContext;
439 HRESULT hr = _this->QueryInterface(IID_ID3D11DeviceChild, (void **)&pDeviceChild);
440 if (SUCCEEDED(hr)) {
441 pDeviceChild->GetDevice(&pDevice);
442 pDevice->GetImmediateContext(&pDeviceContext);
443 pDeviceContext->Flush();
444 } else {
445 retrace::warning(call) << "ReleaseSync without D3D11 device\n";
446 }
447 (void)Key;
448 (void)_result;
449''')
450 return
José Fonseca610942b2012-11-08 10:46:03 +0000451
Jose Fonseca7da30bb2019-07-29 14:17:14 +0100452 Retracer.invokeInterfaceMethod(self, interface, method)
Jose Fonseca4d5cfa42015-07-17 16:21:05 +0100453
José Fonseca610942b2012-11-08 10:46:03 +0000454 # process events after presents
Jose Fonsecaced9c1c2015-08-10 15:10:37 +0100455 if interface.name.startswith('IDXGISwapChain') and method.name.startswith('Present'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100456 print(r' d3dretrace::processEvents();')
José Fonseca610942b2012-11-08 10:46:03 +0000457
José Fonseca6ca20082014-10-07 21:39:43 +0100458 if method.name in ('Map', 'Unmap'):
459 if interface.name.startswith('ID3D11DeviceContext'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100460 print(' void * & _pbData = g_Maps[_this][SubresourceKey(pResource, Subresource)];')
José Fonseca6ca20082014-10-07 21:39:43 +0100461 else:
462 subresourceArg = method.getArgByName('Subresource')
463 if subresourceArg is None:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100464 print(' UINT Subresource = 0;')
465 print(' void * & _pbData = g_Maps[0][SubresourceKey(_this, Subresource)];')
José Fonseca6ca20082014-10-07 21:39:43 +0100466
José Fonseca610942b2012-11-08 10:46:03 +0000467 if method.name == 'Map':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100468 print(' _MAP_DESC _MapDesc;')
469 print(' _getMapDesc(_this, %s, _MapDesc);' % ', '.join(method.argNames()))
470 print(' size_t _MappedSize = _MapDesc.Size;')
471 print(' if (_MapDesc.Size) {')
472 print(' _pbData = _MapDesc.pData;')
Jeff Muizelaar97244f92013-11-19 15:17:45 -0500473 if interface.name.startswith('ID3D11DeviceContext'):
Jose Fonseca1e3c0f72015-07-20 21:50:42 +0100474 # Prevent false warnings on 1D and 2D resources, since the
475 # pitches are often junk there...
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100476 print(' _normalizeMap(pResource, pMappedResource);')
Jeff Muizelaar97244f92013-11-19 15:17:45 -0500477 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100478 print(' _pbData = _MapDesc.pData;')
479 print(' } else {')
480 print(' return;')
481 print(' }')
Zack Rusin1cb7b772018-02-09 11:36:11 -0500482
José Fonseca610942b2012-11-08 10:46:03 +0000483 if method.name == 'Unmap':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100484 print(' if (_pbData) {')
485 print(' retrace::delRegionByPointer(_pbData);')
486 print(' _pbData = 0;')
487 print(' }')
José Fonseca6f810332012-11-11 10:05:09 +0000488
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100489 if interface.name.startswith('ID3D11VideoContext'):
490 if method.name == 'GetDecoderBuffer':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100491 print(' if (*ppBuffer && *pBufferSize) {')
492 print(' g_Maps[nullptr][SubresourceKey(_this, Type)] = *ppBuffer;')
493 print(' }')
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100494 if method.name == 'ReleaseDecoderBuffer':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100495 print(' SubresourceKey _mappingKey(_this, Type);')
496 print(' void *_pBuffer = g_Maps[nullptr][_mappingKey];')
497 print(' if (_pBuffer) {')
498 print(' retrace::delRegionByPointer(_pBuffer);')
499 print(' g_Maps[nullptr][_mappingKey] = 0;')
500 print(' }')
Jose Fonseca2d78bef2016-05-19 15:10:23 +0100501
José Fonseca1d4fd142012-11-28 15:06:22 +0000502 # Attach shader byte code for lookup
503 if 'pShaderBytecode' in method.argNames():
504 ppShader = method.args[-1]
505 assert ppShader.output
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100506 print(r' if (retrace::dumpingState && SUCCEEDED(_result)) {')
507 print(r' (*%s)->SetPrivateData(d3dstate::GUID_D3DSTATE, BytecodeLength, pShaderBytecode);' % ppShader.name)
508 print(r' }')
José Fonseca1d4fd142012-11-28 15:06:22 +0000509
Zack Rusin1cb7b772018-02-09 11:36:11 -0500510 if method.name == 'CreateBuffer':
511 ppBuffer = method.args[-1]
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100512 print(r' if (retrace::dumpingState && SUCCEEDED(_result)) {')
513 print(r' char label[32];')
514 print(r' _snprintf(label, sizeof label, "0x%%llx", call.arg(%u).toArray()->values[0]->toUIntPtr());' % ppBuffer.index)
515 print(r' (*%s)->SetPrivateData(WKPDID_D3DDebugObjectName, strlen(label)+1, label);' % ppBuffer.name)
516 print(r' }')
Zack Rusin1cb7b772018-02-09 11:36:11 -0500517
Jose Fonseca37f769f2017-06-16 17:57:39 +0100518 def retraceInterfaceMethodBody(self, interface, method):
519 Retracer.retraceInterfaceMethodBody(self, interface, method)
520
521 # Add pitch swizzling information to the region
Jose Fonsecab2754af2017-06-19 14:22:55 +0100522 if method.name == 'Map' and interface.name not in ('ID3D10Buffer', 'ID3D10Texture1D'):
523 if interface.name.startswith('ID3D11DeviceContext'):
524 outArg = method.getArgByName('pMappedResource')
525 memberNames = ('pData', 'RowPitch', 'DepthPitch')
526 elif interface.name.startswith('ID3D10'):
527 outArg = method.args[-1]
528 memberNames = ('pData', 'RowPitch', 'DepthPitch')
529 elif interface.name == 'IDXGISurface':
530 outArg = method.getArgByName('pLockedRect')
531 memberNames = ('pBits', 'Pitch', None)
532 else:
533 raise NotImplementedError
534 struct = outArg.type.type
535 dataMemberName, rowPitchMemberName, depthPitchMemberName = memberNames
536 dataMemberIndex = struct.getMemberByName(dataMemberName)
537 rowPitchMemberIndex = struct.getMemberByName(rowPitchMemberName)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100538 print(r' if (_pbData && %s->%s != 0) {' % (outArg.name, rowPitchMemberName))
539 print(r' const trace::Array *_%s = call.arg(%u).toArray();' % (outArg.name, outArg.index))
540 print(r' if (%s) {' % outArg.name)
541 print(r' const trace::Struct *_struct = _%s->values[0]->toStruct();' % (outArg.name))
542 print(r' if (_struct) {')
543 print(r' unsigned long long traceAddress = _struct->members[%u]->toUIntPtr();' % dataMemberIndex)
544 print(r' int traceRowPitch = _struct->members[%u]->toSInt();' % rowPitchMemberIndex)
545 print(r' int realRowPitch = %s->%s;' % (outArg.name, rowPitchMemberName))
546 print(r' if (realRowPitch && traceRowPitch != realRowPitch) {')
547 print(r' retrace::setRegionPitch(traceAddress, 2, traceRowPitch, realRowPitch);')
548 print(r' }')
Jose Fonsecab2754af2017-06-19 14:22:55 +0100549 try:
550 depthPitchMemberIndex = struct.getMemberByName(depthPitchMemberName)
551 except ValueError:
552 assert len(struct.members) < 3
553 pass
554 else:
555 assert depthPitchMemberName == 'DepthPitch'
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100556 print(r' if (%s->DepthPitch) {' % outArg.name)
557 print(r' retrace::checkMismatch(call, "DepthPitch", _struct->members[%u], %s->DepthPitch);' % (struct.getMemberByName('DepthPitch'), outArg.name))
558 print(r' }')
559 print(r' }')
560 print(r' }')
561 print(r' }')
Jose Fonseca37f769f2017-06-16 17:57:39 +0100562
563
José Fonseca3be2c672015-02-06 15:36:40 +0000564 def extractArg(self, function, arg, arg_type, lvalue, rvalue):
565 # Set object names
566 if function.name == 'SetPrivateData' and arg.name == 'pData':
567 iid = function.args[0].name
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100568 print(r' if (%s != WKPDID_D3DDebugObjectName) {' % iid)
569 print(r' return;')
570 print(r' }')
José Fonseca3be2c672015-02-06 15:36:40 +0000571 # Interpret argument as string
572 Retracer.extractArg(self, function, arg, LPCSTR, lvalue, rvalue)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100573 print(r' if (!pData) {')
574 print(r' return;')
575 print(r' }')
576 print(r' assert(DataSize >= strlen((const char *)pData));')
577 print(r' // Some applications include the trailing zero terminator in the data')
578 print(r' DataSize = strlen((const char *)pData);')
José Fonseca3be2c672015-02-06 15:36:40 +0000579 return
580
581 Retracer.extractArg(self, function, arg, arg_type, lvalue, rvalue)
582
José Fonseca6f810332012-11-11 10:05:09 +0000583
584def main():
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100585 print(r'#define INITGUID')
586 print()
587 print(r'#include <string.h>')
588 print()
589 print(r'#include <iostream>')
590 print()
591 print(r'#include "d3dretrace.hpp"')
592 print(r'#include "os_version.hpp"')
593 print()
594 print(r'#include "d3dretrace_dxgi.hpp"')
595 print(r'#include "d3d10imports.hpp"')
596 print(r'#include "d3d10size.hpp"')
597 print(r'#include "d3d10state.hpp"')
598 print(r'#include "d3d11imports.hpp"')
599 print(r'#include "d3d11size.hpp"')
600 print(r'#include "dcompimports.hpp"')
601 print(r'#include "d3dstate.hpp"')
602 print(r'#include "d3d9imports.hpp" // D3DERR_WASSTILLDRAWING')
603 print()
604 print('''static d3dretrace::D3DDumper<IDXGISwapChain> dxgiDumper;''')
605 print('''static d3dretrace::D3DDumper<ID3D10Device> d3d10Dumper;''')
606 print('''static d3dretrace::D3DDumper<ID3D11DeviceContext> d3d11Dumper;''')
607 print()
José Fonseca6f810332012-11-11 10:05:09 +0000608
609 api = API()
José Fonseca50bee952015-02-07 22:32:19 +0000610 api.addModule(dxgi)
611 api.addModule(d3d10)
612 api.addModule(d3d10_1)
613 api.addModule(d3d11)
Jose Fonsecafeb8f7d2016-02-01 14:14:00 +0000614 api.addModule(dcomp)
José Fonsecafc58d052014-06-13 12:47:19 +0100615
José Fonseca6f810332012-11-11 10:05:09 +0000616 retracer = D3DRetracer()
617 retracer.retraceApi(api)
618
619
620if __name__ == '__main__':
621 main()