mirror of
https://github.com/TheBozos/BozoEngine.git
synced 2026-12-01 12:53:50 -10:00
When a player installs ReShade (its opengl32.dll beside the executable), the program registers as one of its add-ons and has it render its effects inside present(), after the 3D picture and before the HUD and menus, so a colour grade, depth of field or ambient occlusion no longer goes over the 2D. The F2 / F3 panels stay clear too. Without ReShade nothing changes; BOZO_RESHADE_AT_PRESENT=1 keeps ReShade's own pass over everything. - Renderer::setBeforeOverlay: a hook present() calls when it draws to the window, between the 3D and the 2D, with the window's size; present() binds its own state again afterwards. - app/reshade_bridge: registration after the window is made (ReShade's DLL loads with it) and before the GL context; the effects run on a texture of the window's size, copied upside down from and back to the window, as ReShade's own OpenGL pass does (its effects expect the top row first). - ReShade 6.8.0's API headers (BSD-3-Clause) in app/reshade/. - MinGW and ReShade's MSVC return structs from methods differently, so no API method returning a struct is called; views are built as ReShade's OpenGL backend makes them. Co-Authored-By: Claude Opus 5.5 <[email protected]>
165 lines
7.8 KiB
C++
165 lines
7.8 KiB
C++
// ReShade, when a player installs it (its opengl32.dll beside the executable): this program registers as one of its
|
|
// add-ons and has it render its effects inside the present, after the 3D picture and before the 2D (HUD, menus), so
|
|
// they leave the 2D alone; ReShade then skips its own pass at the buffer swap (effect_runtime::render_effects). The
|
|
// F2 / F3 panels are drawn after the present and stay clear of the effects too. Without ReShade nothing happens.
|
|
// BOZO_RESHADE_AT_PRESENT=1: no registration, ReShade's own pass over everything as before.
|
|
//
|
|
// The API's headers (reshade/, ReShade 6.8.0's include/, BSD-3-Clause, LICENSE.md there) must match the player's
|
|
// ReShade: their RESHADE_API_VERSION is checked by the registration, and a refused one is logged. This file must not
|
|
// include Dear ImGui: with IMGUI_VERSION_NUM defined the registration also asks for ReShade's ImGui table.
|
|
//
|
|
// ReShade is built with MSVC and this with MinGW GCC: the two return a struct from a C++ method differently (MSVC
|
|
// through a hidden pointer, GCC in RAX for an 8-byte one), so no API method that returns a struct may be called from
|
|
// here (effect_runtime::get_back_buffer crashed). Only methods returning pointers, integers or nothing are used; the
|
|
// picture's view is built as ReShade's OpenGL backend makes it (opengl_impl_type_convert.hpp,
|
|
// make_resource_view_handle: target << 40 | object, here GL_TEXTURE_2D and the texture).
|
|
#include "reshade_bridge.h"
|
|
|
|
#include "bozo/render/gl.h"
|
|
#include "bozo/render/renderer.h"
|
|
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
|
|
#ifdef _WIN32
|
|
// Before ReShade's headers: Windows' (MinGW's __uuidof, used in their templates). Their own warnings are theirs.
|
|
#include <windows.h>
|
|
#include <objbase.h>
|
|
#pragma GCC diagnostic push
|
|
#pragma GCC diagnostic ignored "-Wcast-function-type"
|
|
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
|
|
#pragma GCC diagnostic ignored "-Wignored-qualifiers"
|
|
#pragma GCC diagnostic ignored "-Wattributes"
|
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
|
#include "reshade/reshade.hpp"
|
|
#pragma GCC diagnostic pop
|
|
|
|
namespace bozo::app
|
|
{
|
|
namespace
|
|
{
|
|
|
|
bool g_registered = false;
|
|
reshade::api::effect_runtime *g_runtime = nullptr; // the window's (one GL context); none while ReShade resets it
|
|
|
|
void onInit(reshade::api::effect_runtime *runtime)
|
|
{
|
|
g_runtime = runtime;
|
|
std::fprintf(stderr, "[reshade] effects run before the 2D\n");
|
|
}
|
|
|
|
void onDestroy(reshade::api::effect_runtime *runtime)
|
|
{
|
|
if (g_runtime == runtime)
|
|
g_runtime = nullptr;
|
|
}
|
|
|
|
// The picture the effects run on. ReShade's textures hold the top row first (Direct3D's way), and its OpenGL
|
|
// pass at the buffer swap never runs on the window itself: it copies the window upside down into a texture,
|
|
// runs the effects there and copies it back the same way (runtime.cpp, on_init: "Always use resolve texture in
|
|
// OpenGL to flip vertically"). Handed the window, the effects came out upside down; this does what that pass
|
|
// does, in a texture of its own the window's size.
|
|
struct Picture
|
|
{
|
|
render::gl::GLuint texture = 0, fbo = 0;
|
|
int width = 0, height = 0;
|
|
} g_picture;
|
|
|
|
bool picture(int width, int height)
|
|
{
|
|
namespace gl = render::gl;
|
|
const gl::Api &g = gl::api();
|
|
if (g_picture.texture && g_picture.width == width && g_picture.height == height)
|
|
return true;
|
|
if (!g_picture.texture)
|
|
{
|
|
g.GenTextures(1, &g_picture.texture);
|
|
g.GenFramebuffers(1, &g_picture.fbo);
|
|
}
|
|
g.ActiveTexture(gl::kTexture0);
|
|
g.BindTexture(gl::kTexture2D, g_picture.texture);
|
|
g.TexImage2D(gl::kTexture2D, 0, static_cast<gl::GLint>(gl::kRgba8), width, height, 0, gl::kRgba, gl::kUnsignedByte, nullptr);
|
|
g.TexParameteri(gl::kTexture2D, gl::kTextureMinFilter, static_cast<gl::GLint>(gl::kNearest));
|
|
g.TexParameteri(gl::kTexture2D, gl::kTextureMagFilter, static_cast<gl::GLint>(gl::kNearest));
|
|
g.TexParameteri(gl::kTexture2D, gl::kTextureMaxLevel, 0);
|
|
g.BindTexture(gl::kTexture2D, 0);
|
|
g.BindFramebuffer(gl::kFramebuffer, g_picture.fbo);
|
|
g.FramebufferTexture2D(gl::kFramebuffer, gl::kColorAttachment0, gl::kTexture2D, g_picture.texture, 0);
|
|
const bool complete = g.CheckFramebufferStatus(gl::kFramebuffer) == gl::kFramebufferComplete;
|
|
g.BindFramebuffer(gl::kFramebuffer, 0);
|
|
g_picture.width = complete ? width : 0;
|
|
g_picture.height = complete ? height : 0;
|
|
if (!complete)
|
|
std::fprintf(stderr, "[reshade] no %dx%d picture for the effects: none this frame\n", width, height);
|
|
return complete;
|
|
}
|
|
|
|
// From one framebuffer to the other, upside down (the window's rows go bottom first, ReShade's top first).
|
|
void flip(render::gl::GLuint from, render::gl::GLuint to, int width, int height)
|
|
{
|
|
namespace gl = render::gl;
|
|
const gl::Api &g = gl::api();
|
|
g.BindFramebuffer(gl::kReadFramebuffer, from);
|
|
g.BindFramebuffer(gl::kDrawFramebuffer, to);
|
|
g.Disable(gl::kScissorTest);
|
|
g.ColorMask(gl::kTrue, gl::kTrue, gl::kTrue, gl::kTrue);
|
|
g.BlitFramebuffer(0, 0, width, height, 0, height, width, 0, gl::kColorBufferBit, gl::kNearest);
|
|
}
|
|
|
|
void renderEffects(void *, int width, int height)
|
|
{
|
|
if (!g_runtime || width <= 0 || height <= 0 || !picture(width, height))
|
|
return;
|
|
constexpr uint64_t kGlTexture2D = 0x0DE1;
|
|
const reshade::api::resource_view view = {(kGlTexture2D << 40) | g_picture.texture};
|
|
flip(0, g_picture.fbo, width, height);
|
|
reshade::api::command_list *const list = g_runtime->get_command_queue()->get_immediate_command_list();
|
|
// The same view for the sRGB passes: the picture is not sRGB, nor is the window.
|
|
g_runtime->render_effects(list, view, view);
|
|
flip(g_picture.fbo, 0, width, height);
|
|
}
|
|
}
|
|
|
|
void attachReshade()
|
|
{
|
|
if (g_registered)
|
|
return;
|
|
if (const char *v = std::getenv("BOZO_RESHADE_AT_PRESENT"); v && *v && *v != '0')
|
|
return;
|
|
// ReShade's module is found by its exports; none loaded is the usual case and says nothing.
|
|
if (!reshade::internal::get_reshade_module_handle())
|
|
return;
|
|
if (!reshade::register_addon(GetModuleHandleW(nullptr)))
|
|
{
|
|
std::fprintf(stderr, "[reshade] ReShade is loaded but refused this program as an add-on (API version %d expected; "
|
|
"another ReShade version, or a build without add-on support): its effects cover the 2D too\n",
|
|
RESHADE_API_VERSION);
|
|
return;
|
|
}
|
|
reshade::register_event<reshade::addon_event::init_effect_runtime>(&onInit);
|
|
reshade::register_event<reshade::addon_event::destroy_effect_runtime>(&onDestroy);
|
|
render::Renderer::instance().setBeforeOverlay(&renderEffects, nullptr);
|
|
g_registered = true;
|
|
}
|
|
|
|
void detachReshade()
|
|
{
|
|
if (!g_registered)
|
|
return;
|
|
render::Renderer::instance().setBeforeOverlay(nullptr, nullptr);
|
|
g_picture = {}; // its texture goes with the GL context, destroyed after this
|
|
reshade::unregister_event<reshade::addon_event::init_effect_runtime>(&onInit);
|
|
reshade::unregister_event<reshade::addon_event::destroy_effect_runtime>(&onDestroy);
|
|
reshade::unregister_addon(GetModuleHandleW(nullptr));
|
|
g_runtime = nullptr;
|
|
g_registered = false;
|
|
}
|
|
}
|
|
#else
|
|
namespace bozo::app
|
|
{
|
|
void attachReshade() {}
|
|
void detachReshade() {}
|
|
}
|
|
#endif
|