From 7b7fd99b92abd856a8a41eaedf3d89afa006212b Mon Sep 17 00:00:00 2001 From: chyyran Date: Sat, 28 Sep 2024 01:58:24 -0400 Subject: [PATCH] rt(d3d11): take viewport by reference to avoid AddRef/Release --- .../src/runtime/d3d11/filter_chain.rs | 4 +- librashader-cli/src/render/d3d11.rs | 2 +- librashader-common/src/d3d11.rs | 66 ++++++++++++------- librashader-runtime-d3d11/src/filter_chain.rs | 2 +- librashader-runtime-d3d11/src/filter_pass.rs | 2 +- .../tests/hello_triangle/mod.rs | 2 +- 6 files changed, 47 insertions(+), 31 deletions(-) diff --git a/librashader-capi/src/runtime/d3d11/filter_chain.rs b/librashader-capi/src/runtime/d3d11/filter_chain.rs index 579f8aa..46a8e47 100644 --- a/librashader-capi/src/runtime/d3d11/filter_chain.rs +++ b/librashader-capi/src/runtime/d3d11/filter_chain.rs @@ -241,14 +241,14 @@ extern_fn! { }; let viewport = if viewport.is_null() { - Viewport::new_render_target_sized_origin(ManuallyDrop::into_inner(out.clone()), mvp) + Viewport::new_render_target_sized_origin(out.deref(), mvp) .map_err(|e| LibrashaderError::D3D11FilterError(FilterChainError::Direct3DError(e)))? } else { let viewport = unsafe { viewport.read() }; Viewport { x: viewport.x, y: viewport.y, - output: ManuallyDrop::into_inner(out.clone()), + output: out.deref(), size: Size { height: viewport.height, width: viewport.width diff --git a/librashader-cli/src/render/d3d11.rs b/librashader-cli/src/render/d3d11.rs index a51890e..7bde394 100644 --- a/librashader-cli/src/render/d3d11.rs +++ b/librashader-cli/src/render/d3d11.rs @@ -36,7 +36,7 @@ impl RenderTest for Direct3D11 { if let Some(setter) = param_setter { setter(filter_chain.parameters()); } - let viewport = Viewport::new_render_target_sized_origin(rtv, None)?; + let viewport = Viewport::new_render_target_sized_origin(&rtv, None)?; let options = frame_options.map(|options| FrameOptions { clear_history: options.clear_history, frame_direction: options.frame_direction, diff --git a/librashader-common/src/d3d11.rs b/librashader-common/src/d3d11.rs index dc1b0d5..c2b4a43 100644 --- a/librashader-common/src/d3d11.rs +++ b/librashader-common/src/d3d11.rs @@ -23,7 +23,7 @@ impl From for Direct3D11::D3D11_FILTER { } } -impl GetSize for Direct3D11::ID3D11RenderTargetView { +impl GetSize for &Direct3D11::ID3D11RenderTargetView { type Error = windows::core::Error; fn size(&self) -> Result, Self::Error> { @@ -55,33 +55,49 @@ impl GetSize for Direct3D11::ID3D11RenderTargetView { } } +impl GetSize for Direct3D11::ID3D11RenderTargetView { + type Error = windows::core::Error; + + fn size(&self) -> Result, Self::Error> { + <&Self as GetSize>::size(&self) + } +} + +impl GetSize for &Direct3D11::ID3D11ShaderResourceView { + type Error = windows::core::Error; + + fn size(&self) -> Result, Self::Error> { + let parent: ID3D11Texture2D = unsafe { + let resource = self.GetResource()?; + if resource.GetType() != D3D11_RESOURCE_DIMENSION_TEXTURE2D { + return Err(windows::core::Error::new( + E_NOINTERFACE, + "expected ID3D11Texture2D as the resource for the view.", + )); + } + // SAFETY: We know tha the resource is an `ID3D11Texture2D`. + // This downcast is safe because ID3D11Texture2D has ID3D11Resource in its + // inheritance chain. + // + // This check + transmute is cheaper than doing `.cast` (i.e. `QueryInterface`). + std::mem::transmute(resource) + }; + let mut desc = Default::default(); + unsafe { + parent.GetDesc(&mut desc); + } + + Ok(Size { + height: desc.Height, + width: desc.Width, + }) + } +} + impl GetSize for Direct3D11::ID3D11ShaderResourceView { type Error = windows::core::Error; fn size(&self) -> Result, Self::Error> { - let parent: ID3D11Texture2D = unsafe { - let resource = self.GetResource()?; - if resource.GetType() != D3D11_RESOURCE_DIMENSION_TEXTURE2D { - return Err(windows::core::Error::new( - E_NOINTERFACE, - "expected ID3D11Texture2D as the resource for the view.", - )); - } - // SAFETY: We know tha the resource is an `ID3D11Texture2D`. - // This downcast is safe because ID3D11Texture2D has ID3D11Resource in its - // inheritance chain. - // - // This check + transmute is cheaper than doing `.cast` (i.e. `QueryInterface`). - std::mem::transmute(resource) - }; - let mut desc = Default::default(); - unsafe { - parent.GetDesc(&mut desc); - } - - Ok(Size { - height: desc.Height, - width: desc.Width, - }) + <&Self as GetSize>::size(&self) } } diff --git a/librashader-runtime-d3d11/src/filter_chain.rs b/librashader-runtime-d3d11/src/filter_chain.rs index acd281b..2660d42 100644 --- a/librashader-runtime-d3d11/src/filter_chain.rs +++ b/librashader-runtime-d3d11/src/filter_chain.rs @@ -401,7 +401,7 @@ impl FilterChainD3D11 { &mut self, ctx: Option<&ID3D11DeviceContext>, input: &ID3D11ShaderResourceView, - viewport: &Viewport, + viewport: &Viewport<&ID3D11RenderTargetView>, frame_count: usize, options: Option<&FrameOptionsD3D11>, ) -> error::Result<()> { diff --git a/librashader-runtime-d3d11/src/filter_pass.rs b/librashader-runtime-d3d11/src/filter_pass.rs index 92fe036..20bebe6 100644 --- a/librashader-runtime-d3d11/src/filter_pass.rs +++ b/librashader-runtime-d3d11/src/filter_pass.rs @@ -151,7 +151,7 @@ impl FilterPass { parent: &FilterCommon, frame_count: u32, options: &FrameOptionsD3D11, - viewport: &Viewport, + viewport: &Viewport<&ID3D11RenderTargetView>, original: &InputTexture, source: &InputTexture, output: RenderTarget, diff --git a/librashader-runtime-d3d11/tests/hello_triangle/mod.rs b/librashader-runtime-d3d11/tests/hello_triangle/mod.rs index 98120eb..e6d4c76 100644 --- a/librashader-runtime-d3d11/tests/hello_triangle/mod.rs +++ b/librashader-runtime-d3d11/tests/hello_triangle/mod.rs @@ -569,7 +569,7 @@ pub mod d3d11_hello_triangle { let srv = input_srv.unwrap(); // eprintln!("w: {} h: {}", backbuffer_desc.Width, backbuffer_desc.Height); - let output = resources.backbuffer_rtv.as_ref().unwrap().clone(); + let output = resources.backbuffer_rtv.as_ref().unwrap(); let size: Size = output.size().unwrap(); self.filter .frame(