Dear ImGui Implementation (#598)

* added imgui as dependency

* imgui renderer/basic input implementation

* imgui: add layers system

Add video info layer to show fps. Press F10 to toggle it.

* imgui: add custom imgui config

* imgui: gamepad capture, stopping propagation

* imgui: changed config & log file path to use portable dir

* videoout: render blank frame when video output is closed

required to render imgui even when game has no video output

- fixed merge compile-error
This commit is contained in:
Vinicius Rangel 2024-09-08 16:50:32 -03:00 committed by GitHub
parent f1becb2507
commit 035cb3eeaa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 2386 additions and 8 deletions

5
.gitmodules vendored
View file

@ -85,3 +85,8 @@
[submodule "externals/half"]
path = externals/half
url = https://github.com/ROCm/half.git
[submodule "externals/dear_imgui"]
path = externals/dear_imgui
url = https://github.com/shadps4-emu/ext-imgui.git
shallow = true
branch = docking

View file

@ -561,6 +561,18 @@ set(VIDEO_CORE src/video_core/amdgpu/liverpool.cpp
src/video_core/renderdoc.h
)
set(IMGUI src/imgui/imgui_config.h
src/imgui/imgui_layer.h
src/imgui/layer/video_info.cpp
src/imgui/layer/video_info.h
src/imgui/renderer/imgui_core.cpp
src/imgui/renderer/imgui_core.h
src/imgui/renderer/imgui_impl_sdl3.cpp
src/imgui/renderer/imgui_impl_sdl3.h
src/imgui/renderer/imgui_impl_vulkan.cpp
src/imgui/renderer/imgui_impl_vulkan.h
)
set(INPUT src/input/controller.cpp
src/input/controller.h
)
@ -617,6 +629,7 @@ endif()
if (ENABLE_QT_GUI)
qt_add_executable(shadps4
${AUDIO_CORE}
${IMGUI}
${INPUT}
${QT_GUI}
${COMMON}
@ -629,6 +642,7 @@ if (ENABLE_QT_GUI)
else()
add_executable(shadps4
${AUDIO_CORE}
${IMGUI}
${INPUT}
${COMMON}
${CORE}
@ -645,9 +659,12 @@ endif()
create_target_directory_groups(shadps4)
target_link_libraries(shadps4 PRIVATE magic_enum::magic_enum fmt::fmt toml11::toml11 tsl::robin_map xbyak::xbyak Tracy::TracyClient RenderDoc::API FFmpeg::ffmpeg)
target_link_libraries(shadps4 PRIVATE magic_enum::magic_enum fmt::fmt toml11::toml11 tsl::robin_map xbyak::xbyak Tracy::TracyClient RenderDoc::API FFmpeg::ffmpeg Dear_ImGui)
target_link_libraries(shadps4 PRIVATE Boost::headers GPUOpen::VulkanMemoryAllocator sirit Vulkan::Headers xxHash::xxhash Zydis::Zydis glslang::SPIRV glslang::glslang SDL3::SDL3)
target_compile_definitions(shadps4 PRIVATE IMGUI_USER_CONFIG="imgui/imgui_config.h")
target_compile_definitions(Dear_ImGui PRIVATE IMGUI_USER_CONFIG="${PROJECT_SOURCE_DIR}/src/imgui/imgui_config.h")
if (APPLE)
option(USE_SYSTEM_VULKAN_LOADER "Enables using the system Vulkan loader instead of directly linking with MoltenVK. Useful for loading validation layers." OFF)
if (USE_SYSTEM_VULKAN_LOADER)

View file

@ -155,6 +155,17 @@ if (APPLE)
endif()
endif()
# Dear ImGui
add_library(Dear_ImGui
dear_imgui/imgui.cpp
dear_imgui/imgui_demo.cpp
dear_imgui/imgui_draw.cpp
dear_imgui/imgui_internal.h
dear_imgui/imgui_tables.cpp
dear_imgui/imgui_widgets.cpp
)
target_include_directories(Dear_ImGui INTERFACE dear_imgui/)
# Tracy
option(TRACY_ENABLE "" ON)
option(TRACY_NO_CRASH_HANDLER "" ON) # Otherwise texture cache exceptions will be treaten as a crash

1
externals/dear_imgui vendored Submodule

@ -0,0 +1 @@
Subproject commit 636cd4a7d623a2bc9bf59bb3acbb4ca075befba3

View file

@ -18,3 +18,8 @@ void assert_fail_impl() {
Crash();
throw std::runtime_error("Unreachable code");
}
void assert_fail_debug_msg(const char* msg) {
LOG_CRITICAL(Debug, "Assertion Failed!\n{}", msg);
assert_fail_impl();
}

View file

@ -117,6 +117,7 @@ bool ParseFilterRule(Filter& instance, Iterator begin, Iterator end) {
CLS(Render) \
SUB(Render, Vulkan) \
SUB(Render, Recompiler) \
CLS(ImGui) \
CLS(Input) \
CLS(Tty) \
CLS(Loader)

View file

@ -84,6 +84,7 @@ enum class Class : u8 {
Render, ///< Video Core
Render_Vulkan, ///< Vulkan backend
Render_Recompiler, ///< Shader recompiler
ImGui, ///< ImGui
Loader, ///< ROM loader
Input, ///< Input emulation
Tty, ///< Debug output from emu

View file

@ -161,10 +161,6 @@ int VideoOutDriver::UnregisterBuffers(VideoOutPort* port, s32 attributeIndex) {
}
std::chrono::microseconds VideoOutDriver::Flip(const Request& req) {
if (!req) {
return std::chrono::microseconds{0};
}
const auto start = std::chrono::high_resolution_clock::now();
// Whatever the game is rendering show splash if it is active
@ -207,6 +203,11 @@ std::chrono::microseconds VideoOutDriver::Flip(const Request& req) {
return std::chrono::duration_cast<std::chrono::microseconds>(end - start);
}
void VideoOutDriver::DrawBlankFrame() {
const auto empty_frame = renderer->PrepareBlankFrame(false);
renderer->Present(empty_frame);
}
bool VideoOutDriver::SubmitFlip(VideoOutPort* port, s32 index, s64 flip_arg,
bool is_eop /*= false*/) {
{
@ -283,7 +284,14 @@ void VideoOutDriver::PresentThread(std::stop_token token) {
auto& vblank_status = main_port.vblank_status;
if (vblank_status.count % (main_port.flip_rate + 1) == 0) {
const auto request = receive_request();
delay = Flip(request);
if (!request) {
delay = std::chrono::microseconds{0};
if (!main_port.is_open) {
DrawBlankFrame();
}
} else {
delay = Flip(request);
}
FRAME_END;
}

View file

@ -102,6 +102,7 @@ private:
};
std::chrono::microseconds Flip(const Request& req);
void DrawBlankFrame(); // Used when there is no flip request to keep ImGui up to date
void SubmitFlipInternal(VideoOutPort* port, s32 index, s64 flip_arg, bool is_eop = false);
void PresentThread(std::stop_token token);

29
src/imgui/imgui_config.h Normal file
View file

@ -0,0 +1,29 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
// WARNING: All includes from this file must be relative to allow Dear_ImGui project to compile
// without having this project include paths.
#include <cstdint>
extern void assert_fail_debug_msg(const char* msg);
#define ImDrawIdx std::uint32_t
#define IM_STRINGIZE(x) IM_STRINGIZE2(x)
#define IM_STRINGIZE2(x) #x
#define IM_ASSERT(_EXPR) \
([&]() { \
if (!(_EXPR)) [[unlikely]] { \
assert_fail_debug_msg(#_EXPR " at " __FILE__ ":" IM_STRINGIZE(__LINE__)); \
} \
}())
#define IMGUI_USE_WCHAR32
#define IMGUI_ENABLE_STB_TRUETYPE
#define IMGUI_DEFINE_MATH_OPERATORS
#define IM_VEC2_CLASS_EXTRA \
constexpr ImVec2(float _v) : x(_v), y(_v) {}

21
src/imgui/imgui_layer.h Normal file
View file

@ -0,0 +1,21 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
namespace ImGui {
class Layer {
public:
virtual ~Layer() = default;
static void AddLayer(Layer* layer);
static void RemoveLayer(Layer* layer);
virtual void Draw() = 0;
virtual bool ShouldGrabGamepad() {
return false;
}
};
} // namespace ImGui

View file

@ -0,0 +1,16 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <imgui.h>
#include "video_info.h"
void ImGui::Layers::VideoInfo::Draw() {
const ImGuiIO& io = GetIO();
m_show = IsKeyPressed(ImGuiKey_F10, false) ^ m_show;
if (m_show && Begin("Video Info")) {
Text("Frame time: %.3f ms (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
End();
}
}

View file

@ -0,0 +1,23 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "imgui/imgui_layer.h"
namespace Vulkan {
class RendererVulkan;
}
namespace ImGui::Layers {
class VideoInfo : public Layer {
bool m_show = false;
::Vulkan::RendererVulkan* renderer{};
public:
explicit VideoInfo(::Vulkan::RendererVulkan* renderer) : renderer(renderer) {}
void Draw() override;
};
} // namespace ImGui::Layers

View file

@ -0,0 +1,213 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <SDL3/SDL_events.h>
#include <imgui.h>
#include "common/config.h"
#include "common/path_util.h"
#include "imgui/imgui_layer.h"
#include "imgui_core.h"
#include "imgui_impl_sdl3.h"
#include "imgui_impl_vulkan.h"
#include "sdl_window.h"
#include "video_core/renderer_vulkan/renderer_vulkan.h"
static void CheckVkResult(const vk::Result err) {
LOG_ERROR(ImGui, "Vulkan error {}", vk::to_string(err));
}
static std::vector<ImGui::Layer*> layers;
// Update layers before rendering to allow layer changes to be applied during rendering.
// Using deque to keep the order of changes in case a Layer is removed then added again between
// frames.
static std::deque<std::pair<bool, ImGui::Layer*>> change_layers;
static std::mutex change_layers_mutex{};
namespace ImGui {
namespace Core {
void Initialize(const ::Vulkan::Instance& instance, const Frontend::WindowSDL& window,
const u32 image_count, vk::Format surface_format,
const vk::AllocationCallbacks* allocator) {
const auto config_path = GetUserPath(Common::FS::PathType::UserDir) / "imgui.ini";
const auto log_path = GetUserPath(Common::FS::PathType::LogDir) / "imgui_log.txt";
CreateContext();
ImGuiIO& io = GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
io.DisplaySize = ImVec2((float)window.getWidth(), (float)window.getHeight());
io.IniFilename = SDL_strdup(config_path.string().c_str());
io.LogFilename = SDL_strdup(log_path.string().c_str());
StyleColorsDark();
Sdl::Init(window.GetSdlWindow());
const Vulkan::InitInfo vk_info{
.instance = instance.GetInstance(),
.physical_device = instance.GetPhysicalDevice(),
.device = instance.GetDevice(),
.queue_family = instance.GetPresentQueueFamilyIndex(),
.queue = instance.GetPresentQueue(),
.image_count = image_count,
.min_allocation_size = 1024 * 1024,
.pipeline_rendering_create_info{
.colorAttachmentCount = 1,
.pColorAttachmentFormats = &surface_format,
},
.allocator = allocator,
.check_vk_result_fn = &CheckVkResult,
};
Vulkan::Init(vk_info);
}
void OnResize() {
Sdl::OnResize();
}
void Shutdown(const vk::Device& device) {
device.waitIdle();
const ImGuiIO& io = GetIO();
const auto ini_filename = (void*)io.IniFilename;
const auto log_filename = (void*)io.LogFilename;
Vulkan::Shutdown();
Sdl::Shutdown();
DestroyContext();
SDL_free(ini_filename);
SDL_free(log_filename);
}
bool ProcessEvent(SDL_Event* event) {
Sdl::ProcessEvent(event);
switch (event->type) {
case SDL_EVENT_MOUSE_MOTION:
case SDL_EVENT_MOUSE_WHEEL:
case SDL_EVENT_MOUSE_BUTTON_DOWN:
case SDL_EVENT_MOUSE_BUTTON_UP:
return GetIO().WantCaptureMouse;
case SDL_EVENT_TEXT_INPUT:
case SDL_EVENT_KEY_DOWN:
case SDL_EVENT_KEY_UP:
return GetIO().WantCaptureKeyboard;
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
case SDL_EVENT_GAMEPAD_BUTTON_UP:
case SDL_EVENT_GAMEPAD_AXIS_MOTION:
case SDL_EVENT_GAMEPAD_ADDED:
case SDL_EVENT_GAMEPAD_REMOVED:
case SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN:
case SDL_EVENT_GAMEPAD_TOUCHPAD_UP:
case SDL_EVENT_GAMEPAD_TOUCHPAD_MOTION:
return (GetIO().BackendFlags & ImGuiBackendFlags_HasGamepad) != 0;
default:
return false;
}
}
void NewFrame() {
{
std::scoped_lock lock{change_layers_mutex};
while (!change_layers.empty()) {
const auto [to_be_added, layer] = change_layers.front();
if (to_be_added) {
layers.push_back(layer);
} else {
const auto [begin, end] = std::ranges::remove(layers, layer);
layers.erase(begin, end);
}
change_layers.pop_front();
}
}
Vulkan::NewFrame();
Sdl::NewFrame();
ImGui::NewFrame();
bool capture_gamepad = false;
for (auto* layer : layers) {
layer->Draw();
if (layer->ShouldGrabGamepad()) {
capture_gamepad = true;
}
}
if (capture_gamepad) {
GetIO().BackendFlags |= ImGuiBackendFlags_HasGamepad;
} else {
GetIO().BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
}
}
void Render(const vk::CommandBuffer& cmdbuf, ::Vulkan::Frame* frame) {
ImGui::Render();
ImDrawData* draw_data = GetDrawData();
if (draw_data->CmdListsCount == 0) {
return;
}
if (Config::vkMarkersEnabled()) {
cmdbuf.beginDebugUtilsLabelEXT(vk::DebugUtilsLabelEXT{
.pLabelName = "ImGui Render",
});
}
cmdbuf.pipelineBarrier(vk::PipelineStageFlagBits::eColorAttachmentOutput,
vk::PipelineStageFlagBits::eColorAttachmentOutput, {}, {}, {},
{vk::ImageMemoryBarrier{
.srcAccessMask = vk::AccessFlagBits::eColorAttachmentWrite,
.dstAccessMask = vk::AccessFlagBits::eColorAttachmentRead,
.oldLayout = vk::ImageLayout::eUndefined,
.newLayout = vk::ImageLayout::eColorAttachmentOptimal,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = frame->image,
.subresourceRange{
.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = VK_REMAINING_ARRAY_LAYERS,
},
}});
vk::RenderingAttachmentInfo color_attachments[1]{
{
.imageView = frame->image_view,
.imageLayout = vk::ImageLayout::eColorAttachmentOptimal,
.loadOp = vk::AttachmentLoadOp::eLoad,
.storeOp = vk::AttachmentStoreOp::eStore,
},
};
vk::RenderingInfo render_info = {};
render_info.renderArea = {
.offset = {0, 0},
.extent = {frame->width, frame->height},
};
render_info.layerCount = 1;
render_info.colorAttachmentCount = 1;
render_info.pColorAttachments = color_attachments;
cmdbuf.beginRendering(render_info);
Vulkan::RenderDrawData(*draw_data, cmdbuf);
cmdbuf.endRendering();
if (Config::vkMarkersEnabled()) {
cmdbuf.endDebugUtilsLabelEXT();
}
}
} // namespace Core
void Layer::AddLayer(Layer* layer) {
std::scoped_lock lock{change_layers_mutex};
change_layers.emplace_back(true, layer);
}
void Layer::RemoveLayer(Layer* layer) {
std::scoped_lock lock{change_layers_mutex};
change_layers.emplace_back(false, layer);
}
} // namespace ImGui

View file

@ -0,0 +1,31 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "video_core/renderer_vulkan/vk_instance.h"
#include "vulkan/vulkan_handles.hpp"
union SDL_Event;
namespace Vulkan {
struct Frame;
}
namespace ImGui::Core {
void Initialize(const Vulkan::Instance& instance, const Frontend::WindowSDL& window,
u32 image_count, vk::Format surface_format,
const vk::AllocationCallbacks* allocator = nullptr);
void OnResize();
void Shutdown(const vk::Device& device);
bool ProcessEvent(SDL_Event* event);
void NewFrame();
void Render(const vk::CommandBuffer& cmdbuf, Vulkan::Frame* frame);
} // namespace ImGui::Core

View file

@ -0,0 +1,789 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
// Based on imgui_impl_sdl3.cpp from Dear ImGui repository
#include <imgui.h>
#include "imgui_impl_sdl3.h"
// SDL
#include <SDL3/SDL.h>
#if defined(__APPLE__)
#include <TargetConditionals.h>
#endif
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <Windows.h>
#endif
namespace ImGui::Sdl {
// SDL Data
struct SdlData {
SDL_Window* window{};
SDL_WindowID window_id{};
Uint64 time{};
const char* clipboard_text_data{};
// IME handling
SDL_Window* ime_window{};
// Mouse handling
Uint32 mouse_window_id{};
int mouse_buttons_down{};
SDL_Cursor* mouse_cursors[ImGuiMouseCursor_COUNT]{};
SDL_Cursor* mouse_last_cursor{};
int mouse_pending_leave_frame{};
// Gamepad handling
ImVector<SDL_Gamepad*> gamepads{};
GamepadMode gamepad_mode{};
bool want_update_gamepads_list{};
};
// Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui
// contexts It is STRONGLY preferred that you use docking branch with multi-viewports (== single
// Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
static SdlData* GetBackendData() {
return ImGui::GetCurrentContext() ? (SdlData*)ImGui::GetIO().BackendPlatformUserData : nullptr;
}
static const char* GetClipboardText(ImGuiContext*) {
SdlData* bd = GetBackendData();
if (bd->clipboard_text_data)
SDL_free((void*)bd->clipboard_text_data);
const char* sdl_clipboard_text = SDL_GetClipboardText();
bd->clipboard_text_data = sdl_clipboard_text;
return bd->clipboard_text_data;
}
static void SetClipboardText(ImGuiContext*, const char* text) {
SDL_SetClipboardText(text);
}
static void PlatformSetImeData(ImGuiContext*, ImGuiViewport* viewport, ImGuiPlatformImeData* data) {
SdlData* bd = GetBackendData();
auto window_id = (SDL_WindowID)(intptr_t)viewport->PlatformHandle;
SDL_Window* window = SDL_GetWindowFromID(window_id);
if ((!data->WantVisible || bd->ime_window != window) && bd->ime_window != nullptr) {
SDL_StopTextInput(bd->ime_window);
bd->ime_window = nullptr;
}
if (data->WantVisible) {
SDL_Rect r;
r.x = (int)data->InputPos.x;
r.y = (int)data->InputPos.y;
r.w = 1;
r.h = (int)data->InputLineHeight;
SDL_SetTextInputArea(window, &r, 0);
SDL_StartTextInput(window);
bd->ime_window = window;
}
}
static ImGuiKey KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode) {
// Keypad doesn't have individual key values in SDL3
switch (scancode) {
case SDL_SCANCODE_KP_0:
return ImGuiKey_Keypad0;
case SDL_SCANCODE_KP_1:
return ImGuiKey_Keypad1;
case SDL_SCANCODE_KP_2:
return ImGuiKey_Keypad2;
case SDL_SCANCODE_KP_3:
return ImGuiKey_Keypad3;
case SDL_SCANCODE_KP_4:
return ImGuiKey_Keypad4;
case SDL_SCANCODE_KP_5:
return ImGuiKey_Keypad5;
case SDL_SCANCODE_KP_6:
return ImGuiKey_Keypad6;
case SDL_SCANCODE_KP_7:
return ImGuiKey_Keypad7;
case SDL_SCANCODE_KP_8:
return ImGuiKey_Keypad8;
case SDL_SCANCODE_KP_9:
return ImGuiKey_Keypad9;
case SDL_SCANCODE_KP_PERIOD:
return ImGuiKey_KeypadDecimal;
case SDL_SCANCODE_KP_DIVIDE:
return ImGuiKey_KeypadDivide;
case SDL_SCANCODE_KP_MULTIPLY:
return ImGuiKey_KeypadMultiply;
case SDL_SCANCODE_KP_MINUS:
return ImGuiKey_KeypadSubtract;
case SDL_SCANCODE_KP_PLUS:
return ImGuiKey_KeypadAdd;
case SDL_SCANCODE_KP_ENTER:
return ImGuiKey_KeypadEnter;
case SDL_SCANCODE_KP_EQUALS:
return ImGuiKey_KeypadEqual;
default:
break;
}
switch (keycode) {
case SDLK_TAB:
return ImGuiKey_Tab;
case SDLK_LEFT:
return ImGuiKey_LeftArrow;
case SDLK_RIGHT:
return ImGuiKey_RightArrow;
case SDLK_UP:
return ImGuiKey_UpArrow;
case SDLK_DOWN:
return ImGuiKey_DownArrow;
case SDLK_PAGEUP:
return ImGuiKey_PageUp;
case SDLK_PAGEDOWN:
return ImGuiKey_PageDown;
case SDLK_HOME:
return ImGuiKey_Home;
case SDLK_END:
return ImGuiKey_End;
case SDLK_INSERT:
return ImGuiKey_Insert;
case SDLK_DELETE:
return ImGuiKey_Delete;
case SDLK_BACKSPACE:
return ImGuiKey_Backspace;
case SDLK_SPACE:
return ImGuiKey_Space;
case SDLK_RETURN:
return ImGuiKey_Enter;
case SDLK_ESCAPE:
return ImGuiKey_Escape;
case SDLK_APOSTROPHE:
return ImGuiKey_Apostrophe;
case SDLK_COMMA:
return ImGuiKey_Comma;
case SDLK_MINUS:
return ImGuiKey_Minus;
case SDLK_PERIOD:
return ImGuiKey_Period;
case SDLK_SLASH:
return ImGuiKey_Slash;
case SDLK_SEMICOLON:
return ImGuiKey_Semicolon;
case SDLK_EQUALS:
return ImGuiKey_Equal;
case SDLK_LEFTBRACKET:
return ImGuiKey_LeftBracket;
case SDLK_BACKSLASH:
return ImGuiKey_Backslash;
case SDLK_RIGHTBRACKET:
return ImGuiKey_RightBracket;
case SDLK_GRAVE:
return ImGuiKey_GraveAccent;
case SDLK_CAPSLOCK:
return ImGuiKey_CapsLock;
case SDLK_SCROLLLOCK:
return ImGuiKey_ScrollLock;
case SDLK_NUMLOCKCLEAR:
return ImGuiKey_NumLock;
case SDLK_PRINTSCREEN:
return ImGuiKey_PrintScreen;
case SDLK_PAUSE:
return ImGuiKey_Pause;
case SDLK_LCTRL:
return ImGuiKey_LeftCtrl;
case SDLK_LSHIFT:
return ImGuiKey_LeftShift;
case SDLK_LALT:
return ImGuiKey_LeftAlt;
case SDLK_LGUI:
return ImGuiKey_LeftSuper;
case SDLK_RCTRL:
return ImGuiKey_RightCtrl;
case SDLK_RSHIFT:
return ImGuiKey_RightShift;
case SDLK_RALT:
return ImGuiKey_RightAlt;
case SDLK_RGUI:
return ImGuiKey_RightSuper;
case SDLK_APPLICATION:
return ImGuiKey_Menu;
case SDLK_0:
return ImGuiKey_0;
case SDLK_1:
return ImGuiKey_1;
case SDLK_2:
return ImGuiKey_2;
case SDLK_3:
return ImGuiKey_3;
case SDLK_4:
return ImGuiKey_4;
case SDLK_5:
return ImGuiKey_5;
case SDLK_6:
return ImGuiKey_6;
case SDLK_7:
return ImGuiKey_7;
case SDLK_8:
return ImGuiKey_8;
case SDLK_9:
return ImGuiKey_9;
case SDLK_A:
return ImGuiKey_A;
case SDLK_B:
return ImGuiKey_B;
case SDLK_C:
return ImGuiKey_C;
case SDLK_D:
return ImGuiKey_D;
case SDLK_E:
return ImGuiKey_E;
case SDLK_F:
return ImGuiKey_F;
case SDLK_G:
return ImGuiKey_G;
case SDLK_H:
return ImGuiKey_H;
case SDLK_I:
return ImGuiKey_I;
case SDLK_J:
return ImGuiKey_J;
case SDLK_K:
return ImGuiKey_K;
case SDLK_L:
return ImGuiKey_L;
case SDLK_M:
return ImGuiKey_M;
case SDLK_N:
return ImGuiKey_N;
case SDLK_O:
return ImGuiKey_O;
case SDLK_P:
return ImGuiKey_P;
case SDLK_Q:
return ImGuiKey_Q;
case SDLK_R:
return ImGuiKey_R;
case SDLK_S:
return ImGuiKey_S;
case SDLK_T:
return ImGuiKey_T;
case SDLK_U:
return ImGuiKey_U;
case SDLK_V:
return ImGuiKey_V;
case SDLK_W:
return ImGuiKey_W;
case SDLK_X:
return ImGuiKey_X;
case SDLK_Y:
return ImGuiKey_Y;
case SDLK_Z:
return ImGuiKey_Z;
case SDLK_F1:
return ImGuiKey_F1;
case SDLK_F2:
return ImGuiKey_F2;
case SDLK_F3:
return ImGuiKey_F3;
case SDLK_F4:
return ImGuiKey_F4;
case SDLK_F5:
return ImGuiKey_F5;
case SDLK_F6:
return ImGuiKey_F6;
case SDLK_F7:
return ImGuiKey_F7;
case SDLK_F8:
return ImGuiKey_F8;
case SDLK_F9:
return ImGuiKey_F9;
case SDLK_F10:
return ImGuiKey_F10;
case SDLK_F11:
return ImGuiKey_F11;
case SDLK_F12:
return ImGuiKey_F12;
case SDLK_F13:
return ImGuiKey_F13;
case SDLK_F14:
return ImGuiKey_F14;
case SDLK_F15:
return ImGuiKey_F15;
case SDLK_F16:
return ImGuiKey_F16;
case SDLK_F17:
return ImGuiKey_F17;
case SDLK_F18:
return ImGuiKey_F18;
case SDLK_F19:
return ImGuiKey_F19;
case SDLK_F20:
return ImGuiKey_F20;
case SDLK_F21:
return ImGuiKey_F21;
case SDLK_F22:
return ImGuiKey_F22;
case SDLK_F23:
return ImGuiKey_F23;
case SDLK_F24:
return ImGuiKey_F24;
case SDLK_AC_BACK:
return ImGuiKey_AppBack;
case SDLK_AC_FORWARD:
return ImGuiKey_AppForward;
default:
break;
}
return ImGuiKey_None;
}
static void UpdateKeyModifiers(SDL_Keymod sdl_key_mods) {
ImGuiIO& io = ImGui::GetIO();
io.AddKeyEvent(ImGuiMod_Ctrl, (sdl_key_mods & SDL_KMOD_CTRL) != 0);
io.AddKeyEvent(ImGuiMod_Shift, (sdl_key_mods & SDL_KMOD_SHIFT) != 0);
io.AddKeyEvent(ImGuiMod_Alt, (sdl_key_mods & SDL_KMOD_ALT) != 0);
io.AddKeyEvent(ImGuiMod_Super, (sdl_key_mods & SDL_KMOD_GUI) != 0);
}
static ImGuiViewport* GetViewportForWindowId(SDL_WindowID window_id) {
SdlData* bd = GetBackendData();
return (window_id == bd->window_id) ? ImGui::GetMainViewport() : nullptr;
}
// 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. If you have
// multiple SDL events and some of them are not meant to be used by dear imgui, you may need to
// filter events based on their windowID field.
bool ProcessEvent(const SDL_Event* event) {
SdlData* bd = GetBackendData();
IM_ASSERT(bd != nullptr &&
"Context or backend not initialized! Did you call ImGui_ImplSDL3_Init()?");
ImGuiIO& io = ImGui::GetIO();
switch (event->type) {
case SDL_EVENT_MOUSE_MOTION: {
if (GetViewportForWindowId(event->motion.windowID) == NULL)
return false;
ImVec2 mouse_pos((float)event->motion.x, (float)event->motion.y);
io.AddMouseSourceEvent(event->motion.which == SDL_TOUCH_MOUSEID
? ImGuiMouseSource_TouchScreen
: ImGuiMouseSource_Mouse);
io.AddMousePosEvent(mouse_pos.x, mouse_pos.y);
return true;
}
case SDL_EVENT_MOUSE_WHEEL: {
if (GetViewportForWindowId(event->wheel.windowID) == NULL)
return false;
// IMGUI_DEBUG_LOG("wheel %.2f %.2f, precise %.2f %.2f\n", (float)event->wheel.x,
// (float)event->wheel.y, event->wheel.preciseX, event->wheel.preciseY);
float wheel_x = -event->wheel.x;
float wheel_y = event->wheel.y;
#ifdef __EMSCRIPTEN__
wheel_x /= 100.0f;
#endif
io.AddMouseSourceEvent(event->wheel.which == SDL_TOUCH_MOUSEID
? ImGuiMouseSource_TouchScreen
: ImGuiMouseSource_Mouse);
io.AddMouseWheelEvent(wheel_x, wheel_y);
return true;
}
case SDL_EVENT_MOUSE_BUTTON_DOWN:
case SDL_EVENT_MOUSE_BUTTON_UP: {
if (GetViewportForWindowId(event->button.windowID) == NULL)
return false;
int mouse_button = -1;
if (event->button.button == SDL_BUTTON_LEFT) {
mouse_button = 0;
}
if (event->button.button == SDL_BUTTON_RIGHT) {
mouse_button = 1;
}
if (event->button.button == SDL_BUTTON_MIDDLE) {
mouse_button = 2;
}
if (event->button.button == SDL_BUTTON_X1) {
mouse_button = 3;
}
if (event->button.button == SDL_BUTTON_X2) {
mouse_button = 4;
}
if (mouse_button == -1)
break;
io.AddMouseSourceEvent(event->button.which == SDL_TOUCH_MOUSEID
? ImGuiMouseSource_TouchScreen
: ImGuiMouseSource_Mouse);
io.AddMouseButtonEvent(mouse_button, (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN));
bd->mouse_buttons_down = (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN)
? (bd->mouse_buttons_down | (1 << mouse_button))
: (bd->mouse_buttons_down & ~(1 << mouse_button));
return true;
}
case SDL_EVENT_TEXT_INPUT: {
if (GetViewportForWindowId(event->text.windowID) == NULL)
return false;
io.AddInputCharactersUTF8(event->text.text);
return true;
}
case SDL_EVENT_KEY_DOWN:
case SDL_EVENT_KEY_UP: {
if (GetViewportForWindowId(event->key.windowID) == NULL)
return false;
// IMGUI_DEBUG_LOG("SDL_EVENT_KEY_%d: key=%d, scancode=%d, mod=%X\n", (event->type ==
// SDL_EVENT_KEY_DOWN) ? "DOWN" : "UP", event->key.key, event->key.scancode,
// event->key.mod);
UpdateKeyModifiers((SDL_Keymod)event->key.mod);
ImGuiKey key = KeyEventToImGuiKey(event->key.key, event->key.scancode);
io.AddKeyEvent(key, (event->type == SDL_EVENT_KEY_DOWN));
io.SetKeyEventNativeData(
key, event->key.key, event->key.scancode,
event->key.scancode); // To support legacy indexing (<1.87 user code). Legacy backend
// uses SDLK_*** as indices to IsKeyXXX() functions.
return true;
}
case SDL_EVENT_WINDOW_MOUSE_ENTER: {
if (GetViewportForWindowId(event->window.windowID) == NULL)
return false;
bd->mouse_window_id = event->window.windowID;
bd->mouse_pending_leave_frame = 0;
return true;
}
// - In some cases, when detaching a window from main viewport SDL may send
// SDL_WINDOWEVENT_ENTER one frame too late,
// causing SDL_WINDOWEVENT_LEAVE on previous frame to interrupt drag operation by clear mouse
// position. This is why we delay process the SDL_WINDOWEVENT_LEAVE events by one frame. See
// issue #5012 for details.
// FIXME: Unconfirmed whether this is still needed with SDL3.
case SDL_EVENT_WINDOW_MOUSE_LEAVE: {
if (GetViewportForWindowId(event->window.windowID) == NULL)
return false;
bd->mouse_pending_leave_frame = ImGui::GetFrameCount() + 1;
return true;
}
case SDL_EVENT_WINDOW_FOCUS_GAINED:
case SDL_EVENT_WINDOW_FOCUS_LOST: {
if (GetViewportForWindowId(event->window.windowID) == NULL)
return false;
io.AddFocusEvent(event->type == SDL_EVENT_WINDOW_FOCUS_GAINED);
return true;
}
case SDL_EVENT_GAMEPAD_ADDED:
case SDL_EVENT_GAMEPAD_REMOVED: {
bd->want_update_gamepads_list = true;
return true;
}
}
return false;
}
static void SetupPlatformHandles(ImGuiViewport* viewport, SDL_Window* window) {
viewport->PlatformHandle = (void*)(intptr_t)SDL_GetWindowID(window);
viewport->PlatformHandleRaw = nullptr;
#if defined(_WIN32) && !defined(__WINRT__)
viewport->PlatformHandleRaw = (HWND)SDL_GetPointerProperty(
SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WIN32_HWND_POINTER, nullptr);
#elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA)
viewport->PlatformHandleRaw = SDL_GetPointerProperty(
SDL_GetWindowProperties(window), SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, nullptr);
#endif
}
bool Init(SDL_Window* window) {
ImGuiIO& io = ImGui::GetIO();
IMGUI_CHECKVERSION();
IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!");
// Setup backend capabilities flags
SdlData* bd = IM_NEW(SdlData)();
io.BackendPlatformUserData = (void*)bd;
io.BackendPlatformName = "imgui_impl_sdl3_shadps4";
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values
io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests
// (optional, rarely used)
bd->window = window;
bd->window_id = SDL_GetWindowID(window);
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
platform_io.Platform_SetClipboardTextFn = SetClipboardText;
platform_io.Platform_GetClipboardTextFn = GetClipboardText;
platform_io.Platform_SetImeDataFn = PlatformSetImeData;
// Gamepad handling
bd->gamepad_mode = ImGui_ImplSDL3_GamepadMode_AutoFirst;
bd->want_update_gamepads_list = true;
// Load mouse cursors
#define CURSOR(left, right) \
bd->mouse_cursors[ImGuiMouseCursor_##left] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_##right)
CURSOR(Arrow, DEFAULT);
CURSOR(TextInput, TEXT);
CURSOR(ResizeAll, MOVE);
CURSOR(ResizeNS, NS_RESIZE);
CURSOR(ResizeEW, EW_RESIZE);
CURSOR(ResizeNESW, NESW_RESIZE);
CURSOR(ResizeNWSE, NWSE_RESIZE);
CURSOR(Hand, POINTER);
CURSOR(NotAllowed, NOT_ALLOWED);
#undef CURSOR
// Set platform dependent data in viewport
// Our mouse update function expect PlatformHandle to be filled for the main viewport
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
SetupPlatformHandles(main_viewport, window);
// From 2.0.5: Set SDL hint to receive mouse click events on window focus, otherwise SDL doesn't
// emit the event. Without this, when clicking to gain focus, our widgets wouldn't activate even
// though they showed as hovered. (This is unfortunately a global SDL setting, so enabling it
// might have a side-effect on your application. It is unlikely to make a difference, but if
// your app absolutely needs to ignore the initial on-focus click: you can ignore
// SDL_EVENT_MOUSE_BUTTON_DOWN events coming right after a SDL_WINDOWEVENT_FOCUS_GAINED)
#ifdef SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH
SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
#endif
// From 2.0.22: Disable auto-capture, this is preventing drag and drop across multiple windows
// (see #5710)
#ifdef SDL_HINT_MOUSE_AUTO_CAPTURE
SDL_SetHint(SDL_HINT_MOUSE_AUTO_CAPTURE, "0");
#endif
return true;
}
static void CloseGamepads();
void Shutdown() {
SdlData* bd = GetBackendData();
IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
ImGuiIO& io = ImGui::GetIO();
if (bd->clipboard_text_data) {
SDL_free((void*)bd->clipboard_text_data);
}
for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
SDL_DestroyCursor(bd->mouse_cursors[cursor_n]);
CloseGamepads();
io.BackendPlatformName = nullptr;
io.BackendPlatformUserData = nullptr;
io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos |
ImGuiBackendFlags_HasGamepad);
IM_DELETE(bd);
}
static void UpdateMouseData() {
SdlData* bd = GetBackendData();
ImGuiIO& io = ImGui::GetIO();
// We forward mouse input when hovered or captured (via SDL_EVENT_MOUSE_MOTION) or when focused
// (below)
// SDL_CaptureMouse() let the OS know e.g. that our imgui drag outside the SDL window boundaries
// shouldn't e.g. trigger other operations outside
SDL_CaptureMouse((bd->mouse_buttons_down != 0) ? SDL_TRUE : SDL_FALSE);
SDL_Window* focused_window = SDL_GetKeyboardFocus();
const bool is_app_focused = (bd->window == focused_window);
if (is_app_focused) {
// (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when
// ImGuiConfigFlags_NavEnableSetMousePos is enabled by user)
if (io.WantSetMousePos)
SDL_WarpMouseInWindow(bd->window, io.MousePos.x, io.MousePos.y);
// (Optional) Fallback to provide mouse position when focused (SDL_EVENT_MOUSE_MOTION
// already provides this when hovered or captured)
if (bd->mouse_buttons_down == 0) {
// Single-viewport mode: mouse position in client window coordinates (io.MousePos is
// (0,0) when the mouse is on the upper-left corner of the app window)
float mouse_x_global, mouse_y_global;
int window_x, window_y;
SDL_GetGlobalMouseState(&mouse_x_global, &mouse_y_global);
SDL_GetWindowPosition(focused_window, &window_x, &window_y);
io.AddMousePosEvent(mouse_x_global - (float)window_x, mouse_y_global - (float)window_y);
}
}
}
static void UpdateMouseCursor() {
ImGuiIO& io = ImGui::GetIO();
if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
return;
SdlData* bd = GetBackendData();
ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None) {
// Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
SDL_HideCursor();
} else {
// Show OS mouse cursor
SDL_Cursor* expected_cursor = bd->mouse_cursors[imgui_cursor]
? bd->mouse_cursors[imgui_cursor]
: bd->mouse_cursors[ImGuiMouseCursor_Arrow];
if (bd->mouse_last_cursor != expected_cursor) {
SDL_SetCursor(expected_cursor); // SDL function doesn't have an early out (see #6113)
bd->mouse_last_cursor = expected_cursor;
}
SDL_ShowCursor();
}
}
static void CloseGamepads() {
SdlData* bd = GetBackendData();
if (bd->gamepad_mode != ImGui_ImplSDL3_GamepadMode_Manual)
for (SDL_Gamepad* gamepad : bd->gamepads)
SDL_CloseGamepad(gamepad);
bd->gamepads.resize(0);
}
void SetGamepadMode(GamepadMode mode, SDL_Gamepad** manual_gamepads_array,
int manual_gamepads_count) {
SdlData* bd = GetBackendData();
CloseGamepads();
if (mode == ImGui_ImplSDL3_GamepadMode_Manual) {
IM_ASSERT(manual_gamepads_array != nullptr && manual_gamepads_count > 0);
for (int n = 0; n < manual_gamepads_count; n++)
bd->gamepads.push_back(manual_gamepads_array[n]);
} else {
IM_ASSERT(manual_gamepads_array == nullptr && manual_gamepads_count <= 0);
bd->want_update_gamepads_list = true;
}
bd->gamepad_mode = mode;
}
static void UpdateGamepadButton(SdlData* bd, ImGuiIO& io, ImGuiKey key,
SDL_GamepadButton button_no) {
bool merged_value = false;
for (SDL_Gamepad* gamepad : bd->gamepads)
merged_value |= SDL_GetGamepadButton(gamepad, button_no) != 0;
io.AddKeyEvent(key, merged_value);
}
static inline float Saturate(float v) {
return v < 0.0f ? 0.0f : v > 1.0f ? 1.0f : v;
}
static void UpdateGamepadAnalog(SdlData* bd, ImGuiIO& io, ImGuiKey key, SDL_GamepadAxis axis_no,
float v0, float v1) {
float merged_value = 0.0f;
for (SDL_Gamepad* gamepad : bd->gamepads) {
float vn = Saturate((float)(SDL_GetGamepadAxis(gamepad, axis_no) - v0) / (float)(v1 - v0));
if (merged_value < vn)
merged_value = vn;
}
io.AddKeyAnalogEvent(key, merged_value > 0.1f, merged_value);
}
static void UpdateGamepads() {
ImGuiIO& io = ImGui::GetIO();
SdlData* bd = GetBackendData();
// Update list of gamepads to use
if (bd->want_update_gamepads_list && bd->gamepad_mode != ImGui_ImplSDL3_GamepadMode_Manual) {
CloseGamepads();
int sdl_gamepads_count = 0;
const SDL_JoystickID* sdl_gamepads = SDL_GetGamepads(&sdl_gamepads_count);
for (int n = 0; n < sdl_gamepads_count; n++)
if (SDL_Gamepad* gamepad = SDL_OpenGamepad(sdl_gamepads[n])) {
bd->gamepads.push_back(gamepad);
if (bd->gamepad_mode == ImGui_ImplSDL3_GamepadMode_AutoFirst)
break;
}
bd->want_update_gamepads_list = false;
}
// FIXME: Technically feeding gamepad shouldn't depend on this now that they are regular inputs.
if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0)
return;
io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
if (bd->gamepads.Size == 0)
return;
io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
// Update gamepad inputs
const int thumb_dead_zone = 8000; // SDL_gamepad.h suggests using this value.
UpdateGamepadButton(bd, io, ImGuiKey_GamepadStart, SDL_GAMEPAD_BUTTON_START);
UpdateGamepadButton(bd, io, ImGuiKey_GamepadBack, SDL_GAMEPAD_BUTTON_BACK);
UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceLeft,
SDL_GAMEPAD_BUTTON_WEST); // Xbox X, PS Square
UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceRight,
SDL_GAMEPAD_BUTTON_EAST); // Xbox B, PS Circle
UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceUp,
SDL_GAMEPAD_BUTTON_NORTH); // Xbox Y, PS Triangle
UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceDown,
SDL_GAMEPAD_BUTTON_SOUTH); // Xbox A, PS Cross
UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadLeft, SDL_GAMEPAD_BUTTON_DPAD_LEFT);
UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadRight, SDL_GAMEPAD_BUTTON_DPAD_RIGHT);
UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadUp, SDL_GAMEPAD_BUTTON_DPAD_UP);
UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadDown, SDL_GAMEPAD_BUTTON_DPAD_DOWN);
UpdateGamepadButton(bd, io, ImGuiKey_GamepadL1, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER);
UpdateGamepadButton(bd, io, ImGuiKey_GamepadR1, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER);
UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadL2, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, 0.0f, 32767);
UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadR2, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, 0.0f, 32767);
UpdateGamepadButton(bd, io, ImGuiKey_GamepadL3, SDL_GAMEPAD_BUTTON_LEFT_STICK);
UpdateGamepadButton(bd, io, ImGuiKey_GamepadR3, SDL_GAMEPAD_BUTTON_RIGHT_STICK);
UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickLeft, SDL_GAMEPAD_AXIS_LEFTX,
-thumb_dead_zone, -32768);
UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickRight, SDL_GAMEPAD_AXIS_LEFTX,
+thumb_dead_zone, +32767);
UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickUp, SDL_GAMEPAD_AXIS_LEFTY, -thumb_dead_zone,
-32768);
UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickDown, SDL_GAMEPAD_AXIS_LEFTY,
+thumb_dead_zone, +32767);
UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickLeft, SDL_GAMEPAD_AXIS_RIGHTX,
-thumb_dead_zone, -32768);
UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickRight, SDL_GAMEPAD_AXIS_RIGHTX,
+thumb_dead_zone, +32767);
UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickUp, SDL_GAMEPAD_AXIS_RIGHTY, -thumb_dead_zone,
-32768);
UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickDown, SDL_GAMEPAD_AXIS_RIGHTY,
+thumb_dead_zone, +32767);
}
void NewFrame() {
SdlData* bd = GetBackendData();
IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
ImGuiIO& io = ImGui::GetIO();
// Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution)
// (Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. Happens
// in VMs and Emscripten, see #6189, #6114, #3644)
static Uint64 frequency = SDL_GetPerformanceFrequency();
Uint64 current_time = SDL_GetPerformanceCounter();
if (current_time <= bd->time)
current_time = bd->time + 1;
io.DeltaTime = bd->time > 0 ? (float)((double)(current_time - bd->time) / (double)frequency)
: (float)(1.0f / 60.0f);
bd->time = current_time;
if (bd->mouse_pending_leave_frame && bd->mouse_pending_leave_frame >= ImGui::GetFrameCount() &&
bd->mouse_buttons_down == 0) {
bd->mouse_window_id = 0;
bd->mouse_pending_leave_frame = 0;
io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
}
UpdateMouseData();
UpdateMouseCursor();
// Update game controllers (if enabled and available)
UpdateGamepads();
}
void OnResize() {
SdlData* bd = GetBackendData();
ImGuiIO& io = ImGui::GetIO();
int w, h;
int display_w, display_h;
SDL_GetWindowSize(bd->window, &w, &h);
if (SDL_GetWindowFlags(bd->window) & SDL_WINDOW_MINIMIZED) {
w = h = 0;
}
SDL_GetWindowSizeInPixels(bd->window, &display_w, &display_h);
io.DisplaySize = ImVec2((float)w, (float)h);
if (w > 0 && h > 0) {
io.DisplayFramebufferScale = {(float)display_w / (float)w, (float)display_h / (float)h};
}
}
} // namespace ImGui::Sdl

