如何与GLFW正确共享上下文
How to properly do Context Sharing with GLFW?
我要做的就是做到这一点,这样,如果我替换了窗口,我将使用新窗口渲染,这可能会发生,因为用户会切换屏幕或从FullScreen切换到窗口,或出于许多其他原因。
到目前为止我的代码看起来像这样:
" context.h"
struct window_deleter {
void operator()(GLFWwindow * window) const;
};
class context {
std::unique_ptr<GLFWwindow, window_deleter> window;
public:
context(int width, int height, const char * s, GLFWmonitor * monitor, GLFWwindow * old_window, bool borderless);
GLFWwindow * get_window() const;
void make_current() const;
};
" context.cpp"
context::context(int width, int height, const char * s, GLFWmonitor * monitor, GLFWwindow * old_window, bool borderless) {
if (!glfwInit()) throw std::runtime_error("Unable to Initialize GLFW");
if (borderless) glfwWindowHint(GLFW_DECORATED, 0);
else glfwWindowHint(GLFW_DECORATED, 1);
window.reset(glfwCreateWindow(width, height, s, monitor, old_window));
if (!window) throw std::runtime_error("Unable to Create Window");
make_current();
}
GLFWwindow * context::get_window() const {
return window.get();
}
void context::make_current() const {
glfwMakeContextCurrent(window.get());
}
" windowmanager.h"
#include "Context.h"
class window_style;
/* window_style is basically a really fancy "enum class", and I don't
* believe its implementation or interface are relevant to this project.
* I'll add it if knowing how it works is super critical.
*/
class window_manager {
context c_context;
uint32_t c_width, c_height;
std::string c_title;
window_style c_style;
std::function<bool()> close_test;
std::function<void()> poll_task;
public:
static GLFWmonitor * get_monitor(window_style style);
window_manager(uint32_t width, uint32_t height, std::string const& title, window_style style);
context & get_context();
const context & get_context() const;
bool resize(uint32_t width, uint32_t height, std::string const& title, window_style style);
std::function<bool()> get_default_close_test();
void set_close_test(std::function<bool()> const& test);
std::function<void()> get_default_poll_task();
void set_poll_task(std::function<void()> const& task);
void poll_loop();
};
" windowmanager.cpp"
GLFWmonitor * window_manager::get_monitor(window_style style) {
if (style.type != window_style::style_type::fullscreen) return nullptr;
if (!glfwInit()) throw std::runtime_error("Unable to initialize GLFW");
int count;
GLFWmonitor ** monitors = glfwGetMonitors(&count);
if (style.monitor_number >= uint32_t(count)) throw invalid_monitor_exception{};
return monitors[style.monitor_number];
}
std::function<bool()> window_manager::get_default_close_test() {
return [&] {return glfwWindowShouldClose(c_context.get_window()) != 0; };
}
window_manager::window_manager(uint32_t width, uint32_t height, std::string const& title, window_style style) :
c_context(int(width), int(height), title.c_str(), get_monitor(style), nullptr, style.type == window_style::style_type::borderless),
c_width(width), c_height(height), c_title(title), c_style(style), close_test(get_default_close_test()), poll_task(get_default_poll_task()) {
}
context & window_manager::get_context() {
return c_context;
}
const context & window_manager::get_context() const {
return c_context;
}
bool window_manager::resize(uint32_t width, uint32_t height, std::string const& title, window_style style) {
if (width == c_width && height == c_height && title == c_title && style == c_style) return false;
c_width = width;
c_height = height;
c_title = title;
c_style = style;
c_context = context(int(width), int(height), title.c_str(), get_monitor(style), get_context().get_window(), style.type == window_style::style_type::borderless);
return true;
}
void window_manager::set_close_test(std::function<bool()> const& test) {
close_test = test;
}
std::function<void()> window_manager::get_default_poll_task() {
return [&] {glfwSwapBuffers(c_context.get_window()); };
}
void window_manager::set_poll_task(std::function<void()> const& task) {
poll_task = task;
}
void window_manager::poll_loop() {
while (!close_test()) {
glfwPollEvents();
poll_task();
}
}
" main.cpp"
int main() {
try {
glfwInit();
const GLFWvidmode * vid_mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
gl_backend::window_manager window(vid_mode->width, vid_mode->height, "First test of the window manager", gl_backend::window_style::fullscreen(0));
glfwSetKeyCallback(window.get_context().get_window(), [](GLFWwindow * window, int, int, int, int) {glfwSetWindowShouldClose(window, 1); });
glbinding::Binding::initialize();
//Anything with a "glresource" prefix is basically just a std::shared_ptr<GLuint>
//with some extra deletion code added.
glresource::vertex_array vao;
glresource::buffer square;
float data[] = {
-.5f, -.5f,
.5f, -.5f,
.5f, .5f,
-.5f, .5f
};
gl::glBindVertexArray(*vao);
gl::glBindBuffer(gl::GL_ARRAY_BUFFER, *square);
gl::glBufferData(gl::GL_ARRAY_BUFFER, sizeof(data), data, gl::GL_STATIC_DRAW);
gl::glEnableVertexAttribArray(0);
gl::glVertexAttribPointer(0, 2, gl::GL_FLOAT, false, 2 * sizeof(float), nullptr);
std::string vert_src =
"#version 430n"
"layout(location = 0) in vec2 vertices;"
"void main() {"
"gl_Position = vec4(vertices, 0, 1);"
"}";
std::string frag_src =
"#version 430n"
"uniform vec4 square_color;"
"out vec4 fragment_color;"
"void main() {"
"fragment_color = square_color;"
"}";
glresource::shader vert(gl::GL_VERTEX_SHADER, vert_src);
glresource::shader frag(gl::GL_FRAGMENT_SHADER, frag_src);
glresource::program program({ vert, frag });
window.set_poll_task([&] {
gl::glUseProgram(*program);
gl::glBindVertexArray(*vao);
glm::vec4 color{ (glm::sin(float(glfwGetTime())) + 1) / 2, 0.f, 0.5f, 1.f };
gl::glUniform4fv(gl::glGetUniformLocation(*program, "square_color"), 1, glm::value_ptr(color));
gl::glDrawArrays(gl::GL_QUADS, 0, 4);
glfwSwapBuffers(window.get_context().get_window());
});
window.poll_loop();
window.resize(vid_mode->width, vid_mode->height, "Second test of the window manager", gl_backend::window_style::fullscreen(1));
glfwSetKeyCallback(window.get_context().get_window(), [](GLFWwindow * window, int, int, int, int) {glfwSetWindowShouldClose(window, 1); });
window.poll_loop();
}
catch (std::exception const& e) {
std::cerr << e.what() << std::endl;
std::ofstream error_log("error.log");
error_log << e.what() << std::endl;
system("pause");
}
return 0;
}
因此,代码的当前版本是应该执行以下:
- 在主监视器上显示全屏幕窗口
- 在此显示器上,显示一个"正方形"(矩形,实际上....),随着时间的流逝,洋红色和蓝色之间的过渡,而背景过渡是洋红色和绿色的。
- 当用户按键时,使用第一个窗口的上下文在第二个监视器上创建一个新的全屏窗口,以馈入GLFW的窗口创建,并销毁原始窗口(按照此顺序)
- 在第二个窗口上显示相同的矩形
- 继续定期过渡背景
- 当用户再次按下键时,破坏了第二个窗口并退出程序。
在这些步骤中,步骤4根本不起作用,步骤3部分可行:窗口确实会创建,但默认情况下没有显示,并且用户必须通过任务栏调用它。所有其他步骤都按预期工作,包括两个窗口上的过渡背景。
因此,我的假设是,在上下文之间的对象共享方面出现了问题。具体来说,我创建的第二个上下文似乎并没有接收第一个上下文创建的对象。我有明显的逻辑错误吗?我应该做其他事情以确保上下文共享按预期工作?GLFW中可能只有一个错误?
因此,我的假设是,在上下文之间的对象共享方面出现了问题。具体来说,我创建的第二个上下文似乎并没有接收第一个上下文创建的对象。我有明显的逻辑错误吗?
是的,您的前提是错误的。共享的OpenGL上下文将不会共享整个状态,只是实际保存用户特定数据的"大"对象(例如VBOS,纹理,着色器和程序,RenderBuffers等),而不是仅引用它们的人-State Container像VAO,FBO等人一样,从未分享过。
我应该做其他事情以确保上下文共享按预期工作吗?
好吧,如果您真的想走那条路线,则必须重新构建所有这些状态容器,还可以恢复全球状态(所有这些glEnable
S,深度缓冲区设置,混合状态,其他成本))
但是,我在这里发现您的整个概念都令人怀疑。从全屏到窗口或同一GPU上的其他监视器时,您无需破坏窗口,而GLFW直接支持通过GlfWsetWindowMonitor()。
。,即使您 do 重新创建了一个窗口,这并不意味着您必须重新创建GL上下文。在这方面,GLFWS API可能施加了一些限制,但是基本概念是分开的。您基本上可以在新窗口中使旧上下文当前当前,并且仅处理它。GLFW仅见地将窗口和上下文链接在一起,这是一种不幸的抽象。
但是,我能想象的唯一场景是重新创建窗口的必要条件是驱动不同屏幕是不同的GPU的东西 - 但是GL上下文共享在不同的GL实现中都无法正常工作,因此,即使在这种情况下,您将不得不重建整个上下文状态。
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- #为""定义宏;静态";针对不同的上下文
- 与互斥锁相比,旋转锁可以保证上下文切换
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 在cuda线程之间共享大量常量数据
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- 为什么共享内存(在IPC中)不需要上下文切换?这是内核空间的内存,可以映射到用户空间
- 如何与GLFW正确共享上下文
- OpenGL-与未来的上下文共享现有纹理
- 如何在GLFW 3中的源文件之间共享窗口上下文
- 如何共享OpenGL上下文或数据
- 如何在 OSX 上使用 glfw3 在 OpenGL 和 OpenCL 之间创建共享上下文
- OpenGL的状态需要在新的共享上下文上重置
- 具有共享OpenGL上下文的QOpenGLWidget
- 可以在多个ssl流之间共享boost::asio::ssl::上下文
- ZeroMQ与分叉服务器中的所有子进程共享上下文
- 在OS X(不同版本/配置文件)上的OpenGL上下文之间共享数据
- 我可以在两个OpenGL上下文之间共享外部纹理吗?
- OpenGL多线程/共享上下文和glGenBuffers
- Android opengl在两个线程上运行的两个上下文之间共享纹理