DirectX/SharpDX How to render bitmap drawing to texture off-screen?

This isn’t specifically related to gamedev, but SO seems dead when it comes to questions about DirectX so I thought I’d try asking here:

I’ve been making changes to a desktop recording project found here which uses OutputDuplication. This project originally records the entire desktop to video, my changes instead allow specifying a region of the desktop to record.

My main change to achieve this is implementing a staging texture, and copying frame output to a render texture, which then has the mouse pointer drawn to it via render target, and then copy subresource region of that render texture to the staging texture which then gets written to sink writer.

This works fine, but the original project renders this using a swapchain with a window handle render target. I want to remove the use of this swapchain and render off-screen instead as I don’t want to display the output in window, I only want it written to mp4 file with sink writer, how do I achieve this correctly?

Here is the relevant code for getting the output frame:

private void AcquireFrame() {     SharpDX.DXGI.Resource frame;     OutputDuplicateFrameInformation frameInfo;     OutputDuplication.Value.AcquireNextFrame(500, out frameInfo, out frame);      using(frame) {         if(frameInfo.LastMouseUpdateTime != 0) {             Cursor.Visible = frameInfo.PointerPosition.Visible;             Cursor.Position = frameInfo.PointerPosition.Position;             Cursor.BufferSize = frameInfo.PointerShapeBufferSize;             ComputeCursorBitmap(); //Retrieves data related to mouse pointer and populates Cursor variables         }          //if(frameInfo.LastPresentTime != 0) {         //    swapChain.Value.Present(1, 0); //Removing use of swapchain         //}          if(RecordingState == DuplicatorState.Started && (frameInfo.LastPresentTime != 0 | (frameInfo.LastPresentTime != 0 && Cursor.Bitmap != null))) {             var renderDescription = new Texture2DDescription() {                 CpuAccessFlags = CpuAccessFlags.None,                 BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,                 Format = Format.B8G8R8A8_UNorm,                 Width = SourceSize.Width,                 Height = SourceSize.Height,                 OptionFlags = ResourceOptionFlags.None,                 MipLevels = 1,                 ArraySize = 1,                 SampleDescription = { Count = 1, Quality = 0 },                 Usage = ResourceUsage.Default,             };             videoFrame.RenderTexture = new Texture2D(OutputDevice.Value, renderDescription);              var stagingDescription = new Texture2DDescription() {                 CpuAccessFlags = CpuAccessFlags.Read,                 BindFlags = BindFlags.None,                 Format = Format.B8G8R8A8_UNorm,                 Width = RegionSize.Width,                 Height = RegionSize.Height,                 OptionFlags = ResourceOptionFlags.None,                 MipLevels = 1,                 ArraySize = 1,                 SampleDescription = { Count = 1, Quality = 0 },                 Usage = ResourceUsage.Staging,             };             videoFrame.StagingTexture = new Texture2D(OutputDevice.Value, stagingDescription);              using(var res = frame.QueryInterface<SharpDX.Direct3D11.Resource>()) {                 OutputDevice.Value.ImmediateContext.CopyResource(res, videoFrame.RenderTexture);                  if(Cursor.Visible) {                     using(var fac = new SharpDX.Direct2D1.Factory1()) {                         using(var dxDev = OutputDevice.Value.QueryInterface<SharpDX.DXGI.Device>()) {                             using(var dc = new SharpDX.Direct2D1.DeviceContext(new SharpDX.Direct2D1.Device(fac, dxDev), DeviceContextOptions.EnableMultithreadedOptimizations)) {                                 using(var surface = videoFrame.RenderTexture.QueryInterface<Surface>()) {                                     using(var bmp = new Bitmap1(dc, surface)) {                                         using(Cursor.Bitmap = new Bitmap(dc, Cursor.Size, Cursor.DataPointer, Cursor.Pitch, Cursor.Props)) {                                             dc.Target = bmp;                                             dc.BeginDraw();                                                                                          int captureX = ((Cursor.Position.X - Cursor.Hotspot.X) * SourceSize.Width) / SourceSize.Width;                                             int captureY = ((Cursor.Position.Y - Cursor.Hotspot.Y) * SourceSize.Height) / SourceSize.Height;                                             RawRectangleF rect = new RawRectangleF(captureX, captureY, captureX + (Cursor.Size.Width * SourceSize.Width) / SourceSize.Width, captureY + (Cursor.Size.Height * SourceSize.Height) / SourceSize.Height);                                                                                          dc.DrawBitmap(Cursor.Bitmap, rect, 1, BitmapInterpolationMode.NearestNeighbor);                                                                                          //dc.Flush();                                             dc.EndDraw();                                         }                                     }                                 }                             }                         }                     }                 }                  OutputDevice.Value.ImmediateContext.CopySubresourceRegion(videoFrame.RenderTexture, 0, new ResourceRegion(RegionLocation.X, RegionLocation.Y, 0, RegionSize.Width, RegionSize.Height, 1), videoFrame.StagingTexture, 0, 0, 0);                 videoFrame.RenderTexture.Dispose();             }              WriteVideoSample(videoFrame);         }         OutputDuplication.Value.ReleaseFrame();     } } 

If I remove the swapchain, drawing the bitmap of the mouse pointer to the render texture surface on the Direct2D1 device context breaks. It appears as if it stutters with drawing it, it doesn’t update smoothly each frame.

I found an answer related to my problem here but when I try to add dc.Flush(); after drawing the mouse pointer bitmap, it doesn’t seem to have any affect on the problem.