16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-22 13:59:19 +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

@@ -796,6 +796,11 @@ global_settings::ConfigParse(cParser &Parser) {
Parser.getTokens(1); Parser.getTokens(1);
Parser >> python_threadedupload; Parser >> python_threadedupload;
} }
else if (token == "python.sharectx")
{
Parser.getTokens(1);
Parser >> python_sharectx;
}
else if (token == "python.vsync") else if (token == "python.vsync")
{ {
Parser.getTokens(1); Parser.getTokens(1);

View File

@@ -188,6 +188,7 @@ struct global_settings {
bool python_displaywindows = false; bool python_displaywindows = false;
bool python_threadedupload = true; bool python_threadedupload = true;
bool python_vsync = true; bool python_vsync = true;
bool python_sharectx = false;
int gfx_framebuffer_width = -1; int gfx_framebuffer_width = -1;
int gfx_framebuffer_height = -1; int gfx_framebuffer_height = -1;

View File

@@ -45,7 +45,7 @@ void render_task::run() {
// upload texture data // upload texture data
if( ( outputwidth != nullptr ) if( ( outputwidth != nullptr )
&& ( outputheight != nullptr ) && ( outputheight != nullptr )
&& m_target != -1) { && m_target) {
m_width = PyInt_AsLong( outputwidth ); m_width = PyInt_AsLong( outputwidth );
m_height = PyInt_AsLong( outputheight ); m_height = PyInt_AsLong( outputheight );
@@ -86,13 +86,29 @@ void render_task::upload()
{ {
if (m_image) if (m_image)
{ {
glBindTexture(GL_TEXTURE_2D, m_target); glBindTexture(GL_TEXTURE_2D, m_target->shared_tex);
glTexImage2D( glTexImage2D(
GL_TEXTURE_2D, 0, GL_TEXTURE_2D, 0,
m_format, m_format,
m_width, m_height, 0, m_width, m_height, 0,
m_components, GL_UNSIGNED_BYTE, m_image); m_components, GL_UNSIGNED_BYTE, m_image);
{
std::lock_guard<std::mutex> guard(m_target->mutex);
if (m_target->image)
delete[] m_target->image;
size_t size = m_width * m_height * (m_components == GL_RGB ? 3 : 4);
m_target->image = new unsigned char[size];
memcpy(m_target->image, m_image, size);
m_target->width = m_width;
m_target->height = m_height;
m_target->components = m_components;
m_target->format = m_format;
}
delete[] m_image; delete[] m_image;
if (Global.python_mipmaps) if (Global.python_mipmaps)

26
PyInt.h
View File

@@ -68,25 +68,43 @@ struct dictionary_source {
inline void insert( std::string const &Key, std::string const Value ) { strings.emplace_back( Key, Value ); } inline void insert( std::string const &Key, std::string const Value ) { strings.emplace_back( Key, Value ); }
}; };
// python rendertarget
struct python_rt {
std::mutex mutex;
GLuint shared_tex;
int format;
int components;
int width;
int height;
unsigned char *image = nullptr;
~python_rt() {
if (image)
delete[] image;
}
};
// TODO: extract common base and inherit specialization from it // TODO: extract common base and inherit specialization from it
class render_task { class render_task {
public: public:
// constructors // constructors
render_task( PyObject *Renderer, dictionary_source *Input, GLuint Target ) : render_task( PyObject *Renderer, dictionary_source *Input, std::shared_ptr<python_rt> Target ) :
m_renderer( Renderer ), m_input( Input ), m_target( Target ) m_renderer( Renderer ), m_input( Input ), m_target( Target )
{} {}
// methods // methods
void run(); void run();
void upload(); void upload();
void cancel(); void cancel();
auto target() const -> texture_handle { return m_target; } auto target() const -> std::shared_ptr<python_rt> { return m_target; }
private: private:
// members // members
PyObject *m_renderer {nullptr}; PyObject *m_renderer {nullptr};
dictionary_source *m_input { nullptr }; dictionary_source *m_input { nullptr };
GLuint m_target { 0 }; std::shared_ptr<python_rt> m_target { nullptr };
unsigned char *m_image = nullptr; unsigned char *m_image = nullptr;
int m_width, m_height; int m_width, m_height;
@@ -101,7 +119,7 @@ public:
std::string const &renderer; std::string const &renderer;
dictionary_source *input; dictionary_source *input;
GLuint target; std::shared_ptr<python_rt> target;
}; };
// constructors // constructors
python_taskqueue() = default; python_taskqueue() = default;

View File

@@ -278,7 +278,10 @@ opengl_texture::make_request() {
} }
} }
Application.request( { ToLower( components.front() ), dictionary, id } ); auto rt = std::make_shared<python_rt>();
rt->shared_tex = id;
Application.request( { ToLower( components.front() ), dictionary, rt } );
} }
void void

View File

@@ -6995,11 +6995,14 @@ bool TTrain::InitializeCab(int NewCabNo, std::string const &asFileName)
DynamicObject->asBaseDir + renderername : DynamicObject->asBaseDir + renderername :
renderername }; renderername };
auto rt = std::make_shared<python_rt>();
rt->shared_tex = tex->id;
// record renderer and material binding for future update requests // record renderer and material binding for future update requests
m_screens.emplace_back(rendererpath, tex->id, nullptr); m_screens.emplace_back(rendererpath, rt, nullptr);
if (Global.python_displaywindows) if (Global.python_displaywindows)
std::get<2>(m_screens.back()) = std::make_unique<python_screen_viewer>(tex->id, rendererpath); std::get<2>(m_screens.back()) = std::make_unique<python_screen_viewer>(rt, rendererpath);
} }
// btLampkaUnknown.Init("unknown",mdKabina,false); // btLampkaUnknown.Init("unknown",mdKabina,false);
} while (token != ""); } while (token != "");

