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:
@@ -21,6 +21,14 @@ struct ITexture
|
|||||||
virtual void set_components_hint(int hint) = 0;
|
virtual void set_components_hint(int hint) = 0;
|
||||||
virtual void make_from_memory(size_t width, size_t height, const uint8_t *data) = 0;
|
virtual void make_from_memory(size_t width, size_t height, const uint8_t *data) = 0;
|
||||||
virtual void update_from_memory(size_t width, size_t height, const uint8_t *data) = 0;
|
virtual void update_from_memory(size_t width, size_t height, const uint8_t *data) = 0;
|
||||||
|
// uploads an image from a thread with a gl context of its own, sharing the objects with the drawing one. a texture that
|
||||||
|
// can keeps the image apart until commit_thread_upload(), called on the drawing thread, puts it in place
|
||||||
|
virtual void upload_from_thread(size_t width, size_t height, const uint8_t *data)
|
||||||
|
{
|
||||||
|
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 */ }
|
||||||
static ITexture *null_texture()
|
static ITexture *null_texture()
|
||||||
{
|
{
|
||||||
struct NullTexture : public ITexture
|
struct NullTexture : public ITexture
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ http://mozilla.org/MPL/2.0/.
|
|||||||
#include "winheaders.h"
|
#include "winheaders.h"
|
||||||
|
|
||||||
#include <bit>
|
#include <bit>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
#define EU07_DEFERRED_TEXTURE_UPLOAD
|
#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;
|
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
|
void
|
||||||
opengl_texture::make_request() {
|
opengl_texture::make_request() {
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ http://mozilla.org/MPL/2.0/.
|
|||||||
|
|
||||||
#include <istream>
|
#include <istream>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
#include <mutex>
|
||||||
|
#include <optional>
|
||||||
#include "winheaders.h"
|
#include "winheaders.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "model/ResourceManager.h"
|
#include "model/ResourceManager.h"
|
||||||
@@ -19,6 +21,14 @@ http://mozilla.org/MPL/2.0/.
|
|||||||
|
|
||||||
struct texture_request; // texture data read by a worker thread, with gfx.textures.streaming
|
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 {
|
struct opengl_texture : public ITexture {
|
||||||
static DDSURFACEDESC2 deserialize_ddsd(std::istream&);
|
static DDSURFACEDESC2 deserialize_ddsd(std::istream&);
|
||||||
static DDCOLORKEY deserialize_ddck(std::istream&);
|
static DDCOLORKEY deserialize_ddck(std::istream&);
|
||||||
@@ -85,6 +95,10 @@ struct opengl_texture : public ITexture {
|
|||||||
|
|
||||||
virtual void make_from_memory(size_t width, size_t height, const uint8_t *data) override;
|
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;
|
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;
|
||||||
|
void commit_thread_upload() override;
|
||||||
|
|
||||||
// members
|
// members
|
||||||
static constexpr GLuint invalid_id { std::numeric_limits<GLuint>::max() }; // id value of texture without GL resource
|
static constexpr GLuint invalid_id { std::numeric_limits<GLuint>::max() }; // id value of texture without GL resource
|
||||||
@@ -101,6 +115,9 @@ struct opengl_texture : public ITexture {
|
|||||||
static std::array<GLuint, gl::MAX_TEXTURES + gl::HELPER_TEXTURES> units;
|
static std::array<GLuint, gl::MAX_TEXTURES + gl::HELPER_TEXTURES> units;
|
||||||
static GLint m_activeunit;
|
static GLint m_activeunit;
|
||||||
|
|
||||||
|
std::mutex m_threaduploadmutex;
|
||||||
|
std::optional<texture_thread_upload> m_threadupload; // guarded by m_threaduploadmutex
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// methods
|
// methods
|
||||||
void make_request();
|
void make_request();
|
||||||
|
|||||||
@@ -232,6 +232,24 @@ void render_task::upload() const
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void render_task::upload_from_thread() const
|
||||||
|
{
|
||||||
|
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
|
||||||
|
{
|
||||||
|
if (Global.python_uploadmain && m_target && m_target->shared_tex)
|
||||||
|
{
|
||||||
|
m_target->shared_tex->commit_thread_upload();
|
||||||
|
std::scoped_lock lock(m_target->mutex);
|
||||||
|
m_target->texture_id = m_target->shared_tex->get_id();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void render_task::cancel() const { /* nothing to do */ }
|
void render_task::cancel() const { /* nothing to do */ }
|
||||||
|
|
||||||
// initializes the module. returns true on success
|
// initializes the module. returns true on success
|
||||||
@@ -569,9 +587,11 @@ void python_taskqueue::run(GLFWwindow *Context, rendertask_sequence &Tasks, uplo
|
|||||||
// execute python code
|
// execute python code
|
||||||
task->run();
|
task->run();
|
||||||
if (Context)
|
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);
|
std::scoped_lock lock(Upload_Tasks.mutex);
|
||||||
Upload_Tasks.data.push_back(task);
|
Upload_Tasks.data.push_back(task);
|
||||||
}
|
}
|
||||||
@@ -610,7 +630,12 @@ void python_taskqueue::update()
|
|||||||
std::scoped_lock lock(m_uploadtasks.mutex);
|
std::scoped_lock lock(m_uploadtasks.mutex);
|
||||||
|
|
||||||
for (auto const &task : m_uploadtasks.data)
|
for (auto const &task : m_uploadtasks.data)
|
||||||
task->upload();
|
{
|
||||||
|
if (Global.python_threadedupload)
|
||||||
|
task->commit_upload();
|
||||||
|
else
|
||||||
|
task->upload();
|
||||||
|
}
|
||||||
|
|
||||||
m_uploadtasks.data.clear();
|
m_uploadtasks.data.clear();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,6 +67,9 @@ struct python_rt
|
|||||||
int width;
|
int width;
|
||||||
int height;
|
int height;
|
||||||
std::string image;
|
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;
|
std::chrono::high_resolution_clock::time_point timestamp;
|
||||||
};
|
};
|
||||||
@@ -80,7 +83,12 @@ 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) {}
|
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
|
// methods
|
||||||
void run();
|
void run();
|
||||||
|
// uploads the image on the drawing thread
|
||||||
void upload() const;
|
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;
|
||||||
void cancel() const;
|
void cancel() const;
|
||||||
auto target() const -> std::shared_ptr<python_rt>
|
auto target() const -> std::shared_ptr<python_rt>
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -90,7 +90,8 @@ void python_screen_viewer::threadfunc()
|
|||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
|
|
||||||
if (Global.python_sharectx) {
|
if (Global.python_sharectx) {
|
||||||
glBindTexture(GL_TEXTURE_2D, static_cast<GLuint>(m_rt->shared_tex->get_id()));
|
window->shared_texture = m_rt->shared_tex->get_id();
|
||||||
|
glBindTexture(GL_TEXTURE_2D, static_cast<GLuint>(window->shared_texture));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
GLuint tex;
|
GLuint tex;
|
||||||
@@ -146,6 +147,8 @@ void python_screen_viewer::threadfunc()
|
|||||||
}
|
}
|
||||||
|
|
||||||
glfwMakeContextCurrent(window->window);
|
glfwMakeContextCurrent(window->window);
|
||||||
|
if (Global.python_sharectx)
|
||||||
|
bind_shared_texture(*window);
|
||||||
gl::program::unbind();
|
gl::program::unbind();
|
||||||
gl::buffer::unbind();
|
gl::buffer::unbind();
|
||||||
|
|
||||||
@@ -188,6 +191,20 @@ void python_screen_viewer::threadfunc()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void python_screen_viewer::bind_shared_texture(window_state &Window) 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));
|
||||||
|
}
|
||||||
|
|
||||||
void python_screen_viewer::notify_window_fb_size(GLFWwindow const *window, int w, int h) const
|
void python_screen_viewer::notify_window_fb_size(GLFWwindow const *window, int w, int h) const
|
||||||
{
|
{
|
||||||
for (auto const &conf : m_windows) {
|
for (auto const &conf : m_windows) {
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ class python_screen_viewer
|
|||||||
std::unique_ptr<gl::program> shader;
|
std::unique_ptr<gl::program> shader;
|
||||||
|
|
||||||
std::chrono::high_resolution_clock::time_point timestamp;
|
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() = default;
|
||||||
~window_state();
|
~window_state();
|
||||||
@@ -33,6 +34,8 @@ class python_screen_viewer
|
|||||||
std::atomic_bool m_exit = false;
|
std::atomic_bool m_exit = false;
|
||||||
|
|
||||||
void threadfunc();
|
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;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
python_screen_viewer(std::shared_ptr<python_rt> rt, std::shared_ptr<std::vector<glm::vec2>> touchlist, std::string const &name);
|
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