16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-19 06:29:18 +02:00

python screen windows without context sharing

This commit is contained in:
milek7
2019-03-18 17:02:54 +01:00
parent efb041aaa0
commit 99e7ef5513
12 changed files with 156 additions and 54 deletions

View File

@@ -11,30 +11,26 @@ void texture_window_fb_resize(GLFWwindow *win, int w, int h)
texwindow->notify_window_size(win, w, h);
}
python_screen_viewer::python_screen_viewer(GLuint src, std::string surfacename)
python_screen_viewer::python_screen_viewer(std::shared_ptr<python_rt> rt, std::string surfacename)
{
m_source = src;
m_rt = rt;
for (const auto &viewport : Global.python_viewports) {
if (viewport.surface == surfacename) {
window_config conf;
conf.size = viewport.size;
conf.offset = viewport.offset;
conf.scale = viewport.scale;
conf.window = Application.window(-1, true, conf.size.x, conf.size.y,
Application.find_monitor(viewport.monitor), false);
auto conf = std::make_unique<window_state>();
conf->size = viewport.size;
conf->offset = viewport.offset;
conf->scale = viewport.scale;
conf->window = Application.window(-1, true, conf->size.x, conf->size.y,
Application.find_monitor(viewport.monitor), false, Global.python_sharectx);
glfwSetWindowUserPointer(conf.window, this);
glfwSetFramebufferSizeCallback(conf.window, texture_window_fb_resize);
glfwSetWindowUserPointer(conf->window, this);
glfwSetFramebufferSizeCallback(conf->window, texture_window_fb_resize);
m_windows.push_back(std::move(conf));
}
}
gl::shader vert("texturewindow.vert");
gl::shader frag("texturewindow.frag");
m_shader = std::make_unique<gl::program>(std::vector<std::reference_wrapper<const gl::shader>>({vert, frag}));
m_renderthread = std::make_unique<std::thread>(&python_screen_viewer::threadfunc, this);
}
@@ -42,15 +38,12 @@ python_screen_viewer::~python_screen_viewer()
{
m_exit = true;
m_renderthread->join();
for (auto &window : m_windows)
glfwDestroyWindow(window.window);
}
void python_screen_viewer::threadfunc()
{
for (auto &window : m_windows) {
glfwMakeContextCurrent(window.window);
glfwMakeContextCurrent(window->window);
glfwSwapInterval(Global.python_vsync ? 1 : 0);
GLuint v;
@@ -58,32 +51,69 @@ void python_screen_viewer::threadfunc()
glBindVertexArray(v);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_source);
if (Global.python_sharectx) {
glBindTexture(GL_TEXTURE_2D, m_rt->shared_tex);
}
else {
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
}
if (!Global.gfx_usegles && !Global.gfx_shadergamma)
glEnable(GL_FRAMEBUFFER_SRGB);
window.ubo = std::make_unique<gl::ubo>(sizeof(gl::scene_ubs), 0, GL_STREAM_DRAW);
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)
{
for (auto &window : m_windows) {
glfwMakeContextCurrent(window.window);
m_shader->unbind();
glfwMakeContextCurrent(window->window);
gl::program::unbind();
gl::buffer::unbind();
m_shader->bind();
window.ubo->bind_uniform();
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);
m_ubs.projection = glm::mat4(glm::mat3(glm::translate(glm::scale(glm::mat3(), 1.0f / window->scale), window->offset)));
window->ubo->update(m_ubs);
glViewport(0, 0, window.size.x, window.size.y);
if (!Global.python_sharectx) {
std::lock_guard<std::mutex> guard(m_rt->mutex);
if (!m_rt->image)
continue;
glTexImage2D(
GL_TEXTURE_2D, 0,
m_rt->format,
m_rt->width, m_rt->height, 0,
m_rt->components, GL_UNSIGNED_BYTE, m_rt->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->size.x, window->size.y);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glfwSwapBuffers(window.window);
glfwSwapBuffers(window->window);
}
}
}
@@ -91,10 +121,32 @@ void python_screen_viewer::threadfunc()
void python_screen_viewer::notify_window_size(GLFWwindow *window, int w, int h)
{
for (auto &conf : m_windows) {
if (conf.window == window) {
conf.size.x = w;
conf.size.y = h;
if (conf->window == window) {
conf->size.x = w;
conf->size.y = h;
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);
}