filter-blur: Refactor to use new Blur code

This removes the ridiculous amount of hardcoded values and functions and moves everything to the more modular blur approach.

Fixes #45
Fixes #6
This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2019-04-02 03:05:05 +02:00 committed by Michael Fabian Dirks
parent 8a1579f702
commit 21eef998ec
5 changed files with 480 additions and 648 deletions

View File

@ -9,6 +9,17 @@ FileType.Sounds="Sounds"
FileType.Effect="Effect"
FileType.Effects="Effects"
# Blur
Blur.Type.Box="Box"
Blur.Type.BoxLinear="Box Linear"
Blur.Type.Gaussian="Gaussian"
Blur.Type.GaussianLinear="Gaussian Linear"
Blur.Type.DualFiltering="Dual Filtering"
Blur.Subtype.Area="Area"
Blur.Subtype.Directional="Directional"
Blur.Subtype.Rotational="Rotational"
Blur.Subtype.Zoom="Zoom"
# Mip Generator
MipGenerator="Mipmap Generator"
MipGenerator.Description="Which mip generator should be used?"
@ -38,40 +49,26 @@ CustomShader.Texture.Type.Source="Source"
# Filter - Blur
Filter.Blur="Blur"
Filter.Blur.Type="Type"
Filter.Blur.Type.Description="The type of blur to apply:\n- 'Box' smoothes pixels equally.\n- 'Box Linear' uses linear sampling to reduce the GPU usage, sacrificing minimal quality.\n- 'Gaussian' applies a gaussian curve to the smoothed pixels.\n- 'Gaussian Linear' again uses linear sampling to reduce the total samples, sacrificing more quality this time.\n- 'Bilateral' is an edge detection variant of 'Gaussian'."
Filter.Blur.Type.Description="The type of blur to apply."
Filter.Blur.Type.Box="Box"
Filter.Blur.Type.BoxLinear="Box Linear"
Filter.Blur.Type.Gaussian="Gaussian"
Filter.Blur.Type.GaussianLinear="Gaussian Linear"
Filter.Blur.Type.Bilateral="Bilateral"
Filter.Blur.Size="Size (Pixel)"
Filter.Blur.Size.Description="Area size of the blur, large sizes may cause:\n- Skipped frames\n- Frame loss or drops\n- Input lag\n- GPU overheating\n- or other issues."
Filter.Blur.Bilateral.Smoothing="Smoothing"
Filter.Blur.Bilateral.Sharpness="Sharpness"
Filter.Blur.Directional="Directional Blur"
Filter.Blur.Directional.Description="Change the Blur into a Directional Blur. May not work with all Blur Types."
Filter.Blur.Directional.Angle="Angle"
Filter.Blur.Directional.Angle.Description="The angle in degrees for the Directional Blur."
Filter.Blur.Subtype="Subtype"
Filter.Blur.Subtype.Description="The way this blur should be applied."
Filter.Blur.Size="Size"
Filter.Blur.Size.Description="Size of the blur filter to apply. Large sizes may have a negative effect on performance."
Filter.Blur.Angle="Angle (Degrees)"
Filter.Blur.Angle.Description="Angle of the Blur"
Filter.Blur.Center.X="Center (X) (Percent)"
Filter.Blur.Center.X.Description="The horizontal center of the blur effect, in percent."
Filter.Blur.Center.Y="Center (Y) (Percent)"
Filter.Blur.Center.Y.Description="The vertical center of the blur effect, in percent."
Filter.Blur.StepScale="Step Scaling"
Filter.Blur.StepScale.Description="Scale the texel step used in the Blur shader, which allows for smaller Blur sizes to cover more space, at the cost of some quality.\nCan be combined with Directional Blur to change the behavior drastically."
Filter.Blur.StepScale.X="Step Scale X"
Filter.Blur.StepScale.Y="Step Scale Y"
Filter.Blur.Region="Apply to Region only"
Filter.Blur.Region.Description="Only apply the blur to a region inside the source."
Filter.Blur.Region.Left="Left Edge"
Filter.Blur.Region.Left.Description="Distance to left edge of the source in percent."
Filter.Blur.Region.Top="Top Edge"
Filter.Blur.Region.Top.Description="Distance to top edge of the source in percent."
Filter.Blur.Region.Right="Right Edge"
Filter.Blur.Region.Right.Description="Distance to right edge of the source in percent."
Filter.Blur.Region.Bottom="Bottom Edge"
Filter.Blur.Region.Bottom.Description="Distance to bottom edge of the source in percent."
Filter.Blur.Region.Feather="Feather Area"
Filter.Blur.Region.Feather.Description="Size of the smoothing area in percent, or 0 to turn off feather."
Filter.Blur.Region.Feather.Shift="Feather Shift"
Filter.Blur.Region.Feather.Shift.Description="Shift of the Feather area, positive is inwards, negative is outwards."
Filter.Blur.Region.Invert="Invert Region"
Filter.Blur.Region.Invert.Description="Invert the region so that everything but this area is blurred."
Filter.Blur.Mask="Apply a Mask"
Filter.Blur.Mask.Description="Apply a mask to the area that needs to be blurred, which allows for more control over the blurred area."
Filter.Blur.Mask.Type="Mask Type"
@ -103,7 +100,6 @@ Filter.Blur.Mask.Alpha="Mask Alpha Filter"
Filter.Blur.Mask.Alpha.Description="Filter the mask by this alpha value before applying it."
Filter.Blur.Mask.Multiplier="Mask Multiplier"
Filter.Blur.Mask.Multiplier.Description="Multiply the final mask value by this value."
Filter.Blur.ColorFormat="Color Format"
# Filter - Custom Shader
Filter.CustomShader="Custom Shader"

