filter-blur: Refactoring, one-time rendering, fix mask logic

Unfortunately load is never called on filters.
This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2019-01-28 00:00:45 +01:00
parent 97bf38633b
commit c529e45cfd
2 changed files with 340 additions and 329 deletions

View file

@ -547,8 +547,9 @@ bool filter::blur::blur_instance::can_log()
}
filter::blur::blur_instance::blur_instance(obs_data_t* settings, obs_source_t* parent)
: m_self(parent), m_source_rendered(false)
{
m_source = parent;
m_self = parent;
// Create RenderTargets
try {
@ -556,14 +557,12 @@ filter::blur::blur_instance::blur_instance(obs_data_t* settings, obs_source_t* p
this->rt_primary = std::make_shared<gs::rendertarget>(GS_RGBA, GS_ZS_NONE);
this->rt_secondary = std::make_shared<gs::rendertarget>(GS_RGBA, GS_ZS_NONE);
} catch (std::exception ex) {
P_LOG_ERROR("<filter-blur:%s> Failed to create rendertargets, error %s.", obs_source_get_name(m_source),
P_LOG_ERROR("<filter-blur:%s> Failed to create rendertargets, error %s.", obs_source_get_name(m_self),
ex.what());
}
// Get initial Blur effect.
blur_effect = filter::blur::blur_factory::get()->get_effect(filter::blur::type::Box);
update(settings);
}
filter::blur::blur_instance::~blur_instance()
@ -571,6 +570,9 @@ filter::blur::blur_instance::~blur_instance()
this->mask.source.source_texture.reset();
this->rt_primary.reset();
this->rt_secondary.reset();
this->rt_source.reset();
this->blur_effect.reset();
this->m_source_texture.reset();
}
obs_properties_t* filter::blur::blur_instance::get_properties()
@ -738,30 +740,6 @@ void filter::blur::blur_instance::update(obs_data_t* settings)
} else {
color_format = obs_data_get_default_int(settings, P_COLORFORMAT);
}
// Load Mask
if (mask.type == mask_type::Image) {
if (mask.image.path_old != mask.image.path) {
try {
mask.image.texture = std::make_shared<gs::texture>(mask.image.path);
mask.image.path_old = mask.image.path;
} catch (...) {
P_LOG_ERROR("<filter-blur> Instance '%s' failed to load image '%s'.", obs_source_get_name(m_source),
mask.image.path.c_str());
}
}
} else if (mask.type == mask_type::Source) {
if (mask.source.name_old != mask.source.name) {
try {
mask.source.source_texture = std::make_shared<gfx::source_texture>(mask.source.name, m_source);
mask.source.is_scene = (obs_scene_from_source(mask.source.source_texture->get_object()) != nullptr);
mask.source.name_old = mask.source.name;
} catch (...) {
P_LOG_ERROR("<filter-blur> Instance '%s' failed to grab source '%s'.", obs_source_get_name(m_source),
mask.source.name.c_str());
}
}
}
}
void filter::blur::blur_instance::load(obs_data_t* settings)
@ -783,12 +761,39 @@ void filter::blur::blur_instance::activate() {}
void filter::blur::blur_instance::deactivate() {}
void filter::blur::blur_instance::video_tick(float) {}
void filter::blur::blur_instance::video_tick(float)
{
// Load Mask
if (mask.type == mask_type::Image) {
if (mask.image.path_old != mask.image.path) {
try {
mask.image.texture = std::make_shared<gs::texture>(mask.image.path);
mask.image.path_old = mask.image.path;
} catch (...) {
P_LOG_ERROR("<filter-blur> Instance '%s' failed to load image '%s'.", obs_source_get_name(m_self),
mask.image.path.c_str());
}
}
} else if (mask.type == mask_type::Source) {
if (mask.source.name_old != mask.source.name) {
try {
mask.source.source_texture = std::make_shared<gfx::source_texture>(mask.source.name, m_self);
mask.source.is_scene = (obs_scene_from_source(mask.source.source_texture->get_object()) != nullptr);
mask.source.name_old = mask.source.name;
} catch (...) {
P_LOG_ERROR("<filter-blur> Instance '%s' failed to grab source '%s'.", obs_source_get_name(m_self),
mask.source.name.c_str());
}
}
}
m_source_rendered = false;
}
void filter::blur::blur_instance::video_render(gs_effect_t* effect)
{
obs_source_t* parent = obs_filter_get_parent(this->m_source);
obs_source_t* target = obs_filter_get_target(this->m_source);
obs_source_t* parent = obs_filter_get_parent(this->m_self);
obs_source_t* target = obs_filter_get_target(this->m_self);
uint32_t baseW = obs_source_get_base_width(target);
uint32_t baseH = obs_source_get_base_height(target);
vec4 black;
@ -799,31 +804,33 @@ void filter::blur::blur_instance::video_render(gs_effect_t* effect)
std::shared_ptr<gs::effect> colorConversionEffect = blur_factory::get()->get_color_converter_effect();
// Verify that we can actually run first.
if (!target || !parent || !this->m_source) {
if (!target || !parent || !this->m_self) {
if (this->can_log()) {
P_LOG_ERROR("<filter-blur:%s> Invalid context, memory corruption may have happened.",
obs_source_get_name(this->m_source));
obs_source_get_name(this->m_self));
}
obs_source_skip_video_filter(this->m_source);
obs_source_skip_video_filter(this->m_self);
return;
}
if ((baseW == 0) || (baseH == 0)) {
if (this->can_log()) {
P_LOG_ERROR("<filter-blur:%s> Invalid base size from '%s': %" PRIu32 "x%" PRIu32 ".",
obs_source_get_name(this->m_source), obs_source_get_name(target), baseW, baseH);
obs_source_get_name(this->m_self), obs_source_get_name(target), baseW, baseH);
}
obs_source_skip_video_filter(this->m_source);
obs_source_skip_video_filter(this->m_self);
return;
}
if (!this->rt_primary || !this->rt_secondary || !this->blur_effect) {
if (this->can_log()) {
P_LOG_ERROR("<filter-blur:%s> Missing RenderTarget or Effect.", obs_source_get_name(this->m_source));
P_LOG_ERROR("<filter-blur:%s> Missing RenderTarget or Effect.", obs_source_get_name(this->m_self));
}
obs_source_skip_video_filter(this->m_source);
obs_source_skip_video_filter(this->m_self);
return;
}
gs_effect_t* defaultEffect = obs_get_base_effect(obs_base_effect::OBS_EFFECT_DEFAULT);
if (!m_source_rendered) {
std::shared_ptr<gs::texture> tex_source;
std::shared_ptr<gs::texture> tex_intermediate;
@ -850,18 +857,18 @@ void filter::blur::blur_instance::video_render(gs_effect_t* effect)
gs_clear(GS_CLEAR_COLOR | GS_CLEAR_DEPTH, &black, 0, 0);
// Render
if (obs_source_process_filter_begin(this->m_source, GS_RGBA, OBS_NO_DIRECT_RENDERING)) {
obs_source_process_filter_end(this->m_source, defaultEffect, baseW, baseH);
if (obs_source_process_filter_begin(this->m_self, GS_RGBA, OBS_NO_DIRECT_RENDERING)) {
obs_source_process_filter_end(this->m_self, defaultEffect, baseW, baseH);
} else {
throw std::runtime_error("Failed to render source");
}
} catch (std::exception ex) {
if (this->can_log()) {
P_LOG_ERROR("<filter-blur:%s> Rendering parent source to texture failed with error: %s.",
obs_source_get_name(this->m_source), ex.what());
obs_source_get_name(this->m_self), ex.what());
}
gs_blend_state_pop();
obs_source_skip_video_filter(this->m_source);
obs_source_skip_video_filter(this->m_self);
return;
}
gs_blend_state_pop();
@ -870,7 +877,7 @@ void filter::blur::blur_instance::video_render(gs_effect_t* effect)
if (this->can_log()) {
P_LOG_ERROR("<filter-blur> Failed to get source texture.");
}
obs_source_skip_video_filter(m_source);
obs_source_skip_video_filter(m_self);
return;
}
}
@ -903,10 +910,10 @@ void filter::blur::blur_instance::video_render(gs_effect_t* effect)
} catch (std::exception ex) {
if (this->can_log()) {
P_LOG_ERROR("<filter-blur:%s> RGB-YUV conversion failed with error: %s.",
obs_source_get_name(this->m_source), ex.what());
obs_source_get_name(this->m_self), ex.what());
}
gs_blend_state_pop();
obs_source_skip_video_filter(m_source);
obs_source_skip_video_filter(m_self);
return;
}
gs_blend_state_pop();
@ -914,9 +921,9 @@ void filter::blur::blur_instance::video_render(gs_effect_t* effect)
if (!(tex_source = this->rt_primary->get_texture())) {
if (this->can_log()) {
P_LOG_ERROR("<filter-blur:%s> Failed to get color conversion texture.",
obs_source_get_name(this->m_source));
obs_source_get_name(this->m_self));
}
obs_source_skip_video_filter(m_source);
obs_source_skip_video_filter(m_self);
return;
}
@ -1015,11 +1022,11 @@ void filter::blur::blur_instance::video_render(gs_effect_t* effect)
}
} catch (std::exception ex) {
if (this->can_log()) {
P_LOG_ERROR("<filter-blur:%s> Blur failed with error: %s.", obs_source_get_name(this->m_source),
P_LOG_ERROR("<filter-blur:%s> Blur failed with error: %s.", obs_source_get_name(this->m_self),
ex.what());
}
gs_blend_state_pop();
obs_source_skip_video_filter(this->m_source);
obs_source_skip_video_filter(this->m_self);
return;
}
gs_blend_state_pop();
@ -1098,20 +1105,20 @@ void filter::blur::blur_instance::video_render(gs_effect_t* effect)
} catch (std::exception ex) {
if (this->can_log()) {
P_LOG_ERROR("<filter-blur:%s> Masking failed with error: %s.", obs_source_get_name(this->m_source),
P_LOG_ERROR("<filter-blur:%s> Masking failed with error: %s.", obs_source_get_name(this->m_self),
ex.what());
}
gs_blend_state_pop();
obs_source_skip_video_filter(this->m_source);
obs_source_skip_video_filter(this->m_self);
return;
}
gs_blend_state_pop();
if (!(tex_intermediate = this->rt_primary->get_texture())) {
if (this->can_log()) {
P_LOG_ERROR("<filter-blur:%s> Failed to get masked texture.", obs_source_get_name(this->m_source));
P_LOG_ERROR("<filter-blur:%s> Failed to get masked texture.", obs_source_get_name(this->m_self));
}
obs_source_skip_video_filter(this->m_source);
obs_source_skip_video_filter(this->m_self);
return;
}
@ -1119,6 +1126,9 @@ void filter::blur::blur_instance::video_render(gs_effect_t* effect)
std::swap(this->rt_primary, this->rt_secondary);
}
m_source_texture = tex_intermediate;
}
// Color Conversion RGB-YUV or Straight Draw
{
// It is important that we do not modify the blend state here, as it is set correctly by OBS
@ -1141,18 +1151,18 @@ void filter::blur::blur_instance::video_render(gs_effect_t* effect)
gs_eparam_t* param = gs_effect_get_param_by_name(finalEffect, "image");
if (!param) {
P_LOG_ERROR("<filter-blur:%s> Failed to set image param.", obs_source_get_name(this->m_source));
P_LOG_ERROR("<filter-blur:%s> Failed to set image param.", obs_source_get_name(this->m_self));
failed = true;
} else {
gs_effect_set_texture(param, tex_intermediate->get_object());
gs_effect_set_texture(param, m_source_texture->get_object());
}
while (gs_effect_loop(finalEffect, technique)) {
gs_draw_sprite(tex_intermediate->get_object(), 0, baseW, baseH);
gs_draw_sprite(m_source_texture->get_object(), 0, baseW, baseH);
}
}
if (failed) {
obs_source_skip_video_filter(m_source);
obs_source_skip_video_filter(m_self);
return;
}
}

View file

@ -118,20 +118,29 @@ namespace filter {
};
class blur_instance {
obs_source_t* m_source;
obs_source_t* m_self;
// Rendering
std::shared_ptr<gs::rendertarget> rt_source;
std::shared_ptr<gs::rendertarget> rt_primary;
std::shared_ptr<gs::rendertarget> rt_secondary;
std::shared_ptr<gs::texture> m_source_texture;
bool m_source_rendered;
// blur
// Blur
std::shared_ptr<gs::effect> blur_effect;
std::string blur_technique;
filter::blur::type type;
uint64_t size;
// bilateral
/// Bilateral Blur
double_t bilateral_smoothing;
double_t bilateral_sharpness;
/// Directional Blur
bool directional;
double_t angle;
/// Step Scaling
bool scaling;
std::pair<double_t, double_t> scale;
// Masking
struct {
@ -167,14 +176,6 @@ namespace filter {
float_t multiplier;
} mask;
// Directional
bool directional;
double_t angle;
// Scale
bool scaling;
std::pair<double_t, double_t> scale;
// advanced
uint64_t color_format;