mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-21 21:24:58 +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. * 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>
274 lines
7.6 KiB
C++
274 lines
7.6 KiB
C++
#include "stdafx.h"
|
|
#include "scripting/pythonscreenviewer.h"
|
|
#include "application/application.h"
|
|
#include "gl/shader.h"
|
|
#include "gl/vao.h"
|
|
#include "utilities/Logs.h"
|
|
|
|
void texture_window_resize(GLFWwindow *win, int w, int h)
|
|
{
|
|
auto texwindow = (python_screen_viewer*)glfwGetWindowUserPointer(win);
|
|
texwindow->notify_window_size(win, w, h);
|
|
}
|
|
|
|
void texture_window_fb_resize(GLFWwindow *win, int w, int h)
|
|
{
|
|
auto texwindow = (python_screen_viewer*)glfwGetWindowUserPointer(win);
|
|
texwindow->notify_window_fb_size(win, w, h);
|
|
}
|
|
|
|
void texture_window_mouse_button(GLFWwindow *win, int button, int action, int /*mods*/)
|
|
{
|
|
auto texwindow = (python_screen_viewer*)glfwGetWindowUserPointer(win);
|
|
texwindow->notify_click(win, button, action);
|
|
}
|
|
|
|
void texture_window_cursor_pos(GLFWwindow *win, double x, double y)
|
|
{
|
|
auto texwindow = (python_screen_viewer*)glfwGetWindowUserPointer(win);
|
|
texwindow->notify_cursor_pos(win, x, 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_initialtexture(rt->shared_tex->get_id())
|
|
{
|
|
|
|
for (const auto &viewport : Global.python_viewports) {
|
|
if (viewport.surface == surfacename) {
|
|
auto conf = std::make_unique<window_state>();
|
|
conf->window_size = viewport.size;
|
|
conf->offset = viewport.offset;
|
|
conf->scale = viewport.scale;
|
|
|
|
GLFWmonitor *monitor = Application.find_monitor(viewport.monitor);
|
|
if (!monitor && viewport.monitor != "window")
|
|
continue;
|
|
|
|
conf->window = Application.window(-1, true, conf->window_size.x, conf->window_size.y,
|
|
monitor, false, Global.python_sharectx);
|
|
|
|
{
|
|
int w;
|
|
int h;
|
|
glfwGetWindowSize(conf->window, &w, &h);
|
|
conf->window_size = glm::ivec2(w, h);
|
|
glfwGetFramebufferSize(conf->window, &w, &h);
|
|
conf->fb_size = glm::ivec2(w, h);
|
|
}
|
|
|
|
glfwSetWindowUserPointer(conf->window, this);
|
|
glfwSetWindowSizeCallback(conf->window, texture_window_resize);
|
|
glfwSetFramebufferSizeCallback(conf->window, texture_window_fb_resize);
|
|
glfwSetMouseButtonCallback(conf->window, texture_window_mouse_button);
|
|
glfwSetCursorPosCallback(conf->window, texture_window_cursor_pos);
|
|
|
|
m_windows.push_back(std::move(conf));
|
|
}
|
|
}
|
|
|
|
if (!m_windows.empty())
|
|
m_renderthread = std::make_unique<std::thread>(&python_screen_viewer::threadfunc, this);
|
|
}
|
|
|
|
python_screen_viewer::~python_screen_viewer()
|
|
{
|
|
m_exit = true;
|
|
if (m_renderthread)
|
|
m_renderthread->join();
|
|
}
|
|
|
|
void python_screen_viewer::threadfunc()
|
|
{
|
|
for (auto const &window : m_windows) {
|
|
glfwMakeContextCurrent(window->window);
|
|
|
|
glfwSwapInterval(Global.python_vsync ? 1 : 0);
|
|
GLuint v;
|
|
glGenVertexArrays(1, &v);
|
|
glBindVertexArray(v);
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
if (Global.python_sharectx) {
|
|
bind_shared_texture();
|
|
}
|
|
else {
|
|
GLuint tex;
|
|
glGenTextures(1, &tex);
|
|
glBindTexture(GL_TEXTURE_2D, tex);
|
|
}
|
|
|
|
if (!Global.gfx_usegles && !Global.gfx_shadergamma)
|
|
glEnable(GL_FRAMEBUFFER_SRGB);
|
|
|
|
gl::program::unbind();
|
|
gl::buffer::unbind();
|
|
|
|
window->ubo = std::make_unique<gl::ubo>(sizeof(gl::scene_ubs), 0, GL_STREAM_DRAW);
|
|
|
|
gl::shader vert("texturewindow.vert");
|
|
gl::shader frag("texturewindow.frag");
|
|
window->shader = std::make_unique<gl::program>(std::vector<std::reference_wrapper<const gl::shader>>({vert, frag}));
|
|
}
|
|
|
|
while (!m_exit)
|
|
{
|
|
auto start_time = std::chrono::high_resolution_clock::now();
|
|
|
|
for (auto const &window : m_windows) {
|
|
|
|
unsigned char *image = nullptr;
|
|
int format;
|
|
int components;
|
|
int width;
|
|
int height;
|
|
|
|
if (!Global.python_sharectx) {
|
|
std::scoped_lock guard(m_rt->mutex);
|
|
|
|
if (window->timestamp == m_rt->timestamp)
|
|
continue;
|
|
|
|
window->timestamp = m_rt->timestamp;
|
|
|
|
if (m_rt->image.empty())
|
|
continue;
|
|
|
|
format = m_rt->format;
|
|
components = m_rt->components;
|
|
width = m_rt->width;
|
|
height = m_rt->height;
|
|
|
|
size_t size = width * height * (components == GL_RGB ? 3 : 4);
|
|
|
|
image = new unsigned char[size];
|
|
memcpy(image, m_rt->image.data(), size);
|
|
}
|
|
|
|
glfwMakeContextCurrent(window->window);
|
|
if (Global.python_sharectx)
|
|
bind_shared_texture();
|
|
gl::program::unbind();
|
|
gl::buffer::unbind();
|
|
|
|
window->shader->bind();
|
|
window->ubo->bind_uniform();
|
|
|
|
m_ubs.projection = glm::mat4(glm::mat3(glm::translate(glm::scale(glm::mat3(), 1.0f / window->scale), window->offset)));
|
|
window->ubo->update(m_ubs);
|
|
|
|
if (image) {
|
|
glTexImage2D(
|
|
GL_TEXTURE_2D, 0,
|
|
format,
|
|
width, height, 0,
|
|
components, GL_UNSIGNED_BYTE, image);
|
|
|
|
delete[] image;
|
|
|
|
if (Global.python_mipmaps) {
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
|
glGenerateMipmap(GL_TEXTURE_2D);
|
|
}
|
|
else {
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
}
|
|
}
|
|
|
|
glViewport(0, 0, window->fb_size.x, window->fb_size.y);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
glfwSwapBuffers(window->window);
|
|
}
|
|
|
|
auto frametime = std::chrono::high_resolution_clock::now() - start_time;
|
|
|
|
if ((Global.python_minframetime - frametime).count() > 0.0f)
|
|
std::this_thread::sleep_for(Global.python_minframetime - frametime);
|
|
}
|
|
}
|
|
|
|
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) {
|
|
if (conf->window == window) {
|
|
conf->fb_size.x = w;
|
|
conf->fb_size.y = h;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
void python_screen_viewer::notify_window_size(GLFWwindow const *window, int w, int h) const
|
|
{
|
|
for (auto const &conf : m_windows) {
|
|
if (conf->window == window) {
|
|
conf->window_size.x = w;
|
|
conf->window_size.y = h;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
void python_screen_viewer::notify_cursor_pos(GLFWwindow const *window, double x, double y) const
|
|
{
|
|
for (auto const &conf : m_windows) {
|
|
if (conf->window == window) {
|
|
conf->cursor_pos.x = static_cast<int>(x);
|
|
conf->cursor_pos.y = static_cast<int>(y);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
void python_screen_viewer::notify_click(GLFWwindow const *window, int button, int action) const
|
|
{
|
|
if (button != GLFW_MOUSE_BUTTON_LEFT || action != GLFW_PRESS)
|
|
return;
|
|
|
|
for (auto const &conf : m_windows) {
|
|
if (conf->window == window) {
|
|
auto pos = glm::vec2(conf->cursor_pos) / glm::vec2(conf->window_size);
|
|
pos.y = 1.0f - pos.y;
|
|
pos = (pos + conf->offset) / conf->scale;
|
|
|
|
m_touchlist->emplace_back(pos);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
python_screen_viewer::window_state::~window_state()
|
|
{
|
|
if (!window)
|
|
return;
|
|
|
|
if (!Global.python_sharectx) {
|
|
GLFWwindow *current = glfwGetCurrentContext();
|
|
|
|
glfwMakeContextCurrent(window);
|
|
|
|
ubo = nullptr;
|
|
shader = nullptr;
|
|
|
|
gl::program::unbind();
|
|
gl::buffer::unbind();
|
|
|
|
glfwMakeContextCurrent(current);
|
|
}
|
|
|
|
glfwDestroyWindow(window);
|
|
}
|