| 922 | } |
| 923 | |
| 924 | void CRender::End() |
| 925 | { |
| 926 | r_main.sync(); |
| 927 | |
| 928 | // DoAsyncScreenshot(); |
| 929 | |
| 930 | R_ASSERT(HW.pDevice); |
| 931 | |
| 932 | { |
| 933 | ZoneScopedN("Cleanup"); |
| 934 | |
| 935 | for (auto& id : contexts_pool) |
| 936 | { |
| 937 | id.cmd_list.OnFrameEnd(); |
| 938 | } |
| 939 | |
| 940 | cleanup_contexts(); |
| 941 | } |
| 942 | |
| 943 | { |
| 944 | ZoneScopedN("PresentWait"); |
| 945 | |
| 946 | HW.WaitOnSwapChain(); // wait for prev Present to finish. not sure that it is best place to wait, but it works |
| 947 | } |
| 948 | |
| 949 | { |
| 950 | ZoneScopedN("fps_lock"); |
| 951 | |
| 952 | const auto now = std::chrono::high_resolution_clock::now(); |
| 953 | static auto s_LastPresentTime = now; |
| 954 | |
| 955 | constexpr long long menuFPSlimit{60ll}, pauseFPSlimit{60ll}; |
| 956 | const long long curFPSLimit = IsMainMenuActive() ? menuFPSlimit : Device.Paused() ? pauseFPSlimit : g_dwFPSlimit; |
| 957 | |
| 958 | if (curFPSLimit > 0ll) |
| 959 | { |
| 960 | const auto frameTime = std::chrono::duration_cast<std::chrono::microseconds>(now - s_LastPresentTime).count(); |
| 961 | // конверсия лимита фпс в микросекунды |
| 962 | const auto targetFrameTime = 1000000ll / curFPSLimit; |
| 963 | |
| 964 | if (frameTime < targetFrameTime) |
| 965 | { |
| 966 | // считаем сколько спать |
| 967 | const auto remainingTime = targetFrameTime - frameTime; |
| 968 | const auto sleepTime = remainingTime / 1000ll; |
| 969 | |
| 970 | if (sleepTime > 2ll) |
| 971 | { |
| 972 | Sleep(static_cast<DWORD>(sleepTime - 2ll)); // поспать на 2мс меньше, чтоб не переспать |
| 973 | } |
| 974 | |
| 975 | // spinwait оставшееся время |
| 976 | const auto spinWaitEnd = now + std::chrono::microseconds(targetFrameTime - frameTime); |
| 977 | while (std::chrono::high_resolution_clock::now() < spinWaitEnd) |
| 978 | { |
| 979 | YieldProcessor(); |
| 980 | } |
| 981 | } |
no test coverage detected