mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-21 08:35:01 +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 of8811ee5a: - 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 of939514ea: - 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:
@@ -142,18 +142,24 @@ void render_task::run()
|
||||
}
|
||||
else
|
||||
{
|
||||
std::lock_guard guard(m_target->mutex);
|
||||
// the lock may be held by the drawing thread for a whole upload: waited for without the gil. the bytes stay
|
||||
// valid without it, the object is immutable and held by its reference here
|
||||
Py_BEGIN_ALLOW_THREADS
|
||||
{
|
||||
std::scoped_lock guard(m_target->mutex);
|
||||
|
||||
if (m_target->image.size() != expectedBytes)
|
||||
m_target->image.resize(expectedBytes);
|
||||
if (m_target->image.size() != expectedBytes)
|
||||
m_target->image.resize(expectedBytes);
|
||||
|
||||
std::memcpy(m_target->image.data(), pythonBufferPtr, expectedBytes);
|
||||
std::memcpy(m_target->image.data(), pythonBufferPtr, expectedBytes);
|
||||
|
||||
m_target->width = screenWidth;
|
||||
m_target->height = screenHeight;
|
||||
m_target->components = glComponents;
|
||||
m_target->format = glFormat;
|
||||
m_target->timestamp = std::chrono::high_resolution_clock::now();
|
||||
m_target->width = screenWidth;
|
||||
m_target->height = screenHeight;
|
||||
m_target->components = glComponents;
|
||||
m_target->format = glFormat;
|
||||
m_target->timestamp = std::chrono::high_resolution_clock::now();
|
||||
}
|
||||
Py_END_ALLOW_THREADS
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,6 +238,35 @@ void render_task::upload() 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()));
|
||||
}
|
||||
}
|
||||
|
||||
bool render_task::finish_upload() const
|
||||
{
|
||||
if (false == Global.python_uploadmain || nullptr == m_target || nullptr == m_target->shared_tex)
|
||||
return true;
|
||||
// the old texture is deleted and the new name published under the lock the windows of python.displaywindows bind under,
|
||||
// so none of them binds the old name once it is gone: a name bound before stays alive in its context while bound there
|
||||
std::scoped_lock lock(m_target->mutex);
|
||||
if (m_uploadedbyworker)
|
||||
{
|
||||
if (false == m_target->shared_tex->commit_thread_upload())
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
upload();
|
||||
}
|
||||
m_target->texture_id = m_target->shared_tex->get_id();
|
||||
return true;
|
||||
}
|
||||
|
||||
void render_task::cancel() const { /* nothing to do */ }
|
||||
|
||||
// initializes the module. returns true on success
|
||||
@@ -569,9 +604,11 @@ void python_taskqueue::run(GLFWwindow *Context, rendertask_sequence &Tasks, uplo
|
||||
// execute python code
|
||||
task->run();
|
||||
if (Context)
|
||||
task->upload();
|
||||
else
|
||||
{
|
||||
task->upload_from_thread();
|
||||
}
|
||||
{
|
||||
// the drawing thread uploads the image, or puts in place the one uploaded here
|
||||
std::scoped_lock lock(Upload_Tasks.mutex);
|
||||
Upload_Tasks.data.push_back(task);
|
||||
}
|
||||
@@ -609,10 +646,8 @@ void python_taskqueue::update()
|
||||
{
|
||||
std::scoped_lock lock(m_uploadtasks.mutex);
|
||||
|
||||
for (auto const &task : m_uploadtasks.data)
|
||||
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()
|
||||
|
||||
@@ -67,6 +67,9 @@ struct python_rt
|
||||
int width;
|
||||
int height;
|
||||
std::string image;
|
||||
// gl name of the texture with the last image put in place, for the windows of python.displaywindows (0: none yet). the image
|
||||
// uploaded by a worker gets a texture of its own, so the name changes with each one
|
||||
size_t texture_id{0};
|
||||
|
||||
std::chrono::high_resolution_clock::time_point timestamp;
|
||||
};
|
||||
@@ -80,7 +83,13 @@ class render_task
|
||||
render_task(PyObject *Renderer, std::shared_ptr<dictionary_source> Input, std::shared_ptr<python_rt> Target) : m_renderer(Renderer), m_input(Input), m_target(Target) {}
|
||||
// methods
|
||||
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); 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>
|
||||
{
|
||||
@@ -92,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
|
||||
|
||||
@@ -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,7 +90,7 @@ void python_screen_viewer::threadfunc()
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
|
||||
if (Global.python_sharectx) {
|
||||
glBindTexture(GL_TEXTURE_2D, static_cast<GLuint>(m_rt->shared_tex->get_id()));
|
||||
bind_shared_texture();
|
||||
}
|
||||
else {
|
||||
GLuint tex;
|
||||
@@ -146,6 +146,8 @@ void python_screen_viewer::threadfunc()
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(window->window);
|
||||
if (Global.python_sharectx)
|
||||
bind_shared_texture();
|
||||
gl::program::unbind();
|
||||
gl::buffer::unbind();
|
||||
|
||||
@@ -188,6 +190,16 @@ void python_screen_viewer::threadfunc()
|
||||
}
|
||||
}
|
||||
|
||||
void python_screen_viewer::bind_shared_texture() const
|
||||
{
|
||||
// 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. bound under the lock the drawing thread deletes
|
||||
// the old texture under, so the name read here is never one already gone
|
||||
std::scoped_lock guard(m_rt->mutex);
|
||||
size_t const texture = m_rt->texture_id;
|
||||
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
|
||||
{
|
||||
for (auto const &conf : m_windows) {
|
||||
|
||||
@@ -31,8 +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() const;
|
||||
|
||||
public:
|
||||
python_screen_viewer(std::shared_ptr<python_rt> rt, std::shared_ptr<std::vector<glm::vec2>> touchlist, std::string const &name);
|
||||
|
||||
Reference in New Issue
Block a user