locked
How are the DirectX - XAML - interop rules exactly meant?

    Question

  • In another question/discussion (Link), the DirectX - XAML - interop rules were mentioned:

    http://msdn.microsoft.com/en-us/library/hh825871.aspx

    I have two questions regarding two rules:

    > There is only one SwapChainBackgroundPanel instance per app.

    Does that mean

    1) You can only create one instance of the SwapChainBackgroundPanel and then must keep it.   OR

    2) You cannot have more than one instance at the same time (but you could destroy one instance and then create another one)

    > The SwapChainBackgroundPanel must be the root XAML element of the app.

    Does that mean

    1) The SwapChainBackgroundPanel must really be the root element of the XAML   OR

    2) The SwapChainBackgroundPanel must only be the root element of the _Visual_ tree.

    So, is it possible to say:

    <Page>
        <SwapChainBackgroundPanel>
            ...
        </SwapChainBackgroundPanel>
    </Page>
    or is it not allowed to nest SwapChainBackgroundPanel inside of Page?
    Thursday, March 21, 2013 7:14 AM

Answers

  • DirectX resources are not automatically garbage collected so the memory will not be automatically freed.  If you're using ComPtr then it should be freed once you've removed the last reference to an object.  You'll need to track down anything you're still keeping a reference to per standard C++ practices.

    Regarding your other thread - it might help to just use the same two pages, rather than recreating them each time.  If you're creating a new swap chain every time and never releasing it, memory will grow quickly.

    • Proposed as answer by MikeBMcL Wednesday, April 17, 2013 3:10 PM
    • Marked as answer by eikuh Friday, April 26, 2013 5:06 PM
    Tuesday, April 16, 2013 11:55 PM

All replies

  • Hi,

    First question, keep one instance of swapchainbackground panel.

    Second question,  SwapChainBackgroundPanel must only be the root element of visual tree.

    Here is a good sample codes to answer your question, please check
    http://code.msdn.microsoft.com/windowsapps/Direct2D-lighting-effects-e0801da3/

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by eikuh Friday, March 22, 2013 7:06 AM
    • Unmarked as answer by eikuh Thursday, April 18, 2013 6:15 AM
    Friday, March 22, 2013 5:55 AM
    Moderator
  • Thank you, Jesse, for your reply!

    Ok, I did understand the first rule wrong. So I am going to redesign my app.

    I think the article http://msdn.microsoft.com/en-us/library/hh825871.aspx should be written a little bit more clearer. (My app had one Page with a SwapChainBackgroundPanel for each game level. I thought it is possible if I have only one Page at the same time. But it seems I will have to have only one single Page instance and re-use it for each game level)

    Friday, March 22, 2013 7:09 AM
  • Actually, it's okay to have multiple SwapChainBackgroundPanels. The restriction is that only one of them can be in the active page on screen at a time.  The only issue to be aware of is that the other ones will obviously continue to use extra memory until they're freed.

    Also, it's actually fine to nest SwapChainBackgroundPanel in a Page, e.g.

    <Page>
        <SwapChainBackgroundPanel>
            ...
        </SwapChainBackgroundPanel>
    </Page>
    In fact, this is the recommended pattern which is used in sample apps and the Visual Studio template since a Page is required to use VisualStateManager and AppBars.


    Friday, April 12, 2013 8:05 PM
  • Hi Jesse Bishop,

    Thank you for your reply! Ok, you are saying the opposite of Jesse Jiang. You say that one must free the instances. The question is: How?

    Could you please take a look at this thread:

    http://social.msdn.microsoft.com/Forums/en-US/wingameswithdirectx/thread/781ed1a5-2b86-4e61-91b1-52506e769a5f

    There, I am describing how memory is increasing with multiple instances which are freed by the garbage collector but somehow not totally.

    Sunday, April 14, 2013 2:24 PM
  • DirectX resources are not automatically garbage collected so the memory will not be automatically freed.  If you're using ComPtr then it should be freed once you've removed the last reference to an object.  You'll need to track down anything you're still keeping a reference to per standard C++ practices.

    Regarding your other thread - it might help to just use the same two pages, rather than recreating them each time.  If you're creating a new swap chain every time and never releasing it, memory will grow quickly.

    • Proposed as answer by MikeBMcL Wednesday, April 17, 2013 3:10 PM
    • Marked as answer by eikuh Friday, April 26, 2013 5:06 PM
    Tuesday, April 16, 2013 11:55 PM
  • Thank you!

    OK, keeping the DirectX page and re-using it, is a good idea.

    Of course, my program would be far cleaner if I could create a brand new page everytime.

    I always use ComPtr. It seems there have to be some additional DirectX calls to release everything. But which calls?

    I ask this in a new tread.

    Friday, April 26, 2013 5:22 PM