floooh / sokol

minimal cross-platform standalone C headers
https://floooh.github.io/sokol-html5
zlib License
6.65k stars 474 forks source link

"Multi-window support" brain dump #229

Open floooh opened 4 years ago

floooh commented 4 years ago

What's needed to add "proper" multi-window support to sokol_gfx.h and sokol_app.h:

sokol_gfx.h: Not strictly multi-window specific, but sg_desc needs a couple more parameters to describe the default framebuffer, this is only needed for validation whether a pipeline object is compatible with that pass:

sg_setup_context() needs a sg_contect_desc* param which contains swap-chain / context-specific parameters (a subset of what's in sg_desc):

    const void* (*mtl_renderpass_descriptor_cb)(void);
    const void* (*mtl_drawable_cb)(void);
    const void* (*d3d11_render_target_view_cb)(void);
    const void* (*d3d11_depth_stencil_view_cb)(void);

...plus the color/depth pixel formats and sample count.

The callbacks are needed because each window has its own swap chain. The pixel formats and sample count are needed for pipeline validation.

Then, for each window call sg_setup_context(), and before the default render pass of that window, call sg_activate_context().

sokol_app.h:

This will be much more work:

incrediblejr commented 4 years ago

A proof of concept implementation can be found here with the test file here which creates multiple windows, each using sokol gfx.

This is by no means complete and should seen as a way to get the ball rolling / discussions started. It is currently only working on Windows with D3D11.

Please refer to the comments (make a diff to master to see what changed) in the sokol_app.h + sokol_gfx.h. As I do not know the limitations/quirks of the non-Windows platforms supported by sokol app/gfx input and help is needed to take this further.

icefoxen commented 4 years ago

Personally as a user I would far prefer sokol_app.h stays simple and only supports one window, rather than adding a bunch of complexity for a big feature that often won't be used. I'm only using it for gamedev though, so I'm biased.

medvednikov commented 4 years ago

For me this is very important since I wanted to use Sokol as a backend for a UI engine.

It's not possible, until this is implemented.

ikrima commented 4 years ago

(For posterity, posting here as well)

ImGui multiviewport integration:

Demo: https://twitter.com/ikrimae/status/1252194400681127936 Repo: https://github.com/ikrima/sokol

septag commented 4 years ago

This is impressive. can't wait for multi-view + imgui support on the official repo

kattkieru commented 3 years ago

Curious about the state of this, whether or not the plan includes multi-window iOS support?

ikrima commented 3 years ago

@kattkieru I just merged & updated my fork (https://github.com/ikrima/sokol) to the latest. It's mostly updated to be idiomatic with the rest of sokol

Having that said, some caveats

But given some of the changes in sokol like user context data/etc, it's a bit more feasible to merge into main + add other platforms (cc: @floooh )

It still needs a decent cleanup pass tho since I usually just hack whatever my pressing need is

floooh commented 3 years ago

Thanks for the headsup. I'll need to set aside a bit of time to play around and get familiar with your fork and my year-end vacation is coming up. It'll most likely slip into the next year.

ikrima commented 3 years ago

No rush! Only tagged you for your curiosity; fork isn't really in a good PR ready form + cleaning up some old outstanding bugs. Probably will be till next year before i fully sanitize it as well

kattkieru commented 3 years ago

@ikrima Excellent timing, as I'm looking at this again for my christmas break. Thanks for posting!

stbachmann commented 3 years ago

@ikrima @kattkieru I've opened a new pull-request that has a more complete multi-window implementation (using some of the previous work done here already): https://github.com/floooh/sokol/pull/437

Happy to hear thoughts/feedback!

ikrima commented 3 years ago

@stbachmann excellent. Don't have too much time right now to dive into it but from quick scan, the only thing that sticks out is updating the sokol imgui bits.

There's a bug in the imgui shader bc it doesn't scale the vtx position according to the window clipspace (imgui emits in absolute window space in multiviewport mode). Here's the updated shader:

@module _simgui

@vs vs
uniform vs_params {
  vec4 disp_rect;
};
in vec2 position;
in vec2 texcoord0;
in vec4 color0;
out vec2 uv;
out vec4 color;
void main() {
  gl_Position = vec4((((position-disp_rect.xy)/disp_rect.zw)-0.5)*vec2(2.0,-2.0), 0.5, 1.0);
  uv = texcoord0;
  color = color0;
}
@end

@fs fs
uniform sampler2D tex;
in vec2 uv;
in vec4 color;
out vec4 frag_color;
void main() {
  frag_color = texture(tex, uv) * color;
}
@end

@program simgui vs fs
stbachmann commented 3 years ago

@ikrima thanks, great pointer. I'll check out what you did on your branch to make this work. 👍

ylluminate commented 3 years ago

As mentioned before by @medvednikov this is necessary for V lang's (>21k stars) fantastic V UI library. Highly compelling.

andorxornot commented 6 months ago

Is there any news about support for multiple contexts? Maybe some plans or forecasts

floooh commented 6 months ago

So from my side, I abandonded the old multiwindow branch I had started because it became too gnarly (and also required too many changes in sokol_gfx.h).

sokol_gfx.h at least will become more flexible to support multi-window scenarios when this is implemented:

https://github.com/floooh/sokol/issues/904

(since there will no longer be a special 'default frame buffer' but instead just one or multiple 'external swapchains', the entire context stuff in sokol-gfx will most likely be removed too after this change)

This may be the foundation to do another approach with multi-window support in sokol_app.h, but I was also running into an unrelated problem on macOS with MTKView (there's a one-second timeout freeze when a window becomes fully obscured, this isn't noticeable when there's only a single window, but in an application with multiple windows it becomes a problem).

digitalsignalperson commented 2 months ago

Has anyone glued together the imgui multiviewports/docking branch & multiple windows with sokol-gfx and glfw per https://github.com/floooh/sokol-samples/blob/master/glfw/multiwindow-glfw.c ?