18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-21 08:35:01 +01:00
Files
maszyna/global_include/interfaces
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
..