private unsafe IntPtr EndSceneImpl(IntPtr device) { // With multi-viewports, ImGui might call EndScene again; so we need to prevent stack overflow here. if (_endSceneRecursionLock) { Debug.WriteLine($"[DX9 EndScene] Discarding via Recursion Lock"); return(_endSceneHook.OriginalFunction.Value.Invoke(device)); } _endSceneRecursionLock = true; try { var dev = new Device(device); using var swapChain = dev.GetSwapChain(0); var windowHandle = dev.CreationParameters.HFocusWindow; var swapChainHandle = swapChain.PresentParameters.DeviceWindowHandle; windowHandle = windowHandle == IntPtr.Zero ? swapChainHandle : windowHandle; // Ignore windows which don't belong to us. if (!ImguiHook.CheckWindowHandle(windowHandle)) { Debug.WriteLine($"[DX9 EndScene] Discarding Window Handle {(long)windowHandle:X}"); return(_endSceneHook.OriginalFunction.Value.Invoke(device)); } if (!_initialized) { _device = device; _windowHandle = windowHandle; if (_windowHandle == IntPtr.Zero) { return(_endSceneHook.OriginalFunction.Value.Invoke(device)); } Debug.WriteLine($"[DX9 EndScene] Init, Window Handle {(long)windowHandle:X}"); ImguiHook.InitializeWithHandle(windowHandle); ImGui.ImGuiImplDX9Init((void *)device); _initialized = true; } ImGui.ImGuiImplDX9NewFrame(); ImguiHook.NewFrame(); using var drawData = ImGui.GetDrawData(); ImGui.ImGuiImplDX9RenderDrawData(drawData); return(_endSceneHook.OriginalFunction.Value.Invoke(device)); } finally { _endSceneRecursionLock = false; } }
private unsafe IntPtr PresentImpl(IntPtr swapChainPtr, int syncInterval, PresentFlags flags) { if (_presentRecursionLock) { Debug.WriteLine($"[DX11 Present] Discarding via Recursion Lock"); return(_presentHook.OriginalFunction.Value.Invoke(swapChainPtr, syncInterval, flags)); } _presentRecursionLock = true; try { var swapChain = new SwapChain(swapChainPtr); var windowHandle = swapChain.Description.OutputHandle; // Ignore windows which don't belong to us. if (!ImguiHook.CheckWindowHandle(windowHandle)) { Debug.WriteLine($"[DX11 Present] Discarding Window Handle {windowHandle} due to Mismatch"); return(_presentHook.OriginalFunction.Value.Invoke(swapChainPtr, syncInterval, flags)); } // Initialise using var device = swapChain.GetDevice <Device>(); if (!_initialized) { Debug.WriteLine($"[DX11 Present] Init DX11, Window Handle: {windowHandle:X}"); ImguiHook.InitializeWithHandle(windowHandle); ImGui.ImGuiImplDX11Init((void *)device.NativePointer, (void *)device.ImmediateContext.NativePointer); using var backBuffer = swapChain.GetBackBuffer <Texture2D>(0); _renderTargetView = new RenderTargetView(device, backBuffer); _initialized = true; } ImGui.ImGuiImplDX11NewFrame(); ImguiHook.NewFrame(); device.ImmediateContext.OutputMerger.SetRenderTargets(_renderTargetView); using var drawData = ImGui.GetDrawData(); ImGui.ImGuiImplDX11RenderDrawData(drawData); return(_presentHook.OriginalFunction.Value.Invoke(swapChainPtr, syncInterval, flags)); } finally { _presentRecursionLock = false; } }