use high performance gpu

This commit is contained in:
Lightczx
2024-05-31 15:46:56 +08:00
parent 2c47e7d1da
commit a8baef99d7
4 changed files with 52 additions and 8 deletions

View File

@@ -12,6 +12,7 @@ using Windows.Graphics.DirectX.Direct3D11;
using WinRT;
using static Snap.Hutao.Win32.ConstValues;
using static Snap.Hutao.Win32.D3d11;
using static Snap.Hutao.Win32.Dxgi;
using static Snap.Hutao.Win32.Macros;
namespace Snap.Hutao.Service.Game.Automation.ScreenCapture;
@@ -20,6 +21,20 @@ namespace Snap.Hutao.Service.Game.Automation.ScreenCapture;
[Injection(InjectAs.Singleton, typeof(IGameScreenCaptureService))]
internal sealed partial class GameScreenCaptureService : IGameScreenCaptureService
{
private const uint CreateDXGIFactoryFlag =
#if DEBUG
DXGI_CREATE_FACTORY_DEBUG;
#else
0;
#endif
private const D3D11_CREATE_DEVICE_FLAG D3d11CreateDeviceFlag =
D3D11_CREATE_DEVICE_FLAG.D3D11_CREATE_DEVICE_BGRA_SUPPORT
#if DEBUG
| D3D11_CREATE_DEVICE_FLAG.D3D11_CREATE_DEVICE_DEBUG
#endif
;
private readonly ILogger<GameScreenCaptureService> logger;
public bool IsSupported()
@@ -44,20 +59,34 @@ internal sealed partial class GameScreenCaptureService : IGameScreenCaptureServi
{
session = default;
D3D11_CREATE_DEVICE_FLAG flag = D3D11_CREATE_DEVICE_FLAG.D3D11_CREATE_DEVICE_BGRA_SUPPORT
#if DEBUG
| D3D11_CREATE_DEVICE_FLAG.D3D11_CREATE_DEVICE_DEBUG
#endif
;
HRESULT hr;
hr = D3D11CreateDevice(default, D3D_DRIVER_TYPE.D3D_DRIVER_TYPE_HARDWARE, default, flag, [], D3D11_SDK_VERSION, out ID3D11Device* pD3D11Device, out _, out _);
hr = CreateDXGIFactory2(CreateDXGIFactoryFlag, in IDXGIFactory6.IID, out IDXGIFactory6* factory);
if (FAILED(hr))
{
logger.LogWarning("CreateDXGIFactory2 failed with code: {Code}", hr);
return false;
}
IUnknownMarshal.Release(factory);
hr = factory->EnumAdapterByGpuPreference(0U, DXGI_GPU_PREFERENCE.DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, in IDXGIAdapter.IID, out IDXGIAdapter* adapter);
if (hr != HRESULT.DXGI_ERROR_NOT_FOUND)
{
logger.LogWarning("IDXGIFactory6.EnumAdapterByGpuPreference failed with code: {Code}", hr);
return false;
}
IUnknownMarshal.Release(adapter);
hr = D3D11CreateDevice(adapter, D3D_DRIVER_TYPE.D3D_DRIVER_TYPE_HARDWARE, default, D3d11CreateDeviceFlag, [], D3D11_SDK_VERSION, out ID3D11Device* pD3D11Device, out _, out _);
if (FAILED(hr))
{
logger.LogWarning("D3D11CreateDevice failed with code: {Code}", hr);
return false;
}
IUnknownMarshal.Release(pD3D11Device);
hr = IUnknownMarshal.QueryInterface(pD3D11Device, in IDXGIDevice.IID, out IDXGIDevice* pDXGIDevice);
if (FAILED(hr))
{

View File

@@ -7,6 +7,7 @@ namespace Snap.Hutao.Win32;
internal static class ConstValues
{
public const uint WM_NULL = 0x00000000U;
public const uint DXGI_CREATE_FACTORY_DEBUG = 0x00000001U;
public const uint NOTIFYICON_VERSION = 0x00000003U;
public const uint NOTIFYICON_VERSION_4 = 0x00000004U;
public const uint D3D11_SDK_VERSION = 0x00000007U;

View File

@@ -10,6 +10,7 @@ internal readonly partial struct HRESULT
{
public static readonly HRESULT S_OK = unchecked((int)0x00000000);
public static readonly HRESULT E_FAIL = unchecked((int)0x80004005);
public static readonly HRESULT DXGI_ERROR_NOT_FOUND = unchecked((int)0x887A0002);
public readonly int Value;

View File

@@ -7,7 +7,7 @@ using System.Runtime.InteropServices;
namespace Snap.Hutao.Win32.Graphics.Dxgi;
internal unsafe readonly struct IDXGIFactory6
internal unsafe struct IDXGIFactory6
{
public readonly Vftbl* ThisPtr;
@@ -20,6 +20,19 @@ internal unsafe readonly struct IDXGIFactory6
}
}
[SuppressMessage("", "SA1313")]
public HRESULT EnumAdapterByGpuPreference<T>(uint Adapter, DXGI_GPU_PREFERENCE GpuPreference, ref readonly Guid iid, out T* pvAdapter)
where T : unmanaged
{
fixed (Guid* riid = &iid)
{
fixed (T** ppvAdapter = &pvAdapter)
{
return ThisPtr->EnumAdapterByGpuPreference((IDXGIFactory6*)Unsafe.AsPointer(ref this), Adapter, GpuPreference, riid, (void**)ppvAdapter);
}
}
}
internal readonly struct Vftbl
{
internal readonly IDXGIFactory5.Vftbl IDXGIFactory5Vftbl;