Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/loadable resource #42

Merged
merged 14 commits into from
Oct 13, 2024
46 changes: 22 additions & 24 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ project(ingame_overlay)

if(WIN32) # Setup some variables for Windows build
set(INGAMEOVERLAY_SOURCES
src/BaseHook.cpp
src/Internal.cpp
src/Windows/RendererDetector.cpp
src/Windows/DX9Hook.cpp
src/Windows/DX10Hook.cpp
Expand All @@ -18,9 +16,7 @@ if(WIN32) # Setup some variables for Windows build
)

set(PRIVATE_INGAMEOVERLAY_HEADERS
src/InternalIncludes.h
src/VulkanHelpers.h
src/BaseHook.h
src/Windows/DirectXVTables.h
src/Windows/DX9Hook.h
src/Windows/DX10Hook.h
Expand All @@ -32,10 +28,6 @@ if(WIN32) # Setup some variables for Windows build
)

set(IMGUI_SOURCES
deps/ImGui/imgui.cpp
deps/ImGui/imgui_draw.cpp
deps/ImGui/imgui_tables.cpp
deps/ImGui/imgui_widgets.cpp
deps/ImGui/backends/imgui_impl_dx9.h
deps/ImGui/backends/imgui_impl_dx9.cpp
deps/ImGui/backends/imgui_impl_dx10.h
Expand All @@ -62,27 +54,19 @@ elseif(APPLE)
link_libraries("-framework AppKit -framework Carbon -framework Metal -framework MetalKit")

set(INGAMEOVERLAY_SOURCES
src/BaseHook.cpp
src/Internal.cpp
src/MacOSX/RendererDetector.mm
src/MacOSX/NSViewHook.mm
src/MacOSX/OpenGLHook.mm
src/MacOSX/MetalHook.mm
)

set(PRIVATE_INGAMEOVERLAY_HEADERS
src/InternalIncludes.h
src/BaseHook.h
src/MacOSX/NSViewHook.h
src/MacOSX/OpenGLHook.h
src/MacOSX/MetalHook.h
)

set(IMGUI_SOURCES
deps/ImGui/imgui.cpp
deps/ImGui/imgui_draw.cpp
deps/ImGui/imgui_tables.cpp
deps/ImGui/imgui_widgets.cpp
deps/ImGui/backends/imgui_impl_opengl2.h
deps/ImGui/backends/imgui_impl_opengl2.cpp
deps/ImGui/backends/imgui_impl_opengl3.h
Expand All @@ -96,28 +80,20 @@ elseif(APPLE)
elseif(UNIX)

set(INGAMEOVERLAY_SOURCES
src/BaseHook.cpp
src/Internal.cpp
src/Linux/RendererDetector.cpp
src/Linux/OpenGLXHook.cpp
src/Linux/X11Hook.cpp
src/Linux/VulkanHook.cpp
)

set(PRIVATE_INGAMEOVERLAY_HEADERS
src/InternalIncludes.h
src/VulkanHelpers.h
src/BaseHook.h
src/Linux/OpenGLXHook.h
src/Linux/X11Hook.h
src/Linux/VulkanHook.h
)

set(IMGUI_SOURCES
deps/ImGui/imgui.cpp
deps/ImGui/imgui_draw.cpp
deps/ImGui/imgui_tables.cpp
deps/ImGui/imgui_widgets.cpp
deps/ImGui/backends/imgui_impl_vulkan.h
deps/ImGui/backends/imgui_impl_vulkan.cpp
deps/ImGui/backends/imgui_impl_opengl3.h
Expand Down Expand Up @@ -158,9 +134,31 @@ else()
set(IMGUI_USER_CONFIG "" CACHE FILEPATH "")
endif()

list(APPEND INGAMEOVERLAY_SOURCES
src/BaseHook.cpp
src/Internal.cpp
src/RendererHookInternal.cpp
src/RendererResourceInternal.cpp
)

list(APPEND PRIVATE_INGAMEOVERLAY_HEADERS
src/InternalIncludes.h
src/BaseHook.h
src/RendererHookInternal.h
src/RendererResourceInternal.h
)

list(APPEND IMGUI_SOURCES
deps/ImGui/imgui.cpp
deps/ImGui/imgui_draw.cpp
deps/ImGui/imgui_tables.cpp
deps/ImGui/imgui_widgets.cpp
)

set(INGAMEOVERLAY_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/include/InGameOverlay/RendererHook.h
${CMAKE_CURRENT_SOURCE_DIR}/include/InGameOverlay/RendererDetector.h
${CMAKE_CURRENT_SOURCE_DIR}/include/InGameOverlay/RendererResource.h
)

set(INGAMEOVERLAY_IMGUI_HEADERS
Expand Down
82 changes: 58 additions & 24 deletions include/InGameOverlay/RendererHook.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@
#pragma once

#include <functional>
#include <string>
#include <memory>
#include <cstdint>
#include <set>

#include "RendererResource.h"

namespace InGameOverlay {

Expand Down Expand Up @@ -61,11 +60,17 @@ enum class RendererHookType_t : uint8_t
Any = DirectX9 | DirectX10 | DirectX11 | DirectX12 | OpenGL | Vulkan | Metal,
};

/// <summary>
/// The renderer hook.
/// ResourceAutoLoad_t: Default value is ResourceAutoLoad_t::Batch
/// BatchSize: Default value is 10
/// </summary>
class RendererHook_t
{
public:
virtual ~RendererHook_t() {}

// TODO: Deprecated direct use of thoses and use either a setter or plain C function pointers with void* user parameter.
std::function<void()> OverlayProc;
std::function<void(OverlayHookState)> OverlayHookReady;

Expand All @@ -82,7 +87,7 @@ class RendererHook_t
/// *Can be nullptr*. Fill this parameter with your own ImGuiAtlas pointer if you don't want ImGui to generate one for you.
/// </param>
/// <returns></returns>
virtual bool StartHook(std::function<void()> key_combination_callback, std::set<ToggleKey> toggle_keys, /*ImFontAtlas* */ void* imgui_font_atlas = nullptr) = 0;
virtual bool StartHook(std::function<void()> keyCombinationCallback, ToggleKey toggleKeys[], int toggleKeysCount, /*ImFontAtlas* */ void* imguiFontAtlas = nullptr) = 0;

/// <summary>
/// Change the hooked application input policy.
Expand All @@ -104,42 +109,71 @@ class RendererHook_t
/// <returns></returns>
virtual void HideOverlayInputs(bool hide) = 0;

/// <summary>
/// Returns the hook state. If its started, then the functions are hooked (redirected to InGameOverlay) and will intercepts the application frame rendering.
/// </summary>
/// <returns></returns>
virtual bool IsStarted() = 0;

/// <summary>
/// Load an RGBA ordered buffer into GPU and returns a handle to this ressource to be used by ImGui.
/// Get the current renderer library name.
/// </summary>
/// <param name="image_data">
/// The RGBA buffer.
/// </param>
/// <param name="width">
/// Your RGBA image width.
/// </param>
/// <param name="height">
/// Your RGBA image height.
/// </param>
/// <returns></returns>
virtual std::weak_ptr<uint64_t> CreateImageResource(const void* image_data, uint32_t width, uint32_t height) = 0;
virtual const char* GetLibraryName() const = 0;

/// <summary>
/// Frees a previously image resource created with CreateImageResource.
/// Get the current renderer hook type.
/// </summary>
/// <param name="resource">
/// The weak_ptr to the resource. Its safe to call with an invalid weak_ptr.
/// </param>
virtual void ReleaseImageResource(std::weak_ptr<uint64_t> resource) = 0;
/// <returns></returns>
virtual RendererHookType_t GetRendererHookType() const = 0;

/// <summary>
/// Get the current renderer library name.
/// Gets the auto load batch size.
/// </summary>
/// <returns></returns>
virtual const std::string& GetLibraryName() const = 0;
virtual uint32_t GetAutoLoadBatchSize() = 0;

/// <summary>
/// Get the current renderer hook type.
/// Sets how many resources the renderer hook can load before rendering the frame, to not block the rendering with a hundred of resource to load.
/// </summary>
/// <param name="batchSize"></param>
virtual void SetAutoLoadBatchSize(uint32_t batchSize) = 0;

/// <summary>
/// Gets if the resources will be auto loaded by default. This state doesn't affect resources created before its call.
/// </summary>
/// <returns></returns>
virtual RendererHookType_t GetRendererHookType() const = 0;
virtual ResourceAutoLoad_t GetResourceAutoLoad() const = 0;

/// <summary>
/// Sets if the resources will be auto loaded by default.
/// </summary>
/// <param name="autoLoad"></param>
virtual void SetResourceAutoLoad(ResourceAutoLoad_t autoLoad = ResourceAutoLoad_t::Batch) = 0;

/// <summary>
/// Creates an image resource that can be setup and used later.
/// </summary>
/// <returns></returns>
virtual RendererResource_t* CreateResource() = 0;

/// <summary>
/// Loads an RGBA ordered buffer into GPU and returns a resource.
/// </summary>
/// <param name="image_data">
/// The RGBA buffer.
/// </param>
/// <param name="width">
/// Your RGBA image width.
/// </param>
/// <param name="height">
/// Your RGBA image height.
/// </param>
/// <param name="attach">
/// If set, the pointer, width and height will be stored in the RendererResource_t to be reloaded later if a renderer reset occurs.
/// </param>
/// <returns></returns>
virtual RendererResource_t* CreateAndLoadResource(const void* image_data, uint32_t width, uint32_t height, bool attach) = 0;
};

}
Expand Down
112 changes: 112 additions & 0 deletions include/InGameOverlay/RendererResource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright (C) Nemirtingas
* This file is part of the ingame overlay project
*
* The ingame overlay project is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* The ingame overlay project is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the ingame overlay project; if not, see
* <http://www.gnu.org/licenses/>.
*/

#pragma once

namespace InGameOverlay {

enum class ResourceAutoLoad_t : uint8_t
{
None = 0,
Batch = 1,
OnUse = 2,
};

/// <summary>
/// A renderer resource. It will be tied to the RendererHook that created it. Don't use it if you recycle the renderer hook.
/// </summary>
class RendererResource_t
{
protected:
virtual ~RendererResource_t() {}

public:
/// <summary>
/// Deletes the resource.
/// </summary>
virtual void Delete() = 0;
/// <summary>
/// Checks if the resource is loaded.
/// </summary>
/// <returns>Is loaded or not</returns>
virtual bool IsLoaded() const = 0;
/// <summary>
/// Returns if the resource will be loaded on demand in a frame.
/// </summary>
/// <returns>Will it be loaded automatically if needed</returns>
virtual ResourceAutoLoad_t AutoLoad() const = 0;
/// <summary>
/// Returns if the resource will be loaded on demand in a frame.
/// </summary>
virtual void SetAutoLoad(ResourceAutoLoad_t autoLoad) = 0;
/// <summary>
/// Returns if the resource has an image attached to it so it can be auto loaded.
/// </summary>
/// <returns>Can be loaded</returns>
virtual bool CanBeLoaded() const = 0;
/// <summary>
/// Explicitly load the resource. (If not already loaded or if you changed its attachement)
/// </summary>
/// <returns>Is the resource ready on the GPU.</returns>
virtual bool LoadAttachedResource() = 0;
/// <summary>
/// Explicitly load the resource and don't store the data internally. The resource will not be able to reload itself on renderer reset.
/// </summary>
/// <param name="data"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
virtual bool Load(const void* data, uint32_t width, uint32_t height) = 0;
/// <summary>
/// Gets the resource id usable by ImGui::Image(). It will also trigger the autoload if set.
/// If autoload is in batch mode, the loading can be deferred by a few frames. (at least 1)
/// </summary>
/// <returns>The ImGui's image handle, 0 if it is not ready</returns>
virtual uint64_t GetResourceId() = 0;
/// <summary>
/// Return the loaded or attached resource width.
/// </summary>
/// <returns></returns>
virtual uint32_t Width() const = 0;
/// <summary>
/// Return the loaded or attached resource height.
/// </summary>
/// <returns></returns>
virtual uint32_t Height() const = 0;
/// <summary>
/// Attach a resource to this RendererResource, it will NOT OWN the data.
/// You are responsible to not outlive this object usage to the resource buffer.
/// Attaching a new resource will trigger the autoload if it is enabled, else, the old resource will still be used until you unload it.
/// </summary>
/// <param name="data">The resource raw data (in RGBA format)</param>
/// <param name="width">The resource width</param>
/// <param name="height">The resource height</param>
virtual void AttachResource(const void* data, uint32_t width, uint32_t height) = 0;
/// <summary>
/// Clears the attached resource. This will NOT delete the resource loaded onto the GPU. Call Unload for that purpose.
/// </summary>
virtual void ClearAttachedResource() = 0;
/// <summary>
/// Unloads the resource from the GPU. GetResourceId will return an invalid handle, IsLoaded will return false.
/// If auto loading is enabled, it will load again the resource if its not cleared.
/// </summary>
virtual void Unload(bool clearAttachedResource = true) = 0;
};

}
Loading