├── .clang-format ├── .github └── workflows │ └── artifact.yml ├── .gitignore ├── .gitmodules ├── LICENSE ├── README.md ├── docs ├── BUILDING.md ├── PROFILING.md ├── TROUBLESHOOTING.md └── latencyflex.cfg ├── latencyflex.h ├── layer ├── latencyflex_layer.cpp ├── latencyflex_layer.h ├── latencyflex_perfetto.cpp ├── latencyflex_ue4_hook.cpp ├── layer.json.in ├── meson.build ├── meson_options.txt ├── unity │ ├── .gitignore │ ├── LatencyFleX.csproj │ ├── NuGet.Config │ └── Plugin.cs ├── version.h.in └── wine │ ├── builtin.cpp │ ├── cross-mingw64.txt │ ├── cross-wine64.txt │ ├── latencyflex_layer.spec │ ├── latencyflex_wine.spec │ ├── meson.build │ ├── meson_options.txt │ └── unixlib.cpp └── performance.png /.clang-format: -------------------------------------------------------------------------------- 1 | BasedOnStyle: LLVM 2 | ColumnLimit: 100 -------------------------------------------------------------------------------- /.github/workflows/artifact.yml: -------------------------------------------------------------------------------- 1 | name: Artifacts (Package) 2 | 3 | on: [push, pull_request, workflow_dispatch] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-20.04 8 | 9 | steps: 10 | - name: Checkout code 11 | uses: actions/checkout@v2 12 | with: 13 | submodules: recursive 14 | 15 | - name: Prepare Artifact Git Info 16 | shell: bash 17 | run: | 18 | echo "##[set-output name=branch;]${GITHUB_REF#refs/heads/}" 19 | ARTIFACT_NAME="commit-$(git rev-parse --short "$GITHUB_SHA")" 20 | if [ ${{ github.event_name == 'pull_request' }} ]; then 21 | echo "##[set-output name=short-sha;]$(git rev-parse --short "${{ github.event.pull_request.head.sha }}")" 22 | if [ ! -z "${{ github.event.pull_request.number }}" ]; then 23 | ARTIFACT_NAME="pr-${{ github.event.pull_request.number }}-commit-$(git rev-parse --short "${{ github.event.pull_request.head.sha }}")" 24 | fi 25 | else 26 | echo "##[set-output name=short-sha;]$(git rev-parse --short "$GITHUB_SHA")" 27 | fi 28 | echo "##[set-output name=artifact-metadata;]${ARTIFACT_NAME}" 29 | id: git-vars 30 | 31 | - uses: actions/setup-python@v2 32 | - uses: actions/setup-dotnet@v1 33 | with: 34 | dotnet-version: 6.x.x 35 | 36 | - run: | 37 | sudo apt-add-repository -y ppa:ondrej/php 38 | sudo apt-get install ppa-purge 39 | sudo ppa-purge -y ppa:ondrej/php 40 | sudo dpkg --add-architecture i386 41 | wget -qO - https://dl.winehq.org/wine-builds/winehq.key | sudo apt-key add - 42 | sudo apt-add-repository "deb https://dl.winehq.org/wine-builds/ubuntu focal main" 43 | wget -qO - https://packages.lunarg.com/lunarg-signing-key-pub.asc | sudo apt-key add - 44 | sudo wget -qO /etc/apt/sources.list.d/lunarg-vulkan-focal.list https://packages.lunarg.com/vulkan/lunarg-vulkan-focal.list 45 | sudo apt update 46 | sudo apt install --no-install-recommends -y ninja-build cmake vulkan-sdk winehq-staging wine-staging wine-staging-dev mingw-w64 47 | pip install meson 48 | 49 | - run: | 50 | VERSION=$(git describe --always --tags) 51 | OUTDIR="${PWD}/dist/latencyflex-${VERSION}" 52 | echo "VERSION=${VERSION}" >> $GITHUB_ENV 53 | echo "OUTDIR=${OUTDIR}" >> $GITHUB_ENV 54 | mkdir -p $OUTDIR 55 | 56 | - run: | 57 | cd layer 58 | meson build -Dprefix=/usr 59 | ninja -C build 60 | mkdir -p "${OUTDIR}/layer" 61 | DESTDIR="${OUTDIR}/layer" meson install -C build --skip-subprojects 62 | 63 | - run: | 64 | export LIBRARY_PATH="${OUTDIR}/layer/usr/lib/x86_64-linux-gnu" 65 | cd layer/wine 66 | meson build-wine64 -Dprefix=/usr --cross cross-wine64.txt 67 | ninja -C build-wine64 68 | meson build-mingw64 -Dprefix=/usr --cross cross-mingw64.txt 69 | ninja -C build-mingw64 70 | mkdir -p "${OUTDIR}/wine" 71 | DESTDIR="${OUTDIR}/wine" meson install -C build-wine64 --skip-subprojects 72 | DESTDIR="${OUTDIR}/wine" meson install -C build-mingw64 --skip-subprojects 73 | 74 | - run: | 75 | cd layer/unity 76 | OUTDIR_=$OUTDIR 77 | export OUTDIR="${OUTDIR_}/unity/mono-2018.1" 78 | dotnet build --configuration Release -p:UnityTarget=2018.1 -p:UnityRuntime=Mono LatencyFleX.csproj 79 | export OUTDIR="${OUTDIR_}/unity/mono-2019.3" 80 | dotnet build --configuration Release -p:UnityTarget=2019.3 -p:UnityRuntime=Mono LatencyFleX.csproj 81 | export OUTDIR="${OUTDIR_}/unity/il2cpp-2019.3" 82 | dotnet build --configuration Release -p:UnityTarget=2019.3 -p:UnityRuntime=IL2CPP LatencyFleX.csproj 83 | 84 | - run: | 85 | tar Jcvf "latencyflex-${VERSION}.tar.xz" -C dist latencyflex-${VERSION} 86 | 87 | - name: Upload artifact 88 | uses: actions/upload-artifact@v2 89 | continue-on-error: true 90 | with: 91 | name: latencyflex-${{steps.git-vars.outputs.artifact-metadata}} 92 | path: ${{env.OUTDIR}} 93 | retention-days: 30 94 | 95 | - name: Create Release 96 | uses: softprops/action-gh-release@v1 97 | if: startsWith(github.ref, 'refs/tags/') 98 | with: 99 | files: latencyflex-${{ env.VERSION }}.tar.xz 100 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | build-wine64 3 | build-mingw64 -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "layer/funchook"] 2 | path = layer/subprojects/funchook 3 | url = https://github.com/kubo/funchook.git 4 | [submodule "layer/subprojects/perfetto"] 5 | path = layer/subprojects/perfetto 6 | url = https://android.googlesource.com/platform/external/perfetto 7 | [submodule "layer/unity/unhollowed"] 8 | path = layer/unity/unhollowed 9 | url = https://github.com/ishitatsuyuki/unhollowed-assemblies.git 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LatencyFleX (LFX) 2 | 3 | Vendor agnostic latency reduction middleware. An alternative to NVIDIA Reflex. 4 | 5 | ![LatencyFleX brings competitive advantage with up to 10ms latency reduction](performance.png) 6 | 7 | ## Why LatencyFleX? 8 | 9 | To get an idea why queuing happens in video games and why it causes increased latency, watch this [video](https://www.youtube.com/watch?v=7CKnJ5ujL_Q). 10 | 11 | To learn more about LatencyFleX's internals, check out [the blog post](https://ishitatsuyuki.github.io/post/latencyflex/). 12 | 13 | ## Limitations 14 | 15 | - LatencyFleX current does not provide any benefits when VSync is enabled. 16 | This is blocked on [presentation timing](https://github.com/KhronosGroup/Vulkan-Docs/pull/1364) support. 17 | - LatencyFleX introduces jitter in frame time as a part of its algorithm, which results in microstutters. 18 | Though, most games tend to have a larger frame time fluctuation already, so this is likely unperceivable. 19 | 20 | ## Known issues 21 | 22 | - Minor stutters might happen. 23 | 24 | **Tip:** If you are using AMD GPUs, try modifying the power profile to reduce power management induced stutters: https://gitlab.freedesktop.org/drm/amd/-/issues/1500#note_1228253 25 | - GPU utilization will be lower (around 95% when GPU bound). 26 | - It might take one second or two to adapt to large frame rate increases (e.g. if the game sets a background frame limit). 27 | 28 | ## Building from source 29 | 30 | See [docs/BUILDING.md](./docs/BUILDING.md) 31 | 32 | ## Usage 33 | 34 | For now, LatencyFleX can be used on Linux through one of the following injection method. Game engine integration is planned. 35 | 36 | ### Running games with LatencyFleX 37 | 38 | **Warning:** Be careful when using LatencyFleX with games having anti-cheat: 39 | 40 | - Direct hooking (UE4 hook) can trip the game's integrity check and directly get you banned. 41 | - Proton NVAPI integration is relatively safe, but I am not responsible for any bans issued due to LatencyFleX. 42 | 43 | Please do it at your own risk. 44 | 45 | Tested games: 46 | 47 | | Game | Support | Method | 48 | |--------------------------|---------|-----------------| 49 | | Apex Legends [^1] | ❌ | Proton NVAPI | 50 | | Titanfall 2 w/ Northstar | ✅ | Proton ([Native](https://r2northstar.gitbook.io/r2northstar-wiki/using-northstar/playing-on-linux))| 51 | | Overwatch [^2] | ✅ | Proton NVAPI | 52 | | Splitgate [^3] | ❌ | N/A | 53 | | Ghostrunner | ✅ | Proton NVAPI | 54 | | God of War | ✅ | Proton NVAPI | 55 | | Spider-Man Remastered | ✅ | Proton NVAPI | 56 | 57 | [^1]: [Game Does not work on Linux anymore](https://www.theverge.com/2024/10/31/24284644/apex-legends-loses-linux-steam-deck-support-anti-cheat). 58 | [^2]: Uses anti-cheat. Use at your own risk. 59 | [^3]: Game was previously supported with manual UE4 Linux hook. This is no longer possible in an easy way as the game does not ship with symbols now. 60 | 61 | Game supported but not in list? File a PR to update the table. 62 | 63 | #### Proton NVAPI (for games that already have NVIDIA Reflex integration) 64 | 65 | 1. [Install](#installation) the Vulkan layer, wine extension and DXVK-NVAPI with LFX support. 66 | 2. Put the following in `dxvk.conf` [^2]. If you haven't created one, create it next to the game executable. 67 | If there are multiple executables, try copying and putting `dxvk.conf` next to every executable. 68 | ```ini 69 | dxgi.nvapiHack = False 70 | dxgi.customVendorId = 10de # If running on non-NVIDIA GPU 71 | ``` 72 | 73 | 3. Launch with the following environment variables: 74 | ```shell 75 | PROTON_ENABLE_NVAPI=1 DXVK_NVAPI_DRIVER_VERSION=49729 DXVK_NVAPI_ALLOW_OTHER_DRIVERS=1 LFX=1 %command% 76 | ``` 77 | 4. Don't forget to enable **Reflex Low-Latency** in-game. 78 | 79 | Not working? See [troubleshooting guide](./docs/TROUBLESHOOTING.md) 80 | 81 | [^2]: A previous version of this document claimed that this is DX11 only. This is not true and it's required for DX12 too 82 | as they use DXVK's DXGI implementation. 83 | 84 | #### UE4 Hook 85 | 86 | Supported platforms: Linux (see note) 87 | 88 | **Note:** for now, the UE4 hook only supports Linux UE4 builds with PIE disabled. 89 | 90 | 1. [Install](#installation) the Vulkan layer. 91 | 92 | 2. Obtain an offset to `FEngineLoop::Tick`. If the game ships with debug symbols, the 93 | offset can be obtained with the command: 94 | ```shell 95 | readelf -Ws PortalWars/Binaries/Linux/PortalWars-Linux-Shipping.debug | c++filt | grep FEngineLoop::Tick 96 | ``` 97 | Find the line corresponding to the actual function (other entries are for types used in the function and unrelated): 98 | ``` 99 | 268: 00000000026698e0 9876 FUNC LOCAL HIDDEN 15 FEngineLoop::Tick() 100 | ``` 101 | Here `26698e0` is the offset we need. We will call it `` below. 102 | 3. Modify the launch command-line as follows. 103 | ```shell 104 | LFX=1 LFX_UE4_HOOK=0x %command% 105 | ``` 106 | 107 | #### Unity Mod/Hook 108 | 109 | Supported platforms: Proton, Linux 110 | 111 | 1. [Install](#installation) the Vulkan layer. Also install the Wine extension if the game runs on Wine/Proton. 112 | 2. Install [BepInEx Bleeding Edge](https://docs.bepinex.dev/master/articles/user_guide/installation/index.html) to the game directory. 113 | 3. Run the game once to generate BepInEx directory structure, config files and startup log. 114 | Obtain the Unity version from the first line of `BepInEx/LogOutput.log`. 115 | 4. Drop `unity/-/LatencyFleX.dll` (from [release artifacts](https://github.com/ishitatsuyuki/LatencyFleX/releases)) 116 | into `BepInEx/plugins`. `` is `mono` or `il2cpp`. `` is: 117 | - `2018.1` for any version higher or equal to 2018.1 (This is currently unsupported for IL2CPP) 118 | - `2019.3` for any version higher or equal to 2019.3 119 | - Older versions (5.x, 4.x) are unsupported. 120 | 6. Use the following launch command-line. 121 | ```shell 122 | LFX=1 %command% -force-vulkan # for native 123 | WINEDLLOVERRIDES="winhttp=n,b" LFX=1 %command% # for Proton 124 | ``` 125 | 126 | ## Installation 127 | 128 | ### LatencyFleX Vulkan layer (essential) 129 | 130 | For Debian-like distros, copy the following files from [release artifacts](https://github.com/ishitatsuyuki/LatencyFleX/releases) to your root filesystem. 131 | 132 | ``` 133 | /usr/lib/x86_64-linux-gnu/liblatencyflex_layer.so 134 | /usr/share/vulkan/implicit_layer.d/latencyflex.json 135 | ``` 136 | 137 | For Arch-like distros, you need to copy `/usr/lib/x86_64-linux-gnu/liblatencyflex_layer.so -> /usr/lib/liblatencyflex_layer.so` 138 | and additionally update the path specified in `/usr/share/vulkan/implicit_layer.d/latencyflex.json`. 139 | 140 | ### LatencyFleX Wine extensions (required for Proton Reflex integration) 141 | 142 | **Note:** The Wine extensions are migrated to a new Wine API in this version. Wine 7.0 or later is recommended, older versions might be unsupported. 143 | 144 | 1. Copy the following files from [release artifacts](https://github.com/ishitatsuyuki/LatencyFleX/releases) to your Wine installation location. 145 | 146 | For Wine 7.x: change `/usr/lib/wine` to wherever Wine/Proton is installed. 147 | For Proton and certain distros, you also need to change `lib` to `lib64`. Copy the following files. 148 | 149 | ``` 150 | /usr/lib/wine/x86_64-unix/latencyflex_layer.so 151 | /usr/lib/wine/x86_64-windows/latencyflex_layer.dll 152 | /usr/lib/wine/x86_64-windows/latencyflex_wine.dll 153 | ``` 154 | 155 | For Wine <= 6.x: copy the files as follows. 156 | 157 | ``` 158 | /usr/lib/wine/x86_64-unix/latencyflex_layer.dll.so -> lib/wine/latencyflex_layer.so 159 | /usr/lib/wine/x86_64-windows/latencyflex_layer.dll -> lib/wine/fakedlls/latencyflex_layer.dll 160 | /usr/lib/wine/x86_64-windows/latencyflex_wine.dll -> lib/wine/fakedlls/latencyflex_wine.dll 161 | ``` 162 | 2. Create symbolic links to `latencyflex_layer.dll` and `latencyflex_wine.dll`, inside `drive_c/windows/system32/` under your prefix. 163 | 164 | (Alternatively, copy these files. The drawback is that you need to copy these files on every LFX update.) 165 | 166 | Proton prefixes can be found under `~/.local/share/Steam/steamapps/compatdata//pfx`. 167 | 168 | ### DXVK-NVAPI with LatencyFleX integration (required for Proton Reflex integration) 169 | 170 | Obtain binaries from [GitHub Releases](https://github.com/jp7677/dxvk-nvapi/releases). Minimum version with LatencyFlex integration is 0.5.3. 171 | 172 | For Proton, copy `nvapi64.dll` into `dist/lib64/wine/nvapi`. 173 | 174 | For other Wine installations, see [DXVK-NVAPI documentation](https://github.com/jp7677/dxvk-nvapi#how-to-use). 175 | 176 | ### MangoHud with metric support (optional) 177 | 178 | Obtain binaries from [GitHub Actions](https://github.com/ishitatsuyuki/MangoHud/actions?query=branch%3Acustom-metrics) 179 | and install it to your system. 180 | 181 | Put the following line in `MangoHud.conf` to have real-time latency metrics: 182 | 183 | ``` 184 | graphs=custom_Latency 185 | ``` 186 | -------------------------------------------------------------------------------- /docs/BUILDING.md: -------------------------------------------------------------------------------- 1 | ## Building from source 2 | 3 | **Note:** Fetch submodules recursively (with `git submodule update --init --recursive`) before build. 4 | 5 | The layer (`layer/`) depends on CMake, Meson and the Vulkan SDK. 6 | 7 | Build and install with: 8 | ```shell 9 | cd layer 10 | meson build 11 | ninja -C build 12 | meson install -C build --skip-subprojects 13 | ``` 14 | 15 | --- 16 | 17 | The Wine extension (`layer/wine/`) additionally depends on a Wine installation and a MinGW toolchain. 18 | 19 | Build with: 20 | 21 | ```shell 22 | cd layer/wine 23 | export LIBRARY_PATH="$PWD/../build/" # Required if the layer has not been installed globally 24 | meson build-wine64 --cross cross-wine64.txt 25 | ninja -C build-wine64 26 | meson build-mingw64 --cross cross-mingw64.txt 27 | ninja -C build-mingw64 28 | ``` 29 | 30 | See install instructions for the locations to copy the files to. 31 | 32 | --- 33 | 34 | The Unity mod can be built with .NET Core SDK. 35 | 36 | ```shell 37 | cd layer/unity 38 | dotnet build --configuration Release -p:UnityTarget= -p:UnityRuntime= LatencyFleX.csproj 39 | ``` 40 | 41 | `` is either `2018.1` or `2019.3`. `` is `Mono` or `IL2CPP`. 42 | 43 | The combination `2018.1` and `IL2CPP` is currently unsupported due to lack of unhollowed DLLs. If you own such games, 44 | you can overwrite the DLLs in `unhollowed` with the ones generated by your BepInEx installation to build this combination. -------------------------------------------------------------------------------- /docs/PROFILING.md: -------------------------------------------------------------------------------- 1 | ## Profiling 2 | 3 | [Perfetto](https://perfetto.dev/) can be used to gather a detailed picture of execution. 4 | 5 | The steps to use perfetto for profiling are: 6 | 1. Rebuild and install the layer with `meson build -Dperfetto=true`. (Specify `--reconfigure` when doing this on an existing build directory.) 7 | 2. Build perfetto from sources available at layer/subprojects/perfetto following 8 | [this guide](https://perfetto.dev/docs/quickstart/linux-tracing). 9 | 3. `cd layer/subprojects/perfetto` and run the helper script. latencyflex.cfg is available in this docs directory. 10 | ```shell 11 | tools/tmux -c path/to/latencyflex.cfg -C out/linux -n 12 | ``` 13 | 4. Launch your game. When you are ready to capture, switch to the bottom tmux pane and press enter to run the supplied 14 | perfetto CLI invocation. 15 | 16 | The capture lasts 60 seconds by default, but you can interrupt as you want. You can also modify the command line to 17 | sleep for a delay before capturing. 18 | 19 | When capturing multiple sessions, make sure you change the output file names specified in `-o` of the perfetto CLI 20 | invocation. 21 | 5. Go to https://ui.perfetto.dev and view the trace by selecting "Open Trace File" and navigating into `/tmp/perfetto.XXXXXX`. -------------------------------------------------------------------------------- /docs/TROUBLESHOOTING.md: -------------------------------------------------------------------------------- 1 | ## Troubleshooting 2 | 3 | ### Proton 4 | 5 | If the Reflex option is not available in-game, it means that the installation is not set up correctly. Make sure that you: 6 | 7 | - Copied/symlinked `latencyflex_wine.dll` and `latencyflex_layer.dll` to **both** `lib[64]/wine/x86_64-windows/` and `pfx/drive_c/windows/system32`. 8 | - Copied `latencyflex_layer.so` to `lib[64]/wine/x86_64-unix`. (This is different from `/usr/lib/liblatencyflex_layer.so`) 9 | - Have a version of DXVK-NVAPI supporting LFX or have updated it to a supported version. 10 | 11 | #### Getting Logs 12 | 13 | Logs will provide helpful insights about what went wrong with the installation. Set `PROTON_LOG=1` and `DXVK_NVAPI_LOG_LEVEL=info` in your launch options. 14 | 15 | The log will be created under your home directory as `proton-.log`. 16 | 17 | #### Checking if DXVK-NVAPI is initialized 18 | 19 | Check the log for these messages: 20 | 21 | ``` 22 | DXVK_NVAPI_ALLOW_OTHER_DRIVERS is set, reporting also GPUs with non-NVIDIA proprietary driver. 23 | NvAPI Device: AMD RADV NAVI10 (21.99.99) 24 | NvAPI Output: \\.\DISPLAY1 25 | DXVK_NVAPI_DRIVER_VERSION is set to '49729', reporting driver version 497.29. 26 | NvAPI_Initialize: OK 27 | ``` 28 | 29 | If you're not seeing this, re-check if DXVK-NVAPI is enabled and you have overrided the vendor ID and disabled nvapiHack in `dxvk.conf`. 30 | 31 | #### Checking if the Wine bridge is loaded 32 | 33 | Check the log for messages like this: 34 | 35 | ``` 36 | trace:loaddll:build_module Loaded L"C:\\windows\\system32\\latencyflex_layer.dll" at
: builtin 37 | ``` 38 | 39 | If you can't find it, recheck if you have: 40 | - Set up a supported DXVK-NVAPI version 41 | - Put `latencyflex_layer.dll` at **both** `lib[64]/wine/x86_64-windows/` and `pfx/drive_c/windows/system32` 42 | 43 | #### Checking if the Wine bridge successfully initialized 44 | 45 | If there's a log entry like this: 46 | 47 | ``` 48 | Loading latencyflex_layer.dll failed with error code: 1114 49 | ``` 50 | 51 | It's typically accompanied by a failure reason: 52 | 53 | ``` 54 | ../builtin.cpp: Querying MemoryWineUnixFuncs failed c0000135 55 | ../builtin.cpp: Look for library loading errors in the log and check if liblatencyflex_layer.so is installed on your system. 56 | ``` 57 | 58 | Recheck if you have put **both** `/usr/lib/liblatencyflex_layer.so` and `lib[64]/wine/x86_64-unix/latencyflex_layer.so` correctly. -------------------------------------------------------------------------------- /docs/latencyflex.cfg: -------------------------------------------------------------------------------- 1 | buffers { 2 | size_kb: 20480 3 | } 4 | 5 | write_into_file: true 6 | file_write_period_ms: 1000 7 | duration_ms: 60000 8 | 9 | data_sources { 10 | config { 11 | name: "track_event" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /latencyflex.h: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Tatsuyuki Ishi 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef LATENCYFLEX_H 16 | #define LATENCYFLEX_H 17 | 18 | #ifdef LATENCYFLEX_HAVE_PERFETTO 19 | #include 20 | PERFETTO_DEFINE_CATEGORIES( 21 | perfetto::Category("latencyflex").SetDescription("LatencyFleX latency and throughput metrics")); 22 | #else 23 | #define TRACE_COUNTER(...) 24 | #define TRACE_EVENT_BEGIN(...) 25 | #define TRACE_EVENT_END(...) 26 | #endif 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | namespace lfx { 37 | namespace internal { 38 | // An exponentially weighted moving average estimator. 39 | class EwmaEstimator { 40 | public: 41 | // `alpha`: Smoothing factor. Larger values means less smoothing, resulting in 42 | // a bumpy but quick response. 43 | // `full_weight`: Set to true to disable weight correction for initial 44 | // samples. The estimator will start with a value of 0 weighted 45 | // at 100% instead. 46 | EwmaEstimator(double alpha, bool full_weight = false) 47 | : alpha_(alpha), current_weight_(full_weight ? 1.0 : 0.0) {} 48 | 49 | // Update the estimate with `value`. `value` must not be negative. If a 50 | // negative exponent is used, then `value` must not be too small or the 51 | // internal accumulator will overflow. 52 | void update(double value) { 53 | current_ = (1 - alpha_) * current_ + alpha_ * value; 54 | current_weight_ = (1 - alpha_) * current_weight_ + alpha_; 55 | } 56 | 57 | double get() const { 58 | if (current_weight_ == 0) { 59 | return 0; 60 | } 61 | return current_ / current_weight_; 62 | } 63 | 64 | private: 65 | double alpha_; 66 | double current_ = 0; 67 | double current_weight_; 68 | }; 69 | } // namespace internal 70 | 71 | enum Phases { kUp = 0, kDown, kNumPhases }; 72 | 73 | // Tracks and computes frame time, latency and the desired sleep time before 74 | // next tick. All time is in nanoseconds. The clock domain doesn't matter as 75 | // long as it's a single consistent clock. 76 | // 77 | // Access must be externally synchronized. 78 | class LatencyFleX { 79 | public: 80 | LatencyFleX() : latency_(0.3), inv_throughtput_(0.3), proj_correction_(0.5, true) { 81 | std::fill(std::begin(frame_begin_ids_), std::end(frame_begin_ids_), UINT64_MAX); 82 | } 83 | 84 | // Get the desired wake-up time. Sleep until this time, then call `BeginFrame()`. This function 85 | // must be called *exactly once* before each call to `BeginFrame()`. Calling this the second time 86 | // with the same `frame_id` will corrupt the internal time tracking. 87 | // 88 | // If a wait target cannot be determined due to lack of data, then `0` is 89 | // returned. 90 | uint64_t GetWaitTarget(uint64_t frame_id) { 91 | if (prev_frame_end_id_ != UINT64_MAX) { 92 | size_t phase = frame_id % kNumPhases; 93 | double invtpt = inv_throughtput_.get(); 94 | int64_t comp_to_apply = 0; 95 | if (frame_end_projection_base_ == UINT64_MAX) { 96 | frame_end_projection_base_ = prev_frame_end_ts_; 97 | } else { 98 | // The prediction error is equal to (actual latency) - (expected latency). 99 | // As we adapt our latency estimator to the actual latency values, this 100 | // will eventually converge as long as we are not constantly overpacing, 101 | // building a queue at a faster pace than the estimator can adapt. 102 | 103 | // In the section below, we attempt to apply additional compensation in 104 | // the case of delay increase, to prevent extra queuing as much as possible. 105 | int64_t prediction_error = 106 | (int64_t)prev_frame_end_ts_ - 107 | (int64_t)(frame_end_projection_base_ + 108 | frame_end_projected_ts_[prev_frame_end_id_ % kMaxInflightFrames]); 109 | TRACE_COUNTER("latencyflex", "Prediction error", prediction_error); 110 | int64_t prev_comp_applied = comp_applied_[prev_frame_end_id_ % kMaxInflightFrames]; 111 | // We need to limit the compensation to delay increase, or otherwise we would cancel out the 112 | // regular delay decrease from our pacing. To achieve this, we treat any early prediction as 113 | // having prediction error of zero. 114 | // 115 | // We also want to cancel out the counter-reaction from our previous compensation, so what 116 | // we essentially want here is `prediction_error_ - prev_prediction_error_ + 117 | // prev_comp_applied`. But since we clamp prediction_error_ and prev_prediction_error_, 118 | // the naive approach of adding prev_comp_applied directly would have a bias toward 119 | // overcompensation. Consider the example below where we're pacing at the correct (100%) 120 | // rate but things arrives late due to reason that are *not* queuing (noise): 121 | // 5ms late, 5ms late, ... (a period longer than our latency) ... , 0ms 122 | // We would compensate -5ms on the first frame, bringing the prediction error to 0. But when 123 | // the 0ms frame arrives, the prediction error becomes -5ms due to our overcompensation. 124 | // Due to its negativity, we don't recompensate for this decrease: this is the bias. 125 | // 126 | // The solution here is to include prev_comp_applied as a part of clamping equation, which 127 | // allows it to also undercompensate when it makes sense. It seems to do a great job on 128 | // preventing prediction error from getting stuck in a state that is drift away. 129 | proj_correction_.update( 130 | std::max(INT64_C(0), prediction_error) - 131 | std::max(INT64_C(0), prev_prediction_error_ - prev_comp_applied)); 132 | prev_prediction_error_ = prediction_error; 133 | // Try to cancel out any unintended delay happened to previous frame start. This is 134 | // primarily meant for cases where a frame time spike happens and we get backpressured 135 | // on the main thread. prev_forced_correction_ will stay high until our prediction catches 136 | // up, canceling out any excessive correction we might end up doing. 137 | comp_to_apply = std::round(proj_correction_.get()); 138 | comp_applied_[frame_id % kMaxInflightFrames] = comp_to_apply; 139 | TRACE_COUNTER("latencyflex", "Delay Compensation", comp_to_apply); 140 | } 141 | 142 | // The target wakeup time. 143 | uint64_t target = 144 | (int64_t)frame_end_projection_base_ + 145 | (int64_t)frame_end_projected_ts_[prev_frame_begin_id_ % kMaxInflightFrames] + 146 | comp_to_apply + 147 | (int64_t)std::round((((int64_t)frame_id - (int64_t)prev_frame_begin_id_) + 148 | 1 / (phase == kUp ? up_factor_ : 1) - 1) * 149 | invtpt / down_factor_ - 150 | latency_.get()); 151 | // The projection is something close to the predicted frame end time, but it is always paced 152 | // at down_factor * throughput, which prevents delay compensation from kicking in until it's 153 | // actually necessary (i.e. we're overpacing). 154 | uint64_t new_projection = 155 | (int64_t)frame_end_projected_ts_[prev_frame_begin_id_ % kMaxInflightFrames] + 156 | comp_to_apply + 157 | (int64_t)std::round(((int64_t)frame_id - (int64_t)prev_frame_begin_id_) * invtpt / 158 | down_factor_); 159 | frame_end_projected_ts_[frame_id % kMaxInflightFrames] = new_projection; 160 | TRACE_EVENT_BEGIN( 161 | "latencyflex", "projection", 162 | perfetto::Track(track_base_ + frame_id % kMaxInflightFrames + kMaxInflightFrames), 163 | target); 164 | TRACE_EVENT_END( 165 | "latencyflex", 166 | perfetto::Track(track_base_ + frame_id % kMaxInflightFrames + kMaxInflightFrames), 167 | frame_end_projection_base_ + new_projection); 168 | return target; 169 | } else { 170 | return 0; 171 | } 172 | } 173 | 174 | // Begin the frame. Called on the main/simulation thread. 175 | // 176 | // This call must be preceded with a call to `GetWaitTarget()`. 177 | // 178 | // `target` should be the timestamp returned by `GetWaitTarget()`. 179 | // `timestamp` should be calculated as follows: 180 | // - If a sleep is not performed (because the wait target has already been 181 | // passed), then pass the current time. 182 | // - If a sleep is performed (wait target was not in the past), then pass the 183 | // wait target as-is. This allows compensating for any latency incurred by 184 | // the OS for waking up the process. 185 | void BeginFrame(uint64_t frame_id, uint64_t target, uint64_t timestamp) { 186 | TRACE_EVENT_BEGIN("latencyflex", "frame", 187 | perfetto::Track(track_base_ + frame_id % kMaxInflightFrames), timestamp); 188 | frame_begin_ids_[frame_id % kMaxInflightFrames] = frame_id; 189 | frame_begin_ts_[frame_id % kMaxInflightFrames] = timestamp; 190 | prev_frame_begin_id_ = frame_id; 191 | if (target != 0) { 192 | int64_t forced_correction = timestamp - target; 193 | frame_end_projected_ts_[frame_id % kMaxInflightFrames] += forced_correction; 194 | comp_applied_[frame_id % kMaxInflightFrames] += forced_correction; 195 | prev_prediction_error_ += forced_correction; 196 | } 197 | } 198 | 199 | // End the frame. Called from a rendering-related thread. 200 | // 201 | // The timestamp should be obtained in one of the following ways: 202 | // - Run a thread dedicated to wait for command buffer completion fences. 203 | // Capture the timestamp on CPU when the fence is signaled. 204 | // - Capture a GPU timestamp when frame ends, then convert it into a clock 205 | // domain on CPU (known as "timestamp calibration"). 206 | // 207 | // If `latency` and `frame_time` are not null, then the latency and the frame 208 | // time are returned respectively, or UINT64_MAX is returned if measurement is 209 | // unavailable. 210 | void EndFrame(uint64_t frame_id, uint64_t timestamp, uint64_t *latency, uint64_t *frame_time) { 211 | size_t phase = frame_id % kNumPhases; 212 | int64_t latency_val = -1; 213 | int64_t frame_time_val = -1; 214 | if (frame_begin_ids_[frame_id % kMaxInflightFrames] == frame_id) { 215 | frame_begin_ids_[frame_id % kMaxInflightFrames] = UINT64_MAX; 216 | 217 | if (frame_time && prev_frame_end_id_ != UINT64_MAX) 218 | *frame_time = timestamp - prev_frame_real_end_ts_; 219 | prev_frame_real_end_ts_ = timestamp; 220 | timestamp = std::max(timestamp, prev_frame_end_ts_ + target_frame_time); 221 | auto frame_start = frame_begin_ts_[frame_id % kMaxInflightFrames]; 222 | latency_val = (int64_t)timestamp - (int64_t)frame_start; 223 | if (phase == kDown) { 224 | latency_.update(latency_val); 225 | } 226 | if (latency) 227 | *latency = latency_val; 228 | TRACE_COUNTER("latencyflex", "Latency", latency_val); 229 | TRACE_COUNTER("latencyflex", "Latency (Estimate)", latency_.get()); 230 | if (prev_frame_end_id_ != UINT64_MAX) { 231 | if (frame_id > prev_frame_end_id_) { 232 | auto frames_elapsed = frame_id - prev_frame_end_id_; 233 | frame_time_val = 234 | ((int64_t)timestamp - (int64_t)prev_frame_end_ts_) / (int64_t)frames_elapsed; 235 | frame_time_val = std::clamp(frame_time_val, INT64_C(1000000), INT64_C(50000000)); 236 | if (phase == kUp) { 237 | inv_throughtput_.update(frame_time_val); 238 | } 239 | TRACE_COUNTER("latencyflex", "Frame Time", frame_time_val); 240 | TRACE_COUNTER("latencyflex", "Frame Time (Estimate)", inv_throughtput_.get()); 241 | } 242 | } 243 | prev_frame_end_id_ = frame_id; 244 | prev_frame_end_ts_ = timestamp; 245 | } 246 | if (frame_time) 247 | *frame_time = frame_time_val; 248 | TRACE_EVENT_END("latencyflex", perfetto::Track(track_base_ + frame_id % kMaxInflightFrames), 249 | timestamp); 250 | } 251 | 252 | void Reset() { 253 | auto new_instance = LatencyFleX(); 254 | #ifdef LATENCYFLEX_HAVE_PERFETTO 255 | new_instance.track_base_ = track_base_ + 2 * kMaxInflightFrames; 256 | #endif 257 | new_instance.target_frame_time = target_frame_time; 258 | *this = new_instance; 259 | } 260 | 261 | uint64_t target_frame_time = 0; 262 | 263 | private: 264 | static const std::size_t kMaxInflightFrames = 16; 265 | 266 | uint64_t frame_begin_ts_[kMaxInflightFrames] = {}; 267 | uint64_t frame_begin_ids_[kMaxInflightFrames]; 268 | uint64_t frame_end_projected_ts_[kMaxInflightFrames] = {}; 269 | uint64_t frame_end_projection_base_ = UINT64_MAX; 270 | int64_t comp_applied_[kMaxInflightFrames] = {}; 271 | uint64_t prev_frame_begin_id_ = UINT64_MAX; 272 | double up_factor_ = 1.10; 273 | double down_factor_ = 0.985; 274 | int64_t prev_prediction_error_ = 0; 275 | uint64_t prev_frame_end_id_ = UINT64_MAX; 276 | uint64_t prev_frame_end_ts_ = 0; 277 | uint64_t prev_frame_real_end_ts_ = 0; 278 | internal::EwmaEstimator latency_; 279 | internal::EwmaEstimator inv_throughtput_; 280 | internal::EwmaEstimator proj_correction_; 281 | 282 | #ifdef LATENCYFLEX_HAVE_PERFETTO 283 | uint64_t track_base_ = 0; 284 | #endif 285 | }; 286 | } // namespace lfx 287 | 288 | #endif // LATENCYFLEX_H 289 | -------------------------------------------------------------------------------- /layer/latencyflex_layer.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Tatsuyuki Ishi 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "latencyflex_layer.h" 16 | #include "version.h" 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #include "latencyflex.h" 32 | 33 | #define LAYER_NAME "VK_LAYER_LFX_LatencyFleX" 34 | 35 | namespace { 36 | std::atomic_uint64_t frame_counter = 0; 37 | std::atomic_bool ticker_needs_reset = false; 38 | std::atomic_uint64_t frame_counter_render = 0; 39 | 40 | lfx::LatencyFleX manager; 41 | 42 | // Placebo mode. This turns off all sleeping but still retains latency and frame time tracking. 43 | // Useful for comparison benchmarks. Note that if the game does its own sleeping between the 44 | // syncpoint and input sampling, latency values from placebo mode might not be accurate. 45 | bool is_placebo_mode = false; 46 | 47 | typedef void(VKAPI_PTR *PFN_overlay_SetMetrics)(const char **, const float *, size_t); 48 | PFN_overlay_SetMetrics overlay_SetMetrics = nullptr; 49 | 50 | const int kMaxFrameDrift = 16; 51 | const std::chrono::milliseconds kRecalibrationSleepTime(200); 52 | 53 | typedef std::lock_guard scoped_lock; 54 | // single global lock, for simplicity 55 | std::mutex global_lock; 56 | 57 | struct PresentInfo { 58 | VkDevice device; 59 | VkFence fence; 60 | uint64_t frame_id; 61 | }; 62 | 63 | // use the loader's dispatch table pointer as a key for dispatch map lookups 64 | template void *GetKey(DispatchableType inst) { return *(void **)inst; } 65 | 66 | // layer book-keeping information, to store dispatch tables by key 67 | std::map instance_dispatch; 68 | std::map device_dispatch; 69 | std::map device_map; 70 | 71 | class FenceWaitThread { 72 | public: 73 | FenceWaitThread(); 74 | 75 | ~FenceWaitThread(); 76 | 77 | void Push(PresentInfo &&info) { 78 | scoped_lock l(local_lock_); 79 | queue_.push_back(info); 80 | notify_.notify_all(); 81 | } 82 | 83 | private: 84 | void Worker(); 85 | 86 | std::thread thread_; 87 | std::mutex local_lock_; 88 | std::condition_variable notify_; 89 | std::deque queue_; 90 | bool running_ = true; 91 | }; 92 | 93 | FenceWaitThread::FenceWaitThread() : thread_(&FenceWaitThread::Worker, this) {} 94 | 95 | FenceWaitThread::~FenceWaitThread() { 96 | running_ = false; 97 | notify_.notify_all(); 98 | thread_.join(); 99 | } 100 | 101 | void FenceWaitThread::Worker() { 102 | while (true) { 103 | PresentInfo info; 104 | { 105 | std::unique_lock l(local_lock_); 106 | while (queue_.empty()) { 107 | if (!running_) 108 | return; 109 | notify_.wait(l); 110 | } 111 | info = queue_.front(); 112 | queue_.pop_front(); 113 | } 114 | VkDevice device = info.device; 115 | VkLayerDispatchTable &dispatch = device_dispatch[GetKey(info.device)]; 116 | dispatch.WaitForFences(device, 1, &info.fence, VK_TRUE, -1); 117 | uint64_t complete = current_time_ns(); 118 | dispatch.DestroyFence(device, info.fence, nullptr); 119 | 120 | uint64_t latency; 121 | { 122 | scoped_lock l(global_lock); 123 | manager.EndFrame(info.frame_id, complete, &latency, nullptr); 124 | } 125 | float latency_f = latency / 1000000.; 126 | const char *name = "Latency"; 127 | if (overlay_SetMetrics && latency != UINT64_MAX) { 128 | overlay_SetMetrics(&name, &latency_f, 1); 129 | } 130 | } 131 | } 132 | 133 | std::map> wait_threads; 134 | } // namespace 135 | 136 | /////////////////////////////////////////////////////////////////////////////////////////// 137 | // Layer init and shutdown 138 | 139 | VkResult VKAPI_CALL lfx_CreateInstance(const VkInstanceCreateInfo *pCreateInfo, 140 | const VkAllocationCallbacks *pAllocator, 141 | VkInstance *pInstance) { 142 | VkLayerInstanceCreateInfo *layerCreateInfo = (VkLayerInstanceCreateInfo *)pCreateInfo->pNext; 143 | 144 | // step through the chain of pNext until we get to the link info 145 | while (layerCreateInfo && 146 | (layerCreateInfo->sType != VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO || 147 | layerCreateInfo->function != VK_LAYER_LINK_INFO)) { 148 | layerCreateInfo = (VkLayerInstanceCreateInfo *)layerCreateInfo->pNext; 149 | } 150 | 151 | if (layerCreateInfo == nullptr) { 152 | // No loader instance create info 153 | return VK_ERROR_INITIALIZATION_FAILED; 154 | } 155 | 156 | PFN_vkGetInstanceProcAddr gpa = layerCreateInfo->u.pLayerInfo->pfnNextGetInstanceProcAddr; 157 | // move chain on for next layer 158 | layerCreateInfo->u.pLayerInfo = layerCreateInfo->u.pLayerInfo->pNext; 159 | 160 | PFN_vkCreateInstance createFunc = (PFN_vkCreateInstance)gpa(VK_NULL_HANDLE, "vkCreateInstance"); 161 | 162 | VkResult ret = createFunc(pCreateInfo, pAllocator, pInstance); 163 | if (ret != VK_SUCCESS) 164 | return ret; 165 | 166 | // fetch our own dispatch table for the functions we need, into the next layer 167 | VkLayerInstanceDispatchTable dispatchTable; 168 | dispatchTable.GetInstanceProcAddr = 169 | (PFN_vkGetInstanceProcAddr)gpa(*pInstance, "vkGetInstanceProcAddr"); 170 | dispatchTable.DestroyInstance = (PFN_vkDestroyInstance)gpa(*pInstance, "vkDestroyInstance"); 171 | dispatchTable.EnumerateDeviceExtensionProperties = (PFN_vkEnumerateDeviceExtensionProperties)gpa( 172 | *pInstance, "vkEnumerateDeviceExtensionProperties"); 173 | 174 | // store the table by key 175 | { 176 | scoped_lock l(global_lock); 177 | instance_dispatch[GetKey(*pInstance)] = dispatchTable; 178 | 179 | if (void *mod = dlopen("libMangoHud.so", RTLD_NOW | RTLD_NOLOAD)) { 180 | overlay_SetMetrics = (PFN_overlay_SetMetrics)dlsym(mod, "overlay_SetMetrics"); 181 | } 182 | } 183 | 184 | return VK_SUCCESS; 185 | } 186 | 187 | void VKAPI_CALL lfx_DestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator) { 188 | scoped_lock l(global_lock); 189 | instance_dispatch[GetKey(instance)].DestroyInstance(instance, pAllocator); 190 | instance_dispatch.erase(GetKey(instance)); 191 | } 192 | 193 | VkResult VKAPI_CALL lfx_CreateDevice(VkPhysicalDevice physicalDevice, 194 | const VkDeviceCreateInfo *pCreateInfo, 195 | const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) { 196 | VkLayerDeviceCreateInfo *layerCreateInfo = (VkLayerDeviceCreateInfo *)pCreateInfo->pNext; 197 | 198 | // step through the chain of pNext until we get to the link info 199 | while (layerCreateInfo && 200 | (layerCreateInfo->sType != VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO || 201 | layerCreateInfo->function != VK_LAYER_LINK_INFO)) { 202 | layerCreateInfo = (VkLayerDeviceCreateInfo *)layerCreateInfo->pNext; 203 | } 204 | 205 | if (layerCreateInfo == nullptr) { 206 | // No loader instance create info 207 | return VK_ERROR_INITIALIZATION_FAILED; 208 | } 209 | 210 | PFN_vkGetInstanceProcAddr gipa = layerCreateInfo->u.pLayerInfo->pfnNextGetInstanceProcAddr; 211 | PFN_vkGetDeviceProcAddr gdpa = layerCreateInfo->u.pLayerInfo->pfnNextGetDeviceProcAddr; 212 | // move chain on for next layer 213 | layerCreateInfo->u.pLayerInfo = layerCreateInfo->u.pLayerInfo->pNext; 214 | 215 | PFN_vkCreateDevice createFunc = (PFN_vkCreateDevice)gipa(VK_NULL_HANDLE, "vkCreateDevice"); 216 | 217 | VkResult ret = createFunc(physicalDevice, pCreateInfo, pAllocator, pDevice); 218 | if (ret != VK_SUCCESS) 219 | return ret; 220 | 221 | #define ASSIGN_FUNCTION(name) dispatchTable.name = (PFN_vk##name)gdpa(*pDevice, "vk" #name); 222 | // fetch our own dispatch table for the functions we need, into the next layer 223 | VkLayerDispatchTable dispatchTable; 224 | ASSIGN_FUNCTION(GetDeviceProcAddr); 225 | ASSIGN_FUNCTION(DestroyDevice); 226 | ASSIGN_FUNCTION(QueuePresentKHR); 227 | ASSIGN_FUNCTION(AcquireNextImageKHR); 228 | ASSIGN_FUNCTION(AcquireNextImage2KHR); 229 | ASSIGN_FUNCTION(CreateFence); 230 | ASSIGN_FUNCTION(DestroyFence); 231 | ASSIGN_FUNCTION(QueueSubmit); 232 | ASSIGN_FUNCTION(WaitForFences); 233 | #undef ASSIGN_FUNCTION 234 | 235 | // store the table by key 236 | { 237 | scoped_lock l(global_lock); 238 | device_dispatch[GetKey(*pDevice)] = dispatchTable; 239 | device_map[GetKey(*pDevice)] = *pDevice; 240 | wait_threads[GetKey(*pDevice)] = std::make_unique(); 241 | } 242 | 243 | return VK_SUCCESS; 244 | } 245 | 246 | void VKAPI_CALL lfx_DestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) { 247 | scoped_lock l(global_lock); 248 | wait_threads.erase(GetKey(device)); 249 | device_dispatch[GetKey(device)].DestroyDevice(device, pAllocator); 250 | device_dispatch.erase(GetKey(device)); 251 | device_map.erase(GetKey(device)); 252 | } 253 | 254 | /////////////////////////////////////////////////////////////////////////////////////////// 255 | // Enumeration function 256 | 257 | VkResult VKAPI_CALL lfx_EnumerateInstanceLayerProperties(uint32_t *pPropertyCount, 258 | VkLayerProperties *pProperties) { 259 | if (pPropertyCount) 260 | *pPropertyCount = 1; 261 | 262 | if (pProperties) { 263 | strcpy(pProperties->layerName, LAYER_NAME); 264 | strcpy(pProperties->description, "LatencyFleX (TM) latency reduction middleware"); 265 | pProperties->implementationVersion = 1; 266 | pProperties->specVersion = VK_MAKE_VERSION(1, 2, 136); 267 | } 268 | 269 | return VK_SUCCESS; 270 | } 271 | 272 | VkResult VKAPI_CALL lfx_EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, 273 | uint32_t *pPropertyCount, 274 | VkLayerProperties *pProperties) { 275 | return lfx_EnumerateInstanceLayerProperties(pPropertyCount, pProperties); 276 | } 277 | 278 | VkResult VKAPI_CALL lfx_EnumerateInstanceExtensionProperties(const char *pLayerName, 279 | uint32_t *pPropertyCount, 280 | VkExtensionProperties *pProperties) { 281 | if (pLayerName == nullptr || strcmp(pLayerName, LAYER_NAME)) 282 | return VK_ERROR_LAYER_NOT_PRESENT; 283 | 284 | // don't expose any extensions 285 | if (pPropertyCount) 286 | *pPropertyCount = 0; 287 | return VK_SUCCESS; 288 | } 289 | 290 | VkResult VKAPI_CALL lfx_EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, 291 | const char *pLayerName, 292 | uint32_t *pPropertyCount, 293 | VkExtensionProperties *pProperties) { 294 | // pass through any queries that aren't to us 295 | if (pLayerName == nullptr || strcmp(pLayerName, LAYER_NAME)) { 296 | if (physicalDevice == VK_NULL_HANDLE) 297 | return VK_SUCCESS; 298 | 299 | scoped_lock l(global_lock); 300 | return instance_dispatch[GetKey(physicalDevice)].EnumerateDeviceExtensionProperties( 301 | physicalDevice, pLayerName, pPropertyCount, pProperties); 302 | } 303 | 304 | // don't expose any extensions 305 | if (pPropertyCount) 306 | *pPropertyCount = 0; 307 | return VK_SUCCESS; 308 | } 309 | 310 | VkResult VKAPI_CALL lfx_QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR *pPresentInfo) { 311 | frame_counter_render++; 312 | uint64_t frame_counter_local = frame_counter.load(); 313 | uint64_t frame_counter_render_local = frame_counter_render.load(); 314 | if (frame_counter_local > frame_counter_render_local + kMaxFrameDrift) { 315 | ticker_needs_reset.store(true); 316 | } 317 | 318 | std::unique_lock l(global_lock); 319 | VkDevice device = device_map[GetKey(queue)]; 320 | VkLayerDispatchTable &dispatch = device_dispatch[GetKey(queue)]; 321 | VkFence fence; 322 | VkFenceCreateInfo fenceInfo{}; 323 | fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; 324 | dispatch.CreateFence(device, &fenceInfo, nullptr, 325 | &fence); // TODO: error check 326 | VkSubmitInfo submitInfo{}; 327 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; 328 | VkPipelineStageFlags stages_wait = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT; 329 | submitInfo.waitSemaphoreCount = pPresentInfo->waitSemaphoreCount; 330 | submitInfo.pWaitSemaphores = pPresentInfo->pWaitSemaphores; 331 | submitInfo.pWaitDstStageMask = &stages_wait; 332 | submitInfo.signalSemaphoreCount = pPresentInfo->waitSemaphoreCount; 333 | submitInfo.pSignalSemaphores = pPresentInfo->pWaitSemaphores; 334 | dispatch.QueueSubmit(queue, 1, &submitInfo, fence); 335 | wait_threads[GetKey(device)]->Push({device, fence, frame_counter_render_local}); 336 | l.unlock(); 337 | return dispatch.QueuePresentKHR(queue, pPresentInfo); 338 | } 339 | 340 | VkResult VKAPI_CALL lfx_AcquireNextImageKHR(VkDevice device, VkSwapchainKHR swapchain, 341 | uint64_t timeout, VkSemaphore semaphore, VkFence fence, 342 | uint32_t *pImageIndex) { 343 | std::unique_lock l(global_lock); 344 | VkLayerDispatchTable &dispatch = device_dispatch[GetKey(device)]; 345 | l.unlock(); 346 | VkResult res = 347 | dispatch.AcquireNextImageKHR(device, swapchain, timeout, semaphore, fence, pImageIndex); 348 | if (res < 0) { 349 | // An error has occurred likely due to an Alt-Tab or resize. 350 | // The application will likely give up presenting this frame, which means that we won't get a 351 | // call to QueuePresentKHR! This can cause the frame counter to desync. Schedule a recalibration 352 | // immediately. 353 | ticker_needs_reset.store(true); 354 | } 355 | return res; 356 | } 357 | 358 | VkResult VKAPI_CALL lfx_AcquireNextImage2KHR(VkDevice device, 359 | const VkAcquireNextImageInfoKHR *pAcquireInfo, 360 | uint32_t *pImageIndex) { 361 | std::unique_lock l(global_lock); 362 | VkLayerDispatchTable &dispatch = device_dispatch[GetKey(device)]; 363 | l.unlock(); 364 | VkResult res = dispatch.AcquireNextImage2KHR(device, pAcquireInfo, pImageIndex); 365 | if (res < 0) { 366 | // An error has occurred likely due to an Alt-Tab or resize. 367 | // The application will likely give up presenting this frame, which means that we won't get a 368 | // call to QueuePresentKHR! This can cause the frame counter to desync. Schedule a recalibration 369 | // immediately. 370 | ticker_needs_reset.store(true); 371 | } 372 | return res; 373 | } 374 | 375 | /////////////////////////////////////////////////////////////////////////////////////////// 376 | // GetProcAddr functions, entry points of the layer 377 | 378 | #define GETPROCADDR(func) \ 379 | if (!strcmp(pName, "vk" #func)) \ 380 | return (PFN_vkVoidFunction)&lfx_##func 381 | 382 | extern "C" VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI_CALL lfx_GetDeviceProcAddr(VkDevice device, 383 | const char *pName) { 384 | // device chain functions we intercept 385 | GETPROCADDR(GetDeviceProcAddr); 386 | GETPROCADDR(EnumerateDeviceLayerProperties); 387 | GETPROCADDR(EnumerateDeviceExtensionProperties); 388 | GETPROCADDR(CreateDevice); 389 | GETPROCADDR(DestroyDevice); 390 | GETPROCADDR(QueuePresentKHR); 391 | GETPROCADDR(AcquireNextImageKHR); 392 | GETPROCADDR(AcquireNextImage2KHR); 393 | 394 | { 395 | scoped_lock l(global_lock); 396 | return device_dispatch[GetKey(device)].GetDeviceProcAddr(device, pName); 397 | } 398 | } 399 | 400 | extern "C" VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI_CALL 401 | lfx_GetInstanceProcAddr(VkInstance instance, const char *pName) { 402 | // instance chain functions we intercept 403 | GETPROCADDR(GetInstanceProcAddr); 404 | GETPROCADDR(EnumerateInstanceLayerProperties); 405 | GETPROCADDR(EnumerateInstanceExtensionProperties); 406 | GETPROCADDR(CreateInstance); 407 | GETPROCADDR(DestroyInstance); 408 | 409 | // device chain functions we intercept 410 | GETPROCADDR(GetDeviceProcAddr); 411 | GETPROCADDR(EnumerateDeviceLayerProperties); 412 | GETPROCADDR(EnumerateDeviceExtensionProperties); 413 | GETPROCADDR(CreateDevice); 414 | GETPROCADDR(DestroyDevice); 415 | GETPROCADDR(QueuePresentKHR); 416 | GETPROCADDR(AcquireNextImageKHR); 417 | GETPROCADDR(AcquireNextImage2KHR); 418 | 419 | { 420 | scoped_lock l(global_lock); 421 | return instance_dispatch[GetKey(instance)].GetInstanceProcAddr(instance, pName); 422 | } 423 | } 424 | 425 | extern "C" VK_LAYER_EXPORT void lfx_WaitAndBeginFrame() { 426 | frame_counter++; 427 | uint64_t frame_counter_local = frame_counter.load(); 428 | uint64_t frame_counter_render_local = frame_counter_render.load(); 429 | 430 | if (frame_counter_local <= frame_counter_render_local) { 431 | // Presentation has happened without going through the Tick() hook! 432 | // This typically happens during initialization (where graphics are redrawn 433 | // without ticking the platform loop). 434 | ticker_needs_reset.store(true); 435 | } 436 | 437 | if (ticker_needs_reset.load()) { 438 | std::cerr << "LatencyFleX: Performing recalibration!" << std::endl; 439 | // Try to reset (recalibrate) the state by sleeping for a slightly long 440 | // period and force any work in the rendering thread or the RHI thread to be 441 | // flushed. The frame counter is reset after the calibration. 442 | std::this_thread::sleep_for(kRecalibrationSleepTime); 443 | // The ticker thread has already incremented the frame counter above. Start 444 | // from 1, or otherwise it will result in frame ID mismatch. 445 | frame_counter.store(1); 446 | frame_counter_local = 1; 447 | frame_counter_render.store(0); 448 | frame_counter_render_local = 0; 449 | ticker_needs_reset.store(false); 450 | scoped_lock l(global_lock); 451 | manager.Reset(); 452 | } 453 | uint64_t now = current_time_ns(); 454 | uint64_t target; 455 | uint64_t wakeup; 456 | { 457 | scoped_lock l(global_lock); 458 | target = manager.GetWaitTarget(frame_counter_local); 459 | } 460 | if (!is_placebo_mode && target > now) { 461 | // failsafe: if something ever goes wrong, sustain an interactive framerate 462 | // so the user can at least quit the application 463 | static uint64_t failsafe_triggered = 0; 464 | uint64_t failsafe = now + UINT64_C(50000000); 465 | if (target > failsafe) { 466 | wakeup = failsafe; 467 | failsafe_triggered++; 468 | if (failsafe_triggered > 5) { 469 | // If failsafe is triggered multiple times in a row, initiate a recalibration. 470 | ticker_needs_reset.store(true); 471 | } 472 | } else { 473 | wakeup = target; 474 | failsafe_triggered = 0; 475 | } 476 | std::this_thread::sleep_for(std::chrono::nanoseconds(wakeup - now)); 477 | } else { 478 | wakeup = now; 479 | } 480 | { 481 | scoped_lock l(global_lock); 482 | // Use the sleep target as the frame begin time. See `BeginFrame` docs. 483 | manager.BeginFrame(frame_counter_local, target, wakeup); 484 | } 485 | } 486 | 487 | extern "C" VK_LAYER_EXPORT void lfx_SetTargetFrameTime(uint64_t target_frame_time) { 488 | scoped_lock l(global_lock); 489 | manager.target_frame_time = target_frame_time; 490 | std::cerr << "LatencyFleX: setting target frame time to " << manager.target_frame_time 491 | << std::endl; 492 | } 493 | 494 | namespace { 495 | class OnLoad { 496 | public: 497 | OnLoad() { 498 | std::cerr << "LatencyFleX: module loaded" << std::endl; 499 | std::cerr << "LatencyFleX: Version " LATENCYFLEX_VERSION << std::endl; 500 | if (getenv("LFX_MAX_FPS")) { 501 | // No lock needed because this is done inside static initialization. 502 | manager.target_frame_time = 1000000000 / std::stoul(getenv("LFX_MAX_FPS")); 503 | std::cerr << "LatencyFleX: setting target frame time to " << manager.target_frame_time 504 | << std::endl; 505 | } 506 | if (getenv("LFX_PLACEBO")) { 507 | is_placebo_mode = true; 508 | std::cerr << "LatencyFleX: Running in placebo mode" << std::endl; 509 | } 510 | } 511 | }; 512 | 513 | [[maybe_unused]] OnLoad on_load; 514 | } // namespace 515 | -------------------------------------------------------------------------------- /layer/latencyflex_layer.h: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Tatsuyuki Ishi 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef LATENCYFLEX_LATENCYFLEX_LAYER_H 16 | #define LATENCYFLEX_LATENCYFLEX_LAYER_H 17 | 18 | #if defined(__GNUC__) && __GNUC__ >= 4 19 | #define VK_LAYER_EXPORT __attribute__((visibility("default"))) 20 | #elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590) 21 | #define VK_LAYER_EXPORT __attribute__((visibility("default"))) 22 | #else 23 | #define VK_LAYER_EXPORT 24 | #endif 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | // These are private APIs. There is no backwards compatibility guarantee. 31 | 32 | extern "C" VK_LAYER_EXPORT void lfx_WaitAndBeginFrame(); 33 | extern "C" VK_LAYER_EXPORT void lfx_SetTargetFrameTime(uint64_t target_frame_time); 34 | 35 | inline uint64_t current_time_ns() { 36 | struct timespec tv; 37 | // CLOCK_BOOTTIME used for compatibility with Perfetto timestamps 38 | clock_gettime(CLOCK_BOOTTIME, &tv); 39 | return tv.tv_nsec + tv.tv_sec * UINT64_C(1000000000); 40 | } 41 | 42 | #endif // LATENCYFLEX_LATENCYFLEX_LAYER_H 43 | -------------------------------------------------------------------------------- /layer/latencyflex_perfetto.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Tatsuyuki Ishi 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifdef LATENCYFLEX_HAVE_PERFETTO 16 | #include "latencyflex.h" 17 | 18 | PERFETTO_TRACK_EVENT_STATIC_STORAGE(); 19 | 20 | namespace { 21 | void perfetto_init() { 22 | perfetto::TracingInitArgs args; 23 | args.backends |= perfetto::kSystemBackend; 24 | perfetto::Tracing::Initialize(args); 25 | perfetto::TrackEvent::Register(); 26 | } 27 | 28 | class OnLoad { 29 | public: 30 | OnLoad() { perfetto_init(); } 31 | }; 32 | 33 | [[maybe_unused]] OnLoad on_load; 34 | } // namespace 35 | #endif -------------------------------------------------------------------------------- /layer/latencyflex_ue4_hook.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Tatsuyuki Ishi 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | 17 | #include 18 | 19 | #include "latencyflex_layer.h" 20 | 21 | namespace { 22 | funchook_t *tick_hook; 23 | typedef void (*tick_func)(void *self); 24 | tick_func real_tick; 25 | 26 | void lfx_FEngineLoop_Tick(void *self) { 27 | lfx_WaitAndBeginFrame(); 28 | 29 | real_tick(self); 30 | } 31 | 32 | void ue4_hook_init() { 33 | if (getenv("LFX_UE4_HOOK")) { 34 | real_tick = reinterpret_cast(std::stoul(getenv("LFX_UE4_HOOK"), nullptr, 16)); 35 | } else { 36 | return; 37 | } 38 | int err; 39 | tick_hook = funchook_create(); 40 | err = funchook_prepare(tick_hook, (void **)&real_tick, (void *)lfx_FEngineLoop_Tick); 41 | if (err != 0) 42 | goto err; 43 | err = funchook_install(tick_hook, 0); 44 | if (err != 0) 45 | goto err; 46 | std::cerr << "LatencyFleX: Successfully initialized UE4 hook" << std::endl; 47 | return; 48 | 49 | err: 50 | std::cerr << "LatencyFleX: Error during UE4 hook initialization, err=" << err << std::endl; 51 | } 52 | 53 | class OnLoad { 54 | public: 55 | OnLoad() { ue4_hook_init(); } 56 | }; 57 | 58 | [[maybe_unused]] OnLoad on_load; 59 | } // namespace -------------------------------------------------------------------------------- /layer/layer.json.in: -------------------------------------------------------------------------------- 1 | { 2 | "file_format_version" : "1.0.0", 3 | "layer" : { 4 | "name": "VK_LAYER_LFX_LatencyFleX", 5 | "type": "GLOBAL", 6 | "library_path": "@lib_path@", 7 | "api_version": "1.3.0", 8 | "implementation_version": "1", 9 | "description": "LatencyFleX (TM) latency reduction middleware", 10 | "enable_environment": { "LFX": "1" }, 11 | "disable_environment": { "DISABLE_LFX": "1" }, 12 | "functions": { 13 | "vkGetInstanceProcAddr": "lfx_GetInstanceProcAddr", 14 | "vkGetDeviceProcAddr": "lfx_GetDeviceProcAddr" 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /layer/meson.build: -------------------------------------------------------------------------------- 1 | project('latencyflex_layer', 'c', 'cpp', default_options : ['c_std=c11', 'cpp_std=c++17']) 2 | 3 | cmake = import('cmake') 4 | 5 | cc = meson.get_compiler('c') 6 | funchook_opt = cmake.subproject_options() 7 | funchook_opt.add_cmake_defines({'CMAKE_POSITION_INDEPENDENT_CODE': true}) 8 | funchook_opt.set_install(false) 9 | funchook = cmake.subproject('funchook', options: funchook_opt) 10 | funchook_dep = funchook.dependency('funchook-static') 11 | distorm_dep = funchook.dependency('distorm') 12 | libdl_dep = cc.find_library('dl') 13 | 14 | vulkan_dep = dependency('vulkan') 15 | thread_dep = dependency('threads') 16 | 17 | deps = [vulkan_dep, thread_dep, funchook_dep, distorm_dep, libdl_dep] 18 | 19 | with_perfetto = get_option('perfetto') 20 | if with_perfetto 21 | dep_perfetto = dependency('perfetto', fallback : ['perfetto', 'dep_perfetto']) 22 | deps += dep_perfetto 23 | add_project_arguments('-DLATENCYFLEX_HAVE_PERFETTO', language : ['c', 'cpp']) 24 | endif 25 | 26 | incdir = include_directories('..') 27 | project_version = vcs_tag( 28 | command: ['git', 'describe', '--always', '--tags', '--dirty=+'], 29 | input: 'version.h.in', 30 | output: 'version.h') 31 | layer = library('latencyflex_layer', 'latencyflex_layer.cpp', 'latencyflex_ue4_hook.cpp', 'latencyflex_perfetto.cpp', project_version, 32 | gnu_symbol_visibility : 'hidden', 33 | link_args : '-Wl,--exclude-libs,ALL', 34 | dependencies : deps, 35 | include_directories : incdir, 36 | install: true) 37 | 38 | configure_file(input : 'layer.json.in', 39 | output : 'latencyflex.json', 40 | configuration : {'lib_path' : join_paths(get_option('prefix'), get_option('libdir'), 'liblatencyflex_layer.so')}, 41 | install : true, 42 | install_dir : join_paths(get_option('datadir'), 'vulkan', 'implicit_layer.d'), 43 | ) -------------------------------------------------------------------------------- /layer/meson_options.txt: -------------------------------------------------------------------------------- 1 | option( 2 | 'perfetto', 3 | type : 'boolean', 4 | value : false, 5 | description : 'Enable performance tracing with perfetto. Default: false' 6 | ) 7 | -------------------------------------------------------------------------------- /layer/unity/.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET Core 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # ASP.NET Scaffolding 66 | ScaffoldingReadMe.txt 67 | 68 | # StyleCop 69 | StyleCopReport.xml 70 | 71 | # Files built by Visual Studio 72 | *_i.c 73 | *_p.c 74 | *_h.h 75 | *.ilk 76 | *.meta 77 | *.obj 78 | *.iobj 79 | *.pch 80 | *.pdb 81 | *.ipdb 82 | *.pgc 83 | *.pgd 84 | *.rsp 85 | *.sbr 86 | *.tlb 87 | *.tli 88 | *.tlh 89 | *.tmp 90 | *.tmp_proj 91 | *_wpftmp.csproj 92 | *.log 93 | *.tlog 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio 6 auto-generated project file (contains which files were open etc.) 298 | *.vbp 299 | 300 | # Visual Studio 6 workspace and project file (working project files containing files to include in project) 301 | *.dsw 302 | *.dsp 303 | 304 | # Visual Studio 6 technical files 305 | *.ncb 306 | *.aps 307 | 308 | # Visual Studio LightSwitch build output 309 | **/*.HTMLClient/GeneratedArtifacts 310 | **/*.DesktopClient/GeneratedArtifacts 311 | **/*.DesktopClient/ModelManifest.xml 312 | **/*.Server/GeneratedArtifacts 313 | **/*.Server/ModelManifest.xml 314 | _Pvt_Extensions 315 | 316 | # Paket dependency manager 317 | .paket/paket.exe 318 | paket-files/ 319 | 320 | # FAKE - F# Make 321 | .fake/ 322 | 323 | # CodeRush personal settings 324 | .cr/personal 325 | 326 | # Python Tools for Visual Studio (PTVS) 327 | __pycache__/ 328 | *.pyc 329 | 330 | # Cake - Uncomment if you are using it 331 | # tools/** 332 | # !tools/packages.config 333 | 334 | # Tabs Studio 335 | *.tss 336 | 337 | # Telerik's JustMock configuration file 338 | *.jmconfig 339 | 340 | # BizTalk build output 341 | *.btp.cs 342 | *.btm.cs 343 | *.odx.cs 344 | *.xsd.cs 345 | 346 | # OpenCover UI analysis results 347 | OpenCover/ 348 | 349 | # Azure Stream Analytics local run output 350 | ASALocalRun/ 351 | 352 | # MSBuild Binary and Structured Log 353 | *.binlog 354 | 355 | # NVidia Nsight GPU debugger configuration file 356 | *.nvuser 357 | 358 | # MFractors (Xamarin productivity tool) working folder 359 | .mfractor/ 360 | 361 | # Local History for Visual Studio 362 | .localhistory/ 363 | 364 | # Visual Studio History (VSHistory) files 365 | .vshistory/ 366 | 367 | # BeatPulse healthcheck temp database 368 | healthchecksdb 369 | 370 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 371 | MigrationBackup/ 372 | 373 | # Ionide (cross platform F# VS Code tools) working folder 374 | .ionide/ 375 | 376 | # Fody - auto-generated XML schema 377 | FodyWeavers.xsd 378 | 379 | # VS Code files for those working on multiple tools 380 | .vscode/* 381 | !.vscode/settings.json 382 | !.vscode/tasks.json 383 | !.vscode/launch.json 384 | !.vscode/extensions.json 385 | *.code-workspace 386 | 387 | # Local History for Visual Studio Code 388 | .history/ 389 | 390 | # Windows Installer files from build outputs 391 | *.cab 392 | *.msi 393 | *.msix 394 | *.msm 395 | *.msp 396 | 397 | # JetBrains Rider 398 | *.sln.iml -------------------------------------------------------------------------------- /layer/unity/LatencyFleX.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net35 5 | LatencyFleX 6 | LatencyFleX Unity Hook 7 | 1.0.0 8 | true 9 | 8 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | ..\lib\unhollowed\Il2Cppmscorlib.dll 24 | False 25 | 26 | 27 | ..\lib\unhollowed\UnityEngine.dll 28 | False 29 | 30 | 31 | ..\lib\unhollowed\UnityEngine.CoreModule.dll 32 | False 33 | 34 | 35 | 36 | LFX_USE_IL2CPP;$(DefineConstants) 37 | netstandard2.1 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | LFX_USE_UNITY_2019_3;$(DefineConstants) 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /layer/unity/NuGet.Config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /layer/unity/Plugin.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using System.Runtime.InteropServices; 5 | using BepInEx; 6 | using BepInEx.Logging; 7 | #if LFX_USE_IL2CPP 8 | using BepInEx.IL2CPP; 9 | using BepInEx.IL2CPP.Utils; 10 | using UnhollowerRuntimeLib; 11 | #endif 12 | 13 | #if LFX_USE_UNITY_2019_3 14 | using UnityEngine.LowLevel; 15 | #else 16 | using UnityEngine.Experimental.LowLevel; 17 | #endif 18 | 19 | namespace LatencyFleX 20 | { 21 | public class Plugin 22 | { 23 | [DllImport("latencyflex_layer")] 24 | private static extern int lfx_WaitAndBeginFrame(); 25 | 26 | [DllImport("latencyflex_wine")] 27 | private static extern int winelfx_WaitAndBeginFrame(); 28 | 29 | private bool _isWine = false; 30 | 31 | private ManualLogSource _log; 32 | public Plugin(ManualLogSource log) 33 | { 34 | _log = log; 35 | } 36 | 37 | public void Run() 38 | { 39 | try 40 | { 41 | var method = GetType().GetMethod(nameof(lfx_WaitAndBeginFrame), 42 | BindingFlags.NonPublic | BindingFlags.Static); 43 | Marshal.Prelink(method); 44 | } 45 | catch (DllNotFoundException) 46 | { 47 | _log.LogInfo("Direct DLL load failed: trying wine bridge"); 48 | _isWine = true; 49 | try 50 | { 51 | var method = GetType().GetMethod(nameof(winelfx_WaitAndBeginFrame), 52 | BindingFlags.NonPublic | BindingFlags.Static); 53 | Marshal.Prelink(method); 54 | } 55 | catch (DllNotFoundException) 56 | { 57 | _log.LogError("Cannot find LatencyFleX runtime! Disabling plugin."); 58 | return; 59 | } 60 | } 61 | 62 | var updateDelegate = (Action) (() => 63 | { 64 | if (_isWine) 65 | { 66 | winelfx_WaitAndBeginFrame(); 67 | } 68 | else 69 | { 70 | lfx_WaitAndBeginFrame(); 71 | } 72 | }); 73 | 74 | #if LFX_USE_IL2CPP 75 | ClassInjector.RegisterTypeInIl2Cpp(); 76 | var mySystem = new PlayerLoopSystemInternal 77 | { 78 | type = UnhollowerRuntimeLib.Il2CppType.Of(), 79 | updateDelegate = updateDelegate, 80 | numSubSystems = 0, 81 | updateFunction = System.IntPtr.Zero, 82 | loopConditionFunction = System.IntPtr.Zero, 83 | }; 84 | 85 | var playerLoop = PlayerLoop.GetCurrentPlayerLoopInternal(); 86 | 87 | var systems = new List(playerLoop); 88 | // System 0 is the root node. It will never be executed 89 | systems[0].numSubSystems++; 90 | systems.Insert(1, mySystem); 91 | PlayerLoop.SetPlayerLoopInternal(systems.ToArray()); 92 | #else 93 | var mySystem = new PlayerLoopSystem 94 | { 95 | type = typeof(LfxBeforeLoopInit), 96 | updateDelegate = new PlayerLoopSystem.UpdateFunction(updateDelegate), 97 | }; 98 | 99 | #if LFX_USE_UNITY_2019_3 100 | var playerLoop = PlayerLoop.GetCurrentPlayerLoop(); 101 | #else 102 | var playerLoop = PlayerLoop.GetDefaultPlayerLoop(); 103 | #endif 104 | 105 | var initSubSystem = playerLoop.subSystemList[0]; 106 | var subSystem = new List(initSubSystem.subSystemList); 107 | subSystem.Insert(0, mySystem); 108 | initSubSystem.subSystemList = subSystem.ToArray(); 109 | playerLoop.subSystemList[0] = initSubSystem; 110 | 111 | PlayerLoop.SetPlayerLoop(playerLoop); 112 | #endif 113 | 114 | _log.LogInfo("Plugin " + PluginInfo.PLUGIN_GUID + " is loaded!"); 115 | } 116 | #if LFX_USE_IL2CPP 117 | private class LfxBeforeLoopInit: Il2CppSystem.Object {} 118 | #else 119 | private class LfxBeforeLoopInit {} 120 | #endif 121 | } 122 | 123 | #if LFX_USE_IL2CPP 124 | [BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)] 125 | public class Il2CppPlugin : BasePlugin 126 | { 127 | public override void Load() { 128 | var plugin = new Plugin(Log); 129 | plugin.Run(); 130 | } 131 | } 132 | #else 133 | [BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)] 134 | public class MonoPlugin : BaseUnityPlugin 135 | { 136 | private void Awake() 137 | { 138 | var plugin = new Plugin(Logger); 139 | plugin.Run(); 140 | } 141 | } 142 | #endif 143 | } -------------------------------------------------------------------------------- /layer/version.h.in: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define LATENCYFLEX_VERSION "@VCS_TAG@" -------------------------------------------------------------------------------- /layer/wine/builtin.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Tatsuyuki Ishi 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #define __WINESRC__ 16 | #include 17 | #if defined(__GNUC__) && __GNUC__ >= 4 18 | #define VK_LAYER_EXPORT __attribute__((visibility("default"))) 19 | #elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590) 20 | #define VK_LAYER_EXPORT __attribute__((visibility("default"))) 21 | #else 22 | #define VK_LAYER_EXPORT 23 | #endif 24 | 25 | // Silence keyword conflict in RegisterUserApiHook which uses the name `new` in arguments 26 | #define new 27 | #include 28 | #undef new 29 | 30 | // Keep this in sync with __wine_unix_call_funcs. 31 | enum lfx_funcs { 32 | unix_WaitAndBeginFrame, 33 | unix_SetTargetFrameTime, 34 | }; 35 | 36 | // Internal definitions copied out of the wine source tree. 37 | // These APIs are likely unstable: copy these at your own risk. They will require changes when 38 | // upstream modifies the mechanism. 39 | typedef LONG NTSTATUS; 40 | typedef NTSTATUS (*unixlib_entry_t)(void *args); 41 | typedef UINT64 unixlib_handle_t; 42 | typedef enum _MEMORY_INFORMATION_CLASS { 43 | MemoryWineUnixFuncs = 1000, 44 | } MEMORY_INFORMATION_CLASS; 45 | 46 | static HMODULE ntdll_handle; 47 | static unixlib_handle_t binding_handle; 48 | typedef NTSTATUS(WINAPI *PFN_NtQueryVirtualMemory)(HANDLE, LPCVOID, MEMORY_INFORMATION_CLASS, PVOID, 49 | SIZE_T, SIZE_T *); 50 | static PFN_NtQueryVirtualMemory pNtQueryVirtualMemory; 51 | typedef NTSTATUS(WINAPI *PFN___wine_unix_call)(unixlib_handle_t handle, unsigned int code, 52 | void *args); 53 | static PFN___wine_unix_call __wine_unix_call; 54 | #define UNIX_CALL(func, params) __wine_unix_call(binding_handle, unix_##func, params) 55 | 56 | extern "C" VK_LAYER_EXPORT void winelfx_WaitAndBeginFrame() { 57 | UNIX_CALL(WaitAndBeginFrame, nullptr); 58 | } 59 | 60 | extern "C" VK_LAYER_EXPORT void winelfx_SetTargetFrameTime(__int64 target_frame_time) { 61 | UNIX_CALL(SetTargetFrameTime, &target_frame_time); 62 | } 63 | 64 | BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved) { 65 | switch (reason) { 66 | case DLL_PROCESS_ATTACH: 67 | DisableThreadLibraryCalls(hinst); 68 | ntdll_handle = GetModuleHandleA("ntdll.dll"); 69 | pNtQueryVirtualMemory = reinterpret_cast( 70 | GetProcAddress(ntdll_handle, "NtQueryVirtualMemory")); 71 | __wine_unix_call = 72 | reinterpret_cast(GetProcAddress(ntdll_handle, "__wine_unix_call")); 73 | if (!__wine_unix_call) { 74 | fprintf(stderr, 75 | __FILE__ ": Cannot find __wine_unix_call. This Wine version is likely too old\n"); 76 | return FALSE; 77 | } 78 | NTSTATUS err = pNtQueryVirtualMemory(GetCurrentProcess(), hinst, MemoryWineUnixFuncs, 79 | &binding_handle, sizeof(binding_handle), nullptr); 80 | if (err) { 81 | fprintf(stderr, __FILE__ ": Querying MemoryWineUnixFuncs failed %lx\n", err); 82 | fprintf(stderr, __FILE__ ": Look for library loading errors in the log and check if " 83 | "liblatencyflex_layer.so is installed on your system.\n"); 84 | return FALSE; 85 | } 86 | break; 87 | } 88 | return TRUE; 89 | } 90 | -------------------------------------------------------------------------------- /layer/wine/cross-mingw64.txt: -------------------------------------------------------------------------------- 1 | [binaries] 2 | c = ['winegcc', '-b', 'x86_64-w64-mingw32'] 3 | cpp = ['wineg++', '-b', 'x86_64-w64-mingw32'] 4 | ar = 'x86_64-w64-mingw32-ar' 5 | strip = 'x86_64-w64-mingw32-strip' 6 | 7 | [host_machine] 8 | system = 'windows' 9 | cpu_family = 'x86_64' 10 | cpu = 'x86_64' 11 | endian = 'little' -------------------------------------------------------------------------------- /layer/wine/cross-wine64.txt: -------------------------------------------------------------------------------- 1 | [binaries] 2 | c = 'winegcc' 3 | cpp = 'wineg++' 4 | ar = 'ar' 5 | strip = 'strip' 6 | 7 | [properties] 8 | needs_exe_wrapper = true 9 | 10 | [host_machine] 11 | system = 'linux' 12 | cpu_family = 'x86_64' 13 | cpu = 'x86_64' 14 | endian = 'little' -------------------------------------------------------------------------------- /layer/wine/latencyflex_layer.spec: -------------------------------------------------------------------------------- 1 | @ cdecl lfx_WaitAndBeginFrame() winelfx_WaitAndBeginFrame 2 | @ cdecl lfx_SetTargetFrameTime(int64) winelfx_SetTargetFrameTime -------------------------------------------------------------------------------- /layer/wine/latencyflex_wine.spec: -------------------------------------------------------------------------------- 1 | @ cdecl winelfx_WaitAndBeginFrame() latencyflex_layer.lfx_WaitAndBeginFrame 2 | @ cdecl winelfx_SetTargetFrameTime(int64) latencyflex_layer.lfx_SetTargetFrameTime -------------------------------------------------------------------------------- /layer/wine/meson.build: -------------------------------------------------------------------------------- 1 | project('latencyflex_layer_wine', 'cpp') 2 | 3 | is_mingw = target_machine.system() == 'windows' 4 | 5 | incdir = include_directories('..') 6 | if not is_mingw 7 | layer = meson.get_compiler('cpp').find_library('latencyflex_layer') 8 | endif 9 | 10 | arch_dir_prefix = target_machine.cpu_family() == 'x86_64' ? 'x86_64-' : 'i386-' 11 | os_suffix = is_mingw ? 'windows' : 'unix' 12 | 13 | install_dir = get_option('libdir') / 'wine' / arch_dir_prefix + os_suffix 14 | 15 | if get_option('old_install_layout') 16 | install_dir = is_mingw ? install_dir / 'fakedlls' : install_dir 17 | endif 18 | 19 | if is_mingw 20 | layer_dll = shared_library('latencyflex_layer', ['builtin.cpp'], 21 | name_prefix : '', 22 | link_depends : ['latencyflex_layer.spec'], 23 | link_args : ['-Wl,--wine-builtin', meson.current_source_dir() / 'latencyflex_layer.spec'], 24 | install : true, 25 | install_dir : install_dir) 26 | 27 | compat_dll = shared_library('latencyflex_wine', [], 28 | name_prefix : '', 29 | link_depends : ['latencyflex_wine.spec'], 30 | link_args : ['-Wl,--wine-builtin', meson.current_source_dir() / 'latencyflex_wine.spec'], 31 | install : true, 32 | install_dir : install_dir) 33 | else 34 | binding = shared_library('latencyflex_layer', ['unixlib.cpp'], 35 | name_prefix : '', 36 | dependencies : [layer], 37 | include_directories : incdir, 38 | install : true, 39 | install_dir : install_dir) 40 | endif -------------------------------------------------------------------------------- /layer/wine/meson_options.txt: -------------------------------------------------------------------------------- 1 | option('old_install_layout', 2 | type : 'boolean', 3 | value : false, 4 | description : 'Use old (<= 6.8) Wine directory layout for installs. Default: false') -------------------------------------------------------------------------------- /layer/wine/unixlib.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Tatsuyuki Ishi 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "latencyflex_layer.h" 16 | #define NTSTATUS long 17 | 18 | extern "C" { 19 | 20 | // Internal definitions copied out of the wine source tree. 21 | // These APIs are likely unstable: copy these at your own risk. They will require changes when 22 | // upstream modifies the mechanism. 23 | typedef NTSTATUS (*unixlib_entry_t)(void *args); 24 | 25 | static NTSTATUS winelfx_WaitAndBeginFrame(void *) { 26 | lfx_WaitAndBeginFrame(); 27 | return 0; 28 | } 29 | 30 | static NTSTATUS winelfx_SetTargetFrameTime(void *target_frame_time) { 31 | lfx_SetTargetFrameTime(*(int64_t *)target_frame_time); 32 | return 0; 33 | } 34 | 35 | // extern declaration is required, or g++ would happily mangle the symbol name 36 | extern const unixlib_entry_t __wine_unix_call_funcs[]; 37 | // Keep this in sync with builtin.cpp. 38 | const unixlib_entry_t __wine_unix_call_funcs[] = { 39 | winelfx_WaitAndBeginFrame, 40 | winelfx_SetTargetFrameTime, 41 | }; 42 | 43 | } // extern "C" -------------------------------------------------------------------------------- /performance.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ishitatsuyuki/LatencyFleX/b677c2e41f425a3d4c80ab099a2687479e1b35ef/performance.png --------------------------------------------------------------------------------