18
0
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:
maj00r
2026-11-20 03:39:01 +01:00
committed by GitHub
parent 4b80d3c428
commit 72c69d9249
4 changed files with 73 additions and 3 deletions

View File

@@ -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