Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

SharpDX DirectWrite problem

Started by BlackJoker Aug 2, 2014 at 1:12 PM 7 replies 4.3k views
Original Post
BlackJoker
BlackJoker

I am using sharpdx and trying to get to work with directwrite.

I initialize it and try to draw text. Everything must be ok, but there is no text at screen at all.

Here is code of initialization and using:


var _device = (SharpDX.Direct3D11.Device)GraphicsDevice;

                SharpDX.Direct2D1.Device d2dDevice = new SharpDX.Direct2D1.Device(_device.QueryInterface<SharpDX.DXGI.Device2>());
                d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.EnableMultithreadedOptimizations);

                // Specify the properties for the bitmap that we will use as the target of our Direct2D operations.
                // We want a 32-bit BGRA surface with premultiplied alpha.
                BitmapProperties1 properties = new BitmapProperties1(new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied),
                    DisplayProperties.LogicalDpi, DisplayProperties.LogicalDpi, BitmapOptions.Target | BitmapOptions.CannotDraw);

                // Get the default surface as a backbuffer and create the Bitmap1 that will hold the Direct2D drawing target.
                //Surface backBuffer = swapChain.GetBackBuffer<Surface>(0);
                d2dTarget = new Bitmap1(d2dContext, GraphicsDevice.BackBuffer, properties);

                fontFactory = new SharpDX.DirectWrite.Factory();
                textFormat = new TextFormat(fontFactory, "Segoe UI", 24.0f);

                textLayout1 = new TextLayout(fontFactory, "This is an example of a moving TextLayout object with snapped pixel boundaries.", textFormat, 400.0f, 200.0f);
                textBrush = new SolidColorBrush(d2dContext, Color.Black);
                backgroundBrush = new SolidColorBrush(d2dContext, Color.White);
                d2dContext.Target = d2dTarget;

And here is drawing code:


protected override void Draw(GameTime gameTime)
        {
            //Debug.WriteLine("Draw");
            if (_modelLoaded)
            {
                // Clears the screen with the Color.CornflowerBlue
                GraphicsDevice.Clear(Color.CornflowerBlue);
                // Setup the vertices
                GraphicsDevice.SetVertexBuffer(vertices);
                GraphicsDevice.SetIndexBuffer(indices, true);
                GraphicsDevice.SetVertexInputLayout(inputLayout);

                // Apply the basic effect technique and draw the rotating cube
                basicEffect.CurrentTechnique.Passes[0].Apply();
                GraphicsDevice.SetRasterizerState(currentRasterizerState);
                //GraphicsDevice.Draw(PrimitiveType.TriangleList, vertices.ElementCount);
                GraphicsDevice.DrawIndexed(PrimitiveType.TriangleList, indices.ElementCount);

                
                // Clear the target. 
                d2dContext.BeginDraw();
                //d2dContext.Clear(Color.CornflowerBlue);

                d2dContext.DrawText("????????", textFormat, new RectangleF(0, 0, 400, 200), textBrush);

                // Draw a block of text that will be clipped against the specified layout rectangle.
                d2dContext.FillRectangle(new RectangleF(50, 50, 200, 200), backgroundBrush);
                d2dContext.DrawText("This text is long enough to overflow the designed region but will be clipped to the containing rectangle. Lorem ipsum dolor sit amet, consectetur adipiscing elit. ", textFormat, new RectangleF(50, 50, 200, 200), textBrush, DrawTextOptions.Clip);

                // Draw a block of text that will overflow the specified layout rectangle.
                d2dContext.FillRectangle(new RectangleF(50, 300, 200, 200), backgroundBrush);
                d2dContext.DrawText("However, this other text isn't going to be clipped: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean gravida dui id accumsan dictum.", textFormat, new RectangleF(50, 300, 200, 200), textBrush, DrawTextOptions.None);

                // Draw three lines of text with different measuring modes.
                d2dContext.FillRectangle(new RectangleF(300, 50, 400, 200), backgroundBrush);
                d2dContext.DrawText("MeasuringMode: Natural", textFormat, new RectangleF(300, 50, 400, 200), textBrush, DrawTextOptions.None, MeasuringMode.Natural);
                d2dContext.DrawText("MeasuringMode: GDI classic", textFormat, new RectangleF(300, 80, 400, 200), textBrush, DrawTextOptions.None, MeasuringMode.GdiClassic);
                d2dContext.DrawText("MeasuringMode: GDI natural", textFormat, new RectangleF(300, 110, 400, 200), textBrush, DrawTextOptions.None, MeasuringMode.GdiNatural);

                //float layoutYOffset = (float)Math.Cos(layoutY) * 50.0f;

                // Draw moving text.
                d2dContext.FillRectangle(new RectangleF(300, 300, 400, 200), backgroundBrush);
                //d2dContext.DrawTextLayout(new Vector2(300, 350 + layoutYOffset), textLayout1, textBrush);

                // Draw moving text without pixel snapping, thus giving a smoother movement.
                d2dContext.FillRectangle(new RectangleF(750, 300, 400, 200), backgroundBrush);
                //d2dContext.DrawTextLayout(new Vector2(750, 350 + layoutYOffset), textLayout2, textBrush, DrawTextOptions.NoSnap);

                d2dContext.EndDraw();
            }
            // Handle base.Draw
            base.Draw(gameTime);
        }

