Direct2d off-screen rendering and hardware acceleration
Posted
by
Goran
on Game Development
See other posts from Game Development
or by Goran
Published on 2011-08-26T07:16:51Z
Indexed on
2011/11/22
18:17 UTC
Read the original article
Hit count: 1196
I'm trying to use direct2d to render images off-screen using WindowsAPICodePack. This is easily achieved using WicBitmapRenderTarget but sadly it's not hardware accelerated.
So I'm trying this route:
- Create direct3d device
- Create texture2d
- Use texture surface to create render target using CreateDxgiSurfaceRenderTarget
- Draw some shapes
While this renders the image it appears GPU isn't being used at all while CPU is used heavily.
Am I doing something wrong? Is there a way to check whether hardware or software rendering is used?
Code sample:
var device = D3DDevice1.CreateDevice1(
null,
DriverType.Hardware,
null,
CreateDeviceOptions.SupportBgra
,FeatureLevel.Ten
);
var txd = new Texture2DDescription();
txd.Width = 256;
txd.Height = 256;
txd.MipLevels = 1;
txd.ArraySize = 1;
txd.Format = Format.B8G8R8A8UNorm; //DXGI_FORMAT_R32G32B32A32_FLOAT;
txd.SampleDescription = new SampleDescription(1,0);
txd.Usage = Usage.Default;
txd.BindingOptions = BindingOptions.RenderTarget | BindingOptions.ShaderResource;
txd.MiscellaneousResourceOptions = MiscellaneousResourceOptions.None;
txd.CpuAccessOptions = CpuAccessOptions.None;
var tx = device.CreateTexture2D(txd);
var srfc = tx.GraphicsSurface;
var d2dFactory = D2DFactory.CreateFactory();
var renderTargetProperties = new RenderTargetProperties
{
PixelFormat = new PixelFormat(Format.Unknown, AlphaMode.Premultiplied),
DpiX = 96,
DpiY = 96,
RenderTargetType = RenderTargetType.Default,
};
using(var renderTarget = d2dFactory.CreateGraphicsSurfaceRenderTarget(srfc, renderTargetProperties))
{
renderTarget.BeginDraw();
var clearColor = new ColorF(1f,1f,1f,1f);
renderTarget.Clear(clearColor);
using (var strokeBrush = renderTarget.CreateSolidColorBrush(new ColorF(0.2f,0.2f,0.2f,1f)))
{
for (var i = 0; i < 100000; i++)
{
renderTarget.DrawEllipse(new Ellipse(new Point2F(i, i), 10, 10), strokeBrush, 2);
}
}
var hr = renderTarget.EndDraw();
}
© Game Development or respective owner