Skip to content

Commit

Permalink
Add support for runtime uniform variables, using shared memory for cr…
Browse files Browse the repository at this point in the history
…oss-process communication
  • Loading branch information
wheaney committed Sep 23, 2023
1 parent 4f97f09 commit 64b4161
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/reshade_uniforms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <sys/shm.h>

#include <algorithm>

#include "logger.hpp"

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

namespace vkBasalt
{
void enumerateReshadeUniforms(reshadefx::module module)
Expand Down Expand Up @@ -76,6 +79,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 +387,58 @@ namespace vkBasalt
DepthUniform::~DepthUniform()
{
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////
RuntimeUniform::RuntimeUniform(reshadefx::uniform_info uniformInfo)
{
auto source = std::find_if(uniformInfo.annotations.begin(), uniformInfo.annotations.end(), [](const auto& a) { return a.name == "source"; });
defaultValue = 0.0;
if (auto defaultValueAnnotation =
std::find_if(uniformInfo.annotations.begin(), uniformInfo.annotations.end(), [](const auto& a) { return a.name == "defaultValue"; });
defaultValueAnnotation != uniformInfo.annotations.end())
{
defaultValue = defaultValueAnnotation->type.is_floating_point() ? defaultValueAnnotation->value.as_float[0] : 0.0f;
}

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];
}

Logger::debug("Found runtime uniform: " + std::to_string(defaultValue) + " " + pathname + " " + std::to_string(projId) + "\n");
offset = uniformInfo.offset;
size = uniformInfo.size;
}
void RuntimeUniform::update(void* mapedBuffer)
{
key_t key = ftok(pathname, projId);
float *value = &defaultValue;
if (key != -1) {
int shmid = shmget(key,sizeof(*value),0444); // read-only
if (shmid != -1) {
value = (float*) shmat(shmid,(void*)0,0);
}
}
std::memcpy((uint8_t*) mapedBuffer + offset, value, sizeof(*value));
}
RuntimeUniform::~RuntimeUniform()
{
}
} // namespace vkBasalt
13 changes: 13 additions & 0 deletions src/reshade_uniforms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ 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:
float defaultValue;
char* pathname;
int projId;
};
} // namespace vkBasalt

#endif // RESHADE_UNIFORMS_HPP_INCLUDED

0 comments on commit 64b4161

Please sign in to comment.