C++, XAML and DirectX - Better together - Sridhar Poduri Program Manager Microsoft

Page created by John Wise
 
CONTINUE READING
C++, XAML and DirectX - Better together - Sridhar Poduri Program Manager Microsoft
C++, XAML and DirectX
– Better together
Sridhar Poduri
Program Manager
Microsoft
C++, XAML and DirectX - Better together - Sridhar Poduri Program Manager Microsoft
Agenda
1. Overview of the Windows Runtime Library
2. Overview of the C++ Component Extensions
3. XAML and DirectX
C++, XAML and DirectX - Better together - Sridhar Poduri Program Manager Microsoft
C++, XAML and DirectX - Better together - Sridhar Poduri Program Manager Microsoft
WRL
C++, XAML and DirectX - Better together - Sridhar Poduri Program Manager Microsoft
Why use WRL
C++, XAML and DirectX - Better together - Sridhar Poduri Program Manager Microsoft
C++, XAML and DirectX - Better together - Sridhar Poduri Program Manager Microsoft
WinRT Concepts
WinRT Object                        Event
Reference counted                   Delegate used as Event callback
Implements IInspectable             Server advertises events
Instantiated through factory        Client creates delegate and registers it to receive events
WeakReference support
                                    Misc
Delegate                            Reference count management
Callback                            Asynchronous operations
Interface with method
Delegate instance is WinRT object
WRL Classes
WinRT Object        Event
RuntimeClass        EventSource
InspectableClass    Callback
ActivatableClass
ActivationFactory
                    Misc
                    ComPtr, WeakPtr
Delegate            AsyncBase
Callback            And many more
ComPtr activationFactory;

HRESULT hr = GetActivationFactory(
     HString::MakeReference(RuntimeClass_Windows_Foundation_Uri).Get(),
     &activationFactory

);
// Error handling

ComPtr uriActivationFactory;

hr = activationFactory.As(&uriActivationFactory);
// Error handling

ComPtr uri;

hr = uriActivationFactory->CreateUri(
     HString::MakeReference(L"http://www.microsoft.com").Get(),
     &uri

);
// Error handling

Wrappers::HString domainName;

hr = uri->get_Domain(domainName.GetAddressOf());
// Error handling
ComPtr timerFactory;
HRESULT hr = GetActivationFactory(
        Wrappers::HString::MakeReference(RuntimeClass_Windows_System_Threading_ThreadPoolTimer).Ge
t(),
        &timerFactory
);
       // Error handling

TimeSpan delay;
delay.Duration = 200;
ComPtr timer;
hr = timerFactory->CreateTimer(
        Callback(
                [](IThreadPoolTimer* timer) -> HRESULT {
                // Perform some action in separate thread
                      return S_OK;
        }
        ).Get(), delay, &timer
);
H^T
     “It’s not okay to call me a carrot”

    A hat is a pointer to a pointer to an array of function pointers
      aka: Pointer to vptr to vtable
                                                            Calculator::ICalculator vtable
                                                            &Calculator::QueryInterface
                            ref class Calculator
                                                            &Calculator::AddRef
                            IStorageCell vtable * vfptr1
                                                            &Calculator::Release
ICalculator^ calc           ICalculator vtable * vfptr2
                                                            &Calculator::GetIids
                            int _storage
                                                            &Calculator::GetRuntimeClassName
                            int _refcount
                                                            &Calculator::GetTrustLevel
                                                            &Calculator::Add
H^T
 “It’s not okay to call me a carrot”

•A hat is a pointer to a pointer to an array of function pointers
        aka: Pointer to vptr to vtable
•^ provides automatic reference counting
•Similar to ComPtr
•Adds ability to use casting.
Review:
Your Modern-style UI framework
 •   Built in and custom controls
 •   Layout, styling, databinding
 •   Media, animation, drawing
 •   Design-time tools

A focus on performance
 • Leveraging hardware
 • Independent animations
 • Fast rendering and composition
Review:
Your graphics solution
 Everything in Windows 8 is built around DirectX: hardware to OS to UI frameworks

Control vs. Complexity
 A tradeoff between difficulty and power of lower level APIs
 More work to align with Modern UI style guidelines
Demo
XAML
The Best of Both Worlds

                          Complex 2D/3D graphics, text

                          Image effects
When do you want UI + Graphics?
Example scenarios:
Visual design, modeling, creativity
Complex image processing
Large documents, maps
Simulations
Games
Interop Functionality
Integration
Easy composition of XAML + DirectX using familiar XAML model
All functionality of both XAML and DirectX

Performance
Low-latency input and interactivity
Incremental rendering for both XAML and DirectX content
Low overhead: retained-mode XAML and immediate-mode DirectX together

C++ Only
XAML code-behind can be managed, but interop DirectX is C++ only (without CX wrappers)
Query for native interfaces of XAML WinRT types to access interop methods
3.1 Why use DirectX in your XAML app?
XAML is the first Windows UI platform that includes
fully integrated and high-performance DirectX interop
What is XAML for?
XAML is focused on building great UI
Provides 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
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!
   – DirectComposition in Win 8.1
