2018-03-20 11:45:41 +00:00
|
|
|
// Modern effects for a modern Streamer
|
|
|
|
// Copyright (C) 2017 Michael Fabian Dirks
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
|
2019-12-18 05:37:47 +00:00
|
|
|
#include "gfx-shader.hpp"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2019-12-22 05:14:26 +00:00
|
|
|
#include "obs/obs-tools.hpp"
|
2019-12-18 05:37:47 +00:00
|
|
|
#include "plugin.hpp"
|
2019-01-14 10:23:21 +00:00
|
|
|
|
2019-12-18 05:37:47 +00:00
|
|
|
#define ST "Shader"
|
2020-04-03 16:40:05 +00:00
|
|
|
#define ST_REFRESH ST ".Refresh"
|
2019-12-18 05:37:47 +00:00
|
|
|
#define ST_SHADER ST ".Shader"
|
|
|
|
#define ST_SHADER_FILE ST_SHADER ".File"
|
|
|
|
#define ST_SHADER_TECHNIQUE ST_SHADER ".Technique"
|
|
|
|
#define ST_SHADER_SIZE ST_SHADER ".Size"
|
|
|
|
#define ST_SHADER_SIZE_WIDTH ST_SHADER_SIZE ".Width"
|
|
|
|
#define ST_SHADER_SIZE_HEIGHT ST_SHADER_SIZE ".Height"
|
2020-07-05 21:00:09 +00:00
|
|
|
#define ST_SHADER_SEED ST_SHADER ".Seed"
|
2019-12-18 05:37:47 +00:00
|
|
|
#define ST_PARAMETERS ST ".Parameters"
|
2018-03-20 11:45:41 +00:00
|
|
|
|
2019-12-18 05:37:47 +00:00
|
|
|
gfx::shader::shader::shader(obs_source_t* self, shader_mode mode)
|
2020-07-05 21:00:09 +00:00
|
|
|
: _self(self), _mode(mode), _base_width(1), _base_height(1), _active(true),
|
2019-12-21 17:23:26 +00:00
|
|
|
|
|
|
|
_shader(), _shader_file(), _shader_tech("Draw"), _shader_file_mt(), _shader_file_sz(), _shader_file_tick(0),
|
|
|
|
|
|
|
|
_width_type(size_type::Percent), _width_value(1.0), _height_type(size_type::Percent), _height_value(1.0),
|
|
|
|
|
2020-07-05 21:00:09 +00:00
|
|
|
_have_current_params(false), _time(0), _time_loop(0), _loops(0), _random(), _random_seed(0),
|
2020-04-23 05:56:12 +00:00
|
|
|
|
|
|
|
_rt_up_to_date(false), _rt(std::make_shared<gs::rendertarget>(GS_RGBA, GS_ZS_NONE))
|
2019-08-07 15:14:44 +00:00
|
|
|
{
|
2020-07-05 21:00:09 +00:00
|
|
|
// Intialize random values.
|
|
|
|
_random.seed(static_cast<unsigned long long>(_random_seed));
|
|
|
|
for (size_t idx = 0; idx < 16; idx++) {
|
|
|
|
_random_values[idx] =
|
|
|
|
static_cast<float_t>(static_cast<double_t>(_random()) / static_cast<double_t>(_random.max()));
|
|
|
|
}
|
2019-08-07 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 05:37:47 +00:00
|
|
|
gfx::shader::shader::~shader() {}
|
2018-09-28 09:55:55 +00:00
|
|
|
|
2019-12-22 05:14:26 +00:00
|
|
|
bool gfx::shader::shader::is_shader_different(const std::filesystem::path& file)
|
2019-12-24 07:37:37 +00:00
|
|
|
try {
|
|
|
|
if (std::filesystem::exists(file)) {
|
|
|
|
// Check if the file name differs.
|
|
|
|
if (file != _shader_file)
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-22 05:14:26 +00:00
|
|
|
|
2019-12-24 07:37:37 +00:00
|
|
|
if (std::filesystem::exists(_shader_file)) {
|
|
|
|
// Is the file write time different?
|
|
|
|
if (std::filesystem::last_write_time(_shader_file) != _shader_file_mt)
|
|
|
|
return true;
|
2019-12-21 17:23:26 +00:00
|
|
|
|
2019-12-24 07:37:37 +00:00
|
|
|
// Is the file size different?
|
|
|
|
if (std::filesystem::file_size(_shader_file) != _shader_file_sz)
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-21 17:23:26 +00:00
|
|
|
|
2019-12-24 07:37:37 +00:00
|
|
|
return false;
|
2020-01-01 03:21:02 +00:00
|
|
|
} catch (const std::exception& ex) {
|
2020-07-25 14:51:22 +00:00
|
|
|
DLOG_ERROR("Loading shader '%s' failed with error: %s", file.c_str(), ex.what());
|
2019-12-22 05:14:26 +00:00
|
|
|
return false;
|
2018-03-20 11:45:41 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 05:14:26 +00:00
|
|
|
bool gfx::shader::shader::is_technique_different(const std::string& tech)
|
2019-08-07 18:21:57 +00:00
|
|
|
{
|
2019-12-22 05:14:26 +00:00
|
|
|
// Is the technique different?
|
|
|
|
if (tech != _shader_tech)
|
|
|
|
return true;
|
2019-12-21 17:23:26 +00:00
|
|
|
|
2019-12-22 05:14:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-08-07 16:42:54 +00:00
|
|
|
|
2019-12-22 05:14:26 +00:00
|
|
|
bool gfx::shader::shader::load_shader(const std::filesystem::path& file, const std::string& tech, bool& shader_dirty,
|
|
|
|
bool& param_dirty)
|
2019-12-25 18:18:44 +00:00
|
|
|
try {
|
2019-12-24 07:37:37 +00:00
|
|
|
if (!std::filesystem::exists(file))
|
|
|
|
return false;
|
|
|
|
|
2019-12-22 05:14:26 +00:00
|
|
|
shader_dirty = is_shader_different(file);
|
|
|
|
param_dirty = is_technique_different(tech) || shader_dirty;
|
|
|
|
|
2019-12-25 18:18:44 +00:00
|
|
|
// Update Shader
|
2019-12-22 05:14:26 +00:00
|
|
|
if (shader_dirty) {
|
2019-12-25 18:18:44 +00:00
|
|
|
_shader = gs::effect(file);
|
|
|
|
_shader_file_mt = std::filesystem::last_write_time(file);
|
|
|
|
_shader_file_sz = std::filesystem::file_size(file);
|
|
|
|
_shader_file = file;
|
|
|
|
_shader_file_tick = 0;
|
2019-12-22 05:14:26 +00:00
|
|
|
}
|
2019-12-25 18:18:44 +00:00
|
|
|
|
|
|
|
// Update Params
|
2019-12-22 05:14:26 +00:00
|
|
|
if (param_dirty) {
|
|
|
|
auto settings =
|
|
|
|
std::shared_ptr<obs_data_t>(obs_source_get_settings(_self), [](obs_data_t* p) { obs_data_release(p); });
|
|
|
|
|
|
|
|
bool have_valid_tech = false;
|
2020-04-08 21:38:42 +00:00
|
|
|
for (std::size_t idx = 0; idx < _shader.count_techniques(); idx++) {
|
2019-12-22 05:14:26 +00:00
|
|
|
if (_shader.get_technique(idx).name() == tech) {
|
|
|
|
have_valid_tech = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (have_valid_tech) {
|
|
|
|
_shader_tech = tech;
|
|
|
|
} else {
|
|
|
|
_shader_tech = _shader.get_technique(0).name();
|
|
|
|
|
|
|
|
// Update source data.
|
|
|
|
obs_data_set_string(settings.get(), ST_SHADER_TECHNIQUE, _shader_tech.c_str());
|
|
|
|
}
|
2019-12-18 06:49:56 +00:00
|
|
|
|
2019-12-22 05:14:26 +00:00
|
|
|
// Clear the shader parameters map and rebuild.
|
|
|
|
_shader_params.clear();
|
|
|
|
auto etech = _shader.get_technique(_shader_tech);
|
2020-04-08 21:38:42 +00:00
|
|
|
for (std::size_t idx = 0; idx < etech.count_passes(); idx++) {
|
2019-12-22 05:14:26 +00:00
|
|
|
auto pass = etech.get_pass(idx);
|
2019-08-07 15:14:44 +00:00
|
|
|
|
2020-04-08 21:38:42 +00:00
|
|
|
for (std::size_t vidx = 0; vidx < pass.count_vertex_parameters(); vidx++) {
|
2019-12-18 05:37:47 +00:00
|
|
|
auto el = pass.get_vertex_parameter(vidx);
|
2019-08-07 15:14:44 +00:00
|
|
|
|
2019-12-22 05:14:26 +00:00
|
|
|
if (!el)
|
|
|
|
continue;
|
|
|
|
|
2019-12-18 05:37:47 +00:00
|
|
|
auto fnd = _shader_params.find(el.get_name());
|
|
|
|
if (fnd != _shader_params.end())
|
|
|
|
continue;
|
2019-08-07 15:14:44 +00:00
|
|
|
|
2019-12-18 05:37:47 +00:00
|
|
|
auto param = gfx::shader::parameter::make_parameter(el, ST_PARAMETERS);
|
2019-08-07 15:14:44 +00:00
|
|
|
|
2019-12-22 05:14:26 +00:00
|
|
|
if (param) {
|
2019-12-18 05:37:47 +00:00
|
|
|
_shader_params.insert_or_assign(el.get_name(), param);
|
2019-12-22 05:14:26 +00:00
|
|
|
param->defaults(settings.get());
|
|
|
|
param->update(settings.get());
|
|
|
|
}
|
2019-12-18 05:37:47 +00:00
|
|
|
}
|
2019-08-07 15:14:44 +00:00
|
|
|
|
2020-04-08 21:38:42 +00:00
|
|
|
for (std::size_t vidx = 0; vidx < pass.count_pixel_parameters(); vidx++) {
|
2019-12-18 05:37:47 +00:00
|
|
|
auto el = pass.get_pixel_parameter(vidx);
|
2018-03-20 11:45:41 +00:00
|
|
|
|
2019-12-22 05:14:26 +00:00
|
|
|
if (!el)
|
|
|
|
continue;
|
|
|
|
|
2019-12-18 05:37:47 +00:00
|
|
|
auto fnd = _shader_params.find(el.get_name());
|
|
|
|
if (fnd != _shader_params.end())
|
|
|
|
continue;
|
2019-08-07 15:14:44 +00:00
|
|
|
|
2019-12-18 05:37:47 +00:00
|
|
|
auto param = gfx::shader::parameter::make_parameter(el, ST_PARAMETERS);
|
2019-08-07 15:14:44 +00:00
|
|
|
|
2019-12-22 05:14:26 +00:00
|
|
|
if (param) {
|
2019-12-18 05:37:47 +00:00
|
|
|
_shader_params.insert_or_assign(el.get_name(), param);
|
2019-12-22 05:14:26 +00:00
|
|
|
param->defaults(settings.get());
|
|
|
|
param->update(settings.get());
|
|
|
|
}
|
2019-12-18 05:37:47 +00:00
|
|
|
}
|
2019-08-07 15:14:44 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-21 17:23:26 +00:00
|
|
|
|
2019-12-22 05:14:26 +00:00
|
|
|
return true;
|
2020-01-01 03:21:02 +00:00
|
|
|
} catch (const std::exception& ex) {
|
2020-07-25 14:51:22 +00:00
|
|
|
DLOG_ERROR("Loading shader '%s' failed with error: %s", file.c_str(), ex.what());
|
2020-01-01 03:21:02 +00:00
|
|
|
return false;
|
2019-12-25 18:18:44 +00:00
|
|
|
} catch (...) {
|
|
|
|
return false;
|
2019-08-07 10:42:25 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 20:34:22 +00:00
|
|
|
void gfx::shader::shader::defaults(obs_data_t* data)
|
|
|
|
{
|
|
|
|
obs_data_set_default_string(data, ST_SHADER_FILE, "");
|
|
|
|
obs_data_set_default_string(data, ST_SHADER_TECHNIQUE, "");
|
|
|
|
obs_data_set_default_string(data, ST_SHADER_SIZE_WIDTH, "100.0 %");
|
|
|
|
obs_data_set_default_string(data, ST_SHADER_SIZE_HEIGHT, "100.0 %");
|
2020-07-05 21:00:09 +00:00
|
|
|
obs_data_set_default_int(data, ST_SHADER_SEED, static_cast<long long>(time(NULL)));
|
2020-03-31 20:34:22 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 05:37:47 +00:00
|
|
|
void gfx::shader::shader::properties(obs_properties_t* pr)
|
2019-08-07 17:19:56 +00:00
|
|
|
{
|
2019-12-22 05:14:26 +00:00
|
|
|
_have_current_params = false;
|
|
|
|
|
2019-12-18 05:37:47 +00:00
|
|
|
{
|
|
|
|
auto grp = obs_properties_create();
|
|
|
|
obs_properties_add_group(pr, ST_SHADER, D_TRANSLATE(ST_SHADER), OBS_GROUP_NORMAL, grp);
|
|
|
|
|
|
|
|
{
|
2020-04-23 23:17:28 +00:00
|
|
|
std::string path = "";
|
|
|
|
if (_shader_file.has_parent_path()) {
|
|
|
|
path = _shader_file.parent_path().string();
|
|
|
|
} else {
|
|
|
|
char* vp = obs_module_file("examples");
|
2020-04-24 04:06:46 +00:00
|
|
|
path = vp;
|
2020-04-23 23:17:28 +00:00
|
|
|
bfree(vp);
|
|
|
|
}
|
2019-12-18 05:37:47 +00:00
|
|
|
auto p = obs_properties_add_path(grp, ST_SHADER_FILE, D_TRANSLATE(ST_SHADER_FILE), OBS_PATH_FILE, "*.*",
|
2020-04-23 23:17:28 +00:00
|
|
|
path.c_str());
|
2019-12-18 05:37:47 +00:00
|
|
|
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_SHADER_FILE)));
|
2019-08-07 17:19:56 +00:00
|
|
|
}
|
2019-12-18 05:37:47 +00:00
|
|
|
{
|
|
|
|
auto p = obs_properties_add_list(grp, ST_SHADER_TECHNIQUE, D_TRANSLATE(ST_SHADER_TECHNIQUE),
|
|
|
|
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
|
|
|
|
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_SHADER_TECHNIQUE)));
|
2020-07-05 21:00:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
obs_properties_add_button2(
|
|
|
|
grp, ST_REFRESH, D_TRANSLATE(ST_REFRESH),
|
|
|
|
[](obs_properties_t* props, obs_property_t* prop, void* priv) {
|
|
|
|
return reinterpret_cast<gfx::shader::shader*>(priv)->on_refresh_properties(props, prop);
|
2019-12-18 05:37:47 +00:00
|
|
|
},
|
2020-07-05 21:00:09 +00:00
|
|
|
this);
|
2019-12-18 05:37:47 +00:00
|
|
|
}
|
2020-04-03 16:40:05 +00:00
|
|
|
|
2019-12-18 05:37:47 +00:00
|
|
|
if (_mode != shader_mode::Transition) {
|
|
|
|
auto grp2 = obs_properties_create();
|
|
|
|
obs_properties_add_group(grp, ST_SHADER_SIZE, D_TRANSLATE(ST_SHADER_SIZE), OBS_GROUP_NORMAL, grp2);
|
|
|
|
|
|
|
|
{
|
|
|
|
auto p = obs_properties_add_text(grp2, ST_SHADER_SIZE_WIDTH, D_TRANSLATE(ST_SHADER_SIZE_WIDTH),
|
|
|
|
OBS_TEXT_DEFAULT);
|
2020-04-22 23:32:14 +00:00
|
|
|
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_SHADER_SIZE)));
|
2019-08-07 15:14:44 +00:00
|
|
|
}
|
2019-12-18 05:37:47 +00:00
|
|
|
{
|
|
|
|
auto p = obs_properties_add_text(grp2, ST_SHADER_SIZE_HEIGHT, D_TRANSLATE(ST_SHADER_SIZE_HEIGHT),
|
|
|
|
OBS_TEXT_DEFAULT);
|
2020-04-22 23:32:14 +00:00
|
|
|
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_SHADER_SIZE)));
|
2019-08-07 15:14:44 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-05 21:00:09 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
auto p = obs_properties_add_int_slider(grp, ST_SHADER_SEED, D_TRANSLATE(ST_SHADER_SEED),
|
|
|
|
std::numeric_limits<int>::min(), std::numeric_limits<int>::max(), 1);
|
|
|
|
obs_property_set_long_description(p, D_TRANSLATE(D_DESC(ST_SHADER_SEED)));
|
|
|
|
}
|
2019-08-07 15:14:44 +00:00
|
|
|
}
|
2019-12-18 05:37:47 +00:00
|
|
|
{
|
|
|
|
auto grp = obs_properties_create();
|
|
|
|
obs_properties_add_group(pr, ST_PARAMETERS, D_TRANSLATE(ST_PARAMETERS), OBS_GROUP_NORMAL, grp);
|
2019-08-07 15:14:44 +00:00
|
|
|
}
|
2020-04-03 16:40:05 +00:00
|
|
|
|
|
|
|
// Manually call the refresh.
|
|
|
|
on_refresh_properties(pr, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool gfx::shader::shader::on_refresh_properties(obs_properties_t* props, obs_property_t* prop)
|
|
|
|
{
|
|
|
|
if (_shader) { // Clear list of techniques and rebuild it.
|
|
|
|
obs_property_t* p_tech_list = obs_properties_get(props, ST_SHADER_TECHNIQUE);
|
|
|
|
obs_property_list_clear(p_tech_list);
|
2020-04-08 21:38:42 +00:00
|
|
|
for (std::size_t idx = 0; idx < _shader.count_techniques(); idx++) {
|
2020-04-03 16:40:05 +00:00
|
|
|
auto tech = _shader.get_technique(idx);
|
|
|
|
obs_property_list_add_string(p_tech_list, tech.name().c_str(), tech.name().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{ // Clear parameter options.
|
|
|
|
auto grp = obs_property_group_content(obs_properties_get(props, ST_PARAMETERS));
|
|
|
|
for (auto p = obs_properties_first(grp); p != nullptr; p = obs_properties_first(grp)) {
|
|
|
|
obs::tools::obs_properties_remove_by_name(grp, obs_property_name(p));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rebuild new parameters.
|
|
|
|
obs_data_t* data = obs_source_get_settings(_self);
|
|
|
|
for (auto kv : _shader_params) {
|
|
|
|
kv.second->properties(grp, data);
|
|
|
|
kv.second->defaults(data);
|
|
|
|
kv.second->update(data);
|
|
|
|
}
|
|
|
|
obs_source_update(_self, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2019-08-07 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 16:40:05 +00:00
|
|
|
bool gfx::shader::shader::on_shader_or_technique_modified(obs_properties_t* props, obs_property_t* prop,
|
|
|
|
obs_data_t* data)
|
2019-08-07 17:19:56 +00:00
|
|
|
{
|
2019-12-22 05:14:26 +00:00
|
|
|
bool shader_dirty = false;
|
|
|
|
bool param_dirty = false;
|
2019-08-07 17:19:56 +00:00
|
|
|
|
2019-12-22 05:14:26 +00:00
|
|
|
if (!update_shader(data, shader_dirty, param_dirty))
|
|
|
|
return false;
|
2019-08-07 15:14:44 +00:00
|
|
|
|
2019-12-22 05:14:26 +00:00
|
|
|
{ // Clear list of techniques and rebuild it.
|
|
|
|
obs_property_t* p_tech_list = obs_properties_get(props, ST_SHADER_TECHNIQUE);
|
|
|
|
obs_property_list_clear(p_tech_list);
|
2020-04-08 21:38:42 +00:00
|
|
|
for (std::size_t idx = 0; idx < _shader.count_techniques(); idx++) {
|
2019-12-18 05:37:47 +00:00
|
|
|
auto tech = _shader.get_technique(idx);
|
2019-12-22 05:14:26 +00:00
|
|
|
obs_property_list_add_string(p_tech_list, tech.name().c_str(), tech.name().c_str());
|
2019-08-07 15:14:44 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-22 05:14:26 +00:00
|
|
|
if (param_dirty || !_have_current_params) {
|
|
|
|
// Clear parameter options.
|
|
|
|
auto grp = obs_property_group_content(obs_properties_get(props, ST_PARAMETERS));
|
|
|
|
for (auto p = obs_properties_first(grp); p != nullptr; p = obs_properties_first(grp)) {
|
|
|
|
obs::tools::obs_properties_remove_by_name(grp, obs_property_name(p));
|
2019-12-18 06:49:56 +00:00
|
|
|
}
|
2019-09-05 16:42:28 +00:00
|
|
|
|
2019-12-22 05:14:26 +00:00
|
|
|
// Rebuild new parameters.
|
|
|
|
for (auto kv : _shader_params) {
|
2019-12-25 09:14:04 +00:00
|
|
|
kv.second->properties(grp, data);
|
2019-12-22 05:14:26 +00:00
|
|
|
kv.second->defaults(data);
|
|
|
|
kv.second->update(data);
|
2019-08-07 18:21:57 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-22 05:14:26 +00:00
|
|
|
|
|
|
|
_have_current_params = true;
|
|
|
|
return shader_dirty || param_dirty || !_have_current_params;
|
2019-08-07 18:21:57 +00:00
|
|
|
}
|
2019-08-07 15:14:44 +00:00
|
|
|
|
2019-12-22 05:14:26 +00:00
|
|
|
bool gfx::shader::shader::update_shader(obs_data_t* data, bool& shader_dirty, bool& param_dirty)
|
2019-08-07 18:21:57 +00:00
|
|
|
{
|
2019-12-22 05:14:26 +00:00
|
|
|
const char* file_c = obs_data_get_string(data, ST_SHADER_FILE);
|
|
|
|
std::string file = file_c ? file_c : "";
|
|
|
|
const char* tech_c = obs_data_get_string(data, ST_SHADER_TECHNIQUE);
|
|
|
|
std::string tech = tech_c ? tech_c : "Draw";
|
2019-12-21 17:23:26 +00:00
|
|
|
|
2019-12-22 05:14:26 +00:00
|
|
|
return load_shader(file, tech, shader_dirty, param_dirty);
|
2019-08-07 18:21:57 +00:00
|
|
|
}
|
2019-08-07 15:14:44 +00:00
|
|
|
|
2019-12-18 05:37:47 +00:00
|
|
|
inline std::pair<gfx::shader::size_type, double_t> parse_text_as_size(const char* text)
|
2019-08-07 18:21:57 +00:00
|
|
|
{
|
2019-12-18 05:37:47 +00:00
|
|
|
double_t v = 0;
|
|
|
|
if (sscanf(text, "%lf", &v) == 1) {
|
|
|
|
const char* prc_chr = strrchr(text, '%');
|
|
|
|
if (prc_chr && (*prc_chr == '%')) {
|
|
|
|
return {gfx::shader::size_type::Percent, v / 100.0};
|
|
|
|
} else {
|
|
|
|
return {gfx::shader::size_type::Pixel, v};
|
|
|
|
}
|
2019-08-07 18:21:57 +00:00
|
|
|
} else {
|
2019-12-18 05:37:47 +00:00
|
|
|
return {gfx::shader::size_type::Percent, 1.0};
|
2019-08-07 18:21:57 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-07 15:14:44 +00:00
|
|
|
|
2019-12-18 05:37:47 +00:00
|
|
|
void gfx::shader::shader::update(obs_data_t* data)
|
2019-08-07 18:39:55 +00:00
|
|
|
{
|
2019-12-22 05:14:26 +00:00
|
|
|
bool v1, v2;
|
|
|
|
update_shader(data, v1, v2);
|
2019-08-07 18:39:55 +00:00
|
|
|
|
2019-12-18 05:37:47 +00:00
|
|
|
{
|
|
|
|
auto sz_x = parse_text_as_size(obs_data_get_string(data, ST_SHADER_SIZE_WIDTH));
|
|
|
|
_width_type = sz_x.first;
|
|
|
|
_width_value = std::clamp(sz_x.second, 0.01, 8192.0);
|
2019-08-07 15:14:44 +00:00
|
|
|
|
2019-12-18 05:37:47 +00:00
|
|
|
auto sz_y = parse_text_as_size(obs_data_get_string(data, ST_SHADER_SIZE_HEIGHT));
|
|
|
|
_height_type = sz_y.first;
|
|
|
|
_height_value = std::clamp(sz_y.second, 0.01, 8192.0);
|
2019-08-07 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 21:00:09 +00:00
|
|
|
if (int32_t seed = static_cast<int32_t>(obs_data_get_int(data, ST_SHADER_SEED)); _random_seed != seed) {
|
|
|
|
_random_seed = seed;
|
|
|
|
_random.seed(static_cast<unsigned long long>(_random_seed));
|
|
|
|
for (size_t idx = 0; idx < 16; idx++) {
|
|
|
|
_random_values[idx] =
|
|
|
|
static_cast<float_t>(static_cast<double_t>(_random()) / static_cast<double_t>(_random.max()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-18 05:37:47 +00:00
|
|
|
for (auto kv : _shader_params) {
|
|
|
|
kv.second->update(data);
|
2019-08-07 15:14:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 01:29:05 +00:00
|
|
|
uint32_t gfx::shader::shader::width()
|
2019-08-07 15:14:44 +00:00
|
|
|
{
|
2019-12-18 05:37:47 +00:00
|
|
|
switch (_mode) {
|
|
|
|
case shader_mode::Transition:
|
|
|
|
return _base_width;
|
|
|
|
case shader_mode::Source:
|
|
|
|
switch (_width_type) {
|
|
|
|
case size_type::Pixel:
|
2020-06-21 16:42:05 +00:00
|
|
|
return std::clamp(static_cast<uint32_t>(_width_value), 1u, 16384u);
|
2019-12-18 05:37:47 +00:00
|
|
|
case size_type::Percent:
|
2020-06-21 16:42:05 +00:00
|
|
|
return std::clamp(static_cast<uint32_t>(_width_value * _base_width), 1u, 16384u);
|
2019-12-18 05:37:47 +00:00
|
|
|
}
|
|
|
|
case shader_mode::Filter:
|
|
|
|
switch (_width_type) {
|
|
|
|
case size_type::Pixel:
|
2020-06-21 16:42:05 +00:00
|
|
|
return std::clamp(static_cast<uint32_t>(_width_value), 1u, 16384u);
|
2019-12-18 05:37:47 +00:00
|
|
|
case size_type::Percent:
|
2020-06-21 16:42:05 +00:00
|
|
|
return std::clamp(static_cast<uint32_t>(_width_value * _base_width), 1u, 16384u);
|
2019-08-07 15:14:44 +00:00
|
|
|
}
|
2019-12-18 05:37:47 +00:00
|
|
|
default:
|
|
|
|
return 0;
|
2019-08-07 15:14:44 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-20 11:45:41 +00:00
|
|
|
|
2020-08-10 01:29:05 +00:00
|
|
|
uint32_t gfx::shader::shader::height()
|
2019-08-07 15:14:44 +00:00
|
|
|
{
|
2019-12-18 05:37:47 +00:00
|
|
|
switch (_mode) {
|
|
|
|
case shader_mode::Transition:
|
|
|
|
return _base_height;
|
|
|
|
case shader_mode::Source:
|
|
|
|
switch (_height_type) {
|
|
|
|
case size_type::Pixel:
|
2020-06-21 16:42:05 +00:00
|
|
|
return std::clamp(static_cast<uint32_t>(_height_value), 1u, 16384u);
|
2019-12-18 05:37:47 +00:00
|
|
|
case size_type::Percent:
|
2020-06-21 16:42:05 +00:00
|
|
|
return std::clamp(static_cast<uint32_t>(_height_value * _base_height), 1u, 16384u);
|
2019-08-07 15:14:44 +00:00
|
|
|
}
|
2019-12-18 05:37:47 +00:00
|
|
|
case shader_mode::Filter:
|
|
|
|
switch (_height_type) {
|
|
|
|
case size_type::Pixel:
|
2020-06-21 16:42:05 +00:00
|
|
|
return std::clamp(static_cast<uint32_t>(_height_value), 1u, 16384u);
|
2019-12-18 05:37:47 +00:00
|
|
|
case size_type::Percent:
|
2020-06-21 16:42:05 +00:00
|
|
|
return std::clamp(static_cast<uint32_t>(_height_value * _base_height), 1u, 16384u);
|
2019-12-18 05:37:47 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return 0;
|
2019-08-07 15:14:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 01:29:05 +00:00
|
|
|
uint32_t gfx::shader::shader::base_width()
|
2020-06-21 16:42:05 +00:00
|
|
|
{
|
|
|
|
return _base_width;
|
|
|
|
}
|
|
|
|
|
2020-08-10 01:29:05 +00:00
|
|
|
uint32_t gfx::shader::shader::base_height()
|
2020-06-21 16:42:05 +00:00
|
|
|
{
|
|
|
|
return _base_height;
|
|
|
|
}
|
|
|
|
|
2019-12-18 05:37:47 +00:00
|
|
|
bool gfx::shader::shader::tick(float_t time)
|
2018-09-28 09:55:55 +00:00
|
|
|
{
|
2019-12-22 05:14:26 +00:00
|
|
|
_shader_file_tick = static_cast<float_t>(static_cast<double_t>(_shader_file_tick) + static_cast<double_t>(time));
|
|
|
|
if (_shader_file_tick >= 1.0f / 3.0f) {
|
|
|
|
_shader_file_tick -= 1.0f / 3.0f;
|
|
|
|
bool v1, v2;
|
|
|
|
load_shader(_shader_file, _shader_tech, v1, v2);
|
|
|
|
}
|
|
|
|
|
2019-12-18 05:37:47 +00:00
|
|
|
// Update State
|
|
|
|
_time += time;
|
2020-03-28 21:17:33 +00:00
|
|
|
_time_loop += time;
|
|
|
|
if (_time_loop > 1.) {
|
|
|
|
_time_loop -= 1.;
|
|
|
|
|
|
|
|
// Loops
|
|
|
|
_loops += 1;
|
|
|
|
if (_loops >= 4194304)
|
|
|
|
_loops = -_loops;
|
|
|
|
}
|
2018-03-20 11:45:41 +00:00
|
|
|
|
2020-07-05 21:00:09 +00:00
|
|
|
// Recreate Per-Activation-Random values.
|
|
|
|
for (size_t idx = 0; idx < 8; idx++) {
|
|
|
|
_random_values[8 + idx] =
|
|
|
|
static_cast<float_t>(static_cast<double_t>(_random()) / static_cast<double_t>(_random.max()));
|
|
|
|
}
|
|
|
|
|
2019-08-07 16:42:54 +00:00
|
|
|
return false;
|
2018-03-20 11:45:41 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 17:37:00 +00:00
|
|
|
void gfx::shader::shader::prepare_render()
|
2018-09-28 09:55:55 +00:00
|
|
|
{
|
2019-12-18 05:37:47 +00:00
|
|
|
if (!_shader)
|
2019-08-07 15:14:44 +00:00
|
|
|
return;
|
|
|
|
|
2019-12-18 06:49:56 +00:00
|
|
|
for (auto kv : _shader_params) {
|
|
|
|
kv.second->assign();
|
|
|
|
}
|
|
|
|
|
2020-07-05 21:00:09 +00:00
|
|
|
// float4 Time: (Current Time), (Zero), (Zero), (Random Value)
|
2019-12-18 06:49:56 +00:00
|
|
|
if (gs::effect_parameter el = _shader.get_parameter("Time"); el != nullptr) {
|
|
|
|
if (el.get_type() == gs::effect_parameter::type::Float4) {
|
|
|
|
el.set_float4(
|
2020-03-28 21:17:33 +00:00
|
|
|
_time, _time_loop, static_cast<float_t>(_loops),
|
2020-07-05 21:00:09 +00:00
|
|
|
static_cast<float_t>(static_cast<double_t>(_random()) / static_cast<double_t>(_random.max())));
|
2019-12-18 06:49:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-05 21:00:09 +00:00
|
|
|
// float4 ViewSize: (Width), (Height), (1.0 / Width), (1.0 / Height)
|
2020-01-01 03:21:02 +00:00
|
|
|
if (auto el = _shader.get_parameter("ViewSize"); el != nullptr) {
|
|
|
|
if (el.get_type() == gs::effect_parameter::type::Float4) {
|
|
|
|
el.set_float4(static_cast<float_t>(width()), static_cast<float_t>(height()),
|
|
|
|
1.0f / static_cast<float_t>(width()), 1.0f / static_cast<float_t>(height()));
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 05:56:12 +00:00
|
|
|
|
2020-07-05 21:00:09 +00:00
|
|
|
// float4x4 Random: float4[Per-Instance Random], float4[Per-Activation Random], float4x2[Per-Frame Random]
|
|
|
|
if (auto el = _shader.get_parameter("Random"); el != nullptr) {
|
|
|
|
if (el.get_type() == gs::effect_parameter::type::Matrix) {
|
|
|
|
el.set_value(_random_values, 16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// int32 RandomSeed: Seed used for random generation
|
|
|
|
if (auto el = _shader.get_parameter("RandomSeed"); el != nullptr) {
|
|
|
|
if (el.get_type() == gs::effect_parameter::type::Integer) {
|
|
|
|
el.set_int(_random_seed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 05:56:12 +00:00
|
|
|
_rt_up_to_date = false;
|
2020-03-28 17:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void gfx::shader::shader::render()
|
|
|
|
{
|
|
|
|
if (!_shader)
|
|
|
|
return;
|
2020-01-01 03:21:02 +00:00
|
|
|
|
2020-04-23 05:56:12 +00:00
|
|
|
if (!_rt_up_to_date) {
|
|
|
|
auto op = _rt->render(width(), height());
|
|
|
|
vec4 zero = {0, 0, 0, 0};
|
2020-05-02 16:36:57 +00:00
|
|
|
gs_ortho(0, 1, 0, 1, 0, 1);
|
2020-04-23 05:56:12 +00:00
|
|
|
gs_clear(GS_CLEAR_COLOR, &zero, 0, 0);
|
2020-04-24 04:06:46 +00:00
|
|
|
|
|
|
|
gs_blend_state_push();
|
|
|
|
gs_reset_blend_state();
|
|
|
|
|
2020-04-23 05:56:12 +00:00
|
|
|
gs_enable_blending(true);
|
2020-04-24 05:48:52 +00:00
|
|
|
gs_blend_function_separate(GS_BLEND_ONE, GS_BLEND_ZERO, GS_BLEND_ONE, GS_BLEND_ZERO);
|
2020-04-23 05:56:12 +00:00
|
|
|
gs_enable_color(true, true, true, true);
|
|
|
|
while (gs_effect_loop(_shader.get_object(), _shader_tech.c_str())) {
|
2020-05-02 16:36:57 +00:00
|
|
|
streamfx::gs_draw_fullscreen_tri();
|
2020-04-23 05:56:12 +00:00
|
|
|
}
|
2020-04-24 04:06:46 +00:00
|
|
|
|
|
|
|
gs_blend_state_pop();
|
2020-04-23 05:56:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gs_effect_set_texture(gs_effect_get_param_by_name(obs_get_base_effect(OBS_EFFECT_DEFAULT), "image"),
|
|
|
|
_rt->get_texture()->get_object());
|
|
|
|
while (gs_effect_loop(obs_get_base_effect(OBS_EFFECT_DEFAULT), "Draw")) {
|
2020-03-28 17:37:00 +00:00
|
|
|
gs_draw_sprite(nullptr, 0, width(), height());
|
2019-08-07 15:14:44 +00:00
|
|
|
}
|
2018-03-20 11:45:41 +00:00
|
|
|
}
|
2019-08-07 15:37:34 +00:00
|
|
|
|
2020-08-10 01:29:05 +00:00
|
|
|
void gfx::shader::shader::set_size(uint32_t w, uint32_t h)
|
2019-08-07 18:21:57 +00:00
|
|
|
{
|
2019-12-18 05:37:47 +00:00
|
|
|
_base_width = w;
|
|
|
|
_base_height = h;
|
2019-08-07 18:39:55 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 05:37:47 +00:00
|
|
|
void gfx::shader::shader::set_input_a(std::shared_ptr<gs::texture> tex)
|
2019-08-07 16:42:54 +00:00
|
|
|
{
|
2020-03-28 17:37:00 +00:00
|
|
|
if (!_shader)
|
|
|
|
return;
|
|
|
|
|
2020-05-13 06:41:32 +00:00
|
|
|
std::string_view params[] = {
|
|
|
|
"InputA",
|
|
|
|
"image",
|
|
|
|
"tex_a",
|
|
|
|
};
|
|
|
|
for (auto& name : params) {
|
|
|
|
if (gs::effect_parameter el = _shader.get_parameter(name.data()); el != nullptr) {
|
|
|
|
if (el.get_type() == gs::effect_parameter::type::Texture) {
|
|
|
|
el.set_texture(tex);
|
|
|
|
break;
|
|
|
|
}
|
2020-03-28 17:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-07 15:37:34 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 05:37:47 +00:00
|
|
|
void gfx::shader::shader::set_input_b(std::shared_ptr<gs::texture> tex)
|
2019-08-07 16:42:54 +00:00
|
|
|
{
|
2020-03-28 17:37:00 +00:00
|
|
|
if (!_shader)
|
|
|
|
return;
|
|
|
|
|
2020-05-13 06:41:32 +00:00
|
|
|
std::string_view params[] = {
|
|
|
|
"InputB",
|
|
|
|
"image2",
|
|
|
|
"tex_b",
|
|
|
|
};
|
|
|
|
for (auto& name : params) {
|
|
|
|
if (gs::effect_parameter el = _shader.get_parameter(name.data()); el != nullptr) {
|
|
|
|
if (el.get_type() == gs::effect_parameter::type::Texture) {
|
|
|
|
el.set_texture(tex);
|
|
|
|
break;
|
|
|
|
}
|
2020-03-28 17:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void gfx::shader::shader::set_transition_time(float_t t)
|
|
|
|
{
|
|
|
|
if (!_shader)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (gs::effect_parameter el = _shader.get_parameter("TransitionTime"); el != nullptr) {
|
|
|
|
if (el.get_type() == gs::effect_parameter::type::Float) {
|
|
|
|
el.set_float(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 01:29:05 +00:00
|
|
|
void gfx::shader::shader::set_transition_size(uint32_t w, uint32_t h)
|
2020-03-28 17:37:00 +00:00
|
|
|
{
|
|
|
|
if (!_shader)
|
|
|
|
return;
|
|
|
|
if (gs::effect_parameter el = _shader.get_parameter("TransitionSize"); el != nullptr) {
|
|
|
|
if (el.get_type() == gs::effect_parameter::type::Integer2) {
|
2020-04-08 21:38:42 +00:00
|
|
|
el.set_int2(static_cast<int32_t>(w), static_cast<int32_t>(h));
|
2020-03-28 17:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-07 15:37:34 +00:00
|
|
|
}
|
2020-07-05 21:00:09 +00:00
|
|
|
|
|
|
|
void gfx::shader::shader::set_active(bool active)
|
|
|
|
{
|
|
|
|
_active = active;
|
|
|
|
|
|
|
|
// Recreate Per-Activation-Random values.
|
|
|
|
for (size_t idx = 0; idx < 4; idx++) {
|
|
|
|
_random_values[4 + idx] =
|
|
|
|
static_cast<float_t>(static_cast<double_t>(_random()) / static_cast<double_t>(_random.max()));
|
|
|
|
}
|
|
|
|
}
|