2023-02-28 01:15:26 +00:00
|
|
|
// AUTOGENERATED COPYRIGHT HEADER START
|
|
|
|
// Copyright (C) 2019-2023 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
|
|
|
|
// Copyright (C) 2022 lainon <GermanAizek@yandex.ru>
|
|
|
|
// AUTOGENERATED COPYRIGHT HEADER END
|
2019-04-02 00:56:15 +00:00
|
|
|
|
|
|
|
#include "gfx-blur-gaussian.hpp"
|
2022-08-28 12:28:37 +00:00
|
|
|
#include "common.hpp"
|
2022-11-28 08:11:55 +00:00
|
|
|
#include "gfx/gfx-util.hpp"
|
2019-04-02 22:16:13 +00:00
|
|
|
#include "obs/gs/gs-helper.hpp"
|
2019-04-02 00:56:15 +00:00
|
|
|
#include "plugin.hpp"
|
|
|
|
|
2022-08-29 10:29:44 +00:00
|
|
|
#include "warning-disable.hpp"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include "warning-enable.hpp"
|
|
|
|
|
2021-06-08 08:35:34 +00:00
|
|
|
// TODO: It may be possible to optimize to run much faster: https://rastergrid.com/blog/2010/09/efficient-gaussian-blur-with-linear-sampling/
|
|
|
|
|
|
|
|
#define ST_KERNEL_SIZE 128u
|
|
|
|
#define ST_OVERSAMPLE_MULTIPLIER 2
|
|
|
|
#define ST_MAX_BLUR_SIZE ST_KERNEL_SIZE / ST_OVERSAMPLE_MULTIPLIER
|
2019-04-02 00:56:15 +00:00
|
|
|
|
2022-11-28 08:11:55 +00:00
|
|
|
streamfx::gfx::blur::gaussian_data::gaussian_data() : _gfx_util(::streamfx::gfx::util::get())
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2021-06-08 08:35:34 +00:00
|
|
|
using namespace streamfx::util;
|
|
|
|
|
2022-07-21 11:09:10 +00:00
|
|
|
std::array<double, ST_KERNEL_SIZE> kernel_dbl;
|
|
|
|
std::vector<float> kernel(ST_KERNEL_SIZE);
|
2021-06-08 08:35:34 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
auto gctx = streamfx::obs::gs::context();
|
2021-11-07 13:37:05 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
auto file = streamfx::data_file_path("effects/blur/gaussian.effect");
|
|
|
|
try {
|
|
|
|
_effect = streamfx::obs::gs::effect::create(file);
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
DLOG_ERROR("Error loading '%s': %s", file.generic_u8string().c_str(), ex.what());
|
|
|
|
}
|
|
|
|
}
|
2021-06-08 08:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//#define ST_USE_PASCAL_TRIANGLE
|
|
|
|
|
|
|
|
// Pre-calculate Kernel Information for all Kernel sizes
|
|
|
|
for (size_t size = 1; size <= ST_MAX_BLUR_SIZE; size++) {
|
|
|
|
#ifdef ST_USE_PASCAL_TRIANGLE
|
|
|
|
// The Pascal Triangle can be used to generate Gaussian Kernels, which is
|
|
|
|
// significantly faster than doing the same task with searching. It is also
|
|
|
|
// much more accurate at the same time, so it is a 2-in-1 solution.
|
|
|
|
|
|
|
|
// Generate the required row and sum.
|
|
|
|
size_t offset = size;
|
|
|
|
size_t row = size * 2;
|
|
|
|
auto triangle = math::pascal_triangle<double>(row);
|
|
|
|
double sum = pow(2, row);
|
|
|
|
|
|
|
|
// Convert all integers to floats.
|
|
|
|
double accum = 0.;
|
|
|
|
for (size_t idx = offset; idx < std::min<size_t>(triangle.size(), ST_KERNEL_SIZE); idx++) {
|
|
|
|
double v = static_cast<double>(triangle[idx]) / sum;
|
|
|
|
kernel_dbl[idx - offset] = v;
|
|
|
|
// Accumulator needed as we end up with float inaccuracies above a certain threshold.
|
|
|
|
accum += v * (idx > offset ? 2 : 1);
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 08:35:34 +00:00
|
|
|
// Rescale all values back into useful ranges.
|
|
|
|
accum = 1. / accum;
|
|
|
|
for (size_t idx = offset; idx < ST_KERNEL_SIZE; idx++) {
|
|
|
|
kernel[idx - offset] = kernel_dbl[idx - offset] * accum;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
size_t oversample = size * ST_OVERSAMPLE_MULTIPLIER;
|
|
|
|
|
|
|
|
// Generate initial weights and calculate a total from them.
|
|
|
|
double total = 0.;
|
|
|
|
for (size_t idx = 0; (idx < oversample) && (idx < ST_KERNEL_SIZE); idx++) {
|
|
|
|
kernel_dbl[idx] = math::gaussian<double>(static_cast<double>(idx), static_cast<double>(size));
|
|
|
|
total += kernel_dbl[idx] * (idx > 0 ? 2 : 1);
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 08:35:34 +00:00
|
|
|
// Scale the weights according to the total gathered, and convert to float.
|
|
|
|
for (size_t idx = 0; (idx < oversample) && (idx < ST_KERNEL_SIZE); idx++) {
|
|
|
|
kernel_dbl[idx] /= total;
|
|
|
|
kernel[idx] = static_cast<float>(kernel_dbl[idx]);
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 08:35:34 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Store Kernel
|
|
|
|
_kernels.insert_or_assign(size, kernel);
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
streamfx::gfx::blur::gaussian_data::~gaussian_data()
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2021-06-08 02:38:24 +00:00
|
|
|
auto gctx = streamfx::obs::gs::context();
|
2019-08-04 14:20:26 +00:00
|
|
|
_effect.reset();
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
streamfx::obs::gs::effect streamfx::gfx::blur::gaussian_data::get_effect()
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
return _effect;
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 08:11:55 +00:00
|
|
|
std::shared_ptr<streamfx::gfx::util> streamfx::gfx::blur::gaussian_data::get_gfx_util()
|
|
|
|
{
|
|
|
|
return _gfx_util;
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
std::vector<float_t> const& streamfx::gfx::blur::gaussian_data::get_kernel(std::size_t width)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2021-06-08 08:35:34 +00:00
|
|
|
width = std::clamp<size_t>(width, 1, ST_MAX_BLUR_SIZE);
|
|
|
|
return _kernels.at(width);
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
streamfx::gfx::blur::gaussian_factory::gaussian_factory() {}
|
2019-04-02 00:56:15 +00:00
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
streamfx::gfx::blur::gaussian_factory::~gaussian_factory() {}
|
2019-04-02 00:56:15 +00:00
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
bool streamfx::gfx::blur::gaussian_factory::is_type_supported(::streamfx::gfx::blur::type v)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
|
|
|
switch (v) {
|
2021-06-08 02:47:04 +00:00
|
|
|
case ::streamfx::gfx::blur::type::Area:
|
2019-04-02 00:56:15 +00:00
|
|
|
return true;
|
2021-06-08 02:47:04 +00:00
|
|
|
case ::streamfx::gfx::blur::type::Directional:
|
2019-04-02 00:56:15 +00:00
|
|
|
return true;
|
2021-06-08 02:47:04 +00:00
|
|
|
case ::streamfx::gfx::blur::type::Rotational:
|
2019-04-02 00:56:15 +00:00
|
|
|
return true;
|
2021-06-08 02:47:04 +00:00
|
|
|
case ::streamfx::gfx::blur::type::Zoom:
|
2019-04-02 00:56:15 +00:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-13 12:35:46 +00:00
|
|
|
std::shared_ptr<::streamfx::gfx::blur::base> streamfx::gfx::blur::gaussian_factory::create(::streamfx::gfx::blur::type v)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
|
|
|
switch (v) {
|
2021-06-08 02:47:04 +00:00
|
|
|
case ::streamfx::gfx::blur::type::Area:
|
|
|
|
return std::make_shared<::streamfx::gfx::blur::gaussian>();
|
|
|
|
case ::streamfx::gfx::blur::type::Directional:
|
2023-05-13 12:35:46 +00:00
|
|
|
return std::static_pointer_cast<::streamfx::gfx::blur::gaussian>(std::make_shared<::streamfx::gfx::blur::gaussian_directional>());
|
2021-06-08 02:47:04 +00:00
|
|
|
case ::streamfx::gfx::blur::type::Rotational:
|
|
|
|
return std::make_shared<::streamfx::gfx::blur::gaussian_rotational>();
|
|
|
|
case ::streamfx::gfx::blur::type::Zoom:
|
|
|
|
return std::make_shared<::streamfx::gfx::blur::gaussian_zoom>();
|
2019-04-02 00:56:15 +00:00
|
|
|
default:
|
|
|
|
throw std::runtime_error("Invalid type.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
double_t streamfx::gfx::blur::gaussian_factory::get_min_size(::streamfx::gfx::blur::type)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
|
|
|
return double_t(1.0);
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
double_t streamfx::gfx::blur::gaussian_factory::get_step_size(::streamfx::gfx::blur::type)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
|
|
|
return double_t(1.0);
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
double_t streamfx::gfx::blur::gaussian_factory::get_max_size(::streamfx::gfx::blur::type)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2021-06-08 02:47:04 +00:00
|
|
|
return double_t(ST_MAX_BLUR_SIZE);
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
double_t streamfx::gfx::blur::gaussian_factory::get_min_angle(::streamfx::gfx::blur::type v)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
|
|
|
switch (v) {
|
2021-06-08 02:47:04 +00:00
|
|
|
case ::streamfx::gfx::blur::type::Directional:
|
|
|
|
case ::streamfx::gfx::blur::type::Rotational:
|
2019-04-02 00:56:15 +00:00
|
|
|
return -180.0;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
double_t streamfx::gfx::blur::gaussian_factory::get_step_angle(::streamfx::gfx::blur::type)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
|
|
|
return double_t(0.01);
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
double_t streamfx::gfx::blur::gaussian_factory::get_max_angle(::streamfx::gfx::blur::type v)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
|
|
|
switch (v) {
|
2021-06-08 02:47:04 +00:00
|
|
|
case ::streamfx::gfx::blur::type::Directional:
|
|
|
|
case ::streamfx::gfx::blur::type::Rotational:
|
2019-04-02 00:56:15 +00:00
|
|
|
return 180.0;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
bool streamfx::gfx::blur::gaussian_factory::is_step_scale_supported(::streamfx::gfx::blur::type v)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
|
|
|
switch (v) {
|
2021-06-08 02:47:04 +00:00
|
|
|
case ::streamfx::gfx::blur::type::Area:
|
|
|
|
case ::streamfx::gfx::blur::type::Zoom:
|
|
|
|
case ::streamfx::gfx::blur::type::Directional:
|
2019-04-02 00:56:15 +00:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
double_t streamfx::gfx::blur::gaussian_factory::get_min_step_scale_x(::streamfx::gfx::blur::type)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
|
|
|
return double_t(0.01);
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
double_t streamfx::gfx::blur::gaussian_factory::get_step_step_scale_x(::streamfx::gfx::blur::type)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
|
|
|
return double_t(0.01);
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
double_t streamfx::gfx::blur::gaussian_factory::get_max_step_scale_x(::streamfx::gfx::blur::type)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
|
|
|
return double_t(1000.0);
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
double_t streamfx::gfx::blur::gaussian_factory::get_min_step_scale_y(::streamfx::gfx::blur::type)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
|
|
|
return double_t(0.01);
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
double_t streamfx::gfx::blur::gaussian_factory::get_step_step_scale_y(::streamfx::gfx::blur::type)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
|
|
|
return double_t(0.01);
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
double_t streamfx::gfx::blur::gaussian_factory::get_max_step_scale_y(::streamfx::gfx::blur::type)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
|
|
|
return double_t(1000.0);
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
std::shared_ptr<::streamfx::gfx::blur::gaussian_data> streamfx::gfx::blur::gaussian_factory::data()
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2021-06-08 02:47:04 +00:00
|
|
|
std::unique_lock<std::mutex> ulock(_data_lock);
|
|
|
|
std::shared_ptr<::streamfx::gfx::blur::gaussian_data> data = _data.lock();
|
2019-04-02 00:56:15 +00:00
|
|
|
if (!data) {
|
2021-06-08 02:47:04 +00:00
|
|
|
data = std::make_shared<::streamfx::gfx::blur::gaussian_data>();
|
2019-08-04 14:20:26 +00:00
|
|
|
_data = data;
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
::streamfx::gfx::blur::gaussian_factory& streamfx::gfx::blur::gaussian_factory::get()
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2021-06-08 02:47:04 +00:00
|
|
|
static ::streamfx::gfx::blur::gaussian_factory instance;
|
2019-04-02 00:56:15 +00:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2023-05-13 12:35:46 +00:00
|
|
|
streamfx::gfx::blur::gaussian::gaussian() : _data(::streamfx::gfx::blur::gaussian_factory::get().data()), _size(1.), _step_scale({1., 1.})
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2021-06-08 02:38:24 +00:00
|
|
|
auto gctx = streamfx::obs::gs::context();
|
|
|
|
_rendertarget = std::make_shared<streamfx::obs::gs::rendertarget>(GS_RGBA, GS_ZS_NONE);
|
|
|
|
_rendertarget2 = std::make_shared<streamfx::obs::gs::rendertarget>(GS_RGBA, GS_ZS_NONE);
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
streamfx::gfx::blur::gaussian::~gaussian() {}
|
2019-04-02 00:56:15 +00:00
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
void streamfx::gfx::blur::gaussian::set_input(std::shared_ptr<::streamfx::obs::gs::texture> texture)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2022-07-21 11:09:10 +00:00
|
|
|
_input_texture = std::move(texture);
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
::streamfx::gfx::blur::type streamfx::gfx::blur::gaussian::get_type()
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2021-06-08 02:47:04 +00:00
|
|
|
return ::streamfx::gfx::blur::type::Area;
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
double_t streamfx::gfx::blur::gaussian::get_size()
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
return _size;
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
void streamfx::gfx::blur::gaussian::set_size(double_t width)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
|
|
|
if (width < 1.)
|
|
|
|
width = 1.;
|
2021-06-08 02:47:04 +00:00
|
|
|
if (width > ST_MAX_BLUR_SIZE)
|
|
|
|
width = ST_MAX_BLUR_SIZE;
|
2019-08-04 14:20:26 +00:00
|
|
|
_size = width;
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
void streamfx::gfx::blur::gaussian::set_step_scale(double_t x, double_t y)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
_step_scale.first = x;
|
|
|
|
_step_scale.second = y;
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
void streamfx::gfx::blur::gaussian::get_step_scale(double_t& x, double_t& y)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
x = _step_scale.first;
|
|
|
|
y = _step_scale.second;
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
double_t streamfx::gfx::blur::gaussian::get_step_scale_x()
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
return _step_scale.first;
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
double_t streamfx::gfx::blur::gaussian::get_step_scale_y()
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
return _step_scale.second;
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
std::shared_ptr<::streamfx::obs::gs::texture> streamfx::gfx::blur::gaussian::render()
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2021-06-08 02:38:24 +00:00
|
|
|
auto gctx = streamfx::obs::gs::context();
|
2020-04-25 23:04:04 +00:00
|
|
|
|
2023-04-05 16:24:26 +00:00
|
|
|
#if defined(ENABLE_PROFILING) && !defined(D_PLATFORM_MAC) && _DEBUG
|
2021-06-08 02:38:24 +00:00
|
|
|
auto gdmp = streamfx::obs::gs::debug_marker(streamfx::obs::gs::debug_color_azure_radiance, "Gaussian Blur");
|
2020-04-25 23:04:04 +00:00
|
|
|
#endif
|
2019-04-02 22:16:13 +00:00
|
|
|
|
2021-06-08 02:38:24 +00:00
|
|
|
streamfx::obs::gs::effect effect = _data->get_effect();
|
2019-04-02 00:56:15 +00:00
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
if (!effect || ((_step_scale.first + _step_scale.second) < std::numeric_limits<double_t>::epsilon())) {
|
|
|
|
return _input_texture;
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 08:35:34 +00:00
|
|
|
auto kernel = _data->get_kernel(size_t(_size));
|
2019-08-04 14:20:26 +00:00
|
|
|
float_t width = float_t(_input_texture->get_width());
|
|
|
|
float_t height = float_t(_input_texture->get_height());
|
2019-04-02 00:56:15 +00:00
|
|
|
|
|
|
|
// Setup
|
|
|
|
gs_set_cull_mode(GS_NEITHER);
|
|
|
|
gs_enable_color(true, true, true, true);
|
|
|
|
gs_enable_depth_test(false);
|
|
|
|
gs_depth_function(GS_ALWAYS);
|
|
|
|
gs_blend_state_push();
|
|
|
|
gs_reset_blend_state();
|
|
|
|
gs_enable_blending(false);
|
|
|
|
gs_blend_function(GS_BLEND_ONE, GS_BLEND_ZERO);
|
|
|
|
gs_enable_stencil_test(false);
|
|
|
|
gs_enable_stencil_write(false);
|
|
|
|
gs_stencil_function(GS_STENCIL_BOTH, GS_ALWAYS);
|
|
|
|
gs_stencil_op(GS_STENCIL_BOTH, GS_ZERO, GS_ZERO, GS_ZERO);
|
|
|
|
|
2019-12-15 10:39:57 +00:00
|
|
|
effect.get_parameter("pStepScale").set_float2(float_t(_step_scale.first), float_t(_step_scale.second));
|
2021-06-08 08:35:34 +00:00
|
|
|
effect.get_parameter("pSize").set_float(float_t(_size * ST_OVERSAMPLE_MULTIPLIER));
|
|
|
|
effect.get_parameter("pKernel").set_value(kernel.data(), ST_KERNEL_SIZE);
|
2019-04-02 00:56:15 +00:00
|
|
|
|
|
|
|
// First Pass
|
2019-08-04 14:20:26 +00:00
|
|
|
if (_step_scale.first > std::numeric_limits<double_t>::epsilon()) {
|
2021-06-08 08:35:34 +00:00
|
|
|
effect.get_parameter("pImage").set_texture(_input_texture);
|
2019-12-15 10:39:57 +00:00
|
|
|
effect.get_parameter("pImageTexel").set_float2(float_t(1.f / width), 0.f);
|
2019-04-02 00:56:15 +00:00
|
|
|
|
|
|
|
{
|
2023-04-05 16:24:26 +00:00
|
|
|
#if defined(ENABLE_PROFILING) && !defined(D_PLATFORM_MAC) && _DEBUG
|
2021-06-08 02:38:24 +00:00
|
|
|
auto gdm = streamfx::obs::gs::debug_marker(streamfx::obs::gs::debug_color_azure_radiance, "Horizontal");
|
2020-04-25 23:04:04 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
auto op = _rendertarget2->render(uint32_t(width), uint32_t(height));
|
2019-04-02 00:56:15 +00:00
|
|
|
gs_ortho(0, 1., 0, 1., 0, 1.);
|
2019-12-15 10:39:57 +00:00
|
|
|
while (gs_effect_loop(effect.get_object(), "Draw")) {
|
2022-11-28 08:11:55 +00:00
|
|
|
_data->get_gfx_util()->draw_fullscreen_triangle();
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
std::swap(_rendertarget, _rendertarget2);
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Second Pass
|
2019-08-04 14:20:26 +00:00
|
|
|
if (_step_scale.second > std::numeric_limits<double_t>::epsilon()) {
|
2021-06-08 08:35:34 +00:00
|
|
|
effect.get_parameter("pImage").set_texture(_rendertarget->get_texture());
|
2019-12-15 10:39:57 +00:00
|
|
|
effect.get_parameter("pImageTexel").set_float2(0.f, float_t(1.f / height));
|
2019-04-02 00:56:15 +00:00
|
|
|
|
|
|
|
{
|
2023-04-05 16:24:26 +00:00
|
|
|
#if defined(ENABLE_PROFILING) && !defined(D_PLATFORM_MAC) && _DEBUG
|
2021-06-08 02:38:24 +00:00
|
|
|
auto gdm = streamfx::obs::gs::debug_marker(streamfx::obs::gs::debug_color_azure_radiance, "Vertical");
|
2020-04-25 23:04:04 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
auto op = _rendertarget2->render(uint32_t(width), uint32_t(height));
|
2019-04-02 00:56:15 +00:00
|
|
|
gs_ortho(0, 1., 0, 1., 0, 1.);
|
2019-12-15 10:39:57 +00:00
|
|
|
while (gs_effect_loop(effect.get_object(), "Draw")) {
|
2022-11-28 08:11:55 +00:00
|
|
|
_data->get_gfx_util()->draw_fullscreen_triangle();
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
std::swap(_rendertarget, _rendertarget2);
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gs_blend_state_pop();
|
|
|
|
|
|
|
|
return this->get();
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
std::shared_ptr<::streamfx::obs::gs::texture> streamfx::gfx::blur::gaussian::get()
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
return _rendertarget->get_texture();
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
streamfx::gfx::blur::gaussian_directional::gaussian_directional() : m_angle(0.) {}
|
2019-04-02 00:56:15 +00:00
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
streamfx::gfx::blur::gaussian_directional::~gaussian_directional() {}
|
2019-04-02 00:56:15 +00:00
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
::streamfx::gfx::blur::type streamfx::gfx::blur::gaussian_directional::get_type()
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2021-06-08 02:47:04 +00:00
|
|
|
return ::streamfx::gfx::blur::type::Directional;
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
double_t streamfx::gfx::blur::gaussian_directional::get_angle()
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
return D_RAD_TO_DEG(m_angle);
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
void streamfx::gfx::blur::gaussian_directional::set_angle(double_t angle)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
m_angle = D_DEG_TO_RAD(angle);
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
std::shared_ptr<::streamfx::obs::gs::texture> streamfx::gfx::blur::gaussian_directional::render()
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2021-06-08 02:38:24 +00:00
|
|
|
auto gctx = streamfx::obs::gs::context();
|
2020-04-25 23:04:04 +00:00
|
|
|
|
2023-04-05 16:24:26 +00:00
|
|
|
#if defined(ENABLE_PROFILING) && !defined(D_PLATFORM_MAC) && _DEBUG
|
2023-05-13 12:35:46 +00:00
|
|
|
auto gdmp = streamfx::obs::gs::debug_marker(streamfx::obs::gs::debug_color_azure_radiance, "Gaussian Directional Blur");
|
2020-04-25 23:04:04 +00:00
|
|
|
#endif
|
2019-04-19 07:42:15 +00:00
|
|
|
|
2021-06-08 02:38:24 +00:00
|
|
|
streamfx::obs::gs::effect effect = _data->get_effect();
|
2019-04-02 00:56:15 +00:00
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
if (!effect || ((_step_scale.first + _step_scale.second) < std::numeric_limits<double_t>::epsilon())) {
|
|
|
|
return _input_texture;
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 08:35:34 +00:00
|
|
|
auto kernel = _data->get_kernel(size_t(_size));
|
2019-08-04 14:20:26 +00:00
|
|
|
float_t width = float_t(_input_texture->get_width());
|
|
|
|
float_t height = float_t(_input_texture->get_height());
|
2019-04-02 00:56:15 +00:00
|
|
|
|
|
|
|
// Setup
|
|
|
|
gs_set_cull_mode(GS_NEITHER);
|
|
|
|
gs_enable_color(true, true, true, true);
|
|
|
|
gs_enable_depth_test(false);
|
|
|
|
gs_depth_function(GS_ALWAYS);
|
|
|
|
gs_blend_state_push();
|
|
|
|
gs_reset_blend_state();
|
|
|
|
gs_enable_blending(false);
|
|
|
|
gs_blend_function(GS_BLEND_ONE, GS_BLEND_ZERO);
|
|
|
|
gs_enable_stencil_test(false);
|
|
|
|
gs_enable_stencil_write(false);
|
|
|
|
gs_stencil_function(GS_STENCIL_BOTH, GS_ALWAYS);
|
|
|
|
gs_stencil_op(GS_STENCIL_BOTH, GS_ZERO, GS_ZERO, GS_ZERO);
|
|
|
|
|
2019-12-15 10:39:57 +00:00
|
|
|
effect.get_parameter("pImage").set_texture(_input_texture);
|
2023-05-13 12:35:46 +00:00
|
|
|
effect.get_parameter("pImageTexel").set_float2(float_t(1.f / width * cos(m_angle)), float_t(1.f / height * sin(m_angle)));
|
2019-12-15 10:39:57 +00:00
|
|
|
effect.get_parameter("pStepScale").set_float2(float_t(_step_scale.first), float_t(_step_scale.second));
|
2021-06-08 08:35:34 +00:00
|
|
|
effect.get_parameter("pSize").set_float(float_t(_size * ST_OVERSAMPLE_MULTIPLIER));
|
|
|
|
effect.get_parameter("pKernel").set_value(kernel.data(), ST_KERNEL_SIZE);
|
2019-04-02 00:56:15 +00:00
|
|
|
|
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
auto op = _rendertarget->render(uint32_t(width), uint32_t(height));
|
2019-04-02 00:56:15 +00:00
|
|
|
gs_ortho(0, 1., 0, 1., 0, 1.);
|
2019-12-15 10:39:57 +00:00
|
|
|
while (gs_effect_loop(effect.get_object(), "Draw")) {
|
2022-11-28 08:11:55 +00:00
|
|
|
_data->get_gfx_util()->draw_fullscreen_triangle();
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gs_blend_state_pop();
|
|
|
|
|
|
|
|
return this->get();
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
::streamfx::gfx::blur::type streamfx::gfx::blur::gaussian_rotational::get_type()
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2021-06-08 02:47:04 +00:00
|
|
|
return ::streamfx::gfx::blur::type::Rotational;
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
std::shared_ptr<::streamfx::obs::gs::texture> streamfx::gfx::blur::gaussian_rotational::render()
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2021-06-08 02:38:24 +00:00
|
|
|
auto gctx = streamfx::obs::gs::context();
|
2020-04-25 23:04:04 +00:00
|
|
|
|
2023-04-05 16:24:26 +00:00
|
|
|
#if defined(ENABLE_PROFILING) && !defined(D_PLATFORM_MAC) && _DEBUG
|
2023-05-13 12:35:46 +00:00
|
|
|
auto gdmp = streamfx::obs::gs::debug_marker(streamfx::obs::gs::debug_color_azure_radiance, "Gaussian Rotational Blur");
|
2020-04-25 23:04:04 +00:00
|
|
|
#endif
|
2019-04-02 22:16:13 +00:00
|
|
|
|
2021-06-08 02:38:24 +00:00
|
|
|
streamfx::obs::gs::effect effect = _data->get_effect();
|
2019-04-02 00:56:15 +00:00
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
if (!effect || ((_step_scale.first + _step_scale.second) < std::numeric_limits<double_t>::epsilon())) {
|
|
|
|
return _input_texture;
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 08:35:34 +00:00
|
|
|
auto kernel = _data->get_kernel(size_t(_size));
|
2019-08-04 14:20:26 +00:00
|
|
|
float_t width = float_t(_input_texture->get_width());
|
|
|
|
float_t height = float_t(_input_texture->get_height());
|
2019-04-02 00:56:15 +00:00
|
|
|
|
|
|
|
// Setup
|
|
|
|
gs_set_cull_mode(GS_NEITHER);
|
|
|
|
gs_enable_color(true, true, true, true);
|
|
|
|
gs_enable_depth_test(false);
|
|
|
|
gs_depth_function(GS_ALWAYS);
|
|
|
|
gs_blend_state_push();
|
|
|
|
gs_reset_blend_state();
|
|
|
|
gs_enable_blending(false);
|
|
|
|
gs_blend_function(GS_BLEND_ONE, GS_BLEND_ZERO);
|
|
|
|
gs_enable_stencil_test(false);
|
|
|
|
gs_enable_stencil_write(false);
|
|
|
|
gs_stencil_function(GS_STENCIL_BOTH, GS_ALWAYS);
|
|
|
|
gs_stencil_op(GS_STENCIL_BOTH, GS_ZERO, GS_ZERO, GS_ZERO);
|
|
|
|
|
2019-12-15 10:39:57 +00:00
|
|
|
effect.get_parameter("pImage").set_texture(_input_texture);
|
|
|
|
effect.get_parameter("pImageTexel").set_float2(float_t(1.f / width), float_t(1.f / height));
|
|
|
|
effect.get_parameter("pStepScale").set_float2(float_t(_step_scale.first), float_t(_step_scale.second));
|
2021-06-08 08:35:34 +00:00
|
|
|
effect.get_parameter("pSize").set_float(float_t(_size * ST_OVERSAMPLE_MULTIPLIER));
|
2019-12-15 10:39:57 +00:00
|
|
|
effect.get_parameter("pAngle").set_float(float_t(m_angle / _size));
|
|
|
|
effect.get_parameter("pCenter").set_float2(float_t(m_center.first), float_t(m_center.second));
|
2021-06-08 08:35:34 +00:00
|
|
|
effect.get_parameter("pKernel").set_value(kernel.data(), ST_KERNEL_SIZE);
|
2019-04-02 00:56:15 +00:00
|
|
|
|
|
|
|
// First Pass
|
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
auto op = _rendertarget->render(uint32_t(width), uint32_t(height));
|
2019-04-02 00:56:15 +00:00
|
|
|
gs_ortho(0, 1., 0, 1., 0, 1.);
|
2019-12-15 10:39:57 +00:00
|
|
|
while (gs_effect_loop(effect.get_object(), "Rotate")) {
|
2022-11-28 08:11:55 +00:00
|
|
|
_data->get_gfx_util()->draw_fullscreen_triangle();
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gs_blend_state_pop();
|
|
|
|
|
|
|
|
return this->get();
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
void streamfx::gfx::blur::gaussian_rotational::set_center(double_t x, double_t y)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
|
|
|
m_center.first = x;
|
|
|
|
m_center.second = y;
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
void streamfx::gfx::blur::gaussian_rotational::get_center(double_t& x, double_t& y)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
|
|
|
x = m_center.first;
|
|
|
|
y = m_center.second;
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
double_t streamfx::gfx::blur::gaussian_rotational::get_angle()
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
return double_t(D_RAD_TO_DEG(m_angle));
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
void streamfx::gfx::blur::gaussian_rotational::set_angle(double_t angle)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
m_angle = D_DEG_TO_RAD(angle);
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
::streamfx::gfx::blur::type streamfx::gfx::blur::gaussian_zoom::get_type()
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2021-06-08 02:47:04 +00:00
|
|
|
return ::streamfx::gfx::blur::type::Zoom;
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
std::shared_ptr<::streamfx::obs::gs::texture> streamfx::gfx::blur::gaussian_zoom::render()
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
2021-06-08 02:38:24 +00:00
|
|
|
auto gctx = streamfx::obs::gs::context();
|
2020-04-25 23:04:04 +00:00
|
|
|
|
2023-04-05 16:24:26 +00:00
|
|
|
#if defined(ENABLE_PROFILING) && !defined(D_PLATFORM_MAC) && _DEBUG
|
2021-06-08 02:38:24 +00:00
|
|
|
auto gdmp = streamfx::obs::gs::debug_marker(streamfx::obs::gs::debug_color_azure_radiance, "Gaussian Zoom Blur");
|
2020-04-25 23:04:04 +00:00
|
|
|
#endif
|
2019-04-02 22:16:13 +00:00
|
|
|
|
2021-06-08 02:38:24 +00:00
|
|
|
streamfx::obs::gs::effect effect = _data->get_effect();
|
|
|
|
auto kernel = _data->get_kernel(size_t(_size));
|
2019-04-02 00:56:15 +00:00
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
if (!effect || ((_step_scale.first + _step_scale.second) < std::numeric_limits<double_t>::epsilon())) {
|
|
|
|
return _input_texture;
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
float_t width = float_t(_input_texture->get_width());
|
|
|
|
float_t height = float_t(_input_texture->get_height());
|
2019-04-02 00:56:15 +00:00
|
|
|
|
|
|
|
// Setup
|
|
|
|
gs_set_cull_mode(GS_NEITHER);
|
|
|
|
gs_enable_color(true, true, true, true);
|
|
|
|
gs_enable_depth_test(false);
|
|
|
|
gs_depth_function(GS_ALWAYS);
|
|
|
|
gs_blend_state_push();
|
|
|
|
gs_reset_blend_state();
|
|
|
|
gs_enable_blending(false);
|
|
|
|
gs_blend_function(GS_BLEND_ONE, GS_BLEND_ZERO);
|
|
|
|
gs_enable_stencil_test(false);
|
|
|
|
gs_enable_stencil_write(false);
|
|
|
|
gs_stencil_function(GS_STENCIL_BOTH, GS_ALWAYS);
|
|
|
|
gs_stencil_op(GS_STENCIL_BOTH, GS_ZERO, GS_ZERO, GS_ZERO);
|
|
|
|
|
2019-12-15 10:39:57 +00:00
|
|
|
effect.get_parameter("pImage").set_texture(_input_texture);
|
|
|
|
effect.get_parameter("pImageTexel").set_float2(float_t(1.f / width), float_t(1.f / height));
|
|
|
|
effect.get_parameter("pStepScale").set_float2(float_t(_step_scale.first), float_t(_step_scale.second));
|
|
|
|
effect.get_parameter("pSize").set_float(float_t(_size));
|
|
|
|
effect.get_parameter("pCenter").set_float2(float_t(m_center.first), float_t(m_center.second));
|
2021-06-08 08:35:34 +00:00
|
|
|
effect.get_parameter("pKernel").set_value(kernel.data(), ST_KERNEL_SIZE);
|
2019-04-02 00:56:15 +00:00
|
|
|
|
|
|
|
// First Pass
|
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
auto op = _rendertarget->render(uint32_t(width), uint32_t(height));
|
2019-04-02 00:56:15 +00:00
|
|
|
gs_ortho(0, 1., 0, 1., 0, 1.);
|
2019-12-15 10:39:57 +00:00
|
|
|
while (gs_effect_loop(effect.get_object(), "Zoom")) {
|
2022-11-28 08:11:55 +00:00
|
|
|
_data->get_gfx_util()->draw_fullscreen_triangle();
|
2019-04-02 00:56:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gs_blend_state_pop();
|
|
|
|
|
|
|
|
return this->get();
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
void streamfx::gfx::blur::gaussian_zoom::set_center(double_t x, double_t y)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
|
|
|
m_center.first = x;
|
|
|
|
m_center.second = y;
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:47:04 +00:00
|
|
|
void streamfx::gfx::blur::gaussian_zoom::get_center(double_t& x, double_t& y)
|
2019-04-02 00:56:15 +00:00
|
|
|
{
|
|
|
|
x = m_center.first;
|
|
|
|
y = m_center.second;
|
|
|
|
}
|