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

Add gamescope-reshade Wayland interface #1495

Merged
merged 2 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions protocol/gamescope-reshade.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<protocol name="gamescope_reshade">

<copyright>
Copyright © 2024 Wayne Heaney

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
</copyright>

<description summary="gamescope-specific reshade integration">
This protocol allows applications to load and interact with a reshade FX shader in gamescope.
</description>

<interface name="gamescope_reshade" version="1">
<request name="destroy" type="destructor"></request>

<request name="set_effect">
<description summary="set the path to the reshade FX file">
The effect will be disabled to allow an opportunity to set uniform variables before enabling it.
</description>
<arg name="path" type="string" summary="Path to the FX file"></arg>
</request>

<event name="effect_ready">
<description summary="alerts when the requested effect is ready">
This event alerts the client when an effect has been enabled.
</description>
<arg name="effect_path" type="string" summary="Path to the FX file"/>
</event>

<request name="enable_effect">
<description summary="turn on the effect">
Enables the effect that was previously loaded by set_effect.
</description>
</request>

<request name="set_uniform_variable">
<description summary="set a uniform variable for the currently loaded effect">
Set the value of a uniform variable. Can be called before or after enabling the effect.
</description>
<arg name="key" type="string" summary="Name of the uniform variable"></arg>
<arg name="value" type="array" summary="Value of the uniform variable"></arg>
</request>

<request name="disable_effect">
<description summary="turn off the effect">
Disables the effect that was previously enabled by enable_effect.
</description>
</request>
</interface>
</protocol>
1 change: 1 addition & 0 deletions protocol/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ protocols = [
'gamescope-pipewire.xml',
'gamescope-input-method.xml',
'gamescope-control.xml',
'gamescope-reshade.xml',
'gamescope-swapchain.xml',
'gamescope-private.xml',

Expand Down
58 changes: 58 additions & 0 deletions src/WaylandServer/Reshade.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#pragma once

#include "WaylandProtocol.h"

#include "gamescope-reshade-protocol.h"
#include "reshade_effect_manager.hpp"

#include <cstring>

namespace gamescope::WaylandServer
{
class CReshadeManager : public CWaylandResource
{
public:
WL_PROTO_DEFINE( gamescope_reshade, 1 );
WL_PROTO_DEFAULT_CONSTRUCTOR();

void EffectReadyCallback( const char* path )
{
gamescope_reshade_send_effect_ready(GetResource(), path);
}

void SetEffect( const char *path )
{
reshade_effect_manager_set_effect( path,
[this]( const char* callbackPath ) { EffectReadyCallback( callbackPath ); }
);
}

void EnableEffect()
{
reshade_effect_manager_enable_effect();
}

void SetUniformVariable( const char *key, struct wl_array *value )
{
uint8_t* data_copy = new uint8_t[value->size];
std::memcpy(data_copy, value->data, value->size);
reshade_effect_manager_set_uniform_variable( key, data_copy );
}

void DisableEffect()
{
reshade_effect_manager_disable_effect();
}

};

const struct gamescope_reshade_interface CReshadeManager::Implementation =
{
.destroy = WL_PROTO_DESTROY(),
.set_effect = WL_PROTO( CReshadeManager, SetEffect ),
.enable_effect = WL_PROTO( CReshadeManager, EnableEffect ),
.set_uniform_variable = WL_PROTO( CReshadeManager, SetUniformVariable ),
.disable_effect = WL_PROTO( CReshadeManager, DisableEffect )
};

}
3 changes: 3 additions & 0 deletions src/WaylandServer/WaylandDecls.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ namespace gamescope::WaylandServer
class CLinuxDrmSyncobjTimeline;
using CLinuxDrmSyncobj = CWaylandProtocol<CLinuxDrmSyncobjManager>;

class CReshadeManager;
using CReshade = CWaylandProtocol<CReshadeManager>;

}
162 changes: 160 additions & 2 deletions src/reshade_effect_manager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <cstring>
#include <variant>
#include <unordered_map>

#include "reshade_effect_manager.hpp"
#include "log.hpp"
Expand All @@ -9,16 +10,29 @@
#include "effect_parser.hpp"
#include "effect_codegen.hpp"
#include "effect_preprocessor.hpp"
#include "gamescope-reshade-protocol.h"

