José Fonseca | 50c2a14 | 2015-01-15 13:10:46 +0000 | [diff] [blame] | 1 | ########################################################################## |
| 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 | |
| 30 | import sys |
| 31 | from dllretrace import DllRetracer as Retracer |
| 32 | from specs.stdapi import API |
| 33 | from specs.d3d import ddraw, HWND |
Jose Fonseca | 410e4f9 | 2015-10-30 23:07:26 +0000 | [diff] [blame] | 34 | from specs.ddraw import DDCREATE_LPGUID |
José Fonseca | 50c2a14 | 2015-01-15 13:10:46 +0000 | [diff] [blame] | 35 | |
| 36 | |
| 37 | class D3DRetracer(Retracer): |
| 38 | |
| 39 | def retraceApi(self, api): |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 40 | print('// Swizzling mapping for lock addresses') |
| 41 | print('static std::map<void *, void *> _maps;') |
| 42 | print() |
José Fonseca | c28e606 | 2015-01-16 12:33:25 +0000 | [diff] [blame] | 43 | # TODO: Keep a table of windows |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 44 | print('static HWND g_hWnd;') |
| 45 | print() |
José Fonseca | 50c2a14 | 2015-01-15 13:10:46 +0000 | [diff] [blame] | 46 | |
| 47 | Retracer.retraceApi(self, api) |
| 48 | |
| 49 | def invokeInterfaceMethod(self, interface, method): |
| 50 | # keep track of the last used device for state dumping |
José Fonseca | ff65c91 | 2015-01-16 09:39:52 +0000 | [diff] [blame] | 51 | if interface.name in ('IDirect3DDevice7',): |
| 52 | if method.name == 'Release': |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 53 | print(r' if (call.ret->toUInt() == 0) {') |
| 54 | print(r' d3d7Dumper.unbindDevice(_this);') |
| 55 | print(r' }') |
José Fonseca | ff65c91 | 2015-01-16 09:39:52 +0000 | [diff] [blame] | 56 | else: |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 57 | print(r' d3d7Dumper.bindDevice(_this);') |
José Fonseca | 50c2a14 | 2015-01-15 13:10:46 +0000 | [diff] [blame] | 58 | |
| 59 | # create windows as neccessary |
| 60 | hWndArg = method.getArgByType(HWND) |
| 61 | if hWndArg is not None: |
José Fonseca | c28e606 | 2015-01-16 12:33:25 +0000 | [diff] [blame] | 62 | # FIXME: Try to guess the window size (e.g., from IDirectDrawSurface7::Blt) |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 63 | 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é Fonseca | 50c2a14 | 2015-01-15 13:10:46 +0000 | [diff] [blame] | 67 | |
| 68 | |
José Fonseca | c28e606 | 2015-01-16 12:33:25 +0000 | [diff] [blame] | 69 | if method.name == 'Lock': |
José Fonseca | 50c2a14 | 2015-01-15 13:10:46 +0000 | [diff] [blame] | 70 | # 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ły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 74 | print(r' dwFlags &= ~DDLOCK_DONOTWAIT;') |
| 75 | print(r' dwFlags |= DDLOCK_WAIT;') |
José Fonseca | 50c2a14 | 2015-01-15 13:10:46 +0000 | [diff] [blame] | 76 | |
| 77 | Retracer.invokeInterfaceMethod(self, interface, method) |
| 78 | |
| 79 | if method.name == 'CreateDevice': |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 80 | print(r' if (FAILED(_result)) {') |
| 81 | print(r' exit(1);') |
| 82 | print(r' }') |
José Fonseca | 50c2a14 | 2015-01-15 13:10:46 +0000 | [diff] [blame] | 83 | |
José Fonseca | ff65c91 | 2015-01-16 09:39:52 +0000 | [diff] [blame] | 84 | # notify frame has been completed |
José Fonseca | 50c2a14 | 2015-01-15 13:10:46 +0000 | [diff] [blame] | 85 | # process events after presents |
José Fonseca | ff65c91 | 2015-01-16 09:39:52 +0000 | [diff] [blame] | 86 | if interface.name == 'IDirectDrawSurface7' and method.name == 'Blt': |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 87 | 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é Fonseca | 50c2a14 | 2015-01-15 13:10:46 +0000 | [diff] [blame] | 93 | |
José Fonseca | c28e606 | 2015-01-16 12:33:25 +0000 | [diff] [blame] | 94 | if method.name == 'Lock': |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 95 | print(' VOID *_pbData = NULL;') |
| 96 | print(' size_t _MappedSize = 0;') |
José Fonseca | c28e606 | 2015-01-16 12:33:25 +0000 | [diff] [blame] | 97 | # FIXME: determine the mapping size |
José Fonseca | 50c2a14 | 2015-01-15 13:10:46 +0000 | [diff] [blame] | 98 | #print ' _getMapInfo(_this, %s, _pbData, _MappedSize);' % ', '.join(method.argNames()[:-1]) |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 99 | print(' if (_MappedSize) {') |
| 100 | print(' _maps[_this] = _pbData;') |
José Fonseca | c28e606 | 2015-01-16 12:33:25 +0000 | [diff] [blame] | 101 | # TODO: check pitches match |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 102 | print(' } else {') |
| 103 | print(' return;') |
| 104 | print(' }') |
José Fonseca | 50c2a14 | 2015-01-15 13:10:46 +0000 | [diff] [blame] | 105 | |
José Fonseca | c28e606 | 2015-01-16 12:33:25 +0000 | [diff] [blame] | 106 | if method.name == 'Unlock': |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 107 | 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é Fonseca | 50c2a14 | 2015-01-15 13:10:46 +0000 | [diff] [blame] | 113 | |
Jose Fonseca | 410e4f9 | 2015-10-30 23:07:26 +0000 | [diff] [blame] | 114 | def extractArg(self, function, arg, arg_type, lvalue, rvalue): |
| 115 | # Handle DDCREATE_* flags |
| 116 | if arg.type is DDCREATE_LPGUID: |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 117 | print(' if (%s.toArray()) {' % rvalue) |
Jose Fonseca | 410e4f9 | 2015-10-30 23:07:26 +0000 | [diff] [blame] | 118 | Retracer.extractArg(self, function, arg, arg_type, lvalue, rvalue) |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 119 | print(' } else {') |
| 120 | print(' %s = static_cast<%s>(%s.toPointer());' % (lvalue, arg_type, rvalue)) |
| 121 | print(' }') |
Jose Fonseca | 410e4f9 | 2015-10-30 23:07:26 +0000 | [diff] [blame] | 122 | return |
| 123 | |
| 124 | Retracer.extractArg(self, function, arg, arg_type, lvalue, rvalue) |
| 125 | |
José Fonseca | 50c2a14 | 2015-01-15 13:10:46 +0000 | [diff] [blame] | 126 | |
| 127 | def main(): |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 128 | print(r'#include <string.h>') |
| 129 | print() |
| 130 | print(r'#include <iostream>') |
| 131 | print() |
| 132 | print(r'#include "d3dretrace.hpp"') |
| 133 | print() |
José Fonseca | 50c2a14 | 2015-01-15 13:10:46 +0000 | [diff] [blame] | 134 | |
| 135 | api = API() |
| 136 | |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 137 | print(r'#include "d3dimports.hpp"') |
José Fonseca | 50c2a14 | 2015-01-15 13:10:46 +0000 | [diff] [blame] | 138 | api.addModule(ddraw) |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 139 | print() |
| 140 | print('''static d3dretrace::D3DDumper<IDirect3DDevice7> d3d7Dumper;''') |
| 141 | print() |
José Fonseca | 50c2a14 | 2015-01-15 13:10:46 +0000 | [diff] [blame] | 142 | |
| 143 | retracer = D3DRetracer() |
| 144 | retracer.table_name = 'd3dretrace::ddraw_callbacks' |
| 145 | retracer.retraceApi(api) |
| 146 | |
| 147 | |
| 148 | if __name__ == '__main__': |
| 149 | main() |