From 79406b0b08795107e84c0f7c33652a01e8e640b3 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Sat, 28 Mar 2020 22:17:33 +0100 Subject: [PATCH] gfx-shader: Additional stable time values 'Time.x' gets inaccurate if OBS Studio is running for more than two hours, therefore we have to do something to fix it. By allowing the shader code to control when things loop using 'Time.y' (0..1) and 'Time.z' (the number of times 'Time.y' wrapped back to 0), a much more stable animation can be achieved. --- source/gfx/shader/gfx-shader.cpp | 13 +++++++++++-- source/gfx/shader/gfx-shader.hpp | 8 +++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/source/gfx/shader/gfx-shader.cpp b/source/gfx/shader/gfx-shader.cpp index dc8506b7..08716e2c 100644 --- a/source/gfx/shader/gfx-shader.cpp +++ b/source/gfx/shader/gfx-shader.cpp @@ -38,7 +38,7 @@ gfx::shader::shader::shader(obs_source_t* self, shader_mode mode) _width_type(size_type::Percent), _width_value(1.0), _height_type(size_type::Percent), _height_value(1.0), - _time(0), _random(), _have_current_params(false) + _time(0), _time_loop(0), _loops(0), _random(), _have_current_params(false) { _random.seed(static_cast(time(NULL))); } @@ -362,6 +362,15 @@ bool gfx::shader::shader::tick(float_t time) // Update State _time += time; + _time_loop += time; + if (_time_loop > 1.) { + _time_loop -= 1.; + + // Loops + _loops += 1; + if (_loops >= 4194304) + _loops = -_loops; + } return false; } @@ -379,7 +388,7 @@ void gfx::shader::shader::prepare_render() if (gs::effect_parameter el = _shader.get_parameter("Time"); el != nullptr) { if (el.get_type() == gs::effect_parameter::type::Float4) { el.set_float4( - _time, 0, 0, + _time, _time_loop, static_cast(_loops), static_cast(static_cast(_random()) / static_cast(std::numeric_limits::max()))); } diff --git a/source/gfx/shader/gfx-shader.hpp b/source/gfx/shader/gfx-shader.hpp index 2ba28133..7b714a40 100644 --- a/source/gfx/shader/gfx-shader.hpp +++ b/source/gfx/shader/gfx-shader.hpp @@ -56,9 +56,9 @@ namespace gfx { obs_source_t* _self; // Inputs - shader_mode _mode; - uint32_t _base_width; - uint32_t _base_height; + shader_mode _mode; + uint32_t _base_width; + uint32_t _base_height; // Shader gs::effect _shader; @@ -77,6 +77,8 @@ namespace gfx { // Cache float_t _time; + float_t _time_loop; + int32_t _loops; std::mt19937_64 _random; bool _have_current_params;