Transcript
Page 1: Unlocking the power of DirectX in apps that use XAML
Page 2: Unlocking the power of DirectX in apps that use XAML

Unlocking the power of DirectX in apps that use XAMLBede Jordan, [email protected] Development Lead 4-065

Page 3: Unlocking the power of DirectX in apps that use XAML

Who am I?

Outside of work?• Hail from Australia…• …but there are more

mountains to climb in Washington!

Senior Development Lead, XAML• In Windows 8.1, I worked on graphics,

media, and text in XAML• Previously: XAML, WPF, Silverlight,

Desktop Window Manager, etc.

Page 4: Unlocking the power of DirectX in apps that use XAML

1. Why use DirectX in your XAML app?

2. How can I integrate DirectX in XAML?

• SurfaceImageSource/VirtualSurfaceImageSource

• SwapChainPanel/SwapChainBackgroundPanel3. What is independent input, and

how can I use it in my XAML app?

XAML + DirectX interop

Page 5: Unlocking the power of DirectX in apps that use XAML

1. Why use DirectX in your XAML app?

Page 6: Unlocking the power of DirectX in apps that use XAML

XAML is the first Windows UI platform that includes fully integrated and high-performance DirectX interop

Page 7: Unlocking the power of DirectX in apps that use XAML

What is XAML for?XAML is focused on building great UIProvides a set of controls and drawing primitives, backed by a rich object model• From Rectangle primitives to complex GridViews• Accessible in XAML or code behind• Properties can be animated, databound <StackPanel Background="Black"> <Border Width="100" Height="100" CornerRadius="5" BorderThickness="10" BorderBrush="Red"> <Image Source="Assets/trees.jpg" Stretch="UniformToFill"/> </Border> <Button Content="Click me!" HorizontalAlignment="Center"/> </StackPanel>

Page 8: Unlocking the power of DirectX in apps that use XAML

What is DirectX for?DirectX provides a powerful set of graphics functionality• Flexibility, scalability, and high performance• Lowest-level graphics API for applications - “All the way to the

metal”– Direct3D– Direct2D– WIC (Windows Imaging Component) – DirectWrite – DirectComposition– DirectManipulation

• We built all of XAML on top of DirectX!– DirectComposiiton in Win 8.1

Page 9: Unlocking the power of DirectX in apps that use XAML

C++

XAML + DirectX

DirectX interop in XAML provides developers the best of both worlds• UI and controls? Use

XAML• Custom graphics, image

editing, games? Use DirectX

Windows 8 subsystem

Windows Store app using XAML (C++, C#, VB)

App Theme & PVL

Direct3D

DirectWrit

eDirect2D Media Inpu

t

XAML Framework

Windows Runtime / C++

Page 10: Unlocking the power of DirectX in apps that use XAML

Many great applications that you use are built with DirectX interop:• OneNote: custom display canvas• FreshPaint: custom-rendered painting• Bing Maps app• Reader (PDF)• Games!

Page 11: Unlocking the power of DirectX in apps that use XAML

DirectX

XAM

L

Page 12: Unlocking the power of DirectX in apps that use XAML

XAML

DirectX

DirectX

Page 13: Unlocking the power of DirectX in apps that use XAML

DirectX

XAML

Page 14: Unlocking the power of DirectX in apps that use XAML

DirectX

XAML

Page 15: Unlocking the power of DirectX in apps that use XAML

2. DirectX interop options in XAML

Page 16: Unlocking the power of DirectX in apps that use XAML

DirectX interop options in XAML:a) Use a DirectX surface as a XAML brush:• SurfaceImageSource• VirtualSurfaceImageSource

b) Tightly integrate into compositor, low latency presentation:• SwapChainPanel• SwapChainBackgroundPanel

Page 17: Unlocking the power of DirectX in apps that use XAML

DirectX interop options in XAMLSurfaceImageSource and VirtualSurfaceImageSource

Page 18: Unlocking the power of DirectX in apps that use XAML

SurfaceImageSourceDerives from XAML ImageSource• Think of it as a dynamically

updatable D2D/D3D bitmap• Just like a BitmapImage, it can fill

any XAML elementBegin/Draw/End update model• Application provides D3D device• XAML provides a surface to draw

to with D2D/D3D• XAML retains that surface in the

tree

Object   DependencyObject     ImageSource       SurfaceImageSource

ImageBrush^ brush = ref new ImageBrush();

brush->ImageSource = ref new SurfaceImageSource(300, 300);

Ellipse1->Fill = brush;

Page 19: Unlocking the power of DirectX in apps that use XAML

SurfaceImageSourceinterface ISurfaceImageSourceNative{

SetDevice(IDXGIDevice *pDevice); BeginDraw(RECT updateRect, IDXGISurface** ppSurface, POINT* pOffset); EndDraw();

};

: IUnknown

These methods must be called from the XAML UI thread

Page 20: Unlocking the power of DirectX in apps that use XAML

1. Create a SurfaceImageSource2. Create a D3D device and set it on the

