rt(d3d11): avoid QueryInterface in GetSize

This commit is contained in:
chyyran 2024-09-28 01:07:55 -04:00 committed by Ronny Chan
parent a5c8fcf4f8
commit 2fe7702957

View file

@ -1,6 +1,7 @@
use crate::{FilterMode, GetSize, Size, WrapMode}; 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;
use windows::Win32::Graphics::Direct3D11::{ID3D11Texture2D, D3D11_RESOURCE_DIMENSION_TEXTURE2D};
impl From<WrapMode> for Direct3D11::D3D11_TEXTURE_ADDRESS_MODE { impl From<WrapMode> for Direct3D11::D3D11_TEXTURE_ADDRESS_MODE {
fn from(value: WrapMode) -> Self { fn from(value: WrapMode) -> Self {
@ -26,7 +27,21 @@ impl GetSize<u32> for Direct3D11::ID3D11RenderTargetView {
type Error = windows::core::Error; type Error = windows::core::Error;
fn size(&self) -> Result<Size<u32>, Self::Error> { fn size(&self) -> Result<Size<u32>, Self::Error> {
let parent = unsafe { self.GetResource()?.cast::<Direct3D11::ID3D11Texture2D>()? }; 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(); let mut desc = Default::default();
unsafe { unsafe {
@ -44,8 +59,21 @@ impl GetSize<u32> for Direct3D11::ID3D11ShaderResourceView {
type Error = windows::core::Error; type Error = windows::core::Error;
fn size(&self) -> Result<Size<u32>, Self::Error> { fn size(&self) -> Result<Size<u32>, Self::Error> {
let parent = unsafe { self.GetResource()?.cast::<Direct3D11::ID3D11Texture2D>()? }; 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(); let mut desc = Default::default();
unsafe { unsafe {
parent.GetDesc(&mut desc); parent.GetDesc(&mut desc);