mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-03-22 15:05:03 +01:00
python screen windows without context sharing
This commit is contained in:
@@ -796,6 +796,11 @@ global_settings::ConfigParse(cParser &Parser) {
|
||||
Parser.getTokens(1);
|
||||
Parser >> python_threadedupload;
|
||||
}
|
||||
else if (token == "python.sharectx")
|
||||
{
|
||||
Parser.getTokens(1);
|
||||
Parser >> python_sharectx;
|
||||
}
|
||||
else if (token == "python.vsync")
|
||||
{
|
||||
Parser.getTokens(1);
|
||||
|
||||
@@ -188,6 +188,7 @@ struct global_settings {
|
||||
bool python_displaywindows = false;
|
||||
bool python_threadedupload = true;
|
||||
bool python_vsync = true;
|
||||
bool python_sharectx = false;
|
||||
|
||||
int gfx_framebuffer_width = -1;
|
||||
int gfx_framebuffer_height = -1;
|
||||
|
||||
20
PyInt.cpp
20
PyInt.cpp
@@ -45,7 +45,7 @@ void render_task::run() {
|
||||
// upload texture data
|
||||
if( ( outputwidth != nullptr )
|
||||
&& ( outputheight != nullptr )
|
||||
&& m_target != -1) {
|
||||
&& m_target) {
|
||||
m_width = PyInt_AsLong( outputwidth );
|
||||
m_height = PyInt_AsLong( outputheight );
|
||||
|
||||
@@ -86,13 +86,29 @@ void render_task::upload()
|
||||
{
|
||||
if (m_image)
|
||||
{
|
||||
glBindTexture(GL_TEXTURE_2D, m_target);
|
||||
glBindTexture(GL_TEXTURE_2D, m_target->shared_tex);
|
||||
glTexImage2D(
|
||||
GL_TEXTURE_2D, 0,
|
||||
m_format,
|
||||
m_width, m_height, 0,
|
||||
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;
|
||||
|
||||
if (Global.python_mipmaps)
|
||||
|
||||
26
PyInt.h
26
PyInt.h
@@ -68,25 +68,43 @@ struct dictionary_source {
|
||||
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
|
||||
class render_task {
|
||||
|
||||
public:
|
||||
// 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 )
|
||||
{}
|
||||
// methods
|
||||
void run();
|
||||
void upload();
|
||||
void cancel();
|
||||
auto target() const -> texture_handle { return m_target; }
|
||||
auto target() const -> std::shared_ptr<python_rt> { return m_target; }
|
||||
|
||||
private:
|
||||
// members
|
||||
PyObject *m_renderer {nullptr};
|
||||
dictionary_source *m_input { nullptr };
|
||||
GLuint m_target { 0 };
|
||||
std::shared_ptr<python_rt> m_target { nullptr };
|
||||
|
||||
unsigned char *m_image = nullptr;
|
||||
int m_width, m_height;
|
||||
@@ -101,7 +119,7 @@ public:
|
||||
|
||||
std::string const &renderer;
|
||||
dictionary_source *input;
|
||||
GLuint target;
|
||||
std::shared_ptr<python_rt> target;
|
||||
};
|
||||
// constructors
|
||||
python_taskqueue() = default;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -6995,11 +6995,14 @@ bool TTrain::InitializeCab(int NewCabNo, std::string const &asFileName)
|
||||
DynamicObject->asBaseDir + renderername :
|
||||
renderername };
|
||||
|
||||
auto rt = std::make_shared<python_rt>();
|
||||
rt->shared_tex = tex->id;
|
||||
|
||||
// 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)
|
||||
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);
|
||||
} while (token != "");
|
||||
|
||||
4
Train.h
4
Train.h
@@ -100,7 +100,7 @@ class TTrain
|
||||
float hv_voltage;
|
||||
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;
|
||||
|
||||
// methods
|
||||
@@ -672,7 +672,7 @@ private:
|
||||
float m_mastercontrollerreturndelay { 0.f };
|
||||
int iRadioChannel { 1 }; // numer aktualnego kana?u radiowego
|
||||
screen_map m_screens;
|
||||
uint16_t vid { 0 };
|
||||
uint16_t vid { 0 }; // train network recipient id
|
||||
|
||||
public:
|
||||
float fPress[20][3]; // cisnienia dla wszystkich czlonow
|
||||
|
||||
@@ -470,7 +470,7 @@ void eu07_application::on_focus_change(bool focus) {
|
||||
}
|
||||
|
||||
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 ) {
|
||||
return (
|
||||
@@ -489,8 +489,8 @@ eu07_application::window( int const Windowindex, bool visible, int width, int he
|
||||
|
||||
glfwWindowHint( GLFW_VISIBLE, visible );
|
||||
|
||||
auto *childwindow = glfwCreateWindow( width, height, "eu07helper", monitor,
|
||||
!m_windows.empty() ? m_windows.front() : nullptr);
|
||||
auto *childwindow = glfwCreateWindow( width, height, "eu07window", monitor,
|
||||
share_ctx ? m_windows.front() : nullptr);
|
||||
if (!childwindow)
|
||||
return nullptr;
|
||||
|
||||
@@ -652,7 +652,7 @@ eu07_application::init_glfw() {
|
||||
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 ) {
|
||||
ErrorLog( "Bad init: failed to create glfw window" );
|
||||
|
||||
@@ -78,7 +78,7 @@ public:
|
||||
on_focus_change(bool focus);
|
||||
// gives access to specified window, creates a new window if index == -1
|
||||
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 *
|
||||
find_monitor(const std::string &str);
|
||||
// generate network sync verification number
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -1,23 +1,27 @@
|
||||
#include "renderer.h"
|
||||
#include "PyInt.h"
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
class python_screen_viewer
|
||||
{
|
||||
struct window_config {
|
||||
GLFWwindow *window;
|
||||
struct window_state {
|
||||
GLFWwindow *window = nullptr;
|
||||
glm::ivec2 size;
|
||||
|
||||
glm::vec2 offset;
|
||||
glm::vec2 scale;
|
||||
|
||||
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::unique_ptr<gl::program> m_shader;
|
||||
gl::scene_ubs m_ubs;
|
||||
|
||||
std::atomic_bool m_exit = false;
|
||||
@@ -25,7 +29,7 @@ class python_screen_viewer
|
||||
void threadfunc();
|
||||
|
||||
public:
|
||||
python_screen_viewer(GLuint src, std::string name);
|
||||
python_screen_viewer(std::shared_ptr<python_rt> rt, std::string name);
|
||||
~python_screen_viewer();
|
||||
|
||||
void notify_window_size(GLFWwindow *window, int w, int h);
|
||||
|
||||
@@ -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 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user