Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-contexts with Metal backend #5203

Closed
noisewuwei opened this issue Apr 16, 2022 · 15 comments
Closed

Multi-contexts with Metal backend #5203

noisewuwei opened this issue Apr 16, 2022 · 15 comments

Comments

@noisewuwei
Copy link
Contributor

OS: Mac OS
Backend:Metal
I created two NSWindow with MTKView. When my imgui window name is the same, two NSWindows can respond to mouse events on any one window at the same time. When imgui window names are different, two NSWindows cannot respond to mouse events. What caused it?

@ocornut
Copy link
Owner

ocornut commented Apr 19, 2022

Hello,
It is ambiguous whether you created multiple imgui contexts or not, or what you do you mean by "my imgui window", etc. I think you will need to provide more details in order to receive help. Please post more details and I will reopen this issue.

@ocornut ocornut closed this as completed Apr 19, 2022
@noisewuwei
Copy link
Contributor Author

Hello,
It is ambiguous whether you created multiple imgui contexts or not, or what you do you mean by "my imgui window", etc. I think you will need to provide more details in order to receive help. Please post more details and I will reopen this issue.

I have create two MTKView. And I call ImGui::CreateContext() in each MTKView.

@ocornut
Copy link
Owner

ocornut commented Apr 19, 2022

If you create multiple contexts you need to switch context with ImGui::SetCurrentContext() before accessing the IO structure.
It is unclear which platform backend you are using (you didn't fill the requested template).

@noisewuwei
Copy link
Contributor Author

If you create multiple contexts you need to switch context with ImGui::SetCurrentContext() before accessing the IO structure.
It is unclear which platform backend you are using (you didn't fill the requested template).

MAC OS Metal.

@noisewuwei
Copy link
Contributor Author

@interface IMGUIMetalView (){
ImGuiContext *ctx;
}

  • (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    // Drawing code here.
    ctx = ImGui::CreateContext();
    ImGuiIO& io = ImGui::GetIO(); (void)io;
    }

-(void)drawInMTKView:(MTKView*)view
{
ImGui::SetCurrentContext(ctx);

ImGuiIO& io = ImGui::GetIO();
io.DisplaySize.x = view.bounds.size.width;
io.DisplaySize.y = view.bounds.size.height;

#if TARGET_OS_OSX
CGFloat framebufferScale = view.window.screen.backingScaleFactor ?: NSScreen.mainScreen.backingScaleFactor;
#else
CGFloat framebufferScale = view.window.screen.scale ?: UIScreen.mainScreen.scale;
#endif
io.DisplayFramebufferScale = ImVec2(framebufferScale, framebufferScale);

io.DeltaTime = 1 / float(view.preferredFramesPerSecond ?: 60);

}

Is that right?

@noisewuwei
Copy link
Contributor Author

If you create multiple contexts you need to switch context with ImGui::SetCurrentContext() before accessing the IO structure.
It is unclear which platform backend you are using (you didn't fill the requested template).

https://github.com/ocornut/imgui/commit/4cec3a0467af9cd068d85363d1a7ceaddc3b68e0

I've seen this link.
Metal cannot support multi-context ?

@ocornut ocornut reopened this Apr 19, 2022
@ocornut
Copy link
Owner

ocornut commented Apr 19, 2022

Right, neither Metal nor OSX backend have been refactored to support multiple context.

@noisewuwei
Copy link
Contributor Author

Right, neither Metal nor OSX backend have been refactored to support multiple context.

Metal cannot support multiple-viewprots neither.Is there any way to create multiple windows when using Metal on Macos.

@ocornut
Copy link
Owner

ocornut commented Apr 19, 2022

Well we need pull requests for both.
Multi-contexts should be quite easy to support. Multi-viewport more involved.

@ocornut ocornut changed the title Mouse Event Multi-contexts with Metal backend Apr 19, 2022
@noisewuwei
Copy link
Contributor Author

Well we need pull requests for both.
Multi-contexts should be quite easy to support. Multi-viewport more involved.

Yep.Multi-viewports is like magic.

@noisewuwei
Copy link
Contributor Author

Well we need pull requests for both.
Multi-contexts should be quite easy to support. Multi-viewport more involved.

By the way, is there any plan to make metal support multi contexts in the near future.

@ocornut
Copy link
Owner

ocornut commented Apr 19, 2022

I don't work for Mac so it depends on other users.

For multi-viewport see #2778 and #4821

For multi-contexts, we need both OSX and Metal backends to the same as e.g #4141 with 70c6038 and 4cec3a0
This probably needs to be done first before the multi-viewports stuff gets merged (then #2778 and #4821 rebased over that)

@noisewuwei
Copy link
Contributor Author

I don't work for Mac so it depends on other users.

For multi-viewport see #2778 and #4821

For multi-contexts, we need both OSX and Metal backends to the same as e.g #4141 with 70c6038 and 4cec3a0 This probably needs to be done first before the multi-viewports stuff gets merged (then #2778 and #4821 rebased over that)

OK.I will have a try.

@noisewuwei
Copy link
Contributor Author

I don't work for Mac so it depends on other users.

For multi-viewport see #2778 and #4821

For multi-contexts, we need both OSX and Metal backends to the same as e.g #4141 with 70c6038 and 4cec3a0 This probably needs to be done first before the multi-viewports stuff gets merged (then #2778 and #4821 rebased over that)

Hello.
#5221 pull request.
Take a look.

ocornut pushed a commit that referenced this issue Apr 27, 2022
…owing to use these backends with multiple contexts. (#5203, #5221, #4141)

# Conflicts:
#	docs/CHANGELOG.txt
@ocornut
Copy link
Owner

ocornut commented Apr 27, 2022

Thank you! We have now merged e54db4e which is your PR + various fixes/cleanup done by @rokups.
(Your PR accidentally reverted the two latest commit on those files)

@ocornut ocornut closed this as completed Apr 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants