Independent viewports, contextsnon - shared resources and asynchronous rendering

I want to create multiple threads working/rendering to a single split screen window. Each thread renders a separate scene/contexts and no resources are shared among the threads.

Number of threads= number of viewports.

My intention is to have each thread not be blocked by glfw events like poll and swapbuffers.
Or wait for otger threads to finish rendering. Is there a minimal example to do this.

Are the mouse and keyboard callbacks blocking? Can they be setup in a way to only workinside one viewport at a time (and other viewport callbacks disabled) so that speed of rendering is not reduced for inactive viewports?

HI @rohit-kumar-j,

Welcome to the GLFW forum.

Generally speaking you can only render to multiple windows with multiple threads, and you cannot render to a single window with multiple threads in both Vulkan[*] and OpenGL.

[*] In Vulkan you can use multiple threads to generate multiple command buffers, for example generate the depth pass, shadow pass and colour pass independently.

I have a PR for multi context windows for OpenGL, but generally speaking this is not useful for multithreaded rendering to the same framebuffer. Indeed multithreaded rendering in general does not give significant performance improvements, though it can help with modifying buffer data on multiple threads.

It’s also worth noting that most GPUs render the incoming commands sequentially.

glfwPollEvents should be called from the main thread, and so if you have rendering on a separate thread it will not block this.

glfwSwapBuffers may or may not block depending on the swap interval and driver/GPU behaviour. Even if glfwSwapBuffers doesn’t block, some GPU commands may block if the GPU pipeline is full.

The callbacks themselves are your own code, so how long they take depends on what you do in them. The glfwPollEvents (or glfwWaitEvents etc.) call is what processes the events and calls the callbacks, and this may be blocking if the window is moved etc.

All windows events are processed at the same time from the main thread. If you are rendering on another thread then this will not be blocked unless you add some form of synchronization. Note that generally inactive windows do not receive many events.

I hope this answers your question. You can likely achieve something similar to what you want with multiple undecorated windows (GLFW_DECORATED window hint set to false) placed next to each other, rendered to from separate threads.