Seems inside d2d there is no exceptions or errors, but still no text. What I am doing wrong here?

P.S. I am using WinRT + Swapchainpanel.

BlackJoker
BlackJoker

Forgot to mention that I use feature level 11_0. I get it work on WIn 7 with DIrectX 11 and I hope that in WinRT version it is also should work

Am I right?

BlackJoker
BlackJoker

Please, help me with it!

cephalo
cephalo

Ok, I went through some similar aches and pains using DirectWrite, and I believe this direct interface between DirectWrite and Direct3D11 surfaces only works on Direct3D11.1 and later. I think feature level 11_0 is fine, but you still need to use the SharpDX libraries specifically for 11.1.

It used to be necessary to use DX10 surfaces for drawing and then converting them to DX11 surfaces. It was all very confusing.

BlackJoker
BlackJoker

Its very strange solution in respect that I successfully setup DirectWrite + D3D11 on C++ with feature level 11.0 and without any convertion of Surfaces to DirectX10 or any other - I just draw directly to backbuffer and that all. Nothing special.

Also I don`t see such manipulations in SharpDX samples.

cephalo
cephalo

I think it has to do with which parts of the SDK are being used by what assembly of SharpDX. Sorry I'm not an expert at these things like some of the guys around here. Are you using the latest SharpDX version? They have different assemblies for different platforms, for example, at the time I wrestled with this, I had to stop using the 'normal' DX11 SharpDX assemblies in favor of the 11_1 assemblies, which at the time were brand new. The difference between them is that the newer one calls into newer versions of dlls from the SDK.

In your C++ stuff, maybe linking to the latest dlls happened without your explicit knowledge.

BlackJoker
BlackJoker

I use latest dll from nuget and use highlevel XNA like API, which appears in latest versions.

AvengerDr
AvengerDr

Did you try to use Visual Studio's Graphics Debugger? In list of events, you should see the calls from Direct2D. It should let you see the vertices being sent to the GPU and any resources. I guess that D2D would cache the text to a texture, so it should be visibile there. What does it show?

BlackJoker
BlackJoker

I cant see anything in graphics debugger unfortunately, but I found interesting issue:

If I create d2drender targer, then my app 100% will crash. even If I comment drawing od D2D content:


d2dTarget = (new Bitmap1(d2dContext, surface, properties));

BEcause of this line app is crashes, for example when I am trying to change rasterisation state.

Does anyone knows is this could be related to DirectWrite issue somehow?

Anybody faced with this earlier?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.