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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user