I want to Understand the Rendering Modes in WPF application.

Vishal2 Bansal 145 Reputation points
2024-10-13T17:20:30.0533333+00:00

Recently i came across this section of code on Stackoverflow.

void myClass_SourceInitialized(object sender, EventArgs e)

    {

        Log.Info("myClass_SourceInitialized");

        System.Windows.Interop.HwndSource source = System.Windows.Interop.HwndSource.FromHwnd(new System.Windows.Interop.WindowInteropHelper(this).Handle);

        source.AddHook(new System.Windows.Interop.HwndSourceHook(WndProc));

        var ss = source.CompositionTarget;

        ss.RenderMode = RenderMode.Default;

        Log.Info(ss.TransformFromDevice.ToString());

    }

To my surprise if i set Render mode to RenderMode.SoftwareOnly my application lags but with default mode it runs perfectly. On Stack overflow i saw some answers that default mode is used to avoid the DUCE error. So can you briefly explain it.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,944 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,788 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,054 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hongrui Yu-MSFT 3,015 Reputation points Microsoft Vendor
    2024-10-14T03:34:46.9266667+00:00

    Hi,@Vishal2 Bansal. Welcome to Microsoft Q&A. 

    WPF's internal architecture has two rendering pipelines, hardware and software.

     

    Hardware rendering pipeline: Use hardware (GPU) accelerated rendering. RenderMode.Default, WPF renders in hardware if possible; otherwise in software.

     

    Software rendering pipeline: Rendering using CPU only. RenderMode.SoftwareOnly, WPF renders only in software.

     

    Related Documents: Optimizing Performance: Taking Advantage of Hardware - WPF .NET Framework | Microsoft Learn

     

    Reasons for program lag: When only CPU rendering is used without GPU acceleration, the rendering speed is very slow because the CPU rendering efficiency is not high

     

    
    System.Windows.Media.Composition.DUCE.Channel.SyncFlush()
    
    

    Reasons why switching rendering mode could handle the above errors: 1. When the CPU rendering capacity is not enough to meet the demand (memory), hardware (GPU) could be used for accelerated rendering. 2. Switching rendering mode could detect whether there is a problem with the driver.

     

    A more detailed explanation: WPF render thread failures - .NET Framework | Microsoft Learn

     


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.