code style

This commit is contained in:
DismissedLight
2024-05-04 18:59:29 +08:00
parent 08a3db7dc9
commit 3b8eba3bb1
9 changed files with 79 additions and 78 deletions

View File

@@ -42,6 +42,8 @@ internal sealed partial class TestViewModel : Abstraction.ViewModel
private readonly ITaskContext taskContext;
private readonly MainWindow mainWindow;
private long counter;
private UploadAnnouncement announcement = new();
public UploadAnnouncement Announcement { get => announcement; set => SetProperty(ref announcement, value); }
@@ -157,6 +159,8 @@ internal sealed partial class TestViewModel : Abstraction.ViewModel
[Command("TestWindowsGraphicsCaptureCommand")]
private unsafe void TestWindowsGraphicsCapture()
{
counter = 0;
// https://github.com/obsproject/obs-studio/blob/master/libobs-winrt/winrt-capture.cpp
if (!UniversalApiContract.IsPresent(WindowsVersion.Windows10Version1903))
{
@@ -172,8 +176,7 @@ internal sealed partial class TestViewModel : Abstraction.ViewModel
D3D11_CREATE_DEVICE_FLAG flag = D3D11_CREATE_DEVICE_FLAG.D3D11_CREATE_DEVICE_BGRA_SUPPORT | D3D11_CREATE_DEVICE_FLAG.D3D11_CREATE_DEVICE_DEBUG;
ID3D11Device* pD3D11Device = default;
if (SUCCEEDED(D3D11CreateDevice(default, D3D_DRIVER_TYPE.D3D_DRIVER_TYPE_HARDWARE, default, flag, default, 0, D3D11_SDK_VERSION, &pD3D11Device, default, default)))
if (SUCCEEDED(D3D11CreateDevice(default, D3D_DRIVER_TYPE.D3D_DRIVER_TYPE_HARDWARE, default, flag, [], D3D11_SDK_VERSION, out ID3D11Device* pD3D11Device, out _, out _)))
{
if (SUCCEEDED(IUnknownMarshal.QueryInterface(pD3D11Device, in IDXGIDevice.IID, out IDXGIDevice* pDXGIDevice)))
{
@@ -182,31 +185,36 @@ internal sealed partial class TestViewModel : Abstraction.ViewModel
IDirect3DDevice direct3DDevice = WinRT.CastExtensions.As<IDirect3DDevice>(WinRT.IInspectable.FromAbi((nint)inspectable));
SizeInt32 size = new(1920, 1080);
Direct3D11CaptureFramePool framePool = Direct3D11CaptureFramePool.CreateFreeThreaded(direct3DDevice, DirectXPixelFormat.B8G8R8A8UIntNormalized, 2, size);
GC.KeepAlive(framePool);
framePool.FrameArrived += (pool, obj) =>
using (Direct3D11CaptureFramePool framePool = Direct3D11CaptureFramePool.CreateFreeThreaded(direct3DDevice, DirectXPixelFormat.B8G8R8A8UIntNormalized, 2, size))
{
using (Direct3D11CaptureFrame frame = framePool.TryGetNextFrame())
framePool.FrameArrived += (pool, obj) =>
{
if (frame is not null)
Interlocked.Increment(ref counter);
using (Direct3D11CaptureFrame frame = framePool.TryGetNextFrame())
{
logger.LogInformation("Content Size: {Width} x {Height}", frame.ContentSize.Width, frame.ContentSize.Height);
}
else
{
logger.LogInformation("Null Frame");
if (frame is not null)
{
logger.LogInformation("Content Size: {Width} x {Height} {Count}", frame.ContentSize.Width, frame.ContentSize.Height, Volatile.Read(ref counter));
}
else
{
logger.LogInformation("Null Frame");
}
}
};
HWND hwnd = serviceProvider.GetRequiredService<ICurrentWindowReference>().GetWindowHandle();
GraphicsCaptureItem.As<IGraphicsCaptureItemInterop>().CreateForWindow(hwnd, out GraphicsCaptureItem item);
using (GraphicsCaptureSession captureSession = framePool.CreateCaptureSession(item))
{
captureSession.IsCursorCaptureEnabled = false;
captureSession.IsBorderRequired = false;
captureSession.StartCapture();
Thread.Sleep(1000);
}
};
HWND hwnd = serviceProvider.GetRequiredService<ICurrentWindowReference>().GetWindowHandle();
GraphicsCaptureItem.As<IGraphicsCaptureItemInterop>().CreateForWindow(hwnd, out GraphicsCaptureItem item);
GraphicsCaptureSession captureSession = framePool.CreateCaptureSession(item);
captureSession.StartCapture();
Thread.Sleep(1000);
}
}
else
{
@@ -224,7 +232,5 @@ internal sealed partial class TestViewModel : Abstraction.ViewModel
{
logger.LogWarning("D3D11CreateDevice failed");
}
_ = 1;
}
}

View File

@@ -29,4 +29,21 @@ internal static class D3D11
[DllImport("d3d11.dll", CallingConvention = CallingConvention.Winapi, ExactSpelling = true)]
public static unsafe extern HRESULT D3D11CreateDevice([AllowNull] IDXGIAdapter* pAdapter, D3D_DRIVER_TYPE DriverType, HMODULE Software, D3D11_CREATE_DEVICE_FLAG Flags, [AllowNull] D3D_FEATURE_LEVEL* pFeatureLevels, uint FeatureLevels, uint SDKVersion, [MaybeNull] ID3D11Device** ppDevice, [MaybeNull] D3D_FEATURE_LEVEL* pFeatureLevel, [MaybeNull] ID3D11DeviceContext** ppImmediateContext);
public static unsafe HRESULT D3D11CreateDevice([AllowNull] IDXGIAdapter* pAdapter, D3D_DRIVER_TYPE DriverType, HMODULE Software, D3D11_CREATE_DEVICE_FLAG Flags, [AllowNull] ReadOnlySpan<D3D_FEATURE_LEVEL> featureLevels, uint SDKVersion, out ID3D11Device* pDevice, out D3D_FEATURE_LEVEL featureLevel, out ID3D11DeviceContext* pImmediateContext)
{
fixed (ID3D11Device** ppDevice = &pDevice)
{
fixed (D3D_FEATURE_LEVEL* pFeatureLevels = featureLevels)
{
fixed (D3D_FEATURE_LEVEL* pFeatureLevel = &featureLevel)
{
fixed (ID3D11DeviceContext** ppImmediateContext = &pImmediateContext)
{
return D3D11CreateDevice(pAdapter, DriverType, Software, Flags, pFeatureLevels, (uint)featureLevels.Length, SDKVersion, ppDevice, pFeatureLevel, ppImmediateContext);
}
}
}
}
}
}

