wgpu: Don't create an extra fresh texture for applying filters to a CAB

This commit is contained in:
Nathan Adams 2023-07-20 01:23:31 +02:00
parent e66f2397b5
commit b201e19cc7
5 changed files with 35 additions and 6 deletions

View File

@ -747,6 +747,7 @@ pub fn render_base<'gc>(this: DisplayObject<'gc>, context: &mut RenderContext<'_
let bounds: Rectangle<Twips> = this.bounds_with_transform(&base_transform.matrix);
let name = this.name();
let mut filters: Vec<Filter> = this.filters();
filters.retain(|f| !f.impotent());
if let Some(cache) = this.base_mut(context.gc_context).bitmap_cache_mut() {
let width = bounds.width().to_pixels().ceil().max(0.0);

View File

@ -59,6 +59,17 @@ impl Filter {
_ => {}
}
}
/// Checks if this filter is impotent.
/// Impotent filters will have no effect if applied, and can safely be skipped.
pub fn impotent(&self) -> bool {
// TODO: There's more cases here, find them!
match self {
Filter::BlurFilter(filter) => filter.impotent(),
Filter::ColorMatrixFilter(filter) => filter.impotent(),
_ => false,
}
}
}
impl From<&swf::Filter> for Filter {

View File

@ -548,13 +548,20 @@ impl<T: RenderTarget + 'static> RenderBackend for WgpuRenderBackend<T> {
&mut self.offscreen_texture_pool,
);
} else {
// We're relying on there being no impotent filters here,
// so that we can safely start by using the actual CAB texture.
// It's guaranteed that at least one filter would have used it and moved the target to something else,
// letting us safely copy back to it later.
let mut target = surface.draw_commands(
RenderTargetMode::FreshWithColor(wgpu::Color {
r: f64::from(entry.clear.r) / 255.0,
g: f64::from(entry.clear.g) / 255.0,
b: f64::from(entry.clear.b) / 255.0,
a: f64::from(entry.clear.a) / 255.0,
}),
RenderTargetMode::ExistingWithColor(
texture.texture.clone(),
wgpu::Color {
r: f64::from(entry.clear.r) / 255.0,
g: f64::from(entry.clear.g) / 255.0,
b: f64::from(entry.clear.b) / 255.0,
a: f64::from(entry.clear.a) / 255.0,
},
),
&self.descriptors,
&self.meshes,
entry.commands,

View File

@ -18,6 +18,10 @@ impl BlurFilter {
self.blur_x *= Fixed16::from_f32(x);
self.blur_y *= Fixed16::from_f32(y);
}
pub fn impotent(&self) -> bool {
self.blur_x == Fixed16::ZERO && self.blur_y == Fixed16::ZERO
}
}
bitflags! {

View File

@ -15,3 +15,9 @@ impl Default for ColorMatrixFilter {
}
}
}
impl ColorMatrixFilter {
pub fn impotent(&self) -> bool {
self == &Default::default()
}
}