BugFix Custom Filter shader uniform variable

Response to issue #159. Allow uniform variable names with numbers and underscore, and values with float numbers (i.e. fixed the regular expression). Plus also fix a display bug in shader editor GUI.
This commit is contained in:
Bruno Herbelin
2024-11-16 18:09:38 +01:00
parent 5a933beb16
commit 136b1561c1
3 changed files with 11 additions and 5 deletions

View File

@@ -1,4 +1,4 @@
/*
/*
* This file is part of vimix - video live mixer
*
* **Copyright** (C) 2019-2023 Bruno Herbelin <bruno.herbelin@gmail.com>
@@ -435,7 +435,8 @@ FilteringProgram ImageFilter::program () const
}
#define REGEX_UNIFORM_DECLARATION "uniform\\s+float\\s+"
#define REGEX_UNIFORM_VALUE "(\\s*=\\s*[[:digit:]](\\.[[:digit:]])?)?\\s*\\;"
#define REGEX_VARIABLE_NAME "[a-zA-Z_][\\w]+"
#define REGEX_UNIFORM_VALUE "(\\s*=\\s*[[:digit:]]+(\\.[[:digit:]]*)?)?\\s*\\;"
void ImageFilter::setProgram(const FilteringProgram &f, std::promise<std::string> *ret)
{
@@ -453,7 +454,7 @@ void ImageFilter::setProgram(const FilteringProgram &f, std::promise<std::string
// Search for "uniform float", a variable name, with possibly a '=' and float value
std::string glslcode(codes.first);
std::smatch found_uniform;
std::regex is_a_uniform(REGEX_UNIFORM_DECLARATION "[[:alpha:]]+" REGEX_UNIFORM_VALUE);
std::regex is_a_uniform(REGEX_UNIFORM_DECLARATION REGEX_VARIABLE_NAME REGEX_UNIFORM_VALUE);
// loop over every uniform declarations in the GLSL code
while (std::regex_search(glslcode, found_uniform, is_a_uniform)) {
// found a complete declaration of uniform variable
@@ -468,9 +469,12 @@ void ImageFilter::setProgram(const FilteringProgram &f, std::promise<std::string
// try to find a value in uniform declaration, and set parameter value if valid
float val = 0.f;
std::string valuestring =
std::regex_replace(declaration,std::regex(REGEX_UNIFORM_DECLARATION),"");
valuestring = std::regex_replace(valuestring, std::regex(REGEX_VARIABLE_NAME),"");
std::smatch found_value;
std::regex is_a_float_value("[[:digit:]](\\.[[:digit:]])?");
if (std::regex_search(declaration, found_value, is_a_float_value)) {
std::regex is_a_float_value("[[:digit:]]+(\\.[[:digit:]]*)?");
if (std::regex_search(valuestring, found_value, is_a_float_value)) {
// set value only if a value is given
if ( BaseToolkit::is_a_value(found_value.str(), &val) )
program_.setParameter(varname, val);

View File

@@ -56,6 +56,7 @@ public:
inline std::map< std::string, float > parameters() const { return parameters_; }
// set the value of a parameter
inline void clearParameters() { parameters_.clear(); }
inline void setParameter(const std::string &p, float value) { parameters_[p] = value; }
bool hasParameter(const std::string &p);
void removeParameter(const std::string &p);

View File

@@ -109,6 +109,7 @@ void ShaderEditWindow::BuildShader()
// set the code of the current filter
filters_[current_].setCode( { _editor.GetText(), "" } );
filters_[current_].clearParameters();
// filter changed, cannot be named as before
filters_[current_].setName("Custom");