mirror of
https://github.com/Xaymar/obs-StreamFX
synced 2024-11-10 22:05:06 +00:00
filter-displacement: Further refactoring
This commit is contained in:
parent
c1995c03a9
commit
fedb6e3259
2 changed files with 50 additions and 48 deletions
|
@ -141,18 +141,18 @@ void filter::DisplacementAddon::video_render(void* ptr, gs_effect_t* effect)
|
||||||
}
|
}
|
||||||
|
|
||||||
filter::Displacement::Displacement(obs_data_t* data, obs_source_t* context)
|
filter::Displacement::Displacement(obs_data_t* data, obs_source_t* context)
|
||||||
|
: m_self(context), m_active(true), m_effect(nullptr), m_distance(0), m_timer(0)
|
||||||
{
|
{
|
||||||
this->dispmap.texture = nullptr;
|
this->m_displacement_map.texture = nullptr;
|
||||||
this->dispmap.createTime = 0;
|
this->m_displacement_map.createTime = 0;
|
||||||
this->dispmap.modifiedTime = 0;
|
this->m_displacement_map.modifiedTime = 0;
|
||||||
this->dispmap.size = 0;
|
this->m_displacement_map.size = 0;
|
||||||
this->timer = 0;
|
|
||||||
this->context = context;
|
|
||||||
|
|
||||||
obs_enter_graphics();
|
obs_enter_graphics();
|
||||||
char* effectFile = obs_module_file("effects/displace.effect");
|
char* effectFile = obs_module_file("effects/displace.effect");
|
||||||
char* errorMessage = nullptr;
|
char* errorMessage = nullptr;
|
||||||
this->customEffect = gs_effect_create_from_file(effectFile, &errorMessage);
|
this->m_effect = gs_effect_create_from_file(effectFile, &errorMessage);
|
||||||
bfree(effectFile);
|
bfree(effectFile);
|
||||||
if (errorMessage != nullptr) {
|
if (errorMessage != nullptr) {
|
||||||
P_LOG_ERROR("%s", errorMessage);
|
P_LOG_ERROR("%s", errorMessage);
|
||||||
|
@ -166,8 +166,8 @@ filter::Displacement::Displacement(obs_data_t* data, obs_source_t* context)
|
||||||
filter::Displacement::~Displacement()
|
filter::Displacement::~Displacement()
|
||||||
{
|
{
|
||||||
obs_enter_graphics();
|
obs_enter_graphics();
|
||||||
gs_effect_destroy(customEffect);
|
gs_effect_destroy(m_effect);
|
||||||
gs_texture_destroy(dispmap.texture);
|
gs_texture_destroy(m_displacement_map.texture);
|
||||||
obs_leave_graphics();
|
obs_leave_graphics();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,8 +175,8 @@ void filter::Displacement::update(obs_data_t* data)
|
||||||
{
|
{
|
||||||
updateDisplacementMap(obs_data_get_string(data, S_FILTER_DISPLACEMENT_FILE));
|
updateDisplacementMap(obs_data_get_string(data, S_FILTER_DISPLACEMENT_FILE));
|
||||||
|
|
||||||
distance = float_t(obs_data_get_double(data, S_FILTER_DISPLACEMENT_RATIO));
|
m_distance = float_t(obs_data_get_double(data, S_FILTER_DISPLACEMENT_RATIO));
|
||||||
vec2_set(&displacementScale, float_t(obs_data_get_double(data, S_FILTER_DISPLACEMENT_SCALE)),
|
vec2_set(&m_displacement_scale, float_t(obs_data_get_double(data, S_FILTER_DISPLACEMENT_SCALE)),
|
||||||
float_t(obs_data_get_double(data, S_FILTER_DISPLACEMENT_SCALE)));
|
float_t(obs_data_get_double(data, S_FILTER_DISPLACEMENT_SCALE)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -200,10 +200,10 @@ void filter::Displacement::hide() {}
|
||||||
|
|
||||||
void filter::Displacement::video_tick(float time)
|
void filter::Displacement::video_tick(float time)
|
||||||
{
|
{
|
||||||
timer += time;
|
m_timer += time;
|
||||||
if (timer >= 1.0) {
|
if (m_timer >= 1.0) {
|
||||||
timer -= 1.0;
|
m_timer -= 1.0;
|
||||||
updateDisplacementMap(dispmap.file);
|
updateDisplacementMap(m_displacement_map.file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -214,47 +214,47 @@ float interp(float a, float b, float v)
|
||||||
|
|
||||||
void filter::Displacement::video_render(gs_effect_t*)
|
void filter::Displacement::video_render(gs_effect_t*)
|
||||||
{
|
{
|
||||||
obs_source_t* parent = obs_filter_get_parent(context);
|
obs_source_t* parent = obs_filter_get_parent(m_self);
|
||||||
obs_source_t* target = obs_filter_get_target(context);
|
obs_source_t* target = obs_filter_get_target(m_self);
|
||||||
uint32_t baseW = obs_source_get_base_width(target), baseH = obs_source_get_base_height(target);
|
uint32_t baseW = obs_source_get_base_width(target), baseH = obs_source_get_base_height(target);
|
||||||
|
|
||||||
// Skip rendering if our target, parent or context is not valid.
|
// Skip rendering if our target, parent or context is not valid.
|
||||||
if (!target || !parent || !context || !dispmap.texture || !baseW || !baseH) {
|
if (!target || !parent || !m_self || !m_displacement_map.texture || !baseW || !baseH) {
|
||||||
obs_source_skip_video_filter(context);
|
obs_source_skip_video_filter(m_self);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!obs_source_process_filter_begin(context, GS_RGBA, OBS_ALLOW_DIRECT_RENDERING))
|
if (!obs_source_process_filter_begin(m_self, GS_RGBA, OBS_ALLOW_DIRECT_RENDERING))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
gs_eparam_t* param;
|
gs_eparam_t* param;
|
||||||
|
|
||||||
vec2 texelScale;
|
vec2 texelScale;
|
||||||
vec2_set(&texelScale, interp((1.0f / baseW), 1.0f, distance), interp((1.0f / baseH), 1.0f, distance));
|
vec2_set(&texelScale, interp((1.0f / baseW), 1.0f, m_distance), interp((1.0f / baseH), 1.0f, m_distance));
|
||||||
param = gs_effect_get_param_by_name(customEffect, "texelScale");
|
param = gs_effect_get_param_by_name(m_effect, "texelScale");
|
||||||
if (param)
|
if (param)
|
||||||
gs_effect_set_vec2(param, &texelScale);
|
gs_effect_set_vec2(param, &texelScale);
|
||||||
else
|
else
|
||||||
P_LOG_ERROR("Failed to set texel scale param.");
|
P_LOG_ERROR("Failed to set texel scale param.");
|
||||||
|
|
||||||
param = gs_effect_get_param_by_name(customEffect, "displacementScale");
|
param = gs_effect_get_param_by_name(m_effect, "displacementScale");
|
||||||
if (param)
|
if (param)
|
||||||
gs_effect_set_vec2(param, &displacementScale);
|
gs_effect_set_vec2(param, &m_displacement_scale);
|
||||||
else
|
else
|
||||||
P_LOG_ERROR("Failed to set displacement scale param.");
|
P_LOG_ERROR("Failed to set displacement scale param.");
|
||||||
|
|
||||||
param = gs_effect_get_param_by_name(customEffect, "displacementMap");
|
param = gs_effect_get_param_by_name(m_effect, "displacementMap");
|
||||||
if (param)
|
if (param)
|
||||||
gs_effect_set_texture(param, dispmap.texture);
|
gs_effect_set_texture(param, m_displacement_map.texture);
|
||||||
else
|
else
|
||||||
P_LOG_ERROR("Failed to set texture param.");
|
P_LOG_ERROR("Failed to set texture param.");
|
||||||
|
|
||||||
obs_source_process_filter_end(context, customEffect, baseW, baseH);
|
obs_source_process_filter_end(m_self, m_effect, baseW, baseH);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string filter::Displacement::get_file()
|
std::string filter::Displacement::get_file()
|
||||||
{
|
{
|
||||||
return dispmap.file;
|
return m_displacement_map.file;
|
||||||
}
|
}
|
||||||
|
|
||||||
void filter::Displacement::updateDisplacementMap(std::string file)
|
void filter::Displacement::updateDisplacementMap(std::string file)
|
||||||
|
@ -262,27 +262,27 @@ void filter::Displacement::updateDisplacementMap(std::string file)
|
||||||
bool shouldUpdateTexture = false;
|
bool shouldUpdateTexture = false;
|
||||||
|
|
||||||
// Different File
|
// Different File
|
||||||
if (file != dispmap.file) {
|
if (file != m_displacement_map.file) {
|
||||||
dispmap.file = file;
|
m_displacement_map.file = file;
|
||||||
shouldUpdateTexture = true;
|
shouldUpdateTexture = true;
|
||||||
} else { // Different Timestamps
|
} else { // Different Timestamps
|
||||||
struct stat stats;
|
struct stat stats;
|
||||||
if (os_stat(dispmap.file.c_str(), &stats) != 0) {
|
if (os_stat(m_displacement_map.file.c_str(), &stats) != 0) {
|
||||||
shouldUpdateTexture = shouldUpdateTexture || (dispmap.createTime != stats.st_ctime)
|
shouldUpdateTexture = shouldUpdateTexture || (m_displacement_map.createTime != stats.st_ctime)
|
||||||
|| (dispmap.modifiedTime != stats.st_mtime);
|
|| (m_displacement_map.modifiedTime != stats.st_mtime);
|
||||||
dispmap.createTime = stats.st_ctime;
|
m_displacement_map.createTime = stats.st_ctime;
|
||||||
dispmap.modifiedTime = stats.st_mtime;
|
m_displacement_map.modifiedTime = stats.st_mtime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shouldUpdateTexture) {
|
if (shouldUpdateTexture) {
|
||||||
obs_enter_graphics();
|
obs_enter_graphics();
|
||||||
if (dispmap.texture) {
|
if (m_displacement_map.texture) {
|
||||||
gs_texture_destroy(dispmap.texture);
|
gs_texture_destroy(m_displacement_map.texture);
|
||||||
dispmap.texture = nullptr;
|
m_displacement_map.texture = nullptr;
|
||||||
}
|
}
|
||||||
if (os_file_exists(file.c_str()))
|
if (os_file_exists(file.c_str()))
|
||||||
dispmap.texture = gs_texture_create_from_file(dispmap.file.c_str());
|
m_displacement_map.texture = gs_texture_create_from_file(m_displacement_map.file.c_str());
|
||||||
obs_leave_graphics();
|
obs_leave_graphics();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,21 +64,23 @@ namespace filter {
|
||||||
};
|
};
|
||||||
|
|
||||||
class Displacement {
|
class Displacement {
|
||||||
void updateDisplacementMap(std::string file);
|
obs_source_t* m_self;
|
||||||
|
bool m_active;
|
||||||
|
|
||||||
obs_source_t* context;
|
// Rendering
|
||||||
gs_effect_t* customEffect;
|
gs_effect_t* m_effect;
|
||||||
float_t distance;
|
float_t m_distance;
|
||||||
vec2 displacementScale;
|
vec2 m_displacement_scale;
|
||||||
struct {
|
struct {
|
||||||
std::string file;
|
std::string file;
|
||||||
|
|
||||||
gs_texture_t* texture;
|
gs_texture_t* texture;
|
||||||
time_t createTime, modifiedTime;
|
time_t createTime, modifiedTime;
|
||||||
size_t size;
|
size_t size;
|
||||||
} dispmap;
|
} m_displacement_map;
|
||||||
|
float_t m_timer;
|
||||||
|
|
||||||
float_t timer;
|
void updateDisplacementMap(std::string file);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Displacement(obs_data_t*, obs_source_t*);
|
Displacement(obs_data_t*, obs_source_t*);
|
||||||
|
|
Loading…
Reference in a new issue