380 likes | 761 Views
CL14. Advanced Graphics Functionality on Windows Using DirectX. Michael Oneppo Program Manager Microsoft Corporation. What is Advanced Graphics?. Superior 2D Rendering Performance Interoperating With Other Technologies. 2D Performance. Performance and DirectX.
E N D
CL14 Advanced Graphics Functionality on Windows Using DirectX Michael Oneppo Program Manager Microsoft Corporation
What is Advanced Graphics? Superior 2D Rendering Performance Interoperating With Other Technologies
Performance and DirectX • Direct2D built atop Direct3D 10 • Allocation overhead in Direct3D 10 is high Reduce: Controlling scene complexity Recycle: Repurposing already created resources Reuse: Caching intermediate data to reduce GPU work
Performance and DirectX • Fill-rate bound vs. scene complexity-bound • Improving fill-rate bound cases • Improving complexity-bound cases Reduce: Controlling scene complexity
Managing Scene Complexity • Smaller render target improves performance? • Fill rate bound Scene Complexity Fill Rate
Improving Fill-Rate Bound Cases • Reduce render target size • Find the exact number of pixels you need to render to • Pay attention to over-draw • Rendering elements that aren’t visible • Occlusion • Clipping
Quality vs. Performance • Scene complexity is increased by: • Anti-aliasing • Re-rendering of elements • Complex brushes & effects • Sophisticated geometries
Performance and DirectX • A8 resources and grayscale caching • Mesh & mesh caching Reuse: Caching intermediate data to reduce GPU work
A8 Resources • Great way to cache text, opacity maps, or otherwise grayscale content • Use CreateCompatibleRenderTarget with DXGI_PIXEL_FORMAT_A8_UNORM • Use FillOpacityMask to draw any A8 surface to a render target
Text Animation Sample demo A8 Resources
DirectWrite Performance: Fonts vs. Font Faces • IDWriteFontFace is the full font reference • Loads a lot of information on the font • Useful in measuring text • Querying text features • Assigning a font • IDWriteFont is a lightweight interface for getting info about a font • IDWriteFont is faster to load and takes up less memory • Useful when just listing the fonts on a system
Caching Meshes • ID2D1Mesh represents (aliased) triangles • Direct2D can render these very quickly • ID2D1Mesh::Open returns an ID2D1TessellationSink • Pass this to the ID2DGeometry::Tessellate function to fill the mesh. • Use FillMesh to draw it • Manually implement the ID2D1TessellationSink if you want to see the triangles • Best way to transfer geometry to Direct3D.
Geometry Realization Sample demo Caching Meshes
Performance and DirectX • Surface size and performance • Using tiling or an “atlas” Recycle: Repurposing already created resources
Bigger Surfaces • Seems counter-intuitive • Favor multi-purpose, larger textures over single use, smaller textures • Still only allocate what you need • Minimum size 64Kb • For 24 bpp bitmaps, this is about 150x150 • Recommended making 256x256 your global minimum
Surface Size • Maximum size • Varies per card • Reasonable to assume 2048x2048, many modern cards support 4096x4096 • Find out using ID2D1RenderTarget:: GetMaximumBitmapSize() • Size is in pixels
Atlasing 1024 px 1 3 2 1 5 4 2 3 2 1 4 3 5 4 5 256 px
Using an Atlas • You should create an “AtlasBitmap” class or struct • Contains: • Reference to bitmap (pointer in C++) • Offset in X,Y • Size • Provides you everything you need to use the DrawBitmap function • Update atlas contents using the CopyFromBitmap function
List View Sample demo Atlas Creation and Use
DirectX Interoperability • Understanding Direct2D fallback • Direct2D and GDI • Direct2D and Direct3D
Understanding Direct2D Fallback • Direct2D natively runs on Direct3D 10-class hardware • Utilizes Direct3D 10 Level 9 to run on Direct3D 9-class hardware • For very low end machines, Direct2D has a software fallback • If render target creation fails, pass in the flag D2D1_RENDER_TARGET_TYPE_SOFTWARE
Rendering D2D Content to an HDC ID2D1Factory::CreateDCRenderTarget( D2D1_RENDER_TARGET_PROPERTIES *rtProperties, ID2D1DCRenderTarget **dcRenderTarget ); ID2D1DCRenderTarget::BindDC( HDC hDC, RECT *pSubRect);
GDI/Direct2D Recommendations • Make sure you’ve called EndDraw() before returning to GDI • Batch as much as possible • Do as much as you can in D2D before switching to GDI and vice versa • Minimize the size of the updated rects • Reduces the amount of memory that needs to be copied
Render D2D Content to an ID3D10Texture2D ID2D1Factory::CreateDxgiSurfaceRenderTarget( IDXGISurface *dxgiSurface, D2D1_RENDER_TARGET_PROPERTIES *rtProperties, ID2D1RenderTarget **renderTarget );
Going the Other Way (D3D10 D2D) ID2D1RenderTarget::CreateSharedBitmap( REFIID riid, void *data, D2D1_BITMAP_PROPERTIES *bitmapProperties, ID2D1Bitmap **bitmap ); • Must pass an IDXGISurface into data • bitmapProperties has to match the format of the DXGI surface • Alpha is not included in this check
Direct2D/Direct3D Recommendations • Device must support BGRA, D3D10.1 • Falling back to Direct3D10Level9 can mitigate this • Special flag for device creation: D3D10_CREATE_DEVICE_BGRA_SUPPORT • Render static content once and cache (sound familiar?) • Minimize transitions between Direct3D and Direct2D by batching as much as possible (also sound familiar?)
3D Text demo Putting it All Together
Direct2D Debug Layer • Intercepts calls from Direct2D • Provides critical usage information as well as handy performance information • Something failed? The debug layer will tell you. • Leaves operation of Direct2D completely unchanged • May be a minor performance difference
Turning on the Debug Layer • D2D1_FACTORY_OPTIONS options; • options.debugLevel = • D2D1_DEBUG_LEVEL_INFORMATION; • hr = D2D1CreateFactory( • D2D1_FACTORY_TYPE_SINGLE_THREADED, • options, • &m_pD2DFactory );
Layers vs. Clips • Only use layers when alpha-blending or non-rectangular content is needed. • Clips are faster when you don’t need alpha blending or you have rectangular content • PushAxisAlignedClip/PopAxisAlignedClip • Debug layer helps you: • D1111: Using Layer When Clip Is Sufficient
The Platform Update for Windows Vista • Down-level availability of key Windows 7 features on Windows Vista • Available on Windows Update • More information: • http://support.microsoft.com/kb/971644
Resources • DirectX on MSDN: • http://msdn.microsoft.com/directx • Windows 7 SDK: • http://msdn.microsoft.com/windows • August 2009 DirectX SDK: • http://msdn.microsoft.com/directx/sdk • All Code Samples on Code Gallery: • http://code.msdn.microsoft.com/d2dlistview • http://code.msdn.microsoft.com/d2dtextanimation • http://code.msdn.microsoft.com/d2dgeometryreali • http://code.msdn.microsoft.com/d2dinteractive3dtext • Debug Layer on Code Gallery: • http://code.msdn.microsoft.com/Direct2DTools
YOUR FEEDBACK IS IMPORTANT TO US! Please fill out session evaluation forms online at MicrosoftPDC.com
Learn More On Channel 9 • Expand your PDC experience through Channel 9 • Explore videos, hands-on labs, sample code and demos through the new Channel 9 training courses channel9.msdn.com/learn Built by Developers for Developers….