XAML + DirectX
                                              Windows Store app using XAML (C++, C#, VB)

 DirectX interop in XAML
 provides developers the                                            Windows Runtime / C++

 best of both worlds                 C++
 • UI and controls?  Use XAML
 • Custom graphics, image editing,                                       XAML Framework

   games?  Use DirectX

                                     Direct   Direct   Direct                          Theme
                                                                 Media       App                Input
                                      3D       2D      Write                           & PVL

                                                                                   Windows 8 subsystem
Many great applications that you use are
built with DirectX interop:
•   OneNote: custom display canvas
•   FreshPaint: custom-rendered painting
•   Bing Maps app
•   PDF Reader
•   Games!
3.2 DirectX interop options in 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 (Windows 8.1)
•    SwapChainBackgroundPanel
DirectX interop options in XAML
SurfaceImageSource and VirtualSurfaceImageSource
SurfaceImageSource
Derives from XAML ImageSource                     Object
• Think of it as a dynamically updatable            DependencyObject
  D2D/D3D bitmap                                      ImageSource
• Just like a BitmapImage, it can fill any XAML         SurfaceImageSource
  element

                                                  ImageBrush^ brush = ref new
 Begin/Draw/End update model                          ImageBrush();
 • Application provides D3D device
                                                  brush->ImageSource = ref new
 • XAML provides a surface to draw to with            SurfaceImageSource(300, 300);
   D2D/D3D
                                                  Ellipse1->Fill = brush;
 • XAML retains that surface in the tree
SurfaceImageSource
interface ISurfaceImageSourceNative :   IUnknown
{
     SetDevice(IDXGIDevice *pDevice);
     BeginDraw(                             These methods must
          RECT updateRect,                  be called from the
          IDXGISurface** ppSurface,         XAML UI thread
          POINT* pOffset
          );
     EndDraw();
};
How can I use SurfaceImageSource?
 1.   Create a SurfaceImageSource
 2.   Create a D3D device and set it on the SurfaceImageSource
 3.   Set the SurfaceImageSource on a XAML element’s ImageBrush
 4.   Call BeginDraw
 5.   Draw to the returned IDXGISurface with Direct2D or Direct3D
 6.   Call EndDraw when you’re done drawing
Demo
SurfaceImageSource
VirtualSurfaceImageSource
For very large surfaces that are too           Object
                                                 DependencyObject
large to fit in video memory as a single           ImageSource
texture                                               SurfaceImageSource
                                                        VirtualSurfaceImageSource
• Larger than 1 screen size
• Virtualization via tiles
• Automatically manages tile cache
• XAML requests tiles from the app when they

                                               XAML
  come into view                                              DirectX
• Use for mapping scenarios, large document
  canvases
VirtualSurfaceImageSource
interface IVirtualSurfaceImageSourceNative: ISurfaceImageSourceNative
{
      RegisterForUpdatesNeeded(IVirtualSurfaceUpdatesCallbackNative *pCallback);
      GetUpdateRects(RECT *pUpdates, DWORD count);
      GetUpdateRectCount(DWORD *pCount);
      Invalidate(RECT updateRect);
};

interface IVirtualSurfaceUpdatesCallbackNative: IUnknown
{
      UpdatesNeeded();
};
How can I use VirtualSurfaceImageSource?
 1. Implement IVirtualSurfaceImageSourceCallbackNative interface
 2. Call RegisterForUpdatesNeeded on VSIS
 3. In UpdatesNeeded callback, call GetUpdateRectCount and
    GetUpdateRects
 4. Render the update rectangles (using BeginDraw/EndDraw method as
    per SurfaceImageSource)
DirectX interop options in XAML
SwapChainBackgroundPanel, SwapChainPanel
DirectX
SwapChainBackgroundPanel
Allows 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

Designed for a specific scenario
• Must be background element
• Must be full-window sized
New for 8.1 - SwapChainPanel
 Benefits 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
SwapChainPanel/SwapChainBackgroundPanel
    interface ISwapChainBackgroundPanelNative: IUnknown
    {
         SetSwapChain(IDXGISwapChain *pSwapChain);
    };

interface ISwapChainPanelNative: IUnknown
{
         SetSwapChain(IDXGISwapChain *pSwapChain);
};
                                                     (Look familiar?)
Demo
SwapChainBackgroundPanel
When should I use DirectX interop APIs?
 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 rate

 SwapChainPanel or SwapChainBackgroundPanel when:
 • Your content is updating quickly (e.g. games, ink)
 • You want the lowest presentation latency possible
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

You can target both Windows 8 and Windows
Phone 8 apps.
Resources
Samples
• SwapChainPanel - http://code.msdn.microsoft.com/windowsapps/XAML-SwapChainPanel-
  00cb688b
• SurfaceImageSource - http://code.msdn.microsoft.com/windowsapps/XAML-
  SurfaceImageSource-85773f74

 http://msdn.microsoft.com/en-us/library/windows/apps/hh825871.aspx
                           http://channel9.msdn.com/coding4fun/articles/Getting-started-with-
 Cinder-for-Windows-Store-Apps
                      http://isocpp.org/std/the-committee
                                                   http://dx.codeplex.com
                                   http://bit.ly/14pyw5T http://bit.ly/16Jryep
 http://bit.ly/1bzRVqW

 http://visualstudiogallery.msdn.microsoft.com/4100e9a9-2af1-44b0-af37-29bb32ee2c03
Contact:
sridhar.poduri@microsoft.com
http://twitter.com/sridharpoduri
http://sridharpoduri.com
Questions?
You can also read