capi(d3d11): Don't drop passed in COM pointers

This commit is contained in:
chyyran 2023-01-26 23:12:56 -05:00
parent cb614319ff
commit cdf94cee1f
2 changed files with 17 additions and 34 deletions

View file

@ -4,7 +4,7 @@ use crate::ffi::extern_fn;
use librashader::runtime::d3d11::{D3D11InputView, D3D11OutputView}; use librashader::runtime::d3d11::{D3D11InputView, D3D11OutputView};
use std::ffi::c_char; use std::ffi::c_char;
use std::ffi::CStr; use std::ffi::CStr;
use std::mem::MaybeUninit; use std::mem::{ManuallyDrop, MaybeUninit};
use std::ptr::NonNull; use std::ptr::NonNull;
use std::slice; use std::slice;
use windows::Win32::Graphics::Direct3D11::{ use windows::Win32::Graphics::Direct3D11::{
@ -20,7 +20,7 @@ use librashader::runtime::{FilterChainParameters, Size, Viewport};
#[repr(C)] #[repr(C)]
pub struct libra_source_image_d3d11_t { pub struct libra_source_image_d3d11_t {
/// A shader resource view into the source image /// A shader resource view into the source image
pub handle: ID3D11ShaderResourceView, pub handle: ManuallyDrop<ID3D11ShaderResourceView>,
/// The height of the source image. /// The height of the source image.
pub height: u32, pub height: u32,
/// The width of the source image. /// The width of the source image.
@ -34,7 +34,7 @@ impl TryFrom<libra_source_image_d3d11_t> for D3D11InputView {
let handle = value.handle.clone(); let handle = value.handle.clone();
Ok(D3D11InputView { Ok(D3D11InputView {
handle, handle: ManuallyDrop::into_inner(handle),
size: Size::new(value.width, value.height), size: Size::new(value.width, value.height),
}) })
} }
@ -54,7 +54,7 @@ extern_fn! {
fn libra_d3d11_filter_chain_create( fn libra_d3d11_filter_chain_create(
preset: *mut libra_shader_preset_t, preset: *mut libra_shader_preset_t,
options: *const FilterChainOptionsD3D11, options: *const FilterChainOptionsD3D11,
device: ID3D11Device, device: ManuallyDrop<ID3D11Device>,
out: *mut MaybeUninit<libra_d3d11_filter_chain_t> out: *mut MaybeUninit<libra_d3d11_filter_chain_t>
) { ) {
assert_non_null!(preset); assert_non_null!(preset);
@ -101,7 +101,7 @@ extern_fn! {
frame_count: usize, frame_count: usize,
image: libra_source_image_d3d11_t, image: libra_source_image_d3d11_t,
viewport: libra_viewport_t, viewport: libra_viewport_t,
out: ID3D11RenderTargetView, out: ManuallyDrop<ID3D11RenderTargetView>,
mvp: *const f32, mvp: *const f32,
opt: *const FrameOptionsD3D11 opt: *const FrameOptionsD3D11
) mut |chain| { ) mut |chain| {
@ -124,7 +124,7 @@ extern_fn! {
y: viewport.y, y: viewport.y,
output: D3D11OutputView { output: D3D11OutputView {
size: Size::new(viewport.width, viewport.height), size: Size::new(viewport.width, viewport.height),
handle: out.clone(), handle: ManuallyDrop::into_inner(out.clone()),
}, },
mvp, mvp,
}; };

View file

@ -257,7 +257,7 @@ pub mod d3d11_hello_triangle {
pub elapsed: f32, pub elapsed: f32,
triangle_uniform_values: TriangleUniforms, triangle_uniform_values: TriangleUniforms,
pub backbuffer: ID3D11Texture2D, pub backbuffer: ID3D11Texture2D,
pub rtv: ID3D11RenderTargetView, pub backbuffer_rtv: ID3D11RenderTargetView,
pub viewport: D3D11_VIEWPORT, pub viewport: D3D11_VIEWPORT,
pub shader_output: Option<ID3D11Texture2D>, pub shader_output: Option<ID3D11Texture2D>,
pub frame_count: usize, pub frame_count: usize,
@ -325,7 +325,7 @@ pub mod d3d11_hello_triangle {
self.resources = Some(Resources { self.resources = Some(Resources {
swapchain, swapchain,
rtv, backbuffer_rtv: rtv,
backbuffer, backbuffer,
depth_buffer, depth_buffer,
depth_stencil_view, depth_stencil_view,
@ -399,7 +399,7 @@ pub mod d3d11_hello_triangle {
Some(&[resources.triangle_uniforms.clone()]), Some(&[resources.triangle_uniforms.clone()]),
); );
self.context.OMSetRenderTargets( self.context.OMSetRenderTargets(
Some(&[resources.rtv.clone()]), Some(&[resources.backbuffer_rtv.clone()]),
&resources.depth_stencil_view, &resources.depth_stencil_view,
); );
self.context.RSSetViewports(Some(&[resources.viewport])) self.context.RSSetViewports(Some(&[resources.viewport]))
@ -408,7 +408,7 @@ pub mod d3d11_hello_triangle {
unsafe { unsafe {
let color = [0.3, 0.4, 0.6, 1.0]; let color = [0.3, 0.4, 0.6, 1.0];
self.context self.context
.ClearRenderTargetView(&resources.rtv, color.as_ptr()); .ClearRenderTargetView(&resources.backbuffer_rtv, color.as_ptr());
self.context.ClearDepthStencilView( self.context.ClearDepthStencilView(
&resources.depth_stencil_view, &resources.depth_stencil_view,
D3D11_CLEAR_DEPTH.0 as u32, D3D11_CLEAR_DEPTH.0 as u32,
@ -444,7 +444,7 @@ pub mod d3d11_hello_triangle {
unsafe { unsafe {
let mut tex2d_desc = Default::default(); let mut tex2d_desc = Default::default();
resources.backbuffer.GetDesc(&mut tex2d_desc); resources.backbuffer.GetDesc(&mut tex2d_desc);
let mut backup = None; let mut backbuffer_copy = None;
self.device.CreateTexture2D( self.device.CreateTexture2D(
&D3D11_TEXTURE2D_DESC { &D3D11_TEXTURE2D_DESC {
@ -453,13 +453,13 @@ pub mod d3d11_hello_triangle {
..tex2d_desc ..tex2d_desc
}, },
None, None,
Some(&mut backup), Some(&mut backbuffer_copy),
)?; )?;
let backup = backup.unwrap(); let backup = backbuffer_copy.unwrap();
self.context.CopyResource(&backup, &resources.backbuffer); self.context.CopyResource(&backup, &resources.backbuffer);
let mut srv = None; let mut copy_srv = None;
self.device.CreateShaderResourceView( self.device.CreateShaderResourceView(
&backup, &backup,
Some(&D3D11_SHADER_RESOURCE_VIEW_DESC { Some(&D3D11_SHADER_RESOURCE_VIEW_DESC {
@ -472,9 +472,9 @@ pub mod d3d11_hello_triangle {
}, },
}, },
}), }),
Some(&mut srv), Some(&mut copy_srv),
)?; )?;
let srv = srv.unwrap(); let srv = copy_srv.unwrap();
let mut shader_out = None; let mut shader_out = None;
self.device self.device
@ -494,23 +494,6 @@ pub mod d3d11_hello_triangle {
Some(&mut rtv), Some(&mut rtv),
)?; )?;
// OutputFramebuffer {
// rtv: resources.rtv.clone(),
// // rtv,
// size: Size {
// width: tex2d_desc.Width,
// height: tex2d_desc.Height,
// },
// viewport: resources.viewport, // viewport: D3D11_VIEWPORT {
// // TopLeftX: 0.0,
// // TopLeftY: 0.0,
// // Width: tex2d_desc.Width as f32,
// // Height: tex2d_desc.Height as f32,
// // MinDepth: 0.0,
// // MaxDepth: 1.0,
// // },
// },
self.filter self.filter
.frame( .frame(
D3D11InputView { D3D11InputView {
@ -528,7 +511,7 @@ pub mod d3d11_hello_triangle {
width: tex2d_desc.Width, width: tex2d_desc.Width,
height: tex2d_desc.Height, height: tex2d_desc.Height,
}, },
handle: resources.rtv.clone(), handle: resources.backbuffer_rtv.clone(),
}, },
mvp: None, mvp: None,
}, },