Compare commits

...
Author SHA1 Message Date
DismissedLight 49faf756a1 fix 2023-09-29 22:18:21 +08:00
4 changed files with 19 additions and 13 deletions

No files matched your search

+4 -1
View File
@@ -5,6 +5,9 @@
"emitSingleFile": true,
"public": true,
"comInterop": {
"useIntPtrForComOutPointers": true
"useIntPtrForComOutPointers": true,
"preserveSigMethods": [
"ID3D11Device.GetDeviceRemovedReason"
]
}
}
+3 -1
View File
@@ -31,4 +31,6 @@ IGraphicsCaptureItemInterop
// Constants
D3D11_SDK_VERSION
WM_LBUTTONDOWN
WM_LBUTTONUP
WM_LBUTTONUP
DXGI_ERROR
@@ -70,8 +70,8 @@ namespace Vision.WindowCapture.GraphicsCapture.Helpers
// Wrap the native device using a WinRT interop object.
if (CreateDirect3D11DeviceFromDXGIDevice(d3d11Device.As<IDXGIDevice>(), out Windows.Win32.System.WinRT.IInspectable iInspectable).Succeeded)
{
nint pointer = Marshal.GetIUnknownForObject(iInspectable);
return MarshalInterface<IDirect3DDevice>.FromAbi(pointer);
nint pointer = Marshal.GetIUnknownForObject(iInspectable); // IUnknown
return MarshalInterface<IDirect3DDevice>.FromAbi(pointer); // RCW
}
return default;
@@ -83,8 +83,8 @@ namespace Vision.WindowCapture.GraphicsCapture.Helpers
// Wrap the native device using a WinRT interop object.
if (CreateDirect3D11SurfaceFromDXGISurface(texture.As<IDXGISurface>(), out Windows.Win32.System.WinRT.IInspectable iInspectable).Succeeded)
{
nint pointer = Marshal.GetIUnknownForObject(iInspectable);
return MarshalInterface<IDirect3DSurface>.FromAbi(pointer);
nint pointer = Marshal.GetIUnknownForObject(iInspectable); // IUnknown
return MarshalInterface<IDirect3DSurface>.FromAbi(pointer); // RCW
}
return default;
@@ -95,7 +95,7 @@ namespace Vision.WindowCapture.GraphicsCapture.Helpers
device
.As<IDirect3DDxgiInterfaceAccess>()
.GetInterface(typeof(ID3D11Device).GUID, out var d3dDevice);
return MarshalInterface<ID3D11Device>.FromAbi(d3dDevice);
return (ID3D11Device)Marshal.GetObjectForIUnknown(d3dDevice); // RCW
}
public static ID3D11Texture2D CreateD3D11Texture2D(IDirect3DSurface surface)
@@ -103,7 +103,7 @@ namespace Vision.WindowCapture.GraphicsCapture.Helpers
surface
.As<IDirect3DDxgiInterfaceAccess>()
.GetInterface(typeof(ID3D11Texture2D).GUID, out var d3dSurface);
return MarshalInterface<ID3D11Texture2D>.FromAbi(d3dSurface);
return (ID3D11Texture2D)Marshal.GetObjectForIUnknown(d3dSurface); // RCW
}
}
}
@@ -2,6 +2,7 @@
using System.Drawing.Imaging;
using Windows.Graphics.Capture;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.Graphics.Direct3D11;
using Windows.Win32.Graphics.Dxgi.Common;
using WinRT;
@@ -12,10 +13,8 @@ namespace Vision.WindowCapture.GraphicsCapture.Helpers
{
public static unsafe Bitmap ToBitmap(this Direct3D11CaptureFrame frame)
{
var texture2dBitmap = Direct3D11Helper.CreateD3D11Texture2D(frame.Surface);
texture2dBitmap.GetDevice(out var d3dDevice);
texture2dBitmap.GetDesc(out D3D11_TEXTURE2D_DESC desc);
var texture2d = Direct3D11Helper.CreateD3D11Texture2D(frame.Surface);
texture2d.GetDesc(out D3D11_TEXTURE2D_DESC desc);
D3D11_TEXTURE2D_DESC newDesc = new()
{
Width = (uint)frame.ContentSize.Width,
@@ -29,11 +28,13 @@ namespace Vision.WindowCapture.GraphicsCapture.Helpers
};
// Create texture copy
texture2d.GetDevice(out var d3dDevice);
HRESULT result = d3dDevice.GetDeviceRemovedReason();
d3dDevice.CreateTexture2D(newDesc, default, out ID3D11Texture2D staging);
d3dDevice.GetImmediateContext(out ID3D11DeviceContext context);
// Copy data
context.CopyResource(texture2dBitmap, staging);
context.CopyResource(texture2d, staging);
D3D11_MAPPED_SUBRESOURCE subResource = default;
context.Map(staging, default, D3D11_MAP.D3D11_MAP_READ, default, &subResource);