Sign in to follow this  
BlackJoker

Render to a texture in D2D+DWrite+SharpDX

Recommended Posts

BlackJoker    1328

Hello.

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;
            CreateD2DRenderTarget();
        }

        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),
                                                             dpi,
                                                             dpi,
                                                             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
AlexandreMutel    1105

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
BlackJoker    1328

 

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this