File diff suppressed because it is too large Load Diff

View File

@ -23,6 +23,7 @@
#include <list>
#include <map>
#include <memory>
#include "gfx/blur/gfx-blur-base.hpp"
#include "gfx/gfx-source-texture.hpp"
#include "obs/gs/gs-effect.hpp"
#include "obs/gs/gs-helper.hpp"
@ -44,14 +45,6 @@ namespace filter {
namespace blur {
class blur_instance;
enum type : int64_t {
Box,
Gaussian,
Bilateral,
BoxLinear,
GaussianLinear,
};
enum mask_type : int64_t {
Region,
Image,
@ -65,13 +58,9 @@ namespace filter {
std::list<blur_instance*> sources;
std::shared_ptr<gs::effect> color_converter_effect;
std::shared_ptr<gs::effect> mask_effect;
std::shared_ptr<gs::effect> blur_effect;
std::map<std::string, std::string> translation_map;
std::vector<double_t> gaussian_widths;
std::map<uint8_t, std::shared_ptr<std::vector<float_t>>> gaussian_kernels;
public: // Singleton
static void initialize();
static void finalize();
@ -84,9 +73,6 @@ namespace filter {
void on_list_fill();
void on_list_empty();
void generate_gaussian_kernels();
void generate_kernel_textures();
std::string const& get_translation(std::string const key);
static void* create(obs_data_t* settings, obs_source_t* self);
@ -108,15 +94,9 @@ namespace filter {
static void video_render(void* source, gs_effect_t* effect);
public:
std::shared_ptr<gs::effect> get_effect(filter::blur::type type);
std::string get_technique(filter::blur::type type);
std::shared_ptr<gs::effect> get_color_converter_effect();
std::shared_ptr<gs::effect> get_mask_effect();
std::shared_ptr<std::vector<float_t>> get_gaussian_kernel(uint8_t size);
};
class blur_instance {
@ -128,25 +108,17 @@ namespace filter {
bool m_source_rendered;
// Rendering
std::shared_ptr<gs::rendertarget> m_output_rt1;
std::shared_ptr<gs::rendertarget> m_output_rt2;
std::shared_ptr<gs::texture> m_output_texture;
std::shared_ptr<gs::rendertarget> m_output_rt;
bool m_output_rendered;
// Blur
std::shared_ptr<gs::effect> m_blur_effect;
std::string m_blur_technique;
filter::blur::type m_blur_type;
uint64_t m_blur_size;
/// Bilateral Blur
double_t m_blur_bilateral_smoothing;
double_t m_blur_bilateral_sharpness;
/// Directional Blur
bool m_blur_directional;
double_t m_blur_angle;
/// Step Scaling
bool m_blur_step_scaling;
std::pair<double_t, double_t> m_blur_step_scale;
std::shared_ptr<::gfx::blur::ibase> m_blur;
double_t m_blur_size;
double_t m_blur_angle;
std::pair<double_t, double_t> m_blur_center;
bool m_blur_step_scaling;
std::pair<double_t, double_t> m_blur_step_scale;
// Masking
struct {
@ -182,23 +154,19 @@ namespace filter {
float_t multiplier;
} m_mask;
// advanced
uint64_t m_color_format;
public:
blur_instance(obs_data_t* settings, obs_source_t* self);
~blur_instance();
private:
bool apply_shared_param(gs_texture_t* input, float texelX, float texelY);
bool apply_bilateral_param();
bool apply_gaussian_param(uint8_t width);
bool apply_mask_parameters(std::shared_ptr<gs::effect> effect, gs_texture_t* original_texture,
gs_texture_t* blurred_texture);
static bool modified_properties(void* ptr, obs_properties_t* props, obs_property* prop,
obs_data_t* settings);
void translate_old_settings(obs_data_t*);
public:
obs_properties_t* get_properties();
void update(obs_data_t*);

View File

@ -29,6 +29,8 @@
#define T_FILEFILTERS_EFFECT "*.effect *.txt"
#define T_FILEFILTERS_ANY "*.*"
#define S_VERSION "Version"
#define S_ADVANCED "Advanced"
#define S_FILETYPE_IMAGE "FileType.Image"
@ -40,6 +42,17 @@
#define S_FILETYPE_EFFECT "FileType.Effect"
#define S_FILETYPE_EFFECTS "FileType.Effects"
#define S_BLUR_TYPE_BOX "Blur.Type.Box"
#define S_BLUR_TYPE_BOX_LINEAR "Blur.Type.BoxLinear"
#define S_BLUR_TYPE_GAUSSIAN "Blur.Type.Gaussian"
#define S_BLUR_TYPE_GAUSSIAN_LINEAR "Blur.Type.GaussianLinear"
#define S_BLUR_TYPE_DUALFILTERING "Blur.Type.DualFiltering"
#define S_BLUR_SUBTYPE_AREA "Blur.Subtype.Area"
#define S_BLUR_SUBTYPE_DIRECTIONAL "Blur.Subtype.Directional"
#define S_BLUR_SUBTYPE_ROTATIONAL "Blur.Subtype.Rotational"
#define S_BLUR_SUBTYPE_ZOOM "Blur.Subtype.Zoom"
#define S_MIPGENERATOR "MipGenerator"
#define S_MIPGENERATOR_POINT "MipGenerator.Point"
#define S_MIPGENERATOR_LINEAR "MipGenerator.Linear"

View File

@ -36,25 +36,14 @@
#endif
// Constants
#define PI 3.1415926535897932384626433832795
#define PI2 6.283185307179586476925286766559
#define PI2_SQROOT 2.506628274631000502415765284811
#define PI 3.1415926535897932384626433832795 // PI = pi
#define PI2 6.283185307179586476925286766559 // 2PI = 2 * pi
#define PI2_SQROOT 2.506628274631000502415765284811 // sqrt(2 * pi)
inline double_t Gaussian1D(double_t x, double_t o)
{
double_t c = (x / o);
double_t b = exp(-0.5 * c * c);
double_t a = (1.0 / (o * PI2_SQROOT));
return a * b;
}
inline double_t Bilateral1D(double_t x, double_t o)
{
double_t c = (x / 0);
double_t d = c * c;
double_t b = exp(-0.5 * d) / o;
return 0.39894 * b; // Seems to be (1.0 / (1 * PI2_SQROOT)) * b, otherwise no difference from Gaussian Blur
}
#define V_RAD 57.295779513082320876798154814105 // 180/pi
#define V_DEG 0.01745329251994329576923690768489 // pi/180
#define DEG_TO_RAD(x) (x * V_DEG)
#define RAD_TO_DEG(x) (x * V_RAD)
inline size_t GetNearestPowerOfTwoAbove(size_t v)
{