#include "reshade_api_format.hpp"
#include "convar.h"

#include <stb_image.h>
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include <stb_image_resize.h>

#include <mutex>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <iostream>

// This is based on wl_array_for_each from `wayland-util.h` in the Wayland client library.
#define uint8_array_for_each(pos, data, size) \
for (pos = (decltype(pos))data; (const char *)pos < ((const char *)data + size); (pos)++)

static char* g_reshadeEffectPath = nullptr;
static std::function<void(const char*)> g_effectReadyCallback = nullptr;
static auto g_runtimeUniforms = std::unordered_map<std::string, uint8_t*>();
static std::mutex g_runtimeUniformsMutex;

const char *homedir;

Expand Down Expand Up @@ -172,6 +186,21 @@ class DepthUniform : public ReshadeUniform
virtual ~DepthUniform();
};

class RuntimeUniform : public ReshadeUniform
{
public:
RuntimeUniform(reshadefx::uniform_info uniformInfo);
void virtual update(void* mappedBuffer) override;
virtual ~RuntimeUniform();

private:
uint32_t offset;
uint32_t size;
std::string name;
reshadefx::type type;
std::variant<std::monostate, std::vector<float>, std::vector<int32_t>, std::vector<uint32_t>> defaultValue;
};

class DataUniform : public ReshadeUniform
{
public:
Expand Down Expand Up @@ -470,6 +499,100 @@ DepthUniform::~DepthUniform()
{
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////
RuntimeUniform::RuntimeUniform(reshadefx::uniform_info uniformInfo)
: ReshadeUniform(uniformInfo)
{
offset = uniformInfo.offset;
size = uniformInfo.size;
type = uniformInfo.type;
name = std::find_if(uniformInfo.annotations.begin(), uniformInfo.annotations.end(), [](const auto& a) { return a.name == "source"; })->value.string_data;

if (auto defaultValueAnnotation =
std::find_if(uniformInfo.annotations.begin(), uniformInfo.annotations.end(), [](const auto& a) { return a.name == "defaultValue"; });
defaultValueAnnotation != uniformInfo.annotations.end())
{
reshadefx::constant value = defaultValueAnnotation->value;
if (type.is_floating_point()) {
defaultValue = std::vector<float>(value.as_float, value.as_float + type.components());
reshade_log.debugf("Found float* runtime uniform %s of size %d\n", name.c_str(), type.components());
} else if (type.is_boolean()) {
defaultValue = std::vector<uint32_t>(value.as_uint, value.as_uint + type.components());
reshade_log.debugf("Found bool* runtime uniform %s of size %d\n", name.c_str(), type.components());
} else if (type.is_numeric()) {
if (type.is_signed()) {
defaultValue = std::vector<int32_t>(value.as_int, value.as_int + type.components());
reshade_log.debugf("Found int32_t* runtime uniform %s of size %d\n", name.c_str(), type.components());
} else {
defaultValue = std::vector<uint32_t>(value.as_uint, value.as_uint + type.components());
reshade_log.debugf("Found uint32_t* runtime uniform %s of size %d\n", name.c_str(), type.components());
}
} else {
reshade_log.errorf("Tried to create a runtime uniform variable of an unsupported type\n");
}
}
}
void RuntimeUniform::update(void* mappedBuffer)
{
std::variant<std::monostate, std::vector<float>, std::vector<int32_t>, std::vector<uint32_t>> value;
uint8_t* wl_value = nullptr;

std::lock_guard<std::mutex> lock(g_runtimeUniformsMutex);
auto it = g_runtimeUniforms.find(name);
if (it != g_runtimeUniforms.end()) {
wl_value = it->second;
}

if (wl_value) {
if (type.is_floating_point()) {
value = std::vector<float>();
float *float_value = nullptr;
uint8_array_for_each(float_value, wl_value, type.components() * sizeof(float)) {
std::get<std::vector<float>>(value).push_back(*float_value);
}
} else if (type.is_boolean()) {
// convert to a uint32_t vector, that's how the reshade uniform code understands booleans
value = std::vector<uint32_t>();
uint8_t *bool_value = nullptr;
uint8_array_for_each(bool_value, wl_value, type.components() * sizeof(uint8_t)) {
std::get<std::vector<uint32_t>>(value).push_back(*bool_value);
}
} else if (type.is_numeric()) {
if (type.is_signed()) {
value = std::vector<int32_t>();
int32_t *int_value = nullptr;
uint8_array_for_each(int_value, wl_value, type.components() * sizeof(int32_t)) {
std::get<std::vector<int32_t>>(value).push_back(*int_value);
}
} else {
value = std::vector<uint32_t>();
uint32_t *uint_value = nullptr;
uint8_array_for_each(uint_value, wl_value, type.components() * sizeof(uint32_t)) {
std::get<std::vector<uint32_t>>(value).push_back(*uint_value);
}
}
}
}

if (std::holds_alternative<std::monostate>(value)) {
value = defaultValue;
}

if (std::holds_alternative<std::vector<float>>(value)) {
std::vector<float>& vec = std::get<std::vector<float>>(value);
std::memcpy((uint8_t*) mappedBuffer + offset, vec.data(), vec.size() * sizeof(float));
} else if (std::holds_alternative<std::vector<int32_t>>(value)) {
std::vector<int32_t>& vec = std::get<std::vector<int32_t>>(value);
std::memcpy((uint8_t*) mappedBuffer + offset, vec.data(), vec.size() * sizeof(int32_t));
} else if (std::holds_alternative<std::vector<uint32_t>>(value)) {
std::vector<uint32_t>& vec = std::get<std::vector<uint32_t>>(value);
std::memcpy((uint8_t*) mappedBuffer + offset, vec.data(), vec.size() * sizeof(uint32_t));
}
}
RuntimeUniform::~RuntimeUniform()
{
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////
DataUniform::DataUniform(reshadefx::uniform_info uniformInfo)
: ReshadeUniform(uniformInfo)
Expand Down Expand Up @@ -541,9 +664,9 @@ static std::vector<std::shared_ptr<ReshadeUniform>> createReshadeUniforms(const
{
uniforms.push_back(std::make_shared<DepthUniform>(uniform));
}
else
else if (!source.empty())
{
reshade_log.errorf("Unknown uniform source: %s", source.c_str());
uniforms.push_back(std::make_shared<RuntimeUniform>(uniform));
}
}
}
Expand Down Expand Up @@ -1534,6 +1657,11 @@ bool ReshadeEffectPipeline::init(CVulkanDevice *device, const ReshadeEffectKey &

void ReshadeEffectPipeline::update()
{
if (g_effectReadyCallback && g_reshadeEffectPath) {
g_effectReadyCallback(g_reshadeEffectPath);
g_effectReadyCallback = nullptr;
}

for (auto& uniform : m_uniforms)
uniform->update(m_mappedPtr);
}
Expand Down Expand Up @@ -1793,3 +1921,33 @@ ReshadeEffectPipeline* ReshadeEffectManager::pipeline(const ReshadeEffectKey &ke

ReshadeEffectManager g_reshadeManager;

void reshade_effect_manager_set_uniform_variable(const char *key, uint8_t* value)
{
std::lock_guard<std::mutex> lock(g_runtimeUniformsMutex);

auto it = g_runtimeUniforms.find(key);
if (it != g_runtimeUniforms.end()) {
delete[] it->second;
}

g_runtimeUniforms[std::string(key)] = value;
force_repaint();
}

void reshade_effect_manager_set_effect(const char *path, std::function<void(const char*)> callback)
{
g_runtimeUniforms.clear();
if (g_reshadeEffectPath) free(g_reshadeEffectPath);
g_reshadeEffectPath = strdup(path);
g_effectReadyCallback = callback;
}

void reshade_effect_manager_enable_effect()
{
if (g_reshadeEffectPath) gamescope_set_reshade_effect(g_reshadeEffectPath);
}

void reshade_effect_manager_disable_effect()
{
gamescope_set_reshade_effect(nullptr);
}
6 changes: 6 additions & 0 deletions src/reshade_effect_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,9 @@ class ReshadeEffectManager
};

extern ReshadeEffectManager g_reshadeManager;


void reshade_effect_manager_set_uniform_variable(const char *key, uint8_t* value);
void reshade_effect_manager_set_effect(const char *path, std::function<void(const char*)> callback);
void reshade_effect_manager_enable_effect();
void reshade_effect_manager_disable_effect();
Loading
Loading