Skip to content

Commit

Permalink
Merge pull request #16 from JoeyAnthony/feature/GB-203-Fix-crash-when…
Browse files Browse the repository at this point in the history
…-service-stopped

Added logging and prevented the addon from crashing when service is unavailable
  • Loading branch information
JoeyAnthony authored Feb 2, 2024
2 parents b433879 + 90c85c2 commit a499df2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 20 deletions.
13 changes: 4 additions & 9 deletions gamebridge_reshade/CMakelists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ add_library(${PROJECT_NAME} SHARED
src/hotkeymanager.h
)

# Determines whether the descriptor offset should be fetched from the ReShade source.
# OFF by default as it requires a small change to includes of ReShade
option(GET_DESCRIPTOR_OFFSET_FROM_RESHADE_SOURCE "Enable Get Descriptor Offset" OFF)
if (WIN32 AND CMAKE_SIZEOF_VOID_P EQUAL 4)
set(BUILD_PLATFORM 32)
endif()

# Set output name for the addon
set(ADDON_FILE_NAME "srReshade")
Expand Down Expand Up @@ -62,12 +62,7 @@ if(NOT ${ADDON_COPY_DIR} STREQUAL "")
message("Addon will be copied to ${ADDON_COPY_DIR}")
endif()

# Check the descriptor option and define a preprocessor macro accordingly
if(GET_DESCRIPTOR_OFFSET_FROM_RESHADE_SOURCE)
target_compile_definitions(${PROJECT_NAME} PRIVATE GET_DESCRIPTOR_OFFSET_FROM_RESHADE_SOURCE)
endif()

target_link_libraries(${PROJECT_NAME} PRIVATE
simulatedreality
srDirectX::srDirectX
srDirectX${BUILD_PLATFORM}::srDirectX
)
4 changes: 0 additions & 4 deletions gamebridge_reshade/src/directx12weaver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
* Copyright and license notices must be preserved. Contributors provide an express grant of patent rights. Modifications to the source code must be disclosed publicly.
*/

#ifdef GET_DESCRIPTOR_OFFSET_FROM_RESHADE_SOURCE
#include "reshade/source/d3d12/d3d12_impl_command_list.hpp"
#endif
#include "directx12weaver.h"
#include <sstream>

Expand Down Expand Up @@ -179,7 +176,6 @@ void DirectX12Weaver::on_reshade_finish_effects(reshade::api::effect_runtime* ru

// Weave to back buffer
cmd_list->barrier(effect_frame_copy, reshade::api::resource_usage::copy_dest, reshade::api::resource_usage::unordered_access);
//weaver->setInputFrameBuffer((ID3D12Resource*)effect_frame_copy.handle);
weaver->weave(desc.texture.width, desc.texture.height);

// Check if the descriptor heap offset is set. If it is, we have to reset the descriptor heaps to ensure the ReShade overlay can render.
Expand Down
46 changes: 39 additions & 7 deletions gamebridge_reshade/src/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ static const std::string sr_shader_name = "SR";
static char g_charBuffer[CHAR_BUFFER_SIZE];
static size_t g_charBufferSize = CHAR_BUFFER_SIZE;
static bool effects_are_active = false;
static bool sr_initialized = false;

std::vector<LPCWSTR> reshade_dll_names = { L"dxgi.dll", L"ReShade.dll", L"ReShade64.dll", L"ReShade32.dll", L"d3d9.dll", L"d3d10.dll", L"d3d11.dll", L"d3d12.dll", L"opengl32.dll" };

Expand Down Expand Up @@ -178,11 +179,24 @@ static void execute_hot_key_function_by_type(std::map<shortcutType, bool> hot_ke
}

static void draw_status_overlay(reshade::api::effect_runtime* runtime) {
if (weaver_implementation) {
bool printStatusInWeaver = true;
std::string status_string = "Status: \n";
if (sr_context == nullptr) {
// Unable to connect to the SR Service. Fall back to drawing the overlay UI ourselves.
status_string += "INACTIVE - NO SR SERVICE DETECTED, MAKE SURE THE SR PLATFORM IS INSTALLED AND RUNNING\nwww.srappstore.com\n";
printStatusInWeaver = false;
}
else if (weaver_implementation) {
weaver_implementation->draw_status_overlay(runtime);
} else {
}
else {
// Unable to create weaver implementation. Fall back to drawing the overlay UI ourselves.
ImGui::TextUnformatted("Status: INACTIVE - UNSUPPORTED GRAPHICS API");
status_string += "INACTIVE - UNSUPPORTED GRAPHICS API\n";
printStatusInWeaver = false;
}

if (!printStatusInWeaver) {
ImGui::TextColored(ImColor(240,100,100), "%s", status_string.c_str());
}
}

Expand Down Expand Up @@ -221,6 +235,10 @@ static void on_reshade_begin_effects(reshade::api::effect_runtime* runtime, resh
}

static void on_reshade_finish_effects(reshade::api::effect_runtime* runtime, reshade::api::command_list* cmd_list, reshade::api::resource_view rtv, reshade::api::resource_view rtv_srgb) {
if(!sr_initialized) {
return;
}

std::map<shortcutType, bool> hot_key_list;

//Check if certain hotkeys are being pressed
Expand All @@ -236,17 +254,31 @@ static void on_reshade_finish_effects(reshade::api::effect_runtime* runtime, res
}
}

static void init_sr() {
static bool init_sr() {
//Construct SR Context and senses
if (sr_context == nullptr) {
sr_context = new SR::SRContext;
try {
sr_context = new SR::SRContext;
}
catch (SR::ServerNotAvailableException& ex) {
// Unable to construct SR Context.
reshade::log_message(reshade::log_level::error, "Unable to connect to the SR Service, make sure the SR Platform is installed and running.");
sr_initialized = false;
return false;
}
lens_hint = SR::SwitchableLensHint::create(*sr_context);
sr_context->initialize();
}

// Only register these ReShade event callbacks when we have a valid SR Service, otherwise the app may crash.
sr_initialized = true;
return true;
}

static void on_init_effect_runtime(reshade::api::effect_runtime* runtime) {
init_sr();
if (!init_sr()) {
return;
}

//Todo: Move these hard-coded hotkeys to user-definable hotkeys in the .ini file
//Register some standard hotkeys
Expand Down Expand Up @@ -313,10 +345,10 @@ BOOL APIENTRY DllMain( HMODULE hModule,

reshade::register_event<reshade::addon_event::init_effect_runtime>(&on_init_effect_runtime);
reshade::register_event<reshade::addon_event::reshade_begin_effects>(&on_reshade_begin_effects);
reshade::register_event<reshade::addon_event::reshade_render_technique>(&on_render_technique);
reshade::register_event<reshade::addon_event::reshade_finish_effects>(&on_reshade_finish_effects);
reshade::register_event<reshade::addon_event::reshade_reloaded_effects>(&on_reshade_reload_effects);
reshade::register_event<reshade::addon_event::destroy_swapchain>(&on_destroy_swapchain);
reshade::register_event<reshade::addon_event::reshade_render_technique>(&on_render_technique);

reshade::register_overlay(nullptr, &draw_status_overlay);

Expand Down

0 comments on commit a499df2

Please sign in to comment.