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

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.
This commit is contained in:
maj00r
2026-11-18 17:08:37 +01:00
parent 8811ee5a76
commit 61d195d9fc
7 changed files with 61 additions and 48 deletions

View File

@@ -232,22 +232,31 @@ void render_task::upload() const
}
}
void render_task::upload_from_thread() const
void render_task::upload_from_thread()
{
m_uploadedbyworker = true;
if (Global.python_uploadmain && m_target && m_target->shared_tex)
{
m_target->shared_tex->upload_from_thread(m_target->width, m_target->height, reinterpret_cast<const uint8_t *>(m_target->image.data()));
}
}
void render_task::commit_upload() const
bool render_task::finish_upload() const
{
if (Global.python_uploadmain && m_target && m_target->shared_tex)
if (false == Global.python_uploadmain || nullptr == m_target || nullptr == m_target->shared_tex)
return true;
if (m_uploadedbyworker)
{
m_target->shared_tex->commit_thread_upload();
std::scoped_lock lock(m_target->mutex);
m_target->texture_id = m_target->shared_tex->get_id();
if (false == m_target->shared_tex->commit_thread_upload())
return false;
}
else
{
upload();
}
std::scoped_lock lock(m_target->mutex);
m_target->texture_id = m_target->shared_tex->get_id();
return true;
}
void render_task::cancel() const { /* nothing to do */ }
@@ -629,15 +638,8 @@ void python_taskqueue::update()
{
std::scoped_lock lock(m_uploadtasks.mutex);
for (auto const &task : m_uploadtasks.data)
{
if (Global.python_threadedupload)
task->commit_upload();
else
task->upload();
}
m_uploadtasks.data.clear();
// a task whose image a worker uploaded stays until the gpu has the image
std::erase_if(m_uploadtasks.data, [](std::shared_ptr<render_task> const &Task) { return Task->finish_upload(); });
}
void python_taskqueue::error()

View File

@@ -85,10 +85,11 @@ class render_task
void run();
// uploads the image on the drawing thread
void upload() const;
// uploads the image on a worker with a gl context of its own (python.threadedupload); commit_upload() puts it in place
void upload_from_thread() const;
// puts the image of upload_from_thread() in place, on the drawing thread
void commit_upload() const;
// uploads the image on a worker with a gl context of its own (python.threadedupload); finish_upload() puts it in place
void upload_from_thread();
// on the drawing thread: uploads the image, or puts in place the one a worker uploaded. false while the gpu hasn't got
// the latter yet, to be called again
bool finish_upload() const;
void cancel() const;
auto target() const -> std::shared_ptr<python_rt>
{
@@ -100,6 +101,8 @@ class render_task
PyObject *m_renderer{nullptr};
std::shared_ptr<dictionary_source> m_input{nullptr};
std::shared_ptr<python_rt> m_target{nullptr};
// set by upload_from_thread(); read by the drawing thread after the task was handed over under the lock of the queue
bool m_uploadedbyworker{false};
};
class python_taskqueue

View File

@@ -30,7 +30,7 @@ void texture_window_cursor_pos(GLFWwindow *win, double x, double y)
}
python_screen_viewer::python_screen_viewer(std::shared_ptr<python_rt> rt, std::shared_ptr<std::vector<glm::vec2>> touchlist, std::string const &surfacename)
: m_rt(rt), m_touchlist(touchlist)
: m_rt(rt), m_touchlist(touchlist), m_initialtexture(rt->shared_tex->get_id())
{
for (const auto &viewport : Global.python_viewports) {
@@ -90,8 +90,7 @@ void python_screen_viewer::threadfunc()
glActiveTexture(GL_TEXTURE0);
if (Global.python_sharectx) {
window->shared_texture = m_rt->shared_tex->get_id();
glBindTexture(GL_TEXTURE_2D, static_cast<GLuint>(window->shared_texture));
bind_shared_texture();
}
else {
GLuint tex;
@@ -148,7 +147,7 @@ void python_screen_viewer::threadfunc()
glfwMakeContextCurrent(window->window);
if (Global.python_sharectx)
bind_shared_texture(*window);
bind_shared_texture();
gl::program::unbind();
gl::buffer::unbind();
@@ -191,18 +190,16 @@ void python_screen_viewer::threadfunc()
}
}
void python_screen_viewer::bind_shared_texture(window_state &Window) const
void python_screen_viewer::bind_shared_texture() const
{
size_t texture;
{
std::scoped_lock guard(m_rt->mutex);
texture = m_rt->texture_id;
}
// each image uploaded by a worker comes in a texture of its own
if (texture == 0 || texture == Window.shared_texture)
return;
Window.shared_texture = texture;
glBindTexture(GL_TEXTURE_2D, static_cast<GLuint>(texture));
// bound each frame: each image uploaded by a worker comes in a texture of its own, and a name deleted comes back for
// a later one, so a name seen before may be another texture already
glBindTexture(GL_TEXTURE_2D, static_cast<GLuint>(texture != 0 ? texture : m_initialtexture));
}
void python_screen_viewer::notify_window_fb_size(GLFWwindow const *window, int w, int h) const

View File

@@ -18,7 +18,6 @@ class python_screen_viewer
std::unique_ptr<gl::program> shader;
std::chrono::high_resolution_clock::time_point timestamp;
size_t shared_texture{0}; // with python.sharectx, the texture of the screen bound in this window's context
window_state() = default;
~window_state();
@@ -32,10 +31,12 @@ class python_screen_viewer
gl::scene_ubs m_ubs;
std::atomic_bool m_exit = false;
// the texture of the screen when the viewer was made, read on the drawing thread: shown until an image is put in place
size_t m_initialtexture{0};
void threadfunc();
// with python.sharectx, binds the texture with the last image of the screen, in the context of the window made current
void bind_shared_texture(window_state &Window) const;
void bind_shared_texture() const;
public:
python_screen_viewer(std::shared_ptr<python_rt> rt, std::shared_ptr<std::vector<glm::vec2>> touchlist, std::string const &name);