View File

@@ -1,29 +0,0 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using Snap.Hutao.Win32.Foundation;
using Snap.Hutao.Win32.Graphics.Direct3D;
using Snap.Hutao.Win32.System.Com;
using System.Runtime.InteropServices;
namespace Snap.Hutao.Win32;
[SuppressMessage("", "SA1313")]
[SuppressMessage("", "SYSLIB1054")]
internal static class D3D12
{
[DllImport("d3d12.dll", CallingConvention = CallingConvention.Winapi, ExactSpelling = true)]
public static unsafe extern HRESULT D3D12CreateDevice([AllowNull] IUnknown* pAdapter, D3D_FEATURE_LEVEL MinimumFeatureLevel, Guid* riid, void** ppDevice);
public static unsafe HRESULT D3D12CreateDevice<T>([AllowNull] IUnknown* pAdapter, D3D_FEATURE_LEVEL MinimumFeatureLevel, ref readonly Guid riid, [MaybeNull] out T* pDevice)
where T : unmanaged
{
fixed (Guid* riid2 = &riid)
{
fixed (T** pDevice2 = &pDevice)
{
return D3D12CreateDevice(pAdapter, MinimumFeatureLevel, riid2, (void**)pDevice2);
}
}
}
}

View File

@@ -7,5 +7,5 @@ internal enum D3D11_CULL_MODE
{
D3D11_CULL_NONE = 1,
D3D11_CULL_FRONT = 2,
D3D11_CULL_BACK = 3
D3D11_CULL_BACK = 3,
}

