Skip to content

Documentation gives incorrect information re. Swapchains. #120

@Gavin-Williams

Description

@Gavin-Williams

The documentation says ...

Swap chains must run on the main UI thread. This is usually accomplished by calling SetSwapChain on a reference that was initialized as a XAML object element.

That's worded incorrectly. The swap-chain is constructed from the Ui thread. But it probably won't be running on the UI thread. Typically the swap-chain will be running (worked upon) from a another engine thread or even a tertiary graphics thread.

Further, the documentation goes on to discuss the independent input source technique later down the page...

Processing input on background threads
Using the xref:Microsoft.UI.Xaml.Controls.SwapChainPanel.CreateCoreIndependentInputSource%2A method, apps can process input and render to a SwapChainPanel entirely on one or more background threads. This enables high performance input and rendering independent of the XAML UI thread.

This is also worded incorrectly. That technique will help with input, because hi-rate input will saturate the thread, but it's not required to enable rendering on a background thread. You should already have graphics commands running on a dedicated thread using something like this...

WorkItemHandler workHandler = new WorkItemHandler((IAsyncAction action) =>
            {
                while (action.Status == AsyncStatus.Started && State == EngineState.Running)
                {
                    Update?.Invoke();
                    if (!Gfx.IsRunning)
                        continue;
                    Render?.Invoke();
                }
                Engine.PreShutdown?.Invoke();
                Engine.Shutdown();
            });
            IAsyncAction mainLoopWorker = ThreadPool.RunAsync(
                workHandler,
                WorkItemPriority.High,
                WorkItemOptions.TimeSliced);

I think that wording should be changed to more correctly describe the likely situation. I would suggest something like the following, combining the discussion into one section. I think maybe even this needs a bit of work and extending, but I think it better describes the issues and solutions.

Swap chains can be bound to the Xaml SwapChainPanel by calling SetSwapChain on the native panel interface of the control. Once bound together, Swap chains can run on the main UI thread, but for performance reasons, and to avoid jitters due to the xaml engine competing for cpu cycles are typically run on a dedicated thread. Also consider that processing input on the same thread that is updating a swap chain can also lead to performance issues, due to the high rate of input events coming in from the mouse. To overcome performance issues due to input events, consider using the xref:Microsoft.UI.Xaml.Controls.SwapChainPanel.CreateCoreIndependentInputSource%2A method, which allows apps to process input events related to the SwapChainPanel entirely on one or more background threads. This enables high performance input and rendering, independent from one another and also independent from the XAML UI thread.

Metadata

Metadata

Labels

Pri3Topic is in the bottom 50% of page views for the repo or was not created via Feedback control.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions