2022-02-19 05:33:56 +00:00
|
|
|
#include "imgui.hpp"
|
2022-02-16 05:21:24 +00:00
|
|
|
|
2022-02-19 05:33:56 +00:00
|
|
|
#include "gpu.hpp"
|
2022-02-17 06:03:00 +00:00
|
|
|
|
2022-02-26 20:38:08 +00:00
|
|
|
#include <SDL.h>
|
|
|
|
#include <aurora/aurora.hpp>
|
2022-02-19 05:33:56 +00:00
|
|
|
#include <aurora/imgui.hpp>
|
|
|
|
#include <dawn/webgpu_cpp.h>
|
2022-02-17 06:03:00 +00:00
|
|
|
|
2022-02-26 20:38:08 +00:00
|
|
|
#include "../extern/imgui/backends/imgui_impl_sdl.cpp"
|
|
|
|
#include "../extern/imgui/backends/imgui_impl_wgpu.cpp"
|
|
|
|
|
2022-02-16 05:21:24 +00:00
|
|
|
namespace aurora::imgui {
|
2022-02-19 05:33:56 +00:00
|
|
|
using gpu::g_device;
|
|
|
|
using gpu::g_queue;
|
|
|
|
|
2022-02-26 20:38:08 +00:00
|
|
|
static float g_scale;
|
|
|
|
|
2022-02-28 01:55:47 +00:00
|
|
|
static std::string g_imguiSettings{};
|
|
|
|
static std::string g_imguiLog{};
|
2022-02-19 05:33:56 +00:00
|
|
|
void create_context() noexcept {
|
|
|
|
IMGUI_CHECKVERSION();
|
|
|
|
ImGui::CreateContext();
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
2022-02-28 01:55:47 +00:00
|
|
|
g_imguiSettings = std::string(metaforce::FileStoreManager::instance()->getStoreRoot()) + "/imgui.ini";
|
|
|
|
g_imguiLog = std::string(metaforce::FileStoreManager::instance()->getStoreRoot()) + "/imgui.log";
|
|
|
|
io.IniFilename = g_imguiSettings.c_str();
|
|
|
|
io.LogFilename = g_imguiLog.c_str();
|
2022-02-19 05:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void initialize(SDL_Window* window) noexcept {
|
2022-02-26 20:38:08 +00:00
|
|
|
ImGui_ImplSDL2_Init(window, nullptr);
|
2022-02-26 21:28:58 +00:00
|
|
|
#ifdef __APPLE__
|
2022-02-26 20:38:08 +00:00
|
|
|
// Disable MouseCanUseGlobalState for scaling purposes
|
|
|
|
ImGui_ImplSDL2_GetBackendData()->MouseCanUseGlobalState = false;
|
2022-02-26 21:28:58 +00:00
|
|
|
#endif
|
2022-02-19 05:33:56 +00:00
|
|
|
ImGui_ImplWGPU_Init(g_device.Get(), 1, static_cast<WGPUTextureFormat>(gpu::g_graphicsConfig.colorFormat));
|
|
|
|
}
|
|
|
|
|
|
|
|
void shutdown() noexcept {
|
|
|
|
ImGui_ImplWGPU_Shutdown();
|
|
|
|
ImGui_ImplSDL2_Shutdown();
|
|
|
|
ImGui::DestroyContext();
|
|
|
|
}
|
|
|
|
|
2022-02-26 20:38:08 +00:00
|
|
|
void process_event(const SDL_Event& event) noexcept {
|
2022-02-26 21:28:21 +00:00
|
|
|
#ifdef __APPLE__
|
2022-02-26 21:28:58 +00:00
|
|
|
if (event.type == SDL_MOUSEMOTION) {
|
|
|
|
auto& io = ImGui::GetIO();
|
2022-02-26 20:38:08 +00:00
|
|
|
// Scale up mouse coordinates
|
2022-02-26 21:28:58 +00:00
|
|
|
io.AddMousePosEvent(static_cast<float>(event.motion.x) * g_scale,
|
|
|
|
static_cast<float>(event.motion.y) * g_scale);
|
2022-02-26 20:38:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-26 21:28:58 +00:00
|
|
|
#endif
|
|
|
|
ImGui_ImplSDL2_ProcessEvent(&event);
|
2022-02-26 20:38:08 +00:00
|
|
|
}
|
2022-02-19 05:33:56 +00:00
|
|
|
|
2022-02-26 20:38:08 +00:00
|
|
|
void new_frame(const WindowSize& size) noexcept {
|
2022-02-19 05:33:56 +00:00
|
|
|
ImGui_ImplWGPU_NewFrame();
|
|
|
|
ImGui_ImplSDL2_NewFrame();
|
2022-02-26 20:38:08 +00:00
|
|
|
|
|
|
|
// Render at full DPI
|
|
|
|
g_scale = size.scale;
|
|
|
|
ImGui::GetIO().DisplaySize = ImVec2{static_cast<float>(size.fb_width), static_cast<float>(size.fb_height)};
|
2022-02-19 05:33:56 +00:00
|
|
|
ImGui::NewFrame();
|
|
|
|
}
|
|
|
|
|
|
|
|
void render(const wgpu::RenderPassEncoder& pass) noexcept {
|
|
|
|
ImGui::Render();
|
2022-02-26 20:38:08 +00:00
|
|
|
|
|
|
|
auto* data = ImGui::GetDrawData();
|
|
|
|
// io.DisplayFramebufferScale is informational; we're rendering at full DPI
|
|
|
|
data->FramebufferScale = {1.f, 1.f};
|
|
|
|
ImGui_ImplWGPU_RenderDrawData(data, pass.Get());
|
2022-02-19 05:33:56 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 05:21:24 +00:00
|
|
|
ImTextureID add_texture(uint32_t width, uint32_t height, ArrayRef<uint8_t> data) noexcept {
|
2022-02-17 06:03:00 +00:00
|
|
|
const auto size = wgpu::Extent3D{
|
|
|
|
.width = width,
|
|
|
|
.height = height,
|
|
|
|
};
|
|
|
|
const auto textureDescriptor = wgpu::TextureDescriptor{
|
|
|
|
.label = "imgui texture",
|
|
|
|
.usage = wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::CopyDst,
|
|
|
|
.size = size,
|
|
|
|
.format = wgpu::TextureFormat::RGBA8Unorm,
|
|
|
|
};
|
|
|
|
const auto textureViewDescriptor = wgpu::TextureViewDescriptor{
|
|
|
|
.label = "imgui texture view",
|
|
|
|
.format = wgpu::TextureFormat::RGBA8Unorm,
|
|
|
|
.dimension = wgpu::TextureViewDimension::e2D,
|
|
|
|
.mipLevelCount = 1,
|
|
|
|
.arrayLayerCount = 1,
|
|
|
|
};
|
2022-02-19 05:33:56 +00:00
|
|
|
auto texture = g_device.CreateTexture(&textureDescriptor);
|
2022-02-17 06:03:00 +00:00
|
|
|
auto textureView = texture.CreateView(&textureViewDescriptor);
|
|
|
|
{
|
|
|
|
const auto dstView = wgpu::ImageCopyTexture{
|
|
|
|
.texture = texture,
|
|
|
|
};
|
|
|
|
const auto dataLayout = wgpu::TextureDataLayout{
|
|
|
|
.bytesPerRow = 4 * width,
|
|
|
|
.rowsPerImage = height,
|
|
|
|
};
|
2022-02-19 05:33:56 +00:00
|
|
|
g_queue.WriteTexture(&dstView, data.data(), data.size(), &dataLayout, &size);
|
2022-02-17 06:03:00 +00:00
|
|
|
}
|
|
|
|
texture.Release(); // leak some memory!
|
|
|
|
return textureView.Release();
|
2022-02-16 05:21:24 +00:00
|
|
|
}
|
|
|
|
} // namespace aurora::imgui
|