SurfaceImageSource3. Set the SurfaceImageSource on a XAML element’s

ImageBrush4. Call BeginDraw5. Draw to the returned IDXGISurface with Direct2D or

Direct3D6. Call EndDraw when you’re done drawing

How can I use SurfaceImageSource?

Page 21: Unlocking the power of DirectX in apps that use XAML

DemoSurfaceImageSource

Page 22: Unlocking the power of DirectX in apps that use XAML

VirtualSurfaceImageSourceFor very large surfaces that are too large to fit in video memory as a single texture• Larger than 1 screen size• Virtualization via tiles • Automatically manages tile cache• XAML requests tiles from the app when

they come into view• Use for mapping scenarios, large

document canvases

Object   DependencyObject     ImageSource       SurfaceImageSource VirtualSurfaceImageSource

DirectX

XAM

L

Page 23: Unlocking the power of DirectX in apps that use XAML

VirtualSurfaceImageSourceinterface IVirtualSurfaceImageSourceNative: ISurfaceImageSourceNative {

RegisterForUpdatesNeeded(IVirtualSurfaceUpdatesCallbackNative *pCallback);GetUpdateRects(RECT *pUpdates, DWORD count);GetUpdateRectCount(DWORD *pCount);Invalidate(RECT updateRect);

};

interface IVirtualSurfaceUpdatesCallbackNative: IUnknown{

UpdatesNeeded(); };

Page 24: Unlocking the power of DirectX in apps that use XAML

1. Implement IVirtualSurfaceImageSourceCallbackNative interface

2. Call RegisterForUpdatesNeeded on VSIS3. In UpdatesNeeded callback, call GetUpdateRectCount

and GetUpdateRects4. Render the update rectangles (using BeginDraw/EndDraw

method as per SurfaceImageSource)

How can I use VirtualSurfaceImageSource?

Page 25: Unlocking the power of DirectX in apps that use XAML

New to SurfaceImageSource and VirtualSurfaceImageSource in 8.1

1. Faster Direct2D drawing2. Multi-threaded drawing

Page 26: Unlocking the power of DirectX in apps that use XAML

New for SurfaceImageSource in 8.11. Faster Direct2D drawing• Draw with a Direct2D DeviceContext directly• Faster drawing to multiple SurfaceImageSource objects• Examples: thumbnail generation

Flush

WorkD2D

App Draws Surface 1

No batching

FlushWork

App Draws Surface 2

FlushWork

App Draws Surface 3

WorkD2D

App Draws Surface 1

With D2D device context/batching

Work

App Draws Surface 2

FlushWork

App Draws Surface 3

Page 27: Unlocking the power of DirectX in apps that use XAML

SurfaceImageSourceinterface ISurfaceImageSourceNativeWithD2D: IUnknown{

SetDevice(IUnknown* device);BeginDraw(RECT updateRect, REFIID iid, void **updateObject POINT* offset);EndDraw();SuspendDraw();ResumeDraw();

}

iid can be either:

For using D3D:__uuidof(IDXGISurface) *updateObject is IDXGISurface

For optimized D2D batching:__uuidof(ID2D1DeviceContext) *updateObject isID2D1DeviceContext

Page 28: Unlocking the power of DirectX in apps that use XAML

New for 8.1 in SurfaceImageSource2. Multi-threaded drawing

UI Thread

Input processing

Layout, Render

Application rendering

code

App rendering thread

Cross thread communication

Multi-threaded V/SIS drawing• Expensive app rendering on

another thread• Keeps UI thread responsive

UI Thread

Input processing

Layout, Render

Application rendering

code

Single-threaded V/SIS drawing• Expensive DX drawing can bog

down UI thread• Can delay input processing etc.

Page 29: Unlocking the power of DirectX in apps that use XAML

SurfaceImageSourceinterface ISurfaceImageSourceNativeWithD2D: IUnknown{

SetDevice(IUnknown* device);BeginDraw(RECT updateRect, REFIID iid, void **updateObject POINT* offset);SuspendDraw();ResumeDraw();EndDraw();

}

These methods can be freely called from any thread

Page 30: Unlocking the power of DirectX in apps that use XAML

DirectX interop options in XAMLSwapChainBackgroundPanel, SwapChainPanel

Page 31: Unlocking the power of DirectX in apps that use XAML

DirectX

Page 32: Unlocking the power of DirectX in apps that use XAML

SwapChainBackgroundPanel DX

XAMLAllows full screen DirectX content to be used as a background in a XAML app• Low latency presentation controlled by app• Games, FreshPaint• Can render a swap chain on a background thread

for more fluid renderingDesigned for a specific scenario• Must be background element• Must be full-window sized

Page 33: Unlocking the power of DirectX in apps that use XAML

New for 8.1 - SwapChainPanelBenefits over SwapChainBackgroundPanel• Z-order limitations removed (same for

WebView)!• Composable like any other XAML element• Any sized swap chain allowed• Can use more than one simultaneously• Size changed notification to redraw

content crisply

XAML

DXXAML

Page 34: Unlocking the power of DirectX in apps that use XAML

