@ -11,13 +11,20 @@
# include "imgui_impl_glfw.h"
# include "imgui_impl_glfw.h"
# include "imgui_impl_wgpu.h"
# include "imgui_impl_wgpu.h"
# include <stdio.h>
# include <stdio.h>
# ifdef __EMSCRIPTEN__
# include <emscripten.h>
# include <emscripten.h>
# include <emscripten/html5.h>
# include <emscripten/html5.h>
# include <emscripten/html5_webgpu.h>
# include <emscripten/html5_webgpu.h>
# endif
# include <GLFW/glfw3.h>
# include <GLFW/glfw3.h>
# include <webgpu/webgpu.h>
# include <webgpu/webgpu.h>
# include <webgpu/webgpu_cpp.h>
# include <webgpu/webgpu_cpp.h>
// This example can also compile and run with Emscripten! See 'Makefile.emscripten' for details.
# ifdef __EMSCRIPTEN__
# include "../libs/emscripten/emscripten_mainloop_stub.h"
# endif
// Global WebGPU required states
// Global WebGPU required states
static WGPUDevice wgpu_device = nullptr ;
static WGPUDevice wgpu_device = nullptr ;
static WGPUSurface wgpu_surface = nullptr ;
static WGPUSurface wgpu_surface = nullptr ;
@ -27,15 +34,32 @@ static int wgpu_swap_chain_width = 0;
static int wgpu_swap_chain_height = 0 ;
static int wgpu_swap_chain_height = 0 ;
// Forward declarations
// Forward declarations
static void MainLoopStep ( void * window ) ;
static bool InitWGPU ( ) ;
static bool InitWGPU ( ) ;
static void print_glfw_error ( int error , const char * description ) ;
static void CreateSwapChain ( int width , int height ) ;
static void print_wgpu_error ( WGPUErrorType error_type , const char * message , void * ) ;
static void glfw_error_callback ( int error , const char * description )
{
printf ( " GLFW Error %d: %s \n " , error , description ) ;
}
static void wgpu_error_callback ( WGPUErrorType error_type , const char * message , void * )
{
const char * error_type_lbl = " " ;
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 " ;
}
printf ( " %s error: %s \n " , error_type_lbl , message ) ;
}
// Main code
// Main code
int main ( int , char * * )
int main ( int , char * * )
{
{
glfwSetErrorCallback ( print_glfw_error ) ;
glfwSetErrorCallback ( glfw_error_callback ) ;
if ( ! glfwInit ( ) )
if ( ! glfwInit ( ) )
return 1 ;
return 1 ;
@ -43,11 +67,8 @@ int main(int, char**)
// This needs to be done explicitly later.
// This needs to be done explicitly later.
glfwWindowHint ( GLFW_CLIENT_API , GLFW_NO_API ) ;
glfwWindowHint ( GLFW_CLIENT_API , GLFW_NO_API ) ;
GLFWwindow * window = glfwCreateWindow ( 1280 , 720 , " Dear ImGui GLFW+WebGPU example " , nullptr , nullptr ) ;
GLFWwindow * window = glfwCreateWindow ( 1280 , 720 , " Dear ImGui GLFW+WebGPU example " , nullptr , nullptr ) ;
if ( ! window )
if ( window = = nullptr )
{
glfwTerminate ( ) ;
return 1 ;
return 1 ;
}
// Initialize the WebGPU environment
// Initialize the WebGPU environment
if ( ! InitWGPU ( ) )
if ( ! InitWGPU ( ) )
@ -66,17 +87,15 @@ int main(int, char**)
io . ConfigFlags | = ImGuiConfigFlags_NavEnableKeyboard ; // Enable Keyboard Controls
io . ConfigFlags | = ImGuiConfigFlags_NavEnableKeyboard ; // Enable Keyboard Controls
io . ConfigFlags | = ImGuiConfigFlags_NavEnableGamepad ; // Enable Gamepad Controls
io . ConfigFlags | = ImGuiConfigFlags_NavEnableGamepad ; // Enable Gamepad Controls
// 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.
io . IniFilename = nullptr ;
// Setup Dear ImGui style
// Setup Dear ImGui style
ImGui : : StyleColorsDark ( ) ;
ImGui : : StyleColorsDark ( ) ;
//ImGui::StyleColorsLight();
//ImGui::StyleColorsLight();
// Setup Platform/Renderer backends
// Setup Platform/Renderer backends
ImGui_ImplGlfw_InitForOther ( window , true ) ;
ImGui_ImplGlfw_InitForOther ( window , true ) ;
# ifdef __EMSCRIPTEN__
ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback ( " #canvas " ) ;
ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback ( " #canvas " ) ;
# endif
ImGui_ImplWGPU_Init ( wgpu_device , 3 , wgpu_preferred_fmt , WGPUTextureFormat_Undefined ) ;
ImGui_ImplWGPU_Init ( wgpu_device , 3 , wgpu_preferred_fmt , WGPUTextureFormat_Undefined ) ;
// Load Fonts
// Load Fonts
@ -99,64 +118,35 @@ int main(int, char**)
//IM_ASSERT(font != nullptr);
//IM_ASSERT(font != nullptr);
# endif
# endif
// This function will directly return and exit the main function.
// Our state
// Make sure that no required objects get cleaned up.
bool show_demo_window = true ;
// This way we can use the browsers 'requestAnimationFrame' to control the rendering.
bool show_another_window = false ;
emscripten_set_main_loop_arg ( MainLoopStep , window , 0 , false ) ;
ImVec4 clear_color = ImVec4 ( 0.45f , 0.55f , 0.60f , 1.00f ) ;
return 0 ;
}
static bool InitWGPU ( )
{
wgpu_device = emscripten_webgpu_get_device ( ) ;
if ( ! wgpu_device )
return false ;
wgpuDeviceSetUncapturedErrorCallback ( wgpu_device , print_wgpu_error , nullptr ) ;
// Use C++ wrapper due to misbehavior in Emscripten.
// Some offset computation for wgpuInstanceCreateSurface in JavaScript
// seem to be inline with struct alignments in the C++ structure
wgpu : : SurfaceDescriptorFromCanvasHTMLSelector html_surface_desc = { } ;
html_surface_desc . selector = " #canvas " ;
wgpu : : SurfaceDescriptor surface_desc = { } ;
surface_desc . nextInChain = & html_surface_desc ;
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 . MoveToCHandle ( ) ;
return true ;
}
static void MainLoopStep ( void * window )
// Main loop
# ifdef __EMSCRIPTEN__
// 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.
io . IniFilename = nullptr ;
EMSCRIPTEN_MAINLOOP_BEGIN
# else
while ( ! glfwWindowShouldClose ( window ) )
# endif
{
{
ImGuiIO & io = ImGui : : GetIO ( ) ;
// Poll and handle events (inputs, window resize, etc.)
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
glfwPollEvents ( ) ;
glfwPollEvents ( ) ;
// React to changes in screen size
int width , height ;
int width , height ;
glfwGetFramebufferSize ( ( GLFWwindow * ) window , & width , & height ) ;
glfwGetFramebufferSize ( ( GLFWwindow * ) window , & width , & height ) ;
// React to changes in screen size
if ( width ! = wgpu_swap_chain_width & & height ! = wgpu_swap_chain_height )
if ( width ! = wgpu_swap_chain_width & & height ! = wgpu_swap_chain_height )
{
{
ImGui_ImplWGPU_InvalidateDeviceObjects ( ) ;
ImGui_ImplWGPU_InvalidateDeviceObjects ( ) ;
if ( wgpu_swap_chain )
CreateSwapChain ( width , height ) ;
wgpuSwapChainRelease ( wgpu_swap_chain ) ;
wgpu_swap_chain_width = width ;
wgpu_swap_chain_height = height ;
WGPUSwapChainDescriptor swap_chain_desc = { } ;
swap_chain_desc . usage = WGPUTextureUsage_RenderAttachment ;
swap_chain_desc . format = wgpu_preferred_fmt ;
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 ( ) ;
ImGui_ImplWGPU_CreateDeviceObjects ( ) ;
}
}
@ -165,12 +155,6 @@ static void MainLoopStep(void* window)
ImGui_ImplGlfw_NewFrame ( ) ;
ImGui_ImplGlfw_NewFrame ( ) ;
ImGui : : NewFrame ( ) ;
ImGui : : NewFrame ( ) ;
// 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 ) ;
// 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!).
// 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 )
if ( show_demo_window )
ImGui : : ShowDemoWindow ( & show_demo_window ) ;
ImGui : : ShowDemoWindow ( & show_demo_window ) ;
@ -216,6 +200,7 @@ static void MainLoopStep(void* window)
color_attachments . storeOp = WGPUStoreOp_Store ;
color_attachments . storeOp = WGPUStoreOp_Store ;
color_attachments . clearValue = { clear_color . x * clear_color . w , clear_color . y * clear_color . w , clear_color . z * clear_color . w , clear_color . w } ;
color_attachments . clearValue = { clear_color . x * clear_color . w , clear_color . y * clear_color . w , clear_color . z * clear_color . w , clear_color . w } ;
color_attachments . view = wgpuSwapChainGetCurrentTextureView ( wgpu_swap_chain ) ;
color_attachments . view = wgpuSwapChainGetCurrentTextureView ( wgpu_swap_chain ) ;
WGPURenderPassDescriptor render_pass_desc = { } ;
WGPURenderPassDescriptor render_pass_desc = { } ;
render_pass_desc . colorAttachmentCount = 1 ;
render_pass_desc . colorAttachmentCount = 1 ;
render_pass_desc . colorAttachments = & color_attachments ;
render_pass_desc . colorAttachments = & color_attachments ;
@ -233,22 +218,58 @@ static void MainLoopStep(void* window)
WGPUQueue queue = wgpuDeviceGetQueue ( wgpu_device ) ;
WGPUQueue queue = wgpuDeviceGetQueue ( wgpu_device ) ;
wgpuQueueSubmit ( queue , 1 , & cmd_buffer ) ;
wgpuQueueSubmit ( queue , 1 , & cmd_buffer ) ;
}
}
# ifdef __EMSCRIPTEN__
EMSCRIPTEN_MAINLOOP_END ;
# endif
static void print_glfw_error ( int error , const char * description )
// Cleanup
{
ImGui_ImplWGPU_Shutdown ( ) ;
printf ( " GLFW Error %d: %s \n " , error , description ) ;
ImGui_ImplGlfw_Shutdown ( ) ;
ImGui : : DestroyContext ( ) ;
glfwDestroyWindow ( window ) ;
glfwTerminate ( ) ;
return 0 ;
}
}
static void print_wgpu_error ( WGPUErrorType error_type , const char * message , void * )
static bool InitWGPU ( )
{
const char * error_type_lbl = " " ;
switch ( error_type )
{
{
case WGPUErrorType_Validation : error_type_lbl = " Validation " ; break ;
wgpu_device = emscripten_webgpu_get_device ( ) ;
case WGPUErrorType_OutOfMemory : error_type_lbl = " Out of memory " ; break ;
if ( ! wgpu_device )
case WGPUErrorType_Unknown : error_type_lbl = " Unknown " ; break ;
return false ;
case WGPUErrorType_DeviceLost : error_type_lbl = " Device lost " ; break ;
default : error_type_lbl = " Unknown " ;
wgpuDeviceSetUncapturedErrorCallback ( wgpu_device , wgpu_error_callback , nullptr ) ;
// Use C++ wrapper due to misbehavior in Emscripten.
// Some offset computation for wgpuInstanceCreateSurface in JavaScript
// seem to be inline with struct alignments in the C++ structure
wgpu : : SurfaceDescriptorFromCanvasHTMLSelector html_surface_desc = { } ;
html_surface_desc . selector = " #canvas " ;
wgpu : : SurfaceDescriptor surface_desc = { } ;
surface_desc . nextInChain = & html_surface_desc ;
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 . MoveToCHandle ( ) ;
return true ;
}
}
printf ( " %s error: %s \n " , error_type_lbl , message ) ;
static void CreateSwapChain ( int width , int height )
{
if ( wgpu_swap_chain )
wgpuSwapChainRelease ( wgpu_swap_chain ) ;
wgpu_swap_chain_width = width ;
wgpu_swap_chain_height = height ;
WGPUSwapChainDescriptor swap_chain_desc = { } ;
swap_chain_desc . usage = WGPUTextureUsage_RenderAttachment ;
swap_chain_desc . format = wgpu_preferred_fmt ;
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 ) ;
}
}