Skip to content

Commit

Permalink
Improve feedback about success of loading a shader
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Hlusička committed Apr 18, 2020
1 parent 33fd866 commit 87bd196
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 12 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ crate-type = ["cdylib"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
obs-wrapper = { git = "https://github.com/Limeth/rust-obs-plugins" }
#obs-wrapper = { path = "../rust-obs-plugins" }
# obs-wrapper = { git = "https://github.com/Limeth/rust-obs-plugins" }
obs-wrapper = { path = "../rust-obs-plugins" }
regex = "1"
anyhow = "1.0"
ammolite-math = { git = "https://github.com/metaview-org/ammolite" }
Expand All @@ -24,3 +24,4 @@ paste = "0.1.7"
ordered-float = "1.0"
apodize = "1.0"
downcast = { package = "downcast-rs", version = "1.1" }
textwrap = "0.11"
41 changes: 41 additions & 0 deletions examples/fft_delta_err.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Configure builtin uniforms
// These macros are optional, but improve the user experience
#pragma shaderfilter set main__mix__description Main Mix/Track
#pragma shaderfilter set main__channel__description Main Channel
#pragma shaderfilter set main__dampening_factor_attack 0.0
#pragma shaderfilter set main__dampening_factor_release 0.0001
uniform texture2d builtin_texture_fft_main;
uniform texture2d builtin_texture_fft_main_previous;

float remap(float x, float2 from, float2 to) {
float normalized = (x - from[0]) / (from[1] - from[0]);
return normalized * (to[1] - to[0]) + to[0];
}

float remap_amplitude(float fft_amplitude) {
float fft_db = 20.0 * log(fft_amplitude / 0.5) / log(10.0);
mat2 x = 1;

return remap(fft_db, float2(-50, -0), float2(0, 1));
}

bool below_db(float2 uv, float fft_amplitude) {
return 1.0 - uv.y < remap_amplitude(fft_amplitude);
}

float4 render(float2 uv) {
float3 color = image.Sample(builtin_texture_sampler, uv).rgb;
float fft_frequency = uv.x;
float fft_amplitude = builtin_texture_fft_main.Sample(builtin_texture_sampler, float2(fft_frequency, 0.5)).r;
float fft_amplitude_previous = builtin_texture_fft_main_previous.Sample(builtin_texture_sampler, float2(fft_frequency, 0.5)).r;
float value = float(below_db(uv, fft_amplitude));
float value_previous = float(below_db(uv, fft_amplitude_previous));

float difference = value - value_previous;
float rising = float(difference > 0);
float falling = float(difference < 0);

float4 fft_color = float4(falling, rising, 0.0, abs(difference));

return float4(lerp(color, fft_color.rgb, fft_color.a), 1.0);
}
54 changes: 46 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ struct Data {

property_shader: PropertyDescriptor<PropertyDescriptorSpecializationPath>,
property_shader_reload: PropertyDescriptor<PropertyDescriptorSpecializationButton>,
property_message: PropertyDescriptor<PropertyDescriptorSpecializationString>,
property_message_display: bool,

settings_update_requested: Arc<AtomicBool>,
}
Expand Down Expand Up @@ -439,6 +441,14 @@ impl Data {
}),
)
},
property_message: PropertyDescriptor {
name: CString::new("message").unwrap(),
description: CString::new("").unwrap(),
specialization: PropertyDescriptorSpecializationString {
string_type: StringType::Multiline,
}
},
property_message_display: false,
settings_update_requested,
}
}
Expand Down Expand Up @@ -481,6 +491,10 @@ impl GetPropertiesSource<Data> for ScrollFocusFilter {
properties.add_property(&data.property_shader);
properties.add_property(&data.property_shader_reload);

if data.property_message_display {
properties.add_property(&data.property_message);
}

if let Some(effect) = data.effect.as_ref() {
effect.add_properties(&mut properties);
}
Expand Down Expand Up @@ -593,6 +607,15 @@ impl UpdateSource<Data> for ScrollFocusFilter {
let (data, settings) = context.data_settings_mut();
let data = data.as_mut().ok_or_else(|| "Could not access the data.")?;

// Drop old effect before the new one is created.
let old_effect_source = data.effect.take().map(|old_effect| {
let graphics_context = GraphicsContext::enter()
.expect("Could not enter a graphics context.");
let old_effect_source = old_effect.effect_source.clone();
old_effect.enable_and_drop(&graphics_context);
old_effect_source
});

const EFFECT_SOURCE_TEMPLATE: &'static str = include_str!("effect_template.effect");
let shader_path = settings.get_property_value(&data.property_shader, &PathBuf::new());
let mut shader_file = File::open(&shader_path)
Expand Down Expand Up @@ -634,7 +657,13 @@ impl UpdateSource<Data> for ScrollFocusFilter {
effect_source_c.as_c_str(),
shader_path_c.as_c_str(),
&graphics_context,
).ok_or_else(|| "Could not create the effect.")?;
).map_err(|err| {
if let Some(err) = err {
Cow::Owned(format!("Could not create the effect due to the following error: {}", err))
} else {
Cow::Borrowed("Could not create the effect due to an unknown error. Check the OBS log for more information.")
}
})?;
let mut builtin_param_names = vec!["ViewProj", "image"];

macro_rules! builtin_effect {
Expand Down Expand Up @@ -671,29 +700,38 @@ impl UpdateSource<Data> for ScrollFocusFilter {
.map(|(index, param)| {
(param.name().to_string(), Indexed::from((index, param)))
})
// FIXME: Iterating over a hashmap does not guarantee an order
.collect::<HashMap<_, _>>();

params.custom = EffectParamsCustom::from(custom_params, settings, &preprocess_result)?;

// Drop old effect before the new one is created.
if let Some(old_effect) = data.effect.take() {
old_effect.enable_and_drop(&graphics_context);
}

data.effect = Some(PreparedEffect {
effect: effect.disable(),
effect_source: effect_source.clone(),
params,
});

if data.effect.is_none() || data.effect.as_ref().unwrap().effect_source != effect_source {
if old_effect_source.is_none() || old_effect_source.unwrap() != effect_source {
data.property_message_display = false;

settings.set_property_value(&data.property_message, CString::new("").unwrap());
data.source.update_source_properties();
}
};

if let Err(error_message) = result {
println!("An error occurred while updating a ShaderFilter Plus filter: {}", error_message);

let (data, settings) = context.data_settings_mut();

if let Some(data) = data.as_mut() {
data.property_message_display = true;

settings.set_property_value(
&data.property_message,
CString::new(error_message.as_ref()).unwrap(),
);
data.source.update_source_properties();
}
}
}
}
Expand Down

0 comments on commit 87bd196

Please sign in to comment.