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

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.
This commit is contained in:
maj00r
2026-11-18 16:49:36 +01:00
parent 80fd5aad2b
commit 8811ee5a76
7 changed files with 169 additions and 4 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
@@ -734,6 +735,92 @@ void opengl_texture::update_from_memory(size_t width, size_t height, const uint8
unit = -1;
}
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);
}
else
{
// no sync objects: the drawing context can only be sure of the image once the upload is done
glFinish();
}
// the drawing context waits on the fence, which it can do only once the fence 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);
}
}
void opengl_texture::commit_thread_upload()
{
std::optional<texture_thread_upload> upload;
{
std::scoped_lock lock{m_threaduploadmutex};
upload = std::exchange(m_threadupload, std::nullopt);
}
if (false == upload.has_value())
return;
if (upload->fence != nullptr)
{
// the commands that follow wait on the gpu for the upload to be done, the cpu goes on
glWaitSync(upload->fence, 0, GL_TIMEOUT_IGNORED);
glDeleteSync(upload->fence);
}
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;
}
void
opengl_texture::make_request() {