blob: b507182e63d4b8cbf72e244bac29ccadef2897b8 [file] [log] [blame]
José Fonseca50c2a142015-01-15 13:10:46 +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
30import sys
31from dllretrace import DllRetracer as Retracer
32from specs.stdapi import API
33from specs.d3d import ddraw, HWND
Jose Fonseca410e4f92015-10-30 23:07:26 +000034from specs.ddraw import DDCREATE_LPGUID
José Fonseca50c2a142015-01-15 13:10:46 +000035
36
37class D3DRetracer(Retracer):
38
39 def retraceApi(self, api):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010040 print('// Swizzling mapping for lock addresses')
41 print('static std::map<void *, void *> _maps;')
42 print()
José Fonsecac28e6062015-01-16 12:33:25 +000043 # TODO: Keep a table of windows
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010044 print('static HWND g_hWnd;')
45 print()
José Fonseca50c2a142015-01-15 13:10:46 +000046
47 Retracer.retraceApi(self, api)
48
49 def invokeInterfaceMethod(self, interface, method):
50 # keep track of the last used device for state dumping
José Fonsecaff65c912015-01-16 09:39:52 +000051 if interface.name in ('IDirect3DDevice7',):
52 if method.name == 'Release':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010053 print(r' if (call.ret->toUInt() == 0) {')
54 print(r' d3d7Dumper.unbindDevice(_this);')
55 print(r' }')
José Fonsecaff65c912015-01-16 09:39:52 +000056 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010057 print(r' d3d7Dumper.bindDevice(_this);')
José Fonseca50c2a142015-01-15 13:10:46 +000058
59 # create windows as neccessary
60 hWndArg = method.getArgByType(HWND)
61 if hWndArg is not None:
José Fonsecac28e6062015-01-16 12:33:25 +000062 # FIXME: Try to guess the window size (e.g., from IDirectDrawSurface7::Blt)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010063 print(r' if (!g_hWnd) {')
64 print(r' g_hWnd = d3dretrace::createWindow(512, 512);')
65 print(r' }')
66 print(r' %s = g_hWnd;' % hWndArg.name)
José Fonseca50c2a142015-01-15 13:10:46 +000067
68
José Fonsecac28e6062015-01-16 12:33:25 +000069 if method.name == 'Lock':
José Fonseca50c2a142015-01-15 13:10:46 +000070 # Reset _DONOTWAIT flags. Otherwise they may fail, and we have no
71 # way to cope with it (other than retry).
72 mapFlagsArg = method.getArgByName('dwFlags')
73 if mapFlagsArg is not None:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010074 print(r' dwFlags &= ~DDLOCK_DONOTWAIT;')
75 print(r' dwFlags |= DDLOCK_WAIT;')
José Fonseca50c2a142015-01-15 13:10:46 +000076
77 Retracer.invokeInterfaceMethod(self, interface, method)
78
79 if method.name == 'CreateDevice':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010080 print(r' if (FAILED(_result)) {')
81 print(r' exit(1);')
82 print(r' }')
José Fonseca50c2a142015-01-15 13:10:46 +000083
José Fonsecaff65c912015-01-16 09:39:52 +000084 # notify frame has been completed
José Fonseca50c2a142015-01-15 13:10:46 +000085 # process events after presents
José Fonsecaff65c912015-01-16 09:39:52 +000086 if interface.name == 'IDirectDrawSurface7' and method.name == 'Blt':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010087 print(r' DDSCAPS2 ddsCaps;')
88 print(r' if (SUCCEEDED(_this->GetCaps(&ddsCaps)) &&')
89 print(r' (ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)) {')
90 print(r' retrace::frameComplete(call);')
91 print(r' d3dretrace::processEvents();')
92 print(r' }')
José Fonseca50c2a142015-01-15 13:10:46 +000093
José Fonsecac28e6062015-01-16 12:33:25 +000094 if method.name == 'Lock':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010095 print(' VOID *_pbData = NULL;')
96 print(' size_t _MappedSize = 0;')
José Fonsecac28e6062015-01-16 12:33:25 +000097 # FIXME: determine the mapping size
José Fonseca50c2a142015-01-15 13:10:46 +000098 #print ' _getMapInfo(_this, %s, _pbData, _MappedSize);' % ', '.join(method.argNames()[:-1])
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010099 print(' if (_MappedSize) {')
100 print(' _maps[_this] = _pbData;')
José Fonsecac28e6062015-01-16 12:33:25 +0000101 # TODO: check pitches match
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100102 print(' } else {')
103 print(' return;')
104 print(' }')
José Fonseca50c2a142015-01-15 13:10:46 +0000105
José Fonsecac28e6062015-01-16 12:33:25 +0000106 if method.name == 'Unlock':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100107 print(' VOID *_pbData = 0;')
108 print(' _pbData = _maps[_this];')
109 print(' if (_pbData) {')
110 print(' retrace::delRegionByPointer(_pbData);')
111 print(' _maps[_this] = 0;')
112 print(' }')
José Fonseca50c2a142015-01-15 13:10:46 +0000113
Jose Fonseca410e4f92015-10-30 23:07:26 +0000114 def extractArg(self, function, arg, arg_type, lvalue, rvalue):
115 # Handle DDCREATE_* flags
116 if arg.type is DDCREATE_LPGUID:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100117 print(' if (%s.toArray()) {' % rvalue)
Jose Fonseca410e4f92015-10-30 23:07:26 +0000118 Retracer.extractArg(self, function, arg, arg_type, lvalue, rvalue)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100119 print(' } else {')
120 print(' %s = static_cast<%s>(%s.toPointer());' % (lvalue, arg_type, rvalue))
121 print(' }')
Jose Fonseca410e4f92015-10-30 23:07:26 +0000122 return
123
124 Retracer.extractArg(self, function, arg, arg_type, lvalue, rvalue)
125
José Fonseca50c2a142015-01-15 13:10:46 +0000126
127def main():
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100128 print(r'#include <string.h>')
129 print()
130 print(r'#include <iostream>')
131 print()
132 print(r'#include "d3dretrace.hpp"')
133 print()
José Fonseca50c2a142015-01-15 13:10:46 +0000134
135 api = API()
136
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100137 print(r'#include "d3dimports.hpp"')
José Fonseca50c2a142015-01-15 13:10:46 +0000138 api.addModule(ddraw)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100139 print()
140 print('''static d3dretrace::D3DDumper<IDirect3DDevice7> d3d7Dumper;''')
141 print()
José Fonseca50c2a142015-01-15 13:10:46 +0000142
143 retracer = D3DRetracer()
144 retracer.table_name = 'd3dretrace::ddraw_callbacks'
145 retracer.retraceApi(api)
146
147
148if __name__ == '__main__':
149 main()