diff --git a/global_include/interfaces/ITexture.h b/global_include/interfaces/ITexture.h index ead1c6335..f5b4d29a5 100644 --- a/global_include/interfaces/ITexture.h +++ b/global_include/interfaces/ITexture.h @@ -27,8 +27,13 @@ struct ITexture { update_from_memory(width, height, data); } - // puts in place the image of the last upload_from_thread(), once the gpu has it. called on the drawing thread - virtual void commit_thread_upload() { /* the image went in place right away */ } + // puts in place the image of the last upload_from_thread() once the gpu has it, without waiting for it. called on the + // drawing thread; false while the upload isn't done yet, to be asked again later + virtual bool commit_thread_upload() + { + // the image went in place right away + return true; + } static ITexture *null_texture() { struct NullTexture : public ITexture diff --git a/model/Texture.cpp b/model/Texture.cpp index 779695fa9..06351a6d0 100644 --- a/model/Texture.cpp +++ b/model/Texture.cpp @@ -788,27 +788,31 @@ void opengl_texture::upload_from_thread(size_t width, size_t height, const uint8 } } -void opengl_texture::commit_thread_upload() +bool opengl_texture::commit_thread_upload() { - std::optional upload; + texture_thread_upload upload; { std::scoped_lock lock{m_threaduploadmutex}; - upload = std::exchange(m_threadupload, std::nullopt); + 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 (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(upload->width); - data_height = static_cast(upload->height); + id = upload.id; + data_width = static_cast(upload.width); + data_height = static_cast(upload.height); data_format = GL_SRGB8_ALPHA8; data_components = GL_RGBA; if (type == "internalsrc:") @@ -819,6 +823,7 @@ void opengl_texture::commit_thread_upload() // 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 diff --git a/model/Texture.h b/model/Texture.h index 0a86fe4bc..18e0c1e6a 100644 --- a/model/Texture.h +++ b/model/Texture.h @@ -98,7 +98,7 @@ struct opengl_texture : public ITexture { // 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; - void commit_thread_upload() override; + bool commit_thread_upload() override; // members static constexpr GLuint invalid_id { std::numeric_limits::max() }; // id value of texture without GL resource diff --git a/scripting/PyInt.cpp b/scripting/PyInt.cpp index b82ff73ce..e5cfbccfb 100644 --- a/scripting/PyInt.cpp +++ b/scripting/PyInt.cpp @@ -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(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 const &Task) { return Task->finish_upload(); }); } void python_taskqueue::error() diff --git a/scripting/PyInt.h b/scripting/PyInt.h index 866368b07..3259b418a 100644 --- a/scripting/PyInt.h +++ b/scripting/PyInt.h @@ -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 { @@ -100,6 +101,8 @@ class render_task PyObject *m_renderer{nullptr}; std::shared_ptr m_input{nullptr}; std::shared_ptr 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 diff --git a/scripting/pythonscreenviewer.cpp b/scripting/pythonscreenviewer.cpp index da461649e..99e492963 100644 --- a/scripting/pythonscreenviewer.cpp +++ b/scripting/pythonscreenviewer.cpp @@ -30,7 +30,7 @@ void texture_window_cursor_pos(GLFWwindow *win, double x, double y) } python_screen_viewer::python_screen_viewer(std::shared_ptr rt, std::shared_ptr> 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(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(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(texture != 0 ? texture : m_initialtexture)); } void python_screen_viewer::notify_window_fb_size(GLFWwindow const *window, int w, int h) const diff --git a/scripting/pythonscreenviewer.h b/scripting/pythonscreenviewer.h index 1d75d4d22..41b52a7e2 100644 --- a/scripting/pythonscreenviewer.h +++ b/scripting/pythonscreenviewer.h @@ -18,7 +18,6 @@ class python_screen_viewer std::unique_ptr 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 rt, std::shared_ptr> touchlist, std::string const &name);