ReShade API does not allow access to ImGui context

  • dfw
  • Topic Author
More
1 year 7 months ago - 1 year 7 months ago #1 by dfw ReShade API does not allow access to ImGui context was created by dfw
Right now ReShade API does not allow access to ImGuiContext so addon cannot read positions of already rendered UI windows.

Is there any "dirty" way to read such data? I want to receive ImVec4 xyx2y2 coords of displayed ImGui windows.

EDIT:
I think it is kinda possible if we register scissor rects registered between on_present and on_reshade_present but on_bind_scissor_rects
cannot be registered with limited add-on functionality so it's not ideal.
Last edit: 1 year 7 months ago by dfw.

Please Log in or Create an account to join the conversation.

  • crosire
More
1 year 7 months ago - 1 year 7 months ago #2 by crosire Replied by crosire on topic ReShade API does not allow access to ImGui context
There is no official way to access it (since I'm still hoping to somehow make it possible to support multiple ImGui versions ... converting structs etc.).

But ... it's not impossible with some hacky thinking:
"ImGui::GetIO()" effectively returns a pointer to the "IO" field in "ImGuiContext" (in a registered overlay callback, or a "reshade_overlay" event callback). So to get the pointer to the context, simply need to subtract the offset to that field and ... profit.
ImGuiContext *context = reinterpret_cast<ImGuiContext *>(reinterpret_cast<uintptr_t>(&ImGui::GetIO()) - offsetof(ImGuiContext, IO));
Last edit: 1 year 7 months ago by crosire.
The following user(s) said Thank You: dfw

Please Log in or Create an account to join the conversation.

  • dfw
  • Topic Author
More
1 year 7 months ago #3 by dfw Replied by dfw on topic ReShade API does not allow access to ImGui context
Thanks, You are totally right, I didn't think of it that way =D

Please Log in or Create an account to join the conversation.

  • dfw
  • Topic Author
More
1 year 7 months ago #4 by dfw Replied by dfw on topic ReShade API does not allow access to ImGui context
Strange...
I had some time today morning and tested this solution. Theoretically it should work. But it doesn't and context cannot be used reliably.

The pointer is misaligned with the context struct in the memory and i have no idea why - checked ReShade code and everything is as it should be.

Code to reproduce error:
template<typename... Args>
void reshade_log(Args... inputs)
{
    stringstream s;
    (
        [&]{
            s << inputs;
        } (), ...
    );
    reshade::log_message(3, s.str().c_str());
}

static void on_reshade_overlay(effect_runtime*) {
    ImGuiContext* contextA = reinterpret_cast<ImGuiContext*>(reinterpret_cast<uintptr_t>(&ImGui::GetIO()) - offsetof(ImGuiContext, IO));
    reshade_log("A ", contextA);
    ImGuiContext* contextB = reinterpret_cast<ImGuiContext*>(reinterpret_cast<uintptr_t>(&ImGui::GetStyle()) - offsetof(ImGuiContext, Style));
    reshade_log("B ", contextB);
    reshade_log("PTR DIFF ", reinterpret_cast<uintptr_t>(contextB) - reinterpret_cast<uintptr_t>(contextA));
    reshade_log("A Windows ", contextA->Windows.size()); //Gives mess value
    reshade_log("B Windows ", contextB->Windows.size()); //Gives mess value
}

This results in following logs:
A 000001BB0F420410
B 000001BB0F4204F0
PTR DIFF 224
A Windows -1098611737
B Windows 0

I've checked in difference between IO and Style offset with same results:
reshade_log("Offset diff ", offsetof(ImGuiContext, Style) - offsetof(ImGuiContext, IO));
reshade_log("Real diff ", reinterpret_cast<uintptr_t>(&ImGui::GetStyle()) - reinterpret_cast<uintptr_t>(&ImGui::GetIO()));

Logs:
Offset diff 5480
Real diff 5704

imgui_internal.h is in 1.86 version so context struct should be the same.
Any idea what could be wrong?

Please Log in or Create an account to join the conversation.

  • crosire
More
1 year 7 months ago - 1 year 7 months ago #5 by crosire Replied by crosire on topic ReShade API does not allow access to ImGui context
Make sure you are using the exact imgui version ReShade is using (which is not exactly 1.86, but rather a commit from the docking branch merged with 1.86: github.com/ocornut/imgui/blob/15b4a064f9...321/imgui_internal.h ). The docking branch has quite a few changes to the context struct.
Last edit: 1 year 7 months ago by crosire.
The following user(s) said Thank You: dfw

Please Log in or Create an account to join the conversation.

  • dfw
  • Topic Author
More
1 year 7 months ago #6 by dfw Replied by dfw on topic ReShade API does not allow access to ImGui context
You were right. I was not using exact commit and my struct was slightly off by 224 bytes.

Now everything works as expected, thanks! :)

Please Log in or Create an account to join the conversation.

  • dfw
  • Topic Author
More
1 year 7 months ago #7 by dfw Replied by dfw on topic ReShade API does not allow access to ImGui context
Just for the record how ImGui context can be used:

github.com/dwojtasik/PyHook/commit/e5309...88bd1972e7018ba8a318

Please Log in or Create an account to join the conversation.

We use cookies
We use cookies on our website. Some of them are essential for the operation of the forum. You can decide for yourself whether you want to allow cookies or not. Please note that if you reject them, you may not be able to use all the functionalities of the site.