-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
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
examples/example_emscripten_wgpu: Initialize and change the size of the viewport and framebuffer according to the size of the canvas #6751
Conversation
… size of the viewport and framebuffer according to the size of the canvas
Thanks for the PR. This already works with example_sdl2_opengl3 for me, without your PR. https://github.com/libsdl-org/SDL/blob/d86d02bbbfee47936164dda28749ae65e0feb443/src/video/emscripten/SDL_emscriptenevents.c#L858 I don't know how SDL knows which canvas to query but we need to investigate how SDL does it: selector = SDL_GetHint(SDL_HINT_EMSCRIPTEN_CANVAS_SELECTOR);
if (!selector)
selector = "#canvas";
wdata->canvas_id = SDL_strdup(selector); So the proposed API would be to add to GLFW backend: #ifdef __EMSCRIPTEN__
void ImGui_ImplGlfw_SetEmscriptenCanvasSelector(const char*);
#endif To be called after And main.cpp would still perform the initial query prior to |
@Traveller23 Would you be willing to work on this suggested change? |
I didn't realize you were waiting for me to change it, I apologize for the delay of so many days. 😂 |
I apologize for my delay, having had almost no breaks at all lately. I'll probably be free by the end of the month. |
…ialize and change the size of the viewport and framebuffer according to the size of the canvas
@ocornut All done, please check it out. |
backends/imgui_impl_glfw.cpp
Outdated
double canvas_width, canvas_height; | ||
emscripten_get_element_css_size(bd->CanvasSelector, &canvas_width, &canvas_height); | ||
|
||
// Will cause a "Permissions check failed" error when set to fullscreen resolution. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to let you know that the changes I implemented in emscripten recently fixes this issue when canvas_width
and canvas_height
happen to be the fullscreen sizes. See ChangeLog | 3.1.49
The glfwSetWindowSize function no longer switches to fullscreen when the width/height provided as parameters match the screen size. This behavior now matches the behavior of SDL and glut. In order to switch to fullscreen, the client code should invoke Module.requestFullscreen(...) from a user triggered event otherwise the browser raises an error. (#20600)
@Traveller23 @ocornut I wanted to let you know that my changes to add Hi DPI support to emscripten/glfw have been merged and will be released when emscriten 3.1.51 is completed. From the Changelog | 3.1.51
If you need my help adding this support to ImGui, please let me know. I can open a PR if you want me to. Note that I used |
Hello @ypujante, I am not sure I follow. |
@ocornut I am sorry I wasn't clear enough
if(ImGui::Button("Fullscreen"))
{
EM_ASM(
const fs = document.getElementById("fullscreen");
if(fs)
fs.click();
);
} <input id="fullscreen" type="button" value="Fullscreen" onclick="Module.requestFullscreen(false, true);" style="display: none;">
|
Great! for your first message, I just need to remove those two comments without tweaking any code. For the second message, I really don't have experience with Hi DPI and really need some help from you. |
@Traveller23 No Problem. I am glad I can help. For Hi DPI, no worries it is completely unrelated to your changes and I apologize for having brought it up. I will do a small PR once yours is merged (and emscripten 3.1.51 is official). In regards to your changes for this PR, this is the right approach: use the canvas dimension to create the window of the proper size, then listens to window resize then use For fullscreen, you can check my previous comment on how I ended up making it work from ImGui (the trick is to generate a click event so that you don't trigger the permission error) |
Really glad to hear. That was the whole point of my PR on the emscripten project :) |
I see your trick, but I don't know the difference between pressing F11 and calling |
Pressing F11 does not invoke If you want to offer a way for the user to go fullscreen that is in the ImGui application (for example a button, or a menu entry), then my trick works. I have this example (my code from when I was working on Hi DPI, not your PR obviously, and I was not using But maybe your implementation addresses this issue. |
Thank you, I'll remove those two comments first, and then test the fullscreen effect in Firefox on Windows over the weekend (I only have windows computers). |
Hmm... In Firefox 120.0.1 |
I suppose so. |
Split emscripten only sections off Added callbacks for getting device & adapter Added multi viewport code Added swapchain creation function Added temporary canvas resize on creation until ocornut#6751 is merged in (Refreshing will resize the canvas)
You are right, not everyone needs a full window canvas. |
Updated. Please check it out. A negative aspect of this implementation is that the size of the GLFW window will be changed one more time when the page is initialized. |
Wouldn't it make more sense to rename And maybe provide a flag as an argument, that way you can still use a fixed size canvas (by passing void ImGui_ImplGlfw_EmscriptenInit(const char *canvas_selector, bool use_canvas_size_as_window_size = true)
{
IM_ASSERT(canvas_selector != nullptr);
ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
IM_ASSERT(bd != nullptr && "Did you call ImGui_ImplGlfw_InitForXXX()?");
bd->CanvasSelector = canvas_selector;
emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, bd, false, ImGui_ImplGlfw_OnCanvasSizeChange);
emscripten_set_fullscreenchange_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, bd, false, ImGui_ImplGlfw_OnFullscreenChange);
// Change the size of the GLFW window according to the size of the canvas
if(use_canvas_size_as_window_size)
ImGui_ImplGlfw_OnCanvasSizeChange(EMSCRIPTEN_EVENT_RESIZE, {}, bd);
} |
It seems good to me at least from API point of view.
One tricky aspect is that we already have I'm really overwhelmed with tasks and don't know those tech very well, if you want to help further I'd need people to be more thorough with 1) the overall design work. 2) conveying the "whys" to me so I'm easily convinced a solution is the right one. |
I've considered this approach as well, but it only makes sense at initialization time. After that the GLFW window size would still be changed whenever the canvas size is changed. |
Sorry to have bothered in this effort. I won't post anymore. |
I tested it on Chrome v120 on Windows, there is no flickering visible to the naked eye. |
I really appreciate your advice and hope you continue to give it. 👍 |
Not meaningfully at all. I'm happy to merge as-is now.
Me too. I didn't intend that and apologies if it was misunderstood. People seems to miss-comprehend that my attention is stretched between HUNDREDS of simultaneous issues. And yet, I keep finding issues, incorrect reasoning or lack of explanations in a vast majority of pull requests and suggestion. The best way anyone can help a project is to give it increased attention and care for details. I tried to convey it in https://github.com/ocornut/imgui/blob/0d582dabf34e9e31f072b1ee5c353c18351b4424/docs/CONTRIBUTING.md#how-to-open-a-pull-request : "Please understand that by submitting a PR you are also submitting a request for the maintainer to review your code and then take over its maintenance [forever]. PR should be crafted both in the interest of the end-users and also to ease the maintainer into understanding and accepting it." I don't know much about Emscripten myself! But I know enough to know that in order to trust a PR the people involved with it need to explain it to me and gives me confidence this is the best possible solution at a given time, meaning they explored possibility space and the trade-off of each solutions. Sometimes the difference between a passerby spending 5 minutes on a topic vs 20 minutes on same topic is that the later is more likely to give us the right solution that fits imgui and current constraint, and easier for me to understand and accept. It's mostly a result of me being overwhelmed that I have a tendency to ask more thoughful work of other people, sort of a survival technique. |
Curious about the answer for this? Could you also recap:
I'm ok merging PR as is now if you are confident we can followup with a cleanup/clarification of those. |
We can't use the index.html that emscripten generates by default, otherwise the size of the canvas would be fixed and there would be elements on the page that we don't want to see.
They are identical except for the different titles of the pages. $ diff example_emscripten_wgpu/web/index.html example_glfw_opengl3/web/index.html
6c6
< <title>Dear ImGui Emscripten+WebGPU example</title>
---
> <title>Dear ImGui Emscripten+GLFW+OpenGL3 example</title>
I didn't know “examples/libs/emscripten/shell_minimal.html” existed before, I'll try to see if I can use it directly as example_glfw_opengl3‘s html. |
@ocornut "examples/example_glfw_opengl3/web/index.html" has been deleted, I modified the make file a little bit, and everything works fine. |
Thanks a lot. Then I'm confused as to why |
I'm not quite sure of the significance of the changes in "example_emscripten_wgpu/web/index.html". |
@ypujante Do you know the reason? |
In regards to @ocornut comments about PR, etc... these are fair points. I will try to do my best to be more explicit. FTR I have been working on emscripten/glfw3 for a few months now so I am fairly familiar with the issues discussed in this PR.
|
So emscripten doesn't implicitly initialize webgpu, we have to write our own code to do that, right? |
The emscripten/glfw implementation is doing what glfw3 specifies:
From what I can see in Because |
I got it, thanks. |
I agree it could be renamed (and noticed you suggested that earlier and I skimmed through it, sorry).
Is the call harmful is the user decide to have a fixed size canvas? How about: IMGUI_IMPL_API bool ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback(....); Not overly-worried with occasionally breaking Emscripten-specific API in the future if we need it too, as it's quite moving technology. However I'm a little bit lost with the remaining .html stuff. |
@ocornut I have no further questions and I think you can proceed with the merger. After that a new PR can be opened to deal with the naming and other issues we are discussing now. |
100% agree on that! 👍 |
Split emscripten only sections off Added callbacks for getting device & adapter Added multi viewport code Added swapchain creation function Added temporary canvas resize on creation until ocornut#6751 is merged in (Refreshing will resize the canvas)
First, use emscripten‘s built-in method
emscripten_get_element_css_size
to get the size of the canvas in order to initialize the viewport and framebuffer.Then, use emscripten's built-in method
emscripten_set_resize_callback
to listen for changes of the browser's window size, and update the size of the viewport and framebuffer.I've only implemented the GLFW case, I haven't used SDL. But the implementation should be very similar, and probably the only difference is "glfwSetWindowSize" on line 262.
Related discussion: #6640