Skip to content

Commit

Permalink
fix metal, wgpu and html5 raw samples for Dear ImGui 1.91.5
Browse files Browse the repository at this point in the history
  • Loading branch information
floooh committed Dec 5, 2024
1 parent af4d923 commit 25c3f60
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 124 deletions.
93 changes: 24 additions & 69 deletions html5/imgui-emsc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ static bool show_another_window = false;
static sg_pass_action pass_action;
static sg_pipeline pip;
static sg_bindings bind;
static bool btn_down[3];
static bool btn_up[3];

typedef struct {
ImVec2 disp_size;
} vs_params_t;

static EM_BOOL draw(double time, void* userdata);
static void draw_imgui(ImDrawData*);
static ImGuiKey as_imgui_key(int keycode);

int main() {
// setup WebGL context
Expand All @@ -58,38 +57,18 @@ int main() {
ImGuiIO& io = ImGui::GetIO();
io.IniFilename = nullptr;
io.Fonts->AddFontDefault();
// emscripten has no clearly defined key code constants
io.KeyMap[ImGuiKey_Tab] = 9;
io.KeyMap[ImGuiKey_LeftArrow] = 37;
io.KeyMap[ImGuiKey_RightArrow] = 39;
io.KeyMap[ImGuiKey_UpArrow] = 38;
io.KeyMap[ImGuiKey_DownArrow] = 40;
io.KeyMap[ImGuiKey_Home] = 36;
io.KeyMap[ImGuiKey_End] = 35;
io.KeyMap[ImGuiKey_Delete] = 46;
io.KeyMap[ImGuiKey_Backspace] = 8;
io.KeyMap[ImGuiKey_Enter] = 13;
io.KeyMap[ImGuiKey_Escape] = 27;
io.KeyMap[ImGuiKey_A] = 65;
io.KeyMap[ImGuiKey_C] = 67;
io.KeyMap[ImGuiKey_V] = 86;
io.KeyMap[ImGuiKey_X] = 88;
io.KeyMap[ImGuiKey_Y] = 89;
io.KeyMap[ImGuiKey_Z] = 90;

// emscripten to ImGui input forwarding
emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, nullptr, true,
[](int, const EmscriptenKeyboardEvent* e, void*)->EM_BOOL {
if (e->keyCode < 512) {
ImGui::GetIO().KeysDown[e->keyCode] = true;
}
ImGui::GetIO().AddKeyEvent(as_imgui_key(e->keyCode), true);
// only forward alpha-numeric keys to browser
return e->keyCode < 32;
});
emscripten_set_keyup_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, nullptr, true,
[](int, const EmscriptenKeyboardEvent* e, void*)->EM_BOOL {
if (e->keyCode < 512) {
ImGui::GetIO().KeysDown[e->keyCode] = false;
ImGui::GetIO().AddKeyEvent(as_imgui_key(e->keyCode), false);
}
// only forward alpha-numeric keys to browser
return e->keyCode < 32;
Expand All @@ -101,48 +80,22 @@ int main() {
});
emscripten_set_mousedown_callback("canvas", nullptr, true,
[](int, const EmscriptenMouseEvent* e, void*)->EM_BOOL {
switch (e->button) {
case 0: btn_down[0] = true; break;
case 2: btn_down[1] = true; break;
}
ImGui::GetIO().AddMouseButtonEvent(e->button, true);
return true;
});
emscripten_set_mouseup_callback("canvas", nullptr, true,
[](int, const EmscriptenMouseEvent* e, void*)->EM_BOOL {
switch (e->button) {
case 0: btn_up[0] = true; break;
case 2: btn_up[1] = true; break;
}
return true;
});
emscripten_set_mouseenter_callback("canvas", nullptr, true,
[](int, const EmscriptenMouseEvent*, void*)->EM_BOOL {
auto& io = ImGui::GetIO();
for (int i = 0; i < 3; i++) {
btn_down[i] = btn_up[i] = false;
io.MouseDown[i] = false;
}
return true;
});
emscripten_set_mouseleave_callback("canvas", nullptr, true,
[](int, const EmscriptenMouseEvent*, void*)->EM_BOOL {
auto& io = ImGui::GetIO();
for (int i = 0; i < 3; i++) {
btn_down[i] = btn_up[i] = false;
io.MouseDown[i] = false;
}
ImGui::GetIO().AddMouseButtonEvent(e->button, false);
return true;
});
emscripten_set_mousemove_callback("canvas", nullptr, true,
[](int, const EmscriptenMouseEvent* e, void*)->EM_BOOL {
ImGui::GetIO().MousePos.x = (float) e->targetX;
ImGui::GetIO().MousePos.y = (float) e->targetY;
ImGui::GetIO().AddMousePosEvent((float)e->targetX, (float)e->targetY);
return true;
});
emscripten_set_wheel_callback("canvas", nullptr, true,
[](int, const EmscriptenWheelEvent* e, void*)->EM_BOOL {
ImGui::GetIO().MouseWheelH = -0.1f * (float)e->deltaX;
ImGui::GetIO().MouseWheel = -0.1f * (float)e->deltaY;
ImGui::GetIO().AddMouseWheelEvent(-0.1f * (float)e->deltaX, -0.1f * (float)e->deltaY);
return true;
});

Expand Down Expand Up @@ -254,21 +207,6 @@ static EM_BOOL draw(double time, void* userdata) {
ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2(float(emsc_width()), float(emsc_height()));
io.DeltaTime = (float) stm_sec(stm_laptime(&last_time));
// this mouse button handling fixes the problem when down- and up-events happen in the same frame
for (int i = 0; i < 3; i++) {
if (io.MouseDown[i]) {
if (btn_up[i]) {
io.MouseDown[i] = false;
btn_up[i] = false;
}
}
else {
if (btn_down[i]) {
io.MouseDown[i] = true;
btn_down[i] = false;
}
}
}
ImGui::NewFrame();

// 1. Show a simple window
Expand Down Expand Up @@ -356,3 +294,20 @@ void draw_imgui(ImDrawData* draw_data) {
}
}
}