View File

@@ -24,5 +24,5 @@ internal enum D3D11_RESOURCE_MISC_FLAG : uint
D3D11_RESOURCE_MISC_TILED = 0x40000,
D3D11_RESOURCE_MISC_HW_PROTECTED = 0x80000,
D3D11_RESOURCE_MISC_SHARED_DISPLAYABLE = 0x100000,
D3D11_RESOURCE_MISC_SHARED_EXCLUSIVE_WRITER = 0x200000
D3D11_RESOURCE_MISC_SHARED_EXCLUSIVE_WRITER = 0x200000,
}

View File

@@ -6,7 +6,6 @@ namespace Snap.Hutao.Win32.Graphics.Direct3D11;
[SuppressMessage("", "SA1307")]
internal struct D3D11_SUBRESOURCE_DATA
{
// [Const]
public unsafe void* pSysMem;
public uint SysMemPitch;
public uint SysMemSlicePitch;

View File

@@ -8,5 +8,5 @@ internal enum D3D11_USAGE
D3D11_USAGE_DEFAULT = 0,
D3D11_USAGE_IMMUTABLE = 1,
D3D11_USAGE_DYNAMIC = 2,
D3D11_USAGE_STAGING = 3
D3D11_USAGE_STAGING = 3,
}

View File

@@ -26,25 +26,4 @@ internal unsafe readonly struct ID3D11RasterizerState
internal readonly ID3D11DeviceChild.Vftbl ID3D11DeviceChildVftbl;
internal readonly delegate* unmanaged[Stdcall]<ID3D11RasterizerState*, D3D11_RASTERIZER_DESC*, void> GetDesc;
}
}
[SupportedOSPlatform("windows6.1")]
internal unsafe readonly struct ID3D11SamplerState
{
public readonly Vftbl* ThisPtr;
internal static ref readonly Guid IID
{
get
{
ReadOnlySpan<byte> data = [0x51, 0xEA, 0x6F, 0xDA, 0x4C, 0x56, 0x87, 0x44, 0x98, 0x10, 0xF0, 0xD0, 0xF9, 0xB4, 0xE3, 0xA5];
return ref Unsafe.As<byte, Guid>(ref MemoryMarshal.GetReference(data));
}
}
internal readonly struct Vftbl
{
internal readonly ID3D11DeviceChild.Vftbl ID3D11DeviceChildVftbl;
internal readonly delegate* unmanaged[Stdcall]<ID3D11SamplerState*, D3D11_SAMPLER_DESC*, void> GetDesc;
}
}

View File

@@ -0,0 +1,29 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
namespace Snap.Hutao.Win32.Graphics.Direct3D11;
[SupportedOSPlatform("windows6.1")]
internal unsafe readonly struct ID3D11SamplerState
{
public readonly Vftbl* ThisPtr;
internal static ref readonly Guid IID
{
get
{
ReadOnlySpan<byte> data = [0x51, 0xEA, 0x6F, 0xDA, 0x4C, 0x56, 0x87, 0x44, 0x98, 0x10, 0xF0, 0xD0, 0xF9, 0xB4, 0xE3, 0xA5];
return ref Unsafe.As<byte, Guid>(ref MemoryMarshal.GetReference(data));
}
}
internal readonly struct Vftbl
{
internal readonly ID3D11DeviceChild.Vftbl ID3D11DeviceChildVftbl;
internal readonly delegate* unmanaged[Stdcall]<ID3D11SamplerState*, D3D11_SAMPLER_DESC*, void> GetDesc;
}
}