Skip to content

Commit

Permalink
Merge pull request #21 from JoeyAnthony/feature/clean-code-conventions
Browse files Browse the repository at this point in the history
Feature/clean code conventions
  • Loading branch information
JoeyAnthony authored Apr 4, 2024
2 parents 0624f74 + c7db2de commit 65c5ace
Show file tree
Hide file tree
Showing 16 changed files with 481 additions and 510 deletions.
22 changes: 21 additions & 1 deletion gamebridge_reshade/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
# SimulatedRealityInjector
# SimulatedRealityInjector

## Summary
Implements the SR SDK into ReShade to allow for conversion of SBS content to be turned into 3D on "Simulated Realty" displays from any brand or OEM.

## Installation
Download the latest binary release from the tags section on GitHub and follow the installation instructions hosted here: <br/>
https://docs.google.com/document/d/1ZlB1LhWfolDzp-nI4ojzc0AbdSv05lwYqka7FpztK8Y/edit?usp=sharing

## Building
1. Install the Simulated Reality SDK: www.srappstore.com/develop <br/><sup>(Alternatively you can download a supported SDK version directly from the Google Docs installation instructions above)<sup/>
2. Clone the repository
3. Build the project with Cmake, no additional Cmake arguments are required

## Code conventions
A link to the repository's code conventions can be found here: <br/>
https://docs.google.com/document/d/1mYjsWdWhKdVjgc639hNoqFpKtnVTbTRmOcpre0uQEb8/edit?usp=sharing

## Contact & Contributions
If you have a question or want to make a contribution, please make a pull request or let us know through Discord.<br/>
https://discord.gg/K46jgbzwDa
104 changes: 45 additions & 59 deletions gamebridge_reshade/src/directx10weaver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@

#include "directx10weaver.h"

// Directx
#include <DirectXMath.h>

DirectX10Weaver::DirectX10Weaver(SR::SRContext* context) {
//Set context here.
// Set context here.
sr_context = context;
weaving_enabled = true;
}

