mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-21 06:14:58 +01:00
Fix stutter with fpslimit on Windows and a texture unit cache race with threaded Python screens (#399)
* fix(render): the python upload thread only marks the texture unit cache stale, the drawing thread clears it
update_from_memory() runs on the python upload thread with python.threadedupload (the default), and since
e230d7b9 it cleared opengl_texture::units there outright. That raced Bind_Material filling the same array
for its multi-bind: a unit reset between the fill and glBindTextures() made the bind of that unit fail, and
the object was drawn with the texture of the one drawn before it, for a frame, at the rate of the screens.
The thread now sets an atomic flag, and the drawing thread clears the cache when it next binds textures
(bind() and the multi-bind of Bind_Material), so a screen texture given a new gl name is still rebound.
* fix(app): fpslimit asks Windows for the 1 ms timer, the frames no longer come in turns long and short
The wait of fpslimit sleeps, and Windows wakes a process which didn't ask for a finer timer in steps of
up to ~15.6 ms. A 10 ms slot of fpslimit 100 ended ~15.6 ms later, the pacer made it up with no wait in the
next frame, and with vsync on the frames were shown for one or two refreshes in turn: visible stutter at a
steady FPS. timeBeginPeriod(1) is held from the settings to the exit, only when fpslimit is set.
* fix: the 1 ms timer also for an fpslimit set by the scenery, the stale unit cache checked with a plain load first
A config block of the scenery is parsed after the settings and can set fpslimit, so the timer period is
asked for at the first wait of the pacer too, once. bind() checks the stale flag of the unit cache with a
relaxed load before the exchange, which it does for every texture.
* fix(textures): the stale flag of the unit cache uses the default memory order (Sonar S8417)
On x86 a sequentially consistent load and exchange cost the same as the
relaxed and acq_rel ones; only the store in invalidate_unit_cache() becomes
an xchg, and it runs rarely.
---------
Co-authored-by: maj00r <maj00r@host>
This commit is contained in:
@@ -54,6 +54,7 @@ http://mozilla.org/MPL/2.0/.
|
||||
#pragma comment(lib, "setupapi.lib")
|
||||
#pragma comment(lib, "dbghelp.lib")
|
||||
#pragma comment(lib, "version.lib")
|
||||
#include <timeapi.h>
|
||||
#endif
|
||||
|
||||
#ifdef __unix__
|
||||
@@ -65,6 +66,36 @@ eu07_application Application;
|
||||
screenshot_manager screenshot_man;
|
||||
ui_layer uilayerstaticinitializer;
|
||||
|
||||
namespace
|
||||
{
|
||||
#ifdef _WIN32
|
||||
// windows wakes a sleeping thread of a process which didn't ask for a finer timer in steps of up to ~15.6 ms.
|
||||
// the wait of fpslimit sleeps, so a 10 ms frame slot of fpslimit 100 ended ~15.6 ms later, the pacer made it up
|
||||
// with no wait in the next frame, and the frames came in turns long and short: with vsync on, shown for one or
|
||||
// two refreshes of the screen. the finer timer is asked for only with fpslimit, the only steady sleeper of the
|
||||
// main loop, as it costs some power for as long as it's held
|
||||
bool timer_period_set{false};
|
||||
#endif
|
||||
|
||||
// asks for the finer timer once, when fpslimit is set
|
||||
void begin_timer_period()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
if (!timer_period_set && Global.minframetime.count() > 0.f)
|
||||
timer_period_set = (timeBeginPeriod(1) == TIMERR_NOERROR);
|
||||
#endif
|
||||
}
|
||||
|
||||
void end_timer_period()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
if (timer_period_set)
|
||||
timeEndPeriod(1);
|
||||
timer_period_set = false;
|
||||
#endif
|
||||
}
|
||||
} // namespace
|
||||
|
||||
#ifdef _WIN32
|
||||
extern "C"
|
||||
{
|
||||
@@ -352,6 +383,8 @@ int eu07_application::init(int Argc, char *Argv[])
|
||||
ErrorLog("Failed to initialize settings! Maybe you're missing eu07.ini file?");
|
||||
return result;
|
||||
}
|
||||
// fpslimit is known now
|
||||
begin_timer_period();
|
||||
|
||||
// the run's seed, settled once and kept in Globals: the engines come out of it.
|
||||
// narrowed on purpose - mt19937 and seed_of() both work on 32 bits, and that is
|
||||
@@ -686,6 +719,8 @@ int eu07_application::run()
|
||||
// fpslimit: wait for the next frame's slot (eu07/maszyna#416)
|
||||
if (auto const nextframe{framepacer.next_frame(frame_pacer::clock::now(), Global.minframetime)})
|
||||
{
|
||||
// fpslimit may come from the config block of the scenery, read after the settings
|
||||
begin_timer_period();
|
||||
std::this_thread::sleep_until(*nextframe);
|
||||
}
|
||||
}
|
||||
@@ -782,6 +817,7 @@ void eu07_application::exit()
|
||||
if (!Global.exec_on_exit.empty())
|
||||
system(Global.exec_on_exit.c_str());
|
||||
|
||||
end_timer_period();
|
||||
step("done");
|
||||
StopLogService();
|
||||
auto it = Global.threads.find("LogService");
|
||||
|
||||
@@ -323,6 +323,7 @@ GLint readback_format( GLint const Internalformat ) {
|
||||
|
||||
std::array<GLuint, gl::MAX_TEXTURES + gl::HELPER_TEXTURES> opengl_texture::units = { 0 };
|
||||
GLint opengl_texture::m_activeunit = -1;
|
||||
std::atomic<bool> opengl_texture::m_unitcachestale { false };
|
||||
|
||||
std::unordered_map<GLint, int> opengl_texture::precompressed_formats =
|
||||
{
|
||||
@@ -729,9 +730,10 @@ void opengl_texture::update_from_memory(size_t width, size_t height, const uint8
|
||||
is_static = true;
|
||||
is_ready = true;
|
||||
}
|
||||
// the name may be a new one, and the unit active took the texture past the unit cache
|
||||
for (auto &unit : units)
|
||||
unit = -1;
|
||||
// the name may be a new one, and the unit active took the texture past the unit cache. this runs on the python upload
|
||||
// thread too (python.threadedupload), so the cache is only marked here: clearing it outright raced the drawing thread
|
||||
// filling it for a multi-bind, and an object got the texture of the one drawn before it for a frame
|
||||
invalidate_unit_cache();
|
||||
}
|
||||
|
||||
void
|
||||
@@ -1263,6 +1265,7 @@ opengl_texture::bind(size_t unit) {
|
||||
return false;
|
||||
}
|
||||
|
||||
sync_unit_cache();
|
||||
if (units[unit] == id)
|
||||
return true;
|
||||
if (GLAD_GL_ARB_direct_state_access)
|
||||
@@ -1630,6 +1633,25 @@ opengl_texture::reset_unit_cache() {
|
||||
m_activeunit = -1;
|
||||
}
|
||||
|
||||
void
|
||||
opengl_texture::invalidate_unit_cache() {
|
||||
|
||||
m_unitcachestale.store( true );
|
||||
}
|
||||
|
||||
void
|
||||
opengl_texture::sync_unit_cache() {
|
||||
|
||||
// a plain load first: bind() calls this for every texture, and a read-modify-write each time would cost more
|
||||
if( ( false == m_unitcachestale.load() )
|
||||
|| ( false == m_unitcachestale.exchange( false ) ) ) {
|
||||
return;
|
||||
}
|
||||
for( auto &unit : units ) {
|
||||
unit = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
opengl_texture::set_filtering() const
|
||||
{
|
||||
|
||||
@@ -9,6 +9,7 @@ http://mozilla.org/MPL/2.0/.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <istream>
|
||||
#include <limits>
|
||||
#include "winheaders.h"
|
||||
@@ -47,6 +48,13 @@ struct opengl_texture : public ITexture {
|
||||
set_components_hint( GLint hint ) override;
|
||||
static void
|
||||
reset_unit_cache();
|
||||
// marks the unit cache as not to be trusted. safe to call from any thread: the cache itself is
|
||||
// cleared by the thread drawing with it, the next time it binds textures (sync_unit_cache)
|
||||
static void
|
||||
invalidate_unit_cache();
|
||||
// clears the unit cache if a thread marked it as not to be trusted since. called before the cache is read
|
||||
static void
|
||||
sync_unit_cache();
|
||||
virtual int
|
||||
get_width() const override {
|
||||
return data_width; }
|
||||
@@ -100,6 +108,8 @@ struct opengl_texture : public ITexture {
|
||||
GLenum target = GL_TEXTURE_2D;
|
||||
static std::array<GLuint, gl::MAX_TEXTURES + gl::HELPER_TEXTURES> units;
|
||||
static GLint m_activeunit;
|
||||
// set by invalidate_unit_cache(), from the python upload thread too; the units themselves are touched only by the drawing thread
|
||||
static std::atomic<bool> m_unitcachestale;
|
||||
|
||||
public:
|
||||
// methods
|
||||
|
||||
@@ -2457,6 +2457,8 @@ void opengl33_renderer::Bind_Material( material_handle const Material, TSubModel
|
||||
|
||||
if (GLAD_GL_ARB_multi_bind)
|
||||
{
|
||||
// the cache may have been marked stale by the python upload thread
|
||||
opengl_texture::sync_unit_cache();
|
||||
GLuint lastdiff = 0;
|
||||
size_t i;
|
||||
for (i = 0; i < gl::MAX_TEXTURES; i++)
|
||||
|
||||
Reference in New Issue
Block a user