1 ##########################################################################
3 # Copyright 2011 Jose Fonseca
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:
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
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
24 ##########################################################################/
27 """D3D retracer generator."""
31 from dllretrace import DllRetracer as Retracer
32 from specs.stdapi import API
35 class D3DRetracer(Retracer):
37 def retraceApi(self, api):
38 print '// Swizzling mapping for lock addresses'
39 print 'static std::map<void *, void *> _maps;'
42 Retracer.retraceApi(self, api)
44 def invokeFunction(self, function):
45 if function.name in ('Direct3DCreate9', 'Direct3DCreate9Ex'):
46 print 'if (retrace::debug && !g_szD3D9DllName) {'
48 print ' * XXX: D3D9D only works for simple things, it often introduces errors'
49 print ' * on complex traces, or traces which use unofficial D3D9 features.'
52 print ' g_szD3D9DllName = "d3d9d.dll";'
56 Retracer.invokeFunction(self, function)
58 def invokeInterfaceMethod(self, interface, method):
59 # keep track of the last used device for state dumping
60 if interface.name in ('IDirect3DDevice9', 'IDirect3DDevice9Ex'):
61 if method.name == 'Release':
62 print r' d3d9Dumper.unbindDevice(_this);'
64 print r' d3d9Dumper.bindDevice(_this);'
66 # create windows as neccessary
67 if method.name in ('CreateDevice', 'CreateDeviceEx', 'CreateAdditionalSwapChain'):
68 print r' HWND hWnd = d3dretrace::createWindow(pPresentationParameters->BackBufferWidth, pPresentationParameters->BackBufferHeight);'
69 print r' pPresentationParameters->hDeviceWindow = hWnd;'
70 if 'hFocusWindow' in method.argNames():
71 print r' hFocusWindow = hWnd;'
73 if method.name in ('Reset', 'ResetEx'):
74 print r' if (pPresentationParameters->Windowed) {'
75 print r' d3dretrace::resizeWindow(pPresentationParameters->hDeviceWindow, pPresentationParameters->BackBufferWidth, pPresentationParameters->BackBufferHeight);'
78 # notify frame has been completed
79 if method.name == 'Present':
80 print r' retrace::frameComplete(call);'
81 print r' hDestWindowOverride = NULL;'
83 if 'pSharedHandle' in method.argNames():
84 print r' if (pSharedHandle) {'
85 print r' retrace::warning(call) << "shared surfaces unsupported\n";'
86 print r' pSharedHandle = NULL;'
89 Retracer.invokeInterfaceMethod(self, interface, method)
91 # process events after presents
92 if method.name == 'Present':
93 print r' d3dretrace::processEvents();'
95 if method.name in ('Lock', 'LockRect', 'LockBox'):
96 print ' VOID *_pbData = NULL;'
97 print ' size_t _MappedSize = 0;'
98 print ' _getMapInfo(_this, %s, _pbData, _MappedSize);' % ', '.join(method.argNames()[:-1])
99 print ' if (_MappedSize) {'
100 print ' _maps[_this] = _pbData;'
105 if method.name in ('Unlock', 'UnlockRect', 'UnlockBox'):
106 print ' VOID *_pbData = 0;'
107 print ' _pbData = _maps[_this];'
108 print ' if (_pbData) {'
109 print ' retrace::delRegionByPointer(_pbData);'
110 print ' _maps[_this] = 0;'
115 print r'#include <string.h>'
117 print r'#include <iostream>'
119 print r'#include "d3dretrace.hpp"'
122 moduleName = sys.argv[1]
123 support = int(sys.argv[2])
128 if moduleName == 'd3d9':
129 from specs.d3d9 import d3d9
130 print r'#include "d3d9imports.hpp"'
131 print r'#include "d3d9size.hpp"'
134 print '''static d3dretrace::D3DDumper<IDirect3DDevice9> d3d9Dumper;'''
136 elif moduleName == 'd3d8':
137 from specs.d3d8 import d3d8
138 print r'#include <windows.h>'
139 print r'#include <d3d8.h>'
140 print r'#include "d3d8size.hpp"'
143 #print '''static d3dretrace::D3DDumper<IDirect3DDevice8> d3d8Dumper;'''
148 retracer = D3DRetracer()
149 retracer.table_name = 'd3dretrace::%s_callbacks' % moduleName
150 retracer.retraceApi(api)
153 if __name__ == '__main__':