18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-21 09:44:59 +01:00

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>
This commit is contained in:
maj00r
2026-11-20 03:39:42 +01:00
committed by GitHub
parent 1b26caca24
commit 79c88c1793
7 changed files with 204 additions and 17 deletions

View File

@@ -29,6 +29,7 @@ http://mozilla.org/MPL/2.0/.
#include "winheaders.h"
#include <bit>
#include <utility>
#define EU07_DEFERRED_TEXTURE_UPLOAD
@@ -736,6 +737,100 @@ void opengl_texture::update_from_memory(size_t width, size_t height, const uint8
invalidate_unit_cache();
}
namespace
{
// gl objects of an image uploaded by another thread that was never put in place
void delete_thread_upload(GLuint const Texture, GLsync const Fence)
{
if (Fence != nullptr)
glDeleteSync(Fence);
glDeleteTextures(1, &Texture);
}
} // namespace
void opengl_texture::upload_from_thread(size_t width, size_t height, const uint8_t *raw)
{
// a texture of its own each time: the one drawn from may be in use by the gpu for the drawing context, and writing it from
// here, with nothing to order the two contexts, could leave it incomplete or half written for a frame
texture_thread_upload upload;
upload.width = width;
upload.height = height;
glGenTextures(1, &upload.id);
glBindTexture(target, upload.id);
set_filtering();
glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap_mode_s);
glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap_mode_t);
if (!glGenerateMipmap)
glTexParameteri(target, GL_GENERATE_MIPMAP, GL_TRUE);
glTexImage2D(target, 0, GL_SRGB8_ALPHA8, static_cast<GLsizei>(width), static_cast<GLsizei>(height), 0, GL_RGBA, GL_UNSIGNED_BYTE, raw);
if (glGenerateMipmap)
glGenerateMipmap(target);
glBindTexture(target, 0);
if (glFenceSync)
{
upload.fence = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
}
if (upload.fence == nullptr)
{
// no sync objects, or no fence made: the drawing context can only be sure of the image once the upload is done
glFinish();
}
else
{
// the drawing context asks for the fence, which signals only once it is on its way to the gpu
glFlush();
}
std::optional<texture_thread_upload> superseded;
{
std::scoped_lock lock{m_threaduploadmutex};
superseded = std::exchange(m_threadupload, upload);
}
if (superseded.has_value())
{
// the drawing thread didn't take it, so nothing drew from it
delete_thread_upload(superseded->id, superseded->fence);
}
}
bool opengl_texture::commit_thread_upload()
{
texture_thread_upload upload;
{
std::scoped_lock lock{m_threaduploadmutex};
if (false == m_threadupload.has_value())
return true;
// asked without waiting. the image goes in place only once the upload is done, so every context binding it after
// has it whole: the extra viewports and the windows of python.displaywindows draw in contexts of their own
if (m_threadupload->fence != nullptr)
{
// a failed query (GL_WAIT_FAILED) isn't asked again: the image would never go in place
if (glClientWaitSync(m_threadupload->fence, 0, 0) == GL_TIMEOUT_EXPIRED)
return false;
glDeleteSync(m_threadupload->fence);
}
upload = *m_threadupload;
m_threadupload.reset();
}
if (id != invalid_id)
glDeleteTextures(1, &id);
id = upload.id;
data_width = static_cast<unsigned int>(upload.width);
data_height = static_cast<unsigned int>(upload.height);
data_format = GL_SRGB8_ALPHA8;
data_components = GL_RGBA;
if (type == "internalsrc:")
{
is_static = true;
is_ready = true;
}
// the name deleted may come back for another texture, and a deleted texture isn't bound any more
for (auto &unit : units)
unit = -1;
return true;
}
void
opengl_texture::make_request() {

View File

@@ -12,6 +12,8 @@ http://mozilla.org/MPL/2.0/.
#include <atomic>
#include <istream>
#include <limits>
#include <mutex>
#include <optional>
#include "winheaders.h"
#include <string>
#include "model/ResourceManager.h"
@@ -20,6 +22,14 @@ http://mozilla.org/MPL/2.0/.
struct texture_request; // texture data read by a worker thread, with gfx.textures.streaming
// an image uploaded by another thread into a texture of its own, waiting for the drawing thread to put it in place
struct texture_thread_upload {
GLuint id { 0 };
GLsync fence { nullptr }; // set after the upload in the uploading context; none where the driver has no sync objects
size_t width { 0 };
size_t height { 0 };
};
struct opengl_texture : public ITexture {
static DDSURFACEDESC2 deserialize_ddsd(std::istream&);
static DDCOLORKEY deserialize_ddck(std::istream&);
@@ -93,6 +103,10 @@ struct opengl_texture : public ITexture {
virtual void make_from_memory(size_t width, size_t height, const uint8_t *data) override;
virtual void update_from_memory(size_t width, size_t height, const uint8_t *data) override;
// the image goes into a texture of its own, and the drawing thread swaps it in after the gpu has it (commit_thread_upload).
// the texture drawn from is never written by the uploading thread
void upload_from_thread(size_t width, size_t height, const uint8_t *data) override;
bool commit_thread_upload() override;
// members
static constexpr GLuint invalid_id { std::numeric_limits<GLuint>::max() }; // id value of texture without GL resource
@@ -111,6 +125,9 @@ struct opengl_texture : public ITexture {
// 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;
std::mutex m_threaduploadmutex;
std::optional<texture_thread_upload> m_threadupload; // guarded by m_threaduploadmutex
public:
// methods
void make_request();