View file

@ -0,0 +1,31 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
// Based on imgui_impl_sdl3.h from Dear ImGui repository
#pragma once
struct SDL_Window;
struct SDL_Renderer;
struct SDL_Gamepad;
typedef union SDL_Event SDL_Event;
namespace ImGui::Sdl {
bool Init(SDL_Window* window);
void Shutdown();
void NewFrame();
bool ProcessEvent(const SDL_Event* event);
void OnResize();
// Gamepad selection automatically starts in AutoFirst mode, picking first available SDL_Gamepad.
// You may override this. When using manual mode, caller is responsible for opening/closing gamepad.
enum GamepadMode {
ImGui_ImplSDL3_GamepadMode_AutoFirst,
ImGui_ImplSDL3_GamepadMode_AutoAll,
ImGui_ImplSDL3_GamepadMode_Manual
};
void SetGamepadMode(GamepadMode mode, SDL_Gamepad** manual_gamepads_array = NULL,
int manual_gamepads_count = -1);
}; // namespace ImGui::Sdl

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,43 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
// Based on imgui_impl_vulkan.h from Dear ImGui repository
#pragma once
#define VULKAN_HPP_NO_EXCEPTIONS
#include "video_core/renderer_vulkan/vk_common.h"
struct ImDrawData;
namespace ImGui::Vulkan {
struct InitInfo {
vk::Instance instance;
vk::PhysicalDevice physical_device;
vk::Device device;
uint32_t queue_family;
vk::Queue queue;
uint32_t image_count; // >= 2
vk::DeviceSize min_allocation_size; // Minimum allocation size
vk::PipelineCache pipeline_cache;
uint32_t subpass;
vk::PipelineRenderingCreateInfoKHR pipeline_rendering_create_info;
// (Optional) Allocation, Logging
const vk::AllocationCallbacks* allocator{};
void (*check_vk_result_fn)(vk::Result err);
};
vk::DescriptorSet AddTexture(vk::Sampler sampler, vk::ImageView image_view,
vk::ImageLayout image_layout);
void RemoveTexture(vk::DescriptorSet descriptor_set);
bool Init(InitInfo info);
void Shutdown();
void NewFrame();
void RenderDrawData(ImDrawData& draw_data, vk::CommandBuffer command_buffer,
vk::Pipeline pipeline = VK_NULL_HANDLE);
} // namespace ImGui::Vulkan