View File

@@ -100,7 +100,7 @@ class TTrain
float hv_voltage; float hv_voltage;
std::array<float, 3> hv_current; std::array<float, 3> hv_current;
}; };
typedef std::tuple<std::string, GLuint, std::unique_ptr<python_screen_viewer>> screen_entry; typedef std::tuple<std::string, std::shared_ptr<python_rt>, std::unique_ptr<python_screen_viewer>> screen_entry;
typedef std::vector<screen_entry> screen_map; typedef std::vector<screen_entry> screen_map;
// methods // methods
@@ -672,7 +672,7 @@ private:
float m_mastercontrollerreturndelay { 0.f }; float m_mastercontrollerreturndelay { 0.f };
int iRadioChannel { 1 }; // numer aktualnego kana?u radiowego int iRadioChannel { 1 }; // numer aktualnego kana?u radiowego
screen_map m_screens; screen_map m_screens;
uint16_t vid { 0 }; uint16_t vid { 0 }; // train network recipient id
public: public:
float fPress[20][3]; // cisnienia dla wszystkich czlonow float fPress[20][3]; // cisnienia dla wszystkich czlonow

View File

@@ -470,7 +470,7 @@ void eu07_application::on_focus_change(bool focus) {
} }
GLFWwindow * GLFWwindow *
eu07_application::window( int const Windowindex, bool visible, int width, int height, GLFWmonitor *monitor, bool keep_ownership ) { eu07_application::window(int const Windowindex, bool visible, int width, int height, GLFWmonitor *monitor, bool keep_ownership , bool share_ctx) {
if( Windowindex >= 0 ) { if( Windowindex >= 0 ) {
return ( return (
@@ -489,8 +489,8 @@ eu07_application::window( int const Windowindex, bool visible, int width, int he
glfwWindowHint( GLFW_VISIBLE, visible ); glfwWindowHint( GLFW_VISIBLE, visible );
auto *childwindow = glfwCreateWindow( width, height, "eu07helper", monitor, auto *childwindow = glfwCreateWindow( width, height, "eu07window", monitor,
!m_windows.empty() ? m_windows.front() : nullptr); share_ctx ? m_windows.front() : nullptr);
if (!childwindow) if (!childwindow)
return nullptr; return nullptr;
@@ -652,7 +652,7 @@ eu07_application::init_glfw() {
glfwWindowHint( GLFW_SAMPLES, 1 << Global.iMultisampling ); glfwWindowHint( GLFW_SAMPLES, 1 << Global.iMultisampling );
} }
auto *win = window(-1, true, Global.iWindowWidth, Global.iWindowHeight, Global.bFullScreen ? monitor : nullptr); auto *win = window(-1, true, Global.iWindowWidth, Global.iWindowHeight, Global.bFullScreen ? monitor : nullptr, true, false);
if( win == nullptr ) { if( win == nullptr ) {
ErrorLog( "Bad init: failed to create glfw window" ); ErrorLog( "Bad init: failed to create glfw window" );

View File

@@ -78,7 +78,7 @@ public:
on_focus_change(bool focus); on_focus_change(bool focus);
// gives access to specified window, creates a new window if index == -1 // gives access to specified window, creates a new window if index == -1
GLFWwindow * GLFWwindow *
window(int const Windowindex = 0 , bool visible = false, int width = 1, int height = 1, GLFWmonitor *monitor = nullptr, bool keep_ownership = true); window(int const Windowindex = 0 , bool visible = false, int width = 1, int height = 1, GLFWmonitor *monitor = nullptr, bool keep_ownership = true, bool share_ctx = true);
GLFWmonitor * GLFWmonitor *
find_monitor(const std::string &str); find_monitor(const std::string &str);
// generate network sync verification number // generate network sync verification number

View File

@@ -11,30 +11,26 @@ void texture_window_fb_resize(GLFWwindow *win, int w, int h)
texwindow->notify_window_size(win, w, 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) { for (const auto &viewport : Global.python_viewports) {
if (viewport.surface == surfacename) { if (viewport.surface == surfacename) {
window_config conf; auto conf = std::make_unique<window_state>();
conf.size = viewport.size; conf->size = viewport.size;
conf.offset = viewport.offset; conf->offset = viewport.offset;
conf.scale = viewport.scale; conf->scale = viewport.scale;
conf.window = Application.window(-1, true, conf.size.x, conf.size.y, conf->window = Application.window(-1, true, conf->size.x, conf->size.y,
Application.find_monitor(viewport.monitor), false); Application.find_monitor(viewport.monitor), false, Global.python_sharectx);
glfwSetWindowUserPointer(conf.window, this); glfwSetWindowUserPointer(conf->window, this);
glfwSetFramebufferSizeCallback(conf.window, texture_window_fb_resize); glfwSetFramebufferSizeCallback(conf->window, texture_window_fb_resize);
m_windows.push_back(std::move(conf)); 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); 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_exit = true;
m_renderthread->join(); m_renderthread->join();
for (auto &window : m_windows)
glfwDestroyWindow(window.window);
} }
void python_screen_viewer::threadfunc() void python_screen_viewer::threadfunc()
{ {
for (auto &window : m_windows) { for (auto &window : m_windows) {
glfwMakeContextCurrent(window.window); glfwMakeContextCurrent(window->window);
glfwSwapInterval(Global.python_vsync ? 1 : 0); glfwSwapInterval(Global.python_vsync ? 1 : 0);
GLuint v; GLuint v;
@@ -58,32 +51,69 @@ void python_screen_viewer::threadfunc()
glBindVertexArray(v); glBindVertexArray(v);
glActiveTexture(GL_TEXTURE0); 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) if (!Global.gfx_usegles && !Global.gfx_shadergamma)
glEnable(GL_FRAMEBUFFER_SRGB); 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) while (!m_exit)
{ {
for (auto &window : m_windows) { for (auto &window : m_windows) {
glfwMakeContextCurrent(window.window); glfwMakeContextCurrent(window->window);
m_shader->unbind(); gl::program::unbind();
gl::buffer::unbind(); gl::buffer::unbind();
m_shader->bind(); window->shader->bind();
window.ubo->bind_uniform(); window->ubo->bind_uniform();
m_ubs.projection = glm::mat4(glm::mat3(glm::translate(glm::scale(glm::mat3(), 1.0f / window.scale), window.offset))); m_ubs.projection = glm::mat4(glm::mat3(glm::translate(glm::scale(glm::mat3(), 1.0f / window->scale), window->offset)));
window.ubo->update(m_ubs); 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); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 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) void python_screen_viewer::notify_window_size(GLFWwindow *window, int w, int h)
{ {
for (auto &conf : m_windows) { for (auto &conf : m_windows) {
if (conf.window == window) { if (conf->window == window) {
conf.size.x = w; conf->size.x = w;
conf.size.y = h; conf->size.y = h;
return; 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);
}

View File

@@ -1,23 +1,27 @@
#include "renderer.h" #include "renderer.h"
#include "PyInt.h"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
class python_screen_viewer class python_screen_viewer
{ {
struct window_config { struct window_state {
GLFWwindow *window; GLFWwindow *window = nullptr;
glm::ivec2 size; glm::ivec2 size;
glm::vec2 offset; glm::vec2 offset;
glm::vec2 scale; glm::vec2 scale;
std::unique_ptr<gl::ubo> ubo; std::unique_ptr<gl::ubo> ubo;
std::unique_ptr<gl::program> shader;
window_state() = default;
~window_state();
}; };
std::vector<window_config> m_windows; std::vector<std::unique_ptr<window_state>> m_windows;
GLuint m_source; std::shared_ptr<python_rt> m_rt;
std::shared_ptr<std::thread> m_renderthread; std::shared_ptr<std::thread> m_renderthread;
std::unique_ptr<gl::program> m_shader;
gl::scene_ubs m_ubs; gl::scene_ubs m_ubs;
std::atomic_bool m_exit = false; std::atomic_bool m_exit = false;
@@ -25,7 +29,7 @@ class python_screen_viewer
void threadfunc(); void threadfunc();
public: public:
python_screen_viewer(GLuint src, std::string name); python_screen_viewer(std::shared_ptr<python_rt> rt, std::string name);
~python_screen_viewer(); ~python_screen_viewer();
void notify_window_size(GLFWwindow *window, int w, int h); void notify_window_size(GLFWwindow *window, int w, int h);

View File

@@ -35,7 +35,7 @@ void ui::vehicleparams_panel::render_contents()
glm::vec2 uv0 = glm::vec2(proj * glm::vec3(0.0f, 1.0f, 1.0f)); glm::vec2 uv0 = glm::vec2(proj * glm::vec3(0.0f, 1.0f, 1.0f));
glm::vec2 uv1 = glm::vec2(proj * glm::vec3(1.0f, 0.0f, 1.0f)); glm::vec2 uv1 = glm::vec2(proj * glm::vec3(1.0f, 0.0f, 1.0f));
ImGui::Image(reinterpret_cast<void*>(std::get<GLuint>(entry)), ImVec2(500, 500 * aspect), ImVec2(uv0.x, uv0.y), ImVec2(uv1.x, uv1.y)); ImGui::Image(reinterpret_cast<void*>(std::get<1>(entry)->shared_tex), ImVec2(500, 500 * aspect), ImVec2(uv0.x, uv0.y), ImVec2(uv1.x, uv1.y));
} }
} }
} }