Here's my directwrite related code:
public static void Initialize()
{
//Factory1 factory1 = new Factory1();
//Factory1 factory1 = (Factory1) Engine.Device.Factory;
//Adapter1 adapter1 = factory1.GetAdapter1(0);
Adapter1 adapter1 = Engine.Adapter;
SlimDX.Direct3D10_1.Device1 device10_1 = new SlimDX.Direct3D10_1.Device1(adapter1, SlimDX.Direct3D10.DriverType.Hardware, SlimDX.Direct3D10.DeviceCreationFlags.BgraSupport, SlimDX.Direct3D10_1.FeatureLevel.Level_10_0);
d3d11Texture = new Texture2D(Engine.Device, new Texture2DDescription
{
Width = (int)Engine.Viewport.Width,
Height = (int)Engine.Viewport.Height,
MipLevels = 1,
ArraySize = 1,
Format = Format.B8G8R8A8_UNorm,
SampleDescription = new SampleDescription(1, 0),
Usage = ResourceUsage.Default,
BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.KeyedMutex
});
SlimDX.Direct3D10.Texture2D d3d10Texture;
sharedResource = new Resource(d3d11Texture);
d3d10Texture = device10_1.OpenSharedResource<SlimDX.Direct3D10.Texture2D>(sharedResource.SharedHandle);
d3d10Mutex = new KeyedMutex(d3d10Texture);
d3d11Mutex = new KeyedMutex(d3d11Texture);
SlimDX.Direct2D.Factory d2Factory = new SlimDX.Direct2D.Factory(SlimDX.Direct2D.FactoryType.SingleThreaded, DebugLevel.Information);
DirectWriteFactory = new SlimDX.DirectWrite.Factory(SlimDX.DirectWrite.FactoryType.Shared);
Surface surface = d3d10Texture.AsSurface();
RenderTargetProperties rtp = new RenderTargetProperties
{
MinimumFeatureLevel = SlimDX.Direct2D.FeatureLevel.Direct3D10,
Type = RenderTargetType.Hardware,
Usage = RenderTargetUsage.None,
PixelFormat = new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)
};
dwRenderTarget = RenderTarget.FromDXGI(d2Factory, surface, rtp);
DefaultFont = DirectWriteFactory.CreateTextFormat("Arial", FontWeight.Regular, FontStyle.Normal, FontStretch.Normal, 24, "en-us");
DefaultFont.TextAlignment = SlimDX.DirectWrite.TextAlignment.Center;
DefaultFont.ParagraphAlignment = ParagraphAlignment.Center;
//DefaultFont = new TextFormat(DirectWriteFactory, "Arial", FontWeight.Normal, FontStyle.Normal, FontStretch.Normal, 24, "en-US")
//{
// TextAlignment = SlimDX.DirectWrite.TextAlignment.Center,
// ParagraphAlignment = ParagraphAlignment.Center
//};
WhiteBrush = new SolidColorBrush(dwRenderTarget, Color.White);
BlendStateDescription bsd = new BlendStateDescription();
bsd.RenderTargets[0].BlendEnable = true;
bsd.RenderTargets[0].BlendEnable = true;
bsd.RenderTargets[0].SourceBlend = BlendOption.SourceAlpha;
bsd.RenderTargets[0].DestinationBlend = BlendOption.InverseSourceAlpha;
bsd.RenderTargets[0].BlendOperation = BlendOperation.Add;
bsd.RenderTargets[0].SourceBlendAlpha = BlendOption.One;
bsd.RenderTargets[0].DestinationBlendAlpha = BlendOption.Zero;
bsd.RenderTargets[0].BlendOperationAlpha = BlendOperation.Add;
bsd.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
blendStateTransparent = BlendState.FromDescription(Engine.Device, bsd);
var vertices = new DataStream(VertexPositionTexture.SizeInBytes * 4, true, true);
vertices.Write(new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0)));
vertices.Write(new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0)));
vertices.Write(new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1)));
vertices.Write(new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1)));
vertices.Position = 0;
effect = ContentLoader.LoadEffect(@"Content\Effects\FontShader.fx");
layout = new InputLayout(Engine.Device, effect.GetTechniqueByIndex(0).GetPassByIndex(0).Description.Signature, VertexPositionTexture.VertexDeclaration);
vertexBuffer = new Buffer(Engine.Device, vertices, (int)vertices.Length, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
vertices.Close();
//factory1.Dispose();
//adapter1.Dispose();
device10_1.Dispose();
d2Factory.Dispose();
surface.Dispose();
d3d10Texture.Dispose();
//sharedResource.Dispose();
}
public static void DrawString(TextFormat format, string text, Rectangle rect, SlimDX.Direct2D.Brush brush, float rotation, PointF origin, SizeF scale)
{
//return;
dwRenderTarget.Transform = Matrix3x2.Scale(scale, origin) * Matrix3x2.Rotation(rotation, origin);
d3d10Mutex.Acquire(0, 100);
{
dwRenderTarget.BeginDraw();
dwRenderTarget.Clear(new Color4(0, 0, 0, 0));
dwRenderTarget.DrawText(text, format, rect, brush);
dwRenderTarget.EndDraw();
}
d3d10Mutex.Release(0);
d3d11Mutex.Acquire(0, 100);
{
using(var srv = new ShaderResourceView(Engine.Device, d3d11Texture))
{
Engine.Context.InputAssembler.InputLayout = layout;
Engine.Context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
Engine.Context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, VertexPositionTexture.SizeInBytes, 0));
Engine.Context.OutputMerger.BlendState = blendStateTransparent;
effect.GetVariableByName("TextOverlay").AsResource().SetResource(srv);
effect.GetTechniqueByIndex(0).GetPassByIndex(0).Apply(Engine.Context);
Engine.Context.Draw(4, 0);
}
}
d3d11Mutex.Release(0);
}
And a screenshot of what it's doing:
[attachment=9293:RuinValor 2012-06-06 03-07-31-73.png]