Skip to content

Commit

Permalink
imgui Ref-counted textures
Browse files Browse the repository at this point in the history
- has a background thread to decode textures
  • Loading branch information
viniciuslrangel committed Sep 18, 2024
1 parent fbd2d7a commit 8ee617b
Show file tree
Hide file tree
Showing 8 changed files with 582 additions and 57 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ set(VIDEO_CORE src/video_core/amdgpu/liverpool.cpp
set(IMGUI src/imgui/imgui_config.h
src/imgui/imgui_layer.h
src/imgui/imgui_std.h
src/imgui/imgui_texture.h
src/imgui/layer/video_info.cpp
src/imgui/layer/video_info.h
src/imgui/renderer/imgui_core.cpp
Expand All @@ -609,6 +610,8 @@ set(IMGUI src/imgui/imgui_config.h
src/imgui/renderer/imgui_impl_sdl3.h
src/imgui/renderer/imgui_impl_vulkan.cpp
src/imgui/renderer/imgui_impl_vulkan.h
src/imgui/renderer/texture_manager.cpp
src/imgui/renderer/texture_manager.h
)

set(INPUT src/input/controller.cpp
Expand Down
5 changes: 4 additions & 1 deletion src/imgui/imgui_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ extern void assert_fail_debug_msg(const char* msg);
#define IMGUI_DEFINE_MATH_OPERATORS

#define IM_VEC2_CLASS_EXTRA \
constexpr ImVec2(float _v) : x(_v), y(_v) {}
constexpr ImVec2(float _v) : x(_v), y(_v) {}

#define IM_VEC4_CLASS_EXTRA \
constexpr ImVec4(float _v) : x(_v), y(_v), z(_v), w(_v) {}
45 changes: 45 additions & 0 deletions src/imgui/imgui_texture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <filesystem>
#include <imgui.h>

namespace ImGui {

namespace Core::TextureManager {
struct Inner;
} // namespace Core::TextureManager

class RefCountedTexture {
Core::TextureManager::Inner* inner;

explicit RefCountedTexture(Core::TextureManager::Inner* inner);

public:
struct Image {
ImTextureID im_id;
u32 width;
u32 height;
};

static RefCountedTexture DecodePngTexture(std::vector<u8> data);

static RefCountedTexture DecodePngFile(std::filesystem::path path);

RefCountedTexture();

RefCountedTexture(const RefCountedTexture& other);
RefCountedTexture(RefCountedTexture&& other) noexcept;
RefCountedTexture& operator=(const RefCountedTexture& other);
RefCountedTexture& operator=(RefCountedTexture&& other) noexcept;

virtual ~RefCountedTexture();

[[nodiscard]] Image GetTexture() const;

explicit(false) operator bool() const;
};

}; // namespace ImGui
8 changes: 7 additions & 1 deletion src/imgui/renderer/imgui_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "imgui_impl_sdl3.h"
#include "imgui_impl_vulkan.h"
#include "sdl_window.h"
#include "texture_manager.h"
#include "video_core/renderer_vulkan/renderer_vulkan.h"

static void CheckVkResult(const vk::Result err) {
Expand Down Expand Up @@ -63,6 +64,8 @@ void Initialize(const ::Vulkan::Instance& instance, const Frontend::WindowSDL& w
.check_vk_result_fn = &CheckVkResult,
};
Vulkan::Init(vk_info);

TextureManager::StartWorker();
}

void OnResize() {
Expand All @@ -72,6 +75,8 @@ void OnResize() {
void Shutdown(const vk::Device& device) {
device.waitIdle();

TextureManager::StopWorker();

const ImGuiIO& io = GetIO();
const auto ini_filename = (void*)io.IniFilename;
const auto log_filename = (void*)io.LogFilename;
Expand Down Expand Up @@ -125,7 +130,6 @@ void NewFrame() {
}
}

Vulkan::NewFrame();
Sdl::NewFrame();
ImGui::NewFrame();

Expand All @@ -144,6 +148,8 @@ void NewFrame() {
}

void Render(const vk::CommandBuffer& cmdbuf, ::Vulkan::Frame* frame) {
TextureManager::Tick();

ImGui::Render();
ImDrawData* draw_data = GetDrawData();
if (draw_data->CmdListsCount == 0) {
Expand Down
Loading

0 comments on commit 8ee617b

Please sign in to comment.