640 likes | 2.03k Views
PLAT-769T. Achieving high performance 2D graphics with Direct2D. Megha Jain Program Manager II Microsoft Corporation. Agenda. What makes your 2D graphics app run faster in Windows 8? What are the performance points your app might be sensitive to?
E N D
PLAT-769T Achieving high performance 2D graphics with Direct2D Megha Jain Program Manager II Microsoft Corporation
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?
demo BasicTextAnimation sample
demo GeometryRealization sample
demo DirectX magazine sample
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
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
Caching techniques • Full scene caching using a color bitmap • Per primitive caching using an A8 bitmap and FillOpacityMask() API
Full scene caching • // create a bitmap • m_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 target • ComPtr<ID2D1Image> oldTarget; • m_d2dContext->GetTarget(&oldTarget);
Full scene caching • // render to the sceneBitmap • m_d2dContext->SetTarget(sceneBitmap.Get()); • m_d2dContext->BeginDraw(); • … • m_d2dContext->EndDraw(); • // render sceneBitmap to oldTarget • m_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 bitmap • m_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 target • ComPtr<ID2D1Image> oldTarget; • m_d2dContext->GetTarget(&oldTarget);
Per primitive caching using FillOpacityMask() • // render to the opacityBitmap • m_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);
Handling scroll animation • // enables rasterizing only the changed content • IDXGISwapChain1::Present1(UINT SyncInterval, UINT PresentFlags, DXGI_PRESENT_PARAMETERS* pPresentParameters); • structDXGI_PRESENT_PARAMETERS{ UINT DirtyRectsCount; RECT* pDirtyRects; RECT* pScrollRect; POINT* pScrollOffset;}
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
Use bitmaps for caching display outputUse command list for caching print output.
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
Clipping to arbitrary geometry- layers • // create the layer resource • ComPtr<ID2D1Layer> m_layer; • m_d2dContext->CreateLayer(&m_layer); • // render to the layer with the clipping geometry • m_d2dContext->PushLayer( • D2D1::LayerParameters( • boundsRect, • geometricMask), • m_layer.Get());
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());
Use ID2D1Layer instead of intermediate surfaces whenever possible.Use NULL layers when possible
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
Draw text fast Cache text layout Explicitly set text rendering mode to grayscale Cache text
Cache text layout • // Create text layout once • m_dWriteFactory->CreateTextLayout( • text, • length, • m_textFormat.Get(), • maxWidth, maxHeight, • &m_textlayout); • // draw the text layout repeatedly • m_d2dContext->DrawTextLayout(origin, • m_textlayout.Get(), • brush.Get());
GrayScaletext rendering mode • Set the rendering mode to grayscale explicitly • SetTextAntiAliasMode(D2D1_TEXT_ANTIALIAS_MODE_GRAYSCALE)
Use full scene or per primitive bitmap caching as described in previous slides.
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
Render Direct2D effects fast Cache effect output Group rendering of effects together
Caching effect output • Use ID2D1Properties::SetValue(D2D1_PROPERTY_CACHED, TRUE)
Grouping effect rendering Button 2 Button 1 Button 3 Button 4
Render button text Button 2 Button 1 Button 3 Button 4
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
Enable multi-threaded optimizations • m_d2dDevice->CreateDeviceContext( • D2D1_DEVICE_CONTEXT_OPTIONS_ENABLE_MULTITHREADED_OPTIMIZATIONS, • m_d2dContext);
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
Mixing 2D and 3D content • Reduce state transitions • Minimize switching between 2D and 3D drawing
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
Wrap up • Assess unique app performance needs…profile • Apply performance tips and techniques • Go build your fastest 2D graphics app!
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
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
thank you Feedback and questions http://forums.dev.windows.com Session feedbackhttp://bldw.in/SessionFeedback
© 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.