Transcript
Page 1: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

www.buildwindows.com

Achieving high performance 2D graphics with Direct2D

Megha JainProgram Manager IIMicrosoft Corporation

PLAT-769T

Page 2: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

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?

Page 3: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

Faster 2D graphics on Windows 8

Page 4: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

www.buildwindows.com

BasicTextAnimation sample

demo

Page 5: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

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

Page 6: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

www.buildwindows.com

GeometryRealization sample

demo

Page 7: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

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

Page 8: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

www.buildwindows.com

Your app will run faster on Windows 8

Page 9: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

www.buildwindows.com

DirectX magazine sample

demo

Page 10: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

www.buildwindows.com

Think about opportunities for app-specific optimizations.

Page 11: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

How to write the fastest Direct2D app

Page 12: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

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

Page 13: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

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

Page 14: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

www.buildwindows.com

Caching techniques

• Full scene caching using a color bitmap

• Per primitive caching using an A8 bitmap and FillOpacityMask() API

Page 15: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

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);

Page 16: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

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());

Page 17: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

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

Page 18: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

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);

Page 19: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

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);

Page 20: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

// 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

Page 21: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

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

Page 22: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

www.buildwindows.com

Use bitmaps for caching display output

Use command list for caching print output.

Page 23: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

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

Page 24: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

www.buildwindows.com

Clipping to an arbitrary shape

Page 25: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

// 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

Page 26: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

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());

Page 27: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

www.buildwindows.com

Use ID2D1Layer instead of intermediate surfaces whenever

possible.Use NULL layers when possible

Page 28: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

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

Page 29: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

www.buildwindows.com

Draw text fast

• Cache text layout

• Explicitly set text rendering mode to grayscale

• Cache text

Page 30: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

// 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

Page 31: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

www.buildwindows.com

GrayScale text rendering mode

• Set the rendering mode to grayscale explicitly• SetTextAntiAliasMode(D2D1_TEXT_ANTIALIAS_MODE_GRA

YSCALE)

Page 32: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

www.buildwindows.com

Use full scene or per primitive bitmap caching as described in

previous slides.

Page 33: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

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

Page 34: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

www.buildwindows.com

Render Direct2D effects fast

• Cache effect output

• Group rendering of effects together

Page 35: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

www.buildwindows.com

Caching effect output

• Use ID2D1Properties::SetValue(D2D1_PROPERTY_CACHED, TRUE)

Page 36: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

www.buildwindows.com

Grouping effect rendering

Button 1

Button 4Button 3

Button 2

Page 37: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

www.buildwindows.com

Render glow effect first

Page 38: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

www.buildwindows.com

Render the buttons

Page 39: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

www.buildwindows.com

Render button text

Button 1

Button 4Button 3

Button 2

Page 40: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

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

Page 41: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

m_d2dDevice->CreateDeviceContext(

D2D1_DEVICE_CONTEXT_OPTIONS_ENABLE_MULTITHREADED_OPTIMIZATIONS,m_d2dContext);

Enable multi-threaded optimizations

Page 42: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

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

Page 43: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8
Page 44: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

www.buildwindows.com

Mixing 2D and 3D content

• Reduce state transitions

• Minimize switching between 2D and 3D drawing

Page 45: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

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

Page 46: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

www.buildwindows.com

Wrap up

• Assess unique app performance needs…profile

• Apply performance tips and techniques

• Go build your fastest 2D graphics app!

Page 47: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

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

Page 48: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

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

Page 49: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

www.buildwindows.com

• Feedback and questions http://forums.dev.windows.com

• Session feedbackhttp://bldw.in/SessionFeedback

thank you

Page 50: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

© 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.

Page 51: Faster 2D graphics on Windows 8 Your app will run faster on Windows 8

Top Related