interface ISwapChainBackgroundPanelNative: IUnknown {

SetSwapChain(IDXGISwapChain *pSwapChain); };

SwapChainPanel/SwapChainBackgroundPanel

interface ISwapChainPanelNative: IUnknown {

SetSwapChain(IDXGISwapChain *pSwapChain); };

(Look familiar?)

Page 35: Unlocking the power of DirectX in apps that use XAML

DemoSwapChainPanel

Page 36: Unlocking the power of DirectX in apps that use XAML

When should I use DirectX interop APIs?Use SurfaceImageSource or

VirtualSurfaceImageSource when:• You want your content to integrate tightly with the XAML tree (e.g.

ScrollViewer)• Your DirectX content isn’t being updated too frequently– Discrete updates, not continuous updates– The XAML framework can still manipulate its position etc.– Max update frequency is UI thread frame rateUse SwapChainPanel or SwapChainBackgroundPanel

when:• Your content is updating quickly (e.g. games, ink)• You want the lowest presentation latency possible

Page 37: Unlocking the power of DirectX in apps that use XAML

3. Independent Input

Page 38: Unlocking the power of DirectX in apps that use XAML

SwapChainPanel now exposes the ability to process touch, pen, and mouse input on a background thread, providing a path for high performance, low latency interactivity

Page 39: Unlocking the power of DirectX in apps that use XAML

SwapChainPanel with independent input

Dependent input• Input events in XAML apps arrive on the UI thread• Input processing competes with other messages and processing that occurs

on the UI thread• Apps can feel unresponsive

Independent input• Allows touch input for SwapChain[Background]Panel to be sent to another

thread• Enables input and output (rendering) to be fully independent of the XAML

UI thread• Touch, pen, and mouse can be received• Allows developers to do custom independent input – this isn’t limited to

panning any more

Page 40: Unlocking the power of DirectX in apps that use XAML

SwapChainPanel with UI thread input

XAML

DXXAML

UI Thread

Input processing

Layout, Render

Render

App rendering thread

Cross thread communicationOther

operations

Page 41: Unlocking the power of DirectX in apps that use XAML

DX Input

SwapChainPanel with Independent InputXAML

DXXAML

UI Thread

Input processing

Layout, Render

Render

App rendering thread

Cross thread communicationOther

operations

XAML input processing

Page 42: Unlocking the power of DirectX in apps that use XAML

SwapChainPanel

SwapChainPanel : Grid

{

// Can be called from any non-UI thread, to create a pipe to receive input on

CreateCoreIndependentInputSource(CoreInputDeviceTypes deviceTypes, CoreIndependentInputSource** source);

// Events

CompositionScaleChanged();

};

Page 43: Unlocking the power of DirectX in apps that use XAML

CoreIndependentInputSourceCoreIndependentInputSource {

// EventsPointerPressed(…)PointerReleased(…)PointerMoved(…)// PropertiesWindows.Foundation.Point PointerPosition /* ... */

};

CoreIndependentInputSource provides access to all pointer based events and state, when the input is routed to the SwapChainPanel

Page 44: Unlocking the power of DirectX in apps that use XAML

1. Create a SwapChainPanel, attach it to the XAML tree2. Create an IDXGISwapChain, and set it on the

SwapChainPanel3. Call CreateCoreIndependentInputSource on a

background thread4. Setup background thread to process input messages

and render in a loop5. Profit!

How can I use SwapChainPanel with CoreIndependentInputSource?

Page 45: Unlocking the power of DirectX in apps that use XAML

DemoSwapChainPanel + independent input

Page 46: Unlocking the power of DirectX in apps that use XAML

You can use XAML and DirectX together to create great apps and games

Use both swap chain-style and surface-style interop, depending on application needs

XAML + DirectX + independent input allows for responsive, custom rendering in XAML apps

Recap

Page 47: Unlocking the power of DirectX in apps that use XAML

Resources

Session Code

Speaker Title

2-164 Tim Heuer What’s new in XAML?3-157 Kiran Kumar XAML Performance Fundamentals2-047 Kam VedBrat Building games for Windows2-192 Chris Anderson Building a UI: What Does It Cost?4-021 Silvana Moncayo DirectComposition: Smooth Composition and Animation

for Desktop Applications4-072 Frank Olivier Hyper-Fast Web Graphics with OpenGL

Sessions

Page 49: Unlocking the power of DirectX in apps that use XAML

What?• Are you interested

in having an impact on the future user experiences in Visual Studio? Come help us shape the future.

Give us your feedback!When & Where?• Schedule a time

with us [email protected]

• Room 254 South

Moscone, West Mezzanine Level

Why?• Your input and

feedback will influence future Microsoft Visual Studio tools

Page 50: Unlocking the power of DirectX in apps that use XAML

Questions?

Page 51: Unlocking the power of DirectX in apps that use XAML

Evaluate this session

Scan this QR code to evaluate this session and be automatically entered in a drawing to win a prize!

Page 52: Unlocking the power of DirectX in apps that use XAML

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