6

我正在寻找一个外部应用程序来监视 DirectX 应用程序的“FPS”(例如没有记录的 FRAPS)。我已经阅读了几篇关于性能测量工具的 Microsoft 文章- 但我希望获得社区的反馈(和经验)。

我的问题:获取 DirectX 应用程序的 FPS 的最佳方法是什么?

4

2 回答 2

5

Windows 为与 DirectX 分析相关的 Windows 提供程序提供了一些事件跟踪。最有趣的是Microsoft-Windows-D3D9Microsoft-Windows-DXGI,它允许跟踪帧呈现事件。计算 FPS 的最简单方法是计算某个时间间隔内 PresentStart 事件的数量,然后除以该时间间隔的长度。

要在 C# 中使用 ETW,请安装Microsoft.Diagnostics.Tracing.TraceEvent包。

以下代码示例显示正在运行的进程的 FPS:

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;
using Microsoft.Diagnostics.Tracing.Session;

namespace ConsoleApp1
{
    //helper class to store frame timestamps
    public class TimestampCollection
    {
        const int MAXNUM = 1000;

        public string Name { get; set; }

        List<long> timestamps = new List<long>(MAXNUM + 1);
        object sync = new object();

        //add value to the collection
        public void Add(long timestamp)
        {
            lock (sync)
            {
                timestamps.Add(timestamp);
                if (timestamps.Count > MAXNUM) timestamps.RemoveAt(0);
            }
        }

        //get the number of timestamps withing interval
        public int QueryCount(long from, long to)
        {
            int c = 0;

            lock (sync)
            {
                foreach (var ts in timestamps)
                {
                    if (ts >= from && ts <= to) c++;
                }
            }
            return c;
        }
    }

    class Program
    {
        //event codes (https://github.com/GameTechDev/PresentMon/blob/40ee99f437bc1061a27a2fc16a8993ee8ce4ebb5/PresentData/PresentMonTraceConsumer.cpp)
        public const int EventID_D3D9PresentStart = 1;
        public const int EventID_DxgiPresentStart = 42;

        //ETW provider codes
        public static readonly Guid DXGI_provider = Guid.Parse("{CA11C036-0102-4A2D-A6AD-F03CFED5D3C9}");
        public static readonly Guid D3D9_provider = Guid.Parse("{783ACA0A-790E-4D7F-8451-AA850511C6B9}");

        static TraceEventSession m_EtwSession;
        static Dictionary<int, TimestampCollection> frames = new Dictionary<int, TimestampCollection>();       
        static Stopwatch watch = null;
        static object sync = new object();

        static void EtwThreadProc()
        {            
            //start tracing
            m_EtwSession.Source.Process();
        }

        static void OutputThreadProc()
        {
            //console output loop
            while (true)
            {    
                long t1, t2;
                long dt = 2000;
                Console.Clear();
                Console.WriteLine(DateTime.Now.ToString() + "." + DateTime.Now.Millisecond.ToString());
                Console.WriteLine();

                lock (sync)
                {
                    t2 = watch.ElapsedMilliseconds;
                    t1 = t2 - dt;

                    foreach (var x in frames.Values)
                    {
                        Console.Write(x.Name + ": ");   

                        //get the number of frames
                        int count = x.QueryCount(t1, t2);

                        //calculate FPS
                        Console.WriteLine("{0} FPS", (double)count / dt * 1000.0);
                    }
                }

                Console.WriteLine();
                Console.WriteLine("Press any key to stop tracing...");
                Thread.Sleep(1000);
            }
        }

        public static void Main(string[] argv)
        {
            //create ETW session and register providers
            m_EtwSession = new TraceEventSession("mysess");
            m_EtwSession.StopOnDispose = true;
            m_EtwSession.EnableProvider("Microsoft-Windows-D3D9");
            m_EtwSession.EnableProvider("Microsoft-Windows-DXGI");

            //handle event
            m_EtwSession.Source.AllEvents += data =>
            {
                //filter out frame presentation events
                if (((int)data.ID == EventID_D3D9PresentStart && data.ProviderGuid == D3D9_provider) ||
                ((int)data.ID == EventID_DxgiPresentStart && data.ProviderGuid == DXGI_provider))
                {
                    int pid = data.ProcessID;
                    long t;

                    lock (sync)
                    {
                        t = watch.ElapsedMilliseconds; 

                        //if process is not yet in Dictionary, add it
                        if (!frames.ContainsKey(pid))
                        {
                            frames[pid] = new TimestampCollection();

                            string name = "";
                            var proc = Process.GetProcessById(pid);
                            if (proc != null)
                            {
                                using (proc)
                                {
                                    name = proc.ProcessName;
                                }
                            }
                            else name = pid.ToString();

                            frames[pid].Name = name;
                        }

                        //store frame timestamp in collection
                        frames[pid].Add(t);
                    }
                }
            };

            watch = new Stopwatch();
            watch.Start();            

            Thread thETW = new Thread(EtwThreadProc);
            thETW.IsBackground = true;
            thETW.Start();

            Thread thOutput = new Thread(OutputThreadProc);
            thOutput.IsBackground = true;
            thOutput.Start();

            Console.ReadKey();
            m_EtwSession.Dispose();
        }
    }
}

基于PresentMon项目的源代码。

于 2019-02-11T07:43:56.900 回答
2

Fraps 将一个 DLL 插入到每个正在运行的应用程序中,并挂钩特定的 DX 调用以计算帧速率和捕获视频,很确定您将不得不做类似的事情。经过一番摸索后,我发现了一个 Github 项目,它可以进行一些基本的 DX 挂钩以进行捕获和叠加,因此这可能是一个很好的起点。虽然我没有亲自使用过,所以我不能完全保证质量。

http://spazzarama.com/2011/03/14/c-screen-capture-and-overlays-for-direct3d-9-10-and-11-using-api-hooks/

于 2013-09-12T15:22:41.290 回答