2021-01-28 10:37:34 +00:00
// Dear ImGui: standalone example application for Emscripten, using GLFW + WebGPU
// (Emscripten is a C++-to-javascript compiler, used to publish executables for the web. See https://emscripten.org/)
2023-09-11 11:47:08 +00:00
// Learn about Dear ImGui:
// - FAQ https://dearimgui.com/faq
// - Getting Started https://dearimgui.com/getting-started
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
// - Introduction, links and more at the top of imgui.cpp
2021-01-28 10:37:34 +00:00
# include "imgui.h"
# include "imgui_impl_glfw.h"
# include "imgui_impl_wgpu.h"
# include <stdio.h>
# include <emscripten.h>
# include <emscripten/html5.h>
# include <emscripten/html5_webgpu.h>
# include <GLFW/glfw3.h>
# include <webgpu/webgpu.h>
# include <webgpu/webgpu_cpp.h>
// Global WebGPU required states
2023-08-24 08:04:50 +00:00
static WGPUDevice wgpu_device = nullptr ;
static WGPUSurface wgpu_surface = nullptr ;
static WGPUTextureFormat wgpu_preferred_fmt = WGPUTextureFormat_RGBA8Unorm ;
static WGPUSwapChain wgpu_swap_chain = nullptr ;
static int wgpu_swap_chain_width = 0 ;
static int wgpu_swap_chain_height = 0 ;
2021-01-28 10:37:34 +00:00
2021-01-28 11:11:26 +00:00
// Forward declarations
2023-02-02 15:27:33 +00:00
static void MainLoopStep ( void * window ) ;
static bool InitWGPU ( ) ;
2021-01-28 11:11:26 +00:00
static void print_glfw_error ( int error , const char * description ) ;
static void print_wgpu_error ( WGPUErrorType error_type , const char * message , void * ) ;
2021-01-28 10:37:34 +00:00
2023-02-02 15:27:33 +00:00
// Main code
2021-01-28 10:37:34 +00:00
int main ( int , char * * )
{
glfwSetErrorCallback ( print_glfw_error ) ;
if ( ! glfwInit ( ) )
return 1 ;
// Make sure GLFW does not initialize any graphics context.
2023-02-02 15:27:33 +00:00
// This needs to be done explicitly later.
2021-01-28 10:37:34 +00:00
glfwWindowHint ( GLFW_CLIENT_API , GLFW_NO_API ) ;
2023-04-11 09:25:14 +00:00
GLFWwindow * window = glfwCreateWindow ( 1280 , 720 , " Dear ImGui GLFW+WebGPU example " , nullptr , nullptr ) ;
2021-01-28 11:11:26 +00:00
if ( ! window )
{
2021-01-28 10:37:34 +00:00
glfwTerminate ( ) ;
return 1 ;
}
// Initialize the WebGPU environment
2023-02-02 15:27:33 +00:00
if ( ! InitWGPU ( ) )
2021-01-28 11:11:26 +00:00
{
2021-01-28 10:37:34 +00:00
if ( window )
glfwDestroyWindow ( window ) ;
glfwTerminate ( ) ;
return 1 ;
}
glfwShowWindow ( window ) ;
// Setup Dear ImGui context
IMGUI_CHECKVERSION ( ) ;
ImGui : : CreateContext ( ) ;
ImGuiIO & io = ImGui : : GetIO ( ) ; ( void ) io ;
2023-03-10 17:35:03 +00:00
io . ConfigFlags | = ImGuiConfigFlags_NavEnableKeyboard ; // Enable Keyboard Controls
io . ConfigFlags | = ImGuiConfigFlags_NavEnableGamepad ; // Enable Gamepad Controls
2021-01-28 10:37:34 +00:00
// For an Emscripten build we are disabling file-system access, so let's not attempt to do a fopen() of the imgui.ini file.
// You may manually call LoadIniSettingsFromMemory() to load settings from your own storage.
2023-04-11 09:25:14 +00:00
io . IniFilename = nullptr ;
2021-01-28 10:37:34 +00:00
// Setup Dear ImGui style
ImGui : : StyleColorsDark ( ) ;
2022-07-06 18:58:20 +00:00
//ImGui::StyleColorsLight();
2021-01-28 10:37:34 +00:00
// Setup Platform/Renderer backends
2021-02-10 16:27:33 +00:00
ImGui_ImplGlfw_InitForOther ( window , true ) ;
2023-08-24 08:04:50 +00:00
ImGui_ImplWGPU_Init ( wgpu_device , 3 , wgpu_preferred_fmt , WGPUTextureFormat_Undefined ) ;
2021-01-28 10:37:34 +00:00
// Load Fonts
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
2023-04-11 09:25:14 +00:00
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
2021-01-28 10:37:34 +00:00
// - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
2022-09-19 15:45:05 +00:00
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
2021-01-28 10:37:34 +00:00
// - Read 'docs/FONTS.md' for more instructions and details.
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
// - Emscripten allows preloading a file or folder to be accessible at runtime. See Makefile for details.
//io.Fonts->AddFontDefault();
# ifndef IMGUI_DISABLE_FILE_FUNCTIONS
2022-09-19 15:45:05 +00:00
//io.Fonts->AddFontFromFileTTF("fonts/segoeui.ttf", 18.0f);
io . Fonts - > AddFontFromFileTTF ( " fonts/DroidSans.ttf " , 16.0f ) ;
//io.Fonts->AddFontFromFileTTF("fonts/Roboto-Medium.ttf", 16.0f);
2021-01-28 10:37:34 +00:00
//io.Fonts->AddFontFromFileTTF("fonts/Cousine-Regular.ttf", 15.0f);
//io.Fonts->AddFontFromFileTTF("fonts/ProggyTiny.ttf", 10.0f);
2023-04-11 09:25:14 +00:00
//ImFont* font = io.Fonts->AddFontFromFileTTF("fonts/ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
//IM_ASSERT(font != nullptr);
2021-01-28 10:37:34 +00:00
# endif
2021-01-28 11:11:26 +00:00
// This function will directly return and exit the main function.
2021-01-28 10:37:34 +00:00
// Make sure that no required objects get cleaned up.
// This way we can use the browsers 'requestAnimationFrame' to control the rendering.
2023-02-02 15:27:33 +00:00
emscripten_set_main_loop_arg ( MainLoopStep , window , 0 , false ) ;
2021-01-28 10:37:34 +00:00
return 0 ;
}
2023-02-02 15:27:33 +00:00
static bool InitWGPU ( )
2021-01-28 10:37:34 +00:00
{
wgpu_device = emscripten_webgpu_get_device ( ) ;
if ( ! wgpu_device )
return false ;
2023-04-11 09:25:14 +00:00
wgpuDeviceSetUncapturedErrorCallback ( wgpu_device , print_wgpu_error , nullptr ) ;
2021-01-28 10:37:34 +00:00
2021-01-28 11:11:26 +00:00
// Use C++ wrapper due to misbehavior in Emscripten.
2021-01-28 10:37:34 +00:00
// Some offset computation for wgpuInstanceCreateSurface in JavaScript
// seem to be inline with struct alignments in the C++ structure
2021-01-28 11:11:26 +00:00
wgpu : : SurfaceDescriptorFromCanvasHTMLSelector html_surface_desc = { } ;
2021-01-28 10:37:34 +00:00
html_surface_desc . selector = " #canvas " ;
2021-01-28 11:11:26 +00:00
wgpu : : SurfaceDescriptor surface_desc = { } ;
2021-01-28 10:37:34 +00:00
surface_desc . nextInChain = & html_surface_desc ;
2023-08-24 08:04:50 +00:00
wgpu : : Instance instance = wgpuCreateInstance ( nullptr ) ;
wgpu : : Surface surface = instance . CreateSurface ( & surface_desc ) ;
wgpu : : Adapter adapter = { } ;
wgpu_preferred_fmt = ( WGPUTextureFormat ) surface . GetPreferredFormat ( adapter ) ;
wgpu_surface = surface . Release ( ) ;
2021-01-28 10:37:34 +00:00
return true ;
}
2023-02-02 15:27:33 +00:00
static void MainLoopStep ( void * window )
2021-01-28 10:37:34 +00:00
{
2023-03-13 15:23:45 +00:00
ImGuiIO & io = ImGui : : GetIO ( ) ;
2021-01-28 10:37:34 +00:00
glfwPollEvents ( ) ;
int width , height ;
2023-02-02 15:27:33 +00:00
glfwGetFramebufferSize ( ( GLFWwindow * ) window , & width , & height ) ;
2021-01-28 10:37:34 +00:00
// React to changes in screen size
if ( width ! = wgpu_swap_chain_width & & height ! = wgpu_swap_chain_height )
{
ImGui_ImplWGPU_InvalidateDeviceObjects ( ) ;
if ( wgpu_swap_chain )
wgpuSwapChainRelease ( wgpu_swap_chain ) ;
wgpu_swap_chain_width = width ;
wgpu_swap_chain_height = height ;
WGPUSwapChainDescriptor swap_chain_desc = { } ;
2021-05-16 16:55:58 +00:00
swap_chain_desc . usage = WGPUTextureUsage_RenderAttachment ;
2023-08-24 08:04:50 +00:00
swap_chain_desc . format = wgpu_preferred_fmt ;
2021-01-28 10:37:34 +00:00
swap_chain_desc . width = width ;
swap_chain_desc . height = height ;
swap_chain_desc . presentMode = WGPUPresentMode_Fifo ;
wgpu_swap_chain = wgpuDeviceCreateSwapChain ( wgpu_device , wgpu_surface , & swap_chain_desc ) ;
ImGui_ImplWGPU_CreateDeviceObjects ( ) ;
}
// Start the Dear ImGui frame
ImGui_ImplWGPU_NewFrame ( ) ;
ImGui_ImplGlfw_NewFrame ( ) ;
ImGui : : NewFrame ( ) ;
2023-02-02 15:27:33 +00:00
// Our state
// (we use static, which essentially makes the variable globals, as a convenience to keep the example code easy to follow)
static bool show_demo_window = true ;
static bool show_another_window = false ;
static ImVec4 clear_color = ImVec4 ( 0.45f , 0.55f , 0.60f , 1.00f ) ;
2021-01-28 10:37:34 +00:00
// 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
if ( show_demo_window )
ImGui : : ShowDemoWindow ( & show_demo_window ) ;
2022-09-14 16:19:50 +00:00
// 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
2021-01-28 10:37:34 +00:00
{
static float f = 0.0f ;
static int counter = 0 ;
ImGui : : Begin ( " Hello, world! " ) ; // Create a window called "Hello, world!" and append into it.
ImGui : : Text ( " This is some useful text. " ) ; // Display some text (you can use a format strings too)
ImGui : : Checkbox ( " Demo Window " , & show_demo_window ) ; // Edit bools storing our window open/close state
ImGui : : Checkbox ( " Another Window " , & show_another_window ) ;
ImGui : : SliderFloat ( " float " , & f , 0.0f , 1.0f ) ; // Edit 1 float using a slider from 0.0f to 1.0f
ImGui : : ColorEdit3 ( " clear color " , ( float * ) & clear_color ) ; // Edit 3 floats representing a color
if ( ImGui : : Button ( " Button " ) ) // Buttons return true when clicked (most widgets return true when edited/activated)
counter + + ;
ImGui : : SameLine ( ) ;
ImGui : : Text ( " counter = %d " , counter ) ;
2023-03-10 17:35:03 +00:00
ImGui : : Text ( " Application average %.3f ms/frame (%.1f FPS) " , 1000.0f / io . Framerate , io . Framerate ) ;
2021-01-28 10:37:34 +00:00
ImGui : : End ( ) ;
}
// 3. Show another simple window.
if ( show_another_window )
{
ImGui : : Begin ( " Another Window " , & show_another_window ) ; // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
ImGui : : Text ( " Hello from another window! " ) ;
if ( ImGui : : Button ( " Close Me " ) )
show_another_window = false ;
ImGui : : End ( ) ;
}
2022-09-19 15:45:05 +00:00
2021-01-28 11:11:26 +00:00
// Rendering
2021-01-28 10:37:34 +00:00
ImGui : : Render ( ) ;
2021-05-16 16:55:58 +00:00
WGPURenderPassColorAttachment color_attachments = { } ;
2021-01-28 10:37:34 +00:00
color_attachments . loadOp = WGPULoadOp_Clear ;
color_attachments . storeOp = WGPUStoreOp_Store ;
2022-04-22 18:38:59 +00:00
color_attachments . clearValue = { clear_color . x * clear_color . w , clear_color . y * clear_color . w , clear_color . z * clear_color . w , clear_color . w } ;
2021-05-16 16:55:58 +00:00
color_attachments . view = wgpuSwapChainGetCurrentTextureView ( wgpu_swap_chain ) ;
2021-01-28 10:37:34 +00:00
WGPURenderPassDescriptor render_pass_desc = { } ;
render_pass_desc . colorAttachmentCount = 1 ;
render_pass_desc . colorAttachments = & color_attachments ;
2023-04-11 09:25:14 +00:00
render_pass_desc . depthStencilAttachment = nullptr ;
2021-01-28 10:37:34 +00:00
WGPUCommandEncoderDescriptor enc_desc = { } ;
WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder ( wgpu_device , & enc_desc ) ;
WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass ( encoder , & render_pass_desc ) ;
ImGui_ImplWGPU_RenderDrawData ( ImGui : : GetDrawData ( ) , pass ) ;
2022-03-15 11:32:49 +00:00
wgpuRenderPassEncoderEnd ( pass ) ;
2021-01-28 10:37:34 +00:00
WGPUCommandBufferDescriptor cmd_buffer_desc = { } ;
WGPUCommandBuffer cmd_buffer = wgpuCommandEncoderFinish ( encoder , & cmd_buffer_desc ) ;
2021-05-16 16:55:58 +00:00
WGPUQueue queue = wgpuDeviceGetQueue ( wgpu_device ) ;
2021-01-28 10:37:34 +00:00
wgpuQueueSubmit ( queue , 1 , & cmd_buffer ) ;
}
2021-01-28 11:11:26 +00:00
static void print_glfw_error ( int error , const char * description )
2021-01-28 10:37:34 +00:00
{
2023-02-02 15:27:33 +00:00
printf ( " GLFW Error %d: %s \n " , error , description ) ;
2021-01-28 10:37:34 +00:00
}
2021-01-28 11:11:26 +00:00
static void print_wgpu_error ( WGPUErrorType error_type , const char * message , void * )
2021-01-28 10:37:34 +00:00
{
const char * error_type_lbl = " " ;
2021-01-28 11:11:26 +00:00
switch ( error_type )
{
case WGPUErrorType_Validation : error_type_lbl = " Validation " ; break ;
case WGPUErrorType_OutOfMemory : error_type_lbl = " Out of memory " ; break ;
case WGPUErrorType_Unknown : error_type_lbl = " Unknown " ; break ;
case WGPUErrorType_DeviceLost : error_type_lbl = " Device lost " ; break ;
default : error_type_lbl = " Unknown " ;
2021-01-28 10:37:34 +00:00
}
printf ( " %s error: %s \n " , error_type_lbl , message ) ;
}