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 support for runtime uniform variables, using shared memory for cr… #226

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
117 changes: 117 additions & 0 deletions src/reshade_uniforms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <sys/shm.h>

#include <algorithm>
#include <variant>

#include "logger.hpp"

const std::string defaultRuntimePathnamePrefix = "/tmp/shader_runtime_";

namespace vkBasalt
{
void enumerateReshadeUniforms(reshadefx::module module)
Expand Down Expand Up @@ -76,6 +80,10 @@ namespace vkBasalt
{
uniforms.push_back(std::shared_ptr<ReshadeUniform>(new DepthUniform(uniform)));
}
else if (!source.empty())
{
uniforms.push_back(std::shared_ptr<ReshadeUniform>(new RuntimeUniform(uniform)));
}
}
return uniforms;
}
Expand Down Expand Up @@ -380,4 +388,113 @@ namespace vkBasalt
DepthUniform::~DepthUniform()
{
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////
RuntimeUniform::RuntimeUniform(reshadefx::uniform_info uniformInfo)
{
offset = uniformInfo.offset;
size = uniformInfo.size;
type = uniformInfo.type;
auto source = std::find_if(uniformInfo.annotations.begin(), uniformInfo.annotations.end(), [](const auto& a) { return a.name == "source"; });
if (auto pathnameAnnotation =
std::find_if(uniformInfo.annotations.begin(), uniformInfo.annotations.end(), [](const auto& a) { return a.name == "pathname"; });
pathnameAnnotation != uniformInfo.annotations.end())
{
pathname = new char[pathnameAnnotation->value.string_data.length() + 1];
strcpy(pathname, pathnameAnnotation->value.string_data.c_str());
}
else
{
pathname = new char[defaultRuntimePathnamePrefix.length() + source->value.string_data.length() + 1];
std::string tmp_pathname = defaultRuntimePathnamePrefix + source->value.string_data;
strcpy(pathname, tmp_pathname.c_str());
}

projId = 0;
if (auto projIdAnnotation =
std::find_if(uniformInfo.annotations.begin(), uniformInfo.annotations.end(), [](const auto& a) { return a.name == "projId"; });
projIdAnnotation != uniformInfo.annotations.end())
{
projId = projIdAnnotation->value.as_int[0];
}
shmKey = ftok(pathname, projId);

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());
Logger::debug(std::string("Found float* runtime uniform: ") + pathname + " " + std::to_string(projId) +
" of size " + std::to_string(type.components()) + "\n");
} else if (type.is_boolean()) {
defaultValue = std::vector<uint32_t>(value.as_uint, value.as_uint + type.components());
Logger::debug(std::string("Found bool* runtime uniform: ") + pathname + " " + std::to_string(projId) +
" of size " + std::to_string(type.components()) + "\n");
} else if (type.is_numeric()) {
if (type.is_signed()) {
defaultValue = std::vector<int32_t>(value.as_int, value.as_int + type.components());
Logger::debug(std::string("Found int32_t* runtime uniform: ") + pathname + " " + std::to_string(projId) +
" of size " + std::to_string(type.components()) + "\n");
} else {
defaultValue = std::vector<uint32_t>(value.as_uint, value.as_uint + type.components());
Logger::debug(std::string("Found uint32_t* runtime uniform: ") + pathname + " " + std::to_string(projId) +
" of size " + std::to_string(type.components()) + "\n");
}
} else {
Logger::err("Tried to create a runtime uniform variable of an unsupported type");
}
}
}
void RuntimeUniform::update(void* mapedBuffer)
{
std::variant<std::monostate, std::vector<float>, std::vector<int32_t>, std::vector<uint32_t>, bool> value;
if (shmKey != -1) {
int shmId = shmget(shmKey,size,0444); // read-only
if (shmId != -1) {
if (type.is_floating_point()) {
float* raw_ptr = static_cast<float*>(shmat(shmId, nullptr, 0));
value = std::vector<float>(raw_ptr, raw_ptr + type.components());
shmdt(raw_ptr);
} else if (type.is_boolean()) {
bool* raw_ptr = static_cast<bool*>(shmat(shmId, nullptr, 0));

// convert to a uint32_t vector, that's how the reshade uniform code understands booleans
std::vector<uint32_t> bools_as_uint;
for(size_t i = 0; i < type.components(); ++i) {
bools_as_uint.push_back(static_cast<uint32_t>(raw_ptr[i]));
}
value = bools_as_uint;
shmdt(raw_ptr);
} else if (type.is_numeric()) {
if (type.is_signed()) {
int32_t* raw_ptr = static_cast<int32_t*>(shmat(shmId, nullptr, 0));
value = std::vector<int32_t>(raw_ptr, raw_ptr + type.components());
shmdt(raw_ptr);
} else {
uint32_t* raw_ptr = static_cast<uint32_t*>(shmat(shmId, nullptr, 0));
value = std::vector<uint32_t>(raw_ptr, raw_ptr + type.components());
shmdt(raw_ptr);
}
}
}
}
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*) mapedBuffer + 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*) mapedBuffer + 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*) mapedBuffer + offset, vec.data(), vec.size() * sizeof(uint32_t));
}
}
RuntimeUniform::~RuntimeUniform()
{
}
} // namespace vkBasalt
16 changes: 16 additions & 0 deletions src/reshade_uniforms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <vector>
#include <chrono>
#include <memory>
#include <variant>

#include "vulkan_include.hpp"

Expand Down Expand Up @@ -139,6 +140,21 @@ namespace vkBasalt
void virtual update(void* mapedBuffer) override;
virtual ~DepthUniform();
};

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

private:
reshadefx::type type;
std::variant<std::monostate, std::vector<float>, std::vector<int32_t>, std::vector<uint32_t>, bool> defaultValue;
char* pathname;
int projId;
key_t shmKey;
};
} // namespace vkBasalt

#endif // RESHADE_UNIFORMS_HPP_INCLUDED