From 2fe7702957c7fe58b2ec725adc907a7ebaf9d308 Mon Sep 17 00:00:00 2001 From: chyyran Date: Sat, 28 Sep 2024 01:07:55 -0400 Subject: [PATCH] rt(d3d11): avoid QueryInterface in GetSize --- librashader-common/src/d3d11.rs | 36 +++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/librashader-common/src/d3d11.rs b/librashader-common/src/d3d11.rs index cb7015d..dc1b0d5 100644 --- a/librashader-common/src/d3d11.rs +++ b/librashader-common/src/d3d11.rs @@ -1,6 +1,7 @@ use crate::{FilterMode, GetSize, Size, WrapMode}; -use windows::core::Interface; +use windows::Win32::Foundation::E_NOINTERFACE; use windows::Win32::Graphics::Direct3D11; +use windows::Win32::Graphics::Direct3D11::{ID3D11Texture2D, D3D11_RESOURCE_DIMENSION_TEXTURE2D}; impl From for Direct3D11::D3D11_TEXTURE_ADDRESS_MODE { fn from(value: WrapMode) -> Self { @@ -26,7 +27,21 @@ impl GetSize for Direct3D11::ID3D11RenderTargetView { type Error = windows::core::Error; fn size(&self) -> Result, Self::Error> { - let parent = unsafe { self.GetResource()?.cast::()? }; + 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 { @@ -44,8 +59,21 @@ impl GetSize for Direct3D11::ID3D11ShaderResourceView { type Error = windows::core::Error; fn size(&self) -> Result, Self::Error> { - let parent = unsafe { self.GetResource()?.cast::()? }; - + 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);