faster 2d graphics on windows 8 your app will run faster on windows 8
TRANSCRIPT
www.buildwindows.com
Achieving high performance 2D graphics with Direct2D
Megha JainProgram Manager IIMicrosoft Corporation
PLAT-769T
www.buildwindows.com
Agenda
• What makes your 2D graphics app run faster in Windows 8?
• What are the performance points your app might be sensitive to?
• How to make your app deliver the best graphics performance with Direct2D?
Faster 2D graphics on Windows 8
www.buildwindows.com
BasicTextAnimation sample
demo
www.buildwindows.com
Faster text rendering
Text Render-
ing
0
20
40
60
80
100
120
95.79
46 Windows 8
Windows 7Fra
me R
ate
www.buildwindows.com
GeometryRealization sample
demo
www.buildwindows.com
Faster geometry rendering
512 2048 81920
10
20
30
40
50
60
70
Windows 8Windows 7
Number of Primitives
Fra
me R
ate
www.buildwindows.com
Your app will run faster on Windows 8
www.buildwindows.com
DirectX magazine sample
demo
www.buildwindows.com
Think about opportunities for app-specific optimizations.
How to write the fastest Direct2D app
www.buildwindows.com
How to…
• Animate complex content fast• Clip content to an arbitrary shape fast• Draw text fast• Render Direct2D effects fast• Scale performance on multiple cores• Draw mixed 2D/3D content fast
www.buildwindows.com
How to…
• Animate complex content fast• Clip content to an arbitrary shape fast• Draw text fast• Render Direct2D effects fast• Scale performance on multiple cores• Draw mixed 2D/3D content fast
www.buildwindows.com
Caching techniques
• Full scene caching using a color bitmap
• Per primitive caching using an A8 bitmap and FillOpacityMask() API
Full scene caching
// create a bitmapm_d2dContext->CreateBitmap(size, nullptr, 0,
D2D1::BitmapProperties(D2D1_BITMAP_OPTIONS_TARGET,D2D1::PixelFormat(
DXGI_FORMAT_B8G8R8A8_UNORM,D2D1_ALPHA_MODE_PREMULTIPLIED),
dpiX, dpiY),&sceneBitmap);
// preserve pre-existing targetComPtr<ID2D1Image> oldTarget;m_d2dContext->GetTarget(&oldTarget);
Full scene caching
// render to the sceneBitmapm_d2dContext->SetTarget(sceneBitmap.Get());m_d2dContext->BeginDraw();…m_d2dContext->EndDraw();
// render sceneBitmap to oldTargetm_d2dContext->SetTarget(oldTarget.Get());m_d2dContext->DrawBitmap(sceneBitmap.Get());
Per primitive caching using FillOpacityMask()
• Reuse pre-rendered static geometry/text
• Works for caching anti-aliased content
• Works with changing brush types
• Create the bitmap alpha only
Per primitive caching using FillOpacityMask()
// create an opacity bitmapm_d2dContext->CreateBitmap(size, nullptr, 0,
D2D1::BitmapProperties(D2D1_BITMAP_OPTIONS_TARGET,D2D1::PixelFormat(
DXGI_FORMAT_A8_UNORM,D2D1_ALPHA_MODE_PREMULTIPLIED),
dpiX, dpiY),&opacityBitmap);
// preserve pre-existing targetComPtr<ID2D1Image> oldTarget;m_d2dContext->GetTarget(&oldTarget);
Per primitive caching using FillOpacityMask()
// render to the opacityBitmapm_d2dContext->SetTarget(opacityBitmap.Get());m_d2dContext->BeginDraw();…m_d2dContext->EndDraw();
// call FillOpacityBitmap() m_d2dContext->SetTarget(oldTarget.Get());m_d2dContext->FillOpacityMask(
opacityBitmap.Get(),m_contentBrush().Get(),D2D1_OPACITY_MASK_CONTENT_GRAPHICS);
// enables rasterizing only the changed content1. IDXGISwapChain1::Present1(UINT SyncInterval, UINT
PresentFlags, DXGI_PRESENT_PARAMETERS* pPresentParameters);
2. struct DXGI_PRESENT_PARAMETERS{
UINT DirtyRectsCount;RECT* pDirtyRects;RECT* pScrollRect;POINT* pScrollOffset;
}
Handling scroll animation
www.buildwindows.com
Printing and caching
• Consistent API for both render and print path
• Use command list for caching printing commands
• Keep the same drawing path for both screen and print
www.buildwindows.com
Use bitmaps for caching display output
Use command list for caching print output.
www.buildwindows.com
How to…
• Animate complex content fast• Clip content to an arbitrary shape fast• Draw text fast• Render Direct2D effects fast• Scale performance on multiple cores• Draw mixed 2D/3D content fast
www.buildwindows.com
Clipping to an arbitrary shape
// create the layer resourceComPtr<ID2D1Layer> m_layer;m_d2dContext->CreateLayer(&m_layer);
// render to the layer with the clipping geometrym_d2dContext->PushLayer(
D2D1::LayerParameters(boundsRect,geometricMask),
m_layer.Get());
Clipping to arbitrary geometry- layers
Clipping to arbitrary geometry– FillGeometry()
// create opacity bitmap and render content
// create opacity brush from the opacity bitmap
// call FillGeometry() by passing in the clip geometry and the opacity brush to m_d2dContext->FillGeometry(
clipGeometry.Get(),brush.Get(),opacityBrush.Get());
www.buildwindows.com
Use ID2D1Layer instead of intermediate surfaces whenever
possible.Use NULL layers when possible
www.buildwindows.com
How to…
• Animate complex content fast• Clip content to an arbitrary shape fast• Draw text fast• Render Direct2D effects fast• Scale performance on multiple cores• Draw mixed 2D/3D content fast
www.buildwindows.com
Draw text fast
• Cache text layout
• Explicitly set text rendering mode to grayscale
• Cache text
// Create text layout oncem_dWriteFactory->CreateTextLayout(
text,length,m_textFormat.Get(),maxWidth, maxHeight,&m_textlayout);
// draw the text layout repeatedlym_d2dContext->DrawTextLayout(origin,
m_textlayout.Get(),brush.Get());
Cache text layout
www.buildwindows.com
GrayScale text rendering mode
• Set the rendering mode to grayscale explicitly• SetTextAntiAliasMode(D2D1_TEXT_ANTIALIAS_MODE_GRA
YSCALE)
www.buildwindows.com
Use full scene or per primitive bitmap caching as described in
previous slides.
www.buildwindows.com
How to…
• Animate complex content fast• Clip content to an arbitrary shape fast• Draw text fast• Render Direct2D effects fast• Scale performance on multiple cores• Draw mixed 2D/3D content fast
www.buildwindows.com
Render Direct2D effects fast
• Cache effect output
• Group rendering of effects together
www.buildwindows.com
Caching effect output
• Use ID2D1Properties::SetValue(D2D1_PROPERTY_CACHED, TRUE)
www.buildwindows.com
Grouping effect rendering
Button 1
Button 4Button 3
Button 2
www.buildwindows.com
Render glow effect first
www.buildwindows.com
Render the buttons
www.buildwindows.com
Render button text
Button 1
Button 4Button 3
Button 2
www.buildwindows.com
How to…
• Animate complex content fast• Clip content to an arbitrary shape fast• Draw text fast• Render Direct2D effects fast• Scale performance on multiple cores• Draw mixed 2D/3D content fast
m_d2dDevice->CreateDeviceContext(
D2D1_DEVICE_CONTEXT_OPTIONS_ENABLE_MULTITHREADED_OPTIMIZATIONS,m_d2dContext);
Enable multi-threaded optimizations
www.buildwindows.com
How to…
• Animate complex content fast• Clip content to an arbitrary shape fast• Draw text fast• Render Direct2D effects fast• Scale performance on multiple cores• Draw mixed 2D/3D content fast
www.buildwindows.com
Mixing 2D and 3D content
• Reduce state transitions
• Minimize switching between 2D and 3D drawing
www.buildwindows.com
Recap
• Animate complex content fast• Clip content to an arbitrary shape fast• Draw text fast• Render Direct2D effects fast• Scale performance on multiple cores• Draw mixed 2D/3D content fast
www.buildwindows.com
Wrap up
• Assess unique app performance needs…profile
• Apply performance tips and techniques
• Go build your fastest 2D graphics app!
www.buildwindows.com
Related sessions
• [PLAT-766T] Introduction to DirectX for Metro style apps
• [PLAT-750T] Build your first Metro style game
• [PLAT-752T] Tuning GPU usage for any form factor
• [PLAT-770T] Create cool image effects with Direct2D
• [PLAT-754T] From touch to gamepads: master player input in your Metro style game
• [SAC-217T] Graphics on the server
• [TOOL-761T] A lap around DirectX game development tools
www.buildwindows.com
Further reading and documentation
• Channel 9 PDC’10 talk: Adopting D2D and Dwrite for hardware acceleration in Native Windows Applications
• Channel 9 PDC=09 talk: Advanced graphics functionality using DirectX
www.buildwindows.com
• Feedback and questions http://forums.dev.windows.com
• Session feedbackhttp://bldw.in/SessionFeedback
thank you
© 2011 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to
be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.