ImGuiKey as_imgui_key(int keycode) {
switch (keycode) {
case 9: return ImGuiKey_Tab;
case 37: return ImGuiKey_LeftArrow;
case 39: return ImGuiKey_RightArrow;
case 38: return ImGuiKey_UpArrow;
case 40: return ImGuiKey_DownArrow;
case 36: return ImGuiKey_Home;
case 35: return ImGuiKey_End;
case 46: return ImGuiKey_Delete;
case 8: return ImGuiKey_Backspace;
case 13: return ImGuiKey_Enter;
case 27: return ImGuiKey_Escape;
default: return ImGuiKey_None;
};
}
51 changes: 25 additions & 26 deletions metal/imgui-metal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ static bool show_test_window = true;
static bool show_another_window = false;
static sg_pass_action pass_action;

static ImGuiKey as_imgui_key(int keycode);

void init() {
// setup sokol_gfx and sokol_time
const sg_desc desc = {
Expand All @@ -40,33 +42,13 @@ void init() {
};
simgui_setup(&simgui_desc);

// setup the imgui environment
ImGuiIO& io = ImGui::GetIO();
io.KeyMap[ImGuiKey_Tab] = 0x30;
io.KeyMap[ImGuiKey_LeftArrow] = 0x7B;
io.KeyMap[ImGuiKey_RightArrow] = 0x7C;
io.KeyMap[ImGuiKey_DownArrow] = 0x7D;
io.KeyMap[ImGuiKey_UpArrow] = 0x7E;
io.KeyMap[ImGuiKey_Home] = 0x73;
io.KeyMap[ImGuiKey_End] = 0x77;
io.KeyMap[ImGuiKey_Delete] = 0x75;
io.KeyMap[ImGuiKey_Backspace] = 0x33;
io.KeyMap[ImGuiKey_Enter] = 0x24;
io.KeyMap[ImGuiKey_Escape] = 0x35;
io.KeyMap[ImGuiKey_A] = 0x00;
io.KeyMap[ImGuiKey_C] = 0x08;
io.KeyMap[ImGuiKey_V] = 0x09;
io.KeyMap[ImGuiKey_X] = 0x07;
io.KeyMap[ImGuiKey_Y] = 0x10;
io.KeyMap[ImGuiKey_Z] = 0x06;

// OSX => ImGui input forwarding
osx_mouse_pos([] (float x, float y) { ImGui::GetIO().MousePos = ImVec2(x, y); });
osx_mouse_btn_down([] (int btn) { ImGui::GetIO().MouseDown[btn] = true; });
osx_mouse_btn_up([] (int btn) { ImGui::GetIO().MouseDown[btn] = false; });
osx_mouse_wheel([] (float v) { ImGui::GetIO().MouseWheel = 0.25f * v; });
osx_key_down([] (int key) { if (key < 512) ImGui::GetIO().KeysDown[key] = true; });
osx_key_up([] (int key) { if (key < 512) ImGui::GetIO().KeysDown[key] = false; });
osx_mouse_pos([] (float x, float y) { ImGui::GetIO().AddMousePosEvent(x, y); });
osx_mouse_btn_down([] (int btn) { ImGui::GetIO().AddMouseButtonEvent(btn, true); });
osx_mouse_btn_up([] (int btn) { ImGui::GetIO().AddMouseButtonEvent(btn, false); });
osx_mouse_wheel([] (float v) { ImGui::GetIO().AddMouseWheelEvent(0.0f, 0.25f * v); });
osx_key_down([] (int key) { ImGui::GetIO().AddKeyEvent(as_imgui_key(key), true); });
osx_key_up([] (int key) { ImGui::GetIO().AddKeyEvent(as_imgui_key(key), false); });
osx_char([] (wchar_t c) { ImGui::GetIO().AddInputCharacter(c); });

// initial clear color
Expand Down Expand Up @@ -120,3 +102,20 @@ int main() {
osx_start(WIDTH, HEIGHT, 1, SG_PIXELFORMAT_NONE, "Sokol Dear ImGui (Metal)", init, frame, shutdown);
return 0;
}

static ImGuiKey as_imgui_key(int keycode) {
switch (keycode) {
case 0x30: return ImGuiKey_Tab;
case 0x7B: return ImGuiKey_LeftArrow;
case 0x7C: return ImGuiKey_RightArrow;
case 0x7D: return ImGuiKey_DownArrow;
case 0x7E: return ImGuiKey_UpArrow;
case 0x73: return ImGuiKey_Home;
case 0x77: return ImGuiKey_End;
case 0x75: return ImGuiKey_Delete;
case 0x33: return ImGuiKey_Backspace;
case 0x24: return ImGuiKey_Enter;
case 0x35: return ImGuiKey_Escape;
default: return ImGuiKey_None;
}
}
2 changes: 1 addition & 1 deletion wgpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fips_end_app()

fips_begin_app(drawcallperf-wgpu windowed)
fips_files(drawcallperf-wgpu.c)
fips_deps(wgpu_entry cimgui)
fips_deps(wgpu_entry imgui)
fips_end_app()

fips_begin_app(vertexpulling-wgpu windowed)
Expand Down
9 changes: 4 additions & 5 deletions wgpu/drawcallperf-wgpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
#include "sokol_gfx.h"
#include "sokol_log.h"
#include "sokol_time.h"
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#include "cimgui/cimgui.h"
#include "cimgui.h"
#define SOKOL_IMGUI_IMPL
#define SOKOL_IMGUI_NO_SOKOL_APP
#include "sokol_imgui.h"
Expand Down Expand Up @@ -278,13 +277,13 @@ static void frame(void) {
});

// control ui
igSetNextWindowPos((ImVec2){20,20}, ImGuiCond_Once, (ImVec2){0,0});
igSetNextWindowPos((ImVec2){20,20}, ImGuiCond_Once);
igSetNextWindowSize((ImVec2){600,200}, ImGuiCond_Once);
if (igBegin("Controls", 0, ImGuiWindowFlags_NoResize)) {
igText("Each cube/instance is 1 16-byte uniform update and 1 draw call\n");
igText("DC/texture is the number of adjacent draw calls with the same texture binding\n");
igSliderInt("Num Instances", &state.num_instances, 100, MAX_INSTANCES, "%d", ImGuiSliderFlags_Logarithmic);
igSliderInt("DC/texture", &state.bind_frequency, 1, MAX_BIND_FREQUENCY, "%d", ImGuiSliderFlags_Logarithmic);
igSliderIntEx("Num Instances", &state.num_instances, 100, MAX_INSTANCES, "%d", ImGuiSliderFlags_Logarithmic);
igSliderIntEx("DC/texture", &state.bind_frequency, 1, MAX_BIND_FREQUENCY, "%d", ImGuiSliderFlags_Logarithmic);
igText("Frame duration: %.4fms", frame_measured_time * 1000.0);
igText("sg_apply_bindings(): %d\n", state.stats.num_binding_updates);
igText("sg_apply_uniforms(): %d\n", state.stats.num_uniform_updates);
Expand Down
47 changes: 24 additions & 23 deletions wgpu/imgui-wgpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ typedef struct {
} vs_params_t;

static void draw_imgui(ImDrawData*);
static ImGuiKey as_imgui_key(uint32_t keycode);

static void init(void) {
// setup sokol-gfx, sokol-time and sokol-imgui
Expand All @@ -40,37 +41,20 @@ static void init(void) {
stm_setup();

// input forwarding
wgpu_mouse_pos([] (float x, float y) { ImGui::GetIO().MousePos = ImVec2(x, y); });
wgpu_mouse_btn_down([] (int btn) { ImGui::GetIO().MouseDown[btn] = true; });
wgpu_mouse_btn_up([] (int btn) { ImGui::GetIO().MouseDown[btn] = false; });
wgpu_mouse_wheel([](float v) { ImGui::GetIO().MouseWheel = v; });
wgpu_mouse_pos([] (float x, float y) { ImGui::GetIO().AddMousePosEvent(x, y); });
wgpu_mouse_btn_down([] (int btn) { ImGui::GetIO().AddMouseButtonEvent(btn, true); });
wgpu_mouse_btn_up([] (int btn) { ImGui::GetIO().AddMouseButtonEvent(btn, false); });
wgpu_mouse_wheel([](float v) { ImGui::GetIO().AddMouseWheelEvent(0, v); });
wgpu_char([] (uint32_t c) { ImGui::GetIO().AddInputCharacter(c); });
wgpu_key_down([] (int key) { if (key < 512) ImGui::GetIO().KeysDown[key] = true; });
wgpu_key_up([] (int key) { if (key < 512) ImGui::GetIO().KeysDown[key] = false; });
wgpu_key_down([] (int key) { ImGui::GetIO().AddKeyEvent(as_imgui_key(key), true); });
wgpu_key_up([] (int key) { ImGui::GetIO().AddKeyEvent(as_imgui_key(key), false); });

// setup Dear Imgui
ImGui::CreateContext();
ImGui::StyleColorsDark();
ImGuiIO& io = ImGui::GetIO();
io.IniFilename = nullptr;
io.Fonts->AddFontDefault();
io.KeyMap[ImGuiKey_Tab] = WGPU_KEY_TAB;
io.KeyMap[ImGuiKey_LeftArrow] = WGPU_KEY_LEFT;
io.KeyMap[ImGuiKey_RightArrow] = WGPU_KEY_RIGHT;
io.KeyMap[ImGuiKey_UpArrow] = WGPU_KEY_UP;
io.KeyMap[ImGuiKey_DownArrow] = WGPU_KEY_DOWN;
io.KeyMap[ImGuiKey_Home] = WGPU_KEY_HOME;
io.KeyMap[ImGuiKey_End] = WGPU_KEY_END;
io.KeyMap[ImGuiKey_Delete] = WGPU_KEY_DELETE;
io.KeyMap[ImGuiKey_Backspace] = WGPU_KEY_BACKSPACE;
io.KeyMap[ImGuiKey_Enter] = WGPU_KEY_ENTER;
io.KeyMap[ImGuiKey_Escape] = WGPU_KEY_ESCAPE;
io.KeyMap[ImGuiKey_A] = 'A';
io.KeyMap[ImGuiKey_C] = 'C';
io.KeyMap[ImGuiKey_V] = 'V';
io.KeyMap[ImGuiKey_X] = 'X';
io.KeyMap[ImGuiKey_Y] = 'Y';
io.KeyMap[ImGuiKey_Z] = 'Z';

// dynamic vertex- and index-buffers for imgui-generated geometry
sg_buffer_desc vbuf_desc = { };
Expand Down Expand Up @@ -260,6 +244,23 @@ void draw_imgui(ImDrawData* draw_data) {
}
}

static ImGuiKey as_imgui_key(uint32_t keycode) {
switch (keycode) {
case WGPU_KEY_TAB: return ImGuiKey_Tab;
case WGPU_KEY_LEFT: return ImGuiKey_LeftArrow;
case WGPU_KEY_RIGHT: return ImGuiKey_RightArrow;
case WGPU_KEY_UP: return ImGuiKey_UpArrow;
case WGPU_KEY_DOWN: return ImGuiKey_DownArrow;
case WGPU_KEY_HOME: return ImGuiKey_Home;
case WGPU_KEY_END: return ImGuiKey_End;
case WGPU_KEY_DELETE: return ImGuiKey_Delete;
case WGPU_KEY_BACKSPACE: return ImGuiKey_Backspace;
case WGPU_KEY_ENTER: return ImGuiKey_Enter;
case WGPU_KEY_ESCAPE: return ImGuiKey_Escape;
default: return ImGuiKey_None;
}
}

int main() {
wgpu_desc_t desc = { };
desc.init_cb = init;
Expand Down

0 comments on commit 25c3f60

Please sign in to comment.