bool DirectX10Weaver::create_effect_copy_buffer(const reshade::api::resource_desc& effect_resource_desc)
{
bool DirectX10Weaver::create_effect_copy_buffer(const reshade::api::resource_desc& effect_resource_desc) {
reshade::api::resource_desc desc = effect_resource_desc;
desc.type = reshade::api::resource_type::texture_2d;
desc.heap = reshade::api::memory_heap::gpu_only;
Expand Down Expand Up @@ -68,17 +70,16 @@ bool DirectX10Weaver::init_weaver(reshade::api::effect_runtime* runtime, reshade

try {
weaver = new SR::PredictingDX10Weaver(*sr_context, dev, desc.texture.width, desc.texture.height, (HWND)runtime->get_hwnd());
weaver->setInputFrameBuffer((ID3D10ShaderResourceView*)rtv.handle); //resourceview of the buffer
weaver->setInputFrameBuffer((ID3D10ShaderResourceView*)rtv.handle); // Resourceview of the buffer
sr_context->initialize();
reshade::log_message(reshade::log_level::info, "Initialized weaver");

// Set mode to latency in frames by default.
set_latency_mode(LatencyModes::framerateAdaptive);
set_latency_framerate_adaptive(DEFAULT_WEAVER_LATENCY);
std::string latencyLog = "Current latency mode set to: STATIC " + std::to_string(DEFAULT_WEAVER_LATENCY) + " Microseconds";
reshade::log_message(reshade::log_level::info, latencyLog.c_str());
set_latency_frametime_adaptive(default_weaver_latency);
std::string latency_log = "Current latency mode set to: STATIC " + std::to_string(default_weaver_latency) + " Microseconds";
reshade::log_message(reshade::log_level::info, latency_log.c_str());
}
catch (std::exception e) {
catch (std::exception &e) {
reshade::log_message(reshade::log_level::info, e.what());
return false;
}
Expand All @@ -97,8 +98,8 @@ void DirectX10Weaver::draw_status_overlay(reshade::api::effect_runtime *runtime)

// Log the latency mode
std::string latencyModeDisplay = "Latency mode: ";
if(current_latency_mode == LatencyModes::framerateAdaptive) {
latencyModeDisplay += "IN " + std::to_string(lastLatencyFrameTimeSet) + " MICROSECONDS";
if (current_latency_mode == LatencyModes::FRAMERATE_ADAPTIVE) {
latencyModeDisplay += "IN " + std::to_string(last_latency_frame_time_set) + " MICROSECONDS";
}
else {
latencyModeDisplay += "IN " + std::to_string(runtime->get_back_buffer_count()) + " FRAMES";
Expand All @@ -110,32 +111,6 @@ void DirectX10Weaver::draw_status_overlay(reshade::api::effect_runtime *runtime)
ImGui::TextUnformatted(s.c_str());
}

void DirectX10Weaver::draw_debug_overlay(reshade::api::effect_runtime* runtime)
{
ImGui::TextUnformatted("Some text");

if (ImGui::Button("Press me to open an additional popup window"))
g_popup_window_visible = true;

if (g_popup_window_visible)
{
ImGui::Begin("Popup", &g_popup_window_visible);
ImGui::TextUnformatted("Some other text");
ImGui::End();
}
}

void DirectX10Weaver::draw_sr_settings_overlay(reshade::api::effect_runtime* runtime)
{
ImGui::Checkbox("Turn on SR", &g_popup_window_visible);
ImGui::SliderFloat("View Separation", &view_separation, -50.f, 50.f);
ImGui::SliderFloat("Vertical Shift", &vertical_shift, -50.f, 50.f);
}

void DirectX10Weaver::draw_settings_overlay(reshade::api::effect_runtime* runtime)
{
}

void DirectX10Weaver::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) {
reshade::api::resource_view chosen_rtv;

Expand All @@ -150,25 +125,19 @@ void DirectX10Weaver::on_reshade_finish_effects(reshade::api::effect_runtime* ru

if (weaver_initialized) {
// Check if we need to set the latency in frames.
if(get_latency_mode() == LatencyModes::latencyInFramesAutomatic) {
if (get_latency_mode() == LatencyModes::LATENCY_IN_FRAMES_AUTOMATIC) {
weaver->setLatencyInFrames(runtime->get_back_buffer_count()); // Set the latency with which the weaver should do prediction.
}

//Check texture size
// Check texture size
if (desc.texture.width != effect_frame_copy_x || desc.texture.height != effect_frame_copy_y) {
// Update current buffer format
current_buffer_format = desc.texture.format;

// Check buffer format and see if it's in the list of known problematic ones. Change to SRGB rtv if so.
if ((std::find(srgb_color_formats.begin(), srgb_color_formats.end(), desc.texture.format) != srgb_color_formats.end())) {
// SRGB format detected, switch to SRGB buffer.
use_srgb_rtv = true;
}
else {
use_srgb_rtv = false;
}
// Update color format settings
check_color_format(desc);

//TODO Might have to get the buffer from the create_effect_copy_buffer function and only swap them when creation suceeds
// Todo: Might have to get the buffer from the create_effect_copy_buffer function and only swap them when creation suceeds
d3d10_device->destroy_resource(effect_frame_copy);
d3d10_device->destroy_resource_view(effect_frame_copy_srv);
if (!create_effect_copy_buffer(desc) && !resize_buffer_failed) {
Expand Down Expand Up @@ -196,6 +165,12 @@ void DirectX10Weaver::on_reshade_finish_effects(reshade::api::effect_runtime* ru
}
}
else {
// Set current buffer format
current_buffer_format = desc.texture.format;

// Set color format settings
check_color_format(desc);

create_effect_copy_buffer(desc);
if (init_weaver(runtime, effect_frame_copy, cmd_list)) {
// Set context and input frame buffer again to make sure they are correct
Expand All @@ -214,30 +189,30 @@ void DirectX10Weaver::on_init_effect_runtime(reshade::api::effect_runtime* runti
d3d10_device = runtime->get_device();
}

void DirectX10Weaver::do_weave(bool doWeave)
void DirectX10Weaver::do_weave(bool do_weave)
{
weaving_enabled = doWeave;
weaving_enabled = do_weave;
}

bool DirectX10Weaver::set_latency_in_frames(int32_t numberOfFrames) {
bool DirectX10Weaver::set_latency_in_frames(int32_t number_of_frames) {
if (weaver_initialized) {
if (numberOfFrames < 0) {
set_latency_mode(LatencyModes::latencyInFramesAutomatic);
if (number_of_frames < 0) {
set_latency_mode(LatencyModes::LATENCY_IN_FRAMES_AUTOMATIC);
}
else {
set_latency_mode(LatencyModes::latencyInFrames);
weaver->setLatencyInFrames(numberOfFrames);
set_latency_mode(LatencyModes::LATENCY_IN_FRAMES);
weaver->setLatencyInFrames(number_of_frames);
}
return true;
}
return false;
}

bool DirectX10Weaver::set_latency_framerate_adaptive(uint32_t frametimeInMicroseconds) {
bool DirectX10Weaver::set_latency_frametime_adaptive(uint32_t frametime_in_microseconds) {
if (weaver_initialized) {
set_latency_mode(LatencyModes::framerateAdaptive);
weaver->setLatency(frametimeInMicroseconds);
lastLatencyFrameTimeSet = frametimeInMicroseconds;
set_latency_mode(LatencyModes::FRAMERATE_ADAPTIVE);
weaver->setLatency(frametime_in_microseconds);
last_latency_frame_time_set = frametime_in_microseconds;
return true;
}
return false;
Expand All @@ -247,6 +222,17 @@ void DirectX10Weaver::set_latency_mode(LatencyModes mode) {
current_latency_mode = mode;
}

void DirectX10Weaver::check_color_format(reshade::api::resource_desc desc) {
// Check buffer format and see if it's in the list of known SRGB ones. Change to SRGB rtv if so.
if ((std::find(srgb_color_formats.begin(), srgb_color_formats.end(), desc.texture.format) != srgb_color_formats.end())) {
// SRGB format detected, switch to SRGB buffer.
use_srgb_rtv = true;
}
else {
use_srgb_rtv = false;
}
}

LatencyModes DirectX10Weaver::get_latency_mode() {
return current_latency_mode;
}
57 changes: 34 additions & 23 deletions gamebridge_reshade/src/directx10weaver.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,69 @@
*/

#pragma once
#include "pch.h"

// Directx
#include <d3d10_1.h>
#include <d3d10.h>
#include <DirectXMath.h>

// Weaver
#include "sr/weaver/dx10weaver.h"

// srReshade
#include "igraphicsapi.h"
#include "pch.h"

class DirectX10Weaver: public IGraphicsApi {
uint32_t lastLatencyFrameTimeSet = DEFAULT_WEAVER_LATENCY;
uint32_t last_latency_frame_time_set = default_weaver_latency;
uint32_t effect_frame_copy_x = 0, effect_frame_copy_y = 0;

bool weaver_initialized = false;
bool weaving_enabled = false;
SR::SRContext* sr_context;
SR::PredictingDX10Weaver* weaver = nullptr;
reshade::api::device* d3d10_device = nullptr;
bool popup_window_visible = false;
bool resize_buffer_failed = false;
bool use_srgb_rtv = false;

bool use_srgb_rtv = true;
bool g_popup_window_visible = false;
float view_separation = 0.f;
float vertical_shift = 0.f;

SR::SRContext* sr_context;
SR::PredictingDX10Weaver* weaver = nullptr;

reshade::api::device* d3d10_device = nullptr;
reshade::api::format current_buffer_format = reshade::api::format::unknown;
reshade::api::command_list* command_list{};
reshade::api::resource effect_frame_copy{};
reshade::api::resource_view effect_frame_copy_srv{};
uint32_t effect_frame_copy_x = 0, effect_frame_copy_y = 0;
bool resize_buffer_failed = false;

LatencyModes current_latency_mode = LatencyModes::framerateAdaptive;
LatencyModes current_latency_mode = LatencyModes::FRAMERATE_ADAPTIVE;

/// \brief Private function to set the internal latency mode of the weaver
/// \param mode The latency mode to persist
void set_latency_mode(LatencyModes mode);

/// \brief Checks the current rtv buffer color format and chooses the correct rtv for weaving based on that.
/// \param desc Represents the current ReShade resource view
void DirectX10Weaver::check_color_format(reshade::api::resource_desc desc);

public:
/// \brief Explicit constructor
/// \param context Pointer to an already initialized SRContext
explicit DirectX10Weaver(SR::SRContext* context);

/// \brief Initialized the SR weaver appropriate for the graphics API
/// \param runtime Represents the reshade effect runtime
/// \param rtv Represents the current render target view
/// \param cmd_list Represents the current command list from ReShade
/// \return A bool representing if the weaver was initialized successfully
bool init_weaver(reshade::api::effect_runtime* runtime, reshade::api::resource rtv, reshade::api::command_list* cmd_list);

/// \brief Creates and reset the effect copy resource so it is similar to the back buffer resource, then use it as weaver input.
/// \param effect_resource_desc ReShade resource representing the currently selected back buffer description
/// \return A bool respresenting if the effect frame copy was successful
bool create_effect_copy_buffer(const reshade::api::resource_desc& effect_resource_desc);


// Inherited via IGraphicsApi
void draw_debug_overlay(reshade::api::effect_runtime* runtime) override;
void draw_sr_settings_overlay(reshade::api::effect_runtime* runtime) override;
void draw_settings_overlay(reshade::api::effect_runtime* runtime) override;
void draw_status_overlay(reshade::api::effect_runtime *runtime) override;
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) override;
void on_init_effect_runtime(reshade::api::effect_runtime* runtime) override;
void do_weave(bool doWeave) override;
bool set_latency_in_frames(int32_t numberOfFrames) override;
bool set_latency_framerate_adaptive(uint32_t frametimeInMicroseconds) override;
void draw_status_overlay(reshade::api::effect_runtime *runtime) override;
void do_weave(bool do_weave) override;
bool set_latency_in_frames(int32_t number_of_frames) override;
bool set_latency_frametime_adaptive(uint32_t frametime_in_microseconds) override;
LatencyModes get_latency_mode() override;
};
Loading

0 comments on commit 65c5ace

Please sign in to comment.