dotnet / dotnet/wpf

WPF app with D3D texture is using too much CPU when in background

Open
#9,387 0 comments 0 reactions 0 assignees View on GitHub
Investigate
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

_This issue has been moved from [a ticket on Developer Community](https://developercommunity.visualstudio.com/t/WPF-app-with-D3D-texture-is-using-too-mu/10191120)._

---
I have a native app that renders some image into a shared texture 60 frames per second. I also have a WPF app that gets a handle to this texture on launch and assigns it to a D3DImage control as explained [here](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/walkthrough-hosting-direct3d9-content-in-wpf?view=netframeworkdesktop-4.8). This allows me to see the image in my WPF app and it seems to work OK.

When not the active window, my WPF app consumes ~2% CPU.
When it becomes the foreground window, the CPU usage goes up to ~14%.

My app does not execute any code that is specific to it being in the foreground. CPU samples from the trace also don't contain any frames from my code.

I have acquired some traces. For some reason, releasing a texture seems to be taking the most time.
In the attached etl file,
- The first 9 seconds was recorded when the app was in the background.
- Then the app is brought to foreground until the 17th second.
- App loses its focus again after 17th second.

CPU usage
![a](https://github.com/user-attachments/assets/f8fb6700-b3bf-47b3-912f-5b313622a96a)

Call tree of heaviest frames
![b](https://github.com/user-attachments/assets/f2fd1671-319e-46dd-b7a8-2011a96cb80c)

Pretty much the same code that you can find in the documentation is running to set the pixels dirty (OnRender callback isn't doing anything as you can see from the traces):
![c](https://github.com/user-attachments/assets/8045cad6-dc8c-46d0-b144-9559adf7f2ef)

When the D3DImage visibility is set to "Collapsed", the issue disappears.
When I update the above code to render less frequently, the issue becomes less severe.
What can I do to mitigate this performance issue? I want the app to perform just as fast in the foreground as it is in the background.

---
### Original Comments

#### Feedback Bot on 11/7/2022, 08:24 AM:

(private comment, text removed)
#### Dolly Wang [MSFT] on 11/8/2022, 01:46 PM:

(private comment, text removed)
#### Bekir Ozturk on 11/20/2022, 08:24 PM:

(private comment, text removed)
#### Bekir Ozturk on 11/23/2022, 01:46 AM:

(private comment, text removed)
#### Feedback Bot on 11/16/2022, 02:31 PM:

(private comment, text removed)
#### Feedback Bot on 11/23/2022, 07:40 AM:

(private comment, text removed)
#### Feedback Bot on 1/11/2023, 09:22 PM:

(private comment, text removed)
#### Feedback Bot on 3/2/2023, 01:55 PM:

(private comment, text removed)

---
### Original Solutions
(no solutions)

---
### Repro
[D3DImagePerfSample.zip](https://github.com/user-attachments/files/16191050/D3DImagePerfSample.zip)

Contributor guide

Open the contributing guide

Research direction

Start by running D3DImagePerfSample.zip and comparing foreground and background CPU usage; inspect the D3DImage update path and OnRender callback while reviewing the linked Direct3D9/WPF walkthrough. Use the supplied ETL traces to identify the costly texture-release path and document a mitigation that keeps foreground CPU near background levels.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.