18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-21 21:24:58 +01:00
Commit Graph

12 Commits

Author SHA1 Message Date
maj00r
79c88c1793 Python screens: the worker's image goes in place only after its fence, not a bare glFlush (#411)
* fix(python): a screen uploaded by the worker goes into a texture of its own, put in place after a fence

With python.threadedupload (the default) the worker wrote the image into the texture the drawing thread
draws from, with glTexImage2D and glGenerateMipmap from its own context, and only a glFlush after. Nothing
ordered the two contexts: the drawing one could sample the texture while its storage was being made anew,
its level 0 half written or its mipmaps not made yet, and on a change of size the worker deleted the texture
and wrote a new name into `id` with no synchronisation.

The worker now uploads each image into a new texture, sets a fence after it and flushes. The drawing thread,
once per frame in python_taskqueue::update(), waits for that fence on the gpu (glWaitSync, the cpu goes on),
deletes the old texture and puts the new one in place. The worker no longer touches the texture drawn from,
nor `id`. An image the drawing thread didn't take yet is dropped by the next one. Without sync objects the
worker waits for the upload with glFinish.

The windows of python.displaywindows with python.sharectx bound the texture once; they now bind the name of
the last image put in place, which python_rt keeps for them.

Uploads on the drawing thread (python.threadedupload no, the Lua and WebAssembly screens, the editor) are
as they were.

* fix(python): the image of the worker goes in place once its fence is signalled, the viewer binds it each frame

From the review of 8811ee5a:

- glWaitSync ordered only the context of the main window. The extra viewports and the windows of
  python.displaywindows draw in contexts of their own and could bind the new texture before the upload
  was done. commit_thread_upload() now asks the fence without waiting (glClientWaitSync, timeout 0) and
  puts the image in place only once it is signalled, so every context binding it afterwards has it whole.
  Until then the task stays in the upload queue and is asked again the next frame.
- The drawing thread chose between upload and commit by python.threadedupload, the worker by whether it
  got a context. With python.threadedupload on and no context made for the worker, the screens were no
  longer updated. The task now records that a worker uploaded it.
- The viewer of python.displaywindows told a new image by its texture name, but a deleted name comes back
  for a later texture. It binds the name of the last image each frame, read under the lock of the render
  target, and the name it starts with is read on the drawing thread when it is made.

* fix(python): the old texture deleted under the lock the viewers bind under, a fence not made waited for with glFinish

From the review of #411:

- finish_upload() deleted the old texture before it took the lock of the render target, and the
  windows of python.displaywindows read the name under that lock but bound it after letting go of it.
  A window could bind a name already deleted, or already given to the next upload. In a compatibility
  context (the legacy renderer) that makes a new empty texture: a black frame and a texture never
  deleted. The commit and the publishing of the new name now go under the lock, and the viewer binds
  under it too. A name bound before the deletion stays alive in that window's context while bound.
- A glFenceSync that returns no fence was taken for a signalled one: the image went in place at once,
  with nothing ordering the contexts. The worker now waits for its upload with glFinish then, as
  without sync objects.

* fix(python): the worker waits for the lock of the screen without the gil, no flush after a finish

From the review of 939514ea:

- The drawing thread now holds the lock of the render target for a whole upload with
  python.threadedupload off. The worker, copying the next image under that lock, waited for it holding
  the gil. It now lets the gil go around the lock and the copy: the bytes it copies stay valid, the
  object is immutable and held by the reference the worker has.
- glFlush after glFinish did nothing; the worker flushes only when it made a fence.

---------

Co-authored-by: maj00r <maj00r@host>
2026-11-20 03:39:42 +01:00
maj00r
72c69d9249 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>
2026-11-20 03:39:01 +01:00
maj00r
0bbf07bf11 fix(editor): the images of the materials let go of with all their textures, the browser's Refresh a frame later
An image of a material read the material with all its textures, the normal and specular maps and the variants, and only the first one was let go of: those stayed in the memory for every material scrolled past. They are let go of together now (Unload_Material). texture_manager::unload also takes the textures read and not made yet, waiting for a worker still reading one, and deletes the gl texture itself: release() would read it back from the card with gfx.resource.move only for it to be dropped.

The Refresh of the texture browser let go of the images in the middle of the frame, while panels drawn before it could still refer to them; it now marks them, and they go at the start of the next frame.

Checked on td.scn by a scripted run: 8040 images of the picker read a batch every few frames end at 2.0 GB, against 2.3-2.6 GB before this and 1.1 GB at the start; a trackbed material picked from the folder is drawn on the track after its image was let go.
2026-11-16 02:10:59 +01:00
maj00r
4525023777 fix(editor): the texture pickers let go of the images scrolled past, the memory no longer runs out
The picker of the textures of a track offers every texture of the textures folder, and each tile that came into sight read its texture full size, into the graphics card and the memory, and kept it there for good. Scrolling through the list for a trackbed texture took about 2.5 MB a tile: some 900 tiles made 3.5 GB, the whole folder 28 GB, and the game went down once the memory ran out.

An image out of sight for 3 seconds is now taken out of both again (texture_manager::unload, Unload_Texture of the renderers) and read anew when it's shown once more, a few a frame as before. Only a texture the scene has never drawn is let go; those of the scene stay to the resource sweep. The texture browser lets go of its images on Refresh too. The picker no longer lists textures/previews: the 4100 previews of the node bank are images of models, not textures of anything.

Checked on td.scn by a scripted run: 8000 images of the picker read one batch a few frames after another held the game at 2-3 GB, where it went to 28 GB and fell before; images shown again after they were let go come back; a trackbed texture picked from the folder is drawn on the track after its image was let go.
2026-11-16 01:54:40 +01:00
c9792672c6 fix(render): textures of a far vehicle no longer come back black with gfx.resource.move (#308) 2026-11-10 00:41:14 +01:00
917c8f7285 Merge branch 'master' into feat/modern-editor 2026-10-08 17:44:58 +02:00
28b4e57133 feat(loading): optional background loading: sound files decoded by worker threads (sound.asyncload), data of dds textures read by worker threads (gfx.textures.streaming), gl textures of vehicles farther than a multiple of the draw range released (gfx.textures.releasedistance); all off by default 2026-10-08 16:17:46 +02:00
jerrrrycho
79bd0c8e91 fix(sonar): addressing reliability/security issues (#202) 2026-10-04 20:00:13 +02:00
jerrrrycho
d85096f64d reformat: remove redundant parentheses 2026-06-30 21:19:46 +02:00
docentYT
28baad8c5b Refactor clamp_power_of_two 2026-05-02 00:35:50 +02:00
copilot-swe-agent[bot]
7e49aa8740 Rewrite #includes to use explicit subdirectory paths
Co-authored-by: Hirek193 <23196899+Hirek193@users.noreply.github.com>
2026-03-14 19:16:48 +00:00
copilot-swe-agent[bot]
0531086bb9 Reorganize source files into logical subdirectories
Co-authored-by: Hirek193 <23196899+Hirek193@users.noreply.github.com>
2026-03-14 19:01:57 +00:00