private void SetRenderTarget(D3D11.ID3D11Texture2D *target) { DXGI.IDXGIResource *pDXGIResource; var dxgiResourceGuid = DXGI.IDXGIResource.Guid; target->QueryInterface(ref dxgiResourceGuid, (void **)&pDXGIResource); D3D11.Texture2DDesc texture2DDescription = default; target->GetDesc(ref texture2DDescription); void *sharedHandle; var hr = pDXGIResource->GetSharedHandle(&sharedHandle); SilkMarshal.ThrowHResult(hr); var d3d9 = D3D9.D3D9.GetApi(); D3D9.IDirect3D9Ex *pDirect3D9Ex; hr = d3d9.Direct3DCreate9Ex(SDKVersion: 32, &pDirect3D9Ex); SilkMarshal.ThrowHResult(hr); var d3DContext = pDirect3D9Ex; _pDirect3D9Ex = pDirect3D9Ex; var presentParameters = new D3D9.PresentParameters() { Windowed = 1,// true SwapEffect = D3D9.Swapeffect.SwapeffectDiscard, HDeviceWindow = GetDesktopWindow(), PresentationInterval = D3D9.D3D9.PresentIntervalDefault, }; // 设置使用多线程方式,这样的性能才足够 uint createFlags = D3D9.D3D9.CreateHardwareVertexprocessing | D3D9.D3D9.CreateMultithreaded | D3D9.D3D9.CreateFpuPreserve; D3D9.IDirect3DDevice9Ex *pDirect3DDevice9Ex; hr = d3DContext->CreateDeviceEx(Adapter: 0, DeviceType: D3D9.Devtype.DevtypeHal,// 使用硬件渲染 hFocusWindow: IntPtr.Zero, createFlags, ref presentParameters, pFullscreenDisplayMode: (D3D9.Displaymodeex *)IntPtr.Zero, &pDirect3DDevice9Ex); SilkMarshal.ThrowHResult(hr); var d3DDevice = pDirect3DDevice9Ex; D3D9.IDirect3DTexture9 *pDirect3DTexture9; hr = d3DDevice->CreateTexture(texture2DDescription.Width, texture2DDescription.Height, Levels: 1, D3D9.D3D9.UsageRendertarget, D3D9.Format.FmtA8R8G8B8, // 这是必须要求的颜色,不能使用其他颜色 D3D9.Pool.PoolDefault, &pDirect3DTexture9, &sharedHandle); SilkMarshal.ThrowHResult(hr); _renderTarget = pDirect3DTexture9; D3D9.IDirect3DSurface9 *pDirect3DSurface9; _renderTarget->GetSurfaceLevel(0, &pDirect3DSurface9); _pDirect3DSurface9 = pDirect3DSurface9; D3DImage.Lock(); D3DImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, new IntPtr(pDirect3DSurface9)); D3DImage.Unlock(); }
/// <summary>To be documented.</summary> public readonly unsafe int CreateDevice(uint Adapter, Devtype DeviceType, nint hFocusWindow, uint BehaviorFlags, ref PresentParameters pPresentationParameters, ref IDirect3DDevice9 *ppReturnedDeviceInterface) { var @this = (IDirect3D9 *)Unsafe.AsPointer(ref Unsafe.AsRef(in this)); int ret = default; fixed(PresentParameters *pPresentationParametersPtr = &pPresentationParameters) { fixed(IDirect3DDevice9 **ppReturnedDeviceInterfacePtr = &ppReturnedDeviceInterface) { ret = ((delegate * unmanaged[Cdecl] < IDirect3D9 *, uint, Devtype, nint, uint, PresentParameters *, IDirect3DDevice9 **, int >)LpVtbl[16])(@this, Adapter, DeviceType, hFocusWindow, BehaviorFlags, pPresentationParametersPtr, ppReturnedDeviceInterfacePtr); } } return(ret); }