Jump to content
  • Advertisement
Sign in to follow this  

Render to a texture in D2D+DWrite+SharpDX

This topic is 1422 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


At last I setup my D2D render target for drawing directly to backbuffer.

I did it like this:

private void Initialize()
            var device = (SharpDX.Direct3D11.Device)_graphicsDevice;
            using (var dxgiDevice = device.QueryInterface<SharpDX.DXGI.Device>())
                DeviceCreationFlags flags = new DeviceCreationFlags();

                d2dDevice = ToDispose(new SharpDX.Direct2D1.Device(dxgiDevice, new CreationProperties() { DebugLevel = DebugLevel.Information }));
                d2dContext = ToDispose(new SharpDX.Direct2D1.DeviceContext(d2dDevice, DeviceContextOptions.None));

            _directWriteFactory = ToDispose(new SharpDX.DirectWrite.Factory1());

            textBrush = new SolidColorBrush(d2dContext, Color4.White);
            textFormat = new TextFormat(_directWriteFactory, "Cambria", 24.0f)
                FlowDirection = FlowDirection.TopToBottom,
                ParagraphAlignment = ParagraphAlignment.Center,
                TextAlignment = TextAlignment.Center,
                WordWrapping = WordWrapping.WholeWord
            var parameters = new PresentationParameters((int)swapchainPanel.ActualWidth, (int)swapchainPanel.ActualHeight, swapchainPanel);
            _presenter = ToDispose(new SwapChainGraphicsPresenter(_graphicsDevice, parameters));
            _graphicsDevice.Presenter = _presenter;

        private void CreateD2DRenderTarget()
            var renderTarget = _presenter.BackBuffer;
            var dpi = DisplayProperties.LogicalDpi;

            // 1. Use same format as the underlying render target with premultiplied alpha mode
            // 2. Use correct DPI
            // 3. Deny drawing direct calls and specify that this is a render target.
            var bitmapProperties = new BitmapProperties1(new SharpDX.Direct2D1.PixelFormat(renderTarget.Format, SharpDX.Direct2D1.AlphaMode.Premultiplied),
                                                             BitmapOptions.CannotDraw | BitmapOptions.Target);

            // create the bitmap render target and assign it to the device context
            d2dTarget = ToDispose(new Bitmap1(d2dContext, renderTarget, bitmapProperties));
            d2dContext.Target = d2dTarget;

But now I want to render NOT not backbuffer, but to texture, so I tried to change render target from _presenter.BackBuffer to my loaded texture2d, but when It runs, framework says:

D2D DEBUG ERROR - The bitmap options [0x5] must be a subset of the flags associated with the DXGI surface.


What I am missing here and what I need to do to draw text or whatever d2d content to a texture (2d or other) to apply it after that to the object, for example?


Does someone alredy did this before in sharpDX?

Share this post

Link to post
Share on other sites

The message is pretty explicit. Your render target is not compatible with what is expecting D2D.


As you haven't posted the declaration of your render target, it is impossible to make a clear diagnostic, but when you say "my loaded texture2d,", this is suspicious. This must be a texture declared as a render target, not a texture that you load from the disk.

Share this post

Link to post
Share on other sites


I made it work by casting Toolkit texture2d to Direct3d11.Texture2d, then queryintefrace<Surface1> and use this surface when creating Bitmap1.


Everything works fine!

Edited by BlackJoker

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!