View file

@ -9,6 +9,7 @@
#include "common/config.h"
#include "common/version.h"
#include "core/libraries/pad/pad.h"
#include "imgui/renderer/imgui_core.h"
#include "input/controller.h"
#include "sdl_window.h"
#include "video_core/renderdoc.h"
@ -80,6 +81,10 @@ void WindowSDL::waitEvent() {
return;
}
if (ImGui::Core::ProcessEvent(&event)) {
return;
}
switch (event.type) {
case SDL_EVENT_WINDOW_RESIZED:
case SDL_EVENT_WINDOW_MAXIMIZED:
@ -115,6 +120,7 @@ void WindowSDL::waitEvent() {
void WindowSDL::onResize() {
SDL_GetWindowSizeInPixels(window, &width, &height);
ImGui::Core::OnResize();
}
void WindowSDL::onKeyPress(const SDL_Event* event) {

View file

@ -58,6 +58,10 @@ public:
return is_open;
}
[[nodiscard]] SDL_Window* GetSdlWindow() const {
return window;
}
WindowSystemInfo getWindowInfo() const {
return window_info;
}

View file

@ -6,6 +6,7 @@
#include "common/singleton.h"
#include "core/file_format/splash.h"
#include "core/libraries/system/systemservice.h"
#include "imgui/renderer/imgui_core.h"
#include "sdl_window.h"
#include "video_core/renderer_vulkan/renderer_vulkan.h"
#include "video_core/renderer_vulkan/vk_rasterizer.h"
@ -73,7 +74,7 @@ RendererVulkan::RendererVulkan(Frontend::WindowSDL& window_, AmdGpu::Liverpool*
draw_scheduler{instance}, present_scheduler{instance}, flip_scheduler{instance},
swapchain{instance, window},
rasterizer{std::make_unique<Rasterizer>(instance, draw_scheduler, liverpool)},
texture_cache{rasterizer->GetTextureCache()} {
texture_cache{rasterizer->GetTextureCache()}, video_info_ui{this} {
const u32 num_images = swapchain.GetImageCount();
const vk::Device device = instance.GetDevice();
@ -84,9 +85,14 @@ RendererVulkan::RendererVulkan(Frontend::WindowSDL& window_, AmdGpu::Liverpool*
frame.present_done = device.createFence({.flags = vk::FenceCreateFlagBits::eSignaled});
free_queue.push(&frame);
}
// Setup ImGui
ImGui::Core::Initialize(instance, window, num_images, swapchain.GetSurfaceFormat().format);
ImGui::Layer::AddLayer(&video_info_ui);
}
RendererVulkan::~RendererVulkan() {
ImGui::Layer::RemoveLayer(&video_info_ui);
draw_scheduler.Finish();
const vk::Device device = instance.GetDevice();
for (auto& frame : present_frames) {
@ -94,6 +100,7 @@ RendererVulkan::~RendererVulkan() {
device.destroyImageView(frame.image_view);
device.destroyFence(frame.present_done);
}
ImGui::Core::Shutdown(device);
}
void RendererVulkan::RecreateFrame(Frame* frame, u32 width, u32 height) {
@ -254,6 +261,8 @@ Frame* RendererVulkan::PrepareFrameInternal(VideoCore::Image& image, bool is_eop
}
void RendererVulkan::Present(Frame* frame) {
ImGui::Core::NewFrame();
swapchain.AcquireNextImage();
const vk::Image swapchain_image = swapchain.Image();
@ -286,7 +295,7 @@ void RendererVulkan::Present(Frame* frame) {
vk::ImageMemoryBarrier{
.srcAccessMask = vk::AccessFlagBits::eColorAttachmentWrite,
.dstAccessMask = vk::AccessFlagBits::eTransferRead,
.oldLayout = vk::ImageLayout::eGeneral,
.oldLayout = vk::ImageLayout::eUndefined,
.newLayout = vk::ImageLayout::eTransferSrcOptimal,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
@ -317,6 +326,8 @@ void RendererVulkan::Present(Frame* frame) {
},
};
ImGui::Core::Render(cmdbuf, frame);
cmdbuf.pipelineBarrier(vk::PipelineStageFlagBits::eColorAttachmentOutput,
vk::PipelineStageFlagBits::eTransfer,
vk::DependencyFlagBits::eByRegion, {}, {}, pre_barriers);

View file

@ -4,6 +4,8 @@
#pragma once
#include <condition_variable>
#include "imgui/layer/video_info.h"
#include "video_core/amdgpu/liverpool.h"
#include "video_core/renderer_vulkan/vk_instance.h"
#include "video_core/renderer_vulkan/vk_scheduler.h"
@ -103,6 +105,8 @@ private:
std::condition_variable_any frame_cv;
std::optional<VideoCore::Image> splash_img;
std::vector<VAddr> vo_buffers_addr;
ImGui::Layers::VideoInfo video_info_ui;
};
} // namespace Vulkan