-
Notifications
You must be signed in to change notification settings - Fork 33
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
[WIP] Emscripten support #8
Conversation
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.
Hey @sudo-carson, thanks for opening the PR!
Definitely looks interesting, I don't think cmakefied
is going to be the cause, all that does is pull in the Dear ImGui library to build with CMake (though I could totally be wrong, it might be something we need to change with ExternalProject_Add
?).
I'm not really familiar with emcmake
, do you have a link for trying it out? I'll see if I can try out the branch this weekend.
It would totally be cool to support this as an option though! Hopefully we can get it working 👍 Cheers!
|
Searching Seems like we do need to change |
Hmm, adding
|
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.
Thanks for making the updates, this is looking pretty cool now! I just have two minor nits and then I'll see if I can try it out later. You mentioned there's still an issue right now? I'll see if I hit it too.
Hmm so do all the third-party dependencies build? I take it you'll need to add the change to CMakeLists.txt in the third-party folder too (From looking at that earlier link)? Does there need to be an emscripten configure script too that uses |
Just pushed the changes I made to the third-party CMakeLists 👍 The third-party dependencies don't build, that's what's giving me the |
Hey @sudo-carson, I was able to sync your branch and try building
So I don't think it's a local environment issue. I'm not really sure why it's failing either. Is there any info on the |
No issues about this specific error on bgfx. I opened a discussion there but haven't got a response yet. Super strange... Maybe it's a problem with emscripten? Hmm |
The only mention of an undefined reference to |
🤔
|
Got it built on Unix! set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s MAX_WEBGL_VERSION=2")
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -s MAX_WEBGL_VERSION=2") Well... sort of...
🥲 |
Hey there just catching up on things, hmm interesting. Not sure about that SDL error on Windows, actually looking at the command you have a mix of forward and backward slashes that might be confusing it so it doesn't find the SDL library (with |
Oh the other thing I forgot earlier sorry is for this PR the one other important thing we should add is an update to the README (docs) to let people know emscripten is available and how to build for it (once everything is working that is 🙂) |
Yep, just focusing on getting it built and running first 👍 Adding I'm also trying to build this project with as little things as possible + without ImGui and third-party downloading stuff (just Also, thanks for the heads up about |
Well, I'm not getting warnings running it anymore - but it just hangs at 100% CPU. No graphical or console output. 🥲 |
Thanks for the update, I tried the third-party stuff on Windows but ran into problems unfortunately. I could use...
which seemed to work okay, but then...
and So not sure I'm afraid 😬 Let me know if you have any more luck! No worries about the I'm not a CMake expert but did write up a bunch about it here (mainly to help myself remember how to use it 😝 https://github.com/pr0g/cmake-examples |
@pr0g I believe the commit I just pushed fixes all problems with the code. Now all I have to do is figure out how to get |
Hey @sudo-carson, sorry I'm just getting back to you, work's been pretty hectic recently. That's great to hear, I'll try and pull the latest changes soon and give it a whirl. With the Cheers! |
Adding the args to |
Hmm interesting... not sure what that'd happen. I'll try have a look later this week/weekend to see if I can find the right incantation 😝 Cheers! |
Hey there, I did noodle around with this again for a little while this evening (on Mac) but still kept hitting the I tried passing I did some googling and found a few other people having issues with bgfx and emscripten in general...
So not sure, basically from third-party I'm running this...
And then I hit the link error when linking It's possible something is missing in bgfx.cmake? Might be worth looking at the genie build files in the main bgfx repo to see If the cmake build is missing something. |
bgfx.cmake and bgfx genie files both omit |
I'm afraid I don't want to switch to submodules sorry... are you able to just build |
The problem seems to be the definition of You can see the definition here - https://git.sprintf.io/emscripten-ports/emscripten/commit/f8d9751855d78e36a78a30d007eeadebf01cc610?style=split, I'm not sure why specifying |
I just built |
I made a new fork off bkaradzic/bgfx.cmake here pr0g/bgfx.cmake and updated it to work when building with I had to make this local change to ExternalProject_Add(
bgfx
GIT_REPOSITORY https://github.com/pr0g/bgfx.cmake.git # <-------- updated repo
GIT_TAG 5e7ec5f7d783fb348608abc6f932a34b937f0115 # <------ updated commit
BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/bgfx/build/${build_type_dir}
INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}
CMAKE_COMMAND ${THIRD_PARTY_CMAKE_COMMAND}
CMAKE_ARGS ${build_type_arg}
-D CMAKE_INSTALL_PREFIX=<INSTALL_DIR>
"$<$<CONFIG:Debug>:-DCMAKE_DEBUG_POSTFIX=d>"
-D BGFX_BUILD_EXAMPLES=OFF) With this change, things built fine in third-party, might be worth giving it a try your end. |
Argh but now when trying to build the actual starter demo I'm hitting an issue where emscripten does not respect |
I managed to get everything building and linking but it required quite a few changes that I'm not a massive fan of... As Emscripten is quite different to the normal building approach I think it's actually introducing quite a lot of complexity for the 'normal' case so I'm leaning towards this PR being moved to a separate repo that's Emscripten only. That way we don't have to jump through hoops to support Mac/Windows/Linux/Enscripten at the same time. In the latest version of this PR there were a lot of compile errors with things not being hooked up correctly. I have pushed a branch with my fixes here - https://github.com/pr0g/sdl-bgfx-imgui-starter/tree/sudo-carson-platform/emscripten (4a4cab8). To build use these commands from the root of the repo (this is after building third-party above).
It still doesn't actually work (I think it might be related to the I'll wait to hear back from you but I think at this point it might be worth taking what we've learned to another repo that deals with Emscripten exclusively (we don't need SDL2 in third-party then for example). Thanks, Tom |
I made another small update (0213a3f) that actually does see bgfx get initialised (I realised I had to start a local web server with |
That's (apparently) normal with bgfx - I get the same thing on a simple bgfx example and it renders just fine. Not sure why the black screen - probably has something to do with the shaders because I made that loader code completely from memory and without testing it -- I'm surprised it compiled at all 😅 Here's a super simple working example that I can confirm works, hope this is useful in figuring out the black screen problem: #include <SDL.h>
#include <SDL_syswm.h>
#include <bgfx/bgfx.h>
#include <bgfx/platform.h>
#include <bx/math.h>
#include <bx/bx.h>
#include <stdio.h>
#include <emscripten.h>
#include <emscripten/html5.h>
void loop(void) {
SDL_Event currentEvent;
while (SDL_PollEvent(¤tEvent) != 0) {
if (currentEvent.type == SDL_QUIT) {
return;
}
}
bgfx::setViewRect(0, 0, 0, 800, 600);
bgfx::touch(0);
bgfx::dbgTextClear();
bgfx::dbgTextPrintf(0, 1, 0x0f, "it works!");
bgfx::frame();
}
int main(int argc, char** argv)
{
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize. SDL_Error: %s\n", SDL_GetError());
return 1;
}
const int width = 800;
const int height = 600;
SDL_Window* window = SDL_CreateWindow(
"bgfx", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width,
height, SDL_WINDOW_SHOWN);
if (window == nullptr) {
printf("Window could not be created. SDL_Error: %s\n", SDL_GetError());
return 1;
}
static const char *canvas = "#canvas";
bgfx::PlatformData pd;
bx::memSet(&pd, 0, sizeof(pd));
pd.nwh = (void*)canvas;
bgfx::setPlatformData(pd);
bgfx::Init bgfx_init;
bgfx_init.type = bgfx::RendererType::Count; // auto choose renderer
bgfx_init.resolution.width = width;
bgfx_init.resolution.height = height;
bgfx_init.resolution.reset = BGFX_RESET_VSYNC;
bgfx::init(bgfx_init);
bgfx::setDebug(BGFX_DEBUG_TEXT);
bgfx::setViewClear(
0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x6495EDFF, 1.0f, 0);
bgfx::setViewRect(0, 0, 0, width, height);
bgfx::frame();
emscripten_set_main_loop(loop, -1, 1);
bgfx::shutdown();
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
} |
Haha okay understood. Back at work today so won't have as much time to noodle with this but I'll see if I can get back to it later in the week, thanks for the example, it'll be useful to narrow down what the problem is. |
I had another little noodle around with this, this evening. The Dear ImGui stuff works fine, but the The other option might be to embed the shaders, but that's a bit of a pain... I'll update if I push any more commits |
It lives! 😄 I pushed the latest changes to get it working here - 47cd548 I did some more digging and found you can still use C/C++ file loading APIs with Emscripten, you just have to let Emscripten know ahead of time which files you want to load with I removed the Let me know if you're able to try this updated branch and get it working. I'll have another think about the best way to integrate it, whether that's a separate repo or as part of this one, I'm still on the fence right now... |
I was able to get it working on my end with those changes, so glad to finally see it working after all this time! 😄 |
Great to hear! 😄 Thanks for letting me know 👍 |
I'm closing this PR as it is now replaced by #9. The core work by @sudo-carson is still there but there have been additional updates to ensure everything works cross-platform and the docs reflect the Emscripten addition. |
WIP HTML5 target using Emscripten.
Currently, I can't get this to build properly w/
emcmake
; no matter what I do, CMake will always build for Linux/x86_64. Might be something to do withcmakefied
?But I believe this should work once its built properly, all you would need to get it working is an HTML file with
<canvas id="canvas" width="400" height="400"></canvas>
.