Skip to content

Commit

Permalink
Add builtin properties: builtin_frame, builtin_framerate, `builti…
Browse files Browse the repository at this point in the history
…n_elapsed_time_previous`
  • Loading branch information
Jakub Hlusička committed Apr 17, 2020
1 parent 52b8a41 commit 2861d6a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/effect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,26 +338,38 @@ impl EffectParamsCustom {
}

pub struct EffectParams {
pub frame: EffectParamInt,
pub framerate: EffectParamFloat,
pub elapsed_time: EffectParamFloat,
pub elapsed_time_previous: EffectParamFloat,
pub uv_size: EffectParamIVec2,
pub custom: EffectParamsCustom,
}

impl EffectParams {
pub fn stage_values(&mut self, graphics_context: &GraphicsContext) {
self.frame.stage_value(graphics_context);
self.framerate.stage_value(graphics_context);
self.elapsed_time.stage_value(graphics_context);
self.elapsed_time_previous.stage_value(graphics_context);
self.uv_size.stage_value(graphics_context);
self.custom.stage_values(graphics_context);
}

pub fn assign_values(&mut self, graphics_context: &FilterContext) {
self.frame.assign_value(graphics_context);
self.framerate.assign_value(graphics_context);
self.elapsed_time.assign_value(graphics_context);
self.elapsed_time_previous.assign_value(graphics_context);
self.uv_size.assign_value(graphics_context);
self.custom.assign_values(graphics_context);
}

pub fn enable_and_drop(self, graphics_context: &GraphicsContext) {
self.frame.enable_and_drop(graphics_context);
self.framerate.enable_and_drop(graphics_context);
self.elapsed_time.enable_and_drop(graphics_context);
self.elapsed_time_previous.enable_and_drop(graphics_context);
self.uv_size.enable_and_drop(graphics_context);
self.custom.enable_and_drop(graphics_context);
}
Expand Down
3 changes: 3 additions & 0 deletions src/effect_template.effect
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
uniform float4x4 ViewProj;
uniform texture2d image;

uniform int builtin_frame;
uniform float builtin_framerate;
uniform float builtin_elapsed_time;
uniform float builtin_elapsed_time_previous;
uniform int2 builtin_uv_size;

sampler_state builtin_texture_sampler {
Expand Down
20 changes: 19 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,10 @@ impl GlobalState {
struct Data {
source: SourceContext,
effect: Option<PreparedEffect>,

creation: Instant,
next_frame: u32,
elapsed_time_previous: Option<f32>,

property_shader: PropertyDescriptor<PropertyDescriptorSpecializationPath>,
property_shader_reload: PropertyDescriptor<PropertyDescriptorSpecializationButton>,
Expand All @@ -410,6 +413,8 @@ impl Data {
source,
effect: None,
creation: Instant::now(),
next_frame: 0,
elapsed_time_previous: None,
property_shader: PropertyDescriptor {
name: CString::new("shader").unwrap(),
description: CString::new("The shader to use.").unwrap(),
Expand Down Expand Up @@ -491,10 +496,20 @@ impl VideoTickSource<Data> for ScrollFocusFilter {
return;
};

let frame = data.next_frame;
data.next_frame += 1;
let framerate = ObsVideoInfo::get().map(|info| info.framerate().as_f32()).unwrap_or(0.0);
let elapsed_time = data.creation.elapsed().as_secs_f32();
let elapsed_time_previous = data.elapsed_time_previous.replace(elapsed_time)
.unwrap_or(elapsed_time);

if let Some(effect) = data.effect.as_mut() {
let params = &mut effect.params;

params.elapsed_time.prepare_value(data.creation.elapsed().as_secs_f32());
params.frame.prepare_value(frame as i32);
params.framerate.prepare_value(framerate);
params.elapsed_time.prepare_value(elapsed_time);
params.elapsed_time_previous.prepare_value(elapsed_time_previous);
params.uv_size.prepare_value([
data.source.get_base_width() as i32,
data.source.get_base_height() as i32,
Expand Down Expand Up @@ -638,7 +653,10 @@ impl UpdateSource<Data> for ScrollFocusFilter {
}

let mut params = EffectParams {
frame: builtin_effect!("builtin_frame"),
framerate: builtin_effect!("builtin_framerate"),
elapsed_time: builtin_effect!("builtin_elapsed_time"),
elapsed_time_previous: builtin_effect!("builtin_elapsed_time_previous"),
uv_size: builtin_effect!("builtin_uv_size"),
custom: Default::default(),
};
Expand Down

0 comments on commit 2861d6